Example #1
0
        /// <summary>
        ///   Initializes a new instance of the <see cref = "Setup" /> class.
        /// </summary>
        /// <param name = "viewModel">
        /// </param>
        public Setup(SetupViewModel viewModel)
        {
            this.InitializeComponent();
            this.setupView1.DataContext = viewModel;

            var workingArea = Screen.PrimaryScreen.WorkingArea;
            var displayWidth = viewModel.DisplayWidth == 0 ? 900 : viewModel.DisplayWidth;
            var displayHeight = viewModel.DisplayHeight == 0 ? 650 : viewModel.DisplayHeight;
            this.Width = Math.Min(workingArea.Width, displayWidth);            
            this.Height = Math.Min(workingArea.Height, displayHeight);
        }
        /// <summary>
        ///   The setup.
        /// </summary>
        public void Setup()
        {
            if (this._channels.Any())
            {
                this.LoadDataFromSetupNode();

                var viewModel = new SetupViewModel(_displayWidth, _displayHeight, _background);
                this._channels.ForEach(x => viewModel.Channels.Add(x));
                this._elements.ForEach(x => viewModel.DisplayElements.Add(x));
                bool saveData = false;
                using (this._setupDialog = new Setup(viewModel))
                {
                    this._setupDialog.ShowDialog();

                    var result = MessageBox.Show(
                                                 "Do you want to save the changes made?",
                                                 "Save Changes",
                                                 MessageBoxButton.YesNo,
                                                 MessageBoxImage.Question,
                                                 MessageBoxResult.No);
                    if (result == MessageBoxResult.Yes)
                    {
                        saveData = true;
                        this._elements.Clear();
                        this._elements.AddRange(viewModel.DisplayElements);
                        this._displayWidth = viewModel.DisplayWidth;
                        this._displayHeight = viewModel.DisplayHeight;
                        _background = viewModel.BackgroundImage;
                    }
                }

                if (saveData)
                {
                    while (this._setupNode.ChildNodes.Count > 0)
                    {
                        this._setupNode.RemoveChild(this._setupNode.ChildNodes[0]);
                    }

                    var imageBytes = _background.ToByteArray();
                    if (imageBytes.Length > 0)
                    {
                        _setupData.SetBytes(_setupNode, "BackgroundImage", imageBytes);
                    }

                    var heightAttribute = _setupNode.Attributes.GetNamedItem("displayHeight");
                    var widthAttribute = _setupNode.Attributes.GetNamedItem("displayWidth");

                    if (heightAttribute == null)
                    {
                        _setupNode.AppendAttribute("displayHeight", _displayHeight.ToString());
                    }
                    else
                    {
                        heightAttribute.Value = _displayHeight.ToString();
                    }

                    if (widthAttribute == null)
                    {
                        _setupNode.AppendAttribute("displayWidth", _displayWidth.ToString());
                    }
                    else
                    {
                        widthAttribute.Value = _displayWidth.ToString();
                    }

                    foreach (var element in this._elements)
                    {
                        var node = this._setupNode.OwnerDocument.CreateElement("DisplayElement");
                        node.AppendAttribute("Rows", element.Rows.ToString());
                        node.AppendAttribute("Columns", element.Columns.ToString());
                        node.AppendAttribute("Height", element.Height.ToString());
                        node.AppendAttribute("Width", element.Width.ToString());
                        node.AppendAttribute("LeftOffset", element.LeftOffset.ToString());
                        node.AppendAttribute("TopOffset", element.TopOffset.ToString());
                        node.AppendAttribute("Name", element.Name);
                        node.AppendAttribute("IsUnlocked", element.IsUnlocked.ToString());
                        foreach (var mappedChannel in element.PixelMappings)
                        {
                            var mappedNode = node.OwnerDocument.CreateElement("PixelMapping");

                            var pixel = mappedChannel.Pixel;
                            if (pixel != null)
                            {
                                var channelNode = mappedNode.OwnerDocument.CreateElement("Channel");
                                mappedNode.AppendChild(channelNode);
                                if (pixel is EmptyPixel)
                                {
                                    channelNode.AppendAttribute("Type", "Empty");
                                }
                                else if (pixel is SingleColorPixel)
                                {
                                    channelNode.AppendAttribute("Type", "Single");
                                    var singleColorChannel = (SingleColorPixel)pixel;
                                    var vixenChannel = singleColorChannel.Channel;
                                    channelNode.AppendAttribute(
                                                                "ChannelId",
                                                                vixenChannel == null ? string.Empty : vixenChannel.Name);
                                    channelNode.AppendAttribute("Color", singleColorChannel.DisplayColor.ToString());
                                }
                                else
                                {
                                    var rgb = pixel as RedGreenBluePixel;

                                    var redChannel = rgb.RedChannel;
                                    channelNode.AppendAttribute(
                                                                "RedChannel",
                                                                redChannel == null ? string.Empty : redChannel.Name);
                                    var greenChannel = rgb.GreenChannel;
                                    channelNode.AppendAttribute(
                                                                "GreenChannel",
                                                                greenChannel == null ? string.Empty : greenChannel.Name);
                                    var blueChannel = rgb.BlueChannel;
                                    channelNode.AppendAttribute(
                                                                "BlueChannel",
                                                                blueChannel == null ? string.Empty : blueChannel.Name);

                                    var rgbw = pixel as RedGreenBlueWhitePixel;
                                    var type = "RGB";
                                    if (rgbw != null)
                                    {
                                        var whiteChannel = rgbw.WhiteChannel;
                                        channelNode.AppendAttribute(
                                                                    "WhiteChannel",
                                                                    whiteChannel == null ? string.Empty : whiteChannel.Name);
                                        type += "W";
                                    }

                                    channelNode.AppendAttribute("Type", type);
                                }
                            }

                            node.AppendChild(mappedNode);
                        }

                        this._setupNode.AppendChild(node);
                    }
                }

                this.LoadDataFromSetupNode();
                this._setupDialog = null;
            }
            else
            {
                MessageBox.Show(
                    "There are no channels assigned to this plugin.", 
                    this.Name, 
                    MessageBoxButton.OK, 
                    MessageBoxImage.Hand);
            }
        }