Skip to content

Commit e0b751c

Browse files
committed
Fixes #413
1 parent 4f6f7f5 commit e0b751c

File tree

11 files changed

+193
-48
lines changed

11 files changed

+193
-48
lines changed

samples/Myra.Samples.PlatformAgnostic/Platform/MGPlatform.cs

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Drawing;
4+
using info.lundin.math;
35
using Microsoft.Xna.Framework.Graphics;
46
using Microsoft.Xna.Framework.Input;
57
using Myra.Graphics2D.UI;
@@ -9,6 +11,22 @@ namespace Myra.Samples.AllWidgets
911
{
1012
internal class MGPlatform : IMyraPlatform
1113
{
14+
private static readonly Dictionary<MouseCursorType, MouseCursor> _mouseCursors = new Dictionary<MouseCursorType, MouseCursor>
15+
{
16+
[MouseCursorType.Arrow] = MouseCursor.Arrow,
17+
[MouseCursorType.IBeam] = MouseCursor.IBeam,
18+
[MouseCursorType.Wait] = MouseCursor.Wait,
19+
[MouseCursorType.Crosshair] = MouseCursor.Crosshair,
20+
[MouseCursorType.WaitArrow] = MouseCursor.WaitArrow,
21+
[MouseCursorType.SizeNWSE] = MouseCursor.SizeNWSE,
22+
[MouseCursorType.SizeNESW] = MouseCursor.SizeNESW,
23+
[MouseCursorType.SizeWE] = MouseCursor.SizeWE,
24+
[MouseCursorType.SizeNS] = MouseCursor.SizeNS,
25+
[MouseCursorType.SizeAll] = MouseCursor.SizeAll,
26+
[MouseCursorType.No] = MouseCursor.No,
27+
[MouseCursorType.Hand] = MouseCursor.Hand,
28+
};
29+
1230
private readonly MGRenderer _renderer;
1331

1432
public Point ViewSize
@@ -57,5 +75,16 @@ public TouchCollection GetTouchState()
5775
// Do not bother with accurately returning touch state for now
5876
return TouchCollection.Empty;
5977
}
78+
79+
public void SetMouseCursorType(MouseCursorType mouseCursorType)
80+
{
81+
MouseCursor mouseCursor;
82+
if (!_mouseCursors.TryGetValue(mouseCursorType, out mouseCursor))
83+
{
84+
throw new Exception($"Could not find mouse cursor {mouseCursorType}");
85+
}
86+
87+
Mouse.SetCursor(mouseCursor);
88+
}
6089
}
6190
}

samples/Myra.Samples.Silk.NET.TrippyGL/Platform/TrippyPlatform.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static TrippyPlatform()
3131
{
3232
// Fill _keysMap matching names
3333
var keysValues = Enum.GetValues(typeof(Keys));
34-
foreach(Keys keys in keysValues)
34+
foreach (Keys keys in keysValues)
3535
{
3636
var name = Enum.GetName(typeof(Keys), keys);
3737

@@ -93,7 +93,8 @@ public void SetKeysDown(bool[] keys)
9393
if (_keysMap.TryGetValue(ks, out key))
9494
{
9595
keys[i] = state.IsKeyPressed(key);
96-
} else
96+
}
97+
else
9798
{
9899
keys[i] = false;
99100
}
@@ -105,5 +106,9 @@ public TouchCollection GetTouchState()
105106
// Do not bother with accurately returning touch state for now
106107
return TouchCollection.Empty;
107108
}
109+
110+
public void SetMouseCursorType(MouseCursorType mouseCursorType)
111+
{
112+
}
108113
}
109114
}

samples/Myra.Samples.Silk.NET/Platform/SilkPlatform.cs

+4
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,9 @@ public TouchCollection GetTouchState()
9696
// Do not bother with accurately returning touch state for now
9797
return TouchCollection.Empty;
9898
}
99+
100+
public void SetMouseCursorType(MouseCursorType mouseCursorType)
101+
{
102+
}
99103
}
100104
}

src/Myra/Graphics2D/UI/Enums.cs

+16
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,20 @@ public enum Orientation
2828
Horizontal,
2929
Vertical
3030
}
31+
32+
public enum MouseCursorType
33+
{
34+
Arrow,
35+
IBeam,
36+
Wait,
37+
Crosshair,
38+
WaitArrow,
39+
SizeNWSE,
40+
SizeNESW,
41+
SizeWE,
42+
SizeNS,
43+
SizeAll,
44+
No,
45+
Hand,
46+
}
3147
}

src/Myra/Graphics2D/UI/Simple/TextBox.cs

+10
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ public VerticalAlignment TextVerticalAlignment
236236
get; set;
237237
}
238238

239+
[Category("Behavior")]
240+
[DefaultValue(MouseCursorType.IBeam)]
241+
public override MouseCursorType? MouseCursor
242+
{
243+
get => base.MouseCursor;
244+
set => base.MouseCursor = value;
245+
}
246+
239247
[DefaultValue(true)]
240248
public override bool ClipToBounds
241249
{
@@ -374,6 +382,8 @@ public TextBox(string styleName = Stylesheet.DefaultStyleName)
374382
SetStyle(styleName);
375383

376384
BlinkIntervalInMs = 450;
385+
386+
MouseCursor = MouseCursorType.IBeam;
377387
}
378388

