-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectBox.cpp
138 lines (127 loc) · 3.36 KB
/
SelectBox.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
#include "SelectBox.h"
SelectBox::SelectBox(IntRect box, Color color, bool mid)
:vertices{TriangleStrip, 6}
,width(box.width)
,xChange(width / 25)
,aChange(17)
{
if (mid) {
xDest = box.left + width / 2;
}
else {
xDest = box.left + width / 4;
}
vertices[0].position = Vector2f(box.left, box.top);
vertices[1].position = Vector2f(box.left, box.top + box.height);
vertices[2].position = Vector2f(box.left + box.width / 4, box.top);
vertices[3].position = Vector2f(box.left + box.width / 4, box.top + box.height);
vertices[4].position = Vector2f(box.left + box.width, box.top);
vertices[5].position = Vector2f(box.left + box.width, box.top + box.height);
vertices[2].color = vertices[3].color = color;
vertices[0].color = vertices[1].color = vertices[4].color = vertices[5].color = Color(255, 255, 255, 255);
}
SelectBox::SelectBox(Color color)
:vertices{ TriangleStrip, 6 }
, aChange(17)
{
vertices[2].color = vertices[3].color = color;
vertices[0].color = vertices[1].color = vertices[4].color = vertices[5].color = Color::Transparent;
}
void SelectBox::setAlpha(char alpha)
{
vertices[2].color.a = vertices[3].color.a = alpha;
}
void SelectBox::fade(char alpha)
{
if (vertices[2].color.a - alpha < 0) {
vertices[2].color.a = vertices[3].color.a = 0;
}
else {
vertices[2].color.a = vertices[3].color.a -= alpha;
}
//vertices[2].color.a = vertices[3].color.a -= alpha;
}
void SelectBox::select(short int x)
{
if (vertices[2].color.a != 255) {
fade(-aChange);
}
if (vertices[2].position.x - xChange > x) {
moveCenter(-xChange);
}
else if (vertices[2].position.x + xChange < x) {
moveCenter(xChange);
}
else {
setCenter(x);
}
}
void SelectBox::select()
{
if (vertices[2].color.a != 255) {
fade(-aChange);
}
if (vertices[2].position.x - xChange > xDest) {
moveCenter(-xChange);
}
else if (vertices[2].position.x + xChange < xDest) {
moveCenter(xChange);
}
else {
setCenter(xDest);
}
}
void SelectBox::deselect()
{
if (vertices[2].color.a) {
if (width - vertices[2].position.x < width / 5) {
fade(3 *aChange);
}
else {
fade(aChange);
}
}
moveCenter(xChange);
}
void SelectBox::setCenter(short int x)
{
if (x > vertices[5].position.x) {
x = vertices[5].position.x - 1;
}
else if (x < vertices[0].position.x) {
x = vertices[0].position.x + 1;
}
vertices[2].position.x = vertices[3].position.x = x;
}
void SelectBox::moveCenter(short int x)
{
vertices[3].position.x += x;
if (vertices[3].position.x > vertices[5].position.x) {
vertices[3].position.x = vertices[5].position.x - 1;
}
else if (vertices[3].position.x < vertices[0].position.x) {
vertices[3].position.x = vertices[0].position.x + 1;
}
vertices[2].position.x = vertices[3].position.x;
}
void SelectBox::setBox(IntRect box, bool mid)
{
width = box.width;
if (mid) {
xDest = box.left + width / 2;
}
else {
xDest = box.left + width / 4;
}
xChange = width / 25;
vertices[0].position = Vector2f(box.left, box.top);
vertices[1].position = Vector2f(box.left, box.top + box.height);
vertices[2].position = Vector2f(box.left + box.width - 1, box.top);
vertices[3].position = Vector2f(box.left + box.width - 1, box.top + box.height);
vertices[4].position = Vector2f(box.left + box.width, box.top);
vertices[5].position = Vector2f(box.left + box.width, box.top + box.height);
}
void SelectBox::draw(RenderTarget& target, RenderStates states) const
{
target.draw(vertices);
}