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