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"
25 #include "Interface.h"
26 #include "Variables.h"
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
;
42 ResetEventHandler( SliderOnChange
);
43 State
= IE_GUI_SLIDER_KNOB
;
54 core
->GetVideoDriver()->FreeSprite( Knob
);
57 core
->GetVideoDriver()->FreeSprite( GrabbedKnob
);
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
) ) {
74 Region
r( x
+ XPos
, y
+ YPos
, Width
, Height
);
76 if (( BackGround
->Width
< Width
) || ( BackGround
->Height
< Height
)) {
77 core
->GetVideoDriver()->BlitTiled( r
, BackGround
, true );
79 core
->GetVideoDriver()->BlitSprite( BackGround
, x
+ XPos
, y
+ YPos
, true, &r
);
83 case IE_GUI_SLIDER_KNOB
:
84 core
->GetVideoDriver()->BlitSprite( Knob
,
85 x
+ XPos
+ KnobXPos
+ ( Pos
* KnobStep
),
86 y
+ YPos
+ KnobYPos
, true );
89 case IE_GUI_SLIDER_GRABBEDKNOB
:
90 core
->GetVideoDriver()->BlitSprite( GrabbedKnob
,
91 x
+ XPos
+ KnobXPos
+ ( Pos
* KnobStep
),
92 y
+ YPos
+ KnobYPos
, true );
97 /** Returns the actual Slider Position */
98 unsigned int Slider::GetPosition()
103 /** Sets the actual Slider Position trimming to the Max and Min Values */
104 void Slider::SetPosition(unsigned int pos
)
106 if (pos
<= KnobStepsCount
) {
109 if (VarName
[0] != 0) {
112 core
->GetDictionary()->SetAt( VarName
, pos
* Value
);
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
)) {
127 if (Sum
<= KnobStepsCount
) {
133 /** Sets the selected image */
134 void Slider::SetImage(unsigned char type
, Sprite2D
* img
)
137 case IE_GUI_SLIDER_KNOB
:
139 core
->GetVideoDriver()->FreeSprite( Knob
);
143 case IE_GUI_SLIDER_GRABBEDKNOB
:
144 if (GrabbedKnob
&& Clear
)
145 core
->GetVideoDriver()->FreeSprite( GrabbedKnob
);
149 case IE_GUI_SLIDER_BACKGROUND
:
150 if (BackGround
&& Clear
)
151 core
->GetVideoDriver()->FreeSprite( BackGround
);
158 /** Mouse Button Down */
159 void Slider::OnMouseDown(unsigned short x
, unsigned short y
, unsigned short /*Button*/,
160 unsigned short /*Mod*/)
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
;
178 RunEventHandler( SliderOnChange
);
182 int befst
= xmx
/ KnobStep
;
183 if (befst
>= KnobStepsCount
) {
184 SetPosition( KnobStepsCount
- 1 );
186 RunEventHandler( SliderOnChange
);
190 int aftst
= befst
+ KnobStep
;
191 if (( xmx
- ( befst
* KnobStep
) ) <
192 ( ( aftst
* KnobStep
) - xmx
)) {
193 SetPosition( befst
);
195 SetPosition( aftst
);
198 RunEventHandler( SliderOnChange
);
207 RunEventHandler( SliderOnChange
);
211 int befst
= xmx
/ KnobStep
;
212 if (befst
>= KnobStepsCount
) {
213 SetPosition( KnobStepsCount
- 1 );
215 RunEventHandler( SliderOnChange
);
219 int aftst
= befst
+ KnobStep
;
220 if (( xmx
- ( befst
* KnobStep
) ) < ( ( aftst
* KnobStep
) - xmx
)) {
221 SetPosition( befst
);
223 SetPosition( aftst
);
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
) {
238 State
= IE_GUI_SLIDER_KNOB
;
241 /** Mouse Over Event */
242 void Slider::OnMouseOver(unsigned short x
, unsigned short /*y*/)
245 unsigned int oldPos
= Pos
;
246 if (State
== IE_GUI_SLIDER_GRABBEDKNOB
) {
252 RunEventHandler( SliderOnChange
);
256 int befst
= xmx
/ KnobStep
;
257 if (befst
>= KnobStepsCount
) {
258 SetPosition( KnobStepsCount
- 1 );
260 RunEventHandler( SliderOnChange
);
264 short aftst
= befst
+ KnobStep
;
265 if (( xmx
- ( befst
* KnobStep
) ) < ( ( aftst
* KnobStep
) - xmx
)) {
266 SetPosition( befst
);
268 SetPosition( aftst
);
271 RunEventHandler( SliderOnChange
);
276 /** Sets the Text of the current control */
277 int Slider::SetText(const char* /*string*/, int /*pos*/)
282 /** Sets the slider change event */
283 bool Slider::SetEvent(int eventType
, EventHandler handler
)
288 case IE_GUI_SLIDER_ON_CHANGE
:
289 SliderOnChange
= handler
;