Settings menu + TrackBar

This commit is contained in:
MihailRis
2023-11-17 16:14:10 +03:00
parent f375e66ad1
commit 7608300750
9 changed files with 158 additions and 18 deletions
+5 -2
View File
@@ -45,11 +45,11 @@ void GUI::act(float delta) {
}
this->hover = hover;
if (Events::clicked(0)) {
if (Events::jclicked(0)) {
if (pressed == nullptr && this->hover) {
pressed = hover;
pressed->click(this, mx, my);
if (focus) {
if (focus && focus != pressed) {
focus->defocus();
}
focus = pressed;
@@ -75,6 +75,9 @@ void GUI::act(float delta) {
for (auto key : Events::pressedKeys) {
focus->keyPressed(key);
}
if (Events::clicked(mousecode::BUTTON_1)) {
focus->mouseMove(this, mx, my);
}
}
}
}
+1
View File
@@ -57,6 +57,7 @@ namespace gui {
glm::vec4 margin() const;
virtual void click(GUI*, int x, int y);
virtual void mouseMove(GUI*, int x, int y) {};
virtual void mouseRelease(GUI*, int x, int y);
bool ispressed() const;
+42
View File
@@ -148,4 +148,46 @@ wstring TextBox::text() const {
if (input.empty())
return placeholder;
return input;
}
TrackBar::TrackBar(double min, double max, double value, double step)
: UINode(vec2(), vec2(32)), min(min), max(max), value(value), step(step) {
color(vec4(0.f, 0.f, 0.f, 0.4f));
}
void TrackBar::draw(Batch2D* batch, Assets* assets) {
if (supplier_) {
value = supplier_();
}
vec2 coord = calcCoord();
batch->texture(nullptr);
batch->color = (hover_ ? hoverColor : color_);
batch->rect(coord.x, coord.y, size_.x, size_.y);
float width = size_.x;
float t = (value - min) / (max-min+trackWidth);
batch->color = trackColor;
batch->rect(coord.x + width * t, coord.y, size_.x * (trackWidth / (max-min)), size_.y);
}
void TrackBar::supplier(doublesupplier supplier) {
this->supplier_ = supplier;
}
void TrackBar::consumer(doubleconsumer consumer) {
this->consumer_ = consumer;
}
void TrackBar::mouseMove(GUI*, int x, int y) {
vec2 coord = calcCoord();
x -= coord.x;
x = x/size_.x * (max-min+trackWidth);
x = (x > max) ? max : x;
x = (x < min) ? min : x;
x = (int)(x / step) * step;
value = x;
if (consumer_) {
consumer_(value);
}
}
+27 -1
View File
@@ -16,6 +16,9 @@ namespace gui {
typedef std::function<std::wstring()> wstringsupplier;
typedef std::function<void(std::wstring)> wstringconsumer;
typedef std::function<double()> doublesupplier;
typedef std::function<void(double)> doubleconsumer;
class Label : public UINode {
protected:
std::wstring text_;
@@ -27,10 +30,11 @@ namespace gui {
virtual Label& text(std::wstring text);
std::wstring text() const;
virtual void draw(Batch2D* batch, Assets* assets);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void textSupplier(wstringsupplier supplier);
};
class Button : public Panel {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
@@ -70,6 +74,28 @@ namespace gui {
virtual bool isfocuskeeper() const override {return true;}
virtual std::wstring text() const;
};
class TrackBar : public UINode {
protected:
glm::vec4 hoverColor {0.01f, 0.02f, 0.03f, 0.5f};
glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f};
Label* label;
doublesupplier supplier_ = nullptr;
doubleconsumer consumer_ = nullptr;
double min;
double max;
double value;
double step;
int trackWidth = 3;
public:
TrackBar(double min, double max, double value, double step=1.0);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void supplier(doublesupplier supplier);
virtual void consumer(doubleconsumer consumer);
virtual void mouseMove(GUI*, int x, int y) override;
};
}
#endif // FRONTEND_GUI_CONTROLS_H_