]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/item/slider.c
support for team select dialog, disabled buttons and grid layout; updated mbuiltin.qc
[divverent/nexuiz.git] / data / qcsrc / menu-div0test / item / slider.c
1 #ifdef INTERFACE
2 CLASS(Slider) EXTENDS(Label)
3         METHOD(Slider, resizeNotify, void(entity, vector, vector, vector, vector))
4         METHOD(Slider, configureSliderVisuals, void(entity, string, string, float, float, string))
5         METHOD(Slider, configureSliderValues, void(entity, float, float, float, float, float, float))
6         METHOD(Slider, draw, void(entity))
7         METHOD(Slider, keyDown, float(entity, float, float, float))
8         METHOD(Slider, mousePress, float(entity, vector))
9         METHOD(Slider, mouseDrag, float(entity, vector))
10         METHOD(Slider, mouseRelease, float(entity, vector))
11         ATTRIB(Slider, src, string, "")
12         ATTRIB(Slider, focusable, float, 1)
13         ATTRIB(Slider, value, float, 0)
14         ATTRIB(Slider, valueMin, float, 0)
15         ATTRIB(Slider, valueMax, float, 0)
16         ATTRIB(Slider, valueStep, float, 0)
17         ATTRIB(Slider, valueKeyStep, float, 0)
18         ATTRIB(Slider, valuePageStep, float, 0)
19         ATTRIB(Slider, valueSpace, float, 0)
20         ATTRIB(Slider, controlWidth, float, 0)
21         ATTRIB(Slider, pressed, float, 0)
22         ATTRIB(Slider, pressOffset, float, 0)
23         ATTRIB(Slider, prefix, string, "")
24         ATTRIB(Slider, suffix, string, "")
25         ATTRIB(Slider, previousValue, float, 0)
26 ENDCLASS(Slider)
27 #endif
28
29 #ifdef IMPLEMENTATION
30 void resizeNotifySlider(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
31 {
32         resizeNotifyLabel(me, relOrigin, relSize, absOrigin, absSize);
33         me.controlWidth = absSize_y / absSize_x;
34 }
35 void configureSliderVisualsSlider(entity me, string thePrefix, string theSuffix, float sz, float theValueSpace, string gfx)
36 {
37         configureLabelLabel(me, "", sz, 1);
38         me.prefix = thePrefix;
39         me.suffix = theSuffix;
40         me.valueSpace = theValueSpace;
41         me.src = gfx;
42 }
43 void configureSliderValuesSlider(entity me, float theValueMin, float theValue, float theValueMax, float theValueStep, float theValueKeyStep, float theValuePageStep)
44 {
45         me.value = theValue;
46         me.valueStep = theValueStep;
47         me.valueMin = theValueMin;
48         me.valueMax = theValueMax;
49         me.valueKeyStep = theValueKeyStep;
50         me.valuePageStep = theValuePageStep;
51 }
52 float keyDownSlider(entity me, float key, float ascii, float shift)
53 {
54         if(key == K_LEFTARROW)
55         {
56                 me.value = bound(me.valueMin, me.value - me.valueKeyStep, me.valueMax);
57                 return 1;
58         }
59         if(key == K_RIGHTARROW)
60         {
61                 me.value = bound(me.valueMin, me.value + me.valueKeyStep, me.valueMax);
62                 return 1;
63         }
64         if(key == K_PGUP)
65         {
66                 me.value = bound(me.valueMin, me.value - me.valuePageStep, me.valueMax);
67                 return 1;
68         }
69         if(key == K_PGDN)
70         {
71                 me.value = bound(me.valueMin, me.value + me.valuePageStep, me.valueMax);
72                 return 1;
73         }
74         // TODO more keys
75         return 0;
76 }
77 float mouseDragSlider(entity me, vector pos)
78 {
79         float hit;
80         if(me.pressed)
81         {
82                 hit = 1;
83                 if(pos_x < 0) hit = 0;
84                 if(pos_y < 0) hit = 0;
85                 if(pos_x >= 1 - me.valueSpace) hit = 0;
86                 if(pos_y >= 1) hit = 0;
87                 if(hit)
88                 {
89                         me.value = bound(0, (pos_x - me.pressOffset - 0.5 * me.controlWidth) / (1 - me.valueSpace - me.controlWidth), 1) * (me.valueMax - me.valueMin) + me.valueMin;
90                         if(me.valueStep)
91                                 me.value = floor(0.5 + me.value / me.valueStep) * me.valueStep;
92                 }
93                 else
94                         me.value = me.previousValue;
95         }
96         return 1;
97 }
98 float mousePressSlider(entity me, vector pos)
99 {
100         float controlCenter;
101         if(pos_x < 0) return 0;
102         if(pos_y < 0) return 0;
103         if(pos_x >= 1 - me.valueSpace) return 0;
104         if(pos_y < 0) return 0;
105         controlCenter = (me.value - me.valueMin) / (me.valueMax - me.valueMin) * (1 - me.valueSpace - me.controlWidth) + 0.5 * me.controlWidth;
106         if(fabs(pos_x - controlCenter) <= 0.5 * me.controlWidth)
107         {
108                 me.pressed = 1;
109                 me.pressOffset = pos_x - controlCenter;
110                 me.previousValue = me.value;
111                 me.mouseDrag(me, pos);
112         }
113         else
114         {
115                 if(pos_x < controlCenter)
116                         me.keyDown(me, K_PGUP, 0, 0);
117                 else
118                         me.keyDown(me, K_PGDN, 0, 0);
119         }
120         return 1;
121 }
122 float mouseReleaseSlider(entity me, vector pos)
123 {
124         me.pressed = 0;
125         return 1;
126 }
127 void drawSlider(entity me)
128 {
129         float controlLeft;
130         controlLeft = (me.value - me.valueMin) / (me.valueMax - me.valueMin) * (1 - me.valueSpace - me.controlWidth);
131         draw_ButtonPicture('0 0 0', strcat(me.src, "_s"), eX * (1 - me.valueSpace) + eY, '1 1 1', 1);
132         if(me.pressed)
133                 draw_Picture(eX * controlLeft, strcat(me.src, "_c"), eX * me.controlWidth + eY, '1 1 1', 1);
134         else if(me.focused)
135                 draw_Picture(eX * controlLeft, strcat(me.src, "_f"), eX * me.controlWidth + eY, '1 1 1', 1);
136         else
137                 draw_Picture(eX * controlLeft, strcat(me.src, "_n"), eX * me.controlWidth + eY, '1 1 1', 1);
138         me.setText(me, strcat(me.prefix, ftos(me.value), me.suffix));
139         drawLabel(me);
140         me.text = ""; // TEMPSTRING!
141 }
142 #endif