add iframe 'src' scripting property

This commit is contained in:
MihailRis 2025-04-19 20:34:30 +03:00
parent 8b0f350b69
commit f21d9d0a25
3 changed files with 14 additions and 12 deletions

View File

@ -49,3 +49,7 @@ void InlineFrame::setSize(glm::vec2 size) {
root->setSize(size);
}
}
const std::string& InlineFrame::getSrc() const {
return src;
}

View File

@ -15,6 +15,8 @@ namespace gui {
void act(float delta) override;
void setSize(glm::vec2 size) override;
const std::string& getSrc() const;
private:
std::string src;
std::shared_ptr<UiDocument> document;

View File

@ -13,6 +13,7 @@
#include "graphics/ui/elements/Panel.hpp"
#include "graphics/ui/elements/TextBox.hpp"
#include "graphics/ui/elements/TrackBar.hpp"
#include "graphics/ui/elements/InlineFrame.hpp"
#include "graphics/ui/gui_util.hpp"
#include "graphics/ui/markdown.hpp"
#include "graphics/core/Font.hpp"
@ -330,6 +331,8 @@ static int p_get_markup(UINode* node, lua::State* L) {
static int p_get_src(UINode* node, lua::State* L) {
if (auto image = dynamic_cast<Image*>(node)) {
return lua::pushstring(L, image->getTexture());
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
return lua::pushstring(L, iframe->getSrc());
}
return 0;
}
@ -644,6 +647,8 @@ static void p_set_markup(UINode* node, lua::State* L, int idx) {
static void p_set_src(UINode* node, lua::State* L, int idx) {
if (auto image = dynamic_cast<Image*>(node)) {
image->setTexture(lua::require_string(L, idx));
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
iframe->setSrc(lua::require_string(L, idx));
}
}
static void p_set_value(UINode* node, lua::State* L, int idx) {
@ -704,10 +709,10 @@ static void p_set_inventory(UINode* node, lua::State* L, int idx) {
}
}
static void p_set_focused(
const std::shared_ptr<UINode>& node, lua::State* L, int idx
UINode* node, lua::State* L, int idx
) {
if (lua::toboolean(L, idx) && !node->isFocused()) {
engine->getGUI().setFocus(node);
engine->getGUI().setFocus(node->shared_from_this());
} else if (node->isFocused()) {
node->defocus();
}
@ -771,21 +776,12 @@ static int l_gui_setattr(lua::State* L) {
{"page", p_set_page},
{"inventory", p_set_inventory},
{"cursor", p_set_cursor},
{"focused", p_set_focused},
};
auto func = setters.find(attr);
if (func != setters.end()) {
func->second(node.get(), L, 4);
}
static const std::unordered_map<
std::string_view,
std::function<void(std::shared_ptr<UINode>, lua::State*, int)>>
setters2 {
{"focused", p_set_focused},
};
auto func2 = setters2.find(attr);
if (func2 != setters2.end()) {
func2->second(node, L, 4);
}
return 0;
}