22 #ifndef KJB_CPP_GUI_SLIDER_H
23 #define KJB_CPP_GUI_SLIDER_H
25 #ifdef KJB_HAVE_OPENGL
30 #include <boost/function.hpp>
42 class Slider :
public kjb::gui::Interactive_overlay
44 typedef kjb::gui::Interactive_overlay Base;
52 live_updating_(false),
55 if(value_ < min_ || value_ > max_)
63 Base(x,y, width, std::max(INDICATOR_HEIGHT, BAR_HEIGHT)),
68 live_updating_(false),
72 void set_changed_callback(
const boost::function1<void, Real>& callback,
bool live_updating =
false)
75 live_updating_ = live_updating;
78 virtual void render()
const
81 glPushAttrib(GL_ENABLE_BIT);
82 glPushAttrib(GL_CURRENT_BIT);
83 glDisable(GL_LIGHTING);
84 int bar_center = bar_center_();
86 int bar_right =
x() + width();
87 int bar_bottom = bar_center - BAR_HEIGHT/2;
88 int bar_top = bar_center + BAR_HEIGHT/2;
97 for(
size_t i = 0;
i < 2; ++
i)
99 glVertex2i(bar_left, bar_bottom);
100 glVertex2i(bar_right, bar_bottom);
101 glVertex2i(bar_right, bar_top);
102 glVertex2i(bar_left, bar_top);
108 glBegin(GL_LINE_LOOP);
114 if(has_valid_state_())
116 int current_x = position_from_value_(value_);
117 int indicator_left = current_x - INDICATOR_HEIGHT / 2;
118 int indicator_right = current_x + INDICATOR_HEIGHT / 2;
119 int indicator_bottom = bar_center - INDICATOR_HEIGHT / 2;
120 int indicator_top = bar_center + INDICATOR_HEIGHT / 2;
123 for(
size_t i = 0;
i < 2; ++
i)
125 glVertex2i(indicator_left, indicator_bottom);
126 glVertex2i(indicator_right, indicator_bottom);
127 glVertex2i(indicator_right, indicator_top);
128 glVertex2i(indicator_left, indicator_top);
134 glBegin(GL_LINE_LOOP);
143 virtual bool mouse_event(
int button,
int state,
int u,
int v)
147 if(!has_valid_state_())
return false;
149 if(button != GLUT_LEFT_BUTTON)
return false;
150 if(u <
x() || u >
x() + width() || v < y() || v > y() +
height())
return false;
152 if(state == GLUT_DOWN)
176 else if(state == GLUT_UP)
181 set_value(value_from_position_(u));
185 callback_(get_value());
192 virtual bool mouse_double_event(
int ,
int ,
int ,
int )
197 virtual bool motion_event(
int x,
int y)
199 if(!grabbed_)
return false;
201 if(!has_valid_state_())
return false;
203 set_value(value_from_position_(x));
206 if(callback_ && live_updating_)
207 callback_(get_value());
212 virtual bool passive_motion_event(
int ,
int )
217 virtual bool keyboard_event(
unsigned int ,
int ,
int )
223 virtual bool special_event(
int ,
int ,
int )
229 void set_value(
const Real& v)
235 void set_min(
const Real&
min)
241 void set_max(
const Real&
max)
247 const Real& get_value()
const
252 void redisplay()
const
260 Real value_from_position_(
int pos)
const
262 assert(has_valid_state_());
264 double delta = pos -
x();
266 if(delta < 0.0)
return min_;
267 if(delta > 1.0)
return max_;
268 return delta * (max_ - min_) + min_;
271 int position_from_value_(
const Real& v)
const
273 assert(has_valid_state_());
274 double delta = v - min_;
275 delta /= (max_ - min_);
276 if(delta < 0 || delta > 1.0)
278 return x() + delta * width();
281 inline int bar_center_()
const
283 return y() +
height() / 2.0;
286 bool has_valid_state_()
const
288 if(fabs(max_ - min_) < DBL_EPSILON)
return false;
298 boost::function1<void, Real> callback_;
302 static const int INDICATOR_HEIGHT;
303 static const int BAR_HEIGHT;
307 const int Slider<T>::INDICATOR_HEIGHT = 15;
309 const int Slider<T>::BAR_HEIGHT = 10;
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
height
Definition: APPgetLargeConnectedEdges.m:33
x
Definition: APPgetLargeConnectedEdges.m:100
void render(const Cuboid &c)
Definition: psi_weighted_box.cpp:56
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
#define KJB_THROW_3(ex, fmt, params)
Definition: l_exception.h:56
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
void glColor(const Vector &color)
glColor for a kjb::Vector.
Definition: gr_opengl.cpp:196
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48