From 97d237f11f4d3ca51f747ec278e8697eb0dd2e61 Mon Sep 17 00:00:00 2001 From: tspayne87 Date: Sun, 26 Nov 2017 03:48:57 -0700 Subject: [PATCH] [GUI] Propegation changed also adding in fix for Issue #428 (#441) * Adding in changes to deal with custom control types for loading from a GuiScreen, also adding in block text if a width for the element exists. Adding in Padding to some elements to give the developer some more options in styles. Adding in some changes to the textbox to deal with selecting a section of text. * Adding in a few changes to deal with propagation down the GuiControl tree, this was added to most events. Also, adding in the idea of a form and submit button to deal with keyboard controls when inside of a form. This gives simalar behavier to how forms work in the browser. * Adding in the enter key skip for the textbox. * Fixing issue where GuiSystem would call initialize twice * Adding in a fix for Issue #428, by leveraging the propegation system to deal with hover styles. --- .../Controls/GuiControl.cs | 29 +++++++----- .../Controls/GuiTextBox.cs | 2 +- Source/MonoGame.Extended.Gui/GuiSystem.cs | 5 ++- .../Screens/ScreenGameComponent.cs | 3 +- .../Sandbox/Content/adventure-gui-skin.json | 9 ++++ .../Sandbox/Content/test-addition-screen.json | 45 +++++++++++++++++-- Source/Tests/Sandbox/Game1.cs | 16 ++++++- 7 files changed, 88 insertions(+), 21 deletions(-) diff --git a/Source/MonoGame.Extended.Gui/Controls/GuiControl.cs b/Source/MonoGame.Extended.Gui/Controls/GuiControl.cs index 231ba515..9d32768f 100644 --- a/Source/MonoGame.Extended.Gui/Controls/GuiControl.cs +++ b/Source/MonoGame.Extended.Gui/Controls/GuiControl.cs @@ -70,6 +70,8 @@ namespace MonoGame.Extended.Gui.Controls public HorizontalAlignment HorizontalTextAlignment { get; set; } = HorizontalAlignment.Centre; public VerticalAlignment VerticalTextAlignment { get; set; } = VerticalAlignment.Centre; + private bool _isHovering; + private string _text; public string Text { @@ -169,16 +171,8 @@ namespace MonoGame.Extended.Gui.Controls public virtual void OnScrolled(int delta) { } - public virtual bool OnKeyTyped(IGuiContext context, KeyboardEventArgs args) - { - if (args.Key == Keys.Tab || args.Key == Keys.Enter) return false; - return true; - } - public virtual bool OnKeyPressed(IGuiContext context, KeyboardEventArgs args) - { - if (args.Key == Keys.Tab || args.Key == Keys.Enter) return false; - return true; - } + public virtual bool OnKeyTyped(IGuiContext context, KeyboardEventArgs args) { return true; } + public virtual bool OnKeyPressed(IGuiContext context, KeyboardEventArgs args) { return true; } public virtual bool OnFocus(IGuiContext context) { return true; } public virtual bool OnUnfocus(IGuiContext context) { return true; } @@ -189,15 +183,21 @@ namespace MonoGame.Extended.Gui.Controls public virtual bool OnPointerEnter(IGuiContext context, GuiPointerEventArgs args) { - if (IsEnabled) + if (IsEnabled && !_isHovering) + { + _isHovering = true; HoverStyle?.Apply(this); + } return true; } public virtual bool OnPointerLeave(IGuiContext context, GuiPointerEventArgs args) { - if (IsEnabled) + if (IsEnabled && _isHovering) + { + _isHovering = false; HoverStyle?.Revert(this); + } return true; } @@ -212,6 +212,11 @@ namespace MonoGame.Extended.Gui.Controls DrawForeground(context, renderer, deltaSeconds, GetTextInfo(context, CreateBoxText(Text, Font ?? context.DefaultFont, Width), BoundingRectangle, HorizontalTextAlignment, VerticalTextAlignment)); } + public bool HasParent(GuiControl control) + { + return Parent != null && (Parent == control || Parent.HasParent(control)); + } + protected List FindControls() where T : GuiControl { diff --git a/Source/MonoGame.Extended.Gui/Controls/GuiTextBox.cs b/Source/MonoGame.Extended.Gui/Controls/GuiTextBox.cs index cda3ddcf..9b22fd7f 100644 --- a/Source/MonoGame.Extended.Gui/Controls/GuiTextBox.cs +++ b/Source/MonoGame.Extended.Gui/Controls/GuiTextBox.cs @@ -121,7 +121,7 @@ namespace MonoGame.Extended.Gui.Controls { case Keys.Tab: case Keys.Enter: - return false; + return true; case Keys.Back: if (Text.Length > 0) { diff --git a/Source/MonoGame.Extended.Gui/GuiSystem.cs b/Source/MonoGame.Extended.Gui/GuiSystem.cs index 75ef179c..85021895 100644 --- a/Source/MonoGame.Extended.Gui/GuiSystem.cs +++ b/Source/MonoGame.Extended.Gui/GuiSystem.cs @@ -162,7 +162,8 @@ namespace MonoGame.Extended.Gui if (_hoveredControl != hoveredControl) { - PropagateDown(_hoveredControl, x => x.OnPointerLeave(this, args)); + if (_hoveredControl != null && (hoveredControl == null || !hoveredControl.HasParent(_hoveredControl))) + PropagateDown(_hoveredControl, x => x.OnPointerLeave(this, args)); _hoveredControl = hoveredControl; PropagateDown(_hoveredControl, x => x.OnPointerEnter(this, args)); } @@ -200,7 +201,7 @@ namespace MonoGame.Extended.Gui /// A function to check if the propagation should resume, if returns false it will continue down the tree. private void PropagateDown(GuiControl control, Func predicate) { - while(control != null && !predicate(control)) + while(control != null && predicate(control)) { control = control.Parent; } diff --git a/Source/MonoGame.Extended/Screens/ScreenGameComponent.cs b/Source/MonoGame.Extended/Screens/ScreenGameComponent.cs index b82b631b..bc93a2c8 100644 --- a/Source/MonoGame.Extended/Screens/ScreenGameComponent.cs +++ b/Source/MonoGame.Extended/Screens/ScreenGameComponent.cs @@ -52,7 +52,8 @@ namespace MonoGame.Extended.Screens public override void Initialize() { foreach (var screen in _screens) - screen.Initialize(); + if (!screen.IsInitialized) // Check if screen has already been Initialized from the register + screen.Initialize(); base.Initialize(); } diff --git a/Source/Tests/Sandbox/Content/adventure-gui-skin.json b/Source/Tests/Sandbox/Content/adventure-gui-skin.json index b4fed6bf..c90072a4 100644 --- a/Source/Tests/Sandbox/Content/adventure-gui-skin.json +++ b/Source/Tests/Sandbox/Content/adventure-gui-skin.json @@ -94,6 +94,15 @@ { "Name": "brown-panel-no-size", "Type": "GuiStackPanel", + "BackgroundRegion": "panel_brown", + "HoverStyle": { + "Type": "GuiStackPanel", + "BackgroundRegion": "panelInset_beige" + } + }, + { + "Name": "brown-grid-no-size", + "Type": "GuiUniformGrid", "BackgroundRegion": "panel_brown" }, { diff --git a/Source/Tests/Sandbox/Content/test-addition-screen.json b/Source/Tests/Sandbox/Content/test-addition-screen.json index 111c33c1..144e4b67 100644 --- a/Source/Tests/Sandbox/Content/test-addition-screen.json +++ b/Source/Tests/Sandbox/Content/test-addition-screen.json @@ -1,12 +1,49 @@ { "Skin": "Content/adventure-gui-skin.json", - "Name": "test-addition-screen", + "Name": "test-additional-screen", "Controls": [ { - "Type": "GuiStackPanel", + "Type": "GuiUniformGrid", "Name": "MainPanel", - "Style": "brown-panel-no-size", - "Padding": "15" + "Padding": "15", + "Style": "brown-grid-no-size", + "HorizontalAlignment": "Stretch", + "VerticalAlignment": "Top", + "Orientation": "Horizontal", + "Height": "60", + "Rows": "1", + "Columns": "2", + "Controls": [ + { + "Type": "GuiLabel", + "Style": "label", + "TextColor": "#FFFFFF", + "Margin": "0 10", + "Width": "200", + "Height": "30", + "Text": "Hello World", + "HorizontalAlignment": "Left" + }, + { + "Type": "GuiStackPanel", + "Orientation": "Horizontal", + "Margin": "0 10", + "Controls": [ + { + "Type": "GuiButton", + "Width": "200", + "Text": "Test", + "Style": "white-button" + }, + { + "Type": "GuiButton", + "Width": "200", + "Text": "Test", + "Style": "white-button" + } + ] + } + ] } ] } diff --git a/Source/Tests/Sandbox/Game1.cs b/Source/Tests/Sandbox/Game1.cs index 9443194d..c3c3d716 100644 --- a/Source/Tests/Sandbox/Game1.cs +++ b/Source/Tests/Sandbox/Game1.cs @@ -63,13 +63,17 @@ namespace Sandbox { IsMouseVisible = false; - _viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, _graphicsDeviceManager.PreferredBackBufferWidth, _graphicsDeviceManager.PreferredBackBufferHeight); + // _viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, _graphicsDeviceManager.PreferredBackBufferWidth, _graphicsDeviceManager.PreferredBackBufferHeight); + _viewportAdapter = new WindowViewportAdapter(Window, GraphicsDevice); _skin = GuiSkin.FromFile(Content, @"Content/adventure-gui-skin.json", typeof(MyPanel)); var guiRenderer = new GuiSpriteBatchRenderer(GraphicsDevice, _viewportAdapter.GetScaleMatrix); var viewModel = new ViewModel(); + //_screen = GuiScreen.FromFile(Content, "Content/test-addition-screen.json", typeof(MyPanel)); + //Window.ClientSizeChanged += OnClientSizeChanged; + _screen = GuiScreen.FromFile(Content, "Content/adventure-gui-screen.json", typeof(MyPanel)); var listBox = _screen.FindControl("Listbox"); @@ -123,5 +127,15 @@ namespace Sandbox listBox.Items.Add(new { Name = textBox.Text }); } } + private void OnClientSizeChanged(object sender, EventArgs eventArgs) + { + if (_guiSystem != null) + { + foreach (var screen in _guiSystem.Screens) + { + screen.Layout(_guiSystem, _guiSystem.BoundingRectangle); + } + } + } } }