-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlider.cpp
180 lines (144 loc) · 6.5 KB
/
Slider.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
180
//Slider.cpp
#include "Slider.h"
#include <iostream>
using namespace sdlUtility;
namespace sdlGUI {
int Slider::GetOrientation() {
return Orientation;
}
float Slider::GetSliderValue() {
return ((1.f-Status)*MinimumValue+Status*MaximumValue);
}
int Slider::GetType() {
return Type;
}
int Slider::OnInput(SDL_Event &Event) {
int Input = 0;
if (IsShown(true) && Type == Sliders::Drag) {
if (Event.type == SDL_MOUSEWHEEL) {
const Uint8 *Keyboard = SDL_GetKeyboardState(NULL);
bool ShiftState = Keyboard[SDL_SCANCODE_LSHIFT]||Keyboard[SDL_SCANCODE_RSHIFT];
if ((ShiftState && Orientation == Orientation::Horizontal) || (!ShiftState && Orientation == Orientation::Vertical)) {
Status -= Event.button.x*0.2f;
Input = 241;
}
if (Status < 0) Status = 0; else if (Status > 1) Status = 1;
}
}
return Input;
}
void Slider::OnProcess() {
if (Cursor) {
if (IsClicked(SDL_BUTTON_LEFT)) {
Scaled = true;
Cursor->EnableScaling(true);
if (Orientation == Orientation::Horizontal) {
//std::cout << Name <<" " << DragReference.X()-Cursor->X(Reference::Parent) << std::endl;
SetStatus((DragReference.X()-Cursor->Width()/2.f)/(Width()-Cursor->Width()));
if (Status < 0) {
Status = 0;
DragReference = Vector2(Cursor->Width()/2.f, DragReference.Y());
} else if (Status > 1) {
Status = 1;
DragReference = Vector2(Width()-Cursor->Width()/2.f, DragReference.Y());
}
Cursor->DropAt((DragReference.X()-Cursor->Width()/2.f)/Scale.X(), 0);
} else if (Orientation == Orientation::Vertical) {
SetStatus((DragReference.Y()-Cursor->Height()/2.f)/(Height()-Cursor->Height()));
if (Status < 0) {
Status = 0;
DragReference = Vector2(DragReference.X(), Cursor->Height()/2.f);
} else if (Status > 1) {
Status = 1;
DragReference = Vector2(DragReference.X(), Height()-Cursor->Height()/2.f);
}
Cursor->DropAt(0, (DragReference.Y()-Cursor->Height()/2.f)/Scale.Y());
}
//std::cout << Status << std::endl;
Cursor->EnableScaling(false);
Scaled = false;
} else {
if (Orientation == Orientation::Horizontal) {
Cursor->DropAt(Status*(Width()-Cursor->Width()), 0.f);
} else if (Orientation == Orientation::Vertical) {
Cursor->DropAt(0.f, Status*(Height()-Cursor->Height()));
}
}
}
if (Type == Sliders::Drag) {
if (Parent) {
if (Orientation == Orientation::Horizontal) {
if (Scale == Vector2(1.f, 1.f)) Parent->EnableScaling(true);
float FrameSize = MaximumValue-Width();
Parent->OffsetChildren(Axis::X, -Status*FrameSize);
//DropAt(Status*FrameSize+Parent->PaddingX(), 0.f);
if (Cursor) {
float ParentWidth = Parent->Width();
Resize(Axis::X, ParentWidth-2.f*Parent->PaddingX());
Cursor->Resize(Axis::X, ParentWidth/MaximumValue*ParentWidth);
if (Cursor->Width() >= ParentWidth-2.f*Parent->PaddingX()) {
Shown = false;
Parent->OffsetChildren(Axis::X, 0);
} else Shown = true;
}
if (Scale == Vector2(1.f, 1.f)) Parent->EnableScaling(false);
} else if (Orientation == Orientation::Vertical) {
if (Scale == Vector2(1.f, 1.f)) Parent->EnableScaling(true);
float FrameSize = MaximumValue-Height();
Parent->OffsetChildren(Axis::Y, -Status*FrameSize);
//DropAt(0.f, Status*FrameSize+Parent->PaddingY());
if (Cursor) {
float ParentHeight = Parent->Height();
Resize(Axis::Y, ParentHeight-2.f*Parent->PaddingY());
Cursor->Resize(Axis::Y, ParentHeight/MaximumValue*ParentHeight);
//if (Name == "DebugDialogSlider2") std::cout << GetID() << " " << Height() << " " << ParentHeight << " " << InheritedHeight() << " " << Parent->ChildrenOffsetY() << " " << Parent->GetName() << std::endl;
if (Cursor->Height() >= ParentHeight-2.f*Parent->PaddingY()) {
Shown = false;
Parent->OffsetChildren(Axis::Y, 0);
} else Shown = true;
}
if (Scale == Vector2(1.f, 1.f)) Parent->EnableScaling(false);
}
}
}
}
void Slider::SetCursor(Frame *Cursor) {
this->Cursor = Cursor;
}
void Slider::SetMaximumValue(float MaximumValue) {
this->MaximumValue = MaximumValue;
}
void Slider::SetMinimumValue(float MinimumValue) {
this->MinimumValue = MinimumValue;
}
void Slider::SetOrientation(int Orientation) {
this->Orientation = Orientation;
}
void Slider::SetStatus(float Status) {
this->Status = Status;
EventQueue.push_back(sdlObjects::Events::ValueChange);
}
void Slider::SetSliderValue(float Value) {
if (MaximumValue != MinimumValue) {
Status = (Value-MinimumValue)/(MaximumValue-MinimumValue);
if (Status > 1) Status = 1; else if (Status < 0) Status = 0;
EventQueue.push_back(sdlObjects::Events::ValueChange);
}
}
void Slider::SetType(int Type) {
this->Type = Type;
}
Slider::Slider() {
Next = Previous = NULL;
Sticky = true;
MaximumValue = 1; MinimumValue = Status = 0;
Orientation = Orientation::Horizontal; Type = Sliders::Standard;
Cursor = NULL;
}
Slider::~Slider() {
Next = Previous = NULL;
MaximumValue = 1; MinimumValue = Status = 0;
Orientation = Type = 0;
Cursor = NULL;
}
}