forked from funkey/gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSliderImpl.cpp
151 lines (97 loc) · 3.24 KB
/
SliderImpl.cpp
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
#include <util/Logger.h>
#include "SliderImpl.h"
using namespace logger;
LogChannel sliderlog("sliderlog", "");
namespace gui {
SliderImpl::SliderImpl(double min, double max, double value) :
_value(value),
_painter(new SliderPainter(min, max, min)),
_min(min),
_max(max),
_mouseOver(false),
_dragging(false) {
registerOutput(_value, "value");
registerOutput(_painter, "painter");
_painter.registerForwardCallback(&SliderImpl::onMouseUp, this);
_painter.registerForwardCallback(&SliderImpl::onMouseDown, this);
_painter.registerForwardCallback(&SliderImpl::onMouseMove, this);
_painter->setValue(*_value);
}
void
SliderImpl::onMouseUp(MouseUp& signal) {
if (signal.button == buttons::Left) {
LOG_ALL(sliderlog) << "left button up" << std::endl;
// get size and location of grasp
const util::rect<double>& graspSize = _painter->getGraspSize();
// the mouse position
util::point<double> pos = signal.position;
if (!graspSize.contains(pos)) {
_painter->setHighlight(false);
setDirty(_painter);
}
_dragging = false;
}
}
void
SliderImpl::onMouseDown(MouseDown& signal) {
// left mouse button
if (signal.button == buttons::Left) {
// get size and location of grasp
const util::rect<double>& graspSize = _painter->getGraspSize();
// the mouse position
util::point<double> pos = signal.position;
LOG_ALL(sliderlog) << "left button down at " << pos << std::endl;
// within slider
if (graspSize.contains(pos)) {
LOG_ALL(sliderlog) << "starting dragging mode" << std::endl;
// the x offset to the center of the grasp
_draggingOffset = pos.x - (graspSize.minX + graspSize.width()/2);
_dragging = true;
// let the sender know that we took care of this input event
signal.processed = true;
}
}
}
void
SliderImpl::onMouseMove(MouseMove& signal) {
const util::rect<double>& size = _painter->getSize();
const util::rect<double>& graspSize = _painter->getGraspSize();
util::point<double> pos = signal.position;
LOG_ALL(sliderlog) << "mouse moved at " << pos << ", my grasp is at " << graspSize << std::endl;
if (graspSize.contains(pos)) {
LOG_ALL(sliderlog) << "hihi" << std::endl;
if (_mouseOver == false) {
_mouseOver = true;
_painter->setHighlight(true);
setDirty(_painter);
}
} else {
if (_mouseOver == true) {
_mouseOver = false;
if (!_dragging) {
_painter->setHighlight(false);
setDirty(_painter);
}
}
}
if (_dragging) {
// mouse is dragged
if (signal.modifiers & buttons::LeftDown) {
LOG_ALL(sliderlog) << "button pressed at " << signal.position.x << std::endl;
double pos = signal.position.x - _draggingOffset;
double value = _min + ((pos - graspSize.width()/2)/(size.width() - graspSize.width()))*(_max - _min);
LOG_ALL(sliderlog) << "requested value is " << value << std::endl;
value = std::min(_max, std::max(_min, value));
LOG_ALL(sliderlog) << "new value will be " << value << std::endl;
LOG_ALL(sliderlog) << "value was " << *_value << std::endl;
*_value = value;
LOG_ALL(sliderlog) << "value is " << *_value << std::endl;
_painter->setValue(*_value);
setDirty(_value);
setDirty(_painter);
// let the sender know that we took care of this input event
signal.processed = true;
}
}
}
} // namespace gui