GUI
Scrollbar.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 by Maciej Wiecierzewski
3  */
4 
5 #include "Scrollbar.h"
6 
7 #include <iostream>
8 
9 #include "GUISystem.h"
10 
11 using namespace gui;
12 
13 Scrollbar::Scrollbar(GUISystem *guiSystem, int x, int y, int w, int h) :
14  Widget(x, y, w, h),
15  upButton(guiSystem->addButton(0, h-15, w, 15, "")),
16  downButton(guiSystem->addButton(0, 0, w, 15, "")),
17  slideButton(guiSystem->addButton(0, 20, w, 20, "")),
18  mouseMoveListenerId(0),
19  sliderPosition(0),
20  sliderLength(0.4)
21 {
22  downButton->setParent(this);
23  auto downClickEventListener = std::bind(&Scrollbar::downButtonClick, this, std::placeholders::_1);
24  downButton->addEventListener(EventType::MOUSE_BUTTON_UP, static_cast<EventListener::Function>(downClickEventListener));
25 
26  upButton->setParent(this);
27  auto upClickEventListener = std::bind(&Scrollbar::upButtonClick, this, std::placeholders::_1);
28  upButton->addEventListener(EventType::MOUSE_BUTTON_UP, static_cast<EventListener::Function>(upClickEventListener));
29 
30  slideButton->setParent(this);
31 
32  auto sliderDownEventListener = std::bind(&Scrollbar::sliderMouseDown, this, std::placeholders::_1);
33  slideButton->addEventListener(EventType::MOUSE_BUTTON_DOWN, static_cast<EventListener::Function>(sliderDownEventListener));
34 
35  auto sliderUpEventListener = std::bind(&Scrollbar::sliderMouseUp, this, std::placeholders::_1);
36  slideButton->addEventListener(EventType::MOUSE_BUTTON_UP, static_cast<EventListener::Function>(sliderUpEventListener));
37 
39 }
40 
41 void Scrollbar::render(Renderer *renderer)
42 {
43  int x = xAbs, y = yAbs;
44  renderer->setTarget(x, y, w, h);
45 
46  renderer->drawFilledRect(0, 0, w, h, backgroundColor);
47  renderer->drawRect(1, 1, w-1, h-1, frameColor, 2);
48 }
49 
51 {
53 }
54 
56 {
58 }
59 
61 {
63  return;
64 
65  MouseEvent *event = _event.getEventObject<MouseEvent>();
66  holdPointX = event->mouseX;
67  holdPointY = event->mouseY;
69 
70  if(mouseMoveListenerId != 0)
71  return;
72 
73  auto listenerFunction = std::bind(&Scrollbar::sliderMouseMove, this, std::placeholders::_1);
75  static_cast<EventListener::Function>(listenerFunction));
76 }
77 
79 {
80  if(_event.getType() != EventType::MOUSE_BUTTON_UP)
81  return;
82 
83  if(mouseMoveListenerId == 0)
84  return;
85 
88 }
89 
91 {
92  if(_event.getType() != EventType::MOUSE_MOVE)
93  return;
94 
95  MouseEvent *event = _event.getEventObject<MouseEvent>();
96 
97  //float x = event->mouseX - holdPointX;
98  float y = event->mouseY - holdPointY;
99  float h = this->h - 30 - sliderLength*(this->h-30);
100  float posChange = y / h;
101 
102  setSlider(sliderPrevPos + posChange);
103 }
104 
105 void Scrollbar::setSlider(float position)
106 {
107  sliderPosition = (position > 1) ? 1 : ((position < 0) ? 0 : position);
108 
109  int h = sliderLength * (this->h - 30);
110  int y = sliderPosition * (this->h - 30 - h) + 15;
111  slideButton->resize(0, y, this->w, h);
112 }
113 
114 void Scrollbar::setSliderLength(float length)
115 {
116  sliderLength = (length > 1) ? 1 : ((length < 0) ? 0 : length);
117 
119  sliderPosition = (1-sliderLength < 0) ? 0 : 1-sliderLength;
120 
122 }