public void UnionGroupsImageParams(Group group, List <PngUtil.MergeImageParams> ps)
        {
            if (!GroupLayerCache.ContainsKey(group.Sceneid))
            {
                GroupLayerCache[group.Sceneid] = new SortedDictionary <int, GroupCache>();
            }

            if (ps == null)
            {
                GroupLayerCache[group.Sceneid].Remove(group.LayerIndex);
            }
            else
            {
                GroupLayerCache[group.Sceneid][group.LayerIndex] = new GroupCache
                {
                    GroupLayerId          = group.LayerIndex,
                    GroupSize             = group.Size,
                    GroupPropertiesImages = ps,
                };
            }

            if (GroupLayerCache[group.Sceneid].Count == 0)
            {
                UpdateCenterBoard(group.Sceneid, null);
            }
            else
            {
                List <PngUtil.MergeImageParams> resultList = null;
                GroupSize maxGroupSize = null;
                foreach (GroupCache groupCache in GroupLayerCache[group.Sceneid].Values)
                {
                    if (maxGroupSize == null || groupCache.GroupSize.Width > maxGroupSize.Width)
                    {
                        maxGroupSize = groupCache.GroupSize;
                    }
                    if (resultList == null)
                    {
                        resultList = groupCache.GroupPropertiesImages;
                    }
                    else
                    {
                        resultList = resultList.Union(groupCache.GroupPropertiesImages).ToList();
                    }
                }
                UpdateCenterBoard(group.Sceneid, PngUtil.MergeImageList(resultList, maxGroupSize.Width, maxGroupSize.Height));
            }
        }
