2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
25 /*---------------------------------------------------------------------------*/
29 #define GUI_TYPE 0xFFFE
62 const GLfloat *color0;
63 const GLfloat *color1;
68 /*---------------------------------------------------------------------------*/
70 const GLfloat gui_wht[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
71 const GLfloat gui_yel[4] = { 1.0f, 1.0f, 0.0f, 1.0f };
72 const GLfloat gui_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
73 const GLfloat gui_grn[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
74 const GLfloat gui_blu[4] = { 0.0f, 0.0f, 1.0f, 1.0f };
75 const GLfloat gui_blk[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
76 const GLfloat gui_gry[4] = { 0.3f, 0.3f, 0.3f, 1.0f };
78 /*---------------------------------------------------------------------------*/
80 static struct widget widget[MAXWIDGET];
83 static TTF_Font *font[3] = { NULL, NULL, NULL };
84 static int scale[3] = { 1, 1, 1 };
86 static GLuint digit_text[3][11];
87 static GLuint digit_list[3][11];
88 static int digit_w[3][11];
89 static int digit_h[3][11];
93 /*---------------------------------------------------------------------------*/
95 static int gui_hot(int id)
97 return (widget[id].type & GUI_STATE);
100 /*---------------------------------------------------------------------------*/
102 * Initialize a display list containing a rectangle (x, y, w, h) to
103 * which a rendered-font texture may be applied. Colors c0 and c1
104 * determine the top-to-bottom color gradient of the text.
107 static GLuint gui_list(int x, int y,
108 int w, int h, const float *c0, const float *c1)
110 GLuint list = glGenLists(1);
115 int W, H, d = h / 16;
117 /* Assume the applied texture size is rect size rounded to power-of-two. */
119 image_size(&W, &H, w, h);
121 s0 = 0.5f * (W - w) / W;
122 t0 = 0.5f * (H - h) / H;
126 glNewList(list, GL_COMPILE);
130 glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
131 glTexCoord2f(s0, t1); glVertex2i(x + d, y - d);
132 glTexCoord2f(s1, t1); glVertex2i(x + w + d, y - d);
133 glTexCoord2f(s1, t0); glVertex2i(x + w + d, y + h - d);
134 glTexCoord2f(s0, t0); glVertex2i(x + d, y + h - d);
137 glTexCoord2f(s0, t1); glVertex2i(x, y);
138 glTexCoord2f(s1, t1); glVertex2i(x + w, y);
141 glTexCoord2f(s1, t0); glVertex2i(x + w, y + h);
142 glTexCoord2f(s0, t0); glVertex2i(x, y + h);
152 * Initialize a display list containing a rounded-corner rectangle (x,
153 * y, w, h). Generate texture coordinates to properly apply a texture
154 * map to the rectangle as though the corners were not rounded.
157 static GLuint gui_rect(int x, int y, int w, int h, int f, int r)
159 GLuint list = glGenLists(1);
164 glNewList(list, GL_COMPILE);
166 glBegin(GL_QUAD_STRIP);
170 for (i = 0; i <= n; i++)
172 float a = 0.5f * V_PI * (float) i / (float) n;
173 float s = r * fsinf(a);
174 float c = r * fcosf(a);
177 float Ya = y + h + ((f & GUI_NW) ? (s - r) : 0);
178 float Yb = y + ((f & GUI_SW) ? (r - s) : 0);
180 glTexCoord2f((X - x) / w, 1 - (Ya - y) / h);
183 glTexCoord2f((X - x) / w, 1 - (Yb - y) / h);
187 /* ... Right side. */
189 for (i = 0; i <= n; i++)
191 float a = 0.5f * V_PI * (float) i / (float) n;
192 float s = r * fsinf(a);
193 float c = r * fcosf(a);
195 float X = x + w - r + s;
196 float Ya = y + h + ((f & GUI_NE) ? (c - r) : 0);
197 float Yb = y + ((f & GUI_SE) ? (r - c) : 0);
199 glTexCoord2f((X - x) / w, 1 - (Ya - y) / h);
202 glTexCoord2f((X - x) / w, 1 - (Yb - y) / h);
213 /*---------------------------------------------------------------------------*/
217 const float *c0 = gui_yel;
218 const float *c1 = gui_red;
220 int w = config_get_d(CONFIG_WIDTH);
221 int h = config_get_d(CONFIG_HEIGHT);
222 int i, j, s = (h < w) ? h : w;
224 /* Initialize font rendering. */
233 /* Make sure text size doesn't exceed the maximum texture size. */
235 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m);
241 while (s0 > m) { s0 /= 2; scale[0] *= 2; }
242 while (s1 > m) { s1 /= 2; scale[1] *= 2; }
243 while (s2 > m) { s2 /= 2; scale[2] *= 2; }
245 memset(widget, 0, sizeof (struct widget) * MAXWIDGET);
247 /* Load small, medium, and large typefaces. */
249 font[GUI_SML] = TTF_OpenFont(config_data(GUI_FACE), s0);
250 font[GUI_MED] = TTF_OpenFont(config_data(GUI_FACE), s1);
251 font[GUI_LRG] = TTF_OpenFont(config_data(GUI_FACE), s2);
254 /* Initialize the global pause GUI. */
256 if ((pause_id = gui_pause(0)))
257 gui_layout(pause_id, 0, 0);
259 /* Initialize digit glyphs and lists for counters and clocks. */
261 for (i = 0; i < 3; i++)
265 /* Draw digits 0 through 9. */
267 for (j = 0; j < 10; j++)
269 text[0] = '0' + (char) j;
272 digit_text[i][j] = make_image_from_font(NULL, NULL,
275 text, font[i], scale[i]);
276 digit_list[i][j] = gui_list(-digit_w[i][j] / 2,
279 +digit_h[i][j], c0, c1);
282 /* Draw the colon for the clock. */
284 digit_text[i][j] = make_image_from_font(NULL, NULL,
287 ":", font[i], scale[i]);
288 digit_list[i][j] = gui_list(-digit_w[i][10] / 2,
291 +digit_h[i][10], c0, c1);
302 /* Release any remaining widget texture and display list indices. */
304 for (id = 1; id < MAXWIDGET; id++)
306 if (glIsTexture(widget[id].text_img))
307 glDeleteTextures(1, &widget[id].text_img);
309 if (glIsList(widget[id].text_obj))
310 glDeleteLists(widget[id].text_obj, 1);
311 if (glIsList(widget[id].rect_obj))
312 glDeleteLists(widget[id].rect_obj, 1);
314 widget[id].type = GUI_FREE;
315 widget[id].text_img = 0;
316 widget[id].text_obj = 0;
317 widget[id].rect_obj = 0;
322 /* Release all digit textures and display lists. */
324 for (i = 0; i < 3; i++)
325 for (j = 0; j < 11; j++)
327 if (glIsTexture(digit_text[i][j]))
328 glDeleteTextures(1, &digit_text[i][j]);
330 if (glIsList(digit_list[i][j]))
331 glDeleteLists(digit_list[i][j], 1);
334 /* Release all loaded fonts and finalize font rendering. */
336 if (font[GUI_LRG]) TTF_CloseFont(font[GUI_LRG]);
337 if (font[GUI_MED]) TTF_CloseFont(font[GUI_MED]);
338 if (font[GUI_SML]) TTF_CloseFont(font[GUI_SML]);
343 /*---------------------------------------------------------------------------*/
345 static int gui_widget(int pd, int type)
349 /* Find an unused entry in the widget table. */
351 for (id = 1; id < MAXWIDGET; id++)
352 if (widget[id].type == GUI_FREE)
354 /* Set the type and default properties. */
356 widget[id].type = type;
357 widget[id].token = 0;
358 widget[id].value = 0;
360 widget[id].rect = GUI_NW | GUI_SW | GUI_NE | GUI_SE;
363 widget[id].text_img = 0;
364 widget[id].text_obj = 0;
365 widget[id].rect_obj = 0;
366 widget[id].color0 = gui_wht;
367 widget[id].color1 = gui_wht;
368 widget[id].scale = 1.0f;
370 /* Insert the new widget into the parent's widget list. */
375 widget[id].cdr = widget[pd].car;
387 fprintf(stderr, _("Out of widget IDs\n"));
392 int gui_harray(int pd) { return gui_widget(pd, GUI_HARRAY); }
393 int gui_varray(int pd) { return gui_widget(pd, GUI_VARRAY); }
394 int gui_hstack(int pd) { return gui_widget(pd, GUI_HSTACK); }
395 int gui_vstack(int pd) { return gui_widget(pd, GUI_VSTACK); }
396 int gui_filler(int pd) { return gui_widget(pd, GUI_FILLER); }
398 /*---------------------------------------------------------------------------*/
400 void gui_set_image(int id, const char *file)
402 if (glIsTexture(widget[id].text_img))
403 glDeleteTextures(1, &widget[id].text_img);
405 widget[id].text_img = make_image_from_file(NULL, NULL, NULL, NULL, file);
408 void gui_set_label(int id, const char *text)
412 if (glIsTexture(widget[id].text_img))
413 glDeleteTextures(1, &widget[id].text_img);
414 if (glIsList(widget[id].text_obj))
415 glDeleteLists(widget[id].text_obj, 1);
417 widget[id].text_img = make_image_from_font(NULL, NULL, &w, &h,
418 text, font[widget[id].size],
419 scale[widget[id].size]);
420 widget[id].text_obj = gui_list(-w / 2, -h / 2, w, h,
421 widget[id].color0, widget[id].color1);
424 void gui_set_count(int id, int value)
426 widget[id].value = value;
429 void gui_set_clock(int id, int value)
431 widget[id].value = value;
434 void gui_set_color(int id, const float *c0,
437 widget[id].color0 = c0 ? c0 : gui_yel;
438 widget[id].color1 = c1 ? c1 : gui_red;
441 void gui_set_multi(int id, const char *text)
450 /* Copy each delimited string to a line buffer. */
452 for (p = text, j = 0; *p && j < 8; j++)
454 strncpy(s[j], p, (n = strcspn(p, "\\")));
457 if (*(p += n) == '\\') p++;
460 /* Set the label value for each line. */
462 for (i = j - 1, jd = widget[id].car; i >= 0 && jd; i--, jd = widget[jd].cdr)
463 gui_set_label(jd, s[i]);
466 /*---------------------------------------------------------------------------*/
468 int gui_image(int pd, const char *file, int w, int h)
472 if ((id = gui_widget(pd, GUI_IMAGE)))
474 widget[id].text_img = make_image_from_file(NULL, NULL,
482 int gui_start(int pd, const char *text, int size, int token, int value)
486 if ((id = gui_state(pd, text, size, token, value)))
492 int gui_state(int pd, const char *text, int size, int token, int value)
496 if ((id = gui_widget(pd, GUI_STATE)))
498 widget[id].text_img = make_image_from_font(NULL, NULL,
503 widget[id].size = size;
504 widget[id].token = token;
505 widget[id].value = value;
510 int gui_label(int pd, const char *text, int size, int rect, const float *c0,
515 if ((id = gui_widget(pd, GUI_LABEL)))
517 widget[id].text_img = make_image_from_font(NULL, NULL,
522 widget[id].size = size;
523 widget[id].color0 = c0 ? c0 : gui_yel;
524 widget[id].color1 = c1 ? c1 : gui_red;
525 widget[id].rect = rect;
530 int gui_count(int pd, int value, int size, int rect)
534 if ((id = gui_widget(pd, GUI_COUNT)))
536 for (i = value; i; i /= 10)
537 widget[id].w += digit_w[size][0];
539 widget[id].h = digit_h[size][0];
540 widget[id].value = value;
541 widget[id].size = size;
542 widget[id].color0 = gui_yel;
543 widget[id].color1 = gui_red;
544 widget[id].rect = rect;
549 int gui_clock(int pd, int value, int size, int rect)
553 if ((id = gui_widget(pd, GUI_CLOCK)))
555 widget[id].w = digit_w[size][0] * 6;
556 widget[id].h = digit_h[size][0];
557 widget[id].value = value;
558 widget[id].size = size;
559 widget[id].color0 = gui_yel;
560 widget[id].color1 = gui_red;
561 widget[id].rect = rect;
566 int gui_space(int pd)
570 if ((id = gui_widget(pd, GUI_SPACE)))
578 int gui_pause(int pd)
580 const char *text = _("Paused");
583 if ((id = gui_widget(pd, GUI_PAUSE)))
585 widget[id].text_img = make_image_from_font(NULL, NULL,
590 widget[id].color0 = gui_wht;
591 widget[id].color1 = gui_wht;
592 widget[id].value = 0;
593 widget[id].size = GUI_LRG;
594 widget[id].rect = GUI_ALL;
599 /*---------------------------------------------------------------------------*/
601 * Create a multi-line text box using a vertical array of labels.
602 * Parse the text for '\' characters and treat them as line-breaks.
603 * Preserve the rect specification across the entire array.
606 int gui_multi(int pd, const char *text, int size, int rect, const float *c0,
611 if (text && (id = gui_varray(pd)))
621 /* Copy each delimited string to a line buffer. */
623 for (p = text, j = 0; *p && j < 8; j++)
625 strncpy(s[j], p, (n = strcspn(p, "\\")));
629 if (*(p += n) == '\\') p++;
632 /* Set the curves for the first and last lines. */
636 r[0] |= rect & (GUI_NW | GUI_NE);
637 r[j - 1] |= rect & (GUI_SW | GUI_SE);
640 /* Create a label widget for each line. */
642 for (i = 0; i < j; i++)
643 gui_label(id, s[i], size, r[i], c0, c1);
648 /*---------------------------------------------------------------------------*/
650 * The bottom-up pass determines the area of all widgets. The minimum
651 * width and height of a leaf widget is given by the size of its
652 * contents. Array and stack widths and heights are computed
653 * recursively from these.
656 static void gui_widget_up(int id);
658 static void gui_harray_up(int id)
662 /* Find the widest child width and the highest child height. */
664 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
668 if (widget[id].h < widget[jd].h)
669 widget[id].h = widget[jd].h;
670 if (widget[id].w < widget[jd].w)
671 widget[id].w = widget[jd].w;
676 /* Total width is the widest child width times the child count. */
681 static void gui_varray_up(int id)
685 /* Find the widest child width and the highest child height. */
687 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
691 if (widget[id].h < widget[jd].h)
692 widget[id].h = widget[jd].h;
693 if (widget[id].w < widget[jd].w)
694 widget[id].w = widget[jd].w;
699 /* Total height is the highest child height times the child count. */
704 static void gui_hstack_up(int id)
708 /* Find the highest child height. Sum the child widths. */
710 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
714 if (widget[id].h < widget[jd].h)
715 widget[id].h = widget[jd].h;
717 widget[id].w += widget[jd].w;
721 static void gui_vstack_up(int id)
725 /* Find the widest child width. Sum the child heights. */
727 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
731 if (widget[id].w < widget[jd].w)
732 widget[id].w = widget[jd].w;
734 widget[id].h += widget[jd].h;
738 static void gui_paused_up(int id)
740 /* Store width and height for later use in text rendering. */
742 widget[id].x = widget[id].w;
743 widget[id].y = widget[id].h;
745 /* The pause widget fills the screen. */
747 widget[id].w = config_get_d(CONFIG_WIDTH);
748 widget[id].h = config_get_d(CONFIG_HEIGHT);
751 static void gui_button_up(int id)
753 /* Store width and height for later use in text rendering. */
755 widget[id].x = widget[id].w;
756 widget[id].y = widget[id].h;
758 if (widget[id].w < widget[id].h && widget[id].w > 0)
759 widget[id].w = widget[id].h;
762 /* Padded text elements look a little nicer. */
764 if (widget[id].w < config_get_d(CONFIG_WIDTH))
765 widget[id].w += radius;
766 if (widget[id].h < config_get_d(CONFIG_HEIGHT))
767 widget[id].h += radius;
770 static void gui_widget_up(int id)
773 switch (widget[id].type & GUI_TYPE)
775 case GUI_HARRAY: gui_harray_up(id); break;
776 case GUI_VARRAY: gui_varray_up(id); break;
777 case GUI_HSTACK: gui_hstack_up(id); break;
778 case GUI_VSTACK: gui_vstack_up(id); break;
779 case GUI_PAUSE: gui_paused_up(id); break;
780 default: gui_button_up(id); break;
784 /*---------------------------------------------------------------------------*/
786 * The top-down layout pass distributes available area as computed
787 * during the bottom-up pass. Widgets use their area and position to
788 * initialize rendering state.
791 static void gui_widget_dn(int id, int x, int y, int w, int h);
793 static void gui_harray_dn(int id, int x, int y, int w, int h)
795 int jd, i = 0, c = 0;
802 /* Count children. */
804 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
807 /* Distribute horizontal space evenly to all children. */
809 for (jd = widget[id].car; jd; jd = widget[jd].cdr, i++)
811 int x0 = x + i * w / c;
812 int x1 = x + (i + 1) * w / c;
814 gui_widget_dn(jd, x0, y, x1 - x0, h);
818 static void gui_varray_dn(int id, int x, int y, int w, int h)
820 int jd, i = 0, c = 0;
827 /* Count children. */
829 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
832 /* Distribute vertical space evenly to all children. */
834 for (jd = widget[id].car; jd; jd = widget[jd].cdr, i++)
836 int y0 = y + i * h / c;
837 int y1 = y + (i + 1) * h / c;
839 gui_widget_dn(jd, x, y0, w, y1 - y0);
843 static void gui_hstack_dn(int id, int x, int y, int w, int h)
845 int jd, jx = x, jw = 0, c = 0;
852 /* Measure the total width requested by non-filler children. */
854 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
855 if ((widget[jd].type & GUI_TYPE) == GUI_FILLER)
860 /* Give non-filler children their requested space. */
861 /* Distribute the rest evenly among filler children. */
863 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
865 if ((widget[jd].type & GUI_TYPE) == GUI_FILLER)
866 gui_widget_dn(jd, jx, y, (w - jw) / c, h);
868 gui_widget_dn(jd, jx, y, widget[jd].w, h);
874 static void gui_vstack_dn(int id, int x, int y, int w, int h)
876 int jd, jy = y, jh = 0, c = 0;
883 /* Measure the total height requested by non-filler children. */
885 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
886 if ((widget[jd].type & GUI_TYPE) == GUI_FILLER)
891 /* Give non-filler children their requested space. */
892 /* Distribute the rest evenly among filler children. */
894 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
896 if ((widget[jd].type & GUI_TYPE) == GUI_FILLER)
897 gui_widget_dn(jd, x, jy, w, (h - jh) / c);
899 gui_widget_dn(jd, x, jy, w, widget[jd].h);
905 static void gui_filler_dn(int id, int x, int y, int w, int h)
907 /* Filler expands to whatever size it is given. */
915 static void gui_button_dn(int id, int x, int y, int w, int h)
917 /* Recall stored width and height for text rendering. */
919 int W = widget[id].x;
920 int H = widget[id].y;
921 int R = widget[id].rect;
922 int r = ((widget[id].type & GUI_TYPE) == GUI_PAUSE ? radius * 4 : radius);
924 const float *c0 = widget[id].color0;
925 const float *c1 = widget[id].color1;
932 /* Create display lists for the text area and rounded rectangle. */
934 widget[id].text_obj = gui_list(-W / 2, -H / 2, W, H, c0, c1);
935 widget[id].rect_obj = gui_rect(-w / 2, -h / 2, w, h, R, r);
938 static void gui_widget_dn(int id, int x, int y, int w, int h)
941 switch (widget[id].type & GUI_TYPE)
943 case GUI_HARRAY: gui_harray_dn(id, x, y, w, h); break;
944 case GUI_VARRAY: gui_varray_dn(id, x, y, w, h); break;
945 case GUI_HSTACK: gui_hstack_dn(id, x, y, w, h); break;
946 case GUI_VSTACK: gui_vstack_dn(id, x, y, w, h); break;
947 case GUI_FILLER: gui_filler_dn(id, x, y, w, h); break;
948 case GUI_SPACE: gui_filler_dn(id, x, y, w, h); break;
949 default: gui_button_dn(id, x, y, w, h); break;
953 /*---------------------------------------------------------------------------*/
955 * During GUI layout, we make a bottom-up pass to determine total area
956 * requirements for the widget tree. We position this area to the
957 * sides or center of the screen. Finally, we make a top-down pass to
958 * distribute this area to each widget.
961 void gui_layout(int id, int xd, int yd)
965 int w, W = config_get_d(CONFIG_WIDTH);
966 int h, H = config_get_d(CONFIG_HEIGHT);
974 else if (xd > 0) x = (W - w);
975 else x = (W - w) / 2;
978 else if (yd > 0) y = (H - h);
979 else y = (H - h) / 2;
981 gui_widget_dn(id, x, y, w, h);
983 /* Hilite the widget under the cursor, if any. */
985 gui_point(id, -1, -1);
988 int gui_search(int id, int x, int y)
992 /* Search the hierarchy for the widget containing the given point. */
994 if (id && (widget[id].x <= x && x < widget[id].x + widget[id].w &&
995 widget[id].y <= y && y < widget[id].y + widget[id].h))
1000 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1001 if ((kd = gui_search(jd, x, y)))
1008 * Activate a widget, allowing it to behave as a normal state widget.
1009 * This may be used to create image buttons, or cause an array of
1010 * widgets to behave as a single state widget.
1012 int gui_active(int id, int token, int value)
1014 widget[id].type |= GUI_STATE;
1015 widget[id].token = token;
1016 widget[id].value = value;
1021 int gui_delete(int id)
1025 /* Recursively delete all subwidgets. */
1027 gui_delete(widget[id].cdr);
1028 gui_delete(widget[id].car);
1030 /* Release any GL resources held by this widget. */
1032 if (glIsTexture(widget[id].text_img))
1033 glDeleteTextures(1, &widget[id].text_img);
1035 if (glIsList(widget[id].text_obj))
1036 glDeleteLists(widget[id].text_obj, 1);
1037 if (glIsList(widget[id].rect_obj))
1038 glDeleteLists(widget[id].rect_obj, 1);
1040 /* Mark this widget unused. */
1042 widget[id].type = GUI_FREE;
1043 widget[id].text_img = 0;
1044 widget[id].text_obj = 0;
1045 widget[id].rect_obj = 0;
1052 /*---------------------------------------------------------------------------*/
1054 static void gui_paint_rect(int id, int st)
1056 static const GLfloat back[4][4] = {
1057 { 0.1f, 0.1f, 0.1f, 0.5f }, /* off and inactive */
1058 { 0.5f, 0.5f, 0.5f, 0.8f }, /* off and active */
1059 { 1.0f, 0.7f, 0.3f, 0.5f }, /* on and inactive */
1060 { 1.0f, 0.7f, 0.3f, 0.8f }, /* on and active */
1065 /* Use the widget status to determine the background color. */
1068 i = st | (((widget[id].value) ? 2 : 0) |
1069 ((id == active) ? 1 : 0));
1071 switch (widget[id].type & GUI_TYPE)
1083 /* Recursively paint all subwidgets. */
1085 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1086 gui_paint_rect(jd, i);
1092 /* Draw a leaf's background, colored by widget state. */
1096 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1097 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1099 glColor4fv(back[i]);
1100 glCallList(widget[id].rect_obj);
1108 /*---------------------------------------------------------------------------*/
1110 static void gui_paint_text(int id);
1112 static void gui_paint_array(int id)
1118 GLfloat cx = widget[id].x + widget[id].w / 2.0f;
1119 GLfloat cy = widget[id].y + widget[id].h / 2.0f;
1120 GLfloat ck = widget[id].scale;
1122 glTranslatef(+cx, +cy, 0.0f);
1123 glScalef(ck, ck, ck);
1124 glTranslatef(-cx, -cy, 0.0f);
1126 /* Recursively paint all subwidgets. */
1128 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1134 static void gui_paint_image(int id)
1136 /* Draw the widget rect, textured using the image. */
1140 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1141 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1143 glScalef(widget[id].scale,
1147 glBindTexture(GL_TEXTURE_2D, widget[id].text_img);
1148 glColor4fv(gui_wht);
1149 glCallList(widget[id].rect_obj);
1154 static void gui_paint_count(int id)
1156 int j, i = widget[id].size;
1160 glColor4fv(gui_wht);
1162 /* Translate to the widget center, and apply the pulse scale. */
1164 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1165 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1167 glScalef(widget[id].scale,
1171 if (widget[id].value > 0)
1173 /* Translate left by half the total width of the rendered value. */
1175 for (j = widget[id].value; j; j /= 10)
1176 glTranslatef((GLfloat) +digit_w[i][j % 10] / 2.0f, 0.0f, 0.0f);
1178 glTranslatef((GLfloat) -digit_w[i][0] / 2.0f, 0.0f, 0.0f);
1180 /* Render each digit, moving right after each. */
1182 for (j = widget[id].value; j; j /= 10)
1184 glBindTexture(GL_TEXTURE_2D, digit_text[i][j % 10]);
1185 glCallList(digit_list[i][j % 10]);
1186 glTranslatef((GLfloat) -digit_w[i][j % 10], 0.0f, 0.0f);
1189 else if (widget[id].value == 0)
1191 /* If the value is zero, just display a zero in place. */
1193 glBindTexture(GL_TEXTURE_2D, digit_text[i][0]);
1194 glCallList(digit_list[i][0]);
1200 static void gui_paint_clock(int id)
1202 int i = widget[id].size;
1203 int mt = (widget[id].value / 6000) / 10;
1204 int mo = (widget[id].value / 6000) % 10;
1205 int st = ((widget[id].value % 6000) / 100) / 10;
1206 int so = ((widget[id].value % 6000) / 100) % 10;
1207 int ht = ((widget[id].value % 6000) % 100) / 10;
1208 int ho = ((widget[id].value % 6000) % 100) % 10;
1210 GLfloat dx_large = (GLfloat) digit_w[i][0];
1211 GLfloat dx_small = (GLfloat) digit_w[i][0] * 0.75f;
1213 if (widget[id].value < 0)
1218 glColor4fv(gui_wht);
1220 /* Translate to the widget center, and apply the pulse scale. */
1222 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1223 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1225 glScalef(widget[id].scale,
1229 /* Translate left by half the total width of the rendered value. */
1232 glTranslatef(-2.25f * dx_large, 0.0f, 0.0f);
1234 glTranslatef(-1.75f * dx_large, 0.0f, 0.0f);
1236 /* Render the minutes counter. */
1240 glBindTexture(GL_TEXTURE_2D, digit_text[i][mt]);
1241 glCallList(digit_list[i][mt]);
1242 glTranslatef(dx_large, 0.0f, 0.0f);
1245 glBindTexture(GL_TEXTURE_2D, digit_text[i][mo]);
1246 glCallList(digit_list[i][mo]);
1247 glTranslatef(dx_small, 0.0f, 0.0f);
1249 /* Render the colon. */
1251 glBindTexture(GL_TEXTURE_2D, digit_text[i][10]);
1252 glCallList(digit_list[i][10]);
1253 glTranslatef(dx_small, 0.0f, 0.0f);
1255 /* Render the seconds counter. */
1257 glBindTexture(GL_TEXTURE_2D, digit_text[i][st]);
1258 glCallList(digit_list[i][st]);
1259 glTranslatef(dx_large, 0.0f, 0.0f);
1261 glBindTexture(GL_TEXTURE_2D, digit_text[i][so]);
1262 glCallList(digit_list[i][so]);
1263 glTranslatef(dx_small, 0.0f, 0.0f);
1265 /* Render hundredths counter half size. */
1267 glScalef(0.5f, 0.5f, 1.0f);
1269 glBindTexture(GL_TEXTURE_2D, digit_text[i][ht]);
1270 glCallList(digit_list[i][ht]);
1271 glTranslatef(dx_large, 0.0f, 0.0f);
1273 glBindTexture(GL_TEXTURE_2D, digit_text[i][ho]);
1274 glCallList(digit_list[i][ho]);
1279 static void gui_paint_label(int id)
1281 /* Draw the widget text box, textured using the glyph. */
1285 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1286 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1288 glScalef(widget[id].scale,
1292 glBindTexture(GL_TEXTURE_2D, widget[id].text_img);
1293 glCallList(widget[id].text_obj);
1298 static void gui_paint_text(int id)
1300 switch (widget[id].type & GUI_TYPE)
1302 case GUI_SPACE: break;
1303 case GUI_FILLER: break;
1304 case GUI_HARRAY: gui_paint_array(id); break;
1305 case GUI_VARRAY: gui_paint_array(id); break;
1306 case GUI_HSTACK: gui_paint_array(id); break;
1307 case GUI_VSTACK: gui_paint_array(id); break;
1308 case GUI_IMAGE: gui_paint_image(id); break;
1309 case GUI_COUNT: gui_paint_count(id); break;
1310 case GUI_CLOCK: gui_paint_clock(id); break;
1311 default: gui_paint_label(id); break;
1315 void gui_paint(int id)
1319 glPushAttrib(GL_LIGHTING_BIT |
1320 GL_COLOR_BUFFER_BIT |
1321 GL_DEPTH_BUFFER_BIT);
1322 config_push_ortho();
1325 glEnable(GL_COLOR_MATERIAL);
1326 glDisable(GL_LIGHTING);
1327 glDisable(GL_DEPTH_TEST);
1329 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1331 glPushAttrib(GL_TEXTURE_BIT);
1333 glDisable(GL_TEXTURE_2D);
1334 gui_paint_rect(id, 0);
1340 config_pop_matrix();
1345 void gui_blank(void)
1347 gui_paint(pause_id);
1350 /*---------------------------------------------------------------------------*/
1352 void gui_dump(int id, int d)
1360 switch (widget[id].type & GUI_TYPE)
1362 case GUI_HARRAY: type = "harray"; break;
1363 case GUI_VARRAY: type = "varray"; break;
1364 case GUI_HSTACK: type = "hstack"; break;
1365 case GUI_VSTACK: type = "vstack"; break;
1366 case GUI_FILLER: type = "filler"; break;
1367 case GUI_IMAGE: type = "image"; break;
1368 case GUI_LABEL: type = "label"; break;
1369 case GUI_COUNT: type = "count"; break;
1370 case GUI_CLOCK: type = "clock"; break;
1373 for (i = 0; i < d; i++)
1376 printf("%04d %s\n", id, type);
1378 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1379 gui_dump(jd, d + 1);
1383 void gui_pulse(int id, float k)
1385 if (id) widget[id].scale = k;
1388 void gui_timer(int id, float dt)
1394 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1397 if (widget[id].scale - 1.0f < dt)
1398 widget[id].scale = 1.0f;
1400 widget[id].scale -= dt;
1404 int gui_point(int id, int x, int y)
1406 static int x_cache = 0;
1407 static int y_cache = 0;
1411 /* Reuse the last coordinates if (x,y) == (-1,-1) */
1414 return gui_point(id, x_cache, y_cache);
1419 /* Short-circuit check the current active widget. */
1421 jd = gui_search(active, x, y);
1423 /* If not still active, search the hierarchy for a new active widget. */
1426 jd = gui_search(id, x, y);
1428 /* If the active widget has changed, return the new active id. */
1430 if (jd == 0 || jd == active)
1436 void gui_focus(int i)
1446 int gui_token(int id)
1448 return id ? widget[id].token : 0;
1451 int gui_value(int id)
1453 return id ? widget[id].value : 0;
1456 void gui_toggle(int id)
1458 widget[id].value = widget[id].value ? 0 : 1;
1461 /*---------------------------------------------------------------------------*/
1463 static int gui_vert_test(int id, int jd)
1465 /* Determine whether widget id is in vertical contact with widget jd. */
1467 if (id && gui_hot(id) && jd && gui_hot(jd))
1469 int i0 = widget[id].x;
1470 int i1 = widget[id].x + widget[id].w;
1471 int j0 = widget[jd].x;
1472 int j1 = widget[jd].x + widget[jd].w;
1474 /* Is widget id's top edge is in contact with jd's bottom edge? */
1476 if (widget[id].y + widget[id].h == widget[jd].y)
1478 /* Do widgets id and jd overlap horizontally? */
1480 if (j0 <= i0 && i0 < j1) return 1;
1481 if (j0 < i1 && i1 <= j1) return 1;
1482 if (i0 <= j0 && j0 < i1) return 1;
1483 if (i0 < j1 && j1 <= i1) return 1;
1489 static int gui_horz_test(int id, int jd)
1491 /* Determine whether widget id is in horizontal contact with widget jd. */
1493 if (id && gui_hot(id) && jd && gui_hot(jd))
1495 int i0 = widget[id].y;
1496 int i1 = widget[id].y + widget[id].h;
1497 int j0 = widget[jd].y;
1498 int j1 = widget[jd].y + widget[jd].h;
1500 /* Is widget id's right edge in contact with jd's left edge? */
1502 if (widget[id].x + widget[id].w == widget[jd].x)
1504 /* Do widgets id and jd overlap vertically? */
1506 if (j0 <= i0 && i0 < j1) return 1;
1507 if (j0 < i1 && i1 <= j1) return 1;
1508 if (i0 <= j0 && j0 < i1) return 1;
1509 if (i0 < j1 && j1 <= i1) return 1;
1515 /*---------------------------------------------------------------------------*/
1517 static int gui_stick_L(int id, int dd)
1521 /* Find a widget to the left of widget dd. */
1523 if (gui_horz_test(id, dd))
1526 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1527 if ((kd = gui_stick_L(jd, dd)))
1533 static int gui_stick_R(int id, int dd)
1537 /* Find a widget to the right of widget dd. */
1539 if (gui_horz_test(dd, id))
1542 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1543 if ((kd = gui_stick_R(jd, dd)))
1549 static int gui_stick_D(int id, int dd)
1553 /* Find a widget below widget dd. */
1555 if (gui_vert_test(id, dd))
1558 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1559 if ((kd = gui_stick_D(jd, dd)))
1565 static int gui_stick_U(int id, int dd)
1569 /* Find a widget above widget dd. */
1571 if (gui_vert_test(dd, id))
1574 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1575 if ((kd = gui_stick_U(jd, dd)))
1581 /*---------------------------------------------------------------------------*/
1583 static int gui_wrap_L(int id, int dd)
1587 if ((jd = gui_stick_L(id, dd)) == 0)
1588 for (jd = dd; (kd = gui_stick_R(id, jd)); jd = kd)
1594 static int gui_wrap_R(int id, int dd)
1598 if ((jd = gui_stick_R(id, dd)) == 0)
1599 for (jd = dd; (kd = gui_stick_L(id, jd)); jd = kd)
1605 static int gui_wrap_U(int id, int dd)
1609 if ((jd = gui_stick_U(id, dd)) == 0)
1610 for (jd = dd; (kd = gui_stick_D(id, jd)); jd = kd)
1616 static int gui_wrap_D(int id, int dd)
1620 if ((jd = gui_stick_D(id, dd)) == 0)
1621 for (jd = dd; (kd = gui_stick_U(id, jd)); jd = kd)
1627 /*---------------------------------------------------------------------------*/
1629 int gui_stick(int id, int x, int y)
1631 /* Flag the axes to prevent uncontrolled scrolling. */
1633 static int xflag = 1;
1634 static int yflag = 1;
1638 /* Find a new active widget in the direction of joystick motion. */
1640 if (x && -JOY_MID <= x && x <= +JOY_MID)
1642 else if (x < -JOY_MID && xflag && (jd = gui_wrap_L(id, active)))
1644 else if (x > +JOY_MID && xflag && (jd = gui_wrap_R(id, active)))
1647 if (y && -JOY_MID <= y && y <= +JOY_MID)
1649 else if (y < -JOY_MID && yflag && (jd = gui_wrap_U(id, active)))
1651 else if (y > +JOY_MID && yflag && (jd = gui_wrap_D(id, active)))
1654 /* If the active widget has changed, return the new active id. */
1656 if (jd == 0 || jd == active)
1662 /*---------------------------------------------------------------------------*/