imtk

view src/draw.c @ 14:df2bc9406561

added gradients
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 19 Apr 2011 03:01:46 +0300
parents 9c7987064bb0
children 6893b4dca5a3
line source
1 #include <string.h>
2 #include <assert.h>
3 #include "draw.h"
4 #include "imtk.h"
6 #define COLOR_MASK 0xff
8 /* default colors, can be changed with imtk_set_color */
9 static float colors[][4] = {
10 {0.0, 0.0, 0.0, 1.0}, /* text color */
11 {0.75, 0.75, 0.75, 1.0}, /* top color */
12 {0.56, 0.56, 0.56, 1.0}, /* bot color */
13 {0.9, 0.9, 0.9, 1.0}, /* lit bevel */
14 {0.3, 0.3, 0.3, 1.0}, /* shadowed bevel */
15 {0.8, 0.25, 0.18, 1.0}, /* cursor color */
16 {0.4, 0.5, 0.9, 1.0}, /* selection color */
17 {0.75, 0.1, 0.095, 1.0} /* check color */
18 };
20 static float focus_factor = 1.1;
21 static float press_factor = 0.8;
22 static float alpha = 1.0;
23 static float bevel = 1.0;
25 void imtk_set_color(unsigned int col, float r, float g, float b, float a)
26 {
27 int idx = col & COLOR_MASK;
28 assert(idx >= 0 && idx < sizeof colors / sizeof *colors);
30 colors[idx][0] = r;
31 colors[idx][1] = g;
32 colors[idx][2] = b;
33 colors[idx][3] = a;
34 }
36 float *imtk_get_color(unsigned int col)
37 {
38 static float ret[4];
39 int idx = col & COLOR_MASK;
41 memcpy(ret, colors + idx, sizeof ret);
42 if(col & IMTK_FOCUS_BIT) {
43 ret[0] *= focus_factor;
44 ret[1] *= focus_factor;
45 ret[2] *= focus_factor;
46 }
47 if(col & IMTK_PRESS_BIT) {
48 ret[0] *= press_factor;
49 ret[1] *= press_factor;
50 ret[2] *= press_factor;
51 }
52 ret[3] *= alpha;
53 return ret;
54 }
56 void imtk_set_alpha(float a)
57 {
58 alpha = a;
59 }
61 float imtk_get_alpha(void)
62 {
63 return alpha;
64 }
66 void imtk_set_bevel_width(float b)
67 {
68 bevel = b;
69 }
71 float imtk_get_bevel_width(void)
72 {
73 return bevel;
74 }
76 void imtk_set_focus_factor(float fact)
77 {
78 focus_factor = fact;
79 }
81 float imtk_get_focus_factor(void)
82 {
83 return focus_factor;
84 }
86 void imtk_set_press_factor(float fact)
87 {
88 press_factor = fact;
89 }
91 float imtk_get_press_factor(void)
92 {
93 return press_factor;
94 }
96 void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot)
97 {
98 glBegin(GL_QUADS);
99 if(ctop) {
100 glColor4fv(ctop);
101 }
102 glVertex2f(x, y);
103 glVertex2f(x + w, y);
105 if(cbot) {
106 glColor4fv(cbot);
107 }
108 glVertex2f(x + w, y + h);
109 glVertex2f(x, y + h);
110 glEnd();
111 }
113 void imtk_draw_frame(int x, int y, int w, int h, int style)
114 {
115 float tcol[4], bcol[4];
116 float b = imtk_get_bevel_width();
118 if(!b) {
119 return;
120 }
122 x -= b;
123 y -= b;
124 w += b * 2;
125 h += b * 2;
127 switch(style) {
128 case FRAME_INSET:
129 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
130 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
131 break;
133 case FRAME_OUTSET:
134 default:
135 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
136 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
137 }
139 glBegin(GL_QUADS);
140 glColor4fv(tcol);
141 glVertex2f(x, y);
142 glVertex2f(x + b, y + b);
143 glVertex2f(x + w - b, y + b);
144 glVertex2f(x + w, y);
146 glVertex2f(x + b, y + b);
147 glVertex2f(x, y);
148 glVertex2f(x, y + h);
149 glVertex2f(x + b, y + h - b);
151 glColor4fv(bcol);
152 glVertex2f(x + b, y + h - b);
153 glVertex2f(x + w - b, y + h - b);
154 glVertex2f(x + w, y + h);
155 glVertex2f(x, y + h);
157 glVertex2f(x + w - b, y + b);
158 glVertex2f(x + w, y);
159 glVertex2f(x + w, y + h);
160 glVertex2f(x + w - b, y + h - b);
161 glEnd();
162 }
164 void imtk_draw_string(int x, int y, const char *str)
165 {
166 glRasterPos2i(x, y);
167 while(*str) {
168 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
169 }
170 }
172 int imtk_string_size(const char *str)
173 {
174 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
175 }