Added CheckBox

This commit is contained in:
MihailRis
2023-11-18 23:19:15 +03:00
parent 9cd8870bd6
commit dda9c40c41
5 changed files with 93 additions and 6 deletions
+34
View File
@@ -192,4 +192,38 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
if (consumer_) {
consumer_(value);
}
}
CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) {
color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
}
void CheckBox::draw(Batch2D* batch, Assets* assets) {
if (supplier_) {
checked_ = supplier_();
}
vec2 coord = calcCoord();
batch->texture(nullptr);
batch->color = checked_ ? checkColor : (hover_ ? hoverColor : color_);
batch->rect(coord.x, coord.y, size_.x, size_.y);
}
void CheckBox::mouseRelease(GUI*, int x, int y) {
checked_ = !checked_;
if (consumer_) {
consumer_(checked_);
}
}
void CheckBox::supplier(boolsupplier supplier) {
supplier_ = supplier;
}
void CheckBox::consumer(boolconsumer consumer) {
consumer_ = consumer;
}
CheckBox* CheckBox::checked(bool flag) {
checked_ = flag;
return this;
}
+37 -4
View File
@@ -19,6 +19,9 @@ namespace gui {
typedef std::function<double()> doublesupplier;
typedef std::function<void(double)> doubleconsumer;
typedef std::function<bool()> boolsupplier;
typedef std::function<void(bool)> boolconsumer;
class Label : public UINode {
protected:
std::wstring text_;
@@ -37,7 +40,7 @@ namespace gui {
class Button : public Panel {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 hoverColor {0.05f, 0.1f, 0.15f, 0.75f};
glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f};
std::vector<onaction> actions;
public:
@@ -62,7 +65,8 @@ namespace gui {
wstringsupplier supplier = nullptr;
wstringconsumer consumer = nullptr;
public:
TextBox(std::wstring placeholder, glm::vec4 padding=glm::vec4(2.0f));
TextBox(std::wstring placeholder,
glm::vec4 padding=glm::vec4(2.0f));
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
@@ -79,7 +83,6 @@ namespace gui {
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;
@@ -88,7 +91,11 @@ namespace gui {
double step;
int trackWidth;
public:
TrackBar(double min, double max, double value, double step=1.0, int trackWidth=3);
TrackBar(double min,
double max,
double value,
double step=1.0,
int trackWidth=1);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void supplier(doublesupplier supplier);
@@ -96,6 +103,32 @@ namespace gui {
virtual void mouseMove(GUI*, int x, int y) override;
};
class CheckBox : public UINode {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 checkColor {1.0f, 1.0f, 1.0f, 0.4f};
boolsupplier supplier_ = nullptr;
boolconsumer consumer_ = nullptr;
bool checked_ = false;
public:
CheckBox(bool checked=false);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void mouseRelease(GUI*, int x, int y) override;
virtual void supplier(boolsupplier supplier);
virtual void consumer(boolconsumer consumer);
virtual CheckBox* checked(bool flag);
virtual bool checked() const {
if (supplier_)
return supplier_();
return checked_;
}
};
}
#endif // FRONTEND_GUI_CONTROLS_H_