dbg gui, imtk text drawing
[demo_prior] / src / imtk / draw.c
1 #include <string.h>
2 #include <math.h>
3 #include <assert.h>
4 #include "draw.h"
5 #include "imtk.h"
6 #include "drawtext.h"
7 #include "demo.h"
8
9 #define COLOR_MASK      0xff
10
11 /* default colors, can be changed with imtk_set_color */
12 static float colors[][4] = {
13         {0.0, 0.0, 0.0, 1.0},           /* text color */
14         {0.75, 0.75, 0.75, 1.0},        /* top color */
15         {0.56, 0.56, 0.56, 1.0},        /* bot color */
16         {0.9, 0.9, 0.9, 1.0},           /* lit bevel */
17         {0.3, 0.3, 0.3, 1.0},           /* shadowed bevel */
18         {0.8, 0.25, 0.18, 1.0},         /* cursor color */
19         {0.68, 0.85, 1.3, 1.0},         /* selection color */
20         {0.75, 0.1, 0.095, 1.0}         /* check color */
21 };
22
23 static float focus_factor = 1.15;
24 static float press_factor = 0.8;
25 static float alpha = 1.0;
26 static float bevel = 1.0;
27
28 void imtk_set_color(unsigned int col, float r, float g, float b, float a)
29 {
30         int idx = col & COLOR_MASK;
31         assert(idx >= 0 && idx < sizeof colors / sizeof *colors);
32
33         colors[idx][0] = r;
34         colors[idx][1] = g;
35         colors[idx][2] = b;
36         colors[idx][3] = a;
37 }
38
39 float *imtk_get_color(unsigned int col)
40 {
41         static float ret[4];
42         int idx = col & COLOR_MASK;
43
44         memcpy(ret, colors + idx, sizeof ret);
45         if(col & IMTK_FOCUS_BIT) {
46                 ret[0] *= focus_factor;
47                 ret[1] *= focus_factor;
48                 ret[2] *= focus_factor;
49         }
50         if(col & IMTK_PRESS_BIT) {
51                 ret[0] *= press_factor;
52                 ret[1] *= press_factor;
53                 ret[2] *= press_factor;
54         }
55         if(col & IMTK_SEL_BIT) {
56                 ret[0] *= colors[IMTK_SELECTION_COLOR][0];
57                 ret[1] *= colors[IMTK_SELECTION_COLOR][1];
58                 ret[2] *= colors[IMTK_SELECTION_COLOR][2];
59         }
60         ret[3] *= alpha;
61         return ret;
62 }
63
64 void imtk_set_alpha(float a)
65 {
66         alpha = a;
67 }
68
69 float imtk_get_alpha(void)
70 {
71         return alpha;
72 }
73
74 void imtk_set_bevel_width(float b)
75 {
76         bevel = b;
77 }
78
79 float imtk_get_bevel_width(void)
80 {
81         return bevel;
82 }
83
84 void imtk_set_focus_factor(float fact)
85 {
86         focus_factor = fact;
87 }
88
89 float imtk_get_focus_factor(void)
90 {
91         return focus_factor;
92 }
93
94 void imtk_set_press_factor(float fact)
95 {
96         press_factor = fact;
97 }
98
99 float imtk_get_press_factor(void)
100 {
101         return press_factor;
102 }
103
104 void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot)
105 {
106         glBegin(GL_QUADS);
107         if(ctop) {
108                 glColor4fv(ctop);
109         }
110         glVertex2f(x, y);
111         glVertex2f(x + w, y);
112
113         if(cbot) {
114                 glColor4fv(cbot);
115         }
116         glVertex2f(x + w, y + h);
117         glVertex2f(x, y + h);
118         glEnd();
119 }
120
121 void imtk_draw_frame(int x, int y, int w, int h, int style)
122 {
123         float tcol[4], bcol[4];
124         float b = imtk_get_bevel_width();
125
126         if(!b) {
127                 return;
128         }
129
130         x -= b;
131         y -= b;
132         w += b * 2;
133         h += b * 2;
134
135         switch(style) {
136         case FRAME_INSET:
137                 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
138                 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
139                 break;
140
141         case FRAME_OUTSET:
142         default:
143                 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
144                 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
145         }
146
147         glBegin(GL_QUADS);
148         glColor4fv(tcol);
149         glVertex2f(x, y);
150         glVertex2f(x + b, y + b);
151         glVertex2f(x + w - b, y + b);
152         glVertex2f(x + w, y);
153
154         glVertex2f(x + b, y + b);
155         glVertex2f(x, y);
156         glVertex2f(x, y + h);
157         glVertex2f(x + b, y + h - b);
158
159         glColor4fv(bcol);
160         glVertex2f(x + b, y + h - b);
161         glVertex2f(x + w - b, y + h - b);
162         glVertex2f(x + w, y + h);
163         glVertex2f(x, y + h);
164
165         glVertex2f(x + w - b, y + b);
166         glVertex2f(x + w, y);
167         glVertex2f(x + w, y + h);
168         glVertex2f(x + w - b, y + h - b);
169         glEnd();
170 }
171
172 void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot)
173 {
174         int i;
175         float t, dtheta, theta = 0.0;
176         float color[4];
177         float cx = (float)x;
178         float cy = (float)y;
179
180         subdiv += 3;
181         dtheta = 2.0 * M_PI / subdiv;
182
183         color[0] = (ctop[0] + cbot[0]) * 0.5;
184         color[1] = (ctop[1] + cbot[1]) * 0.5;
185         color[2] = (ctop[2] + cbot[2]) * 0.5;
186         color[3] = (ctop[3] + cbot[3]) * 0.5;
187
188         glBegin(GL_TRIANGLE_FAN);
189         glColor4fv(color);
190         glVertex2f(cx, cy);
191
192         for(i=0; i<=subdiv; i++) {
193                 float vx, vy;
194
195                 vx = cos(theta);
196                 vy = sin(theta);
197                 theta += dtheta;
198
199                 t = (vy + 1.0) / 2.0;
200                 color[0] = ctop[0] + (cbot[0] - ctop[0]) * t;
201                 color[1] = ctop[1] + (cbot[1] - ctop[1]) * t;
202                 color[2] = ctop[2] + (cbot[2] - ctop[2]) * t;
203                 color[3] = ctop[3] + (cbot[3] - ctop[3]) * t;
204
205                 glColor4fv(color);
206                 glVertex2f(cx + vx * rad, cy + vy * rad);
207         }
208         glEnd();
209 }
210
211 void imtk_draw_disc_frame(int x, int y, float inner, float outer, int subdiv, int style)
212 {
213         int i;
214         float t, dtheta, theta = 0.0;
215         float color[4], tcol[4], bcol[4];
216         float cx = (float)x;
217         float cy = (float)y;
218
219         switch(style) {
220         case FRAME_INSET:
221                 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
222                 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
223                 break;
224
225         case FRAME_OUTSET:
226         default:
227                 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
228                 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
229         }
230
231         subdiv += 3;
232         dtheta = 2.0 * M_PI / subdiv;
233
234         glBegin(GL_QUAD_STRIP);
235
236         for(i=0; i<=subdiv; i++) {
237                 float vx, vy;
238
239                 vx = cos(theta);
240                 vy = sin(theta);
241
242                 t = (vy + 1.0) / 2.0;
243                 color[0] = tcol[0] + (bcol[0] - tcol[0]) * t;
244                 color[1] = tcol[1] + (bcol[1] - tcol[1]) * t;
245                 color[2] = tcol[2] + (bcol[2] - tcol[2]) * t;
246                 color[3] = tcol[3] + (bcol[3] - tcol[3]) * t;
247
248                 vx = cos(theta - M_PI / 4.0);
249                 vy = sin(theta - M_PI / 4.0);
250                 theta += dtheta;
251
252                 glColor4fv(color);
253                 glVertex2f(cx + vx * inner, cy + vy * inner);
254                 glVertex2f(cx + vx * outer, cy + vy * outer);
255         }
256         glEnd();
257 }
258
259 void imtk_draw_string(int x, int y, const char *str)
260 {
261         dtx_use_font(fnt_ui, fnt_ui_size);
262
263         glMatrixMode(GL_MODELVIEW);
264         glPushMatrix();
265         glTranslatef(x, y + dtx_line_height() * 0.2, 0);
266         glScalef(1, -1, 1);
267
268         glColor4fv(colors[IMTK_TEXT_COLOR]);
269         dtx_string(str);
270
271         glPopMatrix();
272 }
273
274 int imtk_string_size(const char *str)
275 {
276         dtx_use_font(fnt_ui, fnt_ui_size);
277         return dtx_string_width(str);
278 }