nuclear@6: #include nuclear@18: #include nuclear@6: #include nuclear@6: #include "draw.h" nuclear@6: #include "imtk.h" nuclear@6: nuclear@14: #define COLOR_MASK 0xff nuclear@14: nuclear@6: /* default colors, can be changed with imtk_set_color */ nuclear@6: static float colors[][4] = { nuclear@6: {0.0, 0.0, 0.0, 1.0}, /* text color */ nuclear@14: {0.75, 0.75, 0.75, 1.0}, /* top color */ nuclear@14: {0.56, 0.56, 0.56, 1.0}, /* bot color */ nuclear@14: {0.9, 0.9, 0.9, 1.0}, /* lit bevel */ nuclear@13: {0.3, 0.3, 0.3, 1.0}, /* shadowed bevel */ nuclear@13: {0.8, 0.25, 0.18, 1.0}, /* cursor color */ nuclear@15: {0.68, 0.85, 1.3, 1.0}, /* selection color */ nuclear@14: {0.75, 0.1, 0.095, 1.0} /* check color */ nuclear@6: }; nuclear@6: nuclear@15: static float focus_factor = 1.15; nuclear@14: static float press_factor = 0.8; nuclear@13: static float alpha = 1.0; nuclear@13: static float bevel = 1.0; nuclear@13: nuclear@14: void imtk_set_color(unsigned int col, float r, float g, float b, float a) nuclear@6: { nuclear@14: int idx = col & COLOR_MASK; nuclear@14: assert(idx >= 0 && idx < sizeof colors / sizeof *colors); nuclear@6: nuclear@14: colors[idx][0] = r; nuclear@14: colors[idx][1] = g; nuclear@14: colors[idx][2] = b; nuclear@14: colors[idx][3] = a; nuclear@6: } nuclear@6: nuclear@14: float *imtk_get_color(unsigned int col) nuclear@6: { nuclear@13: static float ret[4]; nuclear@14: int idx = col & COLOR_MASK; nuclear@14: nuclear@14: memcpy(ret, colors + idx, sizeof ret); nuclear@14: if(col & IMTK_FOCUS_BIT) { nuclear@14: ret[0] *= focus_factor; nuclear@14: ret[1] *= focus_factor; nuclear@14: ret[2] *= focus_factor; nuclear@14: } nuclear@14: if(col & IMTK_PRESS_BIT) { nuclear@14: ret[0] *= press_factor; nuclear@14: ret[1] *= press_factor; nuclear@14: ret[2] *= press_factor; nuclear@14: } nuclear@15: if(col & IMTK_SEL_BIT) { nuclear@15: ret[0] *= colors[IMTK_SELECTION_COLOR][0]; nuclear@15: ret[1] *= colors[IMTK_SELECTION_COLOR][1]; nuclear@15: ret[2] *= colors[IMTK_SELECTION_COLOR][2]; nuclear@15: } nuclear@13: ret[3] *= alpha; nuclear@13: return ret; nuclear@13: } nuclear@13: nuclear@13: void imtk_set_alpha(float a) nuclear@13: { nuclear@13: alpha = a; nuclear@13: } nuclear@13: nuclear@13: float imtk_get_alpha(void) nuclear@13: { nuclear@13: return alpha; nuclear@13: } nuclear@13: nuclear@13: void imtk_set_bevel_width(float b) nuclear@13: { nuclear@13: bevel = b; nuclear@13: } nuclear@13: nuclear@13: float imtk_get_bevel_width(void) nuclear@13: { nuclear@13: return bevel; nuclear@6: } nuclear@6: nuclear@14: void imtk_set_focus_factor(float fact) nuclear@14: { nuclear@14: focus_factor = fact; nuclear@14: } nuclear@14: nuclear@14: float imtk_get_focus_factor(void) nuclear@14: { nuclear@14: return focus_factor; nuclear@14: } nuclear@14: nuclear@14: void imtk_set_press_factor(float fact) nuclear@14: { nuclear@14: press_factor = fact; nuclear@14: } nuclear@14: nuclear@14: float imtk_get_press_factor(void) nuclear@14: { nuclear@14: return press_factor; nuclear@14: } nuclear@14: nuclear@14: void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot) nuclear@8: { nuclear@8: glBegin(GL_QUADS); nuclear@14: if(ctop) { nuclear@14: glColor4fv(ctop); nuclear@8: } nuclear@8: glVertex2f(x, y); nuclear@8: glVertex2f(x + w, y); nuclear@14: nuclear@14: if(cbot) { nuclear@14: glColor4fv(cbot); nuclear@14: } nuclear@8: glVertex2f(x + w, y + h); nuclear@8: glVertex2f(x, y + h); nuclear@8: glEnd(); nuclear@8: } nuclear@8: nuclear@18: void imtk_draw_disc(int x, int y, float rad, int subdiv, float *ctop, float *cbot) nuclear@18: { nuclear@18: int i; nuclear@18: float t, dtheta, theta = 0.0; nuclear@18: float color[4]; nuclear@18: float cx = (float)x; nuclear@18: float cy = (float)y; nuclear@18: nuclear@18: subdiv += 3; nuclear@18: dtheta = 2.0 * M_PI / subdiv; nuclear@18: nuclear@18: color[0] = (ctop[0] + cbot[0]) * 0.5; nuclear@18: color[1] = (ctop[1] + cbot[1]) * 0.5; nuclear@18: color[2] = (ctop[2] + cbot[2]) * 0.5; nuclear@18: color[3] = (ctop[3] + cbot[3]) * 0.5; nuclear@18: nuclear@18: glBegin(GL_TRIANGLE_FAN); nuclear@18: glColor4fv(color); nuclear@18: glVertex2f(cx, cy); nuclear@18: nuclear@18: for(i=0; i<=subdiv; i++) { nuclear@18: float vx, vy; nuclear@18: nuclear@18: vx = cx + cos(theta) * rad; nuclear@18: vy = cy + sin(theta) * rad; nuclear@18: theta += dtheta; nuclear@18: nuclear@18: t = (vy - (cy - rad)) / (2.0 * rad); nuclear@18: color[0] = ctop[0] + (cbot[0] - ctop[0]) * t; nuclear@18: color[1] = ctop[1] + (cbot[1] - ctop[1]) * t; nuclear@18: color[2] = ctop[2] + (cbot[2] - ctop[2]) * t; nuclear@18: color[3] = ctop[3] + (cbot[3] - ctop[3]) * t; nuclear@18: nuclear@18: glColor4fv(color); nuclear@18: glVertex2f(vx, vy); nuclear@18: } nuclear@18: glEnd(); nuclear@18: } nuclear@18: nuclear@6: void imtk_draw_frame(int x, int y, int w, int h, int style) nuclear@6: { nuclear@6: float tcol[4], bcol[4]; nuclear@13: float b = imtk_get_bevel_width(); nuclear@6: nuclear@14: if(!b) { nuclear@14: return; nuclear@14: } nuclear@14: nuclear@14: x -= b; nuclear@14: y -= b; nuclear@14: w += b * 2; nuclear@14: h += b * 2; nuclear@14: nuclear@6: switch(style) { nuclear@6: case FRAME_INSET: nuclear@13: memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol); nuclear@13: memcpy(bcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof bcol); nuclear@6: break; nuclear@6: nuclear@6: case FRAME_OUTSET: nuclear@6: default: nuclear@13: memcpy(tcol, imtk_get_color(IMTK_BEVEL_LIT_COLOR), sizeof tcol); nuclear@13: memcpy(bcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof bcol); nuclear@6: } nuclear@6: nuclear@13: glBegin(GL_QUADS); nuclear@13: glColor4fv(tcol); nuclear@13: glVertex2f(x, y); nuclear@13: glVertex2f(x + b, y + b); nuclear@13: glVertex2f(x + w - b, y + b); nuclear@13: glVertex2f(x + w, y); nuclear@13: nuclear@13: glVertex2f(x + b, y + b); nuclear@13: glVertex2f(x, y); nuclear@13: glVertex2f(x, y + h); nuclear@13: glVertex2f(x + b, y + h - b); nuclear@13: nuclear@13: glColor4fv(bcol); nuclear@13: glVertex2f(x + b, y + h - b); nuclear@13: glVertex2f(x + w - b, y + h - b); nuclear@13: glVertex2f(x + w, y + h); nuclear@13: glVertex2f(x, y + h); nuclear@13: nuclear@13: glVertex2f(x + w - b, y + b); nuclear@13: glVertex2f(x + w, y); nuclear@13: glVertex2f(x + w, y + h); nuclear@13: glVertex2f(x + w - b, y + h - b); nuclear@13: glEnd(); nuclear@6: } nuclear@6: nuclear@6: void imtk_draw_string(int x, int y, const char *str) nuclear@6: { nuclear@6: glRasterPos2i(x, y); nuclear@6: while(*str) { nuclear@6: glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *str++); nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: int imtk_string_size(const char *str) nuclear@6: { nuclear@6: return glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (const unsigned char*)str); nuclear@6: }