-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGScrollBar.cpp
179 lines (150 loc) · 4.68 KB
/
GScrollBar.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "GScrollBar.h"
#include "PackageItem.h"
#include "ScrollPane.h"
#include "utils/ByteBuffer.h"
NS_FGUI_BEGIN
USING_NS_CC;
GScrollBar::GScrollBar()
: _grip(nullptr),
_arrowButton1(nullptr),
_arrowButton2(nullptr),
_bar(nullptr),
_target(nullptr),
_vertical(false),
_scrollPerc(0),
_fixedGripSize(false),
_gripDragging(false)
{
}
GScrollBar::~GScrollBar()
{
}
void GScrollBar::setScrollPane(ScrollPane* target, bool vertical)
{
_target = target;
_vertical = vertical;
}
void GScrollBar::setDisplayPerc(float value)
{
if (_vertical)
{
if (!_fixedGripSize)
_grip->setHeight(floor(value * _bar->getHeight()));
_grip->setY(round(_bar->getY() + (_bar->getHeight() - _grip->getHeight()) * _scrollPerc));
}
else
{
if (!_fixedGripSize)
_grip->setWidth(floor(value * _bar->getWidth()));
_grip->setX(round(_bar->getX() + (_bar->getWidth() - _grip->getWidth()) * _scrollPerc));
}
_grip->setVisible(value != 0 && value != 1);
}
void GScrollBar::setScrollPerc(float value)
{
_scrollPerc = value;
if (_vertical)
_grip->setY(round(_bar->getY() + (_bar->getHeight() - _grip->getHeight()) * _scrollPerc));
else
_grip->setX(round(_bar->getX() + (_bar->getWidth() - _grip->getWidth()) * _scrollPerc));
}
float GScrollBar::getMinSize()
{
if (_vertical)
return (_arrowButton1 != nullptr ? _arrowButton1->getHeight() : 0) + (_arrowButton2 != nullptr ? _arrowButton2->getHeight() : 0);
else
return (_arrowButton1 != nullptr ? _arrowButton1->getWidth() : 0) + (_arrowButton2 != nullptr ? _arrowButton2->getWidth() : 0);
}
void GScrollBar::constructExtension(ByteBuffer* buffer)
{
buffer->seek(0, 6);
_fixedGripSize = buffer->readBool();
_grip = getChild("grip");
CCASSERT(_grip != nullptr, "FairyGUI: should define grip");
_bar = getChild("bar");
CCASSERT(_bar != nullptr, "FairyGUI: should define bar");
_arrowButton1 = getChild("arrow1");
_arrowButton2 = getChild("arrow2");
_grip->addEventListener(UIEventType::TouchBegin, CC_CALLBACK_1(GScrollBar::onGripTouchBegin, this));
_grip->addEventListener(UIEventType::TouchMove, CC_CALLBACK_1(GScrollBar::onGripTouchMove, this));
_grip->addEventListener(UIEventType::TouchEnd, CC_CALLBACK_1(GScrollBar::onGripTouchEnd, this));
this->addEventListener(UIEventType::TouchBegin, CC_CALLBACK_1(GScrollBar::onTouchBegin, this));
if (_arrowButton1 != nullptr)
_arrowButton1->addEventListener(UIEventType::TouchBegin, CC_CALLBACK_1(GScrollBar::onArrowButton1Click, this));
if (_arrowButton2 != nullptr)
_arrowButton2->addEventListener(UIEventType::TouchBegin, CC_CALLBACK_1(GScrollBar::onArrowButton2Click, this));
}
void GScrollBar::onTouchBegin(EventContext* context)
{
context->stopPropagation();
InputEvent* evt = context->getInput();
Vec2 pt = _grip->globalToLocal(evt->getPosition());
if (_vertical)
{
if (pt.y < 0)
_target->scrollUp(4, false);
else
_target->scrollDown(4, false);
}
else
{
if (pt.x < 0)
_target->scrollLeft(4, false);
else
_target->scrollRight(4, false);
}
}
void GScrollBar::onGripTouchBegin(EventContext* context)
{
if (_bar == nullptr)
return;
context->stopPropagation();
context->captureTouch();
_gripDragging = true;
_target->updateScrollBarVisible();
_dragOffset = globalToLocal(context->getInput()->getPosition()) - _grip->getPosition();
}
void GScrollBar::onGripTouchMove(EventContext* context)
{
Vec2 pt = globalToLocal(context->getInput()->getPosition());
if (_vertical)
{
float curY = pt.y - _dragOffset.y;
float diff = _bar->getHeight() - _grip->getHeight();
if (diff == 0)
_target->setPercY(0);
else
_target->setPercY((curY - _bar->getY()) / diff);
}
else
{
float curX = pt.x - _dragOffset.x;
float diff = _bar->getWidth() - _grip->getWidth();
if (diff == 0)
_target->setPercX(0);
else
_target->setPercX((curX - _bar->getX()) / diff);
}
}
void GScrollBar::onGripTouchEnd(EventContext* context)
{
_gripDragging = false;
_target->updateScrollBarVisible();
}
void GScrollBar::onArrowButton1Click(EventContext* context)
{
context->stopPropagation();
if (_vertical)
_target->scrollUp();
else
_target->scrollLeft();
}
void GScrollBar::onArrowButton2Click(EventContext* context)
{
context->stopPropagation();
if (_vertical)
_target->scrollDown();
else
_target->scrollRight();
}
NS_FGUI_END