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.
24 #include "Interface.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
;
39 ResetEventHandler( SliderOnChange
);
40 State
= IE_GUI_SLIDER_KNOB
;
51 core
->GetVideoDriver()->FreeSprite( Knob
);
54 core
->GetVideoDriver()->FreeSprite( GrabbedKnob
);
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
) ) {
71 Region
r( x
+ XPos
, y
+ YPos
, Width
, Height
);
73 if (( BackGround
->Width
< Width
) || ( BackGround
->Height
< Height
)) {
74 core
->GetVideoDriver()->BlitTiled( r
, BackGround
, true );
76 core
->GetVideoDriver()->BlitSprite( BackGround
, x
+ XPos
, y
+ YPos
, true, &r
);
80 case IE_GUI_SLIDER_KNOB
:
81 core
->GetVideoDriver()->BlitSprite( Knob
,
82 x
+ XPos
+ KnobXPos
+ ( Pos
* KnobStep
),
83 y
+ YPos
+ KnobYPos
, true );
86 case IE_GUI_SLIDER_GRABBEDKNOB
:
87 core
->GetVideoDriver()->BlitSprite( GrabbedKnob
,
88 x
+ XPos
+ KnobXPos
+ ( Pos
* KnobStep
),
89 y
+ YPos
+ KnobYPos
, true );
94 /** Returns the actual Slider Position */
95 unsigned int Slider::GetPosition()
100 /** Sets the actual Slider Position trimming to the Max and Min Values */
101 void Slider::SetPosition(unsigned int pos
)
103 if (pos
<= KnobStepsCount
) {
106 if (VarName
[0] != 0) {
109 core
->GetDictionary()->SetAt( VarName
, pos
* Value
);
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
)) {
124 if (Sum
<= KnobStepsCount
) {
130 /** Sets the selected image */
131 void Slider::SetImage(unsigned char type
, Sprite2D
* img
)
134 case IE_GUI_SLIDER_KNOB
:
136 core
->GetVideoDriver()->FreeSprite( Knob
);
140 case IE_GUI_SLIDER_GRABBEDKNOB
:
141 if (GrabbedKnob
&& Clear
)
142 core
->GetVideoDriver()->FreeSprite( GrabbedKnob
);
146 case IE_GUI_SLIDER_BACKGROUND
:
147 if (BackGround
&& Clear
)
148 core
->GetVideoDriver()->FreeSprite( BackGround
);
155 /** Mouse Button Down */
156 void Slider::OnMouseDown(unsigned short x
, unsigned short y
, unsigned short /*Button*/,
157 unsigned short /*Mod*/)
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
;
175 RunEventHandler( SliderOnChange
);
179 int befst
= xmx
/ KnobStep
;
180 if (befst
>= KnobStepsCount
) {
181 SetPosition( KnobStepsCount
- 1 );
183 RunEventHandler( SliderOnChange
);
187 int aftst
= befst
+ KnobStep
;
188 if (( xmx
- ( befst
* KnobStep
) ) <
189 ( ( aftst
* KnobStep
) - xmx
)) {
190 SetPosition( befst
);
192 SetPosition( aftst
);
195 RunEventHandler( SliderOnChange
);
204 RunEventHandler( SliderOnChange
);
208 int befst
= xmx
/ KnobStep
;
209 if (befst
>= KnobStepsCount
) {
210 SetPosition( KnobStepsCount
- 1 );
212 RunEventHandler( SliderOnChange
);
216 int aftst
= befst
+ KnobStep
;
217 if (( xmx
- ( befst
* KnobStep
) ) < ( ( aftst
* KnobStep
) - xmx
)) {
218 SetPosition( befst
);
220 SetPosition( aftst
);
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
) {
235 State
= IE_GUI_SLIDER_KNOB
;
238 /** Mouse Over Event */
239 void Slider::OnMouseOver(unsigned short x
, unsigned short /*y*/)
242 unsigned int oldPos
= Pos
;
243 if (State
== IE_GUI_SLIDER_GRABBEDKNOB
) {
249 RunEventHandler( SliderOnChange
);
253 int befst
= xmx
/ KnobStep
;
254 if (befst
>= KnobStepsCount
) {
255 SetPosition( KnobStepsCount
- 1 );
257 RunEventHandler( SliderOnChange
);
261 short aftst
= befst
+ KnobStep
;
262 if (( xmx
- ( befst
* KnobStep
) ) < ( ( aftst
* KnobStep
) - xmx
)) {
263 SetPosition( befst
);
265 SetPosition( aftst
);
268 RunEventHandler( SliderOnChange
);
273 /** Sets the Text of the current control */
274 int Slider::SetText(const char* /*string*/, int /*pos*/)
279 /** Sets the slider change event */
280 bool Slider::SetEvent(int eventType
, const char *handler
)
285 case IE_GUI_SLIDER_ON_CHANGE
:
286 SetEventHandler( SliderOnChange
, handler
);