379389
private void DeleteChars(int pos, int l)

src/Myra/Graphics2D/UI/Widget.cs

+32
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public enum DragDirection
4242

4343
public partial class Widget : BaseObject, ITransformable
4444
{
45+
private MouseCursorType? _mouseCursorType;
4546
private Vector2? _startPos;
4647
private Point _startLeftTop;
4748
private Thickness _margin, _borderThickness, _padding;
@@ -459,6 +460,27 @@ public int ZIndex
459460
}
460461
}
461462

463+
[Category("Behavior")]
464+
[DefaultValue(null)]
465+
public virtual MouseCursorType? MouseCursor
466+
{
467+
get => _mouseCursorType;
468+
set
469+
{
470+
if (value == _mouseCursorType)
471+
{
472+
return;
473+
}
474+
475+
_mouseCursorType = value;
476+
foreach(var child in Children)
477+
{
478+
child.MouseCursor = value;
479+
}
480+
}
481+
}
482+
483+
462484
[Category("Transform")]
463485
[DefaultValue("1, 1")]
464486
[DesignerFolded]
@@ -1304,13 +1326,23 @@ protected virtual void InternalSetStyle(Stylesheet stylesheet, string name)
13041326

13051327
public virtual void OnMouseLeft()
13061328
{
1329+
if (MouseCursor != null)
1330+
{
1331+
MyraEnvironment.MouseCursorType = MyraEnvironment.DefaultMouseCursorType;
1332+
}
1333+
13071334
ChildrenCopy.ProcessMouseLeft();
13081335

13091336
MouseLeft.Invoke(this);
13101337
}
13111338

