]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/button.qc
Move functions out of headers
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / button.qc
1 #ifdef INTERFACE
2 CLASS(Button) EXTENDS(Label)
3         METHOD(Button, configureButton, void(entity, string, float, string))
4         METHOD(Button, draw, void(entity))
5         METHOD(Button, showNotify, void(entity))
6         METHOD(Button, resizeNotify, void(entity, vector, vector, vector, vector))
7         METHOD(Button, keyDown, float(entity, float, float, float))
8         METHOD(Button, mousePress, float(entity, vector))
9         METHOD(Button, mouseDrag, float(entity, vector))
10         METHOD(Button, mouseRelease, float(entity, vector))
11         METHOD(Button, focusEnter, void(entity))
12         ATTRIB(Button, onClick, void(entity, entity), func_null)
13         ATTRIB(Button, onClickEntity, entity, NULL)
14         ATTRIB(Button, src, string, string_null)
15         ATTRIB(Button, srcSuffix, string, string_null)
16         ATTRIB(Button, src2, string, string_null) // is centered, same aspect, and stretched to label size
17         ATTRIB(Button, src2scale, float, 1)
18         ATTRIB(Button, srcMulti, float, 1) // 0: button square left, text right; 1: button stretched, text over it
19         ATTRIB(Button, buttonLeftOfText, float, 0)
20         ATTRIB(Button, focusable, float, 1)
21         ATTRIB(Button, pressed, float, 0)
22         ATTRIB(Button, clickTime, float, 0)
23         ATTRIB(Button, disabled, float, 0)
24         ATTRIB(Button, disabledAlpha, float, 0.3)
25         ATTRIB(Button, forcePressed, float, 0)
26         ATTRIB(Button, color, vector, '1 1 1')
27         ATTRIB(Button, colorC, vector, '1 1 1')
28         ATTRIB(Button, colorF, vector, '1 1 1')
29         ATTRIB(Button, colorD, vector, '1 1 1')
30         ATTRIB(Button, color2, vector, '1 1 1')
31         ATTRIB(Button, alpha2, float, 1)
32
33         ATTRIB(Button, origin, vector, '0 0 0')
34         ATTRIB(Button, size, vector, '0 0 0')
35 ENDCLASS(Button)
36 #endif
37
38 #ifdef IMPLEMENTATION
39 void Button_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
40 {
41         if(me.srcMulti)
42                 me.keepspaceLeft = 0;
43         else
44                 me.keepspaceLeft = min(0.8, absSize.y / absSize.x);
45         SUPER(Button).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
46 }
47 void Button_configureButton(entity me, string txt, float sz, string gfx)
48 {
49         SUPER(Button).configureLabel(me, txt, sz, me.srcMulti ? 0.5 : 0);
50         me.src = gfx;
51 }
52 float Button_keyDown(entity me, float key, float ascii, float shift)
53 {
54         if(key == K_ENTER || key == K_SPACE || key == K_KP_ENTER)
55         {
56                 me.clickTime = 0.1; // delayed for effect
57                 return 1;
58         }
59         return 0;
60 }
61 float Button_mouseDrag(entity me, vector pos)
62 {
63         me.pressed = 1;
64         if(pos.x < 0) me.pressed = 0;
65         if(pos.y < 0) me.pressed = 0;
66         if(pos.x >= 1) me.pressed = 0;
67         if(pos.y >= 1) me.pressed = 0;
68         return 1;
69 }
70 float Button_mousePress(entity me, vector pos)
71 {
72         me.mouseDrag(me, pos); // verify coordinates
73         return 1;
74 }
75 float Button_mouseRelease(entity me, vector pos)
76 {
77         me.mouseDrag(me, pos); // verify coordinates
78         if(me.pressed)
79         {
80                 if (!me.disabled)
81                 {
82                         if(cvar("menu_sounds"))
83                                 localsound("sound/misc/menu2.wav");
84                         if(me.onClick)
85                                 me.onClick(me, me.onClickEntity);
86                 }
87                 me.pressed = 0;
88         }
89         return 1;
90 }
91 void Button_showNotify(entity me)
92 {
93         me.focusable = !me.disabled;
94 }
95 void Button_focusEnter(entity me)
96 {
97         if(cvar("menu_sounds") > 1)
98                 localsound("sound/misc/menu1.wav");
99         SUPER(Button).focusEnter(me);
100 }
101 void Button_draw(entity me)
102 {
103         vector bOrigin, bSize;
104         float save;
105
106         me.focusable = !me.disabled;
107
108         save = draw_alpha;
109         if(me.disabled)
110                 draw_alpha *= me.disabledAlpha;
111
112         if(me.src)
113         {
114                 if(me.srcMulti)
115                 {
116                         bOrigin = '0 0 0';
117                         bSize = '1 1 0';
118                         if(me.disabled)
119                                 draw_ButtonPicture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
120                         else if(me.forcePressed || me.pressed || me.clickTime > 0)
121                                 draw_ButtonPicture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
122                         else if(me.focused)
123                                 draw_ButtonPicture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
124                         else
125                                 draw_ButtonPicture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
126                 }
127                 else
128                 {
129                         if(me.realFontSize_y == 0)
130                         {
131                                 bOrigin = '0 0 0';
132                                 bSize = '1 1 0';
133                         }
134                         else
135                         {
136                                 bOrigin = eY * (0.5 * (1 - me.realFontSize.y)) + eX * (0.5 * (me.keepspaceLeft - me.realFontSize.x));
137                                 bSize = me.realFontSize;
138                         }
139                         if(me.disabled)
140                                 draw_Picture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
141                         else if(me.forcePressed || me.pressed || me.clickTime > 0)
142                                 draw_Picture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
143                         else if(me.focused)
144                                 draw_Picture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
145                         else
146                                 draw_Picture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
147                 }
148         }
149         if(me.src2)
150         {
151                 bOrigin = me.keepspaceLeft * eX;
152                 bSize = eY + eX * (1 - me.keepspaceLeft);
153
154                 bOrigin += bSize * (0.5 - 0.5 * me.src2scale);
155                 bSize = bSize * me.src2scale;
156
157                 draw_Picture(bOrigin, me.src2, bSize, me.color2, me.alpha2);
158         }
159
160         draw_alpha = save;
161
162         if(me.clickTime > 0 && me.clickTime <= frametime)
163         {
164                 // keyboard click timer expired? Fire the event then.
165                 if (!me.disabled)
166                         if(me.onClick)
167                                 me.onClick(me, me.onClickEntity);
168         }
169         me.clickTime -= frametime;
170
171         SUPER(Button).draw(me);
172 }
173 #endif