imtk
view src/draw.c @ 15:6893b4dca5a3
added gradients on the selection
author | John Tsiombikas <nuclear@siggraph.org> |
---|---|
date | Tue, 19 Apr 2011 05:10:09 +0300 |
parents | df2bc9406561 |
children | 737e9047d9c9 |
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.68, 0.85, 1.3, 1.0}, /* selection color */
17 {0.75, 0.1, 0.095, 1.0} /* check color */
18 };
20 static float focus_factor = 1.15;
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 if(col & IMTK_SEL_BIT) {
53 ret[0] *= colors[IMTK_SELECTION_COLOR][0];
54 ret[1] *= colors[IMTK_SELECTION_COLOR][1];
55 ret[2] *= colors[IMTK_SELECTION_COLOR][2];
56 }
57 ret[3] *= alpha;
58 return ret;
59 }
61 void imtk_set_alpha(float a)
62 {
63 alpha = a;
64 }
66 float imtk_get_alpha(void)
67 {
68 return alpha;
69 }
71 void imtk_set_bevel_width(float b)
72 {
73 bevel = b;
74 }
76 float imtk_get_bevel_width(void)
77 {
78 return bevel;
79 }
81 void imtk_set_focus_factor(float fact)
82 {
83 focus_factor = fact;
84 }
86 float imtk_get_focus_factor(void)
87 {
88 return focus_factor;
89 }
91 void imtk_set_press_factor(float fact)
92 {
93 press_factor = fact;
94 }
96 float imtk_get_press_factor(void)
97 {
98 return press_factor;
99 }
101 void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot)
102 {
103 glBegin(GL_QUADS);
104 if(ctop) {
105 glColor4fv(ctop);
106 }
107 glVertex2f(x, y);
108 glVertex2f(x + w, y);
110 if(cbot) {
111 glColor4fv(cbot);
112 }
113 glVertex2f(x + w, y + h);
114 glVertex2f(x, y + h);
115 glEnd();
116 }
118 void imtk_draw_frame(int x, int y, int w, int h, int style)
119 {
120 float tcol[4], bcol[4];
121 float b = imtk_get_bevel_width();
123 if(!b) {
124 return;
125 }
127 x -= b;
128 y -= b;
129 w += b * 2;
130 h += b * 2;
132 switch(style) {
133 case FRAME_INSET:
134 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
135 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
136 break;
138 case FRAME_OUTSET:
139 default:
140 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
141 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
142 }
144 glBegin(GL_QUADS);
145 glColor4fv(tcol);
146 glVertex2f(x, y);
147 glVertex2f(x + b, y + b);
148 glVertex2f(x + w - b, y + b);
149 glVertex2f(x + w, y);
151 glVertex2f(x + b, y + b);
152 glVertex2f(x, y);
153 glVertex2f(x, y + h);
154 glVertex2f(x + b, y + h - b);
156 glColor4fv(bcol);
157 glVertex2f(x + b, y + h - b);
158 glVertex2f(x + w - b, y + h - b);
159 glVertex2f(x + w, y + h);
160 glVertex2f(x, y + h);
162 glVertex2f(x + w - b, y + b);
163 glVertex2f(x + w, y);
164 glVertex2f(x + w, y + h);
165 glVertex2f(x + w - b, y + h - b);
166 glEnd();
167 }
169 void imtk_draw_string(int x, int y, const char *str)
170 {
171 glRasterPos2i(x, y);
172 while(*str) {
173 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
174 }
175 }
177 int imtk_string_size(const char *str)
178 {
179 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
180 }