]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/item/button.c
added checkbox, radio button; removed unnecessary setFocus override in Nexposee and...
[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, resizeNotify, void(entity, vector, vector, vector, vector))
6         METHOD(Button, keyDown, float(entity, float, float, float))
7         METHOD(Button, mousePress, float(entity, vector))
8         METHOD(Button, mouseDrag, float(entity, vector))
9         METHOD(Button, mouseRelease, float(entity, vector))
10         ATTRIB(Button, onClick, void(entity, entity), SUB_Null)
11         ATTRIB(Button, onClickEntity, entity, NULL)
12         ATTRIB(Button, src, string, "")
13         ATTRIB(Button, srcMulti, float, 1)
14         ATTRIB(Button, focusable, float, 1)
15         ATTRIB(Button, pressed, float, 0)
16         ATTRIB(Button, clickTime, float, 0)
17         ATTRIB(Button, forcePressed, float, 0)
18
19         ATTRIB(Button, origin, vector, '0 0 0')
20         ATTRIB(Button, size, vector, '0 0 0')
21 ENDCLASS(Button)
22 #endif
23
24 #ifdef IMPLEMENTATION
25 void resizeNotifyButton(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
26 {
27         resizeNotifyLabel(me, relOrigin, relSize, absOrigin, absSize);
28         me.origin = absOrigin;
29         me.size = absSize;
30 }
31 void configureButtonButton(entity me, string txt, float sz, string gfx)
32 {
33         configureLabelLabel(me, txt, sz, 0.5);
34         me.src = gfx;
35 }
36 float keyDownButton(entity me, float key, float ascii, float shift)
37 {
38         if(key == K_ENTER || key == K_SPACE)
39         {
40                 me.onClick(me, me.onClickEntity);
41                 me.clickTime = 0.1;
42                 return 1;
43         }
44         return 0;
45 }
46 float mouseDragButton(entity me, vector pos)
47 {
48         me.pressed = 1;
49         if(pos_x < 0) me.pressed = 0;
50         if(pos_y < 0) me.pressed = 0;
51         if(pos_x >= 1) me.pressed = 0;
52         if(pos_y >= 1) me.pressed = 0;
53         return 1;
54 }
55 float mousePressButton(entity me, vector pos)
56 {
57         me.mouseDrag(me, pos); // verify coordinates
58         return 1;
59 }
60 float mouseReleaseButton(entity me, vector pos)
61 {
62         me.mouseDrag(me, pos); // verify coordinates
63         if(me.pressed)
64         {
65                 me.onClick(me, me.onClickEntity);
66                 me.pressed = 0;
67         }
68         return 1;
69 }
70 void drawButton(entity me)
71 {
72         if(me.srcMulti)
73         {
74                 float division;
75                 division = min(0.5, 0.5 * me.size_y / me.size_x);
76                 if(me.forcePressed || me.pressed || me.clickTime > 0)
77                 {
78                         draw_Picture('0 0 0', strcat(me.src, "_cl"), eX * division + eY, '1 1 1', 1);
79                         if(division < 0.5)
80                                 draw_Picture('0 0 0' + eX * division, strcat(me.src, "_cm"), eX * (1 - 2 * division) + eY, '1 1 1', 1);
81                         draw_Picture(eX * (1 - division), strcat(me.src, "_cr"), eX * division + eY, '1 1 1', 1);
82                 }
83                 else if(me.focused)
84                 {
85                         draw_Picture('0 0 0', strcat(me.src, "_fl"), eX * division + eY, '1 1 1', 1);
86                         if(division < 0.5)
87                                 draw_Picture('0 0 0' + eX * division, strcat(me.src, "_fm"), eX * (1 - 2 * division) + eY, '1 1 1', 1);
88                         draw_Picture(eX * (1 - division), strcat(me.src, "_fr"), eX * division + eY, '1 1 1', 1);
89                 }
90                 else
91                 {
92                         draw_Picture('0 0 0', strcat(me.src, "_nl"), eX * division + eY, '1 1 1', 1);
93                         if(division < 0.5)
94                                 draw_Picture('0 0 0' + eX * division, strcat(me.src, "_nm"), eX * (1 - 2 * division) + eY, '1 1 1', 1);
95                         draw_Picture(eX * (1 - division), strcat(me.src, "_nr"), eX * division + eY, '1 1 1', 1);
96                 }
97         }
98         else
99         {
100                 if(me.forcePressed || me.pressed || me.clickTime > 0)
101                         draw_Picture('0 0 0', strcat(me.src, "_c"), '1 1 0', '1 1 1', 1);
102                 else if(me.focused)
103                         draw_Picture('0 0 0', strcat(me.src, "_f"), '1 1 0', '1 1 1', 1);
104                 else
105                         draw_Picture('0 0 0', strcat(me.src, "_n"), '1 1 0', '1 1 1', 1);
106         }
107         me.clickTime -= frametime;
108         drawLabel(me);
109 }
110 #endif