glamtk

view src/imtk.c @ 9:4b1989ff2ef7

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 11 Apr 2011 03:59:03 +0300
parents cd00a5775373
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <stdarg.h>
6 #include <assert.h>
7 #ifndef __APPLE__
8 #include <GL/glut.h>
9 #else
10 #include <GLUT/glut.h>
11 #endif
12 #include "imtk.h"
13 #include "draw.h"
15 #define CHECKBOX_SIZE 14
16 #define TEXTBOX_SIZE 100
17 #define SLIDER_SIZE 100
18 #define THUMB_WIDTH 10
19 #define THUMB_HEIGHT 20
21 #define IMTK_BEVEL_SHAD_COLOR 3
22 #define IMTK_BEVEL_LIT_COLOR 3
24 enum {
25 FRAME_OUTSET,
26 FRAME_INSET
27 };
29 struct key_node {
30 int key;
31 struct key_node *next;
32 };
34 static void set_active(int id);
35 static int set_hot(int id);
36 static int hit_test(int x, int y, int w, int h);
38 static void draw_button(int id, const char *label, int x, int y);
39 static void calc_button_size(const char *label, int *wret, int *hret);
40 static void draw_checkbox(int id, const char *label, int x, int y, int state);
41 static void draw_textbox(int id, const char *text, int x, int y);
42 static void draw_slider(int id, float pos, float min, float max, int x, int y);
43 static void draw_progress(int id, float pos, int x, int y);
44 static void draw_string(int x, int y, const char *str);
45 static int string_size(const char *str);
46 static void draw_frame(int x, int y, int w, int h, int style);
49 /* default colors, can be changed with imtk_set_color */
50 static float colors[][4] = {
51 {0.0, 0.0, 0.0, 0.7}, /* text color */
52 {0.6, 0.6, 0.6, 0.7}, /* base color */
53 {0.7, 0.7, 0.7, 0.7}, /* focus color */
54 {0.0, 0.0, 0.0, 0.7} /* frame color */
55 };
57 static int scr_width = 1, scr_height = 1;
58 static int mousex, mousey, prevx, mouse_bnmask;
59 static int active = -1, hot = -1, input = -1, prev_active = -1;
61 static struct key_node *key_list, *key_tail;
64 void imtk_set_color(int col, float r, float g, float b)
65 {
66 assert(col >= 0 && col < sizeof colors / sizeof *colors);
68 colors[col][0] = r;
69 colors[col][1] = g;
70 colors[col][2] = b;
71 }
73 void imtk_inp_key(int key, int state)
74 {
75 if(state == IMTK_DOWN) {
76 struct key_node *node;
78 if(!(node = malloc(sizeof *node))) {
79 return;
80 }
81 node->key = key;
82 node->next = 0;
84 if(key_list) {
85 key_tail->next = node;
86 key_tail = node;
87 } else {
88 key_list = key_tail = node;
89 }
90 }
92 glutPostRedisplay();
93 }
95 void imtk_inp_mouse(int bn, int state)
96 {
97 if(state == IMTK_DOWN) {
98 mouse_bnmask |= 1 << bn;
99 } else {
100 mouse_bnmask &= ~(1 << bn);
101 }
102 glutPostRedisplay();
103 }
105 void imtk_inp_motion(int x, int y)
106 {
107 mousex = x;
108 mousey = y;
110 glutPostRedisplay();
111 }
113 void imtk_inp_reshape(int x, int y)
114 {
115 scr_width = x;
116 scr_height = y;
117 }
119 void imtk_begin(void)
120 {
121 glMatrixMode(GL_PROJECTION);
122 glPushMatrix();
123 glLoadIdentity();
124 glTranslatef(-1, 1, 0);
125 glScalef(2.0 / scr_width, -2.0 / scr_height, 1.0);
127 glMatrixMode(GL_MODELVIEW);
128 glPushMatrix();
129 glLoadIdentity();
131 glPushAttrib(GL_ENABLE_BIT);
132 glDisable(GL_DEPTH_TEST);
133 glDisable(GL_CULL_FACE);
134 glDisable(GL_LIGHTING);
135 }
137 void imtk_end(void)
138 {
139 glPopAttrib();
141 glMatrixMode(GL_PROJECTION);
142 glPopMatrix();
143 glMatrixMode(GL_MODELVIEW);
144 glPopMatrix();
145 }
147 int imtk_button(int id, const char *label, int x, int y)
148 {
149 int w, h, res = 0;
150 int over = 0;
152 assert(id >= 0);
154 calc_button_size(label, &w, &h);
156 if(hit_test(x, y, w, h)) {
157 set_hot(id);
158 over = 1;
159 }
161 if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
162 if(over) {
163 set_active(id);
164 }
165 } else { /* mouse button up */
166 if(active == id) {
167 set_active(-1);
168 if(hot == id && over) {
169 res = 1;
170 }
171 }
172 }
174 draw_button(id, label, x, y);
175 return res;
176 }
178 int imtk_checkbox(int id, const char *label, int x, int y, int state)
179 {
180 int sz = CHECKBOX_SIZE;
181 int over = 0;
183 assert(id >= 0);
185 if(hit_test(x, y, sz, sz)) {
186 set_hot(id);
187 over = 1;
188 }
190 if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
191 if(over) {
192 set_active(id);
193 }
194 } else { /* mouse button up */
195 if(active == id) {
196 set_active(-1);
197 if(hot == id && over) {
198 state = !state;
199 }
200 }
201 }
203 draw_checkbox(id, label, x, y, state);
204 return state;
205 }
207 void imtk_textbox(int id, char *textbuf, size_t buf_sz, int x, int y)
208 {
209 int len, over = 0;
211 assert(id >= 0);
213 if(hit_test(x, y, TEXTBOX_SIZE, 20)) {
214 set_hot(id);
215 over = 1;
216 }
218 if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
219 if(over) {
220 set_active(id);
221 }
222 } else {
223 if(active == id) {
224 set_active(-1);
225 if(hot == id && over) {
226 input = id;
227 }
228 }
229 }
231 if(input == id) {
232 len = strlen(textbuf);
233 while(key_list) {
234 struct key_node *node = key_list;
235 key_list = key_list->next;
237 if(isprint(node->key)) {
238 if(len < buf_sz) {
239 textbuf[len++] = (char)node->key;
240 }
241 } else {
242 switch(node->key) {
243 case '\b':
244 if(len > 0) {
245 textbuf[--len] = 0;
246 }
247 break;
249 default:
250 break;
251 }
252 }
254 free(node);
255 }
256 key_list = key_tail = 0;
257 }
259 draw_textbox(id, textbuf, x, y);
260 }
262 float imtk_slider(int id, float pos, float min, float max, int x, int y)
263 {
264 int thumb_x, thumb_y, over = 0;
265 float range = max - min;
267 assert(id >= 0);
269 pos = (pos - min) / range;
270 if(pos < 0.0) pos = 0.0;
271 if(pos > 1.0) pos = 1.0;
273 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
274 thumb_y = y - THUMB_HEIGHT / 2;
276 if(hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
277 set_hot(id);
278 over = 1;
279 }
281 if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
282 if(over && hot == id) {
283 if(active != id) {
284 prevx = mousex;
285 }
286 set_active(id);
287 }
288 } else {
289 if(active == id) {
290 set_active(-1);
291 }
292 }
294 if(active == id) {
295 float dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
296 pos += dx;
297 prevx = mousex;
299 if(pos < 0.0) pos = 0.0;
300 if(pos > 1.0) pos = 1.0;
301 }
303 draw_slider(id, pos, min, max, x, y);
304 return pos * range + min;
305 }
307 void imtk_progress(int id, float pos, int x, int y)
308 {
309 draw_progress(id, pos, x, y);
310 }
312 int imtk_listbox(int id, const char *list, int sel, int x, int y)
313 {
314 int i;
315 assert(id >= 0);
317 if(!list) {
318 return -1;
319 }
321 if(id & 1) {
322 id++;
323 }
325 for(i=0; *list; i++) {
326 if(imtk_button(id + i * 2 + 1, list, x, y + i * 20)) {
327 sel = i;
328 }
329 list += strlen(list) + 1;
330 }
331 return sel;
332 }
334 int imtk_combobox(int id, char *textbuf, size_t buf_sz, const char *list, int sel, int x, int y)
335 {
336 imtk_textbox(id + 1, textbuf, buf_sz, x, y);
337 imtk_button(id + 3, "V", x + TEXTBOX_SIZE, y);
339 if(prev_active == id + 3) {
340 sel = imtk_listbox(id + 5, list, sel, x, y + 20);
341 }
342 return sel;
343 }
345 char *imtk_create_list(const char *first, ...)
346 {
347 int sz;
348 char *buf, *item;
349 va_list ap;
351 if(!first) {
352 return 0;
353 }
355 sz = strlen(first) + 2;
356 if(!(buf = malloc(sz))) {
357 return 0;
358 }
359 memcpy(buf, first, sz - 2);
360 buf[sz - 1] = buf[sz - 2] = 0;
362 va_start(ap, first);
363 while((item = va_arg(ap, char*))) {
364 int len = strlen(item);
365 char *tmp = realloc(buf, sz + len + 1);
366 if(!tmp) {
367 free(buf);
368 return 0;
369 }
370 buf = tmp;
372 memcpy(buf + sz - 1, item, len);
373 sz += len + 1;
374 buf[sz - 1] = buf[sz - 2] = 0;
375 }
376 va_end(ap);
378 return buf;
379 }
381 void imtk_free_list(char *list)
382 {
383 free(list);
384 }
386 static void set_active(int id)
387 {
388 if(id == -1 || hot == id) {
389 prev_active = active;
390 active = id;
391 }
392 }
394 static int set_hot(int id)
395 {
396 if(active == -1) {
397 hot = id;
398 return 1;
399 }
400 return 0;
401 }
403 static int hit_test(int x, int y, int w, int h)
404 {
405 return mousex >= x && mousex < (x + w) &&
406 mousey >= y && mousey < (y + h);
407 }
409 static void draw_button(int id, const char *label, int x, int y)
410 {
411 int width, height;
413 calc_button_size(label, &width, &height);
415 if(hit_test(x, y, width, height)) {
416 imtk_draw_backgroundv(colors[IMTK_FOCUS_COLOR]);
417 } else {
418 imtk_draw_backgroundv(colors[IMTK_BASE_COLOR]);
419 }
420 imtk_draw_rect(x, y, width, height, 6);
422 glColor3fv(colors[IMTK_TEXT_COLOR]);
423 draw_string(x + 20, y + 15, label);
424 }
426 static void calc_button_size(const char *label, int *wret, int *hret)
427 {
428 int strsz = string_size(label);
429 if(wret) *wret = strsz + 40;
430 if(hret) *hret = 20;
431 }
433 static void draw_checkbox(int id, const char *label, int x, int y, int state)
434 {
435 static const int sz = CHECKBOX_SIZE;
436 static float v[][2] = {
437 {-0.2, 0.63}, /* 0 */
438 {0.121, 0.325}, /* 1 */
439 {0.15, 0.5}, /* 2 */
440 {0.28, 0.125}, /* 3 */
441 {0.38, 0.33}, /* 4 */
442 {0.42, -0.122}, /* 5 */
443 {0.58, 0.25}, /* 6 */
444 {0.72, 0.52}, /* 7 */
445 {0.625, 0.65}, /* 8 */
446 {0.89, 0.78}, /* 9 */
447 {0.875, 0.92}, /* 10 */
448 {1.13, 1.145} /* 11 */
449 };
450 #define TRI(a, b, c) (glVertex2fv(v[a]), glVertex2fv(v[b]), glVertex2fv(v[c]))
453 if(hit_test(x, y, sz, sz)) {
454 imtk_draw_backgroundv(colors[IMTK_FOCUS_COLOR]);
455 } else {
456 imtk_draw_backgroundv(colors[IMTK_BASE_COLOR]);
457 }
459 imtk_draw_rect(x, y, sz, sz, 3);
461 if(state) {
462 glMatrixMode(GL_MODELVIEW);
463 glPushMatrix();
464 glTranslatef(x, y + sz, 0);
465 glScalef(sz * 1.2, -sz * 1.3, 1);
467 glBegin(GL_TRIANGLES);
468 glColor4f(0.63, 0.078, 0.078, colors[IMTK_TEXT_COLOR][3]);
469 TRI(0, 1, 2);
470 TRI(1, 3, 2);
471 TRI(3, 4, 2);
472 TRI(3, 5, 4);
473 TRI(4, 5, 6);
474 TRI(4, 6, 7);
475 TRI(4, 7, 8);
476 TRI(8, 7, 9);
477 TRI(8, 9, 10);
478 TRI(10, 9, 11);
479 glEnd();
481 glPopMatrix();
482 }
484 glColor4fv(colors[IMTK_TEXT_COLOR]);
485 draw_string(x + sz + 5, y + sz - 2, label);
486 }
488 static void draw_textbox(int id, const char *text, int x, int y)
489 {
490 int strsz = string_size(text);
493 if(hit_test(x, y, TEXTBOX_SIZE, 20)) {
494 imtk_draw_backgroundv(colors[IMTK_FOCUS_COLOR]);
495 } else {
496 imtk_draw_backgroundv(colors[IMTK_BASE_COLOR]);
497 }
499 imtk_draw_rect(x, y, TEXTBOX_SIZE, 20, 3);
501 if(input == id) {
502 glBegin(GL_LINES);
503 glColor4f(1, 0, 0, colors[IMTK_TEXT_COLOR][3]);
504 glVertex2f(x + strsz + 2, y + 2);
505 glVertex2f(x + strsz + 2, y + 18);
506 glVertex2f(x + strsz + 3, y + 2);
507 glVertex2f(x + strsz + 3, y + 18);
508 glEnd();
509 }
511 glColor4fv(colors[IMTK_TEXT_COLOR]);
512 draw_string(x + 2, y + 15, text);
513 }
515 static void draw_slider(int id, float pos, float min, float max, int x, int y)
516 {
517 float range = max - min;
518 int thumb_x, thumb_y;
519 char buf[32];
521 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
522 thumb_y = y - THUMB_HEIGHT / 2;
524 /* draw trough */
525 imtk_draw_backgroundv(colors[IMTK_BASE_COLOR]);
526 imtk_draw_rect(x, y - 2, SLIDER_SIZE, 4, 3);
528 if(hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
529 imtk_draw_backgroundv(colors[IMTK_FOCUS_COLOR]);
530 } else {
531 imtk_draw_backgroundv(colors[IMTK_BASE_COLOR]);
532 }
534 /* draw handle */
535 imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, 5);
537 /* draw display */
538 sprintf(buf, "%.3f", pos * range + min);
539 glColor4fv(colors[IMTK_TEXT_COLOR]);
540 draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
541 }
543 static void draw_progress(int id, float pos, int x, int y)
544 {
545 int bar_size = SLIDER_SIZE * pos;
547 if(pos < 0.0) pos = 0.0;
548 if(pos > 1.0) pos = 1.0;
550 /* through */
551 imtk_draw_backgroundv(colors[IMTK_BASE_COLOR]);
552 imtk_draw_rect(x, y, SLIDER_SIZE, 15, 8);
554 if(pos > 0.0) {
555 /* bar */
556 imtk_draw_color(0, 0, 0, 0);
557 imtk_draw_background(0.2, 0.4, 1.0, colors[IMTK_BASE_COLOR][3]);
558 imtk_draw_rect(x, y, bar_size, 15, 8);
559 imtk_draw_colorv(colors[IMTK_FRAME_COLOR]);
560 }
561 }
563 static void draw_string(int x, int y, const char *str)
564 {
565 glRasterPos2i(x, y);
566 while(*str) {
567 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
568 }
569 }
571 static int string_size(const char *str)
572 {
573 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
574 }
576 static void draw_frame(int x, int y, int w, int h, int style)
577 {
578 float tcol[3], bcol[3];
580 switch(style) {
581 case FRAME_INSET:
582 memcpy(tcol, colors[IMTK_BEVEL_SHAD_COLOR], sizeof tcol);
583 memcpy(bcol, colors[IMTK_BEVEL_LIT_COLOR], sizeof bcol);
584 break;
586 case FRAME_OUTSET:
587 default:
588 memcpy(tcol, colors[IMTK_BEVEL_LIT_COLOR], sizeof tcol);
589 memcpy(bcol, colors[IMTK_BEVEL_SHAD_COLOR], sizeof bcol);
590 }
592 glBegin(GL_LINES);
593 glColor3fv(tcol);
594 glVertex2f(x, y + h);
595 glVertex2f(x, y);
596 glVertex2f(x, y);
597 glVertex2f(x + w, y);
598 glColor3fv(bcol);
599 glVertex2f(x + w, y);
600 glVertex2f(x + w, y + h);
601 glVertex2f(x + w, y + h);
602 glVertex2f(x, y + h);
603 glEnd();
604 }