inventory script, modules

This commit is contained in:
MihailRis
2024-02-11 17:38:30 +03:00
parent bb1743105d
commit 8e529eb63b
22 changed files with 274 additions and 163 deletions
+4
View File
@@ -296,6 +296,10 @@ std::shared_ptr<SlotView> InventoryView::addSlot(SlotLayout layout) {
return slot;
}
std::shared_ptr<Inventory> InventoryView::getInventory() const {
return inventory;
}
void InventoryView::bind(
std::shared_ptr<Inventory> inventory,
LevelFrontend* frontend,
+2
View File
@@ -115,6 +115,8 @@ public:
std::shared_ptr<SlotView> addSlot(SlotLayout layout);
std::shared_ptr<Inventory> getInventory() const;
static std::shared_ptr<InventoryView> readXML(
const std::string& src,
const std::string& file,
+16 -9
View File
@@ -8,11 +8,11 @@
#include "../frontend/gui/gui_xml.h"
UiDocument::UiDocument(
std::string namesp,
std::string id,
uidocscript script,
std::shared_ptr<gui::UINode> root,
int env
) : namesp(namesp), script(script), root(root), env(env) {
) : id(id), script(script), root(root), env(env) {
collect(map, root);
}
@@ -21,14 +21,22 @@ const uinodes_map& UiDocument::getMap() const {
return map;
}
const std::string& UiDocument::getNamespace() const {
return namesp;
const std::string& UiDocument::getId() const {
return id;
}
const std::shared_ptr<gui::UINode> UiDocument::getRoot() const {
return root;
}
const uidocscript& UiDocument::getScript() const {
return script;
}
int UiDocument::getEnvironment() const {
return env;
}
void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
const std::string& id = node->getId();
if (!id.empty()) {
@@ -42,11 +50,10 @@ void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
}
}
std::unique_ptr<UiDocument> UiDocument::read(std::string namesp, fs::path file) {
std::unique_ptr<UiDocument> UiDocument::read(int env, std::string namesp, fs::path file) {
const std::string text = files::read_string(file);
auto xmldoc = xml::parse(file.u8string(), text);
auto env = scripting::create_environment();
gui::UiXmlReader reader(*env);
gui::UiXmlReader reader(env);
InventoryView::createReaders(reader);
auto view = reader.readXML(
file.u8string(), xmldoc->getRoot()
@@ -54,7 +61,7 @@ std::unique_ptr<UiDocument> UiDocument::read(std::string namesp, fs::path file)
uidocscript script {};
auto scriptFile = fs::path(file.u8string()+".lua");
if (fs::is_regular_file(scriptFile)) {
scripting::load_layout_script(env->getId(), namesp, scriptFile, script);
scripting::load_layout_script(env, namesp, scriptFile, script);
}
return std::make_unique<UiDocument>(namesp, script, view, env.release()->getId());
return std::make_unique<UiDocument>(namesp, script, view, env);
}
+8 -6
View File
@@ -21,26 +21,28 @@ struct uidocscript {
using uinodes_map = std::unordered_map<std::string, std::shared_ptr<gui::UINode>>;
class UiDocument {
std::string namesp;
std::string id;
uidocscript script;
uinodes_map map;
std::shared_ptr<gui::UINode> root;
int env;
public:
UiDocument(
std::string namesp,
std::string id,
uidocscript script,
std::shared_ptr<gui::UINode> root,
int env);
int env
);
const std::string& getId() const;
const uinodes_map& getMap() const;
const std::string& getNamespace() const;
const std::shared_ptr<gui::UINode> getRoot() const;
const uidocscript& getScript() const;
int getEnvironment() const;
/* Collect map of all uinodes having identifiers */
static void collect(uinodes_map& map, std::shared_ptr<gui::UINode> node);
static std::unique_ptr<UiDocument> read (std::string namesp, fs::path file);
static std::unique_ptr<UiDocument> read(int env, std::string namesp, fs::path file);
};
#endif // FRONTEND_UI_DOCUMENT_H_
+2 -2
View File
@@ -125,8 +125,8 @@ static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement e
if (element->has("onclick")) {
auto callback = scripting::create_runnable(
reader.getEnvironment().getId(),
"<onclick>",
element->attr("onclick").getText()
element->attr("onclick").getText(),
"<onclick>"
);
button->listenAction([callback](GUI*) {
callback();
+26 -31
View File
@@ -46,6 +46,7 @@
#include "../core_defs.h"
#include "../items/ItemDef.h"
#include "../items/Inventory.h"
#include "../logic/scripting/scripting.h"
using glm::vec2;
using glm::vec3;
@@ -212,31 +213,6 @@ std::shared_ptr<InventoryView> HudRenderer::createHotbar() {
return view;
}
std::shared_ptr<InventoryView> HudRenderer::createInventory() {
auto level = frontend->getLevel();
auto player = level->player;
auto inventory = player->getInventory();
auto layout = assets->getLayout("core:inventory");
if (layout) {
std::cout << "custom layout used" << std::endl;
auto view = std::dynamic_pointer_cast<InventoryView>(layout->getRoot());
view->bind(inventory, frontend, interaction.get());
return view;
} else {
std::cout << "generated layout used" << std::endl;
SlotLayout slotLayout(-1, glm::vec2(), true, false, [=](ItemStack& stack) {
stack.clear();
}, nullptr);
InventoryBuilder builder;
builder.addGrid(10, inventory->size(), glm::vec2(), 4, true, slotLayout);
auto view = builder.build();
view->bind(inventory, frontend, interaction.get());
return view;
}
}
HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
: assets(engine->getAssets()),
gui(engine->getGUI()),
@@ -265,8 +241,6 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
contentAccessPanel->setScrollable(true);
hotbarView = createHotbar();
inventoryView = createInventory();
darkOverlay = std::make_unique<Panel>(glm::vec2(4000.0f));
darkOverlay->setColor(glm::vec4(0, 0, 0, 0.5f));
@@ -281,13 +255,14 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
gui->addBack(hotbarView);
gui->add(debugPanel);
gui->add(contentAccessPanel);
gui->add(inventoryView);
gui->add(grabbedItemView);
}
HudRenderer::~HudRenderer() {
gui->remove(grabbedItemView);
gui->remove(inventoryView);
if (inventoryView) {
gui->remove(inventoryView);
}
gui->remove(hotbarView);
gui->remove(darkOverlay);
gui->remove(contentAccessPanel);
@@ -330,7 +305,7 @@ void HudRenderer::update(bool visible) {
if (inventoryOpen) {
closeInventory();
} else {
inventoryOpen = true;
openInventory();
}
}
}
@@ -339,7 +314,6 @@ void HudRenderer::update(bool visible) {
}
vec2 invSize = contentAccessPanel->getSize();
inventoryView->setVisible(inventoryOpen);
contentAccessPanel->setVisible(inventoryOpen);
contentAccessPanel->setSize(vec2(invSize.x, Window::height));
hotbarView->setVisible(visible);
@@ -364,10 +338,31 @@ void HudRenderer::update(bool visible) {
darkOverlay->setVisible(pause);
}
void HudRenderer::openInventory() {
auto level = frontend->getLevel();
auto player = level->player;
auto inventory = player->getInventory();
inventoryOpen = true;
gui->remove(grabbedItemView);
inventoryDocument = assets->getLayout("core:inventory");
inventoryView = std::dynamic_pointer_cast<InventoryView>(inventoryDocument->getRoot());
inventoryView->bind(inventory, frontend, interaction.get());
scripting::on_ui_open(inventoryDocument, inventory.get());
gui->add(inventoryView);
gui->add(grabbedItemView);
}
void HudRenderer::closeInventory() {
scripting::on_ui_close(inventoryDocument, inventoryView->getInventory().get());
inventoryOpen = false;
ItemStack& grabbed = interaction->getGrabbedItem();
grabbed.clear();
gui->remove(inventoryView);
inventoryView = nullptr;
inventoryDocument = nullptr;
}
void HudRenderer::draw(const GfxContext& ctx){
+5 -2
View File
@@ -18,6 +18,7 @@ class Engine;
class SlotView;
class InventoryView;
class LevelFrontend;
class UiDocument;
class InventoryInteraction;
namespace gui {
@@ -40,7 +41,6 @@ class HudRenderer {
std::shared_ptr<gui::Panel> contentAccessPanel;
std::shared_ptr<InventoryView> contentAccess;
std::shared_ptr<InventoryView> hotbarView;
std::shared_ptr<InventoryView> inventoryView;
std::shared_ptr<gui::UINode> debugPanel;
std::shared_ptr<gui::Panel> darkOverlay;
std::unique_ptr<InventoryInteraction> interaction;
@@ -48,10 +48,12 @@ class HudRenderer {
gui::GUI* gui;
LevelFrontend* frontend;
std::shared_ptr<InventoryView> inventoryView = nullptr;
UiDocument* inventoryDocument = nullptr;
std::shared_ptr<gui::UINode> createDebugPanel(Engine* engine);
std::shared_ptr<InventoryView> createContentAccess();
std::shared_ptr<InventoryView> createHotbar();
std::shared_ptr<InventoryView> createInventory();
public:
HudRenderer(Engine* engine, LevelFrontend* frontend);
~HudRenderer();
@@ -63,6 +65,7 @@ public:
bool isInventoryOpen() const;
bool isPause() const;
void openInventory();
void closeInventory();
};