-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGUI_Manager.h
More file actions
441 lines (384 loc) · 14.4 KB
/
GUI_Manager.h
File metadata and controls
441 lines (384 loc) · 14.4 KB
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
/*
This file is part of the GUI library.
Copyright (C) 2008-2013 Benjamin Eikel <benjamin@eikel.org>
Copyright (C) 2008-2012 Claudius Jähn <claudius@uni-paderborn.de>
Copyright (C) 2008-2012 Ralf Petring <ralf@petring.net>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef GUI_MANAGER_H
#define GUI_MANAGER_H
#include "Base/Listener.h"
#include "Components/Component.h"
#include <Util/Graphics/Color.h>
#include <Util/Registry.h>
#include <Util/AttributeProvider.h>
#include <list>
#include <stack>
#include <utility>
// Forward declarations
namespace Util {
class Bitmap;
class FileName;
namespace UI {
union Event;
struct ButtonEvent;
class EventContext;
struct KeyboardEvent;
struct MotionEvent;
class Window;
}
}
namespace Rendering {
class RenderingContext;
} /* Rendering */
namespace GUI {
class AbstractFont;
class AbstractShape;
class Button;
class Checkbox;
class Connector;
class EditorPanel;
class Icon;
class Image;
class ImageData;
class Label;
class ListView;
class NextColumn;
class NextRow;
class Menu;
class Panel;
class DisplayProperty;
class Slider;
class Splitter;
class TabbedPanel;
class Textarea;
class Textfield;
class Window;
class TreeView;
class Entry;
class AnimationHandler;
class Style;
class MouseCursorHandler;
class MouseCursor;
class TooltipHandler;
/***
** GUI_Manager
**/
class GUI_Manager {
public:
// ----------
//! @name Clipboard access
// @{
GUIAPI void copyStringToClipboard(const std::string & s);
GUIAPI std::string getStringFromClipboard() const;
// @}
// ----------
//! @name Main
// @{
public:
typedef Component::flag_t flag_t;
/**
* Create a new GUI manager and associate it with the given event
* context to receive user interface events.
*/
GUIAPI GUI_Manager(Util::UI::EventContext * context=nullptr);
GUIAPI ~GUI_Manager();
GUIAPI bool handleEvent(const Util::UI::Event & e);
#ifdef GUI_BACKEND_RENDERING
GUIAPI void display(Rendering::RenderingContext& rc);
#else // GUI_BACKEND_RENDERING
GUIAPI void display();
#endif // GUI_BACKEND_RENDERING
GUIAPI Geometry::Rect getScreenRect()const;
GUIAPI void setScreenSize(const Geometry::Vec2& size);
//! Associate a window (e.g. X11 or SDL) to the GUI manager
void setWindow(Util::UI::Window * newWindow) {
window = newWindow;
}
//! Access to the associated window
Util::UI::Window * getWindow() {
return window;
}
Util::AttributeProvider userData;
private:
Util::UI::EventContext * eventContext;
Util::UI::Window * window;
std::string alternativeClipboard; // used if no window is available to provide the clipboard.
// @}
// --------------------------------------------------------------------------------
//! @name Animation handling
// @{
public:
GUIAPI void addAnimationHandler(AnimationHandler * );
GUIAPI void finishAnimations(Component * c);
GUIAPI void stopAnimations(Component * c);
private:
GUIAPI void executeAnimations();
typedef std::vector<std::unique_ptr<AnimationHandler>> animationHandlerList_t;
animationHandlerList_t animationHandlerList;
// @}
// ----------
//! @name Cleanup
// @{
public:
GUIAPI void markForRemoval(Component *c);
GUIAPI void cleanup();
private:
std::list<Util::Reference<Component> > removalList;
// @}
// ----------
//! @name Component management
// @{
private:
Component::Ref activeComponent;
Util::Reference<Container> globalContainer;
public:
GUIAPI void registerWindow(Component * w);
GUIAPI void unregisterWindow(Component *w);
GUIAPI void unselectAll();
GUIAPI void setActiveComponent(Component * c);
bool isActiveComponent(const Component * c)const { return activeComponent==c; }
GUIAPI Component * getComponentAtPos(const Geometry::Vec2 & pos);
GUIAPI void selectNext(Component * c);
GUIAPI void selectPrev(Component * c);
GUIAPI bool selectFirst(Component * c);
GUIAPI bool selectLast(Component * c);
Component * getActiveComponent() { return activeComponent.get(); }
//! Check if the component and all its parents are enabled and contained int the global container.
GUIAPI bool isCurrentlyEnabled(Component * c)const;
GUIAPI void closeAllMenus();
// @}
// ----------
//! @name Debug
// @{
private:
uint8_t debugMode; // 0 off, 1 some, 2 all
public:
uint8_t getDebugMode()const { return debugMode; }
void setDebugMode(uint8_t m) { debugMode = m; }
// @}
// ----------
//! @name Event handling & Listener
// @{
private:
ActionListenerRegistry actionListener;
public:
ActionListenerHandle addActionListener(HandleActionFun fun) {
return actionListener.registerElement(std::move(fun));
}
void removeActionListener(ActionListenerHandle handle) {
actionListener.unregisterElement(std::move(handle));
}
private:
typedef std::unordered_map<const Component *, ComponentDestructionListenerRegistry> ComponentDestructionListenerMap;
ComponentDestructionListenerMap componentDestructionListener;
public:
ComponentDestructionListenerHandle addComponentDestructionListener(const Component * component,
HandleComponentDestructionFun fun) {
return componentDestructionListener[component].registerElement(std::move(fun));
}
void removeComponentDestructionListener(const Component * component,
ComponentDestructionListenerHandle handle) {
const auto it = componentDestructionListener.find(component);
if(it != componentDestructionListener.cend()) {
it->second.unregisterElement(std::move(handle));
if(it->second.getElements().empty()) {
componentDestructionListener.erase(it);
}
}
}
private:
typedef std::unordered_map<Component *, DataChangeListenerRegistry> DataChangeListenerMap;
DataChangeListenerMap dataChangeListener;
public:
DataChangeListenerHandle addDataChangeListener(Component * component, HandleDataChangeFun fun) {
return dataChangeListener[component].registerElement(std::move(fun));
}
void removeDataChangeListener(Component * component, DataChangeListenerHandle handle) {
const auto it = dataChangeListener.find(component);
if(it != dataChangeListener.cend()) {
it->second.unregisterElement(std::move(handle));
if(it->second.getElements().empty()) {
dataChangeListener.erase(it);
}
}
}
DataChangeListenerHandle addGlobalDataChangeListener(HandleDataChangeFun fun) {
// Use nullptr as component to access global registry.
return addDataChangeListener(nullptr, std::move(fun));
}
void removeGlobalDataChangeListener(DataChangeListenerHandle handle) {
// Use nullptr as component to access global registry.
removeDataChangeListener(nullptr, std::move(handle));
}
private:
FrameListenerRegistry frameListener;
public:
FrameListenerHandle addFrameListener(FrameListenerFun fun) {
return frameListener.registerElement(std::move(fun));
}
void removeFrameListener(FrameListenerHandle handle) {
frameListener.unregisterElement(std::move(handle));
}
private:
typedef std::unordered_map<Component *, KeyListenerRegistry> KeyListenerMap;
KeyListenerMap keyListener;
public:
KeyListenerHandle addKeyListener(Component * component, HandleKeyFun fun) {
return keyListener[component].registerElement(std::move(fun));
}
void removeKeyListener(Component * component, KeyListenerHandle handle) {
const auto it = keyListener.find(component);
if(it != keyListener.cend()) {
it->second.unregisterElement(std::move(handle));
if(it->second.getElements().empty()) {
keyListener.erase(it);
}
}
}
private:
typedef std::unordered_map<Component *, MouseButtonListenerRegistry> MouseButtonListenerMap;
MouseButtonListenerMap mouseButtonListener;
public:
MouseButtonListenerHandle addMouseButtonListener(Component * component, HandleMouseButtonFun fun) {
return mouseButtonListener[component].registerElement(std::move(fun));
}
void removeMouseButtonListener(Component * component, MouseButtonListenerHandle handle) {
const auto it = mouseButtonListener.find(component);
if(it != mouseButtonListener.cend()) {
it->second.unregisterElement(std::move(handle));
if(it->second.getElements().empty()) {
mouseButtonListener.erase(it);
}
}
}
MouseButtonListenerHandle addGlobalMouseButtonListener(HandleMouseButtonFun fun) {
// Use nullptr as component to access global registry.
return addMouseButtonListener(nullptr, std::move(fun));
}
void removeGlobalMouseButtonListener(MouseButtonListenerHandle handle) {
// Use nullptr as component to access global registry.
removeMouseButtonListener(nullptr, std::move(handle));
}
private:
typedef std::unordered_map<Component *, MouseClickListenerRegistry> MouseClickListenerMap;
MouseClickListenerMap mouseClickListener;
public:
MouseClickListenerHandle addMouseClickListener(Component * component, HandleMouseClickFun fun) {
return mouseClickListener[component].registerElement(std::move(fun));
}
void removeMouseClickListener(Component * component, MouseClickListenerHandle handle) {
const auto it = mouseClickListener.find(component);
if(it != mouseClickListener.cend()) {
it->second.unregisterElement(std::move(handle));
if(it->second.getElements().empty()) {
mouseClickListener.erase(it);
}
}
}
private:
MouseMotionListenerRegistry globalMouseMotionListener;
public:
MouseMotionListenerHandle addGlobalMouseMotionListener(HandleMouseMotionFun fun) {
return globalMouseMotionListener.registerElement(std::move(fun));
}
void removeGlobalMouseMotionListener(MouseMotionListenerHandle handle) {
globalMouseMotionListener.unregisterElement(std::move(handle));
}
GUIAPI void componentActionPerformed(Component *c,const Util::StringIdentifier & actionName);
GUIAPI void componentDataChanged(Component * c);
GUIAPI void componentDestruction(const Component * component);
GUIAPI bool isCtrlPressed() const;
GUIAPI bool isShiftPressed() const;
GUIAPI void enableKeyRepetition(const Util::UI::KeyboardEvent & keyEvent);
GUIAPI void disableKeyRepetition();
private:
std::unique_ptr<std::pair<float,Util::UI::KeyboardEvent>> keyRepeatInfo; // next activation time(sec), keyboard event
GUIAPI bool handleMouseMovement(const Util::UI::MotionEvent & motionEvent);
GUIAPI bool handleMouseButton(const Util::UI::ButtonEvent & buttonEvent);
GUIAPI bool handleKeyEvent(const Util::UI::KeyboardEvent & keyEvent);
// @}
// ----------
//! @name Factories
// @{
public:
GUIAPI Button * createButton(const std::string & text,flag_t flags=0);
GUIAPI Container * createContainer(const Geometry::Rect & r,flag_t flags=0);
GUIAPI Connector * createConnector(flag_t flags=0);
GUIAPI EditorPanel * createEditorPanel(flag_t flags=0);
GUIAPI Panel * createPanel(flag_t flags=0);
GUIAPI Checkbox * createCheckbox(const std::string & text="",bool checked=false,flag_t flags=0);
GUIAPI Icon * createIcon(const Geometry::Vec2 & pos, Util::WeakPointer<ImageData> imageData,const Geometry::Rect & imageRect,flag_t flags=0);
GUIAPI Icon * createIcon(const Geometry::Rect & r,flag_t flags=0);
GUIAPI Image * createImage(const Geometry::Rect & r,flag_t flags=0);
GUIAPI Image * createImage(const Util::FileName & fileName,flag_t flags=0);
GUIAPI Image * createImage(const Util::Bitmap & bitmap,flag_t flags=0);
GUIAPI Label * createLabel(const Geometry::Rect & r,const std::string & text="",flag_t flags=0);
GUIAPI Label * createLabel(const std::string & text="",flag_t flags=0);
GUIAPI ListView * createListView(flag_t flags=0);
GUIAPI Menu * createMenu(flag_t flags=0);
GUIAPI NextColumn * createNextColumn(float additionalSpacing=0.0f);
GUIAPI NextRow * createNextRow(float additionalSpacing=0.0f);
GUIAPI Slider * createSlider(const Geometry::Rect & r,float left=0,float right=1,int steps=10,flag_t flags=0);
GUIAPI Splitter * createVSplitter(flag_t flags=0);
GUIAPI Splitter * createHSplitter(flag_t flags=0);
GUIAPI TabbedPanel * createTabbedPanel(flag_t flags=0);
GUIAPI Textarea * createTextarea(const std::string &text="",flag_t flags=0);
GUIAPI Textfield * createTextfield(const std::string &text="",flag_t flags=0);
GUIAPI TreeView * createTreeView(const Geometry::Rect & r,flag_t flags=0);
GUIAPI Container * createTreeViewEntry(Component * c);
GUIAPI Window * createWindow(const Geometry::Rect & r,const std::string & title="",flag_t flags=0);
// @}
// ----------
//! @name Invalidated regions
// @{
public:
GUIAPI void invalidateRegion(const Geometry::Rect & region);
void enableLazyRendering() { lazyRendering = true; }
void disableLazyRendering() { lazyRendering = false; }
bool isLazyRenderingEnabled()const { return lazyRendering; }
private:
Geometry::Rect invalidRegion;
bool lazyRendering;
// @}
// ----------
//! @name Properties, shapes, style and mouse cursor
// @{
private:
std::unique_ptr<StyleManager> style;
public:
StyleManager & getStyleManager()const { return *style; }
GUIAPI void displayLineShape(const propertyId_t id,const std::vector<Geometry::Vec2> & points,uint16_t flags=0);
GUIAPI void displayShape(const propertyId_t id,const Geometry::Rect & rect,uint16_t flags=0);
GUIAPI void disableProperty(const Util::Reference<DisplayProperty> & p)const;
GUIAPI void enableProperty(const Util::Reference<DisplayProperty> & p)const;
GUIAPI Util::Color4ub getActiveColor(const propertyId_t id)const;
GUIAPI AbstractFont * getActiveFont(const propertyId_t id)const;
GUIAPI AbstractFont * getDefaultFont(const propertyId_t id)const;
GUIAPI float getGlobalValue(const propertyId_t id)const;
GUIAPI void registerMouseCursor(const propertyName_t & id, const Util::Reference<Util::Bitmap> & bitmap, const uint32_t clickX, const uint32_t clickY);
GUIAPI void removeMouseCursor(const propertyName_t & id);
GUIAPI void setDefaultColor(const propertyId_t id,const Util::Color4ub & c);
GUIAPI void setDefaultFont(const propertyId_t id,AbstractFont * f);
GUIAPI void setDefaultShape(const propertyId_t id,AbstractShape * f);
GUIAPI void setGlobalValue(const propertyId_t id,float v);
// @}
// ----------
//! @name Scissor
// @{
public:
std::stack<Geometry::Rect_i> scissors;
GUIAPI void pushScissor(const Geometry::Rect_i & r);
GUIAPI void popScissor();
// @}
//! @name Internal state
// @{
private:
std::unique_ptr<MouseCursorHandler> mouseCursorHandler;
std::unique_ptr<TooltipHandler> tooltipHandler;
// @}
};
}
#endif // GUI_MANAGER_H