[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.
This commit is contained in:
tspayne87
2017-11-26 03:48:57 -07:00
committed by Dylan Wilson
parent 92b3bc04f3
commit 97d237f11f
7 changed files with 88 additions and 21 deletions
@@ -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<T> FindControls<T>()
where T : GuiControl
{
@@ -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)
{
+3 -2
View File
@@ -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
/// <param name="predicate">A function to check if the propagation should resume, if returns false it will continue down the tree.</param>
private void PropagateDown(GuiControl control, Func<GuiControl, bool> predicate)
{
while(control != null && !predicate(control))
while(control != null && predicate(control))
{
control = control.Parent;
}
@@ -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();
}
@@ -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"
},
{
@@ -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"
}
]
}
]
}
]
}
+15 -1
View File
@@ -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<GuiListBox>("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);
}
}
}
}
}