3 #include "../anim/easing.qh"
4 #include "../anim/animhost.qh"
8 void Slider_setValue_allowAnim(entity me, float val, bool allowAnim)
10 if (allowAnim && me.animated)
13 if (!me.sliderAnim) me.sliderAnim = makeHostedEasing(me, Slider_setSliderValue, easingQuadOut, t, me.sliderValue, val);
14 else me.sliderAnim.update(me.sliderAnim, t, me.sliderValue, val);
18 me.setSliderValue(me, val);
22 void Slider_setValue_noAnim(entity me, float val)
24 Slider_setValue_allowAnim(me, val, false);
26 void Slider_setValue(entity me, float val)
28 Slider_setValue_allowAnim(me, val, true);
30 void Slider_setSliderValue(entity me, float val)
34 string Slider_toString(entity me)
36 return sprintf("%d (%s)", me.value, me.valueToText(me, me.value));
38 void Slider_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
40 SUPER(Slider).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
41 me.controlWidth = absSize.x == 0 ? 0 : (absSize.y / absSize.x);
43 string Slider_valueToText(entity me, float val)
45 if (almost_in_bounds(me.valueMin, val, me.valueMax)) return ftos_decimals(val * me.valueDisplayMultiplier, me.valueDigits);
48 void Slider_configureSliderVisuals(entity me, float sz, float theAlign, float theTextSpace, string gfx)
50 SUPER(Slider).configureLabel(me, string_null, sz, theAlign);
51 me.textSpace = theTextSpace;
52 me.keepspaceLeft = (theTextSpace == 0) ? 0 : (1 - theTextSpace);
55 void Slider_configureSliderValues(entity me, float theValueMin, float theValue, float theValueMax, float theValueStep, float theValueKeyStep, float theValuePageStep)
58 me.sliderValue = theValue;
59 me.valueStep = theValueStep;
60 me.valueMin = theValueMin;
61 me.valueMax = theValueMax;
62 me.valueKeyStep = theValueKeyStep;
63 me.valuePageStep = theValuePageStep;
65 if (fabs(floor(me.valueStep * 100 + 0.5) - (me.valueStep * 100)) < 0.01) // about a whole number of 100ths
67 if (fabs(floor(me.valueStep * 10 + 0.5) - (me.valueStep * 10)) < 0.01) // about a whole number of 10ths
69 if (fabs(floor(me.valueStep * 1 + 0.5) - (me.valueStep * 1)) < 0.01) // about a whole number
72 float Slider_keyDown(entity me, float key, float ascii, float shift)
76 if (me.disabled) return 0;
77 inRange = (almost_in_bounds(me.valueMin, me.value, me.valueMax));
78 if (key == K_LEFTARROW || key == K_KP_LEFTARROW || key == K_MWHEELDOWN)
80 if (inRange) me.setValue(me, median(me.valueMin, me.value - me.valueKeyStep, me.valueMax));
81 else me.setValue(me, me.valueMax);
84 if (key == K_RIGHTARROW || key == K_KP_RIGHTARROW || key == K_MWHEELUP)
86 if (inRange) me.setValue(me, median(me.valueMin, me.value + me.valueKeyStep, me.valueMax));
87 else me.setValue(me, me.valueMin);
90 if (key == K_PGDN || key == K_KP_PGDN)
92 if (inRange) me.setValue(me, median(me.valueMin, me.value - me.valuePageStep, me.valueMax));
93 else me.setValue(me, me.valueMax);
96 if (key == K_PGUP || key == K_KP_PGUP)
98 if (inRange) me.setValue(me, median(me.valueMin, me.value + me.valuePageStep, me.valueMax));
99 else me.setValue(me, me.valueMin);
102 if (key == K_HOME || key == K_KP_HOME)
104 me.setValue(me, me.valueMin);
107 if (key == K_END || key == K_KP_END)
109 me.setValue(me, me.valueMax);
115 me.applyButton.disabled = false;
118 // TODO more keys (NOTE also add them to Slider_keyUp)
121 float Slider_keyUp(entity me, float key, float ascii, float shift)
123 if (me.disabled) return 0;
129 case K_KP_RIGHTARROW:
138 m_play_click_sound(MENU_SOUND_SLIDE);
142 float Slider_mouseDrag(entity me, vector pos)
146 if (me.disabled) return 0;
151 if (pos.x < 0 - me.tolerance.x) hit = 0;
152 if (pos.y < 0 - me.tolerance.y) hit = 0;
153 if (pos.x >= 1 - me.textSpace + me.tolerance.x) hit = 0;
154 if (pos.y >= 1 + me.tolerance.y) hit = 0;
160 float f = bound(0, (pos.x - me.pressOffset - 0.5 * me.controlWidth) / (1 - me.textSpace - me.controlWidth), 1);
161 v = f * (me.valueMax - me.valueMin) + me.valueMin;
162 // there's no need to round min and max value... also if we did, v could be set
163 // to an out of bounds value due to precision errors
164 if (f > 0 && f < 1 && me.valueStep)
165 v = floor(0.5 + v / me.valueStep) * me.valueStep;
166 me.setValue_noAnim(me, v);
168 if(me.previousValue != me.value)
169 me.applyButton.disabled = false;
173 me.setValue(me, me.previousValue);
179 METHOD(Slider, mousePress, bool(Slider this, vector pos))
182 if (this.disabled) return false;
183 if (pos.x < 0) return false;
184 if (pos.y < 0) return false;
185 if (pos.x >= 1 - this.textSpace) return false;
186 if (pos.y >= 1) return false;
187 controlCenter = (this.value - this.valueMin) / (this.valueMax - this.valueMin) * (1 - this.textSpace - this.controlWidth) + 0.5 * this.controlWidth;
188 if (fabs(pos.x - controlCenter) <= 0.5 * this.controlWidth)
191 this.pressOffset = pos.x - controlCenter;
192 this.previousValue = this.value;
193 // this.mouseDrag(this, pos);
197 float clickValue, pageValue, inRange;
198 clickValue = median(0, (pos.x - this.pressOffset - 0.5 * this.controlWidth) / (1 - this.textSpace - this.controlWidth), 1) * (this.valueMax - this.valueMin) + this.valueMin;
199 inRange = (almost_in_bounds(this.valueMin, this.value, this.valueMax));
200 if (pos.x < controlCenter)
202 pageValue = this.value - this.valuePageStep;
203 if (this.valueStep) clickValue = floor(clickValue / this.valueStep) * this.valueStep;
204 pageValue = max(pageValue, clickValue);
208 pageValue = this.value + this.valuePageStep;
209 if (this.valueStep) clickValue = ceil(clickValue / this.valueStep) * this.valueStep;
210 pageValue = min(pageValue, clickValue);
212 if (inRange) this.setValue(this, median(this.valueMin, pageValue, this.valueMax));
213 else this.setValue(this, this.valueMax);
215 this.applyButton.disabled = false;
216 if (pageValue == clickValue)
218 controlCenter = (this.value - this.valueMin) / (this.valueMax - this.valueMin) * (1 - this.textSpace - this.controlWidth) + 0.5 * this.controlWidth;
220 this.pressOffset = pos.x - controlCenter;
221 this.previousValue = this.value;
222 // this.mouseDrag(this, pos);
227 float Slider_mouseRelease(entity me, vector pos)
230 if (me.disabled) return 0;
231 m_play_click_sound(MENU_SOUND_SLIDE);
234 void Slider_showNotify(entity me)
236 me.focusable = !me.disabled;
238 void Slider_draw(entity me)
242 me.focusable = !me.disabled;
244 if (me.disabled) draw_alpha *= me.disabledAlpha;
245 draw_ButtonPicture('0 0 0', strcat(me.src, "_s"), eX * (1 - me.textSpace) + eY, me.color2, 1);
246 if (me.valueMax > me.valueMin) // valid?
247 if (almost_in_bounds(me.valueMin, me.sliderValue, me.valueMax))
249 controlLeft = (me.sliderValue - me.valueMin) / (me.valueMax - me.valueMin) * (1 - me.textSpace - me.controlWidth);
250 if (me.disabled) draw_Picture(eX * controlLeft, strcat(me.src, "_d"), eX * me.controlWidth + eY, me.colorD, 1);
251 else if (me.pressed) draw_Picture(eX * controlLeft, strcat(me.src, "_c"), eX * me.controlWidth + eY, me.colorC, 1);
252 else if (me.focused) draw_Picture(eX * controlLeft, strcat(me.src, "_f"), eX * me.controlWidth + eY, me.colorF, 1);
253 else draw_Picture(eX * controlLeft, strcat(me.src, "_n"), eX * me.controlWidth + eY, me.color, 1);
257 if (me.sliderAnim.isFinished(me.sliderAnim))
259 anim.removeObjAnim(anim, me);
260 me.sliderAnim = NULL;
263 if (me.valueMax > me.valueMin) // valid?
264 me.setText(me, me.valueToText(me, me.value));
266 SUPER(Slider).draw(me);
267 me.text = string_null; // TEMPSTRING!