imtk

view src/draw.c @ 13:9c7987064bb0

- fixed the frame drawing a bit - added global alpha value and various drawing parameters - backported the checkbox check mark from glamtk - fixed progress bar drawing so that the bevels of the trough and the bar won't overlap
author John Tsiombikas <nuclear@siggraph.org>
date Mon, 18 Apr 2011 06:15:46 +0300
parents 10604ff95527
children df2bc9406561
line source
1 #include <string.h>
2 #include <assert.h>
3 #include "draw.h"
4 #include "imtk.h"
6 /* default colors, can be changed with imtk_set_color */
7 static float colors[][4] = {
8 {0.0, 0.0, 0.0, 1.0}, /* text color */
9 {0.7, 0.7, 0.7, 1.0}, /* base color */
10 {0.85, 0.85, 0.85, 1.0}, /* focus color */
11 {1.0, 1.0, 1.0, 1.0}, /* lit bevel */
12 {0.3, 0.3, 0.3, 1.0}, /* shadowed bevel */
13 {0.8, 0.25, 0.18, 1.0}, /* cursor color */
14 {0.4, 0.5, 0.9, 1.0}, /* selection color */
15 {0.63, 0.078, 0.078, 1.0} /* check color */
16 };
18 static float alpha = 1.0;
19 static float bevel = 1.0;
21 void imtk_set_color(int col, float r, float g, float b, float a)
22 {
23 assert(col >= 0 && col < sizeof colors / sizeof *colors);
25 colors[col][0] = r;
26 colors[col][1] = g;
27 colors[col][2] = b;
28 colors[col][3] = a;
29 }
31 float *imtk_get_color(int col)
32 {
33 static float ret[4];
34 memcpy(ret, colors + col, sizeof ret);
35 ret[3] *= alpha;
36 return ret;
37 }
39 void imtk_set_alpha(float a)
40 {
41 alpha = a;
42 }
44 float imtk_get_alpha(void)
45 {
46 return alpha;
47 }
49 void imtk_set_bevel_width(float b)
50 {
51 bevel = b;
52 }
54 float imtk_get_bevel_width(void)
55 {
56 return bevel;
57 }
59 void imtk_draw_rect(int x, int y, int w, int h, float *color_rgba)
60 {
61 glBegin(GL_QUADS);
62 if(color_rgba) {
63 glColor4fv(color_rgba);
64 }
65 glVertex2f(x, y);
66 glVertex2f(x + w, y);
67 glVertex2f(x + w, y + h);
68 glVertex2f(x, y + h);
69 glEnd();
70 }
72 void imtk_draw_frame(int x, int y, int w, int h, int style)
73 {
74 float tcol[4], bcol[4];
75 float b = imtk_get_bevel_width();
77 switch(style) {
78 case FRAME_INSET:
79 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
80 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
81 break;
83 case FRAME_OUTSET:
84 default:
85 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
86 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
87 }
89 glBegin(GL_QUADS);
90 glColor4fv(tcol);
91 glVertex2f(x, y);
92 glVertex2f(x + b, y + b);
93 glVertex2f(x + w - b, y + b);
94 glVertex2f(x + w, y);
96 glVertex2f(x + b, y + b);
97 glVertex2f(x, y);
98 glVertex2f(x, y + h);
99 glVertex2f(x + b, y + h - b);
101 glColor4fv(bcol);
102 glVertex2f(x + b, y + h - b);
103 glVertex2f(x + w - b, y + h - b);
104 glVertex2f(x + w, y + h);
105 glVertex2f(x, y + h);
107 glVertex2f(x + w - b, y + b);
108 glVertex2f(x + w, y);
109 glVertex2f(x + w, y + h);
110 glVertex2f(x + w - b, y + h - b);
111 glEnd();
113 /*glBegin(GL_LINES);
114 glColor4fv(tcol);
115 glVertex2f(x, y + h);
116 glVertex2f(x, y);
117 glVertex2f(x, y);
118 glVertex2f(x + w, y);
119 glColor4fv(bcol);
120 glVertex2f(x + w, y);
121 glVertex2f(x + w, y + h);
122 glVertex2f(x + w, y + h);
123 glVertex2f(x, y + h);
124 glEnd();*/
126 }
128 void imtk_draw_string(int x, int y, const char *str)
129 {
130 glRasterPos2i(x, y);
131 while(*str) {
132 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
133 }
134 }
136 int imtk_string_size(const char *str)
137 {
138 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
139 }