Skip to content

Commit

Permalink
Fixes #153
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Jan 10, 2020
1 parent 8e30a0a commit 3ff9717
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
74 changes: 34 additions & 40 deletions src/Myra/Graphics2D/UI/Desktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static class Desktop

private static bool _layoutDirty = true;
private static bool _widgetsDirty = true;
private static Widget _focusedKeyboardWidget;
private static Widget _focusedKeyboardWidget, _focusedMouseWheelWidget;
private static readonly List<Widget> _widgetsCopy = new List<Widget>();
private static DateTime _lastTouchDown;
private static DateTime? _lastKeyDown;
Expand All @@ -67,6 +67,8 @@ public static class Desktop
private static Point _mousePosition, _touchPosition;
private static Point _lastMousePosition, _lastTouchPosition;
private static bool _contextMenuShown = false;
private static bool _keyboardFocusSet = false;
private static bool _mouseWheelFocusSet = false;
#if MONOGAME
public static bool HasExternalTextInput = false;
#endif
Expand Down Expand Up @@ -199,6 +201,11 @@ public static Widget FocusedKeyboardWidget

set
{
if (value != null)
{
_keyboardFocusSet = true;
}

if (value == _focusedKeyboardWidget)
{
return;
Expand Down Expand Up @@ -234,7 +241,20 @@ public static Widget FocusedKeyboardWidget

public static Widget FocusedMouseWheelWidget
{
get; set;
get
{
return _focusedMouseWheelWidget;
}

set
{
if (value != null)
{
_mouseWheelFocusSet = true;
}

_focusedMouseWheelWidget = value;
}
}

private static RenderContext RenderContext
Expand Down Expand Up @@ -541,55 +561,29 @@ private static void ContextMenuOnTouchDown()
HideContextMenu();
}

private static void FocusOnTouchDown()
private static void InputOnTouchDown()
{
// Handle focus
var activeWidget = GetTopWidget(true);
if (activeWidget == null)
{
return;
}

// Widgets at the bottom of tree become focused
Widget focusedWidget = null;
UIUtils.ProcessWidgets(activeWidget, s =>
{
if (s.Enabled && s.IsTouchInside && s.Active && s.AcceptsKeyboardFocus)
{
focusedWidget = s;
}

return true;
});
FocusedKeyboardWidget = focusedWidget;
_contextMenuShown = false;
_keyboardFocusSet = false;
_mouseWheelFocusSet = false;
UpdateIsTouchInside(true);

focusedWidget = null;
UIUtils.ProcessWidgets(activeWidget, s =>
if (!_keyboardFocusSet && FocusedKeyboardWidget != null)
{
if (s.Enabled && s.IsTouchInside && s.Active && s.AcceptsMouseWheelFocus)
{
focusedWidget = s;
}

return true;
});
// Nullify keyboard focus
FocusedKeyboardWidget = null;
}

if (focusedWidget != null ||
(FocusedMouseWheelWidget != null && FocusedMouseWheelWidget.MouseWheelFocusCanBeNull))
if (!_mouseWheelFocusSet && FocusedMouseWheelWidget != null && FocusedMouseWheelWidget.MouseWheelFocusCanBeNull)
{
FocusedMouseWheelWidget = focusedWidget;
// Nullify mouse wheel focus
FocusedMouseWheelWidget = null;
}
}

private static void InputOnTouchDown()
{
_contextMenuShown = false;
UpdateIsTouchInside(true);
if (!_contextMenuShown)
{
ContextMenuOnTouchDown();
}
FocusOnTouchDown();
}

private static void InputOnTouchUp()
Expand Down
11 changes: 11 additions & 0 deletions src/Myra/Graphics2D/UI/Widget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,17 @@ public virtual void OnTouchMoved()
public virtual void OnTouchDown()
{
IsTouchInside = true;

if (Enabled && AcceptsKeyboardFocus)
{
Desktop.FocusedKeyboardWidget = this;
}

if (Enabled && AcceptsMouseWheelFocus)
{
Desktop.FocusedMouseWheelWidget = this;
}

TouchDown.Invoke(this);
}

Expand Down

0 comments on commit 3ff9717

Please sign in to comment.