nuclear@6: #include nuclear@6: #include nuclear@6: #include "imtk.h" nuclear@6: #include "state.h" nuclear@6: #include "draw.h" nuclear@6: nuclear@6: #define SLIDER_SIZE 100 nuclear@6: #define THUMB_WIDTH 10 nuclear@6: #define THUMB_HEIGHT 20 nuclear@6: nuclear@6: static void draw_slider(int id, float pos, float min, float max, int x, int y); nuclear@6: nuclear@6: float imtk_slider(int id, float pos, float min, float max, int x, int y) nuclear@6: { nuclear@6: int mousex, mousey, thumb_x, thumb_y, over = 0; nuclear@6: float range = max - min; nuclear@6: nuclear@6: assert(id >= 0); nuclear@6: nuclear@6: imtk_get_mouse(&mousex, &mousey); nuclear@6: nuclear@6: pos = (pos - min) / range; nuclear@6: if(pos < 0.0) pos = 0.0; nuclear@6: if(pos > 1.0) pos = 1.0; nuclear@6: nuclear@6: thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2; nuclear@6: thumb_y = y - THUMB_HEIGHT / 2; nuclear@6: nuclear@6: if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) { 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 && imtk_is_hot(id)) { nuclear@6: if(!imtk_is_active(id)) { nuclear@6: imtk_set_prev_mouse(mousex, mousey); nuclear@6: } nuclear@6: imtk_set_active(id); nuclear@6: } nuclear@6: } else { nuclear@6: if(imtk_is_active(id)) { nuclear@6: imtk_set_active(-1); nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: if(imtk_is_active(id)) { nuclear@6: int prevx; nuclear@6: float dx; nuclear@6: nuclear@6: imtk_get_prev_mouse(&prevx, 0); nuclear@6: nuclear@6: dx = (float)(mousex - prevx) / (float)SLIDER_SIZE; nuclear@6: pos += dx; nuclear@7: imtk_set_prev_mouse(mousex, mousey); nuclear@6: nuclear@6: if(pos < 0.0) pos = 0.0; nuclear@6: if(pos > 1.0) pos = 1.0; nuclear@6: } nuclear@6: nuclear@6: draw_slider(id, pos, min, max, x, y); nuclear@6: return pos * range + min; nuclear@6: } nuclear@6: nuclear@6: nuclear@6: static void draw_slider(int id, float pos, float min, float max, int x, int y) nuclear@6: { nuclear@6: float range = max - min; nuclear@6: int thumb_x, thumb_y; nuclear@6: char buf[32]; nuclear@6: nuclear@6: thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2; nuclear@6: thumb_y = y - THUMB_HEIGHT / 2; nuclear@6: nuclear@6: /* draw trough */ nuclear@8: imtk_draw_rect(x, y - 2, SLIDER_SIZE, 4, imtk_get_color(IMTK_BASE_COLOR)); nuclear@6: imtk_draw_frame(x, y - 2, SLIDER_SIZE, 4, FRAME_INSET); nuclear@6: nuclear@6: if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_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: nuclear@6: /* draw handle */ nuclear@8: imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, 0); nuclear@6: imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET); nuclear@6: nuclear@6: /* draw display */ nuclear@6: sprintf(buf, "%.3f", pos * range + min); nuclear@6: glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); nuclear@6: imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf); nuclear@6: } nuclear@6: