imtk

annotate src/slider.c @ 14:df2bc9406561

added gradients
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 19 Apr 2011 03:01:46 +0300
parents 10604ff95527
children c7a7ddbe7714
rev   line source
nuclear@6 1 #include <stdio.h>
nuclear@14 2 #include <string.h>
nuclear@6 3 #include <assert.h>
nuclear@6 4 #include "imtk.h"
nuclear@6 5 #include "state.h"
nuclear@6 6 #include "draw.h"
nuclear@6 7
nuclear@6 8 #define SLIDER_SIZE 100
nuclear@6 9 #define THUMB_WIDTH 10
nuclear@6 10 #define THUMB_HEIGHT 20
nuclear@6 11
nuclear@14 12 static void draw_slider(int id, float pos, float min, float max, int x, int y, int over);
nuclear@6 13
nuclear@6 14 float imtk_slider(int id, float pos, float min, float max, int x, int y)
nuclear@6 15 {
nuclear@6 16 int mousex, mousey, thumb_x, thumb_y, over = 0;
nuclear@6 17 float range = max - min;
nuclear@6 18
nuclear@6 19 assert(id >= 0);
nuclear@6 20
nuclear@6 21 imtk_get_mouse(&mousex, &mousey);
nuclear@6 22
nuclear@6 23 pos = (pos - min) / range;
nuclear@6 24 if(pos < 0.0) pos = 0.0;
nuclear@6 25 if(pos > 1.0) pos = 1.0;
nuclear@6 26
nuclear@6 27 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
nuclear@6 28 thumb_y = y - THUMB_HEIGHT / 2;
nuclear@6 29
nuclear@6 30 if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
nuclear@6 31 imtk_set_hot(id);
nuclear@6 32 over = 1;
nuclear@6 33 }
nuclear@6 34
nuclear@6 35 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
nuclear@6 36 if(over && imtk_is_hot(id)) {
nuclear@6 37 if(!imtk_is_active(id)) {
nuclear@6 38 imtk_set_prev_mouse(mousex, mousey);
nuclear@6 39 }
nuclear@6 40 imtk_set_active(id);
nuclear@6 41 }
nuclear@6 42 } else {
nuclear@6 43 if(imtk_is_active(id)) {
nuclear@6 44 imtk_set_active(-1);
nuclear@6 45 }
nuclear@6 46 }
nuclear@6 47
nuclear@6 48 if(imtk_is_active(id)) {
nuclear@6 49 int prevx;
nuclear@6 50 float dx;
nuclear@6 51
nuclear@6 52 imtk_get_prev_mouse(&prevx, 0);
nuclear@6 53
nuclear@6 54 dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
nuclear@6 55 pos += dx;
nuclear@7 56 imtk_set_prev_mouse(mousex, mousey);
nuclear@6 57
nuclear@6 58 if(pos < 0.0) pos = 0.0;
nuclear@6 59 if(pos > 1.0) pos = 1.0;
nuclear@6 60 }
nuclear@6 61
nuclear@14 62 draw_slider(id, pos, min, max, x, y, over);
nuclear@6 63 return pos * range + min;
nuclear@6 64 }
nuclear@6 65
nuclear@6 66
nuclear@14 67 static void draw_slider(int id, float pos, float min, float max, int x, int y, int over)
nuclear@6 68 {
nuclear@6 69 float range = max - min;
nuclear@6 70 int thumb_x, thumb_y;
nuclear@6 71 char buf[32];
nuclear@14 72 float tcol[4], bcol[4];
nuclear@14 73 unsigned int attr = 0;
nuclear@6 74
nuclear@6 75 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
nuclear@6 76 thumb_y = y - THUMB_HEIGHT / 2;
nuclear@6 77
nuclear@14 78 memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol);
nuclear@14 79 memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol);
nuclear@14 80
nuclear@6 81 /* draw trough */
nuclear@14 82 imtk_draw_rect(x, y - 2, SLIDER_SIZE, 5, tcol, bcol);
nuclear@14 83 imtk_draw_frame(x, y - 2, SLIDER_SIZE, 5, FRAME_INSET);
nuclear@6 84
nuclear@14 85 if(over) {
nuclear@14 86 attr |= IMTK_FOCUS_BIT;
nuclear@6 87 }
nuclear@14 88 if(imtk_is_active(id)) {
nuclear@14 89 attr |= IMTK_PRESS_BIT;
nuclear@14 90 }
nuclear@14 91 memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
nuclear@14 92 memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
nuclear@6 93
nuclear@6 94 /* draw handle */
nuclear@14 95 imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, tcol, bcol);
nuclear@6 96 imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET);
nuclear@6 97
nuclear@6 98 /* draw display */
nuclear@6 99 sprintf(buf, "%.3f", pos * range + min);
nuclear@6 100 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
nuclear@6 101 imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
nuclear@6 102 }
nuclear@6 103