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
7 #include "IGUISkin.h"
\r
8 #include "IGUIEnvironment.h"
\r
9 #include "IVideoDriver.h"
\r
10 #include "CGUIButton.h"
\r
11 #include "IGUIFont.h"
\r
12 #include "IGUIFontBitmap.h"
\r
22 CGUIScrollBar::CGUIScrollBar(bool horizontal, IGUIEnvironment* environment,
\r
23 IGUIElement* parent, s32 id,
\r
24 core::rect<s32> rectangle, bool noclip)
\r
25 : IGUIScrollBar(environment, parent, id, rectangle), UpButton(0),
\r
26 DownButton(0), Dragging(false), Horizontal(horizontal),
\r
27 DraggedBySlider(false), TrayClick(false), Pos(0), DrawPos(0),
\r
28 DrawHeight(0), Min(0), Max(100), SmallStep(10), LargeStep(50), DesiredPos(0),
\r
32 setDebugName("CGUIScrollBar");
\r
37 setNotClipped(noclip);
\r
39 // this element can be tabbed to
\r
48 CGUIScrollBar::~CGUIScrollBar()
\r
58 //! called if an event happened.
\r
59 bool CGUIScrollBar::OnEvent(const SEvent& event)
\r
64 switch(event.EventType)
\r
66 case EET_KEY_INPUT_EVENT:
\r
67 if (event.KeyInput.PressedDown)
\r
69 const s32 oldPos = Pos;
\r
71 switch (event.KeyInput.Key)
\r
75 setPos(Pos-SmallStep);
\r
79 setPos(Pos+SmallStep);
\r
85 setPos(Pos-LargeStep);
\r
91 setPos(Pos+LargeStep);
\r
100 newEvent.EventType = EET_GUI_EVENT;
\r
101 newEvent.GUIEvent.Caller = this;
\r
102 newEvent.GUIEvent.Element = 0;
\r
103 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
104 Parent->OnEvent(newEvent);
\r
110 case EET_GUI_EVENT:
\r
111 if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
\r
113 if (event.GUIEvent.Caller == UpButton)
\r
114 setPos(Pos-SmallStep);
\r
116 if (event.GUIEvent.Caller == DownButton)
\r
117 setPos(Pos+SmallStep);
\r
120 newEvent.EventType = EET_GUI_EVENT;
\r
121 newEvent.GUIEvent.Caller = this;
\r
122 newEvent.GUIEvent.Element = 0;
\r
123 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
124 Parent->OnEvent(newEvent);
\r
129 if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
\r
131 if (event.GUIEvent.Caller == this)
\r
135 case EET_MOUSE_INPUT_EVENT:
\r
137 const core::position2di p(event.MouseInput.X, event.MouseInput.Y);
\r
138 bool isInside = isPointInside ( p );
\r
139 switch(event.MouseInput.Event)
\r
141 case EMIE_MOUSE_WHEEL:
\r
142 if (Environment->hasFocus(this))
\r
144 // thanks to a bug report by REAPER
\r
145 // thanks to tommi by tommi for another bugfix
\r
146 // everybody needs a little thanking. hallo niko!;-)
\r
148 ( (event.MouseInput.Wheel < 0 ? -1 : 1) * SmallStep * (Horizontal ? 1 : -1 ) )
\r
152 newEvent.EventType = EET_GUI_EVENT;
\r
153 newEvent.GUIEvent.Caller = this;
\r
154 newEvent.GUIEvent.Element = 0;
\r
155 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
156 Parent->OnEvent(newEvent);
\r
160 case EMIE_LMOUSE_PRESSED_DOWN:
\r
165 DraggedBySlider = SliderRect.isPointInside(p);
\r
166 TrayClick = !DraggedBySlider;
\r
167 DesiredPos = getPosFromMousePos(p);
\r
172 case EMIE_LMOUSE_LEFT_UP:
\r
173 case EMIE_MOUSE_MOVED:
\r
175 if ( !event.MouseInput.isLeftPressed () )
\r
180 if ( event.MouseInput.Event == EMIE_MOUSE_MOVED )
\r
185 if ( event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP )
\r
188 const s32 newPos = getPosFromMousePos(p);
\r
189 const s32 oldPos = Pos;
\r
191 if (!DraggedBySlider)
\r
195 DraggedBySlider = SliderRect.isPointInside(p);
\r
196 TrayClick = !DraggedBySlider;
\r
199 if (DraggedBySlider)
\r
206 if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
\r
211 if (DraggedBySlider)
\r
217 DesiredPos = newPos;
\r
220 if (Pos != oldPos && Parent)
\r
223 newEvent.EventType = EET_GUI_EVENT;
\r
224 newEvent.GUIEvent.Caller = this;
\r
225 newEvent.GUIEvent.Element = 0;
\r
226 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
227 Parent->OnEvent(newEvent);
\r
241 return IGUIElement::OnEvent(event);
\r
244 void CGUIScrollBar::OnPostRender(u32 timeMs)
\r
246 if (Dragging && !DraggedBySlider && TrayClick && timeMs > LastChange + 200)
\r
248 LastChange = timeMs;
\r
250 const s32 oldPos = Pos;
\r
252 if (DesiredPos >= Pos + LargeStep)
\r
253 setPos(Pos + LargeStep);
\r
255 if (DesiredPos <= Pos - LargeStep)
\r
256 setPos(Pos - LargeStep);
\r
258 if (DesiredPos >= Pos - LargeStep && DesiredPos <= Pos + LargeStep)
\r
259 setPos(DesiredPos);
\r
261 if (Pos != oldPos && Parent)
\r
264 newEvent.EventType = EET_GUI_EVENT;
\r
265 newEvent.GUIEvent.Caller = this;
\r
266 newEvent.GUIEvent.Element = 0;
\r
267 newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
\r
268 Parent->OnEvent(newEvent);
\r
274 //! draws the element and its children
\r
275 void CGUIScrollBar::draw()
\r
280 IGUISkin* skin = Environment->getSkin();
\r
285 video::SColor iconColor = skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL);
\r
286 if ( iconColor != CurrentIconColor )
\r
292 SliderRect = AbsoluteRect;
\r
294 // draws the background
\r
295 skin->draw2DRectangle(this, skin->getColor(EGDC_SCROLLBAR), SliderRect, &AbsoluteClippingRect);
\r
297 if ( core::isnotzero ( range() ) )
\r
299 // recalculate slider rectangle
\r
302 SliderRect.UpperLeftCorner.X = AbsoluteRect.UpperLeftCorner.X + DrawPos + RelativeRect.getHeight() - DrawHeight/2;
\r
303 SliderRect.LowerRightCorner.X = SliderRect.UpperLeftCorner.X + DrawHeight;
\r
307 SliderRect.UpperLeftCorner.Y = AbsoluteRect.UpperLeftCorner.Y + DrawPos + RelativeRect.getWidth() - DrawHeight/2;
\r
308 SliderRect.LowerRightCorner.Y = SliderRect.UpperLeftCorner.Y + DrawHeight;
\r
311 skin->draw3DButtonPaneStandard(this, SliderRect, &AbsoluteClippingRect);
\r
315 IGUIElement::draw();
\r
319 void CGUIScrollBar::updateAbsolutePosition()
\r
321 IGUIElement::updateAbsolutePosition();
\r
322 // todo: properly resize
\r
328 s32 CGUIScrollBar::getPosFromMousePos(const core::position2di &pos) const
\r
333 w = RelativeRect.getWidth() - f32(RelativeRect.getHeight())*3.0f;
\r
334 p = pos.X - AbsoluteRect.UpperLeftCorner.X - RelativeRect.getHeight()*1.5f;
\r
338 w = RelativeRect.getHeight() - f32(RelativeRect.getWidth())*3.0f;
\r
339 p = pos.Y - AbsoluteRect.UpperLeftCorner.Y - RelativeRect.getWidth()*1.5f;
\r
341 return (s32) ( p/w * range() ) + Min;
\r
345 //! sets the position of the scrollbar
\r
346 void CGUIScrollBar::setPos(s32 pos)
\r
348 Pos = core::s32_clamp ( pos, Min, Max );
\r
350 if ( core::isnotzero ( range() ) )
\r
354 f32 f = (RelativeRect.getWidth() - ((f32)RelativeRect.getHeight()*3.0f)) / range();
\r
355 DrawPos = (s32)( ( ( Pos - Min ) * f) + ((f32)RelativeRect.getHeight() * 0.5f));
\r
356 DrawHeight = RelativeRect.getHeight();
\r
360 f32 f = (RelativeRect.getHeight() - ((f32)RelativeRect.getWidth()*3.0f)) / range();
\r
362 DrawPos = (s32)( ( ( Pos - Min ) * f) + ((f32)RelativeRect.getWidth() * 0.5f));
\r
363 DrawHeight = RelativeRect.getWidth();
\r
369 //! gets the small step value
\r
370 s32 CGUIScrollBar::getSmallStep() const
\r
376 //! sets the small step value
\r
377 void CGUIScrollBar::setSmallStep(s32 step)
\r
386 //! gets the small step value
\r
387 s32 CGUIScrollBar::getLargeStep() const
\r
393 //! sets the small step value
\r
394 void CGUIScrollBar::setLargeStep(s32 step)
\r
403 //! gets the maximum value of the scrollbar.
\r
404 s32 CGUIScrollBar::getMax() const
\r
410 //! sets the maximum value of the scrollbar.
\r
411 void CGUIScrollBar::setMax(s32 max)
\r
417 bool enable = core::isnotzero ( range() );
\r
418 UpButton->setEnabled(enable);
\r
419 DownButton->setEnabled(enable);
\r
423 //! gets the minimum value of the scrollbar.
\r
424 s32 CGUIScrollBar::getMin() const
\r
430 //! sets the minimum value of the scrollbar.
\r
431 void CGUIScrollBar::setMin(s32 min)
\r
438 bool enable = core::isnotzero ( range() );
\r
439 UpButton->setEnabled(enable);
\r
440 DownButton->setEnabled(enable);
\r
445 //! gets the current position of the scrollbar
\r
446 s32 CGUIScrollBar::getPos() const
\r
452 //! refreshes the position and text on child buttons
\r
453 void CGUIScrollBar::refreshControls()
\r
455 CurrentIconColor = video::SColor(255,255,255,255);
\r
457 IGUISkin* skin = Environment->getSkin();
\r
458 IGUISpriteBank* sprites = 0;
\r
462 sprites = skin->getSpriteBank();
\r
463 CurrentIconColor = skin->getColor(isEnabled() ? EGDC_WINDOW_SYMBOL : EGDC_GRAY_WINDOW_SYMBOL);
\r
468 const s32 h = RelativeRect.getHeight();
\r
469 const s32 w = (h < RelativeRect.getWidth() / 2) ? h : RelativeRect.getWidth() / 2;
\r
472 UpButton = new CGUIButton(Environment, this, -1, core::rect<s32>(0,0, w, h), NoClip);
\r
473 UpButton->setSubElement(true);
\r
474 UpButton->setTabStop(false);
\r
478 UpButton->setSpriteBank(sprites);
\r
479 UpButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_LEFT), CurrentIconColor);
\r
480 UpButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_LEFT), CurrentIconColor);
\r
482 UpButton->setRelativePosition(core::rect<s32>(0,0, w, h));
\r
483 UpButton->setAlignment(EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
\r
486 DownButton = new CGUIButton(Environment, this, -1, core::rect<s32>(RelativeRect.getWidth()-w, 0, RelativeRect.getWidth(), h), NoClip);
\r
487 DownButton->setSubElement(true);
\r
488 DownButton->setTabStop(false);
\r
492 DownButton->setSpriteBank(sprites);
\r
493 DownButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_RIGHT), CurrentIconColor);
\r
494 DownButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_RIGHT), CurrentIconColor);
\r
496 DownButton->setRelativePosition(core::rect<s32>(RelativeRect.getWidth()-w, 0, RelativeRect.getWidth(), h));
\r
497 DownButton->setAlignment(EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
\r
501 const s32 w = RelativeRect.getWidth();
\r
502 const s32 h = (w < RelativeRect.getHeight() / 2) ? w : RelativeRect.getHeight() / 2;
\r
505 UpButton = new CGUIButton(Environment, this, -1, core::rect<s32>(0,0, w, h), NoClip);
\r
506 UpButton->setSubElement(true);
\r
507 UpButton->setTabStop(false);
\r
511 UpButton->setSpriteBank(sprites);
\r
512 UpButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_UP), CurrentIconColor);
\r
513 UpButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_UP), CurrentIconColor);
\r
515 UpButton->setRelativePosition(core::rect<s32>(0,0, w, h));
\r
516 UpButton->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_UPPERLEFT, EGUIA_UPPERLEFT);
\r
519 DownButton = new CGUIButton(Environment, this, -1, core::rect<s32>(0,RelativeRect.getHeight()-h, w, RelativeRect.getHeight()), NoClip);
\r
520 DownButton->setSubElement(true);
\r
521 DownButton->setTabStop(false);
\r
525 DownButton->setSpriteBank(sprites);
\r
526 DownButton->setSprite(EGBS_BUTTON_UP, skin->getIcon(EGDI_CURSOR_DOWN), CurrentIconColor);
\r
527 DownButton->setSprite(EGBS_BUTTON_DOWN, skin->getIcon(EGDI_CURSOR_DOWN), CurrentIconColor);
\r
529 DownButton->setRelativePosition(core::rect<s32>(0,RelativeRect.getHeight()-h, w, RelativeRect.getHeight()));
\r
530 DownButton->setAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT, EGUIA_LOWERRIGHT);
\r
534 } // end namespace gui
\r
535 } // end namespace irr
\r