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