istereo2
diff libs/goatkit/slider.cc @ 6:3bccfc7d10fe
goatkit is drawing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 23 Sep 2015 05:44:58 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/goatkit/slider.cc Wed Sep 23 05:44:58 2015 +0300 1.3 @@ -0,0 +1,196 @@ 1.4 +/* 1.5 +GoatKit - a themable/animated widget toolkit for games 1.6 +Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU Lesser General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 +#include <stdio.h> 1.22 +#include <math.h> 1.23 +#include "slider.h" 1.24 +#include "screen.h" 1.25 + 1.26 +namespace goatkit { 1.27 + 1.28 +struct SliderImpl { 1.29 + float value, prev_value; 1.30 + float range_min, range_max; 1.31 + float padding; 1.32 + float step; 1.33 + bool dragging; 1.34 + bool cont_change; 1.35 +}; 1.36 + 1.37 +static float remap(float val, float inlow, float inhigh, float outlow, float outhigh); 1.38 + 1.39 +Slider::Slider() 1.40 +{ 1.41 + slider = new SliderImpl; 1.42 + slider->value = slider->prev_value = 0.0f; 1.43 + slider->dragging = false; 1.44 + slider->cont_change = true; 1.45 + 1.46 + slider->range_min = 0.0; 1.47 + slider->range_max = 1.0; 1.48 + slider->step = 0.0; 1.49 + 1.50 + slider->padding = -1.0; 1.51 +} 1.52 + 1.53 +Slider::~Slider() 1.54 +{ 1.55 + delete slider; 1.56 +} 1.57 + 1.58 +const char *Slider::get_type_name() const 1.59 +{ 1.60 + return "slider"; 1.61 +} 1.62 + 1.63 +void Slider::set_value(float val) 1.64 +{ 1.65 + slider->value = remap(val, slider->range_min, slider->range_max, 0, 1); 1.66 +} 1.67 + 1.68 +float Slider::get_value() const 1.69 +{ 1.70 + return remap(slider->value, 0, 1, slider->range_min, slider->range_max); 1.71 +} 1.72 + 1.73 +void Slider::set_value_norm(float val) 1.74 +{ 1.75 + slider->value = val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val); 1.76 +} 1.77 + 1.78 +float Slider::get_value_norm() const 1.79 +{ 1.80 + return slider->value; 1.81 +} 1.82 + 1.83 +void Slider::set_padding(float pad) 1.84 +{ 1.85 + slider->padding = pad; 1.86 +} 1.87 + 1.88 +float Slider::get_padding() const 1.89 +{ 1.90 + if(slider->padding < 0.0) { 1.91 + BBox box = get_box(); 1.92 + return (box.bmax.y - box.bmin.y) * 0.25; 1.93 + } 1.94 + return slider->padding; 1.95 +} 1.96 + 1.97 +void Slider::set_continuous_change(bool cont) 1.98 +{ 1.99 + slider->cont_change = cont; 1.100 +} 1.101 + 1.102 +bool Slider::get_continuous_change() const 1.103 +{ 1.104 + return slider->cont_change; 1.105 +} 1.106 + 1.107 +void Slider::set_range(float min, float max) 1.108 +{ 1.109 + slider->range_min = min; 1.110 + slider->range_max = max; 1.111 +} 1.112 + 1.113 +float Slider::get_range_min() const 1.114 +{ 1.115 + return slider->range_min; 1.116 +} 1.117 + 1.118 +float Slider::get_range_max() const 1.119 +{ 1.120 + return slider->range_max; 1.121 +} 1.122 + 1.123 +void Slider::set_step(float step) 1.124 +{ 1.125 + slider->step = step; 1.126 +} 1.127 + 1.128 +float Slider::get_step() const 1.129 +{ 1.130 + return slider->step; 1.131 +} 1.132 + 1.133 +void Slider::on_mouse_button(const ButtonEvent &ev) 1.134 +{ 1.135 + if(ev.button == 0) { 1.136 + Screen *scr = get_screen(); 1.137 + 1.138 + slider->dragging = ev.press; 1.139 + if(ev.press) { 1.140 + if(scr) scr->grab_mouse(this); 1.141 + } else { 1.142 + if(scr) scr->grab_mouse(0); 1.143 + 1.144 + // on release, if the value has changed send the appropriate event 1.145 + if(slider->prev_value != slider->value) { 1.146 + Event ev; 1.147 + ev.type = EV_CHANGE; 1.148 + handle_event(ev); 1.149 + slider->prev_value = slider->value; 1.150 + } 1.151 + } 1.152 + } 1.153 +} 1.154 + 1.155 +#define ROUND(x) floor((x) + 0.5) 1.156 + 1.157 +void Slider::on_mouse_motion(const MotionEvent &ev) 1.158 +{ 1.159 + if(!slider->dragging) { 1.160 + return; 1.161 + } 1.162 + 1.163 + BBox box = get_box(); 1.164 + 1.165 + float padding = get_padding(); 1.166 + float start = box.bmin.x + padding; 1.167 + float end = box.bmax.x - padding; 1.168 + float new_val = (ev.pos.x - start) / (end - start); 1.169 + 1.170 + // if we have a non-zero step, snap to the nearest value 1.171 + if(slider->step > 0.0) { 1.172 + float range = slider->range_max - slider->range_min; 1.173 + float st = slider->step / range; 1.174 + 1.175 + new_val = ROUND(new_val / st) * st; 1.176 + } 1.177 + 1.178 + if(new_val < 0.0) new_val = 0.0; 1.179 + if(new_val > 1.0) new_val = 1.0; 1.180 + 1.181 + if(new_val != slider->value) { 1.182 + slider->value = new_val; 1.183 + if(slider->cont_change) { 1.184 + Event cev; 1.185 + cev.type = EV_CHANGE; 1.186 + handle_event(cev); 1.187 + } 1.188 + } 1.189 +} 1.190 + 1.191 +static float remap(float val, float inlow, float inhigh, float outlow, float outhigh) 1.192 +{ 1.193 + float t = (val - inlow) / (inhigh - inlow); 1.194 + if(t < 0.0) t = 0.0; 1.195 + if(t > 1.0) t = 1.0; 1.196 + return t * (outhigh - outlow) + outlow; 1.197 +} 1.198 + 1.199 +} // namespace goatkit