GameScript: Move initialization out of constructor.
[gemrb.git] / gemrb / core / Slider.cpp
blobd9b69dd1016a3a30e817bf0353428888d8fbe7bb
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 <cmath>
22 #include "win32def.h"
23 #include "Slider.h"
24 #include "Interface.h"
25 #include "Video.h"
26 #include "Variables.h"
28 Slider::Slider(short KnobXPos, short KnobYPos, short KnobStep,
29 unsigned short KnobStepsCount, bool Clear)
31 this->KnobXPos = KnobXPos;
32 this->KnobYPos = KnobYPos;
33 this->KnobStep = KnobStep;
34 this->KnobStepsCount = KnobStepsCount;
35 Knob = NULL;
36 GrabbedKnob = NULL;
37 BackGround = NULL;
38 this->Clear = Clear;
39 ResetEventHandler( SliderOnChange );
40 State = IE_GUI_SLIDER_KNOB;
41 Pos = 0;
42 Value = 1;
45 Slider::~Slider()
47 if (!Clear) {
48 return;
50 if (Knob) {
51 core->GetVideoDriver()->FreeSprite( Knob );
53 if (GrabbedKnob) {
54 core->GetVideoDriver()->FreeSprite( GrabbedKnob );
56 if (BackGround) {
57 core->GetVideoDriver()->FreeSprite( BackGround );
61 /** Draws the Control on the Output Display */
62 void Slider::Draw(unsigned short x, unsigned short y)
64 if (!Changed && !(Owner->Flags&WF_FLOAT) ) {
65 return;
67 Changed = false;
68 if (XPos == 65535) {
69 return;
71 Region r( x + XPos, y + YPos, Width, Height );
72 if (BackGround) {
73 if (( BackGround->Width < Width ) || ( BackGround->Height < Height )) {
74 core->GetVideoDriver()->BlitTiled( r, BackGround, true );
75 } else {
76 core->GetVideoDriver()->BlitSprite( BackGround, x + XPos, y + YPos, true, &r );
79 switch (State) {
80 case IE_GUI_SLIDER_KNOB:
81 core->GetVideoDriver()->BlitSprite( Knob,
82 x + XPos + KnobXPos + ( Pos * KnobStep ),
83 y + YPos + KnobYPos, true );
84 break;
86 case IE_GUI_SLIDER_GRABBEDKNOB:
87 core->GetVideoDriver()->BlitSprite( GrabbedKnob,
88 x + XPos + KnobXPos + ( Pos * KnobStep ),
89 y + YPos + KnobYPos, true );
90 break;
94 /** Returns the actual Slider Position */
95 unsigned int Slider::GetPosition()
97 return Pos;
100 /** Sets the actual Slider Position trimming to the Max and Min Values */
101 void Slider::SetPosition(unsigned int pos)
103 if (pos <= KnobStepsCount) {
104 Pos = pos;
106 if (VarName[0] != 0) {
107 if (!Value)
108 Value = 1;
109 core->GetDictionary()->SetAt( VarName, pos * Value );
111 Changed = true;
114 /** Redraws a slider which is associated with VariableName */
115 void Slider::RedrawSlider(const char* VariableName, int Sum)
117 if (strnicmp( VarName, VariableName, MAX_VARIABLE_LENGTH )) {
118 return;
120 if (!Value) {
121 Value = 1;
123 Sum /= Value;
124 if (Sum <= KnobStepsCount) {
125 Pos = Sum;
127 Changed = true;
130 /** Sets the selected image */
131 void Slider::SetImage(unsigned char type, Sprite2D* img)
133 switch (type) {
134 case IE_GUI_SLIDER_KNOB:
135 if (Knob && Clear)
136 core->GetVideoDriver()->FreeSprite( Knob );
137 Knob = img;
138 break;
140 case IE_GUI_SLIDER_GRABBEDKNOB:
141 if (GrabbedKnob && Clear)
142 core->GetVideoDriver()->FreeSprite( GrabbedKnob );
143 GrabbedKnob = img;
144 break;
146 case IE_GUI_SLIDER_BACKGROUND:
147 if (BackGround && Clear)
148 core->GetVideoDriver()->FreeSprite( BackGround );
149 BackGround = img;
150 break;
152 Changed = true;
155 /** Mouse Button Down */
156 void Slider::OnMouseDown(unsigned short x, unsigned short y, unsigned short /*Button*/,
157 unsigned short /*Mod*/)
159 Changed = true;
160 unsigned int oldPos = Pos;
161 int mx = (KnobXPos + ( Pos * KnobStep ) - Knob->XPos);
162 int my = (KnobYPos - Knob->YPos);
163 int Mx = (mx + Knob->Width);
164 int My = (my + Knob->Height);
166 if (( x >= mx ) && ( y >= my )) {
167 if (( x <= Mx ) && ( y <= My )) {
168 State = IE_GUI_SLIDER_GRABBEDKNOB;
169 } else {
170 int mx = KnobXPos;
171 int xmx = x - mx;
172 if (x < mx) {
173 SetPosition( 0 );
174 if (oldPos != Pos) {
175 RunEventHandler( SliderOnChange );
177 return;
179 int befst = xmx / KnobStep;
180 if (befst >= KnobStepsCount) {
181 SetPosition( KnobStepsCount - 1 );
182 if (oldPos != Pos) {
183 RunEventHandler( SliderOnChange );
185 return;
187 int aftst = befst + KnobStep;
188 if (( xmx - ( befst * KnobStep ) ) <
189 ( ( aftst * KnobStep ) - xmx )) {
190 SetPosition( befst );
191 } else {
192 SetPosition( aftst );
194 if (oldPos != Pos) {
195 RunEventHandler( SliderOnChange );
198 } else {
199 int mx = KnobXPos;
200 int xmx = x - mx;
201 if (x < mx) {
202 SetPosition( 0 );
203 if (oldPos != Pos) {
204 RunEventHandler( SliderOnChange );
206 return;
208 int befst = xmx / KnobStep;
209 if (befst >= KnobStepsCount) {
210 SetPosition( KnobStepsCount - 1 );
211 if (oldPos != Pos) {
212 RunEventHandler( SliderOnChange );
214 return;
216 int aftst = befst + KnobStep;
217 if (( xmx - ( befst * KnobStep ) ) < ( ( aftst * KnobStep ) - xmx )) {
218 SetPosition( befst );
219 } else {
220 SetPosition( aftst );
222 if (oldPos != Pos) {
223 RunEventHandler( SliderOnChange );
228 /** Mouse Button Up */
229 void Slider::OnMouseUp(unsigned short /*x*/, unsigned short /*y*/, unsigned short /*Button*/,
230 unsigned short /*Mod*/)
232 if (State != IE_GUI_SLIDER_KNOB) {
233 Changed = true;
235 State = IE_GUI_SLIDER_KNOB;
238 /** Mouse Over Event */
239 void Slider::OnMouseOver(unsigned short x, unsigned short /*y*/)
241 Changed = true;
242 unsigned int oldPos = Pos;
243 if (State == IE_GUI_SLIDER_GRABBEDKNOB) {
244 int mx = KnobXPos;
245 int xmx = x - mx;
246 if (x < mx) {
247 SetPosition( 0 );
248 if (oldPos != Pos) {
249 RunEventHandler( SliderOnChange );
251 return;
253 int befst = xmx / KnobStep;
254 if (befst >= KnobStepsCount) {
255 SetPosition( KnobStepsCount - 1 );
256 if (oldPos != Pos) {
257 RunEventHandler( SliderOnChange );
259 return;
261 short aftst = befst + KnobStep;
262 if (( xmx - ( befst * KnobStep ) ) < ( ( aftst * KnobStep ) - xmx )) {
263 SetPosition( befst );
264 } else {
265 SetPosition( aftst );
267 if (oldPos != Pos) {
268 RunEventHandler( SliderOnChange );
273 /** Sets the Text of the current control */
274 int Slider::SetText(const char* /*string*/, int /*pos*/)
276 return 0;
279 /** Sets the slider change event */
280 bool Slider::SetEvent(int eventType, const char *handler)
282 Changed = true;
284 switch (eventType) {
285 case IE_GUI_SLIDER_ON_CHANGE:
286 SetEventHandler( SliderOnChange, handler );
287 break;
288 default:
289 return false;
292 return true;