rayfract
diff src/imtk/slider.c @ 10:1496aae2e7d4
- simplified build by including dependences in the source tree
- added make dep tracking
- added mingw cross-build rules
- added readme & licence
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 31 Jul 2023 18:58:56 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/imtk/slider.c Mon Jul 31 18:58:56 2023 +0300 1.3 @@ -0,0 +1,110 @@ 1.4 +#include <stdio.h> 1.5 +#include <string.h> 1.6 +#include <assert.h> 1.7 +#include "imtk.h" 1.8 +#include "state.h" 1.9 +#include "draw.h" 1.10 + 1.11 +#define SLIDER_SIZE 100 1.12 +#define THUMB_WIDTH 10 1.13 +#define THUMB_HEIGHT 20 1.14 + 1.15 +static int draw_slider(int id, float pos, float min, float max, int x, int y, int over); 1.16 + 1.17 +float imtk_slider(int id, float pos, float min, float max, int x, int y) 1.18 +{ 1.19 + int mousex, mousey, thumb_x, thumb_y, txsz, over = 0; 1.20 + float range = max - min; 1.21 + 1.22 + assert(id >= 0); 1.23 + 1.24 + if(x == IMTK_AUTO || y == IMTK_AUTO) { 1.25 + imtk_layout_get_pos(&x, &y); 1.26 + } 1.27 + y += THUMB_HEIGHT / 2; 1.28 + 1.29 + imtk_get_mouse(&mousex, &mousey); 1.30 + 1.31 + pos = (pos - min) / range; 1.32 + if(pos < 0.0) pos = 0.0; 1.33 + if(pos > 1.0) pos = 1.0; 1.34 + 1.35 + thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2; 1.36 + thumb_y = y - THUMB_HEIGHT / 2; 1.37 + 1.38 + if(imtk_hit_test(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT)) { 1.39 + imtk_set_hot(id); 1.40 + over = 1; 1.41 + } 1.42 + 1.43 + if(imtk_button_state(IMTK_LEFT_BUTTON)) { 1.44 + if(over && imtk_is_hot(id)) { 1.45 + if(!imtk_is_active(id)) { 1.46 + imtk_set_prev_mouse(mousex, mousey); 1.47 + } 1.48 + imtk_set_active(id); 1.49 + } 1.50 + } else { 1.51 + if(imtk_is_active(id)) { 1.52 + imtk_set_active(-1); 1.53 + } 1.54 + } 1.55 + 1.56 + if(imtk_is_active(id)) { 1.57 + int prevx; 1.58 + float dx; 1.59 + 1.60 + imtk_get_prev_mouse(&prevx, 0); 1.61 + 1.62 + dx = (float)(mousex - prevx) / (float)SLIDER_SIZE; 1.63 + pos += dx; 1.64 + imtk_set_prev_mouse(mousex, mousey); 1.65 + 1.66 + if(pos < 0.0) pos = 0.0; 1.67 + if(pos > 1.0) pos = 1.0; 1.68 + } 1.69 + 1.70 + txsz = draw_slider(id, pos, min, max, x, y, over); 1.71 + imtk_layout_advance(SLIDER_SIZE + THUMB_WIDTH + txsz, THUMB_HEIGHT); 1.72 + return pos * range + min; 1.73 +} 1.74 + 1.75 + 1.76 +static int draw_slider(int id, float pos, float min, float max, int x, int y, int over) 1.77 +{ 1.78 + float range = max - min; 1.79 + int thumb_x, thumb_y; 1.80 + char buf[32]; 1.81 + float tcol[4], bcol[4]; 1.82 + unsigned int attr = 0; 1.83 + 1.84 + thumb_x = x + SLIDER_SIZE * pos - THUMB_WIDTH / 2; 1.85 + thumb_y = y - THUMB_HEIGHT / 2; 1.86 + 1.87 + memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR), sizeof tcol); 1.88 + memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR), sizeof bcol); 1.89 + 1.90 + /* draw trough */ 1.91 + imtk_draw_rect(x, y - 2, SLIDER_SIZE, 5, tcol, bcol); 1.92 + imtk_draw_frame(x, y - 2, SLIDER_SIZE, 5, FRAME_INSET); 1.93 + 1.94 + if(over) { 1.95 + attr |= IMTK_FOCUS_BIT; 1.96 + } 1.97 + if(imtk_is_active(id)) { 1.98 + attr |= IMTK_PRESS_BIT; 1.99 + } 1.100 + memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol); 1.101 + memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol); 1.102 + 1.103 + /* draw handle */ 1.104 + imtk_draw_rect(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, tcol, bcol); 1.105 + imtk_draw_frame(thumb_x, thumb_y, THUMB_WIDTH, THUMB_HEIGHT, FRAME_OUTSET); 1.106 + 1.107 + /* draw display */ 1.108 + sprintf(buf, "%.3f", pos * range + min); 1.109 + glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); 1.110 + imtk_draw_string(x + SLIDER_SIZE + THUMB_WIDTH / 2 + 2, y + 4, buf); 1.111 + return imtk_string_size(buf); 1.112 +} 1.113 +