Ejemplo n.º 1
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            FontManager.Instance.SetContentManager(Content);

            _uiEditor = UI.LoadJSON("UIDescription/Editor/editor.json");

            _uiEditor.SetSize(
                _graphics.PreferredBackBufferWidth,
                _graphics.PreferredBackBufferHeight
                );

            _uiEditor.Start();

            _modelEditor = new ViewModel(_uiEditor);

            //Callbacks
            _modelEditor.SetCallback("NewUI", EnumCallback.ON_VALUE_CHANGE, (sender, e) =>
            {
                var button = (UIElement <ButtonState>)sender;

                if (button.Value == ButtonState.Pressed)
                {
                    NewUI();
                }
            });

            _modelEditor.SetCallback("SaveUI", EnumCallback.ON_VALUE_CHANGE, (sender, e) =>
            {
                var button = (UIElement <ButtonState>)sender;

                if (button.Value == ButtonState.Pressed)
                {
                    SaveUI();
                }
            });

            var tree = _uiEditor.GetItem <Tree>("Tree");

            _modelEditor.SetCallback("Tree", EnumCallback.ON_VALUE_CHANGE, (sender, e) =>
            {
                var itemSelected = tree.GetItemSelected();

                if (itemSelected == "UI")
                {
                }
                else
                {
                    var parent = _uiEdit.GetItem(itemSelected);

                    LoadProperties(parent);
                }
            });

            _modelEditor.SetCallback("AddButton", EnumCallback.ON_VALUE_CHANGE, (sender, e) =>
            {
                var button       = (UIElement <ButtonState>)sender;
                var itemSelected = tree.GetItemSelected();

                if (button.Value == ButtonState.Pressed && _uiEdit != null && itemSelected != null)
                {
                    var newButton = new Button()
                    {
                        Name   = Guid.NewGuid().ToString(),
                        Width  = 100,
                        Height = 30,
                        Text   = "Button",
                        Font   = "Fonts/Arial-16",
                        Color  = Color.White
                    };

                    if (itemSelected == "UI")
                    {
                        _uiEdit.AddItem(newButton);
                        tree.AddItem(newButton.Name, itemSelected);
                    }
                    else
                    {
                        var parent = _uiEdit.GetItem(itemSelected);

                        if (parent.Type == EnumControl.CONTAINER)
                        {
                            _uiEdit.AddItem(newButton, itemSelected);
                            tree.AddItem(newButton.Name, itemSelected);
                        }
                    }
                }
            });

            _modelEditor.SetCallback("AddTextbox", EnumCallback.ON_VALUE_CHANGE, (sender, e) =>
            {
                var button       = (UIElement <ButtonState>)sender;
                var itemSelected = tree.GetItemSelected();

                if (button.Value == ButtonState.Pressed && _uiEdit != null && itemSelected != null)
                {
                    var newTextbox = new Textbox()
                    {
                        Name       = Guid.NewGuid().ToString(),
                        Width      = 140,
                        Height     = 40,
                        Text       = "Textbox",
                        Background = "Textures/Controls/blue_button13",
                        Font       = "Fonts/Arial-16",
                        Color      = Color.Black
                    };

                    if (itemSelected == "UI")
                    {
                        _uiEdit.AddItem(newTextbox);
                        tree.AddItem(newTextbox.Name, itemSelected);
                    }
                    else
                    {
                        var parent = _uiEdit.GetItem(itemSelected);

                        if (parent.Type == EnumControl.CONTAINER)
                        {
                            _uiEdit.AddItem(newTextbox, itemSelected);
                            tree.AddItem(newTextbox.Name, itemSelected);
                        }
                    }
                }
            });

            _modelEditor.SetCallback("AddProgressBar", EnumCallback.ON_VALUE_CHANGE, (sender, e) =>
            {
                var button       = (UIElement <ButtonState>)sender;
                var itemSelected = tree.GetItemSelected();

                if (button.Value == ButtonState.Pressed && _uiEdit != null && itemSelected != null)
                {
                    var newProgressBar = new ProgressBar()
                    {
                        Name            = Guid.NewGuid().ToString(),
                        Width           = 140,
                        Height          = 50,
                        Text            = "",
                        Value           = 50,
                        BackgroundColor = Color.Gray,
                        Texture         = "Textures/progressBar",
                        Font            = "Fonts/Arial-16",
                        Color           = Color.YellowGreen
                    };

                    if (itemSelected == "UI")
                    {
                        _uiEdit.AddItem(newProgressBar);
                        tree.AddItem(newProgressBar.Name, itemSelected);
                    }
                    else
                    {
                        var parent = _uiEdit.GetItem(itemSelected);

                        if (parent.Type == EnumControl.CONTAINER)
                        {
                            _uiEdit.AddItem(newProgressBar, itemSelected);
                            tree.AddItem(newProgressBar.Name, itemSelected);
                        }
                    }
                }
            });

            _modelEditor.SetCallback("AddPanel", EnumCallback.ON_VALUE_CHANGE, (sender, e) =>
            {
                var button       = (UIElement <ButtonState>)sender;
                var itemSelected = tree.GetItemSelected();

                if (button.Value == ButtonState.Pressed && _uiEdit != null && itemSelected != null)
                {
                    var newPanel = new Panel()
                    {
                        Name    = Guid.NewGuid().ToString(),
                        Width   = 140,
                        Height  = 50,
                        Texture = "Textures/Controls/grey_panel",
                        Color   = Color.Gray
                    };

                    if (itemSelected == "UI")
                    {
                        _uiEdit.AddItem(newPanel);
                        tree.AddItem(newPanel.Name, itemSelected);
                    }
                    else
                    {
                        var parent = _uiEdit.GetItem(itemSelected);

                        if (parent.Type == EnumControl.CONTAINER)
                        {
                            _uiEdit.AddItem(newPanel, itemSelected);
                            tree.AddItem(newPanel.Name, itemSelected);
                        }
                    }
                }
            });


            _modelEditor.SetCallback("SaveProperties", EnumCallback.ON_VALUE_CHANGE, (sender, e) =>
            {
                var button       = (UIElement <ButtonState>)sender;
                var itemSelected = tree.GetItemSelected();

                if (button.Value == ButtonState.Pressed && _uiEdit != null && itemSelected != null && itemSelected != "UI")
                {
                    var textboxX      = _uiEditor.GetItem <Textbox>("TextboxX");
                    var textboxY      = _uiEditor.GetItem <Textbox>("TextboxY");
                    var textboxWidth  = _uiEditor.GetItem <Textbox>("TextboxWidth");
                    var textboxHeight = _uiEditor.GetItem <Textbox>("TextboxHeight");

                    var item = _uiEdit.GetItem(itemSelected);

                    int value = (int)item.X;
                    if (int.TryParse(textboxX.Text, out value))
                    {
                        item.X = int.Parse(textboxX.Text);
                    }

                    value = (int)item.Y;
                    if (int.TryParse(textboxY.Text, out value))
                    {
                        item.Y = int.Parse(textboxY.Text);
                    }

                    value = (int)item.Width;
                    if (int.TryParse(textboxWidth.Text, out value))
                    {
                        item.Width = int.Parse(textboxWidth.Text);
                    }

                    value = (int)item.Height;
                    if (int.TryParse(textboxHeight.Text, out value))
                    {
                        item.Height = int.Parse(textboxHeight.Text);
                    }
                }
            });

            TextureManager.Instance.SetContentManager(Content);
            FontManager.Instance.SetContentManager(Content);


            var textureBlank = new Texture2D(_graphics.GraphicsDevice, 1, 1);

            Color[] tcolor = new Color[1];
            textureBlank.GetData <Color>(tcolor);
            tcolor[0] = new Color(255, 255, 255, 255);
            textureBlank.SetData <Color>(tcolor);

            TextureManager.Instance.AddTexture("Blank", textureBlank);
        }