glamtk

view src/imtk.c @ 0:b04d49e4599c

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 30 Dec 2010 05:22:14 +0200
parents
children dfbd12d1f566
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #ifndef __APPLE__
4 #include <GL/glut.h>
5 #else
6 #include <GLUT/glut.h>
7 #endif
8 #include "imtk.h"
10 static int scr_width = 1, scr_height = 1;
11 static int mousex, mousey, mouse_bnmask;
12 static int active = -1, hot = -1;
14 static void set_active(int id);
15 static int set_hot(int id);
16 static int hit_test(int x, int y, int w, int h);
18 static void draw_button(int id, int x, int y, const char *label, int focused);
19 static void calc_button_size(const char *label, int *wret, int *hret);
20 static void draw_string(int x, int y, const char *str);
21 static int string_size(const char *str);
23 void imtk_inp_key(int key, int state)
24 {
25 glutPostRedisplay();
26 }
28 void imtk_inp_mouse(int bn, int state)
29 {
30 if(state == IMTK_DOWN) {
31 mouse_bnmask |= 1 << bn;
32 } else {
33 mouse_bnmask &= ~(1 << bn);
34 }
35 glutPostRedisplay();
36 }
38 void imtk_inp_motion(int x, int y)
39 {
40 mousex = x;
41 mousey = y;
43 glutPostRedisplay();
44 }
46 void imtk_inp_reshape(int x, int y)
47 {
48 scr_width = x;
49 scr_height = y;
50 }
52 void imtk_begin(void)
53 {
54 glMatrixMode(GL_PROJECTION);
55 glPushMatrix();
56 glLoadIdentity();
57 glTranslatef(-1, 1, 0);
58 glScalef(2.0 / scr_width, -2.0 / scr_height, 1.0);
59 }
61 void imtk_end(void)
62 {
63 glMatrixMode(GL_PROJECTION);
64 glPopMatrix();
65 }
67 int imtk_button(int id, const char *label, int x, int y)
68 {
69 int w, h, res = 0;
70 int over = 0;
72 assert(id >= 0);
74 calc_button_size(label, &w, &h);
76 if(hit_test(x, y, w, h)) {
77 if(set_hot(id)) {
78 glutPostRedisplay();
79 }
80 over = 1;
81 }
84 if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
85 if(over) {
86 set_active(id);
87 }
88 } else { /* mouse button up */
89 if(active == id) {
90 set_active(-1);
91 if(hot == id && over) {
92 res = 1;
93 }
94 }
95 }
97 draw_button(id, x, y, label, over);
98 return res;
99 }
101 static void set_active(int id)
102 {
103 active = id;
104 }
106 static int set_hot(int id)
107 {
108 if(active == -1) {
109 hot = id;
110 return 1;
111 }
112 return 0;
113 }
115 static int hit_test(int x, int y, int w, int h)
116 {
117 return mousex >= x && mousex < (x + w) &&
118 mousey >= y && mousey < (y + h);
119 }
121 static void draw_button(int id, int x, int y, const char *label, int focused)
122 {
123 int width, height;
125 calc_button_size(label, &width, &height);
127 glBegin(GL_QUADS);
128 if(focused) {
129 glColor3f(0.85, 0.85, 0.85);
130 } else {
131 glColor3f(0.7, 0.7, 0.7);
132 }
133 glVertex2f(x, y);
134 glVertex2f(x + width, y);
135 glVertex2f(x + width, y + height);
136 glVertex2f(x, y + height);
137 glEnd();
139 glColor3f(0, 0, 0);
140 draw_string(x + 20, y + 15, label);
141 }
143 static void calc_button_size(const char *label, int *wret, int *hret)
144 {
145 int strsz = string_size(label);
146 if(wret) *wret = strsz + 40;
147 if(hret) *hret = 20;
148 }
150 static void draw_string(int x, int y, const char *str)
151 {
152 glRasterPos2i(x, y);
153 while(*str) {
154 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *str++);
155 }
156 }
158 static int string_size(const char *str)
159 {
160 return glutBitmapLength(GLUT_BITMAP_HELVETICA_10, str);
161 }