1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "CGUIScrollBar.h"
\r
6 #ifdef _IRR_COMPILE_WITH_GUI_
\r
8 #include "IGUISkin.h"
\r
9 #include "IGUIEnvironment.h"
\r
10 #include "IVideoDriver.h"
\r
11 #include "CGUIButton.h"
\r
12 #include "IGUIFont.h"
\r
13 #include "IGUIFontBitmap.h"
\r
23 CGUIScrollBar::CGUIScrollBar(bool horizontal, IGUIEnvironment* environment,
\r
24 IGUIElement* parent, s32 id,
\r
25 core::rect<s32> rectangle, bool noclip)
\r
26 : IGUIScrollBar(environment, parent, id, rectangle), UpButton(0),
\r
27 DownButton(0), Dragging(false), Horizontal(horizontal),
\r
28 DraggedBySlider(false), TrayClick(false), Pos(0), DrawPos(0),
\r
29 DrawHeight(0), Min(0), Max(100), SmallStep(10), LargeStep(50), DesiredPos(0),
\r
33 setDebugName("CGUIScrollBar");
\r
38 setNotClipped(noclip);
\r
40 // this element can be tabbed to
\r
49 CGUIScrollBar::~CGUIScrollBar()
\r
59 //! called if an event happened.
\r
60 bool CGUIScrollBar::OnEvent(const SEvent& event)
\r
65 switch(event.EventType)
\r
67 case EET_KEY_INPUT_EVENT:
\r
68 if (event.KeyInput.PressedDown)
\r
70 const s32 oldPos = Pos;
\r
72 switch (event.KeyInput.Key)
\r
76 setPos(Pos-SmallStep);
\r
80 setPos(Pos+SmallStep);
\r
86 setPos(Pos-LargeStep);
\r
92 setPos(Pos+LargeStep);
\r
101 newEvent.EventType = EET_GUI_EVENT;
\r
102 newEvent.GUIEvent.Caller = this;
\r
103 newEvent.GUIEvent.Element = 0;
\r
104 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
105 Parent->OnEvent(newEvent);
\r
111 case EET_GUI_EVENT:
\r
112 if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
\r
114 if (event.GUIEvent.Caller == UpButton)
\r
115 setPos(Pos-SmallStep);
\r
117 if (event.GUIEvent.Caller == DownButton)
\r
118 setPos(Pos+SmallStep);
\r
121 newEvent.EventType = EET_GUI_EVENT;
\r
122 newEvent.GUIEvent.Caller = this;
\r
123 newEvent.GUIEvent.Element = 0;
\r
124 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
125 Parent->OnEvent(newEvent);
\r
130 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
\r
132 if (event.GUIEvent.Caller == this)
\r
136 case EET_MOUSE_INPUT_EVENT:
\r
138 const core::position2di p(event.MouseInput.X, event.MouseInput.Y);
\r
139 bool isInside = isPointInside ( p );
\r
140 switch(event.MouseInput.Event)
\r
142 case EMIE_MOUSE_WHEEL:
\r
143 if (Environment->hasFocus(this))
\r
145 // thanks to a bug report by REAPER
\r
146 // thanks to tommi by tommi for another bugfix
\r
147 // everybody needs a little thanking. hallo niko!;-)
\r
149 ( (event.MouseInput.Wheel < 0 ? -1 : 1) * SmallStep * (Horizontal ? 1 : -1 ) )
\r
153 newEvent.EventType = EET_GUI_EVENT;
\r
154 newEvent.GUIEvent.Caller = this;
\r
155 newEvent.GUIEvent.Element = 0;
\r
156 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
157 Parent->OnEvent(newEvent);
\r
161 case EMIE_LMOUSE_PRESSED_DOWN:
\r
166 DraggedBySlider = SliderRect.isPointInside(p);
\r
167 TrayClick = !DraggedBySlider;
\r
168 DesiredPos = getPosFromMousePos(p);
\r
173 case EMIE_LMOUSE_LEFT_UP:
\r
174 case EMIE_MOUSE_MOVED:
\r
176 if ( !event.MouseInput.isLeftPressed () )
\r
181 if ( event.MouseInput.Event == EMIE_MOUSE_MOVED )
\r
186 if ( event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP )
\r
189 const s32 newPos = getPosFromMousePos(p);
\r
190 const s32 oldPos = Pos;
\r
192 if (!DraggedBySlider)
\r
196 DraggedBySlider = SliderRect.isPointInside(p);
\r
197 TrayClick = !DraggedBySlider;
\r
200 if (DraggedBySlider)
\r
207 if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
\r
212 if (DraggedBySlider)
\r
218 DesiredPos = newPos;
\r
221 if (Pos != oldPos && Parent)
\r
224 newEvent.EventType = EET_GUI_EVENT;
\r
225 newEvent.GUIEvent.Caller = this;
\r
226 newEvent.GUIEvent.Element = 0;
\r
227 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
228 Parent->OnEvent(newEvent);
\r
242 return IGUIElement::OnEvent(event);
\r
245 void CGUIScrollBar::OnPostRender(u32 timeMs)
\r
247 if (Dragging && !DraggedBySlider && TrayClick && timeMs > LastChange + 200)
\r
249 LastChange = timeMs;
\r
251 const s32 oldPos = Pos;
\r
253 if (DesiredPos >= Pos + LargeStep)
\r
254 setPos(Pos + LargeStep);
\r
256 if (DesiredPos <= Pos - LargeStep)
\r
257 setPos(Pos - LargeStep);
\r
259 if (DesiredPos >= Pos - LargeStep && DesiredPos <= Pos + LargeStep)
\r
260 setPos(DesiredPos);
\r
262 if (Pos != oldPos && Parent)
\r
265 newEvent.EventType = EET_GUI_EVENT;
\r
266 newEvent.GUIEvent.Caller = this;
\r
267 newEvent.GUIEvent.Element = 0;
\r
268 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
269 Parent->OnEvent(newEvent);
\r
275 //! draws the element and its children
\r
276 void CGUIScrollBar::draw()
\r
281 IGUISkin* skin = Environment->getSkin();
\r
286 video::SColor iconColor = skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL);
\r
287 if ( iconColor != CurrentIconColor )
\r
293 SliderRect = AbsoluteRect;
\r
295 // draws the background
\r
296 skin->draw2DRectangle(this, skin->getColor(EGDC_SCROLLBAR), SliderRect, &AbsoluteClippingRect);
\r
298 if ( core::isnotzero ( range() ) )
\r
300 // recalculate slider rectangle
\r
303 SliderRect.UpperLeftCorner.X = AbsoluteRect.UpperLeftCorner.X + DrawPos + RelativeRect.getHeight() - DrawHeight/2;
\r
304 SliderRect.LowerRightCorner.X = SliderRect.UpperLeftCorner.X + DrawHeight;
\r
308 SliderRect.UpperLeftCorner.Y = AbsoluteRect.UpperLeftCorner.Y + DrawPos + RelativeRect.getWidth() - DrawHeight/2;
\r
309 SliderRect.LowerRightCorner.Y = SliderRect.UpperLeftCorner.Y + DrawHeight;
\r
312 skin->draw3DButtonPaneStandard(this, SliderRect, &AbsoluteClippingRect);
\r
316 IGUIElement::draw();
\r
320 void CGUIScrollBar::updateAbsolutePosition()
\r
322 IGUIElement::updateAbsolutePosition();
\r
323 // todo: properly resize
\r
329 s32 CGUIScrollBar::getPosFromMousePos(const core::position2di &pos) const
\r
334 w = RelativeRect.getWidth() - f32(RelativeRect.getHeight())*3.0f;
\r
335 p = pos.X - AbsoluteRect.UpperLeftCorner.X - RelativeRect.getHeight()*1.5f;
\r
339 w = RelativeRect.getHeight() - f32(RelativeRect.getWidth())*3.0f;
\r
340 p = pos.Y - AbsoluteRect.UpperLeftCorner.Y - RelativeRect.getWidth()*1.5f;
\r
342 return (s32) ( p/w * range() ) + Min;
\r
346 //! sets the position of the scrollbar
\r
347 void CGUIScrollBar::setPos(s32 pos)
\r
349 Pos = core::s32_clamp ( pos, Min, Max );
\r
351 if ( core::isnotzero ( range() ) )
\r
355 f32 f = (RelativeRect.getWidth() - ((f32)RelativeRect.getHeight()*3.0f)) / range();
\r
356 DrawPos = (s32)( ( ( Pos - Min ) * f) + ((f32)RelativeRect.getHeight() * 0.5f));
\r
357 DrawHeight = RelativeRect.getHeight();
\r
361 f32 f = (RelativeRect.getHeight() - ((f32)RelativeRect.getWidth()*3.0f)) / range();
\r
363 DrawPos = (s32)( ( ( Pos - Min ) * f) + ((f32)RelativeRect.getWidth() * 0.5f));
\r
364 DrawHeight = RelativeRect.getWidth();
\r
370 //! gets the small step value
\r
371 s32 CGUIScrollBar::getSmallStep() const
\r
377 //! sets the small step value
\r
378 void CGUIScrollBar::setSmallStep(s32 step)
\r
387 //! gets the small step value
\r
388 s32 CGUIScrollBar::getLargeStep() const
\r
394 //! sets the small step value
\r
395 void CGUIScrollBar::setLargeStep(s32 step)
\r
404 //! gets the maximum value of the scrollbar.
\r
405 s32 CGUIScrollBar::getMax() const
\r
411 //! sets the maximum value of the scrollbar.
\r
412 void CGUIScrollBar::setMax(s32 max)
\r
418 bool enable = core::isnotzero ( range() );
\r
419 UpButton->setEnabled(enable);
\r
420 DownButton->setEnabled(enable);
\r
424 //! gets the minimum value of the scrollbar.
\r
425 s32 CGUIScrollBar::getMin() const
\r
431 //! sets the minimum value of the scrollbar.
\r
432 void CGUIScrollBar::setMin(s32 min)
\r
439 bool enable = core::isnotzero ( range() );
\r
440 UpButton->setEnabled(enable);
\r
441 DownButton->setEnabled(enable);
\r
446 //! gets the current position of the scrollbar
\r
447 s32 CGUIScrollBar::getPos() const
\r
453 //! refreshes the position and text on child buttons
\r
454 void CGUIScrollBar::refreshControls()
\r
456 CurrentIconColor = video::SColor(255,255,255,255);
\r
458 IGUISkin* skin = Environment->getSkin();
\r
459 IGUISpriteBank* sprites = 0;
\r
463 sprites = skin->getSpriteBank();
\r
464 CurrentIconColor = skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL);
\r
469 const s32 h = RelativeRect.getHeight();
\r
470 const s32 w = (h < RelativeRect.getWidth() / 2) ? h : RelativeRect.getWidth() / 2;
\r
473 UpButton = new CGUIButton(Environment, this, -1, core::rect<s32>(0,0, w, h), NoClip);
\r
474 UpButton->setSubElement(true);
\r
475 UpButton->setTabStop(false);
\r
479 UpButton->setSpriteBank(sprites);
\r
480 UpButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_LEFT), CurrentIconColor);
\r
481 UpButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_LEFT), CurrentIconColor);
\r
483 UpButton->setRelativePosition(core::rect<s32>(0,0, w, h));
\r
484 UpButton->setAlignment(EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
\r
487 DownButton = new CGUIButton(Environment, this, -1, core::rect<s32>(RelativeRect.getWidth()-w, 0, RelativeRect.getWidth(), h), NoClip);
\r
488 DownButton->setSubElement(true);
\r
489 DownButton->setTabStop(false);
\r
493 DownButton->setSpriteBank(sprites);
\r
494 DownButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_RIGHT), CurrentIconColor);
\r
495 DownButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_RIGHT), CurrentIconColor);
\r
497 DownButton->setRelativePosition(core::rect<s32>(RelativeRect.getWidth()-w, 0, RelativeRect.getWidth(), h));
\r
498 DownButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
\r
502 const s32 w = RelativeRect.getWidth();
\r
503 const s32 h = (w < RelativeRect.getHeight() / 2) ? w : RelativeRect.getHeight() / 2;
\r
506 UpButton = new CGUIButton(Environment, this, -1, core::rect<s32>(0,0, w, h), NoClip);
\r
507 UpButton->setSubElement(true);
\r
508 UpButton->setTabStop(false);
\r
512 UpButton->setSpriteBank(sprites);
\r
513 UpButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_UP), CurrentIconColor);
\r
514 UpButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_UP), CurrentIconColor);
\r
516 UpButton->setRelativePosition(core::rect<s32>(0,0, w, h));
\r
517 UpButton->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
\r
520 DownButton = new CGUIButton(Environment, this, -1, core::rect<s32>(0,RelativeRect.getHeight()-h, w, RelativeRect.getHeight()), NoClip);
\r
521 DownButton->setSubElement(true);
\r
522 DownButton->setTabStop(false);
\r
526 DownButton->setSpriteBank(sprites);
\r
527 DownButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_DOWN), CurrentIconColor);
\r
528 DownButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_DOWN), CurrentIconColor);
\r
530 DownButton->setRelativePosition(core::rect<s32>(0,RelativeRect.getHeight()-h, w, RelativeRect.getHeight()));
\r
531 DownButton->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT);
\r
535 } // end namespace gui
\r
536 } // end namespace irr
\r
538 #endif // _IRR_COMPILE_WITH_GUI_
\r