Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Nov 8, 2023
1 parent 45ee8b0 commit a506be0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 85 deletions.
4 changes: 2 additions & 2 deletions samples/Myra.Samples.AllWidgets/AllWidgets.Generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generated by MyraPad at 11/8/2023 1:41:26 PM */
/* Generated by MyraPad at 11/8/2023 11:24:23 PM */
using Myra;
using Myra.Graphics2D;
using Myra.Graphics2D.TextureAtlases;
Expand Down Expand Up @@ -116,7 +116,7 @@ private void BuildUI()
_buttonSaveFile = new ImageTextButton();
_buttonSaveFile.Text = "Save File";
_buttonSaveFile.Padding = new Thickness(8, 0);
_buttonSaveFile.Tooltip = "Tooltip 1";
_buttonSaveFile.Tooltip = "E=mc/v[-8]2/n/vdMass–energy equivalence.";
_buttonSaveFile.Id = "_buttonSaveFile";
Grid.SetColumn(_buttonSaveFile, 1);

Expand Down
2 changes: 1 addition & 1 deletion samples/Myra.Samples.AllWidgets/allControls.xmmp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<Proportion Type="Fill" />
</Grid.ColumnsProportions>
<Label Text="Button:" />
<ImageTextButton Text="Save File" Padding="8, 0" Tooltip="Tooltip 1" Id="_buttonSaveFile" Grid.Column="1" />
<ImageTextButton Text="Save File" Padding="8, 0" Tooltip="E=mc/v[-8]2/n/vdMass–energy equivalence." Id="_buttonSaveFile" Grid.Column="1" />
<TextBox Id="_textSaveFile" Grid.Column="2" />
<Label Text="Another Button:" Grid.Row="1" />
<ImageTextButton Text="Open File" Padding="8, 0" Tooltip="Tooltip 2" Id="_buttonOpenFile" Grid.Column="1" Grid.Row="1" />
Expand Down
2 changes: 1 addition & 1 deletion samples/Myra.Samples.Silk.NET/AllWidgets.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void BuildUI()
_buttonOpenFile = new ImageTextButton();
_buttonOpenFile.Text = "Open File";
_buttonOpenFile.Padding = new Thickness(8, 0);
_buttonOpenFile.Tooltip = "Tooltip 1";
_buttonOpenFile.Tooltip = "E=mc/v[-8]2/n/vdMass–energy equivalence.";
Grid.SetColumn(_buttonOpenFile, 1);
Grid.SetRow(_buttonOpenFile, 1);
_buttonOpenFile.Id = "_buttonOpenFile";
Expand Down
66 changes: 6 additions & 60 deletions src/Myra/Graphics2D/UI/Desktop.Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ partial class Desktop
private readonly List<InputEventType> _scheduledInputEvents = new List<InputEventType>();
private readonly List<InputEventType> _scheduledInputEventsCopy = new List<InputEventType>();

public Func<MouseInfo> MouseInfoGetter { get; set; }

public Point PreviousMousePosition { get; private set; }
public Point? PreviousTouchPosition { get; private set; }

Expand Down Expand Up @@ -110,9 +108,7 @@ public float MouseWheelDelta
}
}

public Action<bool[]> DownKeysGetter { get; set; }
public bool[] DownKeys => _downKeys;
public Action<Keys> KeyDownHandler;
public int RepeatKeyDownStartInMs { get; set; } = 500;

public int RepeatKeyDownInternalInMs { get; set; } = 50;
Expand Down Expand Up @@ -145,12 +141,12 @@ public static bool IsMobile

public void UpdateMouseInput()
{
if (MouseInfoGetter == null)
if (MyraEnvironment.MouseInfoGetter == null)
{
return;
}

var mouseInfo = MouseInfoGetter();
var mouseInfo = MyraEnvironment.MouseInfoGetter();

// Mouse Position
MousePosition = mouseInfo.Position;
Expand Down Expand Up @@ -210,12 +206,12 @@ public void UpdateTouchInput()

public void UpdateKeyboardInput()
{
if (DownKeysGetter == null)
if (MyraEnvironment.DownKeysGetter == null)
{
return;
}

DownKeysGetter(_downKeys);
MyraEnvironment.DownKeysGetter(_downKeys);

var now = DateTime.Now;
for (var i = 0; i < _downKeys.Length; ++i)
Expand All @@ -228,7 +224,7 @@ public void UpdateKeyboardInput()
FocusNextWidget();
}

KeyDownHandler?.Invoke(key);
OnKeyDown(key);

_lastKeyDown = now;
_keyDownCount = 0;
Expand All @@ -251,7 +247,7 @@ public void UpdateKeyboardInput()
((_keyDownCount == 0 && (now - _lastKeyDown.Value).TotalMilliseconds > RepeatKeyDownStartInMs) ||
(_keyDownCount > 0 && (now - _lastKeyDown.Value).TotalMilliseconds > RepeatKeyDownInternalInMs)))
{
KeyDownHandler?.Invoke(key);
OnKeyDown(key);

_lastKeyDown = now;
++_keyDownCount;
Expand Down Expand Up @@ -332,55 +328,5 @@ public void ProcessInputEvents()
}
}
}

public MouseInfo DefaultMouseInfoGetter()
{
#if MONOGAME || FNA
var state = Mouse.GetState();

return new MouseInfo
{
Position = new Point(state.X, state.Y),
IsLeftButtonDown = state.LeftButton == ButtonState.Pressed,
IsMiddleButtonDown = state.MiddleButton == ButtonState.Pressed,
IsRightButtonDown = state.RightButton == ButtonState.Pressed,
Wheel = state.ScrollWheelValue
};
#elif STRIDE
var input = MyraEnvironment.Game.Input;

var v = input.AbsoluteMousePosition;

return new MouseInfo
{
Position = new Point((int)v.X, (int)v.Y),
IsLeftButtonDown = input.IsMouseButtonDown(MouseButton.Left),
IsMiddleButtonDown = input.IsMouseButtonDown(MouseButton.Middle),
IsRightButtonDown = input.IsMouseButtonDown(MouseButton.Right),
Wheel = input.MouseWheelDelta
};
#else
return MyraEnvironment.Platform.GetMouseInfo();
#endif
}

public void DefaultDownKeysGetter(bool[] keys)
{
#if MONOGAME || FNA
var state = Keyboard.GetState();
for (var i = 0; i < keys.Length; ++i)
{
keys[i] = state.IsKeyDown((Keys)i);
}
#elif STRIDE
var input = MyraEnvironment.Game.Input;
for (var i = 0; i < keys.Length; ++i)
{
keys[i] = input.IsKeyDown((Keys)i);
}
#else
MyraEnvironment.Platform.SetKeysDown(keys);
#endif
}
}
}
5 changes: 0 additions & 5 deletions src/Myra/Graphics2D/UI/Desktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,6 @@ public Desktop()
Opacity = 1.0f;
Widgets.CollectionChanged += WidgetsOnCollectionChanged;

MouseInfoGetter = DefaultMouseInfoGetter;
DownKeysGetter = DefaultDownKeysGetter;

KeyDownHandler = OnKeyDown;

#if FNA
TextInputEXT.TextInput += OnChar;
#endif
Expand Down
55 changes: 55 additions & 0 deletions src/Myra/MyraEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Stride.Engine;
using Stride.Graphics;
using Stride.Core.Mathematics;
using Stride.Input;
#else
using Myra.Platform;
using System.Drawing;
Expand Down Expand Up @@ -226,6 +227,10 @@ public static AssetManager DefaultAssetManager
public static bool DrawMouseHoveredWidgetFrame { get; set; }
public static bool DrawTextGlyphsFrames { get; set; }
public static bool DisableClipping { get; set; }

public static Func<MouseInfo> MouseInfoGetter { get; set; } = DefaultMouseInfoGetter;
public static Action<bool[]> DownKeysGetter { get; set; } = DefaultDownKeysGetter;

public static int TooltipDelayInMs { get; set; } = 500;
public static Point TooltipOffset { get; set; } = new Point(0, 20);
public static Func<Widget, Widget> TooltipCreator { get; set; } = w =>
Expand Down Expand Up @@ -272,5 +277,55 @@ public static string Version
}

internal static string InternalClipboard;

internal static MouseInfo DefaultMouseInfoGetter()
{
#if MONOGAME || FNA
var state = Mouse.GetState();

return new MouseInfo
{
Position = new Point(state.X, state.Y),
IsLeftButtonDown = state.LeftButton == ButtonState.Pressed,
IsMiddleButtonDown = state.MiddleButton == ButtonState.Pressed,
IsRightButtonDown = state.RightButton == ButtonState.Pressed,
Wheel = state.ScrollWheelValue
};
#elif STRIDE
var input = Game.Input;

var v = input.AbsoluteMousePosition;

return new MouseInfo
{
Position = new Point((int)v.X, (int)v.Y),
IsLeftButtonDown = input.IsMouseButtonDown(MouseButton.Left),
IsMiddleButtonDown = input.IsMouseButtonDown(MouseButton.Middle),
IsRightButtonDown = input.IsMouseButtonDown(MouseButton.Right),
Wheel = input.MouseWheelDelta
};
#else
return Platform.GetMouseInfo();
#endif
}

internal static void DefaultDownKeysGetter(bool[] keys)
{
#if MONOGAME || FNA
var state = Keyboard.GetState();
for (var i = 0; i < keys.Length; ++i)
{
keys[i] = state.IsKeyDown((Keys)i);
}
#elif STRIDE
var input = Game.Input;
for (var i = 0; i < keys.Length; ++i)
{
keys[i] = input.IsKeyDown((Keys)i);
}
#else
Platform.SetKeysDown(keys);
#endif
}
}
}
20 changes: 4 additions & 16 deletions src/MyraPad/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,6 @@ public void OnExiting()
private void BuildUI()
{
_desktop.ContextMenuClosed += Desktop_ContextMenuClosed;
_desktop.KeyDownHandler = key =>
{
if (_autoCompleteMenu != null &&
(key == Keys.Up || key == Keys.Down || key == Keys.Enter))
{
_autoCompleteMenu.OnKeyDown(key);
}
else
{
_desktop.OnKeyDown(key);
}
};

_desktop.KeyDown += (s, a) =>
{
if (_desktop.HasModalWidget || _ui._mainMenu.IsOpen)
Expand Down Expand Up @@ -1006,7 +993,10 @@ private void HandleAutoComplete()
var lastStartPos = _currentTagStart.Value;
var lastEndPos = cursorPos;
// Build context menu
_autoCompleteMenu = new VerticalMenu();
_autoCompleteMenu = new VerticalMenu
{
AcceptsKeyboardFocus = true
};
foreach (var a in all)
{
var menuItem = new MenuItem
Expand Down Expand Up @@ -1071,8 +1061,6 @@ private void HandleAutoComplete()
}

_desktop.ShowContextMenu(_autoCompleteMenu, screen);
// Keep focus at text field
_desktop.FocusedKeyboardWidget = _ui._textSource;

_refreshInitiated = null;
}
Expand Down

0 comments on commit a506be0

Please sign in to comment.