Extract standalone Playground SDK source foundation
This commit is contained in:
commit
d49c22a06c
3567 changed files with 1657902 additions and 0 deletions
61
ui/controls.cpp
Normal file
61
ui/controls.cpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#include "ui/controls.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace playground {
|
||||
|
||||
bool Rect::contains(float px, float py) const {
|
||||
return w > 0 && h > 0 && px >= x && py >= y && px < x + w && py < y + h;
|
||||
}
|
||||
|
||||
void Rollover::move(float x, float y) { hovered_ = bounds_.contains(x, y); }
|
||||
|
||||
bool Rollover::down(float x, float y) {
|
||||
move(x, y);
|
||||
pressed_ = hovered_;
|
||||
return pressed_;
|
||||
}
|
||||
|
||||
bool Rollover::up(float x, float y) {
|
||||
move(x, y);
|
||||
const bool clicked = pressed_ && hovered_;
|
||||
pressed_ = false;
|
||||
return clicked;
|
||||
}
|
||||
|
||||
void Rollover::cancel() { pressed_ = false; hovered_ = false; }
|
||||
|
||||
Slider::Slider(Rect track, float minimum, float maximum, float value, float step)
|
||||
: track_(track), minimum_(std::min(minimum, maximum)),
|
||||
maximum_(std::max(minimum, maximum)), value_(minimum_), step_(std::max(0.0f, step)) {
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
void Slider::setValue(float value) {
|
||||
value_ = std::clamp(value, minimum_, maximum_);
|
||||
if (step_ > 0) {
|
||||
value_ = minimum_ + std::round((value_ - minimum_) / step_) * step_;
|
||||
value_ = std::clamp(value_, minimum_, maximum_);
|
||||
}
|
||||
}
|
||||
|
||||
float Slider::fraction() const {
|
||||
return maximum_ > minimum_ ? (value_ - minimum_) / (maximum_ - minimum_) : 0.0f;
|
||||
}
|
||||
|
||||
bool Slider::update(float x) {
|
||||
const float previous = value_;
|
||||
const float fraction = track_.w > 0 ? std::clamp((x - track_.x) / track_.w, 0.0f, 1.0f) : 0.0f;
|
||||
setValue(minimum_ + fraction * (maximum_ - minimum_));
|
||||
return value_ != previous;
|
||||
}
|
||||
|
||||
bool Slider::down(float x, float y) {
|
||||
dragging_ = track_.contains(x, y);
|
||||
if (dragging_) update(x);
|
||||
return dragging_;
|
||||
}
|
||||
|
||||
bool Slider::move(float x, float) { return dragging_ && update(x); }
|
||||
|
||||
} // namespace playground
|
||||
Loading…
Add table
Add a link
Reference in a new issue