nuclear@6: #include nuclear@6: #include "imtk.h" nuclear@6: #include "state.h" nuclear@6: #include "draw.h" nuclear@6: nuclear@6: static void calc_button_size(const char *label, int *wret, int *hret); nuclear@6: static void draw_button(int id, const char *label, int x, int y); nuclear@6: nuclear@6: int imtk_button(int id, const char *label, int x, int y) nuclear@6: { nuclear@6: int w, h, res = 0; nuclear@6: int over = 0; nuclear@6: nuclear@6: assert(id >= 0); nuclear@6: nuclear@6: calc_button_size(label, &w, &h); nuclear@6: nuclear@6: if(imtk_hit_test(x, y, w, h)) { nuclear@6: imtk_set_hot(id); nuclear@6: over = 1; nuclear@6: } nuclear@6: nuclear@6: if(imtk_button_state(IMTK_LEFT_BUTTON)) { nuclear@6: if(over) { nuclear@6: imtk_set_active(id); nuclear@6: } nuclear@6: } else { /* mouse button up */ nuclear@6: if(imtk_is_active(id)) { nuclear@6: imtk_set_active(-1); nuclear@6: if(imtk_is_hot(id) && over) { nuclear@6: res = 1; nuclear@6: } nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: draw_button(id, label, x, y); nuclear@6: return res; nuclear@6: } nuclear@6: nuclear@6: static void draw_button(int id, const char *label, int x, int y) nuclear@6: { nuclear@6: int width, height; nuclear@6: nuclear@6: calc_button_size(label, &width, &height); nuclear@6: nuclear@6: glBegin(GL_QUADS); nuclear@6: if(imtk_hit_test(x, y, width, height)) { nuclear@6: glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR)); nuclear@6: } else { nuclear@6: glColor4fv(imtk_get_color(IMTK_BASE_COLOR)); nuclear@6: } nuclear@6: glVertex2f(x, y); nuclear@6: glVertex2f(x + width, y); nuclear@6: glVertex2f(x + width, y + height); nuclear@6: glVertex2f(x, y + height); nuclear@6: glEnd(); nuclear@6: nuclear@6: imtk_draw_frame(x, y, width, height, imtk_is_active(id) ? FRAME_INSET : FRAME_OUTSET); nuclear@6: nuclear@6: glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); nuclear@6: imtk_draw_string(x + 20, y + 15, label); nuclear@6: } nuclear@6: nuclear@6: static void calc_button_size(const char *label, int *wret, int *hret) nuclear@6: { nuclear@6: int strsz = imtk_string_size(label); nuclear@6: if(wret) *wret = strsz + 40; nuclear@6: if(hret) *hret = 20; nuclear@6: }