]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/button.qc
Uncrustify menu/{anim,item}/*
[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, disabled, float, 0)
27                 ATTRIB(Button, disabledAlpha, float, 0.3)
28                 ATTRIB(Button, forcePressed, float, 0)
29                 ATTRIB(Button, color, vector, '1 1 1')
30                 ATTRIB(Button, colorC, vector, '1 1 1')
31                 ATTRIB(Button, colorF, vector, '1 1 1')
32                 ATTRIB(Button, colorD, vector, '1 1 1')
33                 ATTRIB(Button, color2, vector, '1 1 1')
34                 ATTRIB(Button, alpha2, float, 1)
35
36                 ATTRIB(Button, origin, vector, '0 0 0')
37                 ATTRIB(Button, size, vector, '0 0 0')
38         ENDCLASS(Button)
39 #endif
40
41 #ifdef IMPLEMENTATION
42         void Button_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
43         {
44                 if (me.srcMulti) me.keepspaceLeft = 0;
45                 else me.keepspaceLeft = min(0.8, absSize.x == 0 ? 0 : (absSize.y / absSize.x));
46                 SUPER(Button).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
47         }
48         void Button_configureButton(entity me, string txt, float sz, string gfx)
49         {
50                 SUPER(Button).configureLabel(me, txt, sz, me.srcMulti ? 0.5 : 0);
51                 me.src = gfx;
52         }
53         float Button_keyDown(entity me, float key, float ascii, float shift)
54         {
55                 if (key == K_ENTER || key == K_SPACE || key == K_KP_ENTER)
56                 {
57                         me.playClickSound(me);
58                         me.clickTime = 0.1;  // delayed for effect
59                         return 1;
60                 }
61                 return 0;
62         }
63         float Button_mouseDrag(entity me, vector pos)
64         {
65                 me.pressed = 1;
66                 if (pos.x < 0) me.pressed = 0;
67                 if (pos.y < 0) me.pressed = 0;
68                 if (pos.x >= 1) me.pressed = 0;
69                 if (pos.y >= 1) me.pressed = 0;
70                 return 1;
71         }
72         float Button_mousePress(entity me, vector pos)
73         {
74                 me.mouseDrag(me, pos);  // verify coordinates
75                 return 1;
76         }
77         float Button_mouseRelease(entity me, vector pos)
78         {
79                 me.mouseDrag(me, pos);  // verify coordinates
80                 if (me.pressed)
81                 {
82                         if (!me.disabled)
83                         {
84                                 me.playClickSound(me);
85                                 if (me.onClick) 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_draw(entity me)
96         {
97                 vector bOrigin, bSize;
98                 float save;
99
100                 me.focusable = !me.disabled;
101
102                 save = draw_alpha;
103                 if (me.disabled) draw_alpha *= me.disabledAlpha;
104
105                 if (me.src)
106                 {
107                         if (me.srcMulti)
108                         {
109                                 bOrigin = '0 0 0';
110                                 bSize = '1 1 0';
111                                 if (me.disabled) draw_ButtonPicture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
112                                 else if (me.forcePressed || me.pressed || me.clickTime > 0) draw_ButtonPicture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
113                                 else if (me.focused) draw_ButtonPicture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
114                                 else draw_ButtonPicture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
115                         }
116                         else
117                         {
118                                 if (me.realFontSize_y == 0)
119                                 {
120                                         bOrigin = '0 0 0';
121                                         bSize = '1 1 0';
122                                 }
123                                 else
124                                 {
125                                         bOrigin = eY * (0.5 * (1 - me.realFontSize.y)) + eX * (0.5 * (me.keepspaceLeft - me.realFontSize.x));
126                                         bSize = me.realFontSize;
127                                 }
128                                 if (me.disabled) draw_Picture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
129                                 else if (me.forcePressed || me.pressed || me.clickTime > 0) draw_Picture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
130                                 else if (me.focused) draw_Picture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
131                                 else draw_Picture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
132                         }
133                 }
134                 if (me.src2)
135                 {
136                         bOrigin = me.keepspaceLeft * eX;
137                         bSize = eY + eX * (1 - me.keepspaceLeft);
138
139                         bOrigin += bSize * (0.5 - 0.5 * me.src2scale);
140                         bSize = bSize * me.src2scale;
141
142                         draw_Picture(bOrigin, me.src2, bSize, me.color2, me.alpha2);
143                 }
144
145                 draw_alpha = save;
146
147                 if (me.clickTime > 0 && me.clickTime <= frametime)
148                 {
149                         // keyboard click timer expired? Fire the event then.
150                         if (!me.disabled)
151                                 if (me.onClick) me.onClick(me, me.onClickEntity);
152                 }
153                 me.clickTime -= frametime;
154
155                 SUPER(Button).draw(me);
156         }
157         void Dialog_Close(entity button, entity me);
158         void Button_playClickSound(entity me)
159         {
160                 if (me.onClick == DialogOpenButton_Click) m_play_click_sound(MENU_SOUND_OPEN);
161                 else if (me.onClick == Dialog_Close) m_play_click_sound(MENU_SOUND_CLOSE);
162                 else m_play_click_sound(MENU_SOUND_EXECUTE);
163         }
164 #endif