]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/charmap.c
let's USE the super keyword! yay!
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / charmap.c
1 #ifdef INTERFACE
2 CLASS(XonoticCharmap) EXTENDS(Image)
3         METHOD(XonoticCharmap, configureXonoticCharmap, void(entity, entity))
4         METHOD(XonoticCharmap, mousePress, float(entity, vector))
5         METHOD(XonoticCharmap, mouseRelease, float(entity, vector))
6         METHOD(XonoticCharmap, mouseMove, float(entity, vector))
7         METHOD(XonoticCharmap, mouseDrag, float(entity, vector))
8         METHOD(XonoticCharmap, keyDown, float(entity, float, float, float))
9         METHOD(XonoticCharmap, focusLeave, void(entity))
10         METHOD(XonoticCharmap, draw, void(entity))
11         ATTRIB(XonoticCharmap, controlledTextbox, entity, NULL)
12         ATTRIB(XonoticCharmap, image, string, SKINGFX_CHARMAP)
13         ATTRIB(XonoticCharmap, image2, string, SKINGFX_CHARMAP_SELECTED)
14         ATTRIB(XonoticCharmap, focusable, float, 1)
15         ATTRIB(XonoticCharmap, previouslySelectedCharacterCell, float, -1)
16         ATTRIB(XonoticCharmap, selectedCharacterCell, float, 0)
17         ATTRIB(XonoticCharmap, mouseSelectedCharacterCell, float, -1)
18 ENDCLASS(XonoticCharmap)
19 entity makeXonoticCharmap(entity theTextbox);
20 #endif
21
22 #ifdef IMPLEMENTATION
23 entity makeXonoticCharmap(entity theTextbox)
24 {
25         entity me;
26         me = spawnXonoticCharmap();
27         me.configureXonoticCharmap(me, theTextbox);
28         return me;
29 }
30
31 string CharMap_CellToChar(float c)
32 {
33         if (cvar("utf8_enable")) {
34                 if(c == 13)
35                         return chr(0xE000 + 127);
36                 else if(c < 32)
37                         return chr(0xE000 + c);
38                 else
39                         return chr(0xE000 + c + 96);
40         } else {
41                 if(c == 13)
42                         return chr(127);
43                 else if(c < 32)
44                         return chr(c);
45                 else
46                         return chr(c + 96);
47         }
48 }
49
50 void configureXonoticCharmapXonoticCharmap(entity me, entity theTextbox)
51 {
52         me.controlledTextbox = theTextbox;
53         me.configureImage(me, me.image);
54 }
55
56 float mouseMoveXonoticCharmap(entity me, vector coords)
57 {
58         float x, y, c;
59         x = floor(coords_x * 16);
60         y = floor(coords_y * 10);
61         if(x < 0 || y < 0 || x >= 16 || y >= 10)
62         {
63                 me.mouseSelectedCharacterCell = -1;
64                 return 0;
65         }
66         c = y * 16 + x;
67         if(c != me.mouseSelectedCharacterCell)
68                 me.mouseSelectedCharacterCell = me.selectedCharacterCell = c;
69         return 1;
70 }
71 float mouseDragXonoticCharmap(entity me, vector coords)
72 {
73         return me.mouseMove(me, coords);
74 }
75 float mousePressXonoticCharmap(entity me, vector coords)
76 {
77         me.mouseMove(me, coords);
78         if(me.mouseSelectedCharacterCell >= 0)
79         {
80                 me.pressed = 1;
81                 me.previouslySelectedCharacterCell = me.selectedCharacterCell;
82         }
83         return 1;
84 }
85 float mouseReleaseXonoticCharmap(entity me, vector coords)
86 {
87         if(!me.pressed)
88                 return 0;
89         me.mouseMove(me, coords);
90         if(me.selectedCharacterCell == me.previouslySelectedCharacterCell)
91                 me.controlledTextbox.enterText(me.controlledTextbox, CharMap_CellToChar(me.selectedCharacterCell));
92         me.pressed = 0;
93         return 1;
94 }
95 float keyDownXonoticCharmap(entity me, float key, float ascii, float shift)
96 {
97         switch(key)
98         {
99                 case K_LEFTARROW:
100                         me.selectedCharacterCell = mod(me.selectedCharacterCell + 159, 160);
101                         return 1;
102                 case K_RIGHTARROW:
103                         me.selectedCharacterCell = mod(me.selectedCharacterCell + 1, 160);
104                         return 1;
105                 case K_UPARROW:
106                         me.selectedCharacterCell = mod(me.selectedCharacterCell + 144, 160);
107                         return 1;
108                 case K_DOWNARROW:
109                         me.selectedCharacterCell = mod(me.selectedCharacterCell + 16, 160);
110                         return 1;
111                 case K_HOME:
112                         me.selectedCharacterCell = 0;
113                         return 1;
114                 case K_END:
115                         me.selectedCharacterCell = 159;
116                         return 1;
117                 case K_SPACE:
118                 case K_ENTER:
119                 case K_INS:
120                         me.controlledTextbox.enterText(me.controlledTextbox, CharMap_CellToChar(me.selectedCharacterCell));
121                         return 1;
122                 default:
123                         return me.controlledTextbox.keyDown(me.controlledTextbox, key, ascii, shift);
124         }
125 }
126 void focusLeaveXonoticCharmap(entity me)
127 {
128         me.controlledTextbox.saveCvars(me.controlledTextbox);
129 }
130 void drawXonoticCharmap(entity me)
131 {
132         if(me.focused)
133         {
134                 if(!me.pressed || (me.selectedCharacterCell == me.previouslySelectedCharacterCell))
135                 {
136                         vector c;
137                         c = eX * (mod(me.selectedCharacterCell, 16) / 16.0);
138                         c += eY * (floor(me.selectedCharacterCell / 16.0) / 10.0);
139                         draw_Picture(c, me.image2, '0.0625 0.1 0', '1 1 1', 1);
140                 }
141         }
142         SUPER(XonoticCharmap).draw(me);
143 }
144 #endif