playground-sdk/ui/controls.h

48 lines
1.3 KiB
C++

#pragma once
namespace playground {
struct Rect {
float x = 0, y = 0, w = 0, h = 0;
bool contains(float px, float py) const;
};
// Shared pointer state for image buttons and other clickable UI nodes.
class Rollover {
public:
explicit Rollover(Rect bounds = {}) : bounds_(bounds) {}
void setBounds(Rect bounds) { bounds_ = bounds; }
void move(float x, float y);
bool down(float x, float y);
bool up(float x, float y); // true only for a completed click
void cancel();
bool hovered() const { return hovered_; }
bool pressed() const { return pressed_; }
private:
Rect bounds_;
bool hovered_ = false;
bool pressed_ = false;
};
// Horizontal slider with clamped, optionally stepped values.
class Slider {
public:
Slider(Rect track, float minimum, float maximum, float value, float step = 0);
void setBounds(Rect track) { track_ = track; }
void setValue(float value);
bool down(float x, float y);
bool move(float x, float y); // true if value changed while dragging
void up() { dragging_ = false; }
float value() const { return value_; }
float fraction() const;
bool dragging() const { return dragging_; }
private:
bool update(float x);
Rect track_;
float minimum_, maximum_, value_, step_;
bool dragging_ = false;
};
} // namespace playground