imtk

view src/draw.c @ 19:11da537aeba6

blah
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 26 Apr 2011 22:53:21 +0300
parents 737e9047d9c9
children
line source
1 #include <string.h>
2 #include <math.h>
3 #include <assert.h>
4 #include "draw.h"
5 #include "imtk.h"
7 #define COLOR_MASK 0xff
9 /* default colors, can be changed with imtk_set_color */
10 static float colors[][4] = {
11 {0.0, 0.0, 0.0, 1.0}, /* text color */
12 {0.75, 0.75, 0.75, 1.0}, /* top color */
13 {0.56, 0.56, 0.56, 1.0}, /* bot color */
14 {0.9, 0.9, 0.9, 1.0}, /* lit bevel */
15 {0.3, 0.3, 0.3, 1.0}, /* shadowed bevel */
16 {0.8, 0.25, 0.18, 1.0}, /* cursor color */
17 {0.68, 0.85, 1.3, 1.0}, /* selection color */
18 {0.75, 0.1, 0.095, 1.0} /* check color */
19 };
21 static float focus_factor = 1.15;
22 static float press_factor = 0.8;
23 static float alpha = 1.0;
24 static float bevel = 1.0;
26 void imtk_set_color(unsigned int col, float r, float g, float b, float a)
27 {
28 int idx = col & COLOR_MASK;
29 assert(idx >= 0 && idx < sizeof colors / sizeof *colors);
31 colors[idx][0] = r;
32 colors[idx][1] = g;
33 colors[idx][2] = b;
34 colors[idx][3] = a;
35 }
37 float *imtk_get_color(unsigned int col)
38 {
39 static float ret[4];
40 int idx = col & COLOR_MASK;
42 memcpy(ret, colors + idx, sizeof ret);
43 if(col & IMTK_FOCUS_BIT) {
44 ret[0] *= focus_factor;
45 ret[1] *= focus_factor;
46 ret[2] *= focus_factor;
47 }
48 if(col & IMTK_PRESS_BIT) {
49 ret[0] *= press_factor;
50 ret[1] *= press_factor;
51 ret[2] *= press_factor;
52 }
53 if(col & IMTK_SEL_BIT) {
54 ret[0] *= colors[IMTK_SELECTION_COLOR][0];
55 ret[1] *= colors[IMTK_SELECTION_COLOR][1];
56 ret[2] *= colors[IMTK_SELECTION_COLOR][2];
57 }
58 ret[3] *= alpha;
59 return ret;
60 }
62 void imtk_set_alpha(float a)
63 {
64 alpha = a;
65 }
67 float imtk_get_alpha(void)
68 {
69 return alpha;
70 }
72 void imtk_set_bevel_width(float b)
73 {
74 bevel = b;
75 }
77 float imtk_get_bevel_width(void)
78 {
79 return bevel;
80 }
82 void imtk_set_focus_factor(float fact)
83 {
84 focus_factor = fact;
85 }
87 float imtk_get_focus_factor(void)
88 {
89 return focus_factor;
90 }
92 void imtk_set_press_factor(float fact)
93 {
94 press_factor = fact;
95 }
97 float imtk_get_press_factor(void)
98 {
99 return press_factor;
100 }
102 void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot)
103 {
104 glBegin(GL_QUADS);
105 if(ctop) {
106 glColor4fv(ctop);
107 }
108 glVertex2f(x, y);
109 glVertex2f(x + w, y);
111 if(cbot) {
112 glColor4fv(cbot);
113 }
114 glVertex2f(x + w, y + h);
115 glVertex2f(x, y + h);
116 glEnd();
117 }
119 void imtk_draw_frame(int x, int y, int w, int h, int style)
120 {
121 float tcol[4], bcol[4];
122 float b = imtk_get_bevel_width();
124 if(!b) {
125 return;
126 }
128 x -= b;
129 y -= b;
130 w += b * 2;
131 h += b * 2;
133 switch(style) {
134 case FRAME_INSET:
135 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
136 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
137 break;
139 case FRAME_OUTSET:
140 default:
141 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
142 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
143 }
145 glBegin(GL_QUADS);
146 glColor4fv(tcol);
147 glVertex2f(x, y);
148 glVertex2f(x + b, y + b);
149 glVertex2f(x + w - b, y + b);
150 glVertex2f(x + w, y);
152 glVertex2f(x + b, y + b);
153 glVertex2f(x, y);
154 glVertex2f(x, y + h);
155 glVertex2f(x + b, y + h - b);
157 glColor4fv(bcol);
158 glVertex2f(x + b, y + h - b);
159 glVertex2f(x + w - b, y + h - b);
160 glVertex2f(x + w, y + h);
161 glVertex2f(x, y + h);
163 glVertex2f(x + w - b, y + b);
164 glVertex2f(x + w, y);
165 glVertex2f(x + w, y + h);
166 glVertex2f(x + w - b, y + h - b);
167 glEnd();
168 }
170 void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot)
171 {
172 int i;
173 float t, dtheta, theta = 0.0;
174 float color[4];
175 float cx = (float)x;
176 float cy = (float)y;
178 subdiv += 3;
179 dtheta = 2.0 * M_PI / subdiv;
181 color[0] = (ctop[0] + cbot[0]) * 0.5;
182 color[1] = (ctop[1] + cbot[1]) * 0.5;
183 color[2] = (ctop[2] + cbot[2]) * 0.5;
184 color[3] = (ctop[3] + cbot[3]) * 0.5;
186 glBegin(GL_TRIANGLE_FAN);
187 glColor4fv(color);
188 glVertex2f(cx, cy);
190 for(i=0; i<=subdiv; i++) {
191 float vx, vy;
193 vx = cos(theta);
194 vy = sin(theta);
195 theta += dtheta;
197 t = (vy + 1.0) / 2.0;
198 color[0] = ctop[0] + (cbot[0] - ctop[0]) * t;
199 color[1] = ctop[1] + (cbot[1] - ctop[1]) * t;
200 color[2] = ctop[2] + (cbot[2] - ctop[2]) * t;
201 color[3] = ctop[3] + (cbot[3] - ctop[3]) * t;
203 glColor4fv(color);
204 glVertex2f(cx + vx * rad, cy + vy * rad);
205 }
206 glEnd();
207 }
209 void imtk_draw_disc_frame(int x, int y, float inner, float outer, int subdiv, int style)
210 {
211 int i;
212 float t, dtheta, theta = 0.0;
213 float color[4], tcol[4], bcol[4];
214 float cx = (float)x;
215 float cy = (float)y;
217 switch(style) {
218 case FRAME_INSET:
219 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
220 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
221 break;
223 case FRAME_OUTSET:
224 default:
225 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
226 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
227 }
229 subdiv += 3;
230 dtheta = 2.0 * M_PI / subdiv;
232 glBegin(GL_QUAD_STRIP);
234 for(i=0; i<=subdiv; i++) {
235 float vx, vy;
237 vx = cos(theta);
238 vy = sin(theta);
240 t = (vy + 1.0) / 2.0;
241 color[0] = tcol[0] + (bcol[0] - tcol[0]) * t;
242 color[1] = tcol[1] + (bcol[1] - tcol[1]) * t;
243 color[2] = tcol[2] + (bcol[2] - tcol[2]) * t;
244 color[3] = tcol[3] + (bcol[3] - tcol[3]) * t;
246 vx = cos(theta - M_PI / 4.0);
247 vy = sin(theta - M_PI / 4.0);
248 theta += dtheta;
250 glColor4fv(color);
251 glVertex2f(cx + vx * inner, cy + vy * inner);
252 glVertex2f(cx + vx * outer, cy + vy * outer);
253 }
254 glEnd();
255 }
257 void imtk_draw_string(int x, int y, const char *str)
258 {
259 glRasterPos2i(x, y);
260 while(*str) {
261 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
262 }
263 }
265 int imtk_string_size(const char *str)
266 {
267 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
268 }