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
61 const GLfloat *color0;
62 const GLfloat *color1;
67 /*---------------------------------------------------------------------------*/
69 const GLfloat gui_wht[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
70 const GLfloat gui_yel[4] = { 1.0f, 1.0f, 0.0f, 1.0f };
71 const GLfloat gui_red[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
72 const GLfloat gui_grn[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
73 const GLfloat gui_blu[4] = { 0.0f, 0.0f, 1.0f, 1.0f };
74 const GLfloat gui_blk[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
75 const GLfloat gui_gry[4] = { 0.3f, 0.3f, 0.3f, 1.0f };
77 /*---------------------------------------------------------------------------*/
79 static struct widget widget[MAXWIDGET];
82 static TTF_Font *font[3] = { NULL, NULL, NULL };
83 static int scale[3] = { 1, 1, 1 };
85 static GLuint digit_text[3][11];
86 static GLuint digit_list[3][11];
87 static int digit_w[3][11];
88 static int digit_h[3][11];
90 /*---------------------------------------------------------------------------*/
92 static int gui_hot(int id)
94 return (widget[id].type & GUI_STATE);
97 /*---------------------------------------------------------------------------*/
99 * Initialize a display list containing a rectangle (x, y, w, h) to
100 * which a rendered-font texture may be applied. Colors c0 and c1
101 * determine the top-to-bottom color gradient of the text.
104 static GLuint gui_list(int x, int y,
105 int w, int h, const float *c0, const float *c1)
107 GLuint list = glGenLists(1);
112 int W, H, ww, hh, d = h / 16;
114 /* Assume the applied texture size is rect size rounded to power-of-two. */
116 image_size(&W, &H, w, h);
118 ww = ((W - w) % 2) ? w + 1 : w;
119 hh = ((H - h) % 2) ? h + 1 : h;
121 s0 = 0.5f * (W - ww) / W;
122 t0 = 0.5f * (H - hh) / 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 + ww + d, y - d);
133 glTexCoord2f(s1, t0); glVertex2i(x + ww + d, y + hh - d);
134 glTexCoord2f(s0, t0); glVertex2i(x + d, y + hh - d);
137 glTexCoord2f(s0, t1); glVertex2i(x, y);
138 glTexCoord2f(s1, t1); glVertex2i(x + ww, y);
141 glTexCoord2f(s1, t0); glVertex2i(x + ww, y + hh);
142 glTexCoord2f(s0, t0); glVertex2i(x, y + hh);
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 digit glyphs and lists for counters and clocks. */
256 for (i = 0; i < 3; i++)
260 /* Draw digits 0 through 9. */
262 for (j = 0; j < 10; j++)
264 text[0] = '0' + (char) j;
267 digit_text[i][j] = make_image_from_font(NULL, NULL,
270 text, font[i], scale[i]);
271 digit_list[i][j] = gui_list(-digit_w[i][j] / 2,
274 +digit_h[i][j], c0, c1);
277 /* Draw the colon for the clock. */
279 digit_text[i][j] = make_image_from_font(NULL, NULL,
282 ":", font[i], scale[i]);
283 digit_list[i][j] = gui_list(-digit_w[i][10] / 2,
286 +digit_h[i][10], c0, c1);
297 /* Release any remaining widget texture and display list indices. */
299 for (id = 1; id < MAXWIDGET; id++)
301 if (glIsTexture(widget[id].text_img))
302 glDeleteTextures(1, &widget[id].text_img);
304 if (glIsList(widget[id].text_obj))
305 glDeleteLists(widget[id].text_obj, 1);
306 if (glIsList(widget[id].rect_obj))
307 glDeleteLists(widget[id].rect_obj, 1);
309 widget[id].type = GUI_FREE;
310 widget[id].text_img = 0;
311 widget[id].text_obj = 0;
312 widget[id].rect_obj = 0;
317 /* Release all digit textures and display lists. */
319 for (i = 0; i < 3; i++)
320 for (j = 0; j < 11; j++)
322 if (glIsTexture(digit_text[i][j]))
323 glDeleteTextures(1, &digit_text[i][j]);
325 if (glIsList(digit_list[i][j]))
326 glDeleteLists(digit_list[i][j], 1);
329 /* Release all loaded fonts and finalize font rendering. */
331 if (font[GUI_LRG]) TTF_CloseFont(font[GUI_LRG]);
332 if (font[GUI_MED]) TTF_CloseFont(font[GUI_MED]);
333 if (font[GUI_SML]) TTF_CloseFont(font[GUI_SML]);
338 /*---------------------------------------------------------------------------*/
340 static int gui_widget(int pd, int type)
344 /* Find an unused entry in the widget table. */
346 for (id = 1; id < MAXWIDGET; id++)
347 if (widget[id].type == GUI_FREE)
349 /* Set the type and default properties. */
351 widget[id].type = type;
352 widget[id].token = 0;
353 widget[id].value = 0;
355 widget[id].rect = GUI_NW | GUI_SW | GUI_NE | GUI_SE;
358 widget[id].text_img = 0;
359 widget[id].text_obj = 0;
360 widget[id].rect_obj = 0;
361 widget[id].color0 = gui_wht;
362 widget[id].color1 = gui_wht;
363 widget[id].scale = 1.0f;
365 /* Insert the new widget into the parent's widget list. */
370 widget[id].cdr = widget[pd].car;
382 fprintf(stderr, _("Out of widget IDs\n"));
387 int gui_harray(int pd) { return gui_widget(pd, GUI_HARRAY); }
388 int gui_varray(int pd) { return gui_widget(pd, GUI_VARRAY); }
389 int gui_hstack(int pd) { return gui_widget(pd, GUI_HSTACK); }
390 int gui_vstack(int pd) { return gui_widget(pd, GUI_VSTACK); }
391 int gui_filler(int pd) { return gui_widget(pd, GUI_FILLER); }
393 /*---------------------------------------------------------------------------*/
395 void gui_set_image(int id, const char *file)
397 if (glIsTexture(widget[id].text_img))
398 glDeleteTextures(1, &widget[id].text_img);
400 widget[id].text_img = make_image_from_file(NULL, NULL, NULL, NULL, file);
403 void gui_set_label(int id, const char *text)
407 if (glIsTexture(widget[id].text_img))
408 glDeleteTextures(1, &widget[id].text_img);
409 if (glIsList(widget[id].text_obj))
410 glDeleteLists(widget[id].text_obj, 1);
412 widget[id].text_img = make_image_from_font(NULL, NULL, &w, &h,
413 text, font[widget[id].size],
414 scale[widget[id].size]);
415 widget[id].text_obj = gui_list(-w / 2, -h / 2, w, h,
416 widget[id].color0, widget[id].color1);
419 void gui_set_count(int id, int value)
421 widget[id].value = value;
424 void gui_set_clock(int id, int value)
426 widget[id].value = value;
429 void gui_set_color(int id, const float *c0,
432 widget[id].color0 = c0 ? c0 : gui_yel;
433 widget[id].color1 = c1 ? c1 : gui_red;
436 void gui_set_multi(int id, const char *text)
445 /* Copy each delimited string to a line buffer. */
447 for (p = text, j = 0; *p && j < 8; j++)
449 strncpy(s[j], p, (n = strcspn(p, "\\")));
452 if (*(p += n) == '\\') p++;
455 /* Set the label value for each line. */
457 for (i = j - 1, jd = widget[id].car; i >= 0 && jd; i--, jd = widget[jd].cdr)
458 gui_set_label(jd, s[i]);
461 /*---------------------------------------------------------------------------*/
463 int gui_image(int pd, const char *file, int w, int h)
467 if ((id = gui_widget(pd, GUI_IMAGE)))
469 widget[id].text_img = make_image_from_file(NULL, NULL,
477 int gui_start(int pd, const char *text, int size, int token, int value)
481 if ((id = gui_state(pd, text, size, token, value)))
487 int gui_state(int pd, const char *text, int size, int token, int value)
491 if ((id = gui_widget(pd, GUI_STATE)))
493 widget[id].text_img = make_image_from_font(NULL, NULL,
498 widget[id].size = size;
499 widget[id].token = token;
500 widget[id].value = value;
505 int gui_label(int pd, const char *text, int size, int rect, const float *c0,
510 if ((id = gui_widget(pd, GUI_LABEL)))
512 widget[id].text_img = make_image_from_font(NULL, NULL,
517 widget[id].size = size;
518 widget[id].color0 = c0 ? c0 : gui_yel;
519 widget[id].color1 = c1 ? c1 : gui_red;
520 widget[id].rect = rect;
525 int gui_count(int pd, int value, int size, int rect)
529 if ((id = gui_widget(pd, GUI_COUNT)))
531 for (i = value; i; i /= 10)
532 widget[id].w += digit_w[size][0];
534 widget[id].h = digit_h[size][0];
535 widget[id].value = value;
536 widget[id].size = size;
537 widget[id].color0 = gui_yel;
538 widget[id].color1 = gui_red;
539 widget[id].rect = rect;
544 int gui_clock(int pd, int value, int size, int rect)
548 if ((id = gui_widget(pd, GUI_CLOCK)))
550 widget[id].w = digit_w[size][0] * 6;
551 widget[id].h = digit_h[size][0];
552 widget[id].value = value;
553 widget[id].size = size;
554 widget[id].color0 = gui_yel;
555 widget[id].color1 = gui_red;
556 widget[id].rect = rect;
561 int gui_space(int pd)
565 if ((id = gui_widget(pd, GUI_SPACE)))
573 /*---------------------------------------------------------------------------*/
575 * Create a multi-line text box using a vertical array of labels.
576 * Parse the text for '\' characters and treat them as line-breaks.
577 * Preserve the rect specification across the entire array.
580 int gui_multi(int pd, const char *text, int size, int rect, const float *c0,
585 if (text && (id = gui_varray(pd)))
595 /* Copy each delimited string to a line buffer. */
597 for (p = text, j = 0; *p && j < 8; j++)
599 strncpy(s[j], p, (n = strcspn(p, "\\")));
603 if (*(p += n) == '\\') p++;
606 /* Set the curves for the first and last lines. */
610 r[0] |= rect & (GUI_NW | GUI_NE);
611 r[j - 1] |= rect & (GUI_SW | GUI_SE);
614 /* Create a label widget for each line. */
616 for (i = 0; i < j; i++)
617 gui_label(id, s[i], size, r[i], c0, c1);
622 /*---------------------------------------------------------------------------*/
624 * The bottom-up pass determines the area of all widgets. The minimum
625 * width and height of a leaf widget is given by the size of its
626 * contents. Array and stack widths and heights are computed
627 * recursively from these.
630 static void gui_widget_up(int id);
632 static void gui_harray_up(int id)
636 /* Find the widest child width and the highest child height. */
638 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
642 if (widget[id].h < widget[jd].h)
643 widget[id].h = widget[jd].h;
644 if (widget[id].w < widget[jd].w)
645 widget[id].w = widget[jd].w;
650 /* Total width is the widest child width times the child count. */
655 static void gui_varray_up(int id)
659 /* Find the widest child width and the highest child height. */
661 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
665 if (widget[id].h < widget[jd].h)
666 widget[id].h = widget[jd].h;
667 if (widget[id].w < widget[jd].w)
668 widget[id].w = widget[jd].w;
673 /* Total height is the highest child height times the child count. */
678 static void gui_hstack_up(int id)
682 /* Find the highest child height. Sum the child widths. */
684 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
688 if (widget[id].h < widget[jd].h)
689 widget[id].h = widget[jd].h;
691 widget[id].w += widget[jd].w;
695 static void gui_vstack_up(int id)
699 /* Find the widest child width. Sum the child heights. */
701 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
705 if (widget[id].w < widget[jd].w)
706 widget[id].w = widget[jd].w;
708 widget[id].h += widget[jd].h;
712 static void gui_button_up(int id)
714 /* Store width and height for later use in text rendering. */
716 widget[id].x = widget[id].w;
717 widget[id].y = widget[id].h;
719 if (widget[id].w < widget[id].h && widget[id].w > 0)
720 widget[id].w = widget[id].h;
723 /* Padded text elements look a little nicer. */
725 if (widget[id].w < config_get_d(CONFIG_WIDTH))
726 widget[id].w += radius;
727 if (widget[id].h < config_get_d(CONFIG_HEIGHT))
728 widget[id].h += radius;
731 static void gui_widget_up(int id)
734 switch (widget[id].type & GUI_TYPE)
736 case GUI_HARRAY: gui_harray_up(id); break;
737 case GUI_VARRAY: gui_varray_up(id); break;
738 case GUI_HSTACK: gui_hstack_up(id); break;
739 case GUI_VSTACK: gui_vstack_up(id); break;
740 default: gui_button_up(id); break;
744 /*---------------------------------------------------------------------------*/
746 * The top-down layout pass distributes available area as computed
747 * during the bottom-up pass. Widgets use their area and position to
748 * initialize rendering state.
751 static void gui_widget_dn(int id, int x, int y, int w, int h);
753 static void gui_harray_dn(int id, int x, int y, int w, int h)
755 int jd, i = 0, c = 0;
762 /* Count children. */
764 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
767 /* Distribute horizontal space evenly to all children. */
769 for (jd = widget[id].car; jd; jd = widget[jd].cdr, i++)
771 int x0 = x + i * w / c;
772 int x1 = x + (i + 1) * w / c;
774 gui_widget_dn(jd, x0, y, x1 - x0, h);
778 static void gui_varray_dn(int id, int x, int y, int w, int h)
780 int jd, i = 0, c = 0;
787 /* Count children. */
789 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
792 /* Distribute vertical space evenly to all children. */
794 for (jd = widget[id].car; jd; jd = widget[jd].cdr, i++)
796 int y0 = y + i * h / c;
797 int y1 = y + (i + 1) * h / c;
799 gui_widget_dn(jd, x, y0, w, y1 - y0);
803 static void gui_hstack_dn(int id, int x, int y, int w, int h)
805 int jd, jx = x, jw = 0, c = 0;
812 /* Measure the total width requested by non-filler children. */
814 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
815 if ((widget[jd].type & GUI_TYPE) == GUI_FILLER)
820 /* Give non-filler children their requested space. */
821 /* Distribute the rest evenly among filler children. */
823 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
825 if ((widget[jd].type & GUI_TYPE) == GUI_FILLER)
826 gui_widget_dn(jd, jx, y, (w - jw) / c, h);
828 gui_widget_dn(jd, jx, y, widget[jd].w, h);
834 static void gui_vstack_dn(int id, int x, int y, int w, int h)
836 int jd, jy = y, jh = 0, c = 0;
843 /* Measure the total height requested by non-filler children. */
845 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
846 if ((widget[jd].type & GUI_TYPE) == GUI_FILLER)
851 /* Give non-filler children their requested space. */
852 /* Distribute the rest evenly among filler children. */
854 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
856 if ((widget[jd].type & GUI_TYPE) == GUI_FILLER)
857 gui_widget_dn(jd, x, jy, w, (h - jh) / c);
859 gui_widget_dn(jd, x, jy, w, widget[jd].h);
865 static void gui_filler_dn(int id, int x, int y, int w, int h)
867 /* Filler expands to whatever size it is given. */
875 static void gui_button_dn(int id, int x, int y, int w, int h)
877 /* Recall stored width and height for text rendering. */
879 int W = widget[id].x;
880 int H = widget[id].y;
881 int R = widget[id].rect;
883 const float *c0 = widget[id].color0;
884 const float *c1 = widget[id].color1;
891 /* Create display lists for the text area and rounded rectangle. */
893 widget[id].text_obj = gui_list(-W / 2, -H / 2, W, H, c0, c1);
894 widget[id].rect_obj = gui_rect(-w / 2, -h / 2, w, h, R, radius);
897 static void gui_widget_dn(int id, int x, int y, int w, int h)
900 switch (widget[id].type & GUI_TYPE)
902 case GUI_HARRAY: gui_harray_dn(id, x, y, w, h); break;
903 case GUI_VARRAY: gui_varray_dn(id, x, y, w, h); break;
904 case GUI_HSTACK: gui_hstack_dn(id, x, y, w, h); break;
905 case GUI_VSTACK: gui_vstack_dn(id, x, y, w, h); break;
906 case GUI_FILLER: gui_filler_dn(id, x, y, w, h); break;
907 case GUI_SPACE: gui_filler_dn(id, x, y, w, h); break;
908 default: gui_button_dn(id, x, y, w, h); break;
912 /*---------------------------------------------------------------------------*/
914 * During GUI layout, we make a bottom-up pass to determine total area
915 * requirements for the widget tree. We position this area to the
916 * sides or center of the screen. Finally, we make a top-down pass to
917 * distribute this area to each widget.
920 void gui_layout(int id, int xd, int yd)
924 int w, W = config_get_d(CONFIG_WIDTH);
925 int h, H = config_get_d(CONFIG_HEIGHT);
933 else if (xd > 0) x = (W - w);
934 else x = (W - w) / 2;
937 else if (yd > 0) y = (H - h);
938 else y = (H - h) / 2;
940 gui_widget_dn(id, x, y, w, h);
942 /* Hilite the widget under the cursor, if any. */
944 gui_point(id, -1, -1);
947 int gui_search(int id, int x, int y)
951 /* Search the hierarchy for the widget containing the given point. */
953 if (id && (widget[id].x <= x && x < widget[id].x + widget[id].w &&
954 widget[id].y <= y && y < widget[id].y + widget[id].h))
959 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
960 if ((kd = gui_search(jd, x, y)))
967 * Activate a widget, allowing it to behave as a normal state widget.
968 * This may be used to create image buttons, or cause an array of
969 * widgets to behave as a single state widget.
971 int gui_active(int id, int token, int value)
973 widget[id].type |= GUI_STATE;
974 widget[id].token = token;
975 widget[id].value = value;
980 int gui_delete(int id)
984 /* Recursively delete all subwidgets. */
986 gui_delete(widget[id].cdr);
987 gui_delete(widget[id].car);
989 /* Release any GL resources held by this widget. */
991 if (glIsTexture(widget[id].text_img))
992 glDeleteTextures(1, &widget[id].text_img);
994 if (glIsList(widget[id].text_obj))
995 glDeleteLists(widget[id].text_obj, 1);
996 if (glIsList(widget[id].rect_obj))
997 glDeleteLists(widget[id].rect_obj, 1);
999 /* Mark this widget unused. */
1001 widget[id].type = GUI_FREE;
1002 widget[id].text_img = 0;
1003 widget[id].text_obj = 0;
1004 widget[id].rect_obj = 0;
1011 /*---------------------------------------------------------------------------*/
1013 static void gui_paint_rect(int id, int st, int alt)
1015 static const GLfloat default_back[4][4] = {
1016 { 0.1f, 0.1f, 0.1f, 0.5f }, /* off and inactive */
1017 { 0.5f, 0.5f, 0.5f, 0.8f }, /* off and active */
1018 { 1.0f, 0.7f, 0.3f, 0.5f }, /* on and inactive */
1019 { 1.0f, 0.7f, 0.3f, 0.8f }, /* on and active */
1022 static const GLfloat alt_back[4][4] = {
1023 { 1.0f, 0.0f, 0.0f, 0.3f }, /* off and inactive */
1024 { 0.5f, 0.5f, 0.5f, 0.8f }, /* off and active */
1025 { 1.0f, 0.7f, 0.3f, 0.5f }, /* on and inactive */
1026 { 1.0f, 0.7f, 0.3f, 0.8f }, /* on and active */
1029 const GLfloat (*back)[4] = alt ? alt_back : default_back;
1033 /* Use the widget status to determine the background color. */
1036 i = st | (((widget[id].value) ? 2 : 0) |
1037 ((id == active) ? 1 : 0));
1039 switch (widget[id].type & GUI_TYPE)
1051 /* Recursively paint all subwidgets. */
1053 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1054 gui_paint_rect(jd, i, alt);
1060 /* Draw a leaf's background, colored by widget state. */
1064 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1065 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1067 glColor4fv(back[i]);
1068 glCallList(widget[id].rect_obj);
1076 /*---------------------------------------------------------------------------*/
1078 static void gui_paint_text(int id);
1080 static void gui_paint_array(int id)
1086 GLfloat cx = widget[id].x + widget[id].w / 2.0f;
1087 GLfloat cy = widget[id].y + widget[id].h / 2.0f;
1088 GLfloat ck = widget[id].scale;
1090 glTranslatef(+cx, +cy, 0.0f);
1091 glScalef(ck, ck, ck);
1092 glTranslatef(-cx, -cy, 0.0f);
1094 /* Recursively paint all subwidgets. */
1096 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1102 static void gui_paint_image(int id)
1104 /* Draw the widget rect, textured using the image. */
1108 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1109 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1111 glScalef(widget[id].scale,
1115 glBindTexture(GL_TEXTURE_2D, widget[id].text_img);
1116 glColor4fv(gui_wht);
1117 glCallList(widget[id].rect_obj);
1122 static void gui_paint_count(int id)
1124 int j, i = widget[id].size;
1128 glColor4fv(gui_wht);
1130 /* Translate to the widget center, and apply the pulse scale. */
1132 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1133 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1135 glScalef(widget[id].scale,
1139 if (widget[id].value > 0)
1141 /* Translate left by half the total width of the rendered value. */
1143 for (j = widget[id].value; j; j /= 10)
1144 glTranslatef((GLfloat) +digit_w[i][j % 10] / 2.0f, 0.0f, 0.0f);
1146 glTranslatef((GLfloat) -digit_w[i][0] / 2.0f, 0.0f, 0.0f);
1148 /* Render each digit, moving right after each. */
1150 for (j = widget[id].value; j; j /= 10)
1152 glBindTexture(GL_TEXTURE_2D, digit_text[i][j % 10]);
1153 glCallList(digit_list[i][j % 10]);
1154 glTranslatef((GLfloat) -digit_w[i][j % 10], 0.0f, 0.0f);
1157 else if (widget[id].value == 0)
1159 /* If the value is zero, just display a zero in place. */
1161 glBindTexture(GL_TEXTURE_2D, digit_text[i][0]);
1162 glCallList(digit_list[i][0]);
1168 static void gui_paint_clock(int id)
1170 int i = widget[id].size;
1171 int mt = (widget[id].value / 6000) / 10;
1172 int mo = (widget[id].value / 6000) % 10;
1173 int st = ((widget[id].value % 6000) / 100) / 10;
1174 int so = ((widget[id].value % 6000) / 100) % 10;
1175 int ht = ((widget[id].value % 6000) % 100) / 10;
1176 int ho = ((widget[id].value % 6000) % 100) % 10;
1178 GLfloat dx_large = (GLfloat) digit_w[i][0];
1179 GLfloat dx_small = (GLfloat) digit_w[i][0] * 0.75f;
1181 if (widget[id].value < 0)
1186 glColor4fv(gui_wht);
1188 /* Translate to the widget center, and apply the pulse scale. */
1190 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1191 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1193 glScalef(widget[id].scale,
1197 /* Translate left by half the total width of the rendered value. */
1200 glTranslatef(-2.25f * dx_large, 0.0f, 0.0f);
1202 glTranslatef(-1.75f * dx_large, 0.0f, 0.0f);
1204 /* Render the minutes counter. */
1208 glBindTexture(GL_TEXTURE_2D, digit_text[i][mt]);
1209 glCallList(digit_list[i][mt]);
1210 glTranslatef(dx_large, 0.0f, 0.0f);
1213 glBindTexture(GL_TEXTURE_2D, digit_text[i][mo]);
1214 glCallList(digit_list[i][mo]);
1215 glTranslatef(dx_small, 0.0f, 0.0f);
1217 /* Render the colon. */
1219 glBindTexture(GL_TEXTURE_2D, digit_text[i][10]);
1220 glCallList(digit_list[i][10]);
1221 glTranslatef(dx_small, 0.0f, 0.0f);
1223 /* Render the seconds counter. */
1225 glBindTexture(GL_TEXTURE_2D, digit_text[i][st]);
1226 glCallList(digit_list[i][st]);
1227 glTranslatef(dx_large, 0.0f, 0.0f);
1229 glBindTexture(GL_TEXTURE_2D, digit_text[i][so]);
1230 glCallList(digit_list[i][so]);
1231 glTranslatef(dx_small, 0.0f, 0.0f);
1233 /* Render hundredths counter half size. */
1235 glScalef(0.5f, 0.5f, 1.0f);
1237 glBindTexture(GL_TEXTURE_2D, digit_text[i][ht]);
1238 glCallList(digit_list[i][ht]);
1239 glTranslatef(dx_large, 0.0f, 0.0f);
1241 glBindTexture(GL_TEXTURE_2D, digit_text[i][ho]);
1242 glCallList(digit_list[i][ho]);
1247 static void gui_paint_label(int id)
1249 /* Draw the widget text box, textured using the glyph. */
1253 glTranslatef((GLfloat) (widget[id].x + widget[id].w / 2),
1254 (GLfloat) (widget[id].y + widget[id].h / 2), 0.f);
1256 glScalef(widget[id].scale,
1260 glBindTexture(GL_TEXTURE_2D, widget[id].text_img);
1261 glCallList(widget[id].text_obj);
1266 static void gui_paint_text(int id)
1268 switch (widget[id].type & GUI_TYPE)
1270 case GUI_SPACE: break;
1271 case GUI_FILLER: break;
1272 case GUI_HARRAY: gui_paint_array(id); break;
1273 case GUI_VARRAY: gui_paint_array(id); break;
1274 case GUI_HSTACK: gui_paint_array(id); break;
1275 case GUI_VSTACK: gui_paint_array(id); break;
1276 case GUI_IMAGE: gui_paint_image(id); break;
1277 case GUI_COUNT: gui_paint_count(id); break;
1278 case GUI_CLOCK: gui_paint_clock(id); break;
1279 default: gui_paint_label(id); break;
1283 void gui_paint(int id, int alt)
1287 glPushAttrib(GL_LIGHTING_BIT |
1288 GL_COLOR_BUFFER_BIT |
1289 GL_DEPTH_BUFFER_BIT);
1290 config_push_ortho();
1293 glEnable(GL_COLOR_MATERIAL);
1294 glDisable(GL_LIGHTING);
1295 glDisable(GL_DEPTH_TEST);
1297 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1299 glPushAttrib(GL_TEXTURE_BIT);
1301 glDisable(GL_TEXTURE_2D);
1302 gui_paint_rect(id, 0, alt);
1308 config_pop_matrix();
1313 /*---------------------------------------------------------------------------*/
1315 void gui_dump(int id, int d)
1323 switch (widget[id].type & GUI_TYPE)
1325 case GUI_HARRAY: type = "harray"; break;
1326 case GUI_VARRAY: type = "varray"; break;
1327 case GUI_HSTACK: type = "hstack"; break;
1328 case GUI_VSTACK: type = "vstack"; break;
1329 case GUI_FILLER: type = "filler"; break;
1330 case GUI_IMAGE: type = "image"; break;
1331 case GUI_LABEL: type = "label"; break;
1332 case GUI_COUNT: type = "count"; break;
1333 case GUI_CLOCK: type = "clock"; break;
1336 for (i = 0; i < d; i++)
1339 printf("%04d %s\n", id, type);
1341 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1342 gui_dump(jd, d + 1);
1346 void gui_pulse(int id, float k)
1348 if (id) widget[id].scale = k;
1351 void gui_timer(int id, float dt)
1357 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1360 if (widget[id].scale - 1.0f < dt)
1361 widget[id].scale = 1.0f;
1363 widget[id].scale -= dt;
1367 int gui_point(int id, int x, int y)
1369 static int x_cache = 0;
1370 static int y_cache = 0;
1374 /* Reuse the last coordinates if (x,y) == (-1,-1) */
1377 return gui_point(id, x_cache, y_cache);
1382 /* Short-circuit check the current active widget. */
1384 jd = gui_search(active, x, y);
1386 /* If not still active, search the hierarchy for a new active widget. */
1389 jd = gui_search(id, x, y);
1391 /* If the active widget has changed, return the new active id. */
1393 if (jd == 0 || jd == active)
1399 void gui_focus(int i)
1409 int gui_token(int id)
1411 return id ? widget[id].token : 0;
1414 int gui_value(int id)
1416 return id ? widget[id].value : 0;
1419 void gui_toggle(int id)
1421 widget[id].value = widget[id].value ? 0 : 1;
1424 /*---------------------------------------------------------------------------*/
1426 static int gui_vert_test(int id, int jd)
1428 /* Determine whether widget id is in vertical contact with widget jd. */
1430 if (id && gui_hot(id) && jd && gui_hot(jd))
1432 int i0 = widget[id].x;
1433 int i1 = widget[id].x + widget[id].w;
1434 int j0 = widget[jd].x;
1435 int j1 = widget[jd].x + widget[jd].w;
1437 /* Is widget id's top edge is in contact with jd's bottom edge? */
1439 if (widget[id].y + widget[id].h == widget[jd].y)
1441 /* Do widgets id and jd overlap horizontally? */
1443 if (j0 <= i0 && i0 < j1) return 1;
1444 if (j0 < i1 && i1 <= j1) return 1;
1445 if (i0 <= j0 && j0 < i1) return 1;
1446 if (i0 < j1 && j1 <= i1) return 1;
1452 static int gui_horz_test(int id, int jd)
1454 /* Determine whether widget id is in horizontal contact with widget jd. */
1456 if (id && gui_hot(id) && jd && gui_hot(jd))
1458 int i0 = widget[id].y;
1459 int i1 = widget[id].y + widget[id].h;
1460 int j0 = widget[jd].y;
1461 int j1 = widget[jd].y + widget[jd].h;
1463 /* Is widget id's right edge in contact with jd's left edge? */
1465 if (widget[id].x + widget[id].w == widget[jd].x)
1467 /* Do widgets id and jd overlap vertically? */
1469 if (j0 <= i0 && i0 < j1) return 1;
1470 if (j0 < i1 && i1 <= j1) return 1;
1471 if (i0 <= j0 && j0 < i1) return 1;
1472 if (i0 < j1 && j1 <= i1) return 1;
1478 /*---------------------------------------------------------------------------*/
1480 static int gui_stick_L(int id, int dd)
1484 /* Find a widget to the left of widget dd. */
1486 if (gui_horz_test(id, dd))
1489 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1490 if ((kd = gui_stick_L(jd, dd)))
1496 static int gui_stick_R(int id, int dd)
1500 /* Find a widget to the right of widget dd. */
1502 if (gui_horz_test(dd, id))
1505 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1506 if ((kd = gui_stick_R(jd, dd)))
1512 static int gui_stick_D(int id, int dd)
1516 /* Find a widget below widget dd. */
1518 if (gui_vert_test(id, dd))
1521 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1522 if ((kd = gui_stick_D(jd, dd)))
1528 static int gui_stick_U(int id, int dd)
1532 /* Find a widget above widget dd. */
1534 if (gui_vert_test(dd, id))
1537 for (jd = widget[id].car; jd; jd = widget[jd].cdr)
1538 if ((kd = gui_stick_U(jd, dd)))
1544 /*---------------------------------------------------------------------------*/
1546 static int gui_wrap_L(int id, int dd)
1550 if ((jd = gui_stick_L(id, dd)) == 0)
1551 for (jd = dd; (kd = gui_stick_R(id, jd)); jd = kd)
1557 static int gui_wrap_R(int id, int dd)
1561 if ((jd = gui_stick_R(id, dd)) == 0)
1562 for (jd = dd; (kd = gui_stick_L(id, jd)); jd = kd)
1568 static int gui_wrap_U(int id, int dd)
1572 if ((jd = gui_stick_U(id, dd)) == 0)
1573 for (jd = dd; (kd = gui_stick_D(id, jd)); jd = kd)
1579 static int gui_wrap_D(int id, int dd)
1583 if ((jd = gui_stick_D(id, dd)) == 0)
1584 for (jd = dd; (kd = gui_stick_U(id, jd)); jd = kd)
1590 /*---------------------------------------------------------------------------*/
1592 int gui_stick(int id, int x, int y)
1594 /* Flag the axes to prevent uncontrolled scrolling. */
1596 static int xflag = 1;
1597 static int yflag = 1;
1601 /* Find a new active widget in the direction of joystick motion. */
1603 if (x && -JOY_MID <= x && x <= +JOY_MID)
1605 else if (x < -JOY_MID && xflag && (jd = gui_wrap_L(id, active)))
1607 else if (x > +JOY_MID && xflag && (jd = gui_wrap_R(id, active)))
1610 if (y && -JOY_MID <= y && y <= +JOY_MID)
1612 else if (y < -JOY_MID && yflag && (jd = gui_wrap_U(id, active)))
1614 else if (y > +JOY_MID && yflag && (jd = gui_wrap_D(id, active)))
1617 /* If the active widget has changed, return the new active id. */
1619 if (jd == 0 || jd == active)
1625 /*---------------------------------------------------------------------------*/