imtk

annotate 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
rev   line source
nuclear@6 1 #include <string.h>
nuclear@18 2 #include <math.h>
nuclear@6 3 #include <assert.h>
nuclear@6 4 #include "draw.h"
nuclear@6 5 #include "imtk.h"
nuclear@6 6
nuclear@14 7 #define COLOR_MASK 0xff
nuclear@14 8
nuclear@6 9 /* default colors, can be changed with imtk_set_color */
nuclear@6 10 static float colors[][4] = {
nuclear@6 11 {0.0, 0.0, 0.0, 1.0}, /* text color */
nuclear@14 12 {0.75, 0.75, 0.75, 1.0}, /* top color */
nuclear@14 13 {0.56, 0.56, 0.56, 1.0}, /* bot color */
nuclear@14 14 {0.9, 0.9, 0.9, 1.0}, /* lit bevel */
nuclear@13 15 {0.3, 0.3, 0.3, 1.0}, /* shadowed bevel */
nuclear@13 16 {0.8, 0.25, 0.18, 1.0}, /* cursor color */
nuclear@15 17 {0.68, 0.85, 1.3, 1.0}, /* selection color */
nuclear@14 18 {0.75, 0.1, 0.095, 1.0} /* check color */
nuclear@6 19 };
nuclear@6 20
nuclear@15 21 static float focus_factor = 1.15;
nuclear@14 22 static float press_factor = 0.8;
nuclear@13 23 static float alpha = 1.0;
nuclear@13 24 static float bevel = 1.0;
nuclear@13 25
nuclear@14 26 void imtk_set_color(unsigned int col, float r, float g, float b, float a)
nuclear@6 27 {
nuclear@14 28 int idx = col & COLOR_MASK;
nuclear@14 29 assert(idx >= 0 && idx < sizeof colors / sizeof *colors);
nuclear@6 30
nuclear@14 31 colors[idx][0] = r;
nuclear@14 32 colors[idx][1] = g;
nuclear@14 33 colors[idx][2] = b;
nuclear@14 34 colors[idx][3] = a;
nuclear@6 35 }
nuclear@6 36
nuclear@14 37 float *imtk_get_color(unsigned int col)
nuclear@6 38 {
nuclear@13 39 static float ret[4];
nuclear@14 40 int idx = col & COLOR_MASK;
nuclear@14 41
nuclear@14 42 memcpy(ret, colors + idx, sizeof ret);
nuclear@14 43 if(col & IMTK_FOCUS_BIT) {
nuclear@14 44 ret[0] *= focus_factor;
nuclear@14 45 ret[1] *= focus_factor;
nuclear@14 46 ret[2] *= focus_factor;
nuclear@14 47 }
nuclear@14 48 if(col & IMTK_PRESS_BIT) {
nuclear@14 49 ret[0] *= press_factor;
nuclear@14 50 ret[1] *= press_factor;
nuclear@14 51 ret[2] *= press_factor;
nuclear@14 52 }
nuclear@15 53 if(col & IMTK_SEL_BIT) {
nuclear@15 54 ret[0] *= colors[IMTK_SELECTION_COLOR][0];
nuclear@15 55 ret[1] *= colors[IMTK_SELECTION_COLOR][1];
nuclear@15 56 ret[2] *= colors[IMTK_SELECTION_COLOR][2];
nuclear@15 57 }
nuclear@13 58 ret[3] *= alpha;
nuclear@13 59 return ret;
nuclear@13 60 }
nuclear@13 61
nuclear@13 62 void imtk_set_alpha(float a)
nuclear@13 63 {
nuclear@13 64 alpha = a;
nuclear@13 65 }
nuclear@13 66
nuclear@13 67 float imtk_get_alpha(void)
nuclear@13 68 {
nuclear@13 69 return alpha;
nuclear@13 70 }
nuclear@13 71
nuclear@13 72 void imtk_set_bevel_width(float b)
nuclear@13 73 {
nuclear@13 74 bevel = b;
nuclear@13 75 }
nuclear@13 76
nuclear@13 77 float imtk_get_bevel_width(void)
nuclear@13 78 {
nuclear@13 79 return bevel;
nuclear@6 80 }
nuclear@6 81
nuclear@14 82 void imtk_set_focus_factor(float fact)
nuclear@14 83 {
nuclear@14 84 focus_factor = fact;
nuclear@14 85 }
nuclear@14 86
nuclear@14 87 float imtk_get_focus_factor(void)
nuclear@14 88 {
nuclear@14 89 return focus_factor;
nuclear@14 90 }
nuclear@14 91
nuclear@14 92 void imtk_set_press_factor(float fact)
nuclear@14 93 {
nuclear@14 94 press_factor = fact;
nuclear@14 95 }
nuclear@14 96
nuclear@14 97 float imtk_get_press_factor(void)
nuclear@14 98 {
nuclear@14 99 return press_factor;
nuclear@14 100 }
nuclear@14 101
nuclear@14 102 void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot)
nuclear@8 103 {
nuclear@8 104 glBegin(GL_QUADS);
nuclear@14 105 if(ctop) {
nuclear@14 106 glColor4fv(ctop);
nuclear@8 107 }
nuclear@8 108 glVertex2f(x, y);
nuclear@8 109 glVertex2f(x + w, y);
nuclear@14 110
nuclear@14 111 if(cbot) {
nuclear@14 112 glColor4fv(cbot);
nuclear@14 113 }
nuclear@8 114 glVertex2f(x + w, y + h);
nuclear@8 115 glVertex2f(x, y + h);
nuclear@8 116 glEnd();
nuclear@8 117 }
nuclear@8 118
nuclear@18 119 void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot)
nuclear@18 120 {
nuclear@18 121 int i;
nuclear@18 122 float t, dtheta, theta = 0.0;
nuclear@18 123 float color[4];
nuclear@18 124 float cx = (float)x;
nuclear@18 125 float cy = (float)y;
nuclear@18 126
nuclear@18 127 subdiv += 3;
nuclear@18 128 dtheta = 2.0 * M_PI / subdiv;
nuclear@18 129
nuclear@18 130 color[0] = (ctop[0] + cbot[0]) * 0.5;
nuclear@18 131 color[1] = (ctop[1] + cbot[1]) * 0.5;
nuclear@18 132 color[2] = (ctop[2] + cbot[2]) * 0.5;
nuclear@18 133 color[3] = (ctop[3] + cbot[3]) * 0.5;
nuclear@18 134
nuclear@18 135 glBegin(GL_TRIANGLE_FAN);
nuclear@18 136 glColor4fv(color);
nuclear@18 137 glVertex2f(cx, cy);
nuclear@18 138
nuclear@18 139 for(i=0; i<=subdiv; i++) {
nuclear@18 140 float vx, vy;
nuclear@18 141
nuclear@18 142 vx = cx + cos(theta) * rad;
nuclear@18 143 vy = cy + sin(theta) * rad;
nuclear@18 144 theta += dtheta;
nuclear@18 145
nuclear@18 146 t = (vy - (cy - rad)) / (2.0 * rad);
nuclear@18 147 color[0] = ctop[0] + (cbot[0] - ctop[0]) * t;
nuclear@18 148 color[1] = ctop[1] + (cbot[1] - ctop[1]) * t;
nuclear@18 149 color[2] = ctop[2] + (cbot[2] - ctop[2]) * t;
nuclear@18 150 color[3] = ctop[3] + (cbot[3] - ctop[3]) * t;
nuclear@18 151
nuclear@18 152 glColor4fv(color);
nuclear@18 153 glVertex2f(vx, vy);
nuclear@18 154 }
nuclear@18 155 glEnd();
nuclear@18 156 }
nuclear@18 157
nuclear@6 158 void imtk_draw_frame(int x, int y, int w, int h, int style)
nuclear@6 159 {
nuclear@6 160 float tcol[4], bcol[4];
nuclear@13 161 float b = imtk_get_bevel_width();
nuclear@6 162
nuclear@14 163 if(!b) {
nuclear@14 164 return;
nuclear@14 165 }
nuclear@14 166
nuclear@14 167 x -= b;
nuclear@14 168 y -= b;
nuclear@14 169 w += b * 2;
nuclear@14 170 h += b * 2;
nuclear@14 171
nuclear@6 172 switch(style) {
nuclear@6 173 case FRAME_INSET:
nuclear@13 174 memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol);
nuclear@13 175 memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol);
nuclear@6 176 break;
nuclear@6 177
nuclear@6 178 case FRAME_OUTSET:
nuclear@6 179 default:
nuclear@13 180 memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol);
nuclear@13 181 memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol);
nuclear@6 182 }
nuclear@6 183
nuclear@13 184 glBegin(GL_QUADS);
nuclear@13 185 glColor4fv(tcol);
nuclear@13 186 glVertex2f(x, y);
nuclear@13 187 glVertex2f(x + b, y + b);
nuclear@13 188 glVertex2f(x + w - b, y + b);
nuclear@13 189 glVertex2f(x + w, y);
nuclear@13 190
nuclear@13 191 glVertex2f(x + b, y + b);
nuclear@13 192 glVertex2f(x, y);
nuclear@13 193 glVertex2f(x, y + h);
nuclear@13 194 glVertex2f(x + b, y + h - b);
nuclear@13 195
nuclear@13 196 glColor4fv(bcol);
nuclear@13 197 glVertex2f(x + b, y + h - b);
nuclear@13 198 glVertex2f(x + w - b, y + h - b);
nuclear@13 199 glVertex2f(x + w, y + h);
nuclear@13 200 glVertex2f(x, y + h);
nuclear@13 201
nuclear@13 202 glVertex2f(x + w - b, y + b);
nuclear@13 203 glVertex2f(x + w, y);
nuclear@13 204 glVertex2f(x + w, y + h);
nuclear@13 205 glVertex2f(x + w - b, y + h - b);
nuclear@13 206 glEnd();
nuclear@6 207 }
nuclear@6 208
nuclear@6 209 void imtk_draw_string(int x, int y, const char *str)
nuclear@6 210 {
nuclear@6 211 glRasterPos2i(x, y);
nuclear@6 212 while(*str) {
nuclear@6 213 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++);
nuclear@6 214 }
nuclear@6 215 }
nuclear@6 216
nuclear@6 217 int imtk_string_size(const char *str)
nuclear@6 218 {
nuclear@6 219 return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str);
nuclear@6 220 }