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