UiDocument
This commit is contained in:
@@ -344,11 +344,12 @@ std::shared_ptr<InventoryView> InventoryView::readXML(
|
||||
LevelFrontend* frontend,
|
||||
InventoryInteraction& interaction,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
const std::string& file,
|
||||
const scripting::Environment& env
|
||||
) {
|
||||
auto view = std::make_shared<InventoryView>(frontend, interaction);
|
||||
|
||||
gui::UiXmlReader reader;
|
||||
gui::UiXmlReader reader(env);
|
||||
reader.add("inventory", [=](gui::UiXmlReader& reader, xml::xmlelement element) {
|
||||
reader.readUINode(reader, element, *view);
|
||||
return view;
|
||||
|
||||
@@ -18,6 +18,10 @@ class ContentIndices;
|
||||
class LevelFrontend;
|
||||
class Inventory;
|
||||
|
||||
namespace scripting {
|
||||
class Environment;
|
||||
}
|
||||
|
||||
using itemsharefunc = std::function<void(ItemStack&)>;
|
||||
using slotcallback = std::function<void(ItemStack&, ItemStack&)>;
|
||||
|
||||
@@ -105,7 +109,8 @@ public:
|
||||
LevelFrontend* frontend,
|
||||
InventoryInteraction& interaction,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
const std::string& file,
|
||||
const scripting::Environment& env
|
||||
);
|
||||
|
||||
static const int SLOT_INTERVAL = 4;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "UiDocument.h"
|
||||
|
||||
#include "gui/UINode.h"
|
||||
#include "gui/panels.h"
|
||||
#include "InventoryView.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
#include "../files/files.h"
|
||||
|
||||
UiDocument::UiDocument(
|
||||
std::string namesp,
|
||||
uidocscript script,
|
||||
std::shared_ptr<gui::UINode> root
|
||||
) {
|
||||
collect(map, root);
|
||||
}
|
||||
|
||||
void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
|
||||
const std::string& id = node->getId();
|
||||
if (!id.empty()) {
|
||||
map[id] = node;
|
||||
}
|
||||
auto container = dynamic_cast<gui::Container*>(node.get());
|
||||
if (container) {
|
||||
for (auto subnode : container->getNodes()) {
|
||||
collect(map, subnode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<UiDocument> UiDocument::readInventory(
|
||||
std::string namesp,
|
||||
fs::path file,
|
||||
LevelFrontend* frontend,
|
||||
InventoryInteraction& interaction
|
||||
) {
|
||||
const std::string text = files::read_string(file);
|
||||
auto env = scripting::create_environment();
|
||||
auto view = InventoryView::readXML(
|
||||
frontend, interaction, text, file.u8string(), *env
|
||||
);
|
||||
uidocscript script {};
|
||||
auto scriptFile = fs::path(file.u8string()+".lua");
|
||||
if (fs::is_regular_file(scriptFile)) {
|
||||
scripting::load_layout_script(scriptFile, script);
|
||||
}
|
||||
return std::make_unique<UiDocument>(namesp, script, view);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef FRONTEND_UI_DOCUMENT_H_
|
||||
#define FRONTEND_UI_DOCUMENT_H_
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace gui {
|
||||
class UINode;
|
||||
}
|
||||
|
||||
class InventoryInteraction;
|
||||
class LevelFrontend;
|
||||
|
||||
struct uidocscript {
|
||||
int environment;
|
||||
bool onopen : 1;
|
||||
bool onclose : 1;
|
||||
};
|
||||
|
||||
using uinodes_map = std::unordered_map<std::string, std::shared_ptr<gui::UINode>>;
|
||||
|
||||
class UiDocument {
|
||||
std::string namesp;
|
||||
uidocscript script;
|
||||
uinodes_map map;
|
||||
std::shared_ptr<gui::UINode> root;
|
||||
public:
|
||||
UiDocument(std::string namesp, uidocscript script, std::shared_ptr<gui::UINode> root);
|
||||
|
||||
const uinodes_map& getMap() const;
|
||||
const std::string& getNamespace() const;
|
||||
|
||||
/* Collect map of all uinodes having identifiers */
|
||||
static void collect(uinodes_map& map, std::shared_ptr<gui::UINode> node);
|
||||
|
||||
/* @return root node is always an InventoryView */
|
||||
static std::unique_ptr<UiDocument> readInventory (
|
||||
std::string namesp,
|
||||
fs::path file,
|
||||
LevelFrontend* frontend,
|
||||
InventoryInteraction& interaction
|
||||
);
|
||||
};
|
||||
|
||||
#endif // FRONTEND_UI_DOCUMENT_H_
|
||||
+21
-16
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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,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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "../objects/Player.h"
|
||||
#include "../physics/Hitbox.h"
|
||||
#include "../maths/voxmaths.h"
|
||||
#include "../files/files.h"
|
||||
#include "gui/controls.h"
|
||||
#include "gui/panels.h"
|
||||
#include "gui/UINode.h"
|
||||
@@ -172,7 +173,7 @@ std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
|
||||
auto indices = content->getIndices();
|
||||
auto player = level->player;
|
||||
auto inventory = player->getInventory();
|
||||
|
||||
|
||||
int itemsCount = indices->countItemDefs();
|
||||
auto accessInventory = std::make_shared<Inventory>(0, itemsCount);
|
||||
for (int id = 1; id < itemsCount; id++) {
|
||||
@@ -376,7 +377,6 @@ void HudRenderer::draw(const GfxContext& ctx){
|
||||
uishader->use();
|
||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||
|
||||
// Draw selected item preview
|
||||
hotbarView->setCoord(glm::vec2(width/2, height-65));
|
||||
hotbarView->setSelected(player->getChosenSlot());
|
||||
|
||||
|
||||
@@ -49,7 +49,6 @@ class HudRenderer {
|
||||
LevelFrontend* frontend;
|
||||
|
||||
std::shared_ptr<gui::UINode> createDebugPanel(Engine* engine);
|
||||
|
||||
std::shared_ptr<InventoryView> createContentAccess();
|
||||
std::shared_ptr<InventoryView> createHotbar();
|
||||
std::shared_ptr<InventoryView> createInventory();
|
||||
|
||||
Reference in New Issue
Block a user