1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
using Microsoft.Xna.Framework;
using MonoGame.Extended.BitmapFonts;
using MonoGame.Extended.Gui.Controls;
using MonoGame.Extended.Input.InputListeners;
using MonoGame.Extended.ViewportAdapters;
using System;
using System.Linq;
namespace MonoGame.Extended.Gui
{
public interface IGuiContext
{
BitmapFont DefaultFont { get; }
Vector2 CursorPosition { get; }
Control FocusedControl { get; }
void SetFocus(Control focusedControl);
}
public class GuiSystem : IGuiContext, IRectangular
{
private readonly ViewportAdapter _viewportAdapter;
private readonly IGuiRenderer _renderer;
private readonly MouseListener _mouseListener;
private readonly TouchListener _touchListener;
private readonly KeyboardListener _keyboardListener;
private Control _preFocusedControl;
public GuiSystem(ViewportAdapter viewportAdapter, IGuiRenderer renderer)
{
_viewportAdapter = viewportAdapter;
_renderer = renderer;
_mouseListener = new MouseListener(viewportAdapter);
_mouseListener.MouseDown += (s, e) => OnPointerDown(PointerEventArgs.FromMouseArgs(e));
_mouseListener.MouseMoved += (s, e) => OnPointerMoved(PointerEventArgs.FromMouseArgs(e));
_mouseListener.MouseUp += (s, e) => OnPointerUp(PointerEventArgs.FromMouseArgs(e));
_mouseListener.MouseWheelMoved += (s, e) => FocusedControl?.OnScrolled(e.ScrollWheelDelta);
_touchListener = new TouchListener(viewportAdapter);
_touchListener.TouchStarted += (s, e) => OnPointerDown(PointerEventArgs.FromTouchArgs(e));
_touchListener.TouchMoved += (s, e) => OnPointerMoved(PointerEventArgs.FromTouchArgs(e));
_touchListener.TouchEnded += (s, e) => OnPointerUp(PointerEventArgs.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));
}
public Control FocusedControl { get; private set; }
public Control HoveredControl { get; private set; }
private Screen _activeScreen;
public Screen ActiveScreen
{
get => _activeScreen;
set
{
if (_activeScreen != value)
{
_activeScreen = value;
if(_activeScreen != null)
InitializeScreen(_activeScreen);
}
}
}
public Rectangle BoundingRectangle => _viewportAdapter.BoundingRectangle;
public Vector2 CursorPosition { get; set; }
public BitmapFont DefaultFont => Skin.Default?.DefaultFont;
private void InitializeScreen(Screen screen)
{
screen.Layout(this, BoundingRectangle);
}
public void ClientSizeChanged()
{
//ActiveScreen?.Content?.InvalidateMeasure();
ActiveScreen?.Layout(this, BoundingRectangle);
}
public void Update(GameTime gameTime)
{
if(ActiveScreen == null)
return;
_touchListener.Update(gameTime);
_mouseListener.Update(gameTime);
_keyboardListener.Update(gameTime);
var deltaSeconds = gameTime.GetElapsedSeconds();
if (ActiveScreen != null && ActiveScreen.IsVisible)
UpdateControl(ActiveScreen.Content, deltaSeconds);
//if (ActiveScreen.IsLayoutRequired)
// ActiveScreen.Layout(this, BoundingRectangle);
ActiveScreen.Update(gameTime);
}
public void Draw(GameTime gameTime)
{
var deltaSeconds = gameTime.GetElapsedSeconds();
_renderer.Begin();
if (ActiveScreen != null && ActiveScreen.IsVisible)
{
DrawControl(ActiveScreen.Content, deltaSeconds);
//DrawWindows(ActiveScreen.Windows, deltaSeconds);
}
var cursor = Skin.Default?.Cursor;
if (cursor != null)
_renderer.DrawRegion(cursor.TextureRegion, CursorPosition, cursor.Color);
_renderer.End();
}
//private void DrawWindows(WindowCollection windows, float deltaSeconds)
//{
// foreach (var window in windows)
// {
// window.Draw(this, _renderer, deltaSeconds);
// DrawChildren(window.Controls, deltaSeconds);
// }
//}
public void UpdateControl(Control control, float deltaSeconds)
{
if (control.IsVisible)
{
control.Update(this, deltaSeconds);
foreach (var childControl in control.Children)
UpdateControl(childControl, deltaSeconds);
}
}
private void DrawControl(Control control, float deltaSeconds)
{
if (control.IsVisible)
{
control.Draw(this, _renderer, deltaSeconds);
foreach (var childControl in control.Children)
DrawControl(childControl, deltaSeconds);
}
}
private void OnPointerDown(PointerEventArgs args)
{
if (ActiveScreen == null || !ActiveScreen.IsVisible)
return;
_preFocusedControl = FindControlAtPoint(args.Position);
PropagateDown(HoveredControl, x => x.OnPointerDown(this, args));
}
private void OnPointerUp(PointerEventArgs args)
{
if (ActiveScreen == null || !ActiveScreen.IsVisible)
return;
var postFocusedControl = FindControlAtPoint(args.Position);
if (_preFocusedControl == postFocusedControl)
{
SetFocus(postFocusedControl);
}
_preFocusedControl = null;
PropagateDown(HoveredControl, x => x.OnPointerUp(this, args));
}
private void OnPointerMoved(PointerEventArgs args)
{
CursorPosition = args.Position.ToVector2();
if (ActiveScreen == null || !ActiveScreen.IsVisible)
return;
var hoveredControl = FindControlAtPoint(args.Position);
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));
}
else
{
PropagateDown(HoveredControl, x => x.OnPointerMove(this, args));
}
}
public void SetFocus(Control focusedControl)
{
if (FocusedControl != focusedControl)
{
if (FocusedControl != null)
{
FocusedControl.IsFocused = false;
PropagateDown(FocusedControl, x => x.OnUnfocus(this));
}
FocusedControl = focusedControl;
if (FocusedControl != null)
{
FocusedControl.IsFocused = true;
PropagateDown(FocusedControl, x => x.OnFocus(this));
}
}
}
/// <summary>
/// Method is meant to loop down the parents control to find a suitable event control. If the predicate returns false
/// it will continue down the control tree.
/// </summary>
/// <param name="control">The control we want to check against</param>
/// <param name="predicate">A function to check if the propagation should resume, if returns false it will continue down the tree.</param>
private static void PropagateDown(Control control, Func<Control, bool> predicate)
{
while(control != null && predicate(control))
{
control = control.Parent;
}
}
private Control FindControlAtPoint(Point point)
{
if (ActiveScreen == null || !ActiveScreen.IsVisible)
return null;
return FindControlAtPoint(ActiveScreen.Content, point);
}
private Control FindControlAtPoint(Control control, Point point)
{
foreach (var controlChild in control.Children.Reverse())
{
var c = FindControlAtPoint(controlChild, point);
if (c != null)
return c;
}
if (control.IsVisible && control.Contains(this, point))
return control;
return null;
}
}
}
|