UiDocument

This commit is contained in:
MihailRis
2024-02-07 19:29:20 +03:00
parent c902ed506b
commit 1dd0e9f939
17 changed files with 257 additions and 42 deletions
+21 -16
View File
@@ -5,10 +5,7 @@
using gui::UINode;
using gui::Align;
using glm::vec2;
using glm::vec4;
UINode::UINode(vec2 coord, vec2 size) : coord(coord), size(size) {
UINode::UINode(glm::vec2 coord, glm::vec2 size) : coord(coord), size(size) {
}
UINode::~UINode() {
@@ -67,13 +64,13 @@ bool UINode::isFocused() const {
}
bool UINode::isInside(glm::vec2 pos) {
vec2 coord = calcCoord();
vec2 size = getSize();
glm::vec2 coord = calcCoord();
glm::vec2 size = getSize();
return (pos.x >= coord.x && pos.y >= coord.y &&
pos.x < coord.x + size.x && pos.y < coord.y + size.y);
}
std::shared_ptr<UINode> UINode::getAt(vec2 pos, std::shared_ptr<UINode> self) {
std::shared_ptr<UINode> UINode::getAt(glm::vec2 pos, std::shared_ptr<UINode> self) {
if (!interactive) {
return nullptr;
}
@@ -96,7 +93,7 @@ bool UINode::isResizing() const {
return resizing;
}
vec2 UINode::calcCoord() const {
glm::vec2 UINode::calcCoord() const {
if (parent) {
return coord + parent->calcCoord() + parent->contentOffset();
}
@@ -109,19 +106,19 @@ void UINode::scrolled(int value) {
}
}
void UINode::setCoord(vec2 coord) {
void UINode::setCoord(glm::vec2 coord) {
this->coord = coord;
}
vec2 UINode::getSize() const {
glm::vec2 UINode::getSize() const {
return size;
}
void UINode::setSize(vec2 size) {
void UINode::setSize(glm::vec2 size) {
this->size = size;
}
void UINode::setColor(vec4 color) {
void UINode::setColor(glm::vec4 color) {
this->color = color;
this->hoverColor = color;
}
@@ -134,17 +131,25 @@ glm::vec4 UINode::getHoverColor() const {
return hoverColor;
}
vec4 UINode::getColor() const {
glm::vec4 UINode::getColor() const {
return color;
}
void UINode::setMargin(vec4 margin) {
void UINode::setMargin(glm::vec4 margin) {
this->margin = margin;
}
vec4 UINode::getMargin() const {
glm::vec4 UINode::getMargin() const {
return margin;
}
void UINode::lock() {
}
}
void UINode::setId(const std::string& id) {
this->id = id;
}
const std::string& UINode::getId() const {
return id;
}
+4
View File
@@ -21,6 +21,7 @@ namespace gui {
};
class UINode {
std::string id = "";
protected:
glm::vec2 coord;
glm::vec2 size;
@@ -110,6 +111,9 @@ namespace gui {
virtual void setSize(glm::vec2 size);
virtual void refresh() {};
virtual void lock();
void setId(const std::string& id);
const std::string& getId() const;
};
}
+9 -2
View File
@@ -21,6 +21,9 @@ static Align align_from_string(const std::string& str, Align def) {
/* Read basic UINode properties */
static void _readUINode(xml::xmlelement element, UINode& node) {
if (element->has("id")) {
node.setId(element->attr("id").getText());
}
if (element->has("coord")) {
node.setCoord(element->attr("coord").asVec2());
}
@@ -111,7 +114,7 @@ static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement e
_readPanel(reader, element, *button);
if (element->has("onclick")) {
auto callback = scripting::create_runnable("<onclick>", element->attr("onclick").getText());
auto callback = scripting::create_runnable("<onclick>", element->attr("onclick").getText(), reader.getEnvironment());
button->listenAction([callback](GUI*) {
callback();
});
@@ -139,7 +142,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
return textbox;
}
UiXmlReader::UiXmlReader() {
UiXmlReader::UiXmlReader(const scripting::Environment& env) : env(env) {
add("label", readLabel);
add("button", readButton);
add("textbox", readTextBox);
@@ -181,3 +184,7 @@ std::shared_ptr<UINode> UiXmlReader::readXML(
const std::string& UiXmlReader::getFilename() const {
return filename;
}
const scripting::Environment& UiXmlReader::getEnvironment() const {
return env;
}
+7 -1
View File
@@ -7,6 +7,10 @@
#include "GUI.h"
#include "../../coders/xml.h"
namespace scripting {
class Environment;
}
namespace gui {
class UiXmlReader;
@@ -15,8 +19,9 @@ namespace gui {
class UiXmlReader {
std::unordered_map<std::string, uinode_reader> readers;
std::string filename;
const scripting::Environment& env;
public:
UiXmlReader();
UiXmlReader(const scripting::Environment& env);
void add(const std::string& tag, uinode_reader reader);
@@ -44,6 +49,7 @@ namespace gui {
xml::xmlelement root
);
const scripting::Environment& getEnvironment() const;
const std::string& getFilename() const;
};
}
+4
View File
@@ -144,6 +144,10 @@ void Container::setSize(glm::vec2 size) {
refresh();
}
const std::vector<std::shared_ptr<UINode>>& Container::getNodes() const {
return nodes;
}
Panel::Panel(vec2 size, glm::vec4 padding, float interval)
: Container(vec2(), size),
padding(padding),
+3 -1
View File
@@ -46,7 +46,9 @@ namespace gui {
virtual void setScrollable(bool flag);
void listenInterval(float interval, ontimeout callback, int repeat=-1);
virtual glm::vec2 contentOffset() override {return glm::vec2(0.0f, scroll);};
virtual void setSize(glm::vec2 size);
virtual void setSize(glm::vec2 size) override;
const std::vector<std::shared_ptr<UINode>>& getNodes() const;
};
class Panel : public Container {