imtk

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