improvements to the gui system

This commit is contained in:
Dylan Wilson
2018-02-22 22:49:31 +10:00
parent a99d00a8f7
commit bbec589838
8 changed files with 77 additions and 35 deletions
+22 -23
View File
@@ -26,8 +26,6 @@ namespace MonoGame.Extended.Gui
private readonly KeyboardListener _keyboardListener;
private GuiControl _preFocusedControl;
private GuiControl _focusedControl;
private GuiControl _hoveredControl;
public GuiSystem(ViewportAdapter viewportAdapter, IGuiRenderer renderer)
{
@@ -38,7 +36,7 @@ namespace MonoGame.Extended.Gui
_mouseListener.MouseDown += (s, e) => OnPointerDown(GuiPointerEventArgs.FromMouseArgs(e));
_mouseListener.MouseMoved += (s, e) => OnPointerMoved(GuiPointerEventArgs.FromMouseArgs(e));
_mouseListener.MouseUp += (s, e) => OnPointerUp(GuiPointerEventArgs.FromMouseArgs(e));
_mouseListener.MouseWheelMoved += (s, e) => _focusedControl?.OnScrolled(e.ScrollWheelDelta);
_mouseListener.MouseWheelMoved += (s, e) => FocusedControl?.OnScrolled(e.ScrollWheelDelta);
_touchListener = new TouchListener(viewportAdapter);
_touchListener.TouchStarted += (s, e) => OnPointerDown(GuiPointerEventArgs.FromTouchArgs(e));
@@ -46,15 +44,16 @@ namespace MonoGame.Extended.Gui
_touchListener.TouchEnded += (s, e) => OnPointerUp(GuiPointerEventArgs.FromTouchArgs(e));
_keyboardListener = new KeyboardListener();
_keyboardListener.KeyTyped += (sender, args) => PropagateDown(_focusedControl, x => x.OnKeyTyped(this, args));
_keyboardListener.KeyPressed += (sender, args) => PropagateDown(_focusedControl, x => x.OnKeyPressed(this, args));
_keyboardListener.KeyTyped += (sender, args) => PropagateDown(FocusedControl, x => x.OnKeyTyped(this, args));
_keyboardListener.KeyPressed += (sender, args) => PropagateDown(FocusedControl, x => x.OnKeyPressed(this, args));
Screens = new GuiScreenCollection(this) { ItemAdded = InitializeScreen };
}
public GuiScreenCollection Screens { get; }
public GuiControl FocusedControl { get { return _focusedControl; } }
public GuiControl FocusedControl { get; private set; }
public GuiControl HoveredControl { get; private set; }
public GuiScreen ActiveScreen => Screens.LastOrDefault();
@@ -99,7 +98,7 @@ namespace MonoGame.Extended.Gui
}
}
var cursor = ActiveScreen.Skin?.Cursor;
var cursor = ActiveScreen?.Skin?.Cursor;
if (cursor != null)
_renderer.DrawRegion(cursor.TextureRegion, CursorPosition, cursor.Color);
@@ -131,7 +130,7 @@ namespace MonoGame.Extended.Gui
return;
_preFocusedControl = FindControlAtPoint(args.Position);
PropagateDown(_hoveredControl, x => x.OnPointerDown(this, args));
PropagateDown(HoveredControl, x => x.OnPointerDown(this, args));
}
private void OnPointerUp(GuiPointerEventArgs args)
@@ -147,7 +146,7 @@ namespace MonoGame.Extended.Gui
}
_preFocusedControl = null;
PropagateDown(_hoveredControl, x => x.OnPointerUp(this, args));
PropagateDown(HoveredControl, x => x.OnPointerUp(this, args));
}
private void OnPointerMoved(GuiPointerEventArgs args)
@@ -159,35 +158,35 @@ namespace MonoGame.Extended.Gui
var hoveredControl = FindControlAtPoint(args.Position);
if (_hoveredControl != hoveredControl)
if (HoveredControl != hoveredControl)
{
if (_hoveredControl != null && (hoveredControl == null || !hoveredControl.HasParent(_hoveredControl)))
PropagateDown(_hoveredControl, x => x.OnPointerLeave(this, args));
_hoveredControl = hoveredControl;
PropagateDown(_hoveredControl, x => x.OnPointerEnter(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));
}
else
{
PropagateDown(_hoveredControl, x => x.OnPointerMove(this, args));
PropagateDown(HoveredControl, x => x.OnPointerMove(this, args));
}
}
public void SetFocus(GuiControl focusedControl)
{
if (_focusedControl != focusedControl)
if (FocusedControl != focusedControl)
{
if (_focusedControl != null)
if (FocusedControl != null)
{
_focusedControl.IsFocused = false;
PropagateDown(_focusedControl, x => x.OnUnfocus(this));
FocusedControl.IsFocused = false;
PropagateDown(FocusedControl, x => x.OnUnfocus(this));
}
_focusedControl = focusedControl;
FocusedControl = focusedControl;
if (_focusedControl != null)
if (FocusedControl != null)
{
_focusedControl.IsFocused = true;
PropagateDown(_focusedControl, x => x.OnFocus(this));
FocusedControl.IsFocused = true;
PropagateDown(FocusedControl, x => x.OnFocus(this));
}
}
}