imtk

annotate src/slider.c @ 12:17c9525b2e35

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