# HG changeset patch # User John Tsiombikas # Date 1303171306 -10800 # Node ID df2bc94065617cc5a6ea36a605a32a19d4062951 # Parent 9c7987064bb07f363d827e8930d73c5c0284d2a8 added gradients diff -r 9c7987064bb0 -r df2bc9406561 Makefile --- a/Makefile Mon Apr 18 06:15:46 2011 +0300 +++ b/Makefile Tue Apr 19 03:01:46 2011 +0300 @@ -1,5 +1,6 @@ src = $(wildcard *.c src/*.c) obj = $(src:.c=.o) +depfiles = $(obj:.o=.d) bin = test CC = gcc @@ -15,6 +16,15 @@ $(bin): $(obj) $(CC) -o $@ $(obj) $(LDFLAGS) +-include $(depfiles) + +%.d: %.c + @$(CPP) $(CFLAGS) -MM -MT $(@:.d=.o) $< >$@ + .PHONY: clean clean: rm -f $(obj) $(bin) + +.PHONY: cleandep +cleandep: + rm -f $(depfiles) diff -r 9c7987064bb0 -r df2bc9406561 src/button.c --- a/src/button.c Mon Apr 18 06:15:46 2011 +0300 +++ b/src/button.c Tue Apr 19 03:01:46 2011 +0300 @@ -1,10 +1,11 @@ +#include #include #include "imtk.h" #include "state.h" #include "draw.h" static void calc_button_size(const char *label, int *wret, int *hret); -static void draw_button(int id, const char *label, int x, int y); +static void draw_button(int id, const char *label, int x, int y, int over); int imtk_button(int id, const char *label, int x, int y) { @@ -33,24 +34,26 @@ } } - draw_button(id, label, x, y); + draw_button(id, label, x, y, over); return res; } -static void draw_button(int id, const char *label, int x, int y) +static void draw_button(int id, const char *label, int x, int y, int over) { - int width, height; + float tcol[4], bcol[4]; + int width, height, active = imtk_is_active(id); + unsigned int attr = 0; + + if(over) attr |= IMTK_FOCUS_BIT; + if(active) attr |= IMTK_PRESS_BIT; calc_button_size(label, &width, &height); - if(imtk_hit_test(x, y, width, height)) { - glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR)); - } else { - glColor4fv(imtk_get_color(IMTK_BASE_COLOR)); - } + memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol); + memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol); - imtk_draw_rect(x, y, width, height, 0); - imtk_draw_frame(x, y, width, height, imtk_is_active(id) ? FRAME_INSET : FRAME_OUTSET); + imtk_draw_rect(x, y, width, height, tcol, bcol); + imtk_draw_frame(x, y, width, height, active ? FRAME_INSET : FRAME_OUTSET); glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); imtk_draw_string(x + 20, y + 15, label); diff -r 9c7987064bb0 -r df2bc9406561 src/checkbox.c --- a/src/checkbox.c Mon Apr 18 06:15:46 2011 +0300 +++ b/src/checkbox.c Tue Apr 19 03:01:46 2011 +0300 @@ -1,3 +1,4 @@ +#include #include #include "imtk.h" #include "state.h" @@ -38,32 +39,38 @@ return state; } +static float v[][2] = { + {-0.2, 0.63}, /* 0 */ + {0.121, 0.325}, /* 1 */ + {0.15, 0.5}, /* 2 */ + {0.28, 0.125}, /* 3 */ + {0.38, 0.33}, /* 4 */ + {0.42, -0.122}, /* 5 */ + {0.58, 0.25}, /* 6 */ + {0.72, 0.52}, /* 7 */ + {0.625, 0.65}, /* 8 */ + {0.89, 0.78}, /* 9 */ + {0.875, 0.92}, /* 10 */ + {1.13, 1.145} /* 11 */ +}; +#define TRI(a, b, c) (glVertex2fv(v[a]), glVertex2fv(v[b]), glVertex2fv(v[c])) + static void draw_checkbox(int id, const char *label, int x, int y, int state, int over) { static const int sz = CHECKBOX_SIZE; - static float v[][2] = { - {-0.2, 0.63}, /* 0 */ - {0.121, 0.325}, /* 1 */ - {0.15, 0.5}, /* 2 */ - {0.28, 0.125}, /* 3 */ - {0.38, 0.33}, /* 4 */ - {0.42, -0.122}, /* 5 */ - {0.58, 0.25}, /* 6 */ - {0.72, 0.52}, /* 7 */ - {0.625, 0.65}, /* 8 */ - {0.89, 0.78}, /* 9 */ - {0.875, 0.92}, /* 10 */ - {1.13, 1.145} /* 11 */ - }; -#define TRI(a, b, c) (glVertex2fv(v[a]), glVertex2fv(v[b]), glVertex2fv(v[c])) + unsigned int attr = 0; + float tcol[4], bcol[4]; if(over) { - glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR)); - } else { - glColor4fv(imtk_get_color(IMTK_BASE_COLOR)); + attr |= IMTK_FOCUS_BIT; } + if(imtk_is_active(id)) { + attr |= IMTK_PRESS_BIT; + } + memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol); + memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol); - imtk_draw_rect(x, y, sz, sz, 0); + imtk_draw_rect(x, y, sz, sz, tcol, bcol); imtk_draw_frame(x, y, sz, sz, FRAME_INSET); glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); diff -r 9c7987064bb0 -r df2bc9406561 src/draw.c --- a/src/draw.c Mon Apr 18 06:15:46 2011 +0300 +++ b/src/draw.c Tue Apr 19 03:01:46 2011 +0300 @@ -3,35 +3,52 @@ #include "draw.h" #include "imtk.h" +#define COLOR_MASK 0xff + /* default colors, can be changed with imtk_set_color */ static float colors[][4] = { {0.0, 0.0, 0.0, 1.0}, /* text color */ - {0.7, 0.7, 0.7, 1.0}, /* base color */ - {0.85, 0.85, 0.85, 1.0}, /* focus color */ - {1.0, 1.0, 1.0, 1.0}, /* lit bevel */ + {0.75, 0.75, 0.75, 1.0}, /* top color */ + {0.56, 0.56, 0.56, 1.0}, /* bot color */ + {0.9, 0.9, 0.9, 1.0}, /* lit bevel */ {0.3, 0.3, 0.3, 1.0}, /* shadowed bevel */ {0.8, 0.25, 0.18, 1.0}, /* cursor color */ {0.4, 0.5, 0.9, 1.0}, /* selection color */ - {0.63, 0.078, 0.078, 1.0} /* check color */ + {0.75, 0.1, 0.095, 1.0} /* check color */ }; +static float focus_factor = 1.1; +static float press_factor = 0.8; static float alpha = 1.0; static float bevel = 1.0; -void imtk_set_color(int col, float r, float g, float b, float a) +void imtk_set_color(unsigned int col, float r, float g, float b, float a) { - assert(col >= 0 && col < sizeof colors / sizeof *colors); + int idx = col & COLOR_MASK; + assert(idx >= 0 && idx < sizeof colors / sizeof *colors); - colors[col][0] = r; - colors[col][1] = g; - colors[col][2] = b; - colors[col][3] = a; + colors[idx][0] = r; + colors[idx][1] = g; + colors[idx][2] = b; + colors[idx][3] = a; } -float *imtk_get_color(int col) +float *imtk_get_color(unsigned int col) { static float ret[4]; - memcpy(ret, colors + col, sizeof ret); + int idx = col & COLOR_MASK; + + memcpy(ret, colors + idx, sizeof ret); + if(col & IMTK_FOCUS_BIT) { + ret[0] *= focus_factor; + ret[1] *= focus_factor; + ret[2] *= focus_factor; + } + if(col & IMTK_PRESS_BIT) { + ret[0] *= press_factor; + ret[1] *= press_factor; + ret[2] *= press_factor; + } ret[3] *= alpha; return ret; } @@ -56,14 +73,38 @@ return bevel; } -void imtk_draw_rect(int x, int y, int w, int h, float *color_rgba) +void imtk_set_focus_factor(float fact) +{ + focus_factor = fact; +} + +float imtk_get_focus_factor(void) +{ + return focus_factor; +} + +void imtk_set_press_factor(float fact) +{ + press_factor = fact; +} + +float imtk_get_press_factor(void) +{ + return press_factor; +} + +void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot) { glBegin(GL_QUADS); - if(color_rgba) { - glColor4fv(color_rgba); + if(ctop) { + glColor4fv(ctop); } glVertex2f(x, y); glVertex2f(x + w, y); + + if(cbot) { + glColor4fv(cbot); + } glVertex2f(x + w, y + h); glVertex2f(x, y + h); glEnd(); @@ -74,6 +115,15 @@ float tcol[4], bcol[4]; float b = imtk_get_bevel_width(); + if(!b) { + return; + } + + x -= b; + y -= b; + w += b * 2; + h += b * 2; + switch(style) { case FRAME_INSET: memcpy(tcol, imtk_get_color(IMTK_BEVEL_SHAD_COLOR), sizeof tcol); @@ -109,20 +159,6 @@ glVertex2f(x + w, y + h); glVertex2f(x + w - b, y + h - b); glEnd(); - - /*glBegin(GL_LINES); - glColor4fv(tcol); - glVertex2f(x, y + h); - glVertex2f(x, y); - glVertex2f(x, y); - glVertex2f(x + w, y); - glColor4fv(bcol); - glVertex2f(x + w, y); - glVertex2f(x + w, y + h); - glVertex2f(x + w, y + h); - glVertex2f(x, y + h); - glEnd();*/ - } void imtk_draw_string(int x, int y, const char *str) diff -r 9c7987064bb0 -r df2bc9406561 src/draw.h --- a/src/draw.h Mon Apr 18 06:15:46 2011 +0300 +++ b/src/draw.h Tue Apr 19 03:01:46 2011 +0300 @@ -13,9 +13,7 @@ FRAME_INSET }; -/*void imtk_set_color(int col, float r, float g, float b, float a); in imtk.h */ -float *imtk_get_color(int col); -void imtk_draw_rect(int x, int y, int w, int h, float *color_rgba); +void imtk_draw_rect(int x, int y, int w, int h, float *ctop, float *cbot); void imtk_draw_frame(int x, int y, int w, int h, int style); void imtk_draw_string(int x, int y, const char *str); int imtk_string_size(const char *str); diff -r 9c7987064bb0 -r df2bc9406561 src/imtk.h --- a/src/imtk.h Mon Apr 18 06:15:46 2011 +0300 +++ b/src/imtk.h Tue Apr 19 03:01:46 2011 +0300 @@ -22,8 +22,8 @@ enum { IMTK_TEXT_COLOR, - IMTK_BASE_COLOR, - IMTK_FOCUS_COLOR, + IMTK_TOP_COLOR, + IMTK_BOTTOM_COLOR, IMTK_BEVEL_LIT_COLOR, IMTK_BEVEL_SHAD_COLOR, IMTK_CURSOR_COLOR, @@ -31,6 +31,12 @@ IMTK_CHECK_COLOR }; +#define IMTK_FOCUS_BIT 0x100 +#define IMTK_PRESS_BIT 0x200 + +#define IMTK_BASE_COLOR IMTK_BOTTOM_COLOR +#define IMTK_FOCUS_COLOR (IMTK_TOP_COLOR | IMTK_FOCUS_BIT) + #ifdef __cplusplus extern "C" { @@ -65,11 +71,16 @@ void imtk_free_list(char *list); /* defined in draw.c */ -void imtk_set_color(int col, float r, float g, float b, float a); +void imtk_set_color(unsigned int col, float r, float g, float b, float a); +float *imtk_get_color(unsigned int col); void imtk_set_alpha(float a); float imtk_get_alpha(void); void imtk_set_bevel_width(float b); float imtk_get_bevel_width(void); +void imtk_set_focus_factor(float fact); +float imtk_get_focus_factor(void); +void imtk_set_press_factor(float fact); +float imtk_get_press_factor(void); #ifdef __cplusplus } diff -r 9c7987064bb0 -r df2bc9406561 src/progress.c --- a/src/progress.c Mon Apr 18 06:15:46 2011 +0300 +++ b/src/progress.c Tue Apr 19 03:01:46 2011 +0300 @@ -1,3 +1,4 @@ +#include #include "imtk.h" #include "draw.h" @@ -15,17 +16,24 @@ { int bar_size = PROGR_SIZE * pos; float b = imtk_get_bevel_width(); + float tcol[4], bcol[4]; if(pos < 0.0) pos = 0.0; if(pos > 1.0) pos = 1.0; + memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol); + memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol); + /* through */ - imtk_draw_rect(x - b, y - b, PROGR_SIZE + b * 2, PROGR_HEIGHT + b * 2, imtk_get_color(IMTK_BASE_COLOR)); + imtk_draw_rect(x - b, y - b, PROGR_SIZE + b * 2, PROGR_HEIGHT + b * 2, tcol, bcol); imtk_draw_frame(x - b, y - b, PROGR_SIZE + b * 2, PROGR_HEIGHT + b * 2, FRAME_INSET); + /* bar */ if(pos > 0.0) { - /* bar */ - imtk_draw_rect(x, y, bar_size, PROGR_HEIGHT, imtk_get_color(IMTK_BASE_COLOR)); + memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR), sizeof tcol); + memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof bcol); + + imtk_draw_rect(x, y, bar_size, PROGR_HEIGHT, tcol, bcol); imtk_draw_frame(x, y, bar_size, PROGR_HEIGHT, FRAME_OUTSET); } } diff -r 9c7987064bb0 -r df2bc9406561 src/slider.c --- a/src/slider.c Mon Apr 18 06:15:46 2011 +0300 +++ b/src/slider.c Tue Apr 19 03:01:46 2011 +0300 @@ -1,4 +1,5 @@ #include +#include #include #include "imtk.h" #include "state.h" @@ -8,7 +9,7 @@ #define THUMB_WIDTH 10 #define THUMB_HEIGHT 20 -static void draw_slider(int id, float pos, float min, float max, int x, int y); +static void draw_slider(int id, float pos, float min, float max, int x, int y, int over); float imtk_slider(int id, float pos, float min, float max, int x, int y) { @@ -58,32 +59,40 @@ if(pos > 1.0) pos = 1.0; } - draw_slider(id, pos, min, max, x, y); + draw_slider(id, pos, min, max, x, y, over); return pos * range + min; } -static void draw_slider(int id, float pos, float min, float max, int x, int y) +static void draw_slider(int id, float pos, float min, float max, int x, int y, int over) { float range = max - min; int thumb_x, thumb_y; char buf[32]; + float tcol[4], bcol[4]; + unsigned int attr = 0; thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2; thumb_y = y - THUMB_HEIGHT / 2; + memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol); + memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol); + /* draw trough */ - imtk_draw_rect(x, y - 2, SLIDER_SIZE, 4, imtk_get_color(IMTK_BASE_COLOR)); - imtk_draw_frame(x, y - 2, SLIDER_SIZE, 4, FRAME_INSET); + imtk_draw_rect(x, y - 2, SLIDER_SIZE, 5, tcol, bcol); + imtk_draw_frame(x, y - 2, SLIDER_SIZE, 5, FRAME_INSET); - if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) { - glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR)); - } else { - glColor4fv(imtk_get_color(IMTK_BASE_COLOR)); + if(over) { + attr |= IMTK_FOCUS_BIT; } + if(imtk_is_active(id)) { + attr |= IMTK_PRESS_BIT; + } + memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol); + memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol); /* draw handle */ - imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, 0); + imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, tcol, bcol); imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET); /* draw display */ diff -r 9c7987064bb0 -r df2bc9406561 src/textbox.c --- a/src/textbox.c Mon Apr 18 06:15:46 2011 +0300 +++ b/src/textbox.c Tue Apr 19 03:01:46 2011 +0300 @@ -7,7 +7,7 @@ #define TEXTBOX_SIZE 100 -static void draw_textbox(int id, const char *text, int x, int y); +static void draw_textbox(int id, const char *text, int x, int y, int over); void imtk_textbox(int id, char *textbuf, size_t buf_sz, int x, int y) @@ -58,21 +58,23 @@ } } - draw_textbox(id, textbuf, x, y); + draw_textbox(id, textbuf, x, y, over); } -static void draw_textbox(int id, const char *text, int x, int y) +static void draw_textbox(int id, const char *text, int x, int y, int over) { int strsz = imtk_string_size(text); + float tcol[4], bcol[4]; + unsigned int attr = 0; - if(imtk_hit_test(x, y, TEXTBOX_SIZE, 20)) { - glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR)); - } else { - glColor4fv(imtk_get_color(IMTK_BASE_COLOR)); + if(over) { + attr |= IMTK_FOCUS_BIT; } + memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol); + memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol); - imtk_draw_rect(x, y, TEXTBOX_SIZE, 20, 0); + imtk_draw_rect(x, y, TEXTBOX_SIZE, 20, tcol, bcol); if(imtk_has_focus(id)) { glBegin(GL_LINES); diff -r 9c7987064bb0 -r df2bc9406561 test.c --- a/test.c Mon Apr 18 06:15:46 2011 +0300 +++ b/test.c Tue Apr 19 03:01:46 2011 +0300 @@ -53,8 +53,7 @@ glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0); - /*imtk_set_color(IMTK_BEVEL_LIT_COLOR, 0.3, 0.3, 0.3, 0.5); - imtk_set_color(IMTK_BEVEL_SHAD_COLOR, 0.3, 0.3, 0.3, 0.5);*/ + imtk_set_bevel_width(1); glutMainLoop(); return 0;