Exemplo n.º 1
0
 /// <summary>
 /// Updates the lists of Cultivar and Field names in the model.
 /// This is used when the model has been changed. For example, when a
 /// new crop has been selecled.
 /// </summary>
 /// <param name="model">The new model</param>
 public void UpdateModel(Model model)
 {
     this.model = model;
     if (this.model != null)
     {
         IGridCell curCell = this.grid.GetCurrentCell;
         for (int i = 0; i < this.properties.Count; i++)
         {
             IGridCell cell = this.grid.GetCell(1, i);
             if (cell.RowIndex == curCell.RowIndex && cell.ColumnIndex == curCell.ColumnIndex)
             {
                 continue;
             }
             if (this.properties[i].DisplayType == DisplayAttribute.DisplayTypeEnum.CultivarName)
             {
                 ICrop crop = GetCrop(properties);
                 if (crop != null)
                 {
                     cell.DropDownStrings = GetCultivarNames(crop);
                 }
             }
             else if (this.properties[i].DisplayType == DisplayAttribute.DisplayTypeEnum.FieldName)
             {
                 string[] fieldNames = GetFieldNames();
                 if (fieldNames != null)
                 {
                     cell.DropDownStrings = fieldNames;
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 public SmartObjectLayer(SubPattern pattern, string name, System.Drawing.Bitmap bitmap, int x, int y, int width, int height) : base(pattern, name)
 {
     Crop          = Crops[0];
     Resampler     = Resamplers[0];
     Bitmap        = bitmap;
     _ObjectX      = x;
     _ObjectY      = y;
     _ObjectWidth  = width;
     _ObjectHeight = height;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Go find a crop property in the specified list of properties or if not
        /// found, find the first crop in scope.
        /// </summary>
        /// <param name="properties">The list of properties to look through.</param>
        /// <returns>The found crop or null if none found.</returns>
        private ICrop GetCrop(List <VariableProperty> properties)
        {
            foreach (VariableProperty property in properties)
            {
                if (property.DataType == typeof(ICrop))
                {
                    ICrop plant = property.Value as ICrop;
                    if (plant != null)
                    {
                        return(plant);
                    }
                }
            }

            // Not found so look for one in scope.
            return(Apsim.Find(this.model, typeof(ICrop)) as ICrop);
        }
Exemplo n.º 4
0
        /// <summary>Get a list of cultivars for crop.</summary>
        /// <param name="crop">The crop.</param>
        /// <returns>A list of cultivars.</returns>
        private string[] GetCultivarNames(ICrop crop)
        {
            if (crop.CultivarNames.Length == 0)
            {
                Simulations  simulations  = Apsim.Parent(crop as IModel, typeof(Simulations)) as Simulations;
                Replacements replacements = Apsim.Child(simulations, typeof(Replacements)) as Replacements;
                if (replacements != null)
                {
                    ICrop replacementCrop = Apsim.Child(replacements, (crop as IModel).Name) as ICrop;
                    if (replacementCrop != null)
                    {
                        return(replacementCrop.CultivarNames);
                    }
                }
            }
            else
            {
                return(crop.CultivarNames);
            }

            return(new string[0]);
        }
Exemplo n.º 5
0
        /// <summary>Get a list of cultivars for crop.</summary>
        /// <param name="crop">The crop.</param>
        /// <returns>A list of cultivars.</returns>
        private string[] GetCultivarNames(ICrop crop)
        {
            if (crop.CultivarNames.Length == 0)
            {
                Simulations simulations = Apsim.Parent(crop as IModel, typeof(Simulations)) as Simulations;
                Replacements replacements = Apsim.Child(simulations, typeof(Replacements)) as Replacements;
                if (replacements != null)
                {
                    ICrop replacementCrop = Apsim.Child(replacements, (crop as IModel).Name) as ICrop;
                    if (replacementCrop != null)
                        return replacementCrop.CultivarNames;
                }
            }
            else
                return crop.CultivarNames;

            return new string[0];
        }
Exemplo n.º 6
0
        /// <summary>
        /// Format the grid.
        /// </summary>
        private void FormatGrid()
        {
            for (int i = 0; i < this.properties.Count; i++)
            {
                IGridCell cell = this.grid.GetCell(1, i);

                if (this.properties[i].DisplayType == DisplayAttribute.DisplayTypeEnum.TableName)
                {
                    DataStore dataStore = new DataStore(this.model);
                    cell.EditorType      = EditorTypeEnum.DropDown;
                    cell.DropDownStrings = dataStore.TableNames;
                    dataStore.Disconnect();
                }
                else if (this.properties[i].DisplayType == DisplayAttribute.DisplayTypeEnum.CultivarName)
                {
                    cell.EditorType = EditorTypeEnum.DropDown;
                    ICrop crop = GetCrop(properties);
                    if (crop != null)
                    {
                        cell.DropDownStrings = crop.CultivarNames;
                    }
                }
                else
                {
                    object cellValue = this.properties[i].ValueWithArrayHandling;
                    if (cellValue is DateTime)
                    {
                        cell.EditorType = EditorTypeEnum.DateTime;
                    }
                    else if (cellValue is bool)
                    {
                        cell.EditorType = EditorTypeEnum.Boolean;
                    }
                    else if (cellValue.GetType().IsEnum)
                    {
                        cell.EditorType      = EditorTypeEnum.DropDown;
                        cell.DropDownStrings = StringUtilities.EnumToStrings(cellValue);
                    }
                    else if (cellValue.GetType() == typeof(ICrop))
                    {
                        cell.EditorType = EditorTypeEnum.DropDown;
                        List <string> cropNames = new List <string>();
                        foreach (Model crop in Apsim.FindAll(this.model, typeof(ICrop)))
                        {
                            cropNames.Add(crop.Name);
                        }
                        cell.DropDownStrings = cropNames.ToArray();
                    }
                    else if (this.properties[i].DataType == typeof(ICrop))
                    {
                        List <string> plantNames = Apsim.FindAll(this.model, typeof(ICrop)).Select(m => m.Name).ToList();
                        cell.EditorType      = EditorTypeEnum.DropDown;
                        cell.DropDownStrings = plantNames.ToArray();
                    }
                    else
                    {
                        cell.EditorType = EditorTypeEnum.TextBox;
                    }
                }
            }

            IGridColumn descriptionColumn = this.grid.GetColumn(0);

            descriptionColumn.Width    = -1;
            descriptionColumn.ReadOnly = true;

            IGridColumn valueColumn = this.grid.GetColumn(1);

            valueColumn.Width = -1;
        }
Exemplo n.º 7
0
 public void ChangeCrop(int num)
 {
     Crop = Crops[num];
 }
Exemplo n.º 8
0
    public void ParsePattern(ICrop crop, ISampling sampling, IColorQuantizer quantizer, IColorCache colorCache)
    {
        if (!this.IsParsing)
        {
            IsParsing = true;
            Thread thread = new Thread(() =>
            {
                var bmp = new Bitmap((System.Drawing.Image)Image.Clone());

                int desiredWidth  = 32;
                int desiredHeight = 32;
                if (crop != null)
                {
                    crop.SetImage(bmp);
                    desiredWidth  = crop.GetWidth();
                    desiredHeight = crop.GetHeight();
                }

                if (quantizer is BaseColorCacheQuantizer colorCacheQuantizer)
                {
                    colorCacheQuantizer.ChangeCacheProvider(colorCache);
                }

                var sampledBmp = sampling.Resample(bmp, desiredWidth, desiredHeight);
                bmp.Dispose();
                bmp = sampledBmp;

                Bitmap croppedBmp = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(croppedBmp))
                {
                    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    graphics.DrawImage(bmp, (32 - bmp.Width) / 2, (32 - bmp.Height) / 2, bmp.Width, bmp.Height);
                }

                bmp.Dispose();
                bmp = croppedBmp;

                var transparentPixels = new bool[bmp.Width * bmp.Height];
                for (var y = 0; y < bmp.Height; y++)
                {
                    for (var x = 0; x < bmp.Width; x++)
                    {
                        transparentPixels[x + y * bmp.Width] = bmp.GetPixel(x, y).A != 255;
                    }
                }
                var targetImage = ImageBuffer.QuantizeImage(bmp, quantizer, null, 15, 1);

                bmp.Dispose();
                bmp = new Bitmap(targetImage);
                for (var y = 0; y < bmp.Height; y++)
                {
                    for (var x = 0; x < bmp.Width; x++)
                    {
                        if (transparentPixels[x + y * bmp.Width])
                        {
                            bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(0, 0, 0, 0));
                        }
                    }
                }
                Result    = bmp;
                IsReady   = true;
                IsParsing = false;
            });
            thread.Start();
        }
    }