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 v = median(0, (pos.x - me.pressOffset - 0.5 * me.controlWidth) / (1 - me.textSpace - me.controlWidth), 1) * (me.valueMax - me.valueMin) + me.valueMin;
161 if (me.valueStep) v = floor(0.5 + v / me.valueStep) * me.valueStep;
162 me.setValue_noAnim(me, v);
164 if(me.previousValue != me.value)
165 me.applyButton.disabled = false;
169 me.setValue(me, me.previousValue);
175 METHOD(Slider, mousePress, bool(Slider this, vector pos))
178 if (this.disabled) return false;
179 if (pos.x < 0) return false;
180 if (pos.y < 0) return false;
181 if (pos.x >= 1 - this.textSpace) return false;
182 if (pos.y >= 1) return false;
183 controlCenter = (this.value - this.valueMin) / (this.valueMax - this.valueMin) * (1 - this.textSpace - this.controlWidth) + 0.5 * this.controlWidth;
184 if (fabs(pos.x - controlCenter) <= 0.5 * this.controlWidth)
187 this.pressOffset = pos.x - controlCenter;
188 this.previousValue = this.value;
189 // this.mouseDrag(this, pos);
193 float clickValue, pageValue, inRange;
194 clickValue = median(0, (pos.x - this.pressOffset - 0.5 * this.controlWidth) / (1 - this.textSpace - this.controlWidth), 1) * (this.valueMax - this.valueMin) + this.valueMin;
195 inRange = (almost_in_bounds(this.valueMin, this.value, this.valueMax));
196 if (pos.x < controlCenter)
198 pageValue = this.value - this.valuePageStep;
199 if (this.valueStep) clickValue = floor(clickValue / this.valueStep) * this.valueStep;
200 pageValue = max(pageValue, clickValue);
204 pageValue = this.value + this.valuePageStep;
205 if (this.valueStep) clickValue = ceil(clickValue / this.valueStep) * this.valueStep;
206 pageValue = min(pageValue, clickValue);
208 if (inRange) this.setValue(this, median(this.valueMin, pageValue, this.valueMax));
209 else this.setValue(this, this.valueMax);
211 this.applyButton.disabled = false;
212 if (pageValue == clickValue)
214 controlCenter = (this.value - this.valueMin) / (this.valueMax - this.valueMin) * (1 - this.textSpace - this.controlWidth) + 0.5 * this.controlWidth;
216 this.pressOffset = pos.x - controlCenter;
217 this.previousValue = this.value;
218 // this.mouseDrag(this, pos);
223 float Slider_mouseRelease(entity me, vector pos)
226 if (me.disabled) return 0;
227 m_play_click_sound(MENU_SOUND_SLIDE);
230 void Slider_showNotify(entity me)
232 me.focusable = !me.disabled;
234 void Slider_draw(entity me)
238 me.focusable = !me.disabled;
240 if (me.disabled) draw_alpha *= me.disabledAlpha;
241 draw_ButtonPicture('0 0 0', strcat(me.src, "_s"), eX * (1 - me.textSpace) + eY, me.color2, 1);
242 if (me.valueMax > me.valueMin) // valid?
243 if (almost_in_bounds(me.valueMin, me.sliderValue, me.valueMax))
245 controlLeft = (me.sliderValue - me.valueMin) / (me.valueMax - me.valueMin) * (1 - me.textSpace - me.controlWidth);
246 if (me.disabled) draw_Picture(eX * controlLeft, strcat(me.src, "_d"), eX * me.controlWidth + eY, me.colorD, 1);
247 else if (me.pressed) draw_Picture(eX * controlLeft, strcat(me.src, "_c"), eX * me.controlWidth + eY, me.colorC, 1);
248 else if (me.focused) draw_Picture(eX * controlLeft, strcat(me.src, "_f"), eX * me.controlWidth + eY, me.colorF, 1);
249 else draw_Picture(eX * controlLeft, strcat(me.src, "_n"), eX * me.controlWidth + eY, me.color, 1);
253 if (me.sliderAnim.isFinished(me.sliderAnim))
255 anim.removeObjAnim(anim, me);
256 me.sliderAnim = NULL;
259 if (me.valueMax > me.valueMin) // valid?
260 me.setText(me, me.valueToText(me, me.value));
262 SUPER(Slider).draw(me);
263 me.text = string_null; // TEMPSTRING!