nuclear@6: #include nuclear@14: #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@22: static int draw_slider(int id, float pos, float min, float max, int x, int y, int over); nuclear@6: nuclear@6: float imtk_slider(int id, float pos, float min, float max, int x, int y) nuclear@6: { nuclear@22: int mousex, mousey, thumb_x, thumb_y, txsz, over = 0; nuclear@6: float range = max - min; nuclear@6: nuclear@6: assert(id >= 0); nuclear@6: nuclear@20: if(x == IMTK_AUTO || y == IMTK_AUTO) { nuclear@20: imtk_layout_get_pos(&x, &y); nuclear@20: } nuclear@20: y += THUMB_HEIGHT / 2; nuclear@20: 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@22: txsz = draw_slider(id, pos, min, max, x, y, over); nuclear@22: imtk_layout_advance(SLIDER_SIZE + THUMB_WIDTH + txsz, THUMB_HEIGHT); nuclear@6: return pos * range + min; nuclear@6: } nuclear@6: nuclear@6: nuclear@22: static int draw_slider(int id, float pos, float min, float max, int x, int y, int over) nuclear@6: { nuclear@6: float range = max - min; nuclear@6: int thumb_x, thumb_y; nuclear@6: char buf[32]; nuclear@14: float tcol[4], bcol[4]; nuclear@14: unsigned int attr = 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@14: memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol); nuclear@14: memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol); nuclear@14: nuclear@6: /* draw trough */ nuclear@14: imtk_draw_rect(x, y - 2, SLIDER_SIZE, 5, tcol, bcol); nuclear@14: imtk_draw_frame(x, y - 2, SLIDER_SIZE, 5, FRAME_INSET); nuclear@6: nuclear@14: if(over) { nuclear@14: attr |= IMTK_FOCUS_BIT; nuclear@6: } nuclear@14: if(imtk_is_active(id)) { nuclear@14: attr |= IMTK_PRESS_BIT; nuclear@14: } nuclear@14: memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol); nuclear@14: memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol); nuclear@6: nuclear@6: /* draw handle */ nuclear@14: imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, tcol, bcol); 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@22: return imtk_string_size(buf); nuclear@6: } nuclear@6: