]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/item/button.c
improved dialog system; actually handle cvars; put some stuff in video dialog
[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, "")
14         ATTRIB(Button, srcMulti, float, 1)
15         ATTRIB(Button, focusable, float, 1)
16         ATTRIB(Button, pressed, float, 0)
17         ATTRIB(Button, clickTime, float, 0)
18         ATTRIB(Button, disabled, float, 0)
19         ATTRIB(Button, forcePressed, float, 0)
20         ATTRIB(Button, color, vector, '1 1 1')
21
22         ATTRIB(Button, origin, vector, '0 0 0')
23         ATTRIB(Button, size, vector, '0 0 0')
24 ENDCLASS(Button)
25 #endif
26
27 #ifdef IMPLEMENTATION
28 void resizeNotifyButton(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
29 {
30         resizeNotifyLabel(me, relOrigin, relSize, absOrigin, absSize);
31         me.origin = absOrigin;
32         me.size = absSize;
33 }
34 void configureButtonButton(entity me, string txt, float sz, string gfx)
35 {
36         configureLabelLabel(me, txt, sz, 0.5);
37         me.src = gfx;
38 }
39 float keyDownButton(entity me, float key, float ascii, float shift)
40 {
41         if(key == K_ENTER || key == K_SPACE)
42         {
43                 me.clickTime = 0.1; // delayed for effect
44                 return 1;
45         }
46         return 0;
47 }
48 float mouseDragButton(entity me, vector pos)
49 {
50         me.pressed = 1;
51         if(pos_x < 0) me.pressed = 0;
52         if(pos_y < 0) me.pressed = 0;
53         if(pos_x >= 1) me.pressed = 0;
54         if(pos_y >= 1) me.pressed = 0;
55         return 1;
56 }
57 float mousePressButton(entity me, vector pos)
58 {
59         me.mouseDrag(me, pos); // verify coordinates
60         return 1;
61 }
62 float mouseReleaseButton(entity me, vector pos)
63 {
64         me.mouseDrag(me, pos); // verify coordinates
65         if(me.pressed)
66         {
67                 if(!me.disabled)
68                         me.onClick(me, me.onClickEntity);
69                 me.pressed = 0;
70         }
71         return 1;
72 }
73 void showNotifyButton(entity me)
74 {
75         me.focusable = !me.disabled;
76 }
77 void drawButton(entity me)
78 {
79         me.focusable = !me.disabled;
80         if(me.disabled)
81                 draw_alpha *= 0.5;
82
83         if(me.srcMulti)
84         {
85                 if(me.disabled)
86                         draw_ButtonPicture('0 0 0', strcat(me.src, "_d"), '1 1 0', '1 1 1', 1);
87                 else if(me.forcePressed || me.pressed || me.clickTime > 0)
88                         draw_ButtonPicture('0 0 0', strcat(me.src, "_c"), '1 1 0', me.color, 1);
89                 else if(me.focused)
90                         draw_ButtonPicture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.color, 1);
91                 else
92                         draw_ButtonPicture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
93         }
94         else
95         {
96                 if(me.disabled)
97                         draw_Picture('0 0 0', strcat(me.src, "_d"), '1 1 0', '1 1 1', 1);
98                 else if(me.forcePressed || me.pressed || me.clickTime > 0)
99                         draw_Picture('0 0 0', strcat(me.src, "_c"), '1 1 0', me.color, 1);
100                 else if(me.focused)
101                         draw_Picture('0 0 0', strcat(me.src, "_f"), '1 1 0', me.color, 1);
102                 else
103                         draw_Picture('0 0 0', strcat(me.src, "_n"), '1 1 0', me.color, 1);
104         }
105         drawLabel(me);
106
107         if(me.clickTime > 0 && me.clickTime < frametime)
108         {
109                 // keyboard click timer expired? Fire the event then.
110                 if(!me.disabled)
111                         me.onClick(me, me.onClickEntity);
112         }
113         me.clickTime -= frametime;
114 }
115 #endif