]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/item/button.c
adding qcsrc/menu-div0test with a test menu.dat source... may or may not replace...
[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, focusable, float, 1)
14         ATTRIB(Button, pressed, float, 0)
15         ATTRIB(Button, clickTime, float, 0)
16         ATTRIB(Button, forcePressed, float, 0)
17
18         ATTRIB(Button, origin, vector, '0 0 0')
19         ATTRIB(Button, size, vector, '0 0 0')
20 ENDCLASS(Button)
21 #endif
22
23 #ifdef IMPLEMENTATION
24 void resizeNotifyButton(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
25 {
26         resizeNotifyLabel(me, relOrigin, relSize, absOrigin, absSize);
27         me.origin = absOrigin;
28         me.size = absSize;
29 }
30 void configureButtonButton(entity me, string txt, float sz, string gfx)
31 {
32         configureLabelLabel(me, txt, sz, 0.5);
33         me.src = gfx;
34 }
35 float keyDownButton(entity me, float key, float ascii, float shift)
36 {
37         if(key == K_ENTER || key == K_SPACE)
38         {
39                 me.onClick(me, me.onClickEntity);
40                 me.clickTime = 0.1;
41                 return 1;
42         }
43         return 0;
44 }
45 float mouseDragButton(entity me, vector pos)
46 {
47         me.pressed = 1;
48         if(pos_x < 0) me.pressed = 0;
49         if(pos_y < 0) me.pressed = 0;
50         if(pos_x >= 1) me.pressed = 0;
51         if(pos_y >= 1) me.pressed = 0;
52         return 1;
53 }
54 float mousePressButton(entity me, vector pos)
55 {
56         me.mouseDrag(me, pos); // verify coordinates
57         return 1;
58 }
59 float mouseReleaseButton(entity me, vector pos)
60 {
61         me.mouseDrag(me, pos); // verify coordinates
62         if(me.pressed)
63         {
64                 me.onClick(me, me.onClickEntity);
65                 me.pressed = 0;
66         }
67         return 1;
68 }
69 void drawButton(entity me)
70 {
71         float division;
72         division = min(0.5, 0.5 * me.size_y / me.size_x);
73         if(me.forcePressed || me.pressed || me.clickTime > 0)
74         {
75                 draw_Picture('0 0 0', strcat(me.src, "_cl"), eX * division + eY, '1 1 1', 1);
76                 if(division < 0.5)
77                         draw_Picture('0 0 0' + eX * division, strcat(me.src, "_cm"), eX * (1 - 2 * division) + eY, '1 1 1', 1);
78                 draw_Picture(eX * (1 - division), strcat(me.src, "_cr"), eX * division + eY, '1 1 1', 1);
79         }
80         else if(me.focused)
81         {
82                 draw_Picture('0 0 0', strcat(me.src, "_fl"), eX * division + eY, '1 1 1', 1);
83                 if(division < 0.5)
84                         draw_Picture('0 0 0' + eX * division, strcat(me.src, "_fm"), eX * (1 - 2 * division) + eY, '1 1 1', 1);
85                 draw_Picture(eX * (1 - division), strcat(me.src, "_fr"), eX * division + eY, '1 1 1', 1);
86         }
87         else
88         {
89                 draw_Picture('0 0 0', strcat(me.src, "_nl"), eX * division + eY, '1 1 1', 1);
90                 if(division < 0.5)
91                         draw_Picture('0 0 0' + eX * division, strcat(me.src, "_nm"), eX * (1 - 2 * division) + eY, '1 1 1', 1);
92                 draw_Picture(eX * (1 - division), strcat(me.src, "_nr"), eX * division + eY, '1 1 1', 1);
93         }
94         me.clickTime -= frametime;
95         drawLabel(me);
96 }
97 #endif