Move EventMgr to GUI.
[gemrb.git] / gemrb / core / GUI / Slider.cpp
blob09812ab3cc05b4dd2a7dfbb535073f57f51a0647
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "GUI/Slider.h"
23 #include "win32def.h"
25 #include "Interface.h"
26 #include "Variables.h"
27 #include "Video.h"
29 #include <cmath>
31 Slider::Slider(short KnobXPos, short KnobYPos, short KnobStep,
32 unsigned short KnobStepsCount, bool Clear)
34 this->KnobXPos = KnobXPos;
35 this->KnobYPos = KnobYPos;
36 this->KnobStep = KnobStep;
37 this->KnobStepsCount = KnobStepsCount;
38 Knob = NULL;
39 GrabbedKnob = NULL;
40 BackGround = NULL;
41 this->Clear = Clear;
42 ResetEventHandler( SliderOnChange );
43 State = IE_GUI_SLIDER_KNOB;
44 Pos = 0;
45 Value = 1;
48 Slider::~Slider()
50 if (!Clear) {
51 return;
53 if (Knob) {
54 core->GetVideoDriver()->FreeSprite( Knob );
56 if (GrabbedKnob) {
57 core->GetVideoDriver()->FreeSprite( GrabbedKnob );
59 if (BackGround) {
60 core->GetVideoDriver()->FreeSprite( BackGround );
64 /** Draws the Control on the Output Display */
65 void Slider::Draw(unsigned short x, unsigned short y)
67 if (!Changed && !(Owner->Flags&WF_FLOAT) ) {
68 return;
70 Changed = false;
71 if (XPos == 65535) {
72 return;
74 Region r( x + XPos, y + YPos, Width, Height );
75 if (BackGround) {
76 if (( BackGround->Width < Width ) || ( BackGround->Height < Height )) {
77 core->GetVideoDriver()->BlitTiled( r, BackGround, true );
78 } else {
79 core->GetVideoDriver()->BlitSprite( BackGround, x + XPos, y + YPos, true, &r );
82 switch (State) {
83 case IE_GUI_SLIDER_KNOB:
84 core->GetVideoDriver()->BlitSprite( Knob,
85 x + XPos + KnobXPos + ( Pos * KnobStep ),
86 y + YPos + KnobYPos, true );
87 break;
89 case IE_GUI_SLIDER_GRABBEDKNOB:
90 core->GetVideoDriver()->BlitSprite( GrabbedKnob,
91 x + XPos + KnobXPos + ( Pos * KnobStep ),
92 y + YPos + KnobYPos, true );
93 break;
97 /** Returns the actual Slider Position */
98 unsigned int Slider::GetPosition()
100 return Pos;
103 /** Sets the actual Slider Position trimming to the Max and Min Values */
104 void Slider::SetPosition(unsigned int pos)
106 if (pos <= KnobStepsCount) {
107 Pos = pos;
109 if (VarName[0] != 0) {
110 if (!Value)
111 Value = 1;
112 core->GetDictionary()->SetAt( VarName, pos * Value );
114 Changed = true;
117 /** Redraws a slider which is associated with VariableName */
118 void Slider::RedrawSlider(const char* VariableName, int Sum)
120 if (strnicmp( VarName, VariableName, MAX_VARIABLE_LENGTH )) {
121 return;
123 if (!Value) {
124 Value = 1;
126 Sum /= Value;
127 if (Sum <= KnobStepsCount) {
128 Pos = Sum;
130 Changed = true;
133 /** Sets the selected image */
134 void Slider::SetImage(unsigned char type, Sprite2D* img)
136 switch (type) {
137 case IE_GUI_SLIDER_KNOB:
138 if (Knob && Clear)
139 core->GetVideoDriver()->FreeSprite( Knob );
140 Knob = img;
141 break;
143 case IE_GUI_SLIDER_GRABBEDKNOB:
144 if (GrabbedKnob && Clear)
145 core->GetVideoDriver()->FreeSprite( GrabbedKnob );
146 GrabbedKnob = img;
147 break;
149 case IE_GUI_SLIDER_BACKGROUND:
150 if (BackGround && Clear)
151 core->GetVideoDriver()->FreeSprite( BackGround );
152 BackGround = img;
153 break;
155 Changed = true;
158 /** Mouse Button Down */
159 void Slider::OnMouseDown(unsigned short x, unsigned short y, unsigned short /*Button*/,
160 unsigned short /*Mod*/)
162 Changed = true;
163 unsigned int oldPos = Pos;
164 int mx = (KnobXPos + ( Pos * KnobStep ) - Knob->XPos);
165 int my = (KnobYPos - Knob->YPos);
166 int Mx = (mx + Knob->Width);
167 int My = (my + Knob->Height);
169 if (( x >= mx ) && ( y >= my )) {
170 if (( x <= Mx ) && ( y <= My )) {
171 State = IE_GUI_SLIDER_GRABBEDKNOB;
172 } else {
173 int mx = KnobXPos;
174 int xmx = x - mx;
175 if (x < mx) {
176 SetPosition( 0 );
177 if (oldPos != Pos) {
178 RunEventHandler( SliderOnChange );
180 return;
182 int befst = xmx / KnobStep;
183 if (befst >= KnobStepsCount) {
184 SetPosition( KnobStepsCount - 1 );
185 if (oldPos != Pos) {
186 RunEventHandler( SliderOnChange );
188 return;
190 int aftst = befst + KnobStep;
191 if (( xmx - ( befst * KnobStep ) ) <
192 ( ( aftst * KnobStep ) - xmx )) {
193 SetPosition( befst );
194 } else {
195 SetPosition( aftst );
197 if (oldPos != Pos) {
198 RunEventHandler( SliderOnChange );
201 } else {
202 int mx = KnobXPos;
203 int xmx = x - mx;
204 if (x < mx) {
205 SetPosition( 0 );
206 if (oldPos != Pos) {
207 RunEventHandler( SliderOnChange );
209 return;
211 int befst = xmx / KnobStep;
212 if (befst >= KnobStepsCount) {
213 SetPosition( KnobStepsCount - 1 );
214 if (oldPos != Pos) {
215 RunEventHandler( SliderOnChange );
217 return;
219 int aftst = befst + KnobStep;
220 if (( xmx - ( befst * KnobStep ) ) < ( ( aftst * KnobStep ) - xmx )) {
221 SetPosition( befst );
222 } else {
223 SetPosition( aftst );
225 if (oldPos != Pos) {
226 RunEventHandler( SliderOnChange );
231 /** Mouse Button Up */
232 void Slider::OnMouseUp(unsigned short /*x*/, unsigned short /*y*/, unsigned short /*Button*/,
233 unsigned short /*Mod*/)
235 if (State != IE_GUI_SLIDER_KNOB) {
236 Changed = true;
238 State = IE_GUI_SLIDER_KNOB;
241 /** Mouse Over Event */
242 void Slider::OnMouseOver(unsigned short x, unsigned short /*y*/)
244 Changed = true;
245 unsigned int oldPos = Pos;
246 if (State == IE_GUI_SLIDER_GRABBEDKNOB) {
247 int mx = KnobXPos;
248 int xmx = x - mx;
249 if (x < mx) {
250 SetPosition( 0 );
251 if (oldPos != Pos) {
252 RunEventHandler( SliderOnChange );
254 return;
256 int befst = xmx / KnobStep;
257 if (befst >= KnobStepsCount) {
258 SetPosition( KnobStepsCount - 1 );
259 if (oldPos != Pos) {
260 RunEventHandler( SliderOnChange );
262 return;
264 short aftst = befst + KnobStep;
265 if (( xmx - ( befst * KnobStep ) ) < ( ( aftst * KnobStep ) - xmx )) {
266 SetPosition( befst );
267 } else {
268 SetPosition( aftst );
270 if (oldPos != Pos) {
271 RunEventHandler( SliderOnChange );
276 /** Sets the Text of the current control */
277 int Slider::SetText(const char* /*string*/, int /*pos*/)
279 return 0;
282 /** Sets the slider change event */
283 bool Slider::SetEvent(int eventType, EventHandler handler)
285 Changed = true;
287 switch (eventType) {
288 case IE_GUI_SLIDER_ON_CHANGE:
289 SliderOnChange = handler;
290 break;
291 default:
292 return false;
295 return true;