UiDocument
This commit is contained in:
@@ -255,7 +255,7 @@ void ContentLoader::loadBlock(Block* def, std::string full, std::string name) {
|
|||||||
|
|
||||||
fs::path scriptfile = folder/fs::path("scripts/"+def->scriptName+".lua");
|
fs::path scriptfile = folder/fs::path("scripts/"+def->scriptName+".lua");
|
||||||
if (fs::is_regular_file(scriptfile)) {
|
if (fs::is_regular_file(scriptfile)) {
|
||||||
scripting::load_block_script(full, scriptfile, &def->rt.funcsset);
|
scripting::load_block_script(full, scriptfile, def->rt.funcsset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,7 +267,7 @@ void ContentLoader::loadItem(ItemDef* def, std::string full, std::string name) {
|
|||||||
|
|
||||||
fs::path scriptfile = folder/fs::path("scripts/"+def->scriptName+".lua");
|
fs::path scriptfile = folder/fs::path("scripts/"+def->scriptName+".lua");
|
||||||
if (fs::is_regular_file(scriptfile)) {
|
if (fs::is_regular_file(scriptfile)) {
|
||||||
scripting::load_item_script(full, scriptfile, &def->rt.funcsset);
|
scripting::load_item_script(full, scriptfile, def->rt.funcsset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -344,11 +344,12 @@ std::shared_ptr<InventoryView> InventoryView::readXML(
|
|||||||
LevelFrontend* frontend,
|
LevelFrontend* frontend,
|
||||||
InventoryInteraction& interaction,
|
InventoryInteraction& interaction,
|
||||||
const std::string& src,
|
const std::string& src,
|
||||||
const std::string& file
|
const std::string& file,
|
||||||
|
const scripting::Environment& env
|
||||||
) {
|
) {
|
||||||
auto view = std::make_shared<InventoryView>(frontend, interaction);
|
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.add("inventory", [=](gui::UiXmlReader& reader, xml::xmlelement element) {
|
||||||
reader.readUINode(reader, element, *view);
|
reader.readUINode(reader, element, *view);
|
||||||
return view;
|
return view;
|
||||||
|
|||||||
@@ -18,6 +18,10 @@ class ContentIndices;
|
|||||||
class LevelFrontend;
|
class LevelFrontend;
|
||||||
class Inventory;
|
class Inventory;
|
||||||
|
|
||||||
|
namespace scripting {
|
||||||
|
class Environment;
|
||||||
|
}
|
||||||
|
|
||||||
using itemsharefunc = std::function<void(ItemStack&)>;
|
using itemsharefunc = std::function<void(ItemStack&)>;
|
||||||
using slotcallback = std::function<void(ItemStack&, ItemStack&)>;
|
using slotcallback = std::function<void(ItemStack&, ItemStack&)>;
|
||||||
|
|
||||||
@@ -105,7 +109,8 @@ public:
|
|||||||
LevelFrontend* frontend,
|
LevelFrontend* frontend,
|
||||||
InventoryInteraction& interaction,
|
InventoryInteraction& interaction,
|
||||||
const std::string& src,
|
const std::string& src,
|
||||||
const std::string& file
|
const std::string& file,
|
||||||
|
const scripting::Environment& env
|
||||||
);
|
);
|
||||||
|
|
||||||
static const int SLOT_INTERVAL = 4;
|
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_
|
||||||
+20
-15
@@ -5,10 +5,7 @@
|
|||||||
using gui::UINode;
|
using gui::UINode;
|
||||||
using gui::Align;
|
using gui::Align;
|
||||||
|
|
||||||
using glm::vec2;
|
UINode::UINode(glm::vec2 coord, glm::vec2 size) : coord(coord), size(size) {
|
||||||
using glm::vec4;
|
|
||||||
|
|
||||||
UINode::UINode(vec2 coord, vec2 size) : coord(coord), size(size) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UINode::~UINode() {
|
UINode::~UINode() {
|
||||||
@@ -67,13 +64,13 @@ bool UINode::isFocused() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool UINode::isInside(glm::vec2 pos) {
|
bool UINode::isInside(glm::vec2 pos) {
|
||||||
vec2 coord = calcCoord();
|
glm::vec2 coord = calcCoord();
|
||||||
vec2 size = getSize();
|
glm::vec2 size = getSize();
|
||||||
return (pos.x >= coord.x && pos.y >= coord.y &&
|
return (pos.x >= coord.x && pos.y >= coord.y &&
|
||||||
pos.x < coord.x + size.x && pos.y < coord.y + size.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) {
|
if (!interactive) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -96,7 +93,7 @@ bool UINode::isResizing() const {
|
|||||||
return resizing;
|
return resizing;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec2 UINode::calcCoord() const {
|
glm::vec2 UINode::calcCoord() const {
|
||||||
if (parent) {
|
if (parent) {
|
||||||
return coord + parent->calcCoord() + parent->contentOffset();
|
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;
|
this->coord = coord;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec2 UINode::getSize() const {
|
glm::vec2 UINode::getSize() const {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UINode::setSize(vec2 size) {
|
void UINode::setSize(glm::vec2 size) {
|
||||||
this->size = size;
|
this->size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UINode::setColor(vec4 color) {
|
void UINode::setColor(glm::vec4 color) {
|
||||||
this->color = color;
|
this->color = color;
|
||||||
this->hoverColor = color;
|
this->hoverColor = color;
|
||||||
}
|
}
|
||||||
@@ -134,17 +131,25 @@ glm::vec4 UINode::getHoverColor() const {
|
|||||||
return hoverColor;
|
return hoverColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec4 UINode::getColor() const {
|
glm::vec4 UINode::getColor() const {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UINode::setMargin(vec4 margin) {
|
void UINode::setMargin(glm::vec4 margin) {
|
||||||
this->margin = margin;
|
this->margin = margin;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec4 UINode::getMargin() const {
|
glm::vec4 UINode::getMargin() const {
|
||||||
return margin;
|
return margin;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UINode::lock() {
|
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 {
|
class UINode {
|
||||||
|
std::string id = "";
|
||||||
protected:
|
protected:
|
||||||
glm::vec2 coord;
|
glm::vec2 coord;
|
||||||
glm::vec2 size;
|
glm::vec2 size;
|
||||||
@@ -110,6 +111,9 @@ namespace gui {
|
|||||||
virtual void setSize(glm::vec2 size);
|
virtual void setSize(glm::vec2 size);
|
||||||
virtual void refresh() {};
|
virtual void refresh() {};
|
||||||
virtual void lock();
|
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 */
|
/* Read basic UINode properties */
|
||||||
static void _readUINode(xml::xmlelement element, UINode& node) {
|
static void _readUINode(xml::xmlelement element, UINode& node) {
|
||||||
|
if (element->has("id")) {
|
||||||
|
node.setId(element->attr("id").getText());
|
||||||
|
}
|
||||||
if (element->has("coord")) {
|
if (element->has("coord")) {
|
||||||
node.setCoord(element->attr("coord").asVec2());
|
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);
|
_readPanel(reader, element, *button);
|
||||||
|
|
||||||
if (element->has("onclick")) {
|
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*) {
|
button->listenAction([callback](GUI*) {
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
@@ -139,7 +142,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
|
|||||||
return textbox;
|
return textbox;
|
||||||
}
|
}
|
||||||
|
|
||||||
UiXmlReader::UiXmlReader() {
|
UiXmlReader::UiXmlReader(const scripting::Environment& env) : env(env) {
|
||||||
add("label", readLabel);
|
add("label", readLabel);
|
||||||
add("button", readButton);
|
add("button", readButton);
|
||||||
add("textbox", readTextBox);
|
add("textbox", readTextBox);
|
||||||
@@ -181,3 +184,7 @@ std::shared_ptr<UINode> UiXmlReader::readXML(
|
|||||||
const std::string& UiXmlReader::getFilename() const {
|
const std::string& UiXmlReader::getFilename() const {
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const scripting::Environment& UiXmlReader::getEnvironment() const {
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,10 @@
|
|||||||
#include "GUI.h"
|
#include "GUI.h"
|
||||||
#include "../../coders/xml.h"
|
#include "../../coders/xml.h"
|
||||||
|
|
||||||
|
namespace scripting {
|
||||||
|
class Environment;
|
||||||
|
}
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
class UiXmlReader;
|
class UiXmlReader;
|
||||||
|
|
||||||
@@ -15,8 +19,9 @@ namespace gui {
|
|||||||
class UiXmlReader {
|
class UiXmlReader {
|
||||||
std::unordered_map<std::string, uinode_reader> readers;
|
std::unordered_map<std::string, uinode_reader> readers;
|
||||||
std::string filename;
|
std::string filename;
|
||||||
|
const scripting::Environment& env;
|
||||||
public:
|
public:
|
||||||
UiXmlReader();
|
UiXmlReader(const scripting::Environment& env);
|
||||||
|
|
||||||
void add(const std::string& tag, uinode_reader reader);
|
void add(const std::string& tag, uinode_reader reader);
|
||||||
|
|
||||||
@@ -44,6 +49,7 @@ namespace gui {
|
|||||||
xml::xmlelement root
|
xml::xmlelement root
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const scripting::Environment& getEnvironment() const;
|
||||||
const std::string& getFilename() const;
|
const std::string& getFilename() const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,6 +144,10 @@ void Container::setSize(glm::vec2 size) {
|
|||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::vector<std::shared_ptr<UINode>>& Container::getNodes() const {
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
Panel::Panel(vec2 size, glm::vec4 padding, float interval)
|
Panel::Panel(vec2 size, glm::vec4 padding, float interval)
|
||||||
: Container(vec2(), size),
|
: Container(vec2(), size),
|
||||||
padding(padding),
|
padding(padding),
|
||||||
|
|||||||
@@ -46,7 +46,9 @@ namespace gui {
|
|||||||
virtual void setScrollable(bool flag);
|
virtual void setScrollable(bool flag);
|
||||||
void listenInterval(float interval, ontimeout callback, int repeat=-1);
|
void listenInterval(float interval, ontimeout callback, int repeat=-1);
|
||||||
virtual glm::vec2 contentOffset() override {return glm::vec2(0.0f, scroll);};
|
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 {
|
class Panel : public Container {
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
#include "../objects/Player.h"
|
#include "../objects/Player.h"
|
||||||
#include "../physics/Hitbox.h"
|
#include "../physics/Hitbox.h"
|
||||||
#include "../maths/voxmaths.h"
|
#include "../maths/voxmaths.h"
|
||||||
|
#include "../files/files.h"
|
||||||
#include "gui/controls.h"
|
#include "gui/controls.h"
|
||||||
#include "gui/panels.h"
|
#include "gui/panels.h"
|
||||||
#include "gui/UINode.h"
|
#include "gui/UINode.h"
|
||||||
@@ -376,7 +377,6 @@ void HudRenderer::draw(const GfxContext& ctx){
|
|||||||
uishader->use();
|
uishader->use();
|
||||||
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
uishader->uniformMatrix("u_projview", uicamera->getProjView());
|
||||||
|
|
||||||
// Draw selected item preview
|
|
||||||
hotbarView->setCoord(glm::vec2(width/2, height-65));
|
hotbarView->setCoord(glm::vec2(width/2, height-65));
|
||||||
hotbarView->setSelected(player->getChosenSlot());
|
hotbarView->setSelected(player->getChosenSlot());
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ class HudRenderer {
|
|||||||
LevelFrontend* frontend;
|
LevelFrontend* frontend;
|
||||||
|
|
||||||
std::shared_ptr<gui::UINode> createDebugPanel(Engine* engine);
|
std::shared_ptr<gui::UINode> createDebugPanel(Engine* engine);
|
||||||
|
|
||||||
std::shared_ptr<InventoryView> createContentAccess();
|
std::shared_ptr<InventoryView> createContentAccess();
|
||||||
std::shared_ptr<InventoryView> createHotbar();
|
std::shared_ptr<InventoryView> createHotbar();
|
||||||
std::shared_ptr<InventoryView> createInventory();
|
std::shared_ptr<InventoryView> createInventory();
|
||||||
|
|||||||
@@ -51,6 +51,16 @@ bool lua::LuaState::getglobal(const std::string& name) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool lua::LuaState::hasglobal(const std::string& name) {
|
||||||
|
lua_getglobal(L, name.c_str());
|
||||||
|
if (lua_isnil(L, lua_gettop(L))) {
|
||||||
|
lua_pop(L, lua_gettop(L));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
lua_pop(L, lua_gettop(L));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void lua::LuaState::setglobal(const std::string& name) {
|
void lua::LuaState::setglobal(const std::string& name) {
|
||||||
lua_setglobal(L, name.c_str());
|
lua_setglobal(L, name.c_str());
|
||||||
}
|
}
|
||||||
@@ -196,6 +206,18 @@ int lua::LuaState::createEnvironment() {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lua::LuaState::removeEnvironment(int id) {
|
||||||
|
if (currentEnvironment == id) {
|
||||||
|
setEnvironment(0);
|
||||||
|
}
|
||||||
|
lua_pushnil(L);
|
||||||
|
setglobal("_N"+util::mangleid(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
int lua::LuaState::getEnvironment() const {
|
||||||
|
return currentEnvironment;
|
||||||
|
}
|
||||||
|
|
||||||
void lua::LuaState::setEnvironment(int id) {
|
void lua::LuaState::setEnvironment(int id) {
|
||||||
if (id == 0) {
|
if (id == 0) {
|
||||||
getglobal(":G");
|
getglobal(":G");
|
||||||
|
|||||||
@@ -41,10 +41,13 @@ namespace lua {
|
|||||||
void addfunc(const std::string& name, lua_CFunction func);
|
void addfunc(const std::string& name, lua_CFunction func);
|
||||||
bool getglobal(const std::string& name);
|
bool getglobal(const std::string& name);
|
||||||
void setglobal(const std::string& name);
|
void setglobal(const std::string& name);
|
||||||
|
bool hasglobal(const std::string& name);
|
||||||
bool rename(const std::string& from, const std::string& to);
|
bool rename(const std::string& from, const std::string& to);
|
||||||
void remove(const std::string& name);;
|
void remove(const std::string& name);;
|
||||||
void createFuncs();
|
void createFuncs();
|
||||||
int createEnvironment();
|
int createEnvironment();
|
||||||
|
void removeEnvironment(int id);
|
||||||
|
int getEnvironment() const;
|
||||||
void setEnvironment(int id);
|
void setEnvironment(int id);
|
||||||
|
|
||||||
const std::string storeAnonymous();
|
const std::string storeAnonymous();
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include "../../voxels/Block.h"
|
#include "../../voxels/Block.h"
|
||||||
#include "../../items/ItemDef.h"
|
#include "../../items/ItemDef.h"
|
||||||
#include "../../logic/BlocksController.h"
|
#include "../../logic/BlocksController.h"
|
||||||
|
#include "../../frontend/UiDocument.h"
|
||||||
#include "../../engine.h"
|
#include "../../engine.h"
|
||||||
#include "LuaState.h"
|
#include "LuaState.h"
|
||||||
#include "../../util/stringutil.h"
|
#include "../../util/stringutil.h"
|
||||||
@@ -28,6 +29,19 @@ const Content* scripting::content = nullptr;
|
|||||||
const ContentIndices* scripting::indices = nullptr;
|
const ContentIndices* scripting::indices = nullptr;
|
||||||
BlocksController* scripting::blocks = nullptr;
|
BlocksController* scripting::blocks = nullptr;
|
||||||
|
|
||||||
|
Environment::Environment(int env) : env(env) {
|
||||||
|
}
|
||||||
|
|
||||||
|
Environment::~Environment() {
|
||||||
|
if (env) {
|
||||||
|
state->removeEnvironment(env);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Environment::getId() const {
|
||||||
|
return env;
|
||||||
|
}
|
||||||
|
|
||||||
void load_script(fs::path name) {
|
void load_script(fs::path name) {
|
||||||
auto paths = scripting::engine->getPaths();
|
auto paths = scripting::engine->getPaths();
|
||||||
fs::path file = paths->getResources()/fs::path("scripts")/name;
|
fs::path file = paths->getResources()/fs::path("scripts")/name;
|
||||||
@@ -54,6 +68,19 @@ runnable scripting::create_runnable(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runnable scripting::create_runnable(
|
||||||
|
const std::string& file,
|
||||||
|
const std::string& src,
|
||||||
|
const Environment& env
|
||||||
|
) {
|
||||||
|
return [=](){
|
||||||
|
int previous = state->getEnvironment();
|
||||||
|
state->setEnvironment(env.getId());
|
||||||
|
state->execute(src, file);
|
||||||
|
state->setEnvironment(previous);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
wstringconsumer scripting::create_wstring_consumer(
|
wstringconsumer scripting::create_wstring_consumer(
|
||||||
const std::string& src,
|
const std::string& src,
|
||||||
const std::string& file
|
const std::string& file
|
||||||
@@ -75,6 +102,10 @@ wstringconsumer scripting::create_wstring_consumer(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<Environment> scripting::create_environment() {
|
||||||
|
return std::make_unique<Environment>(state->createEnvironment());
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
||||||
scripting::level = level;
|
scripting::level = level;
|
||||||
scripting::content = level->content;
|
scripting::content = level->content;
|
||||||
@@ -186,27 +217,27 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset) {
|
void scripting::load_block_script(std::string prefix, fs::path file, block_funcs_set& funcsset) {
|
||||||
std::string src = files::read_string(file);
|
std::string src = files::read_string(file);
|
||||||
std::cout << "loading script " << file.u8string() << std::endl;
|
std::cout << "loading script " << file.u8string() << std::endl;
|
||||||
state->execute(src, file.u8string());
|
state->execute(src, file.u8string());
|
||||||
|
|
||||||
funcsset->init=state->rename("init", prefix+".init");
|
funcsset.init=state->rename("init", prefix+".init");
|
||||||
funcsset->update=state->rename("on_update", prefix+".update");
|
funcsset.update=state->rename("on_update", prefix+".update");
|
||||||
funcsset->randupdate=state->rename("on_random_update", prefix+".randupdate");
|
funcsset.randupdate=state->rename("on_random_update", prefix+".randupdate");
|
||||||
funcsset->onbroken=state->rename("on_broken", prefix+".broken");
|
funcsset.onbroken=state->rename("on_broken", prefix+".broken");
|
||||||
funcsset->onplaced=state->rename("on_placed", prefix+".placed");
|
funcsset.onplaced=state->rename("on_placed", prefix+".placed");
|
||||||
funcsset->oninteract=state->rename("on_interact", prefix+".oninteract");
|
funcsset.oninteract=state->rename("on_interact", prefix+".oninteract");
|
||||||
funcsset->onblockstick=state->rename("on_blocks_tick", prefix+".blockstick");
|
funcsset.onblockstick=state->rename("on_blocks_tick", prefix+".blockstick");
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset) {
|
void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_set& funcsset) {
|
||||||
std::string src = files::read_string(file);
|
std::string src = files::read_string(file);
|
||||||
std::cout << "loading script " << file.u8string() << std::endl;
|
std::cout << "loading script " << file.u8string() << std::endl;
|
||||||
state->execute(src, file.u8string());
|
state->execute(src, file.u8string());
|
||||||
funcsset->init=state->rename("init", prefix+".init");
|
funcsset.init=state->rename("init", prefix+".init");
|
||||||
funcsset->on_use_on_block=state->rename("on_use_on_block", prefix+".useon");
|
funcsset.on_use_on_block=state->rename("on_use_on_block", prefix+".useon");
|
||||||
funcsset->on_block_break_by=state->rename("on_block_break_by", prefix+".blockbreakby");
|
funcsset.on_block_break_by=state->rename("on_block_break_by", prefix+".blockbreakby");
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::load_world_script(std::string prefix, fs::path file) {
|
void scripting::load_world_script(std::string prefix, fs::path file) {
|
||||||
@@ -222,6 +253,17 @@ void scripting::load_world_script(std::string prefix, fs::path file) {
|
|||||||
state->rename("on_world_quit", prefix+".worldquit");
|
state->rename("on_world_quit", prefix+".worldquit");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void scripting::load_layout_script(fs::path file, uidocscript& script) {
|
||||||
|
std::string src = files::read_string(file);
|
||||||
|
std::cout << "loading script " << file.u8string() << std::endl;
|
||||||
|
|
||||||
|
script.environment = state->createEnvironment();
|
||||||
|
state->loadbuffer(src, file.u8string());
|
||||||
|
state->callNoThrow(0);
|
||||||
|
script.onopen = state->hasglobal("on_open");
|
||||||
|
script.onclose = state->hasglobal("on_close");
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::close() {
|
void scripting::close() {
|
||||||
delete state;
|
delete state;
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class Player;
|
|||||||
class ItemDef;
|
class ItemDef;
|
||||||
struct block_funcs_set;
|
struct block_funcs_set;
|
||||||
struct item_funcs_set;
|
struct item_funcs_set;
|
||||||
|
struct uidocscript;
|
||||||
class BlocksController;
|
class BlocksController;
|
||||||
|
|
||||||
namespace scripting {
|
namespace scripting {
|
||||||
@@ -25,6 +26,15 @@ namespace scripting {
|
|||||||
extern Level* level;
|
extern Level* level;
|
||||||
extern BlocksController* blocks;
|
extern BlocksController* blocks;
|
||||||
|
|
||||||
|
class Environment {
|
||||||
|
int env;
|
||||||
|
public:
|
||||||
|
Environment(int env);
|
||||||
|
~Environment();
|
||||||
|
|
||||||
|
int getId() const;
|
||||||
|
};
|
||||||
|
|
||||||
void initialize(Engine* engine);
|
void initialize(Engine* engine);
|
||||||
|
|
||||||
runnable create_runnable(
|
runnable create_runnable(
|
||||||
@@ -32,11 +42,19 @@ namespace scripting {
|
|||||||
const std::string& source
|
const std::string& source
|
||||||
);
|
);
|
||||||
|
|
||||||
|
runnable create_runnable(
|
||||||
|
const std::string& filename,
|
||||||
|
const std::string& source,
|
||||||
|
const Environment& env
|
||||||
|
);
|
||||||
|
|
||||||
wstringconsumer create_wstring_consumer(
|
wstringconsumer create_wstring_consumer(
|
||||||
const std::string& src,
|
const std::string& src,
|
||||||
const std::string& file="<string>"
|
const std::string& file="<string>"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
std::unique_ptr<Environment> create_environment();
|
||||||
|
|
||||||
void on_world_load(Level* level, BlocksController* blocks);
|
void on_world_load(Level* level, BlocksController* blocks);
|
||||||
void on_world_save();
|
void on_world_save();
|
||||||
void on_world_quit();
|
void on_world_quit();
|
||||||
@@ -48,8 +66,9 @@ namespace scripting {
|
|||||||
bool on_block_interact(Player* player, const Block* block, int x, int y, int z);
|
bool on_block_interact(Player* player, const Block* block, int x, int y, int z);
|
||||||
bool on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z);
|
bool on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||||
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||||
void load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset);
|
void load_block_script(std::string prefix, fs::path file, block_funcs_set& funcsset);
|
||||||
void load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset);
|
void load_item_script(std::string prefix, fs::path file, item_funcs_set& funcsset);
|
||||||
void load_world_script(std::string prefix, fs::path file);
|
void load_world_script(std::string prefix, fs::path file);
|
||||||
|
void load_layout_script(fs::path file, uidocscript& script);
|
||||||
void close();
|
void close();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user