imtk

annotate src/slider.c @ 25:bb358d8c4903

added $(PREFIX)/include and $(PREFIX)/lib into the paths
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 04 Oct 2011 19:24:51 +0300
parents c7a7ddbe7714
children
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@22 12 static int 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@22 16 int mousex, mousey, thumb_x, thumb_y, txsz, over = 0;
nuclear@6 17 float range = max - min;
nuclear@6 18
nuclear@6 19 assert(id >= 0);
nuclear@6 20
nuclear@20 21 if(x == IMTK_AUTO || y == IMTK_AUTO) {
nuclear@20 22 imtk_layout_get_pos(&x, &y);
nuclear@20 23 }
nuclear@20 24 y += THUMB_HEIGHT / 2;
nuclear@20 25
nuclear@6 26 imtk_get_mouse(&mousex, &mousey);
nuclear@6 27
nuclear@6 28 pos = (pos - min) / range;
nuclear@6 29 if(pos < 0.0) pos = 0.0;
nuclear@6 30 if(pos > 1.0) pos = 1.0;
nuclear@6 31
nuclear@6 32 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
nuclear@6 33 thumb_y = y - THUMB_HEIGHT / 2;
nuclear@6 34
nuclear@6 35 if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) {
nuclear@6 36 imtk_set_hot(id);
nuclear@6 37 over = 1;
nuclear@6 38 }
nuclear@6 39
nuclear@6 40 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
nuclear@6 41 if(over && imtk_is_hot(id)) {
nuclear@6 42 if(!imtk_is_active(id)) {
nuclear@6 43 imtk_set_prev_mouse(mousex, mousey);
nuclear@6 44 }
nuclear@6 45 imtk_set_active(id);
nuclear@6 46 }
nuclear@6 47 } else {
nuclear@6 48 if(imtk_is_active(id)) {
nuclear@6 49 imtk_set_active(-1);
nuclear@6 50 }
nuclear@6 51 }
nuclear@6 52
nuclear@6 53 if(imtk_is_active(id)) {
nuclear@6 54 int prevx;
nuclear@6 55 float dx;
nuclear@6 56
nuclear@6 57 imtk_get_prev_mouse(&prevx, 0);
nuclear@6 58
nuclear@6 59 dx = (float)(mousex - prevx) / (float)SLIDER_SIZE;
nuclear@6 60 pos += dx;
nuclear@7 61 imtk_set_prev_mouse(mousex, mousey);
nuclear@6 62
nuclear@6 63 if(pos < 0.0) pos = 0.0;
nuclear@6 64 if(pos > 1.0) pos = 1.0;
nuclear@6 65 }
nuclear@6 66
nuclear@22 67 txsz = draw_slider(id, pos, min, max, x, y, over);
nuclear@22 68 imtk_layout_advance(SLIDER_SIZE + THUMB_WIDTH + txsz, THUMB_HEIGHT);
nuclear@6 69 return pos * range + min;
nuclear@6 70 }
nuclear@6 71
nuclear@6 72
nuclear@22 73 static int draw_slider(int id, float pos, float min, float max, int x, int y, int over)
nuclear@6 74 {
nuclear@6 75 float range = max - min;
nuclear@6 76 int thumb_x, thumb_y;
nuclear@6 77 char buf[32];
nuclear@14 78 float tcol[4], bcol[4];
nuclear@14 79 unsigned int attr = 0;
nuclear@6 80
nuclear@6 81 thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2;
nuclear@6 82 thumb_y = y - THUMB_HEIGHT / 2;
nuclear@6 83
nuclear@14 84 memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol);
nuclear@14 85 memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol);
nuclear@14 86
nuclear@6 87 /* draw trough */
nuclear@14 88 imtk_draw_rect(x, y - 2, SLIDER_SIZE, 5, tcol, bcol);
nuclear@14 89 imtk_draw_frame(x, y - 2, SLIDER_SIZE, 5, FRAME_INSET);
nuclear@6 90
nuclear@14 91 if(over) {
nuclear@14 92 attr |= IMTK_FOCUS_BIT;
nuclear@6 93 }
nuclear@14 94 if(imtk_is_active(id)) {
nuclear@14 95 attr |= IMTK_PRESS_BIT;
nuclear@14 96 }
nuclear@14 97 memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
nuclear@14 98 memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
nuclear@6 99
nuclear@6 100 /* draw handle */
nuclear@14 101 imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, tcol, bcol);
nuclear@6 102 imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET);
nuclear@6 103
nuclear@6 104 /* draw display */
nuclear@6 105 sprintf(buf, "%.3f", pos * range + min);
nuclear@6 106 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
nuclear@6 107 imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf);
nuclear@22 108 return imtk_string_size(buf);
nuclear@6 109 }
nuclear@6 110