no longer needs ui/mousepointer.lmp
[divverent/darkplaces.git] / ui.c
1
2 #include "quakedef.h"
3
4 cvar_t ui_showname = {0, "ui_showname", "0"};
5
6 #define ITEM_CLICKABLE 1
7 #define ITEM_DRAWABLE 2
8
9 #define UIKEY_LEFT 1
10 #define UIKEY_RIGHT 2
11 #define UIKEY_UP 3
12 #define UIKEY_DOWN 4
13 #define UIKEY_ENTER 5
14
15 #define UI_MOUSEBUTTONS 3
16
17 static int                      ui_alive, ui_active;
18 static float            ui_mouse_x, ui_mouse_y;
19 static int                      ui_mousebutton[UI_MOUSEBUTTONS], ui_mouseclick;
20 static int                      ui_keyui, ui_keyitem;
21 static ui_item_t        *ui_keyrealitem;
22
23 static ui_t *ui_list[MAX_UI_COUNT];
24
25 //static qpic_t *ui_mousepointer;
26 static rtexture_t *ui_mousepointertexture;
27
28 static byte pointerimage[256] =
29 {
30         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
31         0x02, 0x06, 0x07, 0x07, 0x07, 0x07, 0x06, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
32         0x02, 0x06, 0x05, 0x05, 0x05, 0x04, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
33         0x02, 0x06, 0x05, 0x05, 0x04, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
34         0x02, 0x06, 0x05, 0x04, 0x05, 0x06, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
35         0x02, 0x06, 0x04, 0x01, 0x04, 0x05, 0x06, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
36         0x02, 0x05, 0x01, 0xFF, 0x01, 0x04, 0x05, 0x06, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
37         0x02, 0x01, 0xFF, 0xFF, 0xFF, 0x01, 0x04, 0x05, 0x06, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
38         0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x04, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
39         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
40         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
41         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
42         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
43         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
44         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
45         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 
46 };
47
48 void ui_start(void)
49 {
50 //      ui_mousepointer = Draw_CachePic("ui/mousepointer.lmp");
51         ui_mousepointertexture = R_LoadTexture("mousepointer", 16, 16, pointerimage, TEXF_ALPHA | TEXF_PRECACHE);
52         ui_mouse_x = vid.width * 0.5;
53         ui_mouse_y = vid.height * 0.5;
54         ui_alive = true;
55 }
56
57 void ui_shutdown(void)
58 {
59 //      ui_mousepointer = NULL;
60         ui_mousepointertexture = NULL;
61         ui_alive = false;
62 }
63
64 void ui_newmap(void)
65 {
66 }
67
68 void ui_init(void)
69 {
70         Cvar_RegisterVariable(&ui_showname);
71         R_RegisterModule("UI", ui_start, ui_shutdown, ui_newmap);
72 }
73
74 void ui_mouseupdate(float x, float y)
75 {
76         if (ui_alive)
77         {
78                 ui_mouse_x = bound(0, x, vid.width);
79                 ui_mouse_y = bound(0, y, vid.height);
80         }
81 }
82
83 void ui_mouseupdaterelative(float x, float y)
84 {
85         if (ui_alive)
86         {
87                 ui_mouse_x += x;
88                 ui_mouse_y += y;
89                 ui_mouse_x = bound(0, ui_mouse_x, vid.width);
90                 ui_mouse_y = bound(0, ui_mouse_y, vid.height);
91         }
92 }
93
94 ui_t *ui_create(void)
95 {
96         ui_t *ui;
97         ui = qmalloc(sizeof(*ui));
98         if (ui == NULL)
99                 Sys_Error("ui_create: unable to allocate memory for new ui\n");
100         memset(ui, 0, sizeof(*ui));
101         return ui;
102 }
103
104 void ui_free(ui_t *ui)
105 {
106         if (ui)
107                 qfree(ui);
108 }
109
110 void ui_clear(ui_t *ui)
111 {
112         ui->item_count = 0;
113 }
114
115 void ui_item
116 (
117         ui_t *ui, char *basename, int number,
118         float x, float y, qpic_t *pic,
119         float left, float top, float width, float height,
120         void(*leftkey)(void *nativedata1, void *nativedata2, float data1, float data2),
121         void(*rightkey)(void *nativedata1, void *nativedata2, float data1, float data2),
122         void(*enterkey)(void *nativedata1, void *nativedata2, float data1, float data2),
123         void(*mouseclick)(void *nativedata1, void *nativedata2, float data1, float data2, float xfrac, float yfrac),
124         void *nativedata1, void *nativedata2, float data1, float data2
125 )
126 {
127         int i;
128         ui_item_t *it;
129         char itemname[32];
130         snprintf(itemname, sizeof(itemname), "%s%04d", basename, number);
131         for (it = ui->items, i = 0;i < ui->item_count;it++, i++)
132                 if (it->name == NULL || !strncmp(itemname, it->name, 32))
133                         break;
134         if (i == ui->item_count)
135         {
136                 if (i == MAX_UI_ITEMS)
137                 {
138                         Con_Printf("ui_item: ran out of UI item slots\n");
139                         return;
140                 }
141                 ui->item_count++;
142         }
143         strncpy(it->name, itemname, 32);
144         it->flags = 0;
145         if (leftkey || rightkey || enterkey || mouseclick)
146                 it->flags |= ITEM_CLICKABLE;
147         if (pic)
148                 it->flags |= ITEM_DRAWABLE;
149         it->draw_x = x;
150         it->draw_y = y;
151         it->click_x = x + left;
152         it->click_y = y + top;
153         it->click_x2 = it->click_x + width;
154         it->click_y2 = it->click_y + height;
155         it->leftkey = leftkey;
156         it->rightkey = rightkey;
157         it->enterkey = enterkey;
158         it->mouseclick = mouseclick;
159         if (it->mouseclick == NULL)
160                 it->mouseclick = (void *)it->enterkey;
161         if (it->leftkey == NULL)
162                 it->leftkey = it->enterkey;
163         if (it->rightkey == NULL)
164                 it->rightkey = it->enterkey;
165         it->nativedata1 = nativedata1;
166         it->nativedata2 = nativedata2;
167 }
168
169 void ui_item_remove(ui_t *ui, char *basename, int number)
170 {
171         int i;
172         ui_item_t *it;
173         char itemname[32];
174         snprintf(itemname, sizeof(itemname), "%s%04d", basename, number);
175         for (it = ui->items, i = 0;i < ui->item_count;it++, i++)
176                 if (it->name && !strncmp(itemname, it->name, 32))
177                         break;
178         if (i < ui->item_count)
179                 it->name[0] = 0;
180 }
181
182 ui_item_t *ui_hititem(float x, float y)
183 {
184         int i, j;
185         ui_item_t *it;
186         ui_t *ui;
187         for (j = 0;j < MAX_UI_COUNT;j++)
188                 if ((ui = ui_list[j]))
189                         for (it = ui->items, i = 0;i < ui->item_count;it++, i++)
190                                 if (it->name[0] && (it->flags & ITEM_CLICKABLE))
191                                         if (x >= it->click_x && y >= it->click_y && x < it->click_x2 && y < it->click_y2)
192                                                 return it;
193         return NULL;
194 }
195
196 int ui_uiactive(ui_t *ui)
197 {
198         int i;
199         for (i = 0;i < MAX_UI_COUNT;i++)
200                 if (ui_list[i] == ui)
201                         return true;
202         return false;
203 }
204
205 void ui_activate(ui_t *ui, int yes)
206 {
207         int i;
208         if (yes)
209         {
210                 if (ui_uiactive(ui))
211                         return;
212
213                 for (i = 0;i < MAX_UI_COUNT;i++)
214                 {
215                         if (ui_list[i] == NULL)
216                         {
217                                 ui_list[i] = ui;
218                                 return;
219                         }
220                 }
221
222                 Con_Printf("ui_activate: ran out of active ui list items\n");
223         }
224         else
225         {
226                 for (i = 0;i < MAX_UI_COUNT;i++)
227                 {
228                         if (ui_list[i] == ui)
229                         {
230                                 ui_list[i] = NULL;
231                                 return;
232                         }
233                 }
234         }
235 }
236
237 int ui_isactive(void)
238 {
239         int j;
240         ui_t *ui;
241         if (ui_alive)
242         {
243                 for (j = 0;j < MAX_UI_COUNT;j++)
244                         if ((ui = ui_list[j]))
245                                 if (ui->item_count)
246                                         return true;
247         }
248         return false;
249 }
250
251 #define UI_QUEUE_SIZE 256
252 static byte ui_keyqueue[UI_QUEUE_SIZE];
253 static int ui_keyqueuepos = 0;
254
255 void ui_leftkeyupdate(int pressed)
256 {
257         static int key = false;
258         if (pressed && !key && ui_keyqueuepos < UI_QUEUE_SIZE)
259                 ui_keyqueue[ui_keyqueuepos++] = UIKEY_LEFT;
260         key = pressed;
261 }
262
263 void ui_rightkeyupdate(int pressed)
264 {
265         static int key = false;
266         if (pressed && !key && ui_keyqueuepos < UI_QUEUE_SIZE)
267                 ui_keyqueue[ui_keyqueuepos++] = UIKEY_RIGHT;
268         key = pressed;
269 }
270
271 void ui_upkeyupdate(int pressed)
272 {
273         static int key = false;
274         if (pressed && !key && ui_keyqueuepos < UI_QUEUE_SIZE)
275                 ui_keyqueue[ui_keyqueuepos++] = UIKEY_UP;
276         key = pressed;
277 }
278
279 void ui_downkeyupdate(int pressed)
280 {
281         static int key = false;
282         if (pressed && !key && ui_keyqueuepos < UI_QUEUE_SIZE)
283                 ui_keyqueue[ui_keyqueuepos++] = UIKEY_DOWN;
284         key = pressed;
285 }
286
287 void ui_mousebuttonupdate(int button, int pressed)
288 {
289         if (button < 0 || button >= UI_MOUSEBUTTONS)
290                 return;
291         if (button == 0 && ui_mousebutton[button] && !pressed)
292                 ui_mouseclick = true;
293         ui_mousebutton[button] = pressed;
294 }
295
296 void ui_update(void)
297 {
298         ui_item_t *startitem, *it;
299         if (ui_alive)
300         {
301                 ui_mouse_x = bound(0, ui_mouse_x, vid.width);
302                 ui_mouse_y = bound(0, ui_mouse_y, vid.height);
303
304                 if ((ui_active = ui_isactive()))
305                 {
306                         // validate currently selected item
307                         if(ui_list[ui_keyui] == NULL)
308                         {
309                                 while (ui_list[ui_keyui] == NULL)
310                                         ui_keyui = (ui_keyui + 1) % MAX_UI_COUNT;
311                                 ui_keyitem = 0;
312                         }
313                         ui_keyitem = bound(0, ui_keyitem, ui_list[ui_keyui]->item_count - 1);
314                         startitem = ui_keyrealitem = &ui_list[ui_keyui]->items[ui_keyitem];
315                         if ((ui_keyrealitem->flags & ITEM_CLICKABLE) == 0)
316                         {
317                                 do
318                                 {
319                                         // FIXME: cycle through UIs as well as items in a UI
320                                         ui_keyitem = (ui_keyitem - 1) % ui_list[ui_keyui]->item_count - 1;
321                                         ui_keyrealitem = &ui_list[ui_keyui]->items[ui_keyitem];
322                                 }
323                                 while (ui_keyrealitem != startitem && (ui_keyrealitem->flags & ITEM_CLICKABLE) == 0);
324                         }
325
326                         if (ui_keyqueuepos)
327                         {
328                                 int i;
329                                 for (i = 0;i < ui_keyqueuepos;i++)
330                                 {
331                                         startitem = ui_keyrealitem;
332                                         switch(ui_keyqueue[i])
333                                         {
334                                         case UIKEY_UP:
335                                                 do
336                                                 {
337                                                         ui_keyitem--;
338                                                         if (ui_keyitem < 0)
339                                                         {
340                                                                 do
341                                                                         ui_keyui = (ui_keyui - 1) % MAX_UI_COUNT;
342                                                                 while(ui_list[ui_keyui] == NULL);
343                                                                 ui_keyitem = ui_list[ui_keyui]->item_count - 1;
344                                                         }
345                                                         ui_keyrealitem = &ui_list[ui_keyui]->items[ui_keyitem];
346                                                 }
347                                                 while (ui_keyrealitem != startitem && (ui_keyrealitem->flags & ITEM_CLICKABLE) == 0);
348                                                 break;
349                                         case UIKEY_DOWN:
350                                                 do
351                                                 {
352                                                         ui_keyitem++;
353                                                         if (ui_keyitem >= ui_list[ui_keyui]->item_count)
354                                                         {
355                                                                 do
356                                                                         ui_keyui = (ui_keyui + 1) % MAX_UI_COUNT;
357                                                                 while(ui_list[ui_keyui] == NULL);
358                                                                 ui_keyitem = 0;
359                                                         }
360                                                         ui_keyrealitem = &ui_list[ui_keyui]->items[ui_keyitem];
361                                                 }
362                                                 while (ui_keyrealitem != startitem && (ui_keyrealitem->flags & ITEM_CLICKABLE) == 0);
363                                                 break;
364                                         case UIKEY_LEFT:
365                                                 if (ui_keyrealitem->leftkey)
366                                                         ui_keyrealitem->leftkey(ui_keyrealitem->nativedata1, ui_keyrealitem->nativedata2, ui_keyrealitem->data1, ui_keyrealitem->data2);
367                                                 break;
368                                         case UIKEY_RIGHT:
369                                                 if (ui_keyrealitem->rightkey)
370                                                         ui_keyrealitem->rightkey(ui_keyrealitem->nativedata1, ui_keyrealitem->nativedata2, ui_keyrealitem->data1, ui_keyrealitem->data2);
371                                                 break;
372                                         case UIKEY_ENTER:
373                                                 if (ui_keyrealitem->enterkey)
374                                                         ui_keyrealitem->enterkey(ui_keyrealitem->nativedata1, ui_keyrealitem->nativedata2, ui_keyrealitem->data1, ui_keyrealitem->data2);
375                                                 break;
376                                         }
377                                 }
378                         }
379                         ui_keyqueuepos = 0;
380
381                         if (ui_mouseclick && (it = ui_hititem(ui_mouse_x, ui_mouse_y)) && it->mouseclick)
382                                 it->mouseclick(it->nativedata1, it->nativedata2, it->data1, it->data2, ui_mouse_x - it->click_x, ui_mouse_y - it->click_y);
383         }
384         }
385         ui_mouseclick = false;
386 }
387
388 void ui_draw(void)
389 {
390         int i, j;
391         ui_item_t *it;
392         ui_t *ui;
393         if (ui_alive && ui_active)
394         {
395                 for (j = 0;j < MAX_UI_COUNT;j++)
396                         if ((ui = ui_list[j]))
397                                 if (ui->item_count)
398                                         for (i = 0, it = ui->items;i < ui->item_count;i++, it++)
399                                                 if (it->flags & ITEM_DRAWABLE)
400                                                         Draw_Pic(it->draw_x, it->draw_y, it->draw_pic);
401
402                 if ((it = ui_hititem(ui_mouse_x, ui_mouse_y)))
403                 {
404                         Draw_AdditivePic(it->draw_x, it->draw_y, it->draw_pic);
405                         if (ui_showname.value)
406                                 Draw_String(ui_mouse_x, ui_mouse_y + 16, it->name, 9999);
407         }
408
409                 it = ui_keyrealitem;
410                 Draw_AdditivePic(it->draw_x, it->draw_y, it->draw_pic);
411
412 //              Draw_Pic(ui_mouse_x, ui_mouse_y, ui_mousepointer);
413                 Draw_GenericPic(ui_mousepointertexture, 1, 1, 1, 1, ui_mouse_x, ui_mouse_y, 16, 16);
414         }
415 }