1 /**
2 Copyright: Copyright (c) 2013-2014 Andrey Penechko.
3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module anchovy.gui.guicontext;
8 
9 import anchovy.gui;
10 
11 //version = Debug_guicontext;
12 
13 class GuiContext
14 {
15 	alias WidgetCreator = Widget delegate();
16 	alias LayoutCreator = ILayout delegate();
17 	alias BehaviorCreator = IWidgetBehavior delegate();
18 
19 	WidgetCreator[string] widgetFactories;
20 	LayoutCreator[string] layoutFactories;
21 	BehaviorCreator[][string] behaviorFactories;
22 
23 protected:
24 
25 	Widget[] _roots;
26 	Widget _overlay;
27 
28 	// Key modifiers
29 	uint modifiers;
30 
31 	/// Gui renderer used for drawing all children widgets.
32 	IGuiRenderer	_guiRenderer;
33 
34 	/// Used for timers.
35 	TimerManager	_timerManager;
36 
37 	TemplateManager _templateManager;
38 
39 	WidgetManager _wman;
40 
41 	EventDispatcher _eventDispatcher;
42 
43 	TooltipManager _tooltipManager;
44 
45 	/// This will be called when widget sets clipboard string.
46 	void delegate(dstring newClipboardString) _setClipboardStringCallback;
47 
48 	/// This will be called when widget requests clipboard string.
49 	dstring delegate() _getClipboardStringCallback;
50 
51 public:
52 
53 	this(IGuiRenderer guiRenderer, TimerManager timerMan, TemplateManager templateMan, GuiSkin skin)
54 	in
55 	{
56 		assert(guiRenderer);
57 		assert(skin);
58 		assert(timerMan);
59 	}
60 	body
61 	{
62 		_guiRenderer = guiRenderer;
63 		_timerManager = timerMan;
64 		_templateManager = templateMan;
65 		_wman = WidgetManager(this);
66 		_eventDispatcher = EventDispatcher(this);
67 		_tooltipManager = TooltipManager(this);
68 
69 		_overlay = createWidget("overlay");
70 		_overlay["hasBack"] = false;
71 		_overlay["respondsToPointer"] = false;
72 		_overlay["id"] = "overlay";
73 		_overlay.setProperty!("layout", ILayout) = new AbsoluteLayout;
74 		_overlay.setProperty!"size"(cast(ivec2)_guiRenderer.renderer.windowSize);
75 	}
76 
77 	void update(double deltaTime)
78 	{
79 		_eventDispatcher.update(deltaTime);
80 	}
81 
82 	void addRoot(Widget root)
83 	{
84 		root.setProperty!"size"(cast(ivec2)_guiRenderer.renderer.windowSize);
85 		_roots ~= root;
86 	}
87 
88 	/// Returns widget found by given id.
89 	Widget getWidgetById(string id)
90 	{
91 		return _wman.getWidgetById(id);
92 	}
93 
94 	Widget createWidget(string type, Widget parent = null)
95 	{
96 		return _wman.createWidget(type, parent);
97 	}
98 
99 //+-------------------------------------------------------------------------------+
100 //|                                  Properties                                   |
101 //+-------------------------------------------------------------------------------+
102 
103 @property
104 {
105 	Widget overlay()
106 	{
107 		return _overlay;
108 	}
109 
110 	auto roots()
111 	{
112 		import std.range : chain, repeat;
113 
114 		return chain(_roots, _overlay.repeat(1));
115 	}
116 
117 	TemplateManager templateManager()
118 	{
119 		return _templateManager;
120 	}
121 
122 	TimerManager timerManager()
123 	{
124 		return _timerManager;
125 	}
126 
127 	ref WidgetManager widgetManager()
128 	{
129 		return _wman;
130 	}
131 
132 	ref EventDispatcher eventDispatcher()
133 	{
134 		return _eventDispatcher;
135 	}
136 
137 	ref TooltipManager tooltipManager()
138 	{
139 		return _tooltipManager;
140 	}
141 
142 	IGuiRenderer guiRenderer()
143 	{
144 		return _guiRenderer;
145 	}
146 
147 	/// Sets new size for all root widgets.
148 	void size(ivec2 newSize)
149 	{
150 		foreach(widget; roots)
151 		{
152 			widget.setProperty!"size"(newSize);
153 		}
154 	}
155 
156 	/// Used to get current clipboard string
157 	string clipboardString()
158 	{
159 		if (_getClipboardStringCallback !is null)
160 			return to!string(_getClipboardStringCallback());
161 		else
162 			return "";
163 	}
164 
165 	/// Used to set current clipboard string
166 	void clipboardString(string newString)
167 	{
168 		if (_setClipboardStringCallback !is null)
169 			_setClipboardStringCallback(to!dstring(newString));
170 	}
171 
172 	/// Will be used by window to provide clipboard functionality.
173 	void getClipboardStringCallback(dstring delegate() callback)
174 	{
175 		_getClipboardStringCallback = callback;
176 	}
177 
178 	/// ditto
179 	void setClipboardStringCallback(void delegate(dstring) callback)
180 	{
181 		_setClipboardStringCallback = callback;
182 	}
183 }
184 }