]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/item/button.c
Remove debug prints from the gecko controls.
[divverent/nexuiz.git] / data / qcsrc / menu-div0test / item / button.c
1 #ifdef INTERFACE
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, 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, disabledAlpha, float, 0.3)
23         ATTRIB(Button, forcePressed, float, 0)
24         ATTRIB(Button, color, vector, '1 1 1')
25         ATTRIB(Button, colorC, vector, '1 1 1')
26         ATTRIB(Button, colorF, vector, '1 1 1')
27         ATTRIB(Button, colorD, vector, '1 1 1')
28         ATTRIB(Button, color2, vector, '1 1 1')
29         ATTRIB(Button, alpha2, float, 1)
30
31         ATTRIB(Button, origin, vector, '0 0 0')
32         ATTRIB(Button, size, vector, '0 0 0')
33 ENDCLASS(Button)
34 #endif
35
36 #ifdef IMPLEMENTATION
37 void resizeNotifyButton(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
38 {
39         if(me.srcMulti)
40                 me.keepspaceLeft = 0;
41         else
42                 me.keepspaceLeft = min(0.8, absSize_y / absSize_x);
43         resizeNotifyLabel(me, relOrigin, relSize, absOrigin, absSize);
44         me.origin = absOrigin;
45         me.size = absSize;
46 }
47 void configureButtonButton(entity me, string txt, float sz, string gfx)
48 {
49         configureLabelLabel(me, txt, sz, me.srcMulti ? 0.5 : 0);
50         me.src = gfx;
51 }
52 float keyDownButton(entity me, float key, float ascii, float shift)
53 {
54         if(key == K_ENTER || key == K_SPACE)
55         {
56                 me.clickTime = 0.1; // delayed for effect
57                 return 1;
58         }
59         return 0;
60 }
61 float mouseDragButton(entity me, vector pos)
62 {
63         me.pressed = 1;
64         if(pos_x < 0) me.pressed = 0;
65         if(pos_y < 0) me.pressed = 0;
66         if(pos_x >= 1) me.pressed = 0;
67         if(pos_y >= 1) me.pressed = 0;
68         return 1;
69 }
70 float mousePressButton(entity me, vector pos)
71 {
72         me.mouseDrag(me, pos); // verify coordinates
73         return 1;
74 }
75 float mouseReleaseButton(entity me, vector pos)
76 {
77         me.mouseDrag(me, pos); // verify coordinates
78         if(me.pressed)
79         {
80                 if not(me.disabled)
81                         me.onClick(me, me.onClickEntity);
82                 me.pressed = 0;
83         }
84         return 1;
85 }
86 void showNotifyButton(entity me)
87 {
88         me.focusable = !me.disabled;
89 }
90 void drawButton(entity me)
91 {
92         vector bOrigin, bSize;
93         float save;
94
95         me.focusable = !me.disabled;
96
97         save = draw_alpha;
98         if(me.disabled)
99                 draw_alpha *= me.disabledAlpha;
100
101         if(me.src)
102         {
103                 if(me.srcMulti)
104                 {
105                         bOrigin = '0 0 0';
106                         bSize = '1 1 0';
107                         if(me.disabled)
108                                 draw_ButtonPicture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
109                         else if(me.forcePressed || me.pressed || me.clickTime > 0)
110                                 draw_ButtonPicture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
111                         else if(me.focused)
112                                 draw_ButtonPicture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
113                         else
114                                 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)
129                                 draw_Picture(bOrigin, strcat(me.src, "_d", me.srcSuffix), bSize, me.colorD, 1);
130                         else if(me.forcePressed || me.pressed || me.clickTime > 0)
131                                 draw_Picture(bOrigin, strcat(me.src, "_c", me.srcSuffix), bSize, me.colorC, 1);
132                         else if(me.focused)
133                                 draw_Picture(bOrigin, strcat(me.src, "_f", me.srcSuffix), bSize, me.colorF, 1);
134                         else
135                                 draw_Picture(bOrigin, strcat(me.src, "_n", me.srcSuffix), bSize, me.color, 1);
136                 }
137         }
138         if(me.src2)
139         {
140                 bOrigin = me.keepspaceLeft * eX;
141                 bSize = eY + eX * (1 - me.keepspaceLeft);
142                 draw_Picture(bOrigin, me.src2, bSize, me.color2, me.alpha2);
143         }
144
145         draw_alpha = save;
146
147         drawLabel(me);
148
149         if(me.clickTime > 0 && me.clickTime < frametime)
150         {
151                 // keyboard click timer expired? Fire the event then.
152                 if not(me.disabled)
153                         me.onClick(me, me.onClickEntity);
154         }
155         me.clickTime -= frametime;
156 }
157 #endif