Пример #2
0
        private static void LoadImageProperty(TableLayoutPanel tabPanel, Property property, int index, int maxIndex, int propertyHeight)
        {
            Panel panel = new Panel
            {
                Dock    = DockStyle.Fill,
                Margin  = new Padding(0, 0, 0, 0),
                Padding = new Padding(0, 0, 0, 0),
            };

            tabPanel.Controls.Add(panel, 0, index);

            PictureBox cachedPb;
            Image      image;

            if (property.RefPropertyId > 0)
            {
                cachedPb = GlobalConfig.Controller.GetPictureBox(property.RefPropertyId.ToString());
                if (cachedPb != null)
                {
                    image = cachedPb.Image;
                }
                else
                {
                    Log.Error("Flow1_FlowPanel", "LoadImageProperty", string.Format("property id={0} Get picture box nil", property.Id));
                    image = Image.FromFile(Path.Combine(GlobalConfig.Project.GetUserResourcesDir(), property.Value));
                }
            }
            else
            {
                image = Image.FromFile(Path.Combine(GlobalConfig.Project.GetUserResourcesDir(), property.Value));
            }

            PictureBox pictureBox = new PictureBox
            {
                Name        = property.GetPictureBoxId(),
                Size        = new Size(0, 0),
                Margin      = new Padding(0, 0, 0, 0),
                Padding     = new Padding(0, 0, 0, 0),
                Image       = image,
                SizeMode    = PictureBoxSizeMode.Zoom,
                BorderStyle = BorderStyle.FixedSingle,
                Location    = new Point(0, 0),
                BackColor   = Color.Gray,
                Visible     = false,
            };

            panel.Controls.Add(pictureBox);

            SortedDictionary <int, Property> brothers = GlobalConfig.Project.CarConfig.GetGroupSameLayerProperties(property.GroupId, property);

            if (brothers.Count > 1) //包含自己所以大于1
            {
                CheckBox checkBox = new CheckBox
                {
                    Name    = property.GetCheckBoxId(),
                    Margin  = new Padding(0, 0, 0, 0),
                    Padding = new Padding(0, 0, 0, 0),
                    Checked = brothers.FirstOrDefault().Key == property.Id,
                    Width   = 13,
                    Text    = "",
                };
                if (!checkBox.Checked)
                {
                    pictureBox.Enabled = false;
                }
                checkBox.CheckedChanged += new System.EventHandler(delegate(object sender, EventArgs e)
                {
                    if (checkBox.Checked)
                    {
                        pictureBox.Enabled = true;
                        foreach (Property brotherProperty in brothers.Values)
                        {
                            if (brotherProperty.GetCheckBoxId() == checkBox.Name)
                            {
                                continue;
                            }

                            Control[] pbControls = tabPanel.Controls.Find(brotherProperty.GetPictureBoxId(), true);
                            foreach (PictureBox pb in pbControls)
                            {
                                pb.Enabled = false;
                            }

                            Control[] ckControls = tabPanel.Controls.Find(brotherProperty.GetCheckBoxId(), true);
                            foreach (CheckBox ck in ckControls)
                            {
                                ck.Checked = false;
                            }
                        }
                    }
                    else
                    {
                        pictureBox.Enabled = false;
                    }

                    GlobalConfig.Controller.ShowGroupOnCenterBoard(tabPanel, property.GetGroup());
                });
                panel.Controls.Add(checkBox);
            }

            //每个Property的PictureBox都先注册到缓存中, 当有Property需要引用其他Property的图片时,直接取出
            GlobalConfig.Controller.SetPictureBox(property.Id.ToString(), pictureBox);

            if (!property.ShowLabel)
            {
                tabPanel.Controls.Add(pictureBox, 0, 0);  //若property不现实标签, 则直接挂载到table的第0行中
                return;
            }

            tabPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, propertyHeight));

            Label label = new Label
            {
                Text      = property.Name,
                Location  = new Point(14, 0),
                Margin    = new Padding(0, 0, 0, 0),
                Padding   = new Padding(0, 0, 0, 0),
                Font      = new Font("微软雅黑", 10F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                Height    = 20,
                Width     = 180,
                TextAlign = ContentAlignment.MiddleLeft,
            };

            panel.Controls.Add(label);

            Button button = new Button
            {
                Location  = new Point(panel.Width - 100, 0),
                Width     = 100,
                Height    = 26,
                Font      = new Font("微软雅黑", 11F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                TextAlign = ContentAlignment.MiddleCenter
            };

            if (property.OptType == PropertyOperateType.ReplaceImage)
            {
                button.Text   = "更换图片";
                button.Click += new EventHandler(delegate(object _, EventArgs b)
                {
                    try
                    {
                        OpenFileDialog openFileDialog = new OpenFileDialog
                        {
                            Filter = "Png|*.png|Jpg|*.jpg"
                        };
                        if (openFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            pictureBox.Image = Image.FromFile(openFileDialog.FileName);
                            pictureBox.Image.Save(Path.Combine(GlobalConfig.Project.GetUserUploadImageDir(), openFileDialog.SafeFileName));;
                            pictureBox.Refresh();

                            if (openFileDialog.FileName != property.Value)
                            {
                                Property propertyCopy = property.Clone();
                                propertyCopy.Value    = GlobalConfig.Project.GetPropertyImagePath(openFileDialog.SafeFileName);
                                GlobalConfig.Project.Editer.Add(property.Id, propertyCopy);
                            }
                            else
                            {
                                GlobalConfig.Project.Editer.Remove(property.Id);
                            }
                        }
                        GlobalConfig.Controller.ShowGroupOnCenterBoard(tabPanel, property.GetGroup());
                    }
                    catch (Exception)
                    { }
                });
                panel.Controls.Add(button);
            }
            else if (property.OptType == PropertyOperateType.AlphaWhiteImageSetColor)
            {
                TextBox text = new TextBox
                {
                    Name        = property.GetTextBoxColorID(),
                    AutoSize    = false,
                    Width       = 35,
                    Height      = 25,
                    Location    = new Point(label.Width + GlobalConfig.UiConfig.PropertyLabelMargin, 0),
                    Margin      = new Padding(0, 0, 0, 0),
                    BorderStyle = BorderStyle.FixedSingle,
                };

                if (property.DefaultValue != null && property.DefaultValue.Length > 0)
                {
                    try
                    {
                        text.BackColor = Color.FromArgb(
                            Convert.ToInt32(property.DefaultValue.Substring(2, 2), 16),
                            Convert.ToInt32(property.DefaultValue.Substring(4, 2), 16),
                            Convert.ToInt32(property.DefaultValue.Substring(6, 2), 16));

                        PngUtil.SetAlphaWhilteImage((Bitmap)image, text.BackColor);
                        pictureBox.Refresh();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }

                button.Text   = "设置颜色";
                button.Click += new EventHandler(delegate(object _, EventArgs b)
                {
                    ColorDialog dialog = new ColorDialog();
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        if (dialog.Color.ToArgb() != text.BackColor.ToArgb())
                        {
                            Property propertyCopy     = property.Clone();
                            propertyCopy.DefaultValue = "0x" + dialog.Color.R.ToString("X2") + dialog.Color.G.ToString("X2") + dialog.Color.B.ToString("X2");
                            GlobalConfig.Project.Editer.Set(property.Id, propertyCopy);
                        }
                        else
                        {
                            GlobalConfig.Project.Editer.Remove(property.Id);
                        }

                        PngUtil.SetAlphaWhilteImage((Bitmap)image, dialog.Color);
                        text.BackColor = dialog.Color;
                        pictureBox.Refresh();
                    }
                    GlobalConfig.Controller.ShowGroupOnCenterBoard(tabPanel, property.GetGroup());
                });
                panel.Controls.Add(text);
                panel.Controls.Add(button);
            }
            else if (property.OptType == PropertyOperateType.AlphaWhiteImageSetAlpha)
            {
                TextBox text = new TextBox
                {
                    Name     = property.GetTextBoxAlphaID(),
                    Width    = 35,
                    Location = new Point(label.Width + GlobalConfig.UiConfig.PropertyLabelMargin, 0),
                    Height   = 20,
                    Margin   = new Padding(0, 0, 0, 0),
                    Font     = new Font("微软雅黑", 11F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                };
                if (property.DefaultValue == null || property.DefaultValue.Length == 0)
                {
                    text.Text = "0";
                }
                else
                {
                    text.Text = property.DefaultValue;
                    PngUtil.SetAlphaWhilteImage((Bitmap)image, Convert.ToInt32(property.DefaultValue));
                    pictureBox.Refresh();
                }

                string oldText = text.Text;
                button.Text   = "确定";
                button.Click += new EventHandler(delegate(object _, EventArgs b)
                {
                    if (oldText != text.Text)
                    {
                        Property propertyCopy     = property.Clone();
                        propertyCopy.DefaultValue = text.Text;
                        GlobalConfig.Project.Editer.Set(property.Id, propertyCopy);
                    }
                    else
                    {
                        GlobalConfig.Project.Editer.Remove(property.Id);
                    }
                    try
                    {
                        int alphaValue = Convert.ToInt32(text.Text);
                        PngUtil.SetAlphaWhilteImage((Bitmap)image, alphaValue);
                        pictureBox.Refresh();
                        GlobalConfig.Controller.ShowGroupOnCenterBoard(tabPanel, property.GetGroup());
                    }
                    catch (Exception)
                    { }
                });
                panel.Controls.Add(button);
                panel.Controls.Add(text);
            }
            else if (property.OptType == PropertyOperateType.ImageFilterColor)
            {
                TextBox textBox = new TextBox
                {
                    Width    = 35,
                    Location = new Point(label.Width + 20, 0),
                    Height   = 20,
                    Margin   = new Padding(0, 0, 0, 0),
                    Font     = new Font("微软雅黑", 11F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                    Text     = "0"
                };

                TrackBar trackBar = new TrackBar
                {
                    Location      = new Point(panel.Width - 150, 0),
                    Size          = new Size(150, 45),
                    Maximum       = 180,
                    Minimum       = -180,
                    Value         = 0,
                    TickFrequency = 30,
                };
                trackBar.ValueChanged += new EventHandler(delegate(object sender, EventArgs e)
                {
                    textBox.Text     = trackBar.Value.ToString();
                    pictureBox.Image = (Image)PngUtil.RelativeChangeColor((Bitmap)image, Convert.ToInt32(trackBar.Value) + 180);
                    pictureBox.Refresh();
                    GlobalConfig.Controller.ShowGroupOnCenterBoard(tabPanel, property.GetGroup());
                });
                panel.Controls.Add(textBox);
                panel.Controls.Add(trackBar);
            }
        }
        public string StartOutput(Outputs outputs, Dictionary <int, Property> propertyIdMapping, string basePath, Form_Progress progressForm)
        {
            progressForm.SetProgressMax(outputs.ImageOutputs.Length + outputs.XmlOutputs.Length + outputs.MergeImageOutputs.Length);

            foreach (ImageOutput imageOutout in outputs.ImageOutputs)
            {
                if (!propertyIdMapping.ContainsKey(imageOutout.PropertyId))
                {
                    return(Errors.PropertyIdNotExist);
                }

                Property property = propertyIdMapping[imageOutout.PropertyId];

                Control[] controls = GetSceneFlowLayoutPanel(property.SceneId).Controls.Find(property.GetPictureBoxId(), true);
                if (controls == null || controls.Length == 0)
                {
                    return(Errors.UiPictureBoxNotExist);
                }

                try
                {
                    string newFile = Path.Combine(basePath, imageOutout.Target);
                    string dirName = newFile.Substring(0, newFile.LastIndexOf('\\'));

                    if (!Directory.Exists(dirName))
                    {
                        Directory.CreateDirectory(dirName);
                    }

                    ((PictureBox)(controls[0])).Image.Save(newFile);
                }
                catch (Exception ex)
                {
                    return(ex.ToString());
                }
                progressForm.AddProgressValue(1, string.Format("组件:{0},   属性: {1} 导出完毕", property.GroupName, property.Name));
            }

            foreach (XmlOutput xmlOutout in outputs.XmlOutputs)
            {
                if (!propertyIdMapping.ContainsKey(xmlOutout.PropertyId))
                {
                    return(Errors.PropertyIdNotExist);
                }
                Property property = propertyIdMapping[xmlOutout.PropertyId];


                Control[] controls = null;
                if (property.OptType == PropertyOperateType.AlphaWhiteImageSetAlpha)
                {
                    controls = GetSceneFlowLayoutPanel(property.SceneId).Controls.Find(property.GetTextBoxAlphaID(), true);
                }
                else if (property.OptType == PropertyOperateType.AlphaWhiteImageSetColor)
                {
                    controls = GetSceneFlowLayoutPanel(property.SceneId).Controls.Find(property.GetTextBoxColorID(), true);
                }

                if (controls == null || controls.Length == 0)
                {
                    return(Errors.UiPictureBoxNotExist);
                }
                progressForm.AddProgressValue(1, string.Format("组件:{0}, 属性: {1} 导出完毕", property.GroupName, property.Name));
            }


            List <PngUtil.MergeImageParams> list = new List <PngUtil.MergeImageParams>();

            foreach (MergeImageOutput MergeImageOutPut in outputs.MergeImageOutputs)
            {
                Dictionary <int, Property> map = MergeImageOutPut.GetPropertyMap();
                foreach (int proertyId in map.Keys)
                {
                    if (!propertyIdMapping.ContainsKey(proertyId))
                    {
                        return(Errors.PropertyIdNotExist);
                    }

                    Property propertItem = propertyIdMapping[proertyId];
                    if (map.ContainsKey(propertItem.RefPropertyId))   //如果出现引用形式, 则此图不需要参与合成
                    {
                        continue;
                    }

                    Control[] controls = GetSceneFlowLayoutPanel(propertItem.SceneId).Controls.Find(propertItem.GetPictureBoxId(), true);
                    if (controls == null || controls.Length == 0)
                    {
                        return(Errors.UiPictureBoxNotExist);
                    }


                    list.Add(new PngUtil.MergeImageParams
                    {
                        Image = ((PictureBox)(controls[0])).Image,
                    });
                }
                ;

                Image image = null;
                if (list.Count == 1)
                {
                    image = list[0].Image;
                }
                else
                {
                    image = PngUtil.MergeImageList(list, list[0].Image.Width, list[0].Image.Height);
                }

                try
                {
                    string newFile = Path.Combine(basePath, MergeImageOutPut.Target);
                    string dirName = newFile.Substring(0, newFile.LastIndexOf('\\'));

                    if (!Directory.Exists(dirName))
                    {
                        Directory.CreateDirectory(dirName);
                    }

                    image.Save(newFile);
                }
                catch (Exception ex)
                {
                    return(ex.ToString());
                }

                list.Clear();
                progressForm.AddProgressValue(1, string.Format("图片合成导出完毕"));
            }

            return(null);
        }