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 ATTRIB(Button, onClick, void(entity, entity), SUB_Null)
12 ATTRIB(Button, onClickEntity, entity, NULL)
13 ATTRIB(Button, src, string, string_null)
14 ATTRIB(Button, srcSuffix, string, string_null)
15 ATTRIB(Button, src2, string, string_null) // is centered, same aspect, and stretched to label size
16 ATTRIB(Button, src2scale, float, 1)
17 ATTRIB(Button, srcMulti, float, 1) // 0: button square left, text right; 1: button stretched, text over it
18 ATTRIB(Button, buttonLeftOfText, float, 0)
19 ATTRIB(Button, focusable, float, 1)
20 ATTRIB(Button, pressed, float, 0)
21 ATTRIB(Button, clickTime, float, 0)
22 ATTRIB(Button, disabled, float, 0)
23 ATTRIB(Button, disabledAlpha, float, 0.3)
24 ATTRIB(Button, forcePressed, float, 0)
25 ATTRIB(Button, color, vector, '1 1 1')
26 ATTRIB(Button, colorC, vector, '1 1 1')
27 ATTRIB(Button, colorF, vector, '1 1 1')
28 ATTRIB(Button, colorD, vector, '1 1 1')
29 ATTRIB(Button, color2, vector, '1 1 1')
30 ATTRIB(Button, alpha2, float, 1)
32 ATTRIB(Button, origin, vector, '0 0 0')
33 ATTRIB(Button, size, vector, '0 0 0')
38 void Button_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
43 me.keepspaceLeft = min(0.8, absSize_y / absSize_x);
44 SUPER(Button).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
46 void Button_configureButton(entity me, string txt, float sz, string gfx)
48 SUPER(Button).configureLabel(me, txt, sz, me.srcMulti ? 0.5 : 0);
51 float Button_keyDown(entity me, float key, float ascii, float shift)
53 if(key == K_ENTER || key == K_SPACE || key == K_KP_ENTER)
55 me.clickTime = 0.1; // delayed for effect
60 float Button_mouseDrag(entity me, vector pos)
63 if(pos_x < 0) me.pressed = 0;
64 if(pos_y < 0) me.pressed = 0;
65 if(pos_x >= 1) me.pressed = 0;
66 if(pos_y >= 1) me.pressed = 0;
69 float Button_mousePress(entity me, vector pos)
71 me.mouseDrag(me, pos); // verify coordinates
72 if(cvar("menu_sounds"))
73 localsound("sound/misc/menu2.wav");
76 float Button_mouseRelease(entity me, vector pos)
78 me.mouseDrag(me, pos); // verify coordinates
82 me.onClick(me, me.onClickEntity);
87 void Button_showNotify(entity me)
89 me.focusable = !me.disabled;
92 void Button_draw(entity me)
94 vector bOrigin, bSize;
97 me.focusable = !me.disabled;
101 draw_alpha *= me.disabledAlpha;
110 draw_ButtonPicture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
111 else if(me.forcePressed || me.pressed || me.clickTime > 0)
112 draw_ButtonPicture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
114 draw_ButtonPicture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
116 draw_ButtonPicture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
120 if(me.realFontSize_y == 0)
127 bOrigin = eY * (0.5 * (1 - me.realFontSize_y)) + eX * (0.5 * (me.keepspaceLeft - me.realFontSize_x));
128 bSize = me.realFontSize;
131 draw_Picture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
132 else if(me.forcePressed || me.pressed || me.clickTime > 0)
133 draw_Picture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
135 draw_Picture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
137 draw_Picture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
142 bOrigin = me.keepspaceLeft * eX;
143 bSize = eY + eX * (1 - me.keepspaceLeft);
145 bOrigin += bSize * (0.5 - 0.5 * me.src2scale);
146 bSize = bSize * me.src2scale;
148 draw_Picture(bOrigin, me.src2, bSize, me.color2, me.alpha2);
153 SUPER(Button).draw(me);
155 if(me.clickTime > 0 && me.clickTime <= frametime)
157 // keyboard click timer expired? Fire the event then.
159 me.onClick(me, me.onClickEntity);
161 me.clickTime -= frametime;
163 if(cvar("menu_sounds") > 1)
164 if(me.focused && !me.playedfocus)
166 localsound("sound/misc/menu1.wav");
169 else if(!me.focused && me.playedfocus)