RichButton removed, use Container instead
This commit is contained in:
@@ -43,12 +43,22 @@ UINode* UINode::getParent() const {
|
||||
return parent;
|
||||
}
|
||||
|
||||
UINode* UINode::listenAction(onaction action) {
|
||||
actions.push_back(action);
|
||||
return this;
|
||||
}
|
||||
|
||||
void UINode::click(GUI*, int x, int y) {
|
||||
pressed = true;
|
||||
}
|
||||
|
||||
void UINode::mouseRelease(GUI*, int x, int y) {
|
||||
void UINode::mouseRelease(GUI* gui, int x, int y) {
|
||||
pressed = false;
|
||||
if (isInside(glm::vec2(x, y))) {
|
||||
for (auto callback : actions) {
|
||||
callback(gui);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool UINode::isPressed() const {
|
||||
@@ -136,6 +146,7 @@ void UINode::setMinSize(glm::vec2 minSize) {
|
||||
void UINode::setColor(glm::vec4 color) {
|
||||
this->color = color;
|
||||
this->hoverColor = color;
|
||||
this->pressedColor = color;
|
||||
}
|
||||
|
||||
void UINode::setHoverColor(glm::vec4 newColor) {
|
||||
@@ -150,6 +161,14 @@ glm::vec4 UINode::getColor() const {
|
||||
return color;
|
||||
}
|
||||
|
||||
glm::vec4 UINode::getPressedColor() const {
|
||||
return pressedColor;
|
||||
}
|
||||
|
||||
void UINode::setPressedColor(glm::vec4 color) {
|
||||
pressedColor = color;
|
||||
}
|
||||
|
||||
void UINode::setMargin(glm::vec4 margin) {
|
||||
this->margin = margin;
|
||||
}
|
||||
|
||||
@@ -55,6 +55,8 @@ namespace gui {
|
||||
glm::vec4 color {1.0f};
|
||||
/// @brief element color when mouse is over it
|
||||
glm::vec4 hoverColor {1.0f};
|
||||
/// @brief element color when clicked
|
||||
glm::vec4 pressedColor {1.0f};
|
||||
/// @brief element margin (only supported for Panel sub-nodes)
|
||||
glm::vec4 margin {1.0f};
|
||||
/// @brief is element visible
|
||||
@@ -77,6 +79,8 @@ namespace gui {
|
||||
UINode* parent = nullptr;
|
||||
/// @brief position supplier for the element (called on parent element size update)
|
||||
vec2supplier positionfunc = nullptr;
|
||||
/// @brief 'onclick' callbacks
|
||||
std::vector<onaction> actions;
|
||||
|
||||
UINode(glm::vec2 size);
|
||||
public:
|
||||
@@ -110,6 +114,9 @@ namespace gui {
|
||||
virtual void setHoverColor(glm::vec4 newColor);
|
||||
glm::vec4 getHoverColor() const;
|
||||
|
||||
virtual glm::vec4 getPressedColor() const;
|
||||
virtual void setPressedColor(glm::vec4 color);
|
||||
|
||||
virtual void setMargin(glm::vec4 margin);
|
||||
glm::vec4 getMargin() const;
|
||||
|
||||
@@ -120,6 +127,8 @@ namespace gui {
|
||||
/// @brief Get element z-index
|
||||
int getZIndex() const;
|
||||
|
||||
virtual UINode* listenAction(onaction action);
|
||||
|
||||
virtual void onFocus(GUI*) {focused = true;}
|
||||
virtual void click(GUI*, int x, int y);
|
||||
virtual void clicked(GUI*, mousecode button) {}
|
||||
|
||||
@@ -97,7 +97,8 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
|
||||
}
|
||||
|
||||
void Container::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
if (color.a <= 0.0f)
|
||||
glm::vec4 color = isPressed() ? pressedColor : (hover ? hoverColor : this->color);
|
||||
if (color.a <= 0.001f)
|
||||
return;
|
||||
glm::vec2 pos = calcPos();
|
||||
|
||||
|
||||
@@ -229,6 +229,7 @@ Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
|
||||
add(content);
|
||||
setScrollable(false);
|
||||
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
|
||||
setPressedColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.95f));
|
||||
content->setInteractive(false);
|
||||
}
|
||||
|
||||
@@ -258,6 +259,7 @@ Button::Button(
|
||||
label->setInteractive(false);
|
||||
add(label);
|
||||
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
|
||||
setPressedColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.95f));
|
||||
}
|
||||
|
||||
void Button::setText(std::wstring text) {
|
||||
@@ -273,14 +275,6 @@ std::wstring Button::getText() const {
|
||||
return L"";
|
||||
}
|
||||
|
||||
glm::vec4 Button::getPressedColor() const {
|
||||
return pressedColor;
|
||||
}
|
||||
|
||||
void Button::setPressedColor(glm::vec4 color) {
|
||||
pressedColor = color;
|
||||
}
|
||||
|
||||
Button* Button::textSupplier(wstringsupplier supplier) {
|
||||
if (label) {
|
||||
label->textSupplier(supplier);
|
||||
@@ -303,20 +297,6 @@ void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
batch->rect(pos.x, pos.y, size.x, size.y);
|
||||
}
|
||||
|
||||
void Button::mouseRelease(GUI* gui, int x, int y) {
|
||||
UINode::mouseRelease(gui, x, y);
|
||||
if (isInside(glm::vec2(x, y))) {
|
||||
for (auto callback : actions) {
|
||||
callback(gui);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Button* Button::listenAction(onaction action) {
|
||||
actions.push_back(action);
|
||||
return this;
|
||||
}
|
||||
|
||||
void Button::setTextAlign(Align align) {
|
||||
if (label) {
|
||||
label->setAlign(align);
|
||||
@@ -331,33 +311,6 @@ Align Button::getTextAlign() const {
|
||||
return Align::left;
|
||||
}
|
||||
|
||||
// ============================== RichButton ==================================
|
||||
RichButton::RichButton(glm::vec2 size) : Container(size) {
|
||||
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
|
||||
}
|
||||
|
||||
void RichButton::mouseRelease(GUI* gui, int x, int y) {
|
||||
UINode::mouseRelease(gui, x, y);
|
||||
if (isInside(glm::vec2(x, y))) {
|
||||
for (auto callback : actions) {
|
||||
callback(gui);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RichButton* RichButton::listenAction(onaction action) {
|
||||
actions.push_back(action);
|
||||
return this;
|
||||
}
|
||||
|
||||
void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color));
|
||||
batch->rect(pos.x, pos.y, size.x, size.y);
|
||||
}
|
||||
|
||||
// ================================ TextBox ===================================
|
||||
TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
|
||||
: Panel(glm::vec2(200,32), padding, 0),
|
||||
|
||||
@@ -104,8 +104,6 @@ namespace gui {
|
||||
|
||||
class Button : public Panel {
|
||||
protected:
|
||||
glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f};
|
||||
std::vector<onaction> actions;
|
||||
std::shared_ptr<Label> label = nullptr;
|
||||
public:
|
||||
Button(std::shared_ptr<UINode> content,
|
||||
@@ -118,36 +116,17 @@ namespace gui {
|
||||
|
||||
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
|
||||
|
||||
virtual void mouseRelease(GUI*, int x, int y) override;
|
||||
virtual Button* listenAction(onaction action);
|
||||
|
||||
virtual Align getTextAlign() const;
|
||||
virtual void setTextAlign(Align align);
|
||||
|
||||
virtual void setText(std::wstring text);
|
||||
virtual std::wstring getText() const;
|
||||
|
||||
virtual glm::vec4 getPressedColor() const;
|
||||
virtual void setPressedColor(glm::vec4 color);
|
||||
|
||||
virtual Button* textSupplier(wstringsupplier supplier);
|
||||
|
||||
virtual void refresh() override;
|
||||
};
|
||||
|
||||
class RichButton : public Container {
|
||||
protected:
|
||||
glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f};
|
||||
std::vector<onaction> actions;
|
||||
public:
|
||||
RichButton(glm::vec2 size);
|
||||
|
||||
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
|
||||
|
||||
virtual void mouseRelease(GUI*, int x, int y) override;
|
||||
virtual RichButton* listenAction(onaction action);
|
||||
};
|
||||
|
||||
|
||||
class TextBox : public Panel {
|
||||
protected:
|
||||
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
|
||||
|
||||
@@ -54,11 +54,16 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no
|
||||
if (element->has("color")) {
|
||||
glm::vec4 color = element->attr("color").asColor();
|
||||
glm::vec4 hoverColor = color;
|
||||
glm::vec4 pressedColor = color;
|
||||
if (element->has("hover-color")) {
|
||||
hoverColor = node.getHoverColor();
|
||||
}
|
||||
if (element->has("pressed-color")) {
|
||||
pressedColor = node.getPressedColor();
|
||||
}
|
||||
node.setColor(color);
|
||||
node.setHoverColor(hoverColor);
|
||||
node.setPressedColor(pressedColor);
|
||||
}
|
||||
if (element->has("margin")) {
|
||||
node.setMargin(element->attr("margin").asVec4());
|
||||
@@ -83,6 +88,9 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no
|
||||
if (element->has("hover-color")) {
|
||||
node.setHoverColor(element->attr("hover-color").asColor());
|
||||
}
|
||||
if (element->has("pressed-color")) {
|
||||
node.setPressedColor(element->attr("pressed-color").asColor());
|
||||
}
|
||||
std::string alignName = element->attr("align", "").getText();
|
||||
node.setAlign(align_from_string(alignName, node.getAlign()));
|
||||
|
||||
@@ -230,9 +238,6 @@ static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement e
|
||||
if (element->has("text-align")) {
|
||||
button->setTextAlign(align_from_string(element->attr("text-align").getText(), button->getTextAlign()));
|
||||
}
|
||||
if (element->has("pressed-color")) {
|
||||
button->setPressedColor(element->attr("pressed-color").asColor());
|
||||
}
|
||||
return button;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user