-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToolGroup.cpp
More file actions
313 lines (244 loc) · 6.7 KB
/
ToolGroup.cpp
File metadata and controls
313 lines (244 loc) · 6.7 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
#include 'tools/Tool.cpp';
#include '../../lib/ui3/elements/Button.cpp';
#include '../../lib/ui3/elements/Image.cpp';
#include '../../lib/ui3/utils/ButtonGroup.cpp';
class ToolGroup
{
AdvToolScript@ script;
string name;
array<Tool@> tools;
Button@ button;
Image@ icon;
private ButtonGroup@ button_group;
private Tool@ current_tool;
private array<Tool@> selectable_tools;
private int num_selectable_tools;
private PopupOptions@ popup;
private Container@ popup_content;
ToolGroup()
{
}
void create(AdvToolScript@ script, const string name, ButtonGroup@ button_group)
{
@this.script = script;
this.name = name;
@this.button_group = button_group;
@button = create_button(name, null, '', '');
button.mouse_enter.on(EventCallback(on_button_enter));
@icon = cast<Image@>(button.content);
}
void init_ui()
{
for(uint i = 0; i < tools.length(); i++)
{
Tool@ tool = @tools[i];
if(@tool.toolbar_button == null)
continue;
Image@ icon = tool.toolbar_button.icon;
icon.set_sprite(tool.icon_sprite_set,
tool.icon_sprite_name, tool.icon_width, tool.icon_height,
tool.icon_offset_x, tool.icon_offset_y);
icon.rotation = tool.icon_rotation;
icon.width = script.config.ToolbarIconSize;
icon.height = script.config.ToolbarIconSize;
icon.sizing = ImageSize::FitInside;
}
}
void on_init()
{
for(uint i = 0; i < tools.length(); i++)
{
Tool@ tool = tools[i];
@tool.group = this;
tool.on_init();
}
}
Button@ create_button(const string name, ShortcutKey@ shortcut_key,
const string sprite_set, const string sprite_name,
const float width=-1, const float height=-1,
const float offset_x=0, const float offset_y=0)
{
Image@ icon = Image(script.ui, sprite_set, sprite_name, width, height, offset_x, offset_y);
icon.width = script.config.ToolbarIconSize;
icon.height = script.config.ToolbarIconSize;
icon.sizing = ImageSize::FitInside;
Button@ button = Button(script.ui, icon);
button.name = name;
button.selectable = true;
button.fit_to_contents();
@button.group = button_group;
@button.tooltip = PopupOptions(script.ui, get_tooltip(name, shortcut_key), false, PopupPosition::Below);
return button;
}
void add_tool(Tool@ tool)
{
tools.insertLast(@tool);
if(tool.selectable)
{
selectable_tools.insertLast(tool);
num_selectable_tools++;
}
if(num_selectable_tools > 1)
{
if(num_selectable_tools == 2)
{
create_popup();
}
else
{
create_popup_button(tool);
}
}
tool.create(this);
if(@current_tool == null)
{
set_tool(tool);
}
}
void set_tool(Tool@ tool)
{
@current_tool = tool;
update_tooltip();
button.name = tool.name;
icon.set_sprite(tool.icon_sprite_set, tool.icon_sprite_name, tool.icon_width, tool.icon_height, tool.icon_offset_x, tool.icon_offset_y);
icon.width = script.config.ToolbarIconSize;
icon.height = script.config.ToolbarIconSize;
update_popup_buttons();
}
Tool@ get_next_selectable_tool(Tool@ tool, const int dir=1)
{
if(num_selectable_tools == 0)
return null;
if(@tool == null)
return selectable_tools[0];
int index = selectable_tools.findByRef(tool);
if(index == -1)
return selectable_tools[0];
index += dir >= 0 ? 1 : -1;
if(index < 0 || index >= num_selectable_tools)
return null;
return selectable_tools[index];
}
Tool@ get_first_selectable_tool()
{
return num_selectable_tools > 0
? selectable_tools[0] : null;
}
Tool@ get_last_selectable_tool()
{
return num_selectable_tools > 0
? selectable_tools[num_selectable_tools - 1] : null;
}
void on_select()
{
button.selected = true;
button.override_alpha = 1;
}
void on_deselect()
{
button.selected = false;
button.override_alpha = -1;
}
void on_settings_loaded()
{
Image@ icon = button.icon;
icon.width = script.config.ToolbarIconSize;
icon.height = script.config.ToolbarIconSize;
button.fit_to_contents();
for(uint i = 0; i < tools.length; i++)
{
Button@ button = tools[i].toolbar_button;
if(@button == null)
continue;
@icon = button.icon;
if(@icon == null)
continue;
icon.width = script.config.ToolbarIconSize;
icon.height = script.config.ToolbarIconSize;
button.fit_to_contents();
}
update_tooltip();
}
private void update_tooltip()
{
button.tooltip.content_string = get_tooltip(current_tool.name, current_tool.key);
}
private void create_popup()
{
if(@popup != null)
return;
@popup_content = Container(script.ui);
popup_content.mouse_self = false;
@popup = PopupOptions(script.ui, popup_content, true, PopupPosition::Below, PopupTriggerType::Manual);
popup.mouse_self = false;
popup.padding = script.ui.style.spacing;
popup.spacing = 0;
popup.show.on(EventCallback(on_popup_show));
popup.hide.on(EventCallback(on_popup_hide));
for(uint i = 0; i < tools.length(); i++)
{
create_popup_button(tools[i]);
}
popup_content.fit_to_contents(true);
}
private void create_popup_button(Tool@ tool)
{
if(!tool.selectable)
return;
@tool.toolbar_button = create_button(tool.name, tool.key,
tool.icon_sprite_set, tool.icon_sprite_name, tool.icon_width, tool.icon_height,
tool.icon_offset_x, tool.icon_offset_y);
popup_content.add_child(tool.toolbar_button);
}
private void update_popup_buttons()
{
float y = 0;
for(uint i = 0; i < tools.length(); i++)
{
Tool@ tool = tools[i];
Button@ button = tool.toolbar_button;
if(@button == null)
return;
button.visible = @tool != @current_tool;
if(!button.visible)
continue;
button.y = y;
y += button.height + script.ui.style.spacing;
}
button.tooltip.spacing = script.ui.style.tooltip_default_spacing + y;
y = 0;
for(int i = int(tools.length()) - 1; i >= 0; i--)
{
Button@ button = tools[i].toolbar_button;
if(@button == null || !button.visible)
continue;
button.tooltip.spacing = script.ui.style.tooltip_default_spacing + y;
y += button.height + script.ui.style.spacing;
}
popup_content.fit_to_contents(true);
}
private string get_tooltip(const string name, ShortcutKey@ key)
{
if(@key == null || !key.is_set() || !script.config.EnableShortcuts)
return name;
return name + ' [' + key.to_string() + ']';
}
// //////////////////////////////////////////////////////////
// Events
// //////////////////////////////////////////////////////////
void on_button_enter(EventInfo@ event)
{
if(tools.length() <= 1)
return;
update_popup_buttons();
script.ui.show_tooltip(popup, event.target);
}
void on_popup_show(EventInfo@ event)
{
script.track_tool_group_popups(true);
}
void on_popup_hide(EventInfo@ event)
{
script.track_tool_group_popups(false);
}
}