]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiScrollBar.cpp
Inventory: Send dirty lists where appropriate (#8742)
[dragonfireclient.git] / src / gui / guiScrollBar.cpp
1 /*
2 Copyright (C) 2002-2013 Nikolaus Gebhardt
3 This file is part of the "Irrlicht Engine".
4 For conditions of distribution and use, see copyright notice in irrlicht.h
5
6 Modified 2019.05.01 by stujones11, Stuart Jones <stujones111@gmail.com>
7
8 This is a heavily modified copy of the Irrlicht CGUIScrollBar class
9 which includes automatic scaling of the thumb slider and hiding of
10 the arrow buttons where there is insufficient space.
11 */
12
13 #include "guiScrollBar.h"
14 #include <IGUIButton.h>
15 #include <IGUISkin.h>
16
17 GUIScrollBar::GUIScrollBar(IGUIEnvironment *environment, IGUIElement *parent, s32 id,
18                 core::rect<s32> rectangle, bool horizontal, bool auto_scale) :
19                 IGUIElement(EGUIET_ELEMENT, environment, parent, id, rectangle),
20                 up_button(nullptr), down_button(nullptr), is_dragging(false),
21                 is_horizontal(horizontal), is_auto_scaling(auto_scale),
22                 dragged_by_slider(false), tray_clicked(false), scroll_pos(0),
23                 draw_center(0), thumb_size(0), min_pos(0), max_pos(100), small_step(10),
24                 large_step(50), last_change(0), drag_offset(0), page_size(100), border_size(0)
25 {
26         refreshControls();
27         setNotClipped(false);
28         setTabStop(true);
29         setTabOrder(-1);
30         setPos(0);
31 }
32
33 bool GUIScrollBar::OnEvent(const SEvent &event)
34 {
35         if (isEnabled()) {
36                 switch (event.EventType) {
37                 case EET_KEY_INPUT_EVENT:
38                         if (event.KeyInput.PressedDown) {
39                                 const s32 old_pos = scroll_pos;
40                                 bool absorb = true;
41                                 switch (event.KeyInput.Key) {
42                                 case KEY_LEFT:
43                                 case KEY_UP:
44                                         setPos(scroll_pos - small_step);
45                                         break;
46                                 case KEY_RIGHT:
47                                 case KEY_DOWN:
48                                         setPos(scroll_pos + small_step);
49                                         break;
50                                 case KEY_HOME:
51                                         setPos(min_pos);
52                                         break;
53                                 case KEY_PRIOR:
54                                         setPos(scroll_pos - large_step);
55                                         break;
56                                 case KEY_END:
57                                         setPos(max_pos);
58                                         break;
59                                 case KEY_NEXT:
60                                         setPos(scroll_pos + large_step);
61                                         break;
62                                 default:
63                                         absorb = false;
64                                 }
65                                 if (scroll_pos != old_pos) {
66                                         SEvent e;
67                                         e.EventType = EET_GUI_EVENT;
68                                         e.GUIEvent.Caller = this;
69                                         e.GUIEvent.Element = nullptr;
70                                         e.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
71                                         Parent->OnEvent(e);
72                                 }
73                                 if (absorb)
74                                         return true;
75                         }
76                         break;
77                 case EET_GUI_EVENT:
78                         if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED) {
79                                 if (event.GUIEvent.Caller == up_button)
80                                         setPos(scroll_pos - small_step);
81                                 else if (event.GUIEvent.Caller == down_button)
82                                         setPos(scroll_pos + small_step);
83
84                                 SEvent e;
85                                 e.EventType = EET_GUI_EVENT;
86                                 e.GUIEvent.Caller = this;
87                                 e.GUIEvent.Element = nullptr;
88                                 e.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
89                                 Parent->OnEvent(e);
90                                 return true;
91                         } else if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
92                                 if (event.GUIEvent.Caller == this)
93                                         is_dragging = false;
94                         break;
95                 case EET_MOUSE_INPUT_EVENT: {
96                         const core::position2di p(event.MouseInput.X, event.MouseInput.Y);
97                         bool is_inside = isPointInside(p);
98                         switch (event.MouseInput.Event) {
99                         case EMIE_MOUSE_WHEEL:
100                                 if (Environment->hasFocus(this)) {
101                                         s8 d = event.MouseInput.Wheel < 0 ? -1 : 1;
102                                         s8 h = is_horizontal ? 1 : -1;
103                                         setPos(getPos() + (d * small_step * h));
104
105                                         SEvent e;
106                                         e.EventType = EET_GUI_EVENT;
107                                         e.GUIEvent.Caller = this;
108                                         e.GUIEvent.Element = nullptr;
109                                         e.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
110                                         Parent->OnEvent(e);
111                                         return true;
112                                 }
113                                 break;
114                         case EMIE_LMOUSE_PRESSED_DOWN: {
115                                 if (is_inside) {
116                                         is_dragging = true;
117                                         dragged_by_slider = slider_rect.isPointInside(p);
118                                         core::vector2di corner = slider_rect.UpperLeftCorner;
119                                         drag_offset = is_horizontal ? p.X - corner.X : p.Y - corner.Y;
120                                         tray_clicked = !dragged_by_slider;
121                                         if (tray_clicked) {
122                                                 const s32 new_pos = getPosFromMousePos(p);
123                                                 const s32 old_pos = scroll_pos;
124                                                 setPos(new_pos);
125                                                 // drag in the middle
126                                                 drag_offset = thumb_size / 2;
127                                                 // report the scroll event
128                                                 if (scroll_pos != old_pos && Parent) {
129                                                         SEvent e;
130                                                         e.EventType = EET_GUI_EVENT;
131                                                         e.GUIEvent.Caller = this;
132                                                         e.GUIEvent.Element = nullptr;
133                                                         e.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
134                                                         Parent->OnEvent(e);
135                                                 }
136                                         }
137                                         Environment->setFocus(this);
138                                         return true;
139                                 }
140                                 break;
141                         }
142                         case EMIE_LMOUSE_LEFT_UP:
143                         case EMIE_MOUSE_MOVED: {
144                                 if (!event.MouseInput.isLeftPressed())
145                                         is_dragging = false;
146
147                                 if (!is_dragging) {
148                                         if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
149                                                 break;
150                                         return is_inside;
151                                 }
152
153                                 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
154                                         is_dragging = false;
155
156                                 // clang-format off
157                                 if (!dragged_by_slider) {
158                                         if (is_inside) {
159                                                 dragged_by_slider = slider_rect.isPointInside(p);
160                                                 tray_clicked = !dragged_by_slider;
161                                         }
162                                         if (!dragged_by_slider) {
163                                                 tray_clicked = false;
164                                                 if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
165                                                         return is_inside;
166                                         }
167                                 }
168                                 // clang-format on
169
170                                 const s32 new_pos = getPosFromMousePos(p);
171                                 const s32 old_pos = scroll_pos;
172
173                                 setPos(new_pos);
174
175                                 if (scroll_pos != old_pos && Parent) {
176                                         SEvent e;
177                                         e.EventType = EET_GUI_EVENT;
178                                         e.GUIEvent.Caller = this;
179                                         e.GUIEvent.Element = nullptr;
180                                         e.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
181                                         Parent->OnEvent(e);
182                                 }
183                                 return is_inside;
184                         }
185                         default:
186                                 break;
187                         }
188                 } break;
189                 default:
190                         break;
191                 }
192         }
193         return IGUIElement::OnEvent(event);
194 }
195
196 void GUIScrollBar::draw()
197 {
198         if (!IsVisible)
199                 return;
200
201         IGUISkin *skin = Environment->getSkin();
202         if (!skin)
203                 return;
204
205         video::SColor icon_color = skin->getColor(
206                         isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL);
207         if (icon_color != current_icon_color)
208                 refreshControls();
209
210         slider_rect = AbsoluteRect;
211         skin->draw2DRectangle(this, skin->getColor(EGDC_SCROLLBAR), slider_rect,
212                         &AbsoluteClippingRect);
213
214         if (core::isnotzero(range())) {
215                 if (is_horizontal) {
216                         slider_rect.UpperLeftCorner.X = AbsoluteRect.UpperLeftCorner.X +
217                                                         draw_center - thumb_size / 2;
218                         slider_rect.LowerRightCorner.X =
219                                         slider_rect.UpperLeftCorner.X + thumb_size;
220                 } else {
221                         slider_rect.UpperLeftCorner.Y = AbsoluteRect.UpperLeftCorner.Y +
222                                                         draw_center - thumb_size / 2;
223                         slider_rect.LowerRightCorner.Y =
224                                         slider_rect.UpperLeftCorner.Y + thumb_size;
225                 }
226                 skin->draw3DButtonPaneStandard(this, slider_rect, &AbsoluteClippingRect);
227         }
228         IGUIElement::draw();
229 }
230
231 void GUIScrollBar::updateAbsolutePosition()
232 {
233         IGUIElement::updateAbsolutePosition();
234         refreshControls();
235         setPos(scroll_pos);
236 }
237
238 s32 GUIScrollBar::getPosFromMousePos(const core::position2di &pos) const
239 {
240         s32 w, p;
241         s32 offset = dragged_by_slider ? drag_offset : thumb_size / 2;
242
243         if (is_horizontal) {
244                 w = RelativeRect.getWidth() - border_size * 2 - thumb_size;
245                 p = pos.X - AbsoluteRect.UpperLeftCorner.X - border_size - offset;
246         } else {
247                 w = RelativeRect.getHeight() - border_size * 2 - thumb_size;
248                 p = pos.Y - AbsoluteRect.UpperLeftCorner.Y - border_size - offset;
249         }
250         return core::isnotzero(range()) ? s32(f32(p) / f32(w) * range()) + min_pos : 0;
251 }
252
253 void GUIScrollBar::setPos(const s32 &pos)
254 {
255         s32 thumb_area = 0;
256         s32 thumb_min = 0;
257
258         if (is_horizontal) {
259                 thumb_min = RelativeRect.getHeight();
260                 thumb_area = RelativeRect.getWidth() - border_size * 2;
261         } else {
262                 thumb_min = RelativeRect.getWidth();
263                 thumb_area = RelativeRect.getHeight() - border_size * 2;
264         }
265
266         if (is_auto_scaling)
267                 thumb_size = s32(thumb_area /
268                                  (f32(page_size) / f32(thumb_area + border_size * 2)));
269
270         thumb_size = core::s32_clamp(thumb_size, thumb_min, thumb_area);
271         scroll_pos = core::s32_clamp(pos, min_pos, max_pos);
272
273         f32 f = core::isnotzero(range()) ? (f32(thumb_area) - f32(thumb_size)) / range()
274                                          : 1.0f;
275         draw_center = s32((f32(scroll_pos) * f) + (f32(thumb_size) * 0.5f)) + border_size;
276 }
277
278 void GUIScrollBar::setSmallStep(const s32 &step)
279 {
280         small_step = step > 0 ? step : 10;
281 }
282
283 void GUIScrollBar::setLargeStep(const s32 &step)
284 {
285         large_step = step > 0 ? step : 50;
286 }
287
288 void GUIScrollBar::setMax(const s32 &max)
289 {
290         max_pos = max;
291         if (min_pos > max_pos)
292                 min_pos = max_pos;
293
294         bool enable = core::isnotzero(range());
295         up_button->setEnabled(enable);
296         down_button->setEnabled(enable);
297         setPos(scroll_pos);
298 }
299
300 void GUIScrollBar::setMin(const s32 &min)
301 {
302         min_pos = min;
303         if (max_pos < min_pos)
304                 max_pos = min_pos;
305
306         bool enable = core::isnotzero(range());
307         up_button->setEnabled(enable);
308         down_button->setEnabled(enable);
309         setPos(scroll_pos);
310 }
311
312 void GUIScrollBar::setPageSize(const s32 &size)
313 {
314         page_size = size;
315         setPos(scroll_pos);
316 }
317
318 s32 GUIScrollBar::getPos() const
319 {
320         return scroll_pos;
321 }
322
323 void GUIScrollBar::refreshControls()
324 {
325         IGUISkin *skin = Environment->getSkin();
326         IGUISpriteBank *sprites = nullptr;
327         current_icon_color = video::SColor(255, 255, 255, 255);
328
329         if (skin) {
330                 sprites = skin->getSpriteBank();
331                 current_icon_color =
332                                 skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL
333                                                            : EGDC_GRAY_WINDOW_SYMBOL);
334         }
335         if (is_horizontal) {
336                 s32 h = RelativeRect.getHeight();
337                 border_size = RelativeRect.getWidth() < h * 4 ? 0 : h;
338                 if (!up_button) {
339                         up_button = Environment->addButton(
340                                         core::rect<s32>(0, 0, h, h), this);
341                         up_button->setSubElement(true);
342                         up_button->setTabStop(false);
343                 }
344                 if (sprites) {
345                         up_button->setSpriteBank(sprites);
346                         up_button->setSprite(EGBS_BUTTON_UP,
347                                         s32(skin->getIcon(EGDI_CURSOR_LEFT)),
348                                         current_icon_color);
349                         up_button->setSprite(EGBS_BUTTON_DOWN,
350                                         s32(skin->getIcon(EGDI_CURSOR_LEFT)),
351                                         current_icon_color);
352                 }
353                 up_button->setRelativePosition(core::rect<s32>(0, 0, h, h));
354                 up_button->setAlignment(EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT,
355                                 EGUIA_LOWERRIGHT);
356                 if (!down_button) {
357                         down_button = Environment->addButton(
358                                         core::rect<s32>(RelativeRect.getWidth() - h, 0,
359                                                         RelativeRect.getWidth(), h),
360                                         this);
361                         down_button->setSubElement(true);
362                         down_button->setTabStop(false);
363                 }
364                 if (sprites) {
365                         down_button->setSpriteBank(sprites);
366                         down_button->setSprite(EGBS_BUTTON_UP,
367                                         s32(skin->getIcon(EGDI_CURSOR_RIGHT)),
368                                         current_icon_color);
369                         down_button->setSprite(EGBS_BUTTON_DOWN,
370                                         s32(skin->getIcon(EGDI_CURSOR_RIGHT)),
371                                         current_icon_color);
372                 }
373                 down_button->setRelativePosition(
374                                 core::rect<s32>(RelativeRect.getWidth() - h, 0,
375                                                 RelativeRect.getWidth(), h));
376                 down_button->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT,
377                                 EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
378         } else {
379                 s32 w = RelativeRect.getWidth();
380                 border_size = RelativeRect.getHeight() < w * 4 ? 0 : w;
381                 if (!up_button) {
382                         up_button = Environment->addButton(
383                                         core::rect<s32>(0, 0, w, w), this);
384                         up_button->setSubElement(true);
385                         up_button->setTabStop(false);
386                 }
387                 if (sprites) {
388                         up_button->setSpriteBank(sprites);
389                         up_button->setSprite(EGBS_BUTTON_UP,
390                                         s32(skin->getIcon(EGDI_CURSOR_UP)),
391                                         current_icon_color);
392                         up_button->setSprite(EGBS_BUTTON_DOWN,
393                                         s32(skin->getIcon(EGDI_CURSOR_UP)),
394                                         current_icon_color);
395                 }
396                 up_button->setRelativePosition(core::rect<s32>(0, 0, w, w));
397                 up_button->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT,
398                                 EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
399                 if (!down_button) {
400                         down_button = Environment->addButton(
401                                         core::rect<s32>(0, RelativeRect.getHeight() - w,
402                                                         w, RelativeRect.getHeight()),
403                                         this);
404                         down_button->setSubElement(true);
405                         down_button->setTabStop(false);
406                 }
407                 if (sprites) {
408                         down_button->setSpriteBank(sprites);
409                         down_button->setSprite(EGBS_BUTTON_UP,
410                                         s32(skin->getIcon(EGDI_CURSOR_DOWN)),
411                                         current_icon_color);
412                         down_button->setSprite(EGBS_BUTTON_DOWN,
413                                         s32(skin->getIcon(EGDI_CURSOR_DOWN)),
414                                         current_icon_color);
415                 }
416                 down_button->setRelativePosition(
417                                 core::rect<s32>(0, RelativeRect.getHeight() - w, w,
418                                                 RelativeRect.getHeight()));
419                 down_button->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT,
420                                 EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT);
421         }
422         bool visible = (border_size != 0);
423         up_button->setVisible(visible);
424         down_button->setVisible(visible);
425 }