nanogui-experiments/fancyknob.hpp

86 lines
2.4 KiB
C++

#ifndef FANCYKNOB_H
#define FANCYKNOB_H
#include <nanogui/widget.h>
NAMESPACE_BEGIN(nanogui)
class NANOGUI_EXPORT FancyKnob : public Widget {
public:
FancyKnob(Widget* parent, int rad = 80);
float value() const { return m_value; }
void set_value(float value)
{
if (value != m_value)
m_value = value;
}
float min_default() const { return m_default; }
void set_default(float value) { m_default = value; }
float min_value() const { return m_min_val; }
void set_min_value(float value);
float max_value() const { return m_max_val; }
void set_max_value(float value);
float increment() const { return m_increment; }
void set_increment(float value) { m_increment = value; }
float scroll_speed() const { return m_scroll_speed; }
void set_scroll_speed(float value);
const Color& gauge_color() const { return m_gauge_color; }
void set_gauge_color(const Color& color);
std::function<void(float)> callback() const { return m_callback; }
void set_callback(const std::function<void(float)>& callback) { m_callback = callback; }
virtual Vector2i preferred_size(NVGcontext* ctx) const override;
virtual bool mouse_enter_event(const Vector2i& p, bool enter);
virtual bool mouse_drag_event(const Vector2i& p, const Vector2i& rel, int button, int modifiers) override;
virtual bool mouse_button_event(const Vector2i& p, int button, bool down, int modifiers) override;
virtual bool scroll_event(const Vector2i& p, const Vector2f& rel) override;
virtual bool keyboard_event(int key, int scancode, int action, int modifiers);
virtual void draw(NVGcontext* ctx) override;
protected:
bool adjust_value(float value, float incr);
private:
static const float m_pi;
// colors
Color
m_gauge_color,
m_gauge_bg_color,
m_knob_color_1,
m_knob_color_2,
m_outline_color_1,
m_outline_color_2;
// sizes, value and range
float
m_value,
m_min_val,
m_max_val,
m_default,
m_increment,
m_scroll_speed,
m_scroll_increment,
m_drag_increment,
/* value gauge width as ratio of knob radius */
m_gauge_width,
/* value indicator line length as ratio of knob radius */
m_indicator_size;
// behaviour
bool m_fine_mode = false;
std::function<void(float)> m_callback;
};
NAMESPACE_END(nanogui)
#endif // FANCYKNOB_H