/
UserInterfaceManager.cs
511 lines (438 loc) · 19 KB
/
UserInterfaceManager.cs
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
using Lidgren.Network;
using SFML.Graphics;
using SFML.System;
using SFML.Window;
using SS14.Client.Graphics;
using SS14.Client.Graphics.Event;
using SS14.Client.Interfaces.Console;
using SS14.Client.Interfaces.Resource;
using SS14.Client.Interfaces.UserInterface;
using SS14.Client.UserInterface.Components;
using SS14.Shared;
using SS14.Shared.IoC;
using SS14.Shared.Interfaces.Configuration;
using SS14.Shared.Maths;
using System;
using System.Collections.Generic;
using System.Linq;
using SS14.Shared.Configuration;
namespace SS14.Client.UserInterface
{
//TODO Make sure all ui compos use gorgon.currentrendertarget instead of gorgon.screen so they draw to the ui rendertarget. also add the actual rendertarget.
/// <summary>
/// Manages UI Components. This includes input, rendering, updates and net messages.
/// </summary>
public class UserInterfaceManager : IUserInterfaceManager, IPostInjectInit
{
/// <summary>
/// List of iGuiComponents. Components in this list will recieve input, updates and net messages.
/// </summary>
private readonly List<IGuiComponent> _components = new List<IGuiComponent>();
[Dependency]
private readonly IConfigurationManager _config;
[Dependency]
private readonly IResourceManager _resourceManager;
private IGuiComponent _currentFocus;
private Sprite _cursorSprite;
private DebugConsole _console;
private Vector2i dragOffset = new Vector2i();
private bool moveMode;
private IGuiComponent movingComp;
private bool showCursor = true;
public void PostInject()
{
_config.RegisterCVar("key.keyboard.console", Keyboard.Key.Home, CVarFlags.ARCHIVE);
}
public void Initialize()
{
_console = new DebugConsole("dbgConsole", new Vector2i((int)CluwneLib.Screen.Size.X, 400), _resourceManager);
_console.SetVisible(false);
}
public Vector2i MousePos { get; private set; }
#region IUserInterfaceManager Members
public IDragDropInfo DragInfo { get; private set; } = new DragDropInfo();
public IDebugConsole Console => _console;
/// <summary>
/// Toggles UI element move mode.
/// </summary>
public void ToggleMoveMode()
{
moveMode = !moveMode;
}
/// <summary>
/// Disposes all components and clears component list. Components need to implement their own Dispose method.
/// </summary>
public void DisposeAllComponents()
{
foreach (IGuiComponent x in _components.ToList())
{
x.Dispose();
}
_components.Clear();
}
/// <summary>
/// Disposes all components of the given type.
/// </summary>
public void DisposeAllComponents<T>()
{
List<IGuiComponent> componentsOfType = (from IGuiComponent component in _components
where component.GetType() == typeof(T)
select component).ToList();
foreach (IGuiComponent current in componentsOfType)
{
current.Dispose();
_components.Remove(current);
}
}
public void AddComponent(IGuiComponent component)
{
_components.Add(component);
}
public void RemoveComponent(IGuiComponent component)
{
if (_components.Contains(component))
_components.Remove(component);
}
/// <summary>
/// Calls the custom Update Method for Components. This allows components to update ui elements if they implement the the needed method.
/// </summary>
public void ComponentUpdate(GuiComponentType componentType, params object[] args)
{
IGuiComponent firstOrDefault = (from IGuiComponent comp in _components
where comp.ComponentClass == componentType
select comp).FirstOrDefault();
if (firstOrDefault != null)
firstOrDefault.ComponentUpdate(args);
}
/// <summary>
/// Handles Net messages directed at the UI manager or components thereof. This must be called by the currently active state. See GameScreen.
/// </summary>
public void HandleNetMessage(NetIncomingMessage msg)
{
var uiMsg = (UiManagerMessage)msg.ReadByte();
switch (uiMsg)
{
case UiManagerMessage.ComponentMessage:
HandleComponentMessage(msg);
break;
case UiManagerMessage.CreateUiElement:
HandleElementCreation(msg);
break;
}
}
//TODO Holy shit make this not complete crap. Oh man.
/// <summary>
/// Sets focus for a component.
/// </summary>
public void SetFocus(IGuiComponent newFocus)
{
if (_currentFocus != null)
{
RemoveFocus();
}
_currentFocus = newFocus;
newFocus.Focus = true;
}
/// <summary>
/// Removes focus for currently focused control.
/// </summary>
public void RemoveFocus()
{
if (_currentFocus == null)
return;
_currentFocus.Focus = false;
_currentFocus = null;
}
/// <summary>
/// Removes focus for given control if control has focus.
/// </summary>
public void RemoveFocus(IGuiComponent remFocus)
{
if (_currentFocus != remFocus)
return;
_currentFocus.Focus = false;
_currentFocus = null;
}
public void ResizeComponents()
{
foreach (IGuiComponent guiComponent in _components)
{
guiComponent.Resize();
}
}
#endregion IUserInterfaceManager Members
#region Input
//The game states have to feed the UI Input!!! This is to allow more flexibility.
//Maybe this should be handled in the main methods for input. But then the state wouldnt have power over
//this and things like the chat might get difficult.
//Other Notes: When a component returns true to an input event the loop stops and so only that control recieves the input.
// That way we don't have all components reacting to one event.
/// <summary>
/// Handles MouseDown event. Returns true if a component accepted and handled the event.
/// </summary>
public virtual bool MouseDown(MouseButtonEventArgs e)
{
if (_console.IsVisible())
{
if (_console.MouseDown(e)) return true;
}
if (moveMode)
{
foreach (IGuiComponent comp in _components)
{
if (comp.ClientArea.Contains(e.X, e.Y))
{
movingComp = comp;
dragOffset = (new Vector2i(e.X, e.Y)) -
new Vector2i(comp.ClientArea.Left, comp.ClientArea.Top);
break;
}
}
return true;
}
else
{
IOrderedEnumerable<IGuiComponent> inputList = from IGuiComponent comp in _components
where comp.ReceiveInput
orderby comp.ZDepth ascending
orderby comp.IsVisible() descending
//Invisible controls still recieve input but after everyone else. This is mostly for the inventory and other toggleable components.
orderby comp.Focus descending
select comp;
foreach (IGuiComponent current in inputList)
if (current.MouseDown(e))
{
SetFocus(current);
return true;
}
return false;
}
}
/// <summary>
/// Handles MouseUp event. Returns true if a component accepted and handled the event.
/// </summary>
public virtual bool MouseUp(MouseButtonEventArgs e)
{
if (_console.IsVisible())
{
if (_console.MouseUp(e)) return true;
}
if (moveMode)
{
if (movingComp != null) movingComp = null;
return true;
}
else
{
IOrderedEnumerable<IGuiComponent> inputList = from IGuiComponent comp in _components
where comp.ReceiveInput
orderby comp.ZDepth ascending
orderby comp.IsVisible() descending
//Invisible controls still recieve input but after everyone else. This is mostly for the inventory and other toggleable components.
orderby comp.Focus descending
select comp;
if (inputList.Any(current => current.MouseUp(e)))
{
return true;
}
if (DragInfo.IsActive) //Drag & dropped into nothing or invalid. Remove dragged obj.
DragInfo.Reset();
return false;
}
}
/// <summary>
/// Handles MouseMove event. Sent to all visible components.
/// </summary>
public virtual void MouseMove(MouseMoveEventArgs e)
{
MousePos = new Vector2i(e.X, e.Y);
if (_console.IsVisible())
{
_console.MouseMove(e);
}
IOrderedEnumerable<IGuiComponent> inputList = from IGuiComponent comp in _components
where comp.ReceiveInput
orderby comp.ZDepth ascending
select comp;
foreach (IGuiComponent current in inputList)
current.MouseMove(e);
}
/// <summary>
/// Handles MouseWheelMove event. Sent to Focused component. Returns true if component accepted and handled the event.
/// </summary>
public virtual void MouseWheelMove(MouseWheelEventArgs e)
{
if (_console.IsVisible())
{
_console.MouseWheelMove(e);
}
IGuiComponent inputTo = (from IGuiComponent comp in _components
where comp.ReceiveInput
where comp.Focus
select comp).FirstOrDefault();
if (inputTo != null) inputTo.MouseWheelMove(e);
}
/// <summary>
/// Handles MouseEntered event. Not sent to Focused component.
/// </summary>
public virtual void MouseEntered(EventArgs e)
{
showCursor = true;
}
/// <summary>
/// Handles MouseLeft event. Not sent to Focused component.
/// </summary>
public virtual void MouseLeft(EventArgs e)
{
showCursor = false;
}
/// <summary>
/// Handles KeyDown event. Returns true if a component accepted and handled the event.
/// </summary>
public virtual bool KeyDown(KeyEventArgs e)
{
if (e.Code == _config.GetCVar<Keyboard.Key>("key.keyboard.console"))
{
_console.ToggleVisible();
return true;
}
if (_console.IsVisible())
{
if (_console.KeyDown(e)) return true;
}
IOrderedEnumerable<IGuiComponent> inputList = from IGuiComponent comp in _components
where comp.ReceiveInput
orderby comp.ZDepth ascending
orderby comp.IsVisible() descending
// Invisible controls still recieve input but after everyone else. This is mostly for the inventory and other toggleable components.
orderby comp.Focus descending
select comp;
return inputList.Any(current => current.KeyDown(e));
}
public virtual bool TextEntered(TextEventArgs e)
{
if (_console.IsVisible())
{
if (_console.TextEntered(e)) return true;
}
IOrderedEnumerable<IGuiComponent> inputList = from IGuiComponent comp in _components
where comp.ReceiveInput
orderby comp.ZDepth ascending
orderby comp.IsVisible() descending
// Invisible controls still recieve input but after everyone else. This is mostly for the inventory and other toggleable components.
orderby comp.Focus descending
select comp;
return inputList.Any(current => current.TextEntered(e));
}
#endregion Input
#region Component retrieval
/// <summary>
/// Returns all components of given type.
/// </summary>
public IEnumerable<IGuiComponent> GetComponentsByType(Type type)
{
return from IGuiComponent comp in _components
where comp.GetType() == type
select comp;
}
/// <summary>
/// Returns the first component with a matching Type or null if none.
/// </summary>
public IGuiComponent GetSingleComponentByType(Type componentType)
{
return (from IGuiComponent comp in _components
where comp.GetType() == componentType
select comp).FirstOrDefault();
}
/// <summary>
/// Returns the first component with a matching GuiComponentType or null if none.
/// </summary>
public IGuiComponent GetSingleComponentByGuiComponentType(GuiComponentType componentType)
{
return (from IGuiComponent comp in _components
where comp.ComponentClass == componentType
select comp).FirstOrDefault();
}
/// <summary>
/// Returns all components with matching GuiComponentType.
/// </summary>
public IEnumerable<IGuiComponent> GetComponentsByGuiComponentType(GuiComponentType componentType)
{
return from IGuiComponent comp in _components
where comp.ComponentClass == componentType
select comp;
}
#endregion Component retrieval
/// <summary>
/// Handles creation of ui elements over network.
/// </summary>
public void HandleElementCreation(NetIncomingMessage msg) //I've opted for hardcoding these in for the moment.
{
/*
var uiType = (CreateUiType)msg.ReadByte();
switch (uiType)
{
}
*/
}
/// <summary>
/// Handles and reroutes Net messages directed at components.
/// </summary>
public void HandleComponentMessage(NetIncomingMessage msg)
{
var component = (GuiComponentType)msg.ReadByte();
IEnumerable<IGuiComponent> targetComponents = GetComponentsByGuiComponentType(component);
foreach (IGuiComponent current in targetComponents)
current.HandleNetworkMessage(msg);
}
#region Update & Render
//These methods are called directly from the main loop to allow for cross-state ui elements. (Console maybe)
/// <summary>
/// Updates the logic of UI components.
/// </summary>
public void Update(FrameEventArgs e)
{
if (_console.IsVisible()) _console.Update(e.FrameDeltaTime);
if (moveMode && movingComp != null)
movingComp.Position = (MousePos - dragOffset);
foreach (IGuiComponent component in _components)
component.Update(e.FrameDeltaTime);
}
/// <summary>
/// Renders UI components to screen.
/// </summary>
public void Render(FrameEventArgs e)
{
IOrderedEnumerable<IGuiComponent> renderList = from IGuiComponent comp in _components
where comp.IsVisible()
orderby comp.Focus ascending
orderby comp.ZDepth ascending
select comp;
foreach (IGuiComponent component in renderList)
{
component.Render();
if (moveMode)
{ /*
CluwneLib.Screen.BlendingMode = BlendingModes.Modulated;
CluwneLib.Screen.FilledRectangle(component.ClientArea.X, component.ClientArea.Y,
component.ClientArea.Width, component.ClientArea.Height,
Color.FromArgb(100, Color.Green));
CluwneLib.Screen.Rectangle(component.ClientArea.X, component.ClientArea.Y, component.ClientArea.Width,
component.ClientArea.Height, Color.LightGreen);
CluwneLib.Screen.BlendingMode = BlendingModes.None;
*/
}
}
if (_console.IsVisible()) _console.Render();
if (showCursor)
{
_cursorSprite = DragInfo.DragSprite != null && DragInfo.IsActive
? DragInfo.DragSprite
: _resourceManager.GetSprite("cursor");
_cursorSprite.Position = MousePos.ToFloat();
_cursorSprite.Draw();
}
}
#endregion Update & Render
}
}