imtk

view src/draw.c @ 18:737e9047d9c9

added radio group
author John Tsiombikas <nuclear@siggraph.org>
date Mon, 25 Apr 2011 08:54:05 +0300
parents 6893b4dca5a3
children 11da537aeba6
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_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot)
120 {
121 int i;
122 float t, dtheta, theta = 0.0;
123 float color[4];
124 float cx = (float)x;
125 float cy = (float)y;
127 subdiv += 3;
128 dtheta = 2.0 * M_PI / subdiv;
130 color[0] = (ctop[0] + cbot[0]) * 0.5;
131 color[1] = (ctop[1] + cbot[1]) * 0.5;
132 color[2] = (ctop[2] + cbot[2]) * 0.5;
133 color[3] = (ctop[3] + cbot[3]) * 0.5;
135 glBegin(GL_TRIANGLE_FAN);
136 glColor4fv(color);
137 glVertex2f(cx, cy);
139 for(i=0; i<=subdiv; i++) {
140 float vx, vy;
142 vx = cx + cos(theta) * rad;
143 vy = cy + sin(theta) * rad;
144 theta += dtheta;
146 t = (vy - (cy - rad)) / (2.0 * rad);
147 color[0] = ctop[0] + (cbot[0] - ctop[0]) * t;
148 color[1] = ctop[1] + (cbot[1] - ctop[1]) * t;
149 color[2] = ctop[2] + (cbot[2] - ctop[2]) * t;
150 color[3] = ctop[3] + (cbot[3] - ctop[3]) * t;
152 glColor4fv(color);
153 glVertex2f(vx, vy);
154 }
155 glEnd();
156 }
158 void imtk_draw_frame(int x, int y, int w, int h, int style)
159 {
160 float tcol[4], bcol[4];
161 float b = imtk_get_bevel_width();
163 if(!b) {
164 return;
165 }
167 x -= b;
168 y -= b;
169 w += b * 2;
170 h += b * 2;
172 switch(style) {
173 case FRAME_INSET:
174 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
175 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
176 break;
178 case FRAME_OUTSET:
179 default:
180 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
181 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
182 }
184 glBegin(GL_QUADS);
185 glColor4fv(tcol);
186 glVertex2f(x, y);
187 glVertex2f(x + b, y + b);
188 glVertex2f(x + w - b, y + b);
189 glVertex2f(x + w, y);
191 glVertex2f(x + b, y + b);
192 glVertex2f(x, y);
193 glVertex2f(x, y + h);
194 glVertex2f(x + b, y + h - b);
196 glColor4fv(bcol);
197 glVertex2f(x + b, y + h - b);
198 glVertex2f(x + w - b, y + h - b);
199 glVertex2f(x + w, y + h);
200 glVertex2f(x, y + h);
202 glVertex2f(x + w - b, y + b);
203 glVertex2f(x + w, y);
204 glVertex2f(x + w, y + h);
205 glVertex2f(x + w - b, y + h - b);
206 glEnd();
207 }
209 void imtk_draw_string(int x, int y, const char *str)
210 {
211 glRasterPos2i(x, y);
212 while(*str) {
213 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
214 }
215 }
217 int imtk_string_size(const char *str)
218 {
219 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
220 }