13121339
public virtual void OnMouseEntered()
13131340
{
1341+
if (MouseCursor != null)
1342+
{
1343+
MyraEnvironment.MouseCursorType = MouseCursor.Value;
1344+
}
1345+
13141346
ChildrenCopy.ProcessMouseMovement();
13151347

13161348
MouseEntered.Invoke(this);

src/Myra/MML/BaseContext.cs

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ protected static void ParseProperties(Type type, bool checkSkipSave,
7474
if (propertyType.IsPrimitive ||
7575
propertyType.IsNullablePrimitive() ||
7676
propertyType.IsEnum ||
77+
propertyType.IsNullableEnum() ||
7778
propertyType == typeof(string) ||
7879
propertyType == typeof(Vector2) ||
7980
propertyType == typeof(Color) ||

src/Myra/MML/LoadContext.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,13 @@ public void Load<T>(object obj, XElement el, T handler) where T : class
133133
{
134134
value = serializer.Deserialize(attr.Value);
135135
} else
136-
if (propertyType.IsEnum)
136+
if (propertyType.IsEnum ||
137+
propertyType.IsNullableEnum())
137138
{
139+
if (propertyType.IsNullableEnum())
140+
{
141+
propertyType = propertyType.GetNullableType();
142+
}
138143
value = Enum.Parse(propertyType, attr.Value);
139144
}
140145
else if (propertyType == typeof(Color) || propertyType == typeof(Color?))

src/Myra/MyraEnvironment.cs

+81-39
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
using System;
22
using System.Reflection;
33
using Myra.Graphics2D.UI.Styles;
4-
using FontStashSharp.Interfaces;
54
using Myra.Utility;
6-
using FontStashSharp;
75
using AssetManagementBase;
6+
using Myra.Graphics2D.UI;
7+
using System.Collections.Generic;
88

99
#if MONOGAME || FNA
1010
using Microsoft.Xna.Framework;
1111
using Microsoft.Xna.Framework.Graphics;
12+
using Microsoft.Xna.Framework.Input;
13+
#if FNA
14+
using static SDL2.SDL;
15+
using MouseCursor = System.Nullable<System.IntPtr>;
16+
#endif
17+
1218
#elif STRIDE
1319
using Stride.Engine;
1420
using Stride.Graphics;
@@ -20,59 +26,95 @@ namespace Myra
2026
{
2127
public static class MyraEnvironment
2228
{
23-
private static AssetManager _defaultAssetManager;
24-
25-
[Obsolete("Use FontSystemDefaults.KernelWidth")]
26-
public static int FontKernelWidth
29+
#if MONOGAME
30+
private static readonly Dictionary<MouseCursorType, MouseCursor> _mouseCursors = new Dictionary<MouseCursorType, MouseCursor>
2731
{
28-
get => FontSystemDefaults.KernelWidth;
29-
set
30-
{
31-
FontSystemDefaults.KernelWidth = value;
32-
}
33-
}
34-
35-
[Obsolete("Use FontSystemDefaults.KernelHeight")]
36-
public static int FontKernelHeight
32+
[MouseCursorType.Arrow] = MouseCursor.Arrow,
33+
[MouseCursorType.IBeam] = MouseCursor.IBeam,
34+
[MouseCursorType.Wait] = MouseCursor.Wait,
35+
[MouseCursorType.Crosshair] = MouseCursor.Crosshair,
36+
[MouseCursorType.WaitArrow] = MouseCursor.WaitArrow,
37+
[MouseCursorType.SizeNWSE] = MouseCursor.SizeNWSE,
38+
[MouseCursorType.SizeNESW] = MouseCursor.SizeNESW,
39+
[MouseCursorType.SizeWE] = MouseCursor.SizeWE,
40+
[MouseCursorType.SizeNS] = MouseCursor.SizeNS,
41+
[MouseCursorType.SizeAll] = MouseCursor.SizeAll,
42+
[MouseCursorType.No] = MouseCursor.No,
43+
[MouseCursorType.Hand] = MouseCursor.Hand,
44+
};
45+
#elif FNA
46+
private static readonly Dictionary<SDL_SystemCursor, IntPtr> _systemCursors = new Dictionary<SDL_SystemCursor, IntPtr>();
47+
48+
private static IntPtr GetSystemCursor(SDL_SystemCursor type)
3749
{
38-
get => FontSystemDefaults.KernelHeight;
39-
set
50+
IntPtr result;
51+
if (_systemCursors.TryGetValue(type, out result))
4052
{
41-
FontSystemDefaults.KernelHeight = value;
53+
return result;
4254
}
43-
}
4455

45-
[Obsolete("Use FontSystemDefaults.PremultiplyAlpha")]
46-
public static bool FontPremultiplyAlpha
47-
{
48-
get => FontSystemDefaults.PremultiplyAlpha;
49-
set
50-
{
51-
FontSystemDefaults.PremultiplyAlpha = value;
52-
}
56+
result = SDL_CreateSystemCursor(type);
57+
_systemCursors[type] = result;
58+
59+
return result;
5360
}
5461

55-
[Obsolete("Use FontSystemDefaults.FontResolutionFactor")]
56-
public static float FontResolutionFactor
62+
private static readonly Dictionary<MouseCursorType, SDL_SystemCursor> _mouseCursors = new Dictionary<MouseCursorType, SDL_SystemCursor>
5763
{
58-
get => FontSystemDefaults.FontResolutionFactor;
59-
set
60-
{
61-
FontSystemDefaults.FontResolutionFactor = value;
62-
}
63-
}
64+
[MouseCursorType.Arrow] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_ARROW,
65+
[MouseCursorType.IBeam] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_IBEAM,
66+
[MouseCursorType.Wait] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_WAIT,
67+
[MouseCursorType.Crosshair] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_CROSSHAIR,
68+
[MouseCursorType.WaitArrow] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_WAITARROW,
69+
[MouseCursorType.SizeNWSE] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENWSE,
70+
[MouseCursorType.SizeNESW] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENESW,
71+
[MouseCursorType.SizeWE] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEWE,
72+
[MouseCursorType.SizeNS] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENS,
73+
[MouseCursorType.SizeAll] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEALL,
74+
[MouseCursorType.No] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_NO,
75+
[MouseCursorType.Hand] = SDL_SystemCursor.SDL_SYSTEM_CURSOR_HAND,
76+
};
77+
#endif
6478

79+
private static MouseCursorType _mouseCursorType;
80+
private static AssetManager _defaultAssetManager;
6581

66-
[Obsolete("Use FontSystemDefaults.FontLoader")]
67-
public static IFontLoader FontLoader
82+
public static MouseCursorType MouseCursorType
6883
{
69-
get => FontSystemDefaults.FontLoader;
84+
get => _mouseCursorType;
7085
set
7186
{
72-
FontSystemDefaults.FontLoader = value;
87+
if (_mouseCursorType == value)
88+
{
89+
return;
90+
}
91+
92+
_mouseCursorType = value;
93+
#if MONOGAME
94+
MouseCursor mouseCursor;
95+
if (!_mouseCursors.TryGetValue(value, out mouseCursor))
96+
{
97+
throw new Exception($"Could not find mouse cursor {value}");
98+
}
99+
100+
Mouse.SetCursor(mouseCursor);
101+
#elif FNA
102+
SDL_SystemCursor mouseCursor;
103+
if (!_mouseCursors.TryGetValue(value, out mouseCursor))
104+
{
105+
throw new Exception($"Could not find mouse cursor {value}");
106+
}
107+
108+
var mouseCursorPtr = GetSystemCursor(mouseCursor);
109+
SDL2.SDL.SDL_SetCursor(mouseCursorPtr);
110+
#elif PLATFORM_AGNOSTIC
111+
Platform.SetMouseCursorType(value);
112+
#endif
73113
}
74114
}
75115

116+
public static MouseCursorType DefaultMouseCursorType { get; set; }
117+
76118
#if MONOGAME || FNA || STRIDE
77119

78120
private static Game _game;

src/Myra/Platform/IMyraPlatform.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public interface IMyraPlatform
1919
/// </summary>
2020
/// <param name="keys"></param>
2121
void SetKeysDown(bool[] keys);
22+
void SetMouseCursorType(MouseCursorType mouseCursorType);
2223

2324
TouchCollection GetTouchState();
2425
}

0 commit comments

Comments
 (0)