]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/item/inputcontainer.qc
b06e2556513f44d0cded72d0daeb5b0973e59388
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / item / inputcontainer.qc
1 #ifndef ITEM_INPUTCONTAINER_H
2 #define ITEM_INPUTCONTAINER_H
3 CLASS(InputContainer, Container)
4         METHOD(InputContainer, keyDown, float(entity, float, float, float))
5         METHOD(InputContainer, mouseMove, float(entity, vector))
6         METHOD(InputContainer, mousePress, float(entity, vector))
7         METHOD(InputContainer, mouseRelease, float(entity, vector))
8         METHOD(InputContainer, mouseDrag, float(entity, vector))
9         METHOD(InputContainer, focusLeave, void(entity))
10         METHOD(InputContainer, resizeNotify, void(entity, vector, vector, vector, vector))
11
12         METHOD(InputContainer, _changeFocusXY, float(entity, vector))
13         ATTRIB(InputContainer, mouseFocusedChild, entity, NULL)
14         ATTRIB(InputContainer, isTabRoot, float, 0)
15 ENDCLASS(InputContainer)
16 #endif
17
18 #ifdef IMPLEMENTATION
19 void InputContainer_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
20 {
21         SUPER(InputContainer).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
22         /*
23         if(me.parent.instanceOfInputContainer)
24                 me.isTabRoot = 0;
25         else
26                 me.isTabRoot = 1;
27         */
28 }
29
30 void InputContainer_focusLeave(entity me)
31 {
32         SUPER(InputContainer).focusLeave(me);
33         me.mouseFocusedChild = NULL;
34 }
35
36 float InputContainer_keyDown(entity me, float scan, float ascii, float shift)
37 {
38         entity f, ff;
39         if(SUPER(InputContainer).keyDown(me, scan, ascii, shift))
40                 return 1;
41         if(scan == K_ESCAPE)
42         {
43                 f = me.focusedChild;
44                 if(f)
45                 {
46                         me.setFocus(me, NULL);
47                         return 1;
48                 }
49                 return 0;
50         }
51         if(scan == K_TAB)
52         {
53                 f = me.focusedChild;
54                 if(shift & S_SHIFT)
55                 {
56                         if(f)
57                         {
58                                 for(ff = f.prevSibling; ff; ff = ff.prevSibling)
59                                 {
60                                         if (!ff.focusable)
61                                                 continue;
62                                         me.setFocus(me, ff);
63                                         return 1;
64                                 }
65                         }
66                         if(!f || me.isTabRoot)
67                         {
68                                 for(ff = me.lastChild; ff; ff = ff.prevSibling)
69                                 {
70                                         if (!ff.focusable)
71                                                 continue;
72                                         me.setFocus(me, ff);
73                                         return 1;
74                                 }
75                                 return 0; // AIIIIEEEEE!
76                         }
77                 }
78                 else
79                 {
80                         if(f)
81                         {
82                                 for(ff = f.nextSibling; ff; ff = ff.nextSibling)
83                                 {
84                                         if (!ff.focusable)
85                                                 continue;
86                                         me.setFocus(me, ff);
87                                         return 1;
88                                 }
89                         }
90                         if(!f || me.isTabRoot)
91                         {
92                                 for(ff = me.firstChild; ff; ff = ff.nextSibling)
93                                 {
94                                         if (!ff.focusable)
95                                                 continue;
96                                         me.setFocus(me, ff);
97                                         return 1;
98                                 }
99                                 return 0; // AIIIIEEEEE!
100                         }
101                 }
102         }
103         return 0;
104 }
105
106 float InputContainer__changeFocusXY(entity me, vector pos)
107 {
108         entity e, ne;
109         e = me.mouseFocusedChild;
110         ne = me.itemFromPoint(me, pos);
111         if(ne)
112                 if (!ne.focusable)
113                         ne = NULL;
114         me.mouseFocusedChild = ne;
115         if(ne)
116                 if(ne != e)
117                 {
118                         me.setFocus(me, ne);
119                         if(ne.instanceOfInputContainer)
120                         {
121                                 ne.focusedChild = NULL;
122                                 ne._changeFocusXY(e, globalToBox(pos, ne.Container_origin, ne.Container_size));
123                         }
124                 }
125         return (ne != NULL);
126 }
127
128 float InputContainer_mouseDrag(entity me, vector pos)
129 {
130         if(SUPER(InputContainer).mouseDrag(me, pos))
131                 return 1;
132         if(pos.x >= 0 && pos.y >= 0 && pos.x < 1 && pos.y < 1)
133                 return 1;
134         return 0;
135 }
136 float InputContainer_mouseMove(entity me, vector pos)
137 {
138         if(me.mouseFocusedChild != me.focusedChild) // if the keyboard moved the focus away
139                 me.mouseFocusedChild = NULL; // force focusing
140         if(me._changeFocusXY(me, pos))
141                 if(SUPER(InputContainer).mouseMove(me, pos))
142                         return 1;
143         if(pos.x >= 0 && pos.y >= 0 && pos.x < 1 && pos.y < 1)
144                 return 1;
145         return 0;
146 }
147 float InputContainer_mousePress(entity me, vector pos)
148 {
149         me.mouseFocusedChild = NULL; // force focusing
150         if(me._changeFocusXY(me, pos))
151                 if(SUPER(InputContainer).mousePress(me, pos))
152                         return 1;
153         if(pos.x >= 0 && pos.y >= 0 && pos.x < 1 && pos.y < 1)
154                 return 1;
155         return 0;
156 }
157 float InputContainer_mouseRelease(entity me, vector pos)
158 {
159         SUPER(InputContainer).mouseRelease(me, pos); // return value?
160         if(me.focused) // am I still eligible for this? (UGLY HACK, but a mouse event could have changed focus away)
161                 if(me._changeFocusXY(me, pos))
162                         return 1;
163         if(pos.x >= 0 && pos.y >= 0 && pos.x < 1 && pos.y < 1)
164                 return 1;
165         return 0;
166 }
167 #endif