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
|
/*************************************************************************/
/* scroll_container.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "scroll_container.h"
#include "os/os.h"
bool ScrollContainer::clips_input() const {
return true;
}
Size2 ScrollContainer::get_minimum_size() const {
return Size2(1, 1);
};
void ScrollContainer::_cancel_drag() {
set_fixed_process(false);
drag_touching_deaccel=false;
drag_touching=false;
drag_speed=Vector2();
drag_accum=Vector2();
last_drag_accum=Vector2();
drag_from=Vector2();
}
void ScrollContainer::_input_event(const InputEvent& p_input_event) {
switch(p_input_event.type) {
case InputEvent::MOUSE_BUTTON: {
const InputEventMouseButton &mb=p_input_event.mouse_button;
if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed && v_scroll->is_visible()) {
v_scroll->set_val( v_scroll->get_val()-v_scroll->get_page()/8 );
}
if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed && v_scroll->is_visible()) {
v_scroll->set_val( v_scroll->get_val()+v_scroll->get_page()/8 );
}
if(!OS::get_singleton()->has_touchscreen_ui_hint())
return;
if (mb.button_index!=BUTTON_LEFT)
break;
if (mb.pressed) {
if (drag_touching) {
set_fixed_process(false);
drag_touching_deaccel=false;
drag_touching=false;
drag_speed=Vector2();
drag_accum=Vector2();
last_drag_accum=Vector2();
drag_from=Vector2();
}
if (true) {
drag_speed=Vector2();
drag_accum=Vector2();
last_drag_accum=Vector2();
drag_from=Vector2(h_scroll->get_val(),v_scroll->get_val());
drag_touching=OS::get_singleton()->has_touchscreen_ui_hint();
drag_touching_deaccel=false;
time_since_motion=0;
if (drag_touching) {
set_fixed_process(true);
time_since_motion=0;
}
}
} else {
if (drag_touching) {
if (drag_speed==Vector2()) {
drag_touching_deaccel=false;
drag_touching=false;
set_fixed_process(false);
} else {
drag_touching_deaccel=true;
}
}
}
} break;
case InputEvent::MOUSE_MOTION: {
const InputEventMouseMotion &mm=p_input_event.mouse_motion;
if (drag_touching && ! drag_touching_deaccel) {
Vector2 motion = Vector2(mm.relative_x,mm.relative_y);
drag_accum-=motion;
Vector2 diff = drag_from+drag_accum;
if (scroll_h)
h_scroll->set_val(diff.x);
else
drag_accum.x=0;
if (scroll_v)
v_scroll->set_val(diff.y);
else
drag_accum.y=0;
time_since_motion=0;
}
} break;
}
}
void ScrollContainer::_update_scrollbar_pos() {
Size2 size = get_size();
Size2 hmin = h_scroll->get_combined_minimum_size();
Size2 vmin = v_scroll->get_combined_minimum_size();
v_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,vmin.width);
v_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
v_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,0);
v_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
h_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,0);
h_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
h_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,hmin.height);
h_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
h_scroll->raise();
v_scroll->raise();
}
void ScrollContainer::_notification(int p_what) {
if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
call_deferred("_update_scrollbar_pos");
};
if (p_what==NOTIFICATION_SORT_CHILDREN) {
child_max_size = Size2(0, 0);
Size2 size = get_size();
for(int i=0;i<get_child_count();i++) {
Control *c = get_child(i)->cast_to<Control>();
if (!c)
continue;
if (c->is_set_as_toplevel())
continue;
if (c == h_scroll || c == v_scroll)
continue;
Size2 minsize = c->get_combined_minimum_size();
child_max_size.x = MAX(child_max_size.x, minsize.x);
child_max_size.y = MAX(child_max_size.y, minsize.y);
Rect2 r = Rect2(-scroll,minsize);
if (!scroll_h) {
r.pos.x=0;
r.size.width=size.width;
}
if (!scroll_v) {
r.pos.y=0;
r.size.height=size.height;
}
fit_child_in_rect(c,r);
}
update();
};
if (p_what == NOTIFICATION_DRAW) {
update_scrollbars();
VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(),true);
}
if (p_what==NOTIFICATION_FIXED_PROCESS) {
if (drag_touching) {
if (drag_touching_deaccel) {
Vector2 pos = Vector2(h_scroll->get_val(),v_scroll->get_val());
pos+=drag_speed*get_fixed_process_delta_time();
bool turnoff_h=false;
bool turnoff_v=false;
if (pos.x<0) {
pos.x=0;
turnoff_h=true;
}
if (pos.x > (h_scroll->get_max()-h_scroll->get_page())) {
pos.x=h_scroll->get_max()-h_scroll->get_page();
turnoff_h=true;
}
if (pos.y<0) {
pos.y=0;
turnoff_v=true;
}
if (pos.y > (v_scroll->get_max()-v_scroll->get_page())) {
pos.y=v_scroll->get_max()-v_scroll->get_page();
turnoff_v=true;
}
if (scroll_h)
h_scroll->set_val(pos.x);
if (scroll_v)
v_scroll->set_val(pos.y);
float sgn_x = drag_speed.x<0? -1 : 1;
float val_x = Math::abs(drag_speed.x);
val_x-=1000*get_fixed_process_delta_time();
if (val_x<0) {
turnoff_h=true;
}
float sgn_y = drag_speed.y<0? -1 : 1;
float val_y = Math::abs(drag_speed.y);
val_y-=1000*get_fixed_process_delta_time();
if (val_y<0) {
turnoff_v=true;
}
drag_speed=Vector2(sgn_x*val_x,sgn_y*val_y);
if (turnoff_h && turnoff_v) {
set_fixed_process(false);
drag_touching=false;
drag_touching_deaccel=false;
}
} else {
if (time_since_motion==0 || time_since_motion>0.1) {
Vector2 diff = drag_accum - last_drag_accum;
last_drag_accum=drag_accum;
drag_speed=diff/get_fixed_process_delta_time();
}
time_since_motion+=get_fixed_process_delta_time();
}
}
}
};
void ScrollContainer::update_scrollbars() {
Size2 size = get_size();
Size2 hmin = h_scroll->get_combined_minimum_size();
Size2 vmin = v_scroll->get_combined_minimum_size();
Size2 min = child_max_size;
if (!scroll_v || min.height <= size.height - hmin.height) {
v_scroll->hide();
scroll.y=0;
} else {
v_scroll->show();
scroll.y=v_scroll->get_val();
}
v_scroll->set_max(min.height);
v_scroll->set_page(size.height - hmin.height);
if (!scroll_h || min.width <= size.width - vmin.width) {
h_scroll->hide();
scroll.x=0;
} else {
h_scroll->show();
h_scroll->set_max(min.width);
h_scroll->set_page(size.width - vmin.width);
scroll.x=h_scroll->get_val();
}
}
void ScrollContainer::_scroll_moved(float) {
scroll.x=h_scroll->get_val();
scroll.y=v_scroll->get_val();
queue_sort();
update();
};
void ScrollContainer::set_enable_h_scroll(bool p_enable) {
scroll_h=p_enable;
queue_sort();
}
bool ScrollContainer::is_h_scroll_enabled() const{
return scroll_h;
}
void ScrollContainer::set_enable_v_scroll(bool p_enable) {
scroll_v=p_enable;
queue_sort();
}
bool ScrollContainer::is_v_scroll_enabled() const{
return scroll_v;
}
int ScrollContainer::get_v_scroll() const {
return v_scroll->get_val();
}
void ScrollContainer::set_v_scroll(int p_pos) {
v_scroll->set_val(p_pos);
_cancel_drag();
}
int ScrollContainer::get_h_scroll() const {
return h_scroll->get_val();
}
void ScrollContainer::set_h_scroll(int p_pos) {
h_scroll->set_val(p_pos);
_cancel_drag();
}
void ScrollContainer::_bind_methods() {
ObjectTypeDB::bind_method(_MD("_scroll_moved"),&ScrollContainer::_scroll_moved);
ObjectTypeDB::bind_method(_MD("_input_event"),&ScrollContainer::_input_event);
ObjectTypeDB::bind_method(_MD("set_enable_h_scroll","enable"),&ScrollContainer::set_enable_h_scroll);
ObjectTypeDB::bind_method(_MD("is_h_scroll_enabled"),&ScrollContainer::is_h_scroll_enabled);
ObjectTypeDB::bind_method(_MD("set_enable_v_scroll","enable"),&ScrollContainer::set_enable_v_scroll);
ObjectTypeDB::bind_method(_MD("is_v_scroll_enabled"),&ScrollContainer::is_v_scroll_enabled);
ObjectTypeDB::bind_method(_MD("_update_scrollbar_pos"),&ScrollContainer::_update_scrollbar_pos);
ObjectTypeDB::bind_method(_MD("set_h_scroll","val"),&ScrollContainer::set_h_scroll);
ObjectTypeDB::bind_method(_MD("get_h_scroll"),&ScrollContainer::get_h_scroll);
ObjectTypeDB::bind_method(_MD("set_v_scroll","val"),&ScrollContainer::set_v_scroll);
ObjectTypeDB::bind_method(_MD("get_v_scroll"),&ScrollContainer::get_v_scroll);
ADD_PROPERTY( PropertyInfo( Variant::BOOL, "scroll/horizontal"), _SCS("set_enable_h_scroll"),_SCS("is_h_scroll_enabled"));
ADD_PROPERTY( PropertyInfo( Variant::BOOL, "scroll/vertical"), _SCS("set_enable_v_scroll"),_SCS("is_v_scroll_enabled"));
};
ScrollContainer::ScrollContainer() {
h_scroll = memnew(HScrollBar);
h_scroll->set_name("_h_scroll");
add_child(h_scroll);
v_scroll = memnew(VScrollBar);
v_scroll->set_name("_v_scroll");
add_child(v_scroll);
h_scroll->connect("value_changed", this,"_scroll_moved");
v_scroll->connect("value_changed", this,"_scroll_moved");
drag_speed=Vector2();
drag_touching=false;
drag_touching_deaccel=false;
scroll_h=true;
scroll_v=true;
};
|