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, "")
15 ATTRIB(Button, src2, string, string_null) // is centered, same aspect, and stretched to label size
16 ATTRIB(Button, srcMulti, float, 1) // 0: button square left, text right; 1: button stretched, text over it
17 ATTRIB(Button, buttonLeftOfText, float, 0)
18 ATTRIB(Button, focusable, float, 1)
19 ATTRIB(Button, pressed, float, 0)
20 ATTRIB(Button, clickTime, float, 0)
21 ATTRIB(Button, disabled, float, 0)
22 ATTRIB(Button, forcePressed, float, 0)
23 ATTRIB(Button, color, vector, '1 1 1')
25 ATTRIB(Button, origin, vector, '0 0 0')
26 ATTRIB(Button, size, vector, '0 0 0')
31 void resizeNotifyButton(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
36 me.keepspaceLeft = min(0.8, absSize_y / absSize_x);
37 resizeNotifyLabel(me, relOrigin, relSize, absOrigin, absSize);
38 me.origin = absOrigin;
41 void configureButtonButton(entity me, string txt, float sz, string gfx)
43 configureLabelLabel(me, txt, sz, me.srcMulti ? 0.5 : 0);
46 float keyDownButton(entity me, float key, float ascii, float shift)
48 if(key == K_ENTER || key == K_SPACE)
50 me.clickTime = 0.1; // delayed for effect
55 float mouseDragButton(entity me, vector pos)
58 if(pos_x < 0) me.pressed = 0;
59 if(pos_y < 0) me.pressed = 0;
60 if(pos_x >= 1) me.pressed = 0;
61 if(pos_y >= 1) me.pressed = 0;
64 float mousePressButton(entity me, vector pos)
66 me.mouseDrag(me, pos); // verify coordinates
69 float mouseReleaseButton(entity me, vector pos)
71 me.mouseDrag(me, pos); // verify coordinates
75 me.onClick(me, me.onClickEntity);
80 void showNotifyButton(entity me)
82 me.focusable = !me.disabled;
84 void drawButton(entity me)
86 vector bOrigin, bSize;
88 me.focusable = !me.disabled;
99 draw_ButtonPicture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, '1 1 1', 1);
100 else if(me.forcePressed || me.pressed || me.clickTime > 0)
101 draw_ButtonPicture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.color, 1);
103 draw_ButtonPicture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.color, 1);
105 draw_ButtonPicture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
109 if(me.realFontSize_y == 0)
116 bOrigin = eY * (0.5 * (1 - me.realFontSize_y)) + eX * (0.5 * (me.keepspaceLeft - me.realFontSize_x));
117 bSize = me.realFontSize;
120 draw_Picture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, '1 1 1', 1);
121 else if(me.forcePressed || me.pressed || me.clickTime > 0)
122 draw_Picture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.color, 1);
124 draw_Picture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.color, 1);
126 draw_Picture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
131 bOrigin = me.keepspaceLeft * eX;
132 bSize = eY + eX * (1 - me.keepspaceLeft);
133 draw_Picture(bOrigin, me.src2, bSize, me.color, 1);
137 if(me.clickTime > 0 && me.clickTime < frametime)
139 // keyboard click timer expired? Fire the event then.
141 me.onClick(me, me.onClickEntity);
143 me.clickTime -= frametime;