75 lines
1.9 KiB
C++
75 lines
1.9 KiB
C++
|
#pragma once
|
||
|
|
||
|
#include <halp/custom_widgets.hpp>
|
||
|
#include <avnd/wrappers/controls.hpp>
|
||
|
#include <avnd/concepts/painter.hpp>
|
||
|
|
||
|
struct CustomSlider
|
||
|
{
|
||
|
// Same as above
|
||
|
static constexpr double width() { return 100.; }
|
||
|
static constexpr double height() { return 20.; }
|
||
|
|
||
|
// Needed for changing the ui. It's the type above - it's already defined as-is
|
||
|
// in the helpers library.
|
||
|
halp::transaction<int> transaction;
|
||
|
|
||
|
// Called when the value changes from the host software.
|
||
|
void set_value(const auto& control, int value)
|
||
|
{
|
||
|
this->value = avnd::map_control_to_01(control, value / 127.f);
|
||
|
}
|
||
|
|
||
|
// When transaction.update() is called, this converts the value in the slider
|
||
|
// into one fit for the control definition passed as argument.
|
||
|
static auto value_to_control(auto& control, int value)
|
||
|
{
|
||
|
return avnd::map_control_from_01(control, value / 127.f);
|
||
|
}
|
||
|
|
||
|
// Paint method: same as above
|
||
|
void paint(avnd::painter auto ctx)
|
||
|
{
|
||
|
ctx.set_stroke_color({200, 200, 200, 255});
|
||
|
ctx.set_stroke_width(2.);
|
||
|
ctx.set_fill_color({120, 120, 120, 255});
|
||
|
ctx.begin_path();
|
||
|
ctx.draw_rect(0., 0., width(), height());
|
||
|
ctx.fill();
|
||
|
ctx.stroke();
|
||
|
|
||
|
ctx.begin_path();
|
||
|
ctx.set_fill_color({90, 90, 90, 255});
|
||
|
ctx.draw_rect(2., 2., (width() - 4) * (value / 127.f), (height() - 4));
|
||
|
ctx.fill();
|
||
|
}
|
||
|
|
||
|
// Return true to handle the event. x, y, are the positions of the item in local coordinates.
|
||
|
bool mouse_press(double x, double y)
|
||
|
{
|
||
|
transaction.start();
|
||
|
mouse_move(x, y);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// Obvious :-)
|
||
|
void mouse_move(double x, double y)
|
||
|
{
|
||
|
const int res{static_cast<int>(std::clamp(x / width(), 0., 1.) * 127)};
|
||
|
transaction.update(res);
|
||
|
on_changed(value);
|
||
|
}
|
||
|
|
||
|
// Same
|
||
|
void mouse_release(double x, double y)
|
||
|
{
|
||
|
mouse_move(x, y);
|
||
|
transaction.commit();
|
||
|
}
|
||
|
|
||
|
int value{};
|
||
|
|
||
|
std::function<void(int)> on_changed{[](int){}};
|
||
|
};
|
||
|
|