nuclear@10: #include nuclear@10: #include nuclear@10: #include nuclear@10: #include "imtk.h" nuclear@10: #include "state.h" nuclear@10: #include "draw.h" nuclear@10: nuclear@10: #define SLIDER_SIZE 100 nuclear@10: #define THUMB_WIDTH 10 nuclear@10: #define THUMB_HEIGHT 20 nuclear@10: nuclear@10: static int draw_slider(int id, float pos, float min, float max, int x, int y, int over); nuclear@10: nuclear@10: float imtk_slider(int id, float pos, float min, float max, int x, int y) nuclear@10: { nuclear@10: int mousex, mousey, thumb_x, thumb_y, txsz, over = 0; nuclear@10: float range = max - min; nuclear@10: nuclear@10: assert(id >= 0); nuclear@10: nuclear@10: if(x == IMTK_AUTO || y == IMTK_AUTO) { nuclear@10: imtk_layout_get_pos(&x, &y); nuclear@10: } nuclear@10: y += THUMB_HEIGHT / 2; nuclear@10: nuclear@10: imtk_get_mouse(&mousex, &mousey); nuclear@10: nuclear@10: pos = (pos - min) / range; nuclear@10: if(pos < 0.0) pos = 0.0; nuclear@10: if(pos > 1.0) pos = 1.0; nuclear@10: nuclear@10: thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2; nuclear@10: thumb_y = y - THUMB_HEIGHT / 2; nuclear@10: nuclear@10: if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) { nuclear@10: imtk_set_hot(id); nuclear@10: over = 1; nuclear@10: } nuclear@10: nuclear@10: if(imtk_button_state(IMTK_LEFT_BUTTON)) { nuclear@10: if(over && imtk_is_hot(id)) { nuclear@10: if(!imtk_is_active(id)) { nuclear@10: imtk_set_prev_mouse(mousex, mousey); nuclear@10: } nuclear@10: imtk_set_active(id); nuclear@10: } nuclear@10: } else { nuclear@10: if(imtk_is_active(id)) { nuclear@10: imtk_set_active(-1); nuclear@10: } nuclear@10: } nuclear@10: nuclear@10: if(imtk_is_active(id)) { nuclear@10: int prevx; nuclear@10: float dx; nuclear@10: nuclear@10: imtk_get_prev_mouse(&prevx, 0); nuclear@10: nuclear@10: dx = (float)(mousex - prevx) / (float)SLIDER_SIZE; nuclear@10: pos += dx; nuclear@10: imtk_set_prev_mouse(mousex, mousey); nuclear@10: nuclear@10: if(pos < 0.0) pos = 0.0; nuclear@10: if(pos > 1.0) pos = 1.0; nuclear@10: } nuclear@10: nuclear@10: txsz = draw_slider(id, pos, min, max, x, y, over); nuclear@10: imtk_layout_advance(SLIDER_SIZE + THUMB_WIDTH + txsz, THUMB_HEIGHT); nuclear@10: return pos * range + min; nuclear@10: } nuclear@10: nuclear@10: nuclear@10: static int draw_slider(int id, float pos, float min, float max, int x, int y, int over) nuclear@10: { nuclear@10: float range = max - min; nuclear@10: int thumb_x, thumb_y; nuclear@10: char buf[32]; nuclear@10: float tcol[4], bcol[4]; nuclear@10: unsigned int attr = 0; nuclear@10: nuclear@10: thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2; nuclear@10: thumb_y = y - THUMB_HEIGHT / 2; nuclear@10: nuclear@10: memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol); nuclear@10: memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol); nuclear@10: nuclear@10: /* draw trough */ nuclear@10: imtk_draw_rect(x, y - 2, SLIDER_SIZE, 5, tcol, bcol); nuclear@10: imtk_draw_frame(x, y - 2, SLIDER_SIZE, 5, FRAME_INSET); nuclear@10: nuclear@10: if(over) { nuclear@10: attr |= IMTK_FOCUS_BIT; nuclear@10: } nuclear@10: if(imtk_is_active(id)) { nuclear@10: attr |= IMTK_PRESS_BIT; nuclear@10: } nuclear@10: memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol); nuclear@10: memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol); nuclear@10: nuclear@10: /* draw handle */ nuclear@10: imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, tcol, bcol); nuclear@10: imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET); nuclear@10: nuclear@10: /* draw display */ nuclear@10: sprintf(buf, "%.3f", pos * range + min); nuclear@10: glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); nuclear@10: imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf); nuclear@10: return imtk_string_size(buf); nuclear@10: } nuclear@10: