diff --git a/CMakeLists.txt b/CMakeLists.txt index f928a7ff..a0ed202e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,22 +39,19 @@ set(SOL_HEADERS lib/header/sol2/include_linux) endif() include_directories( - # Include Header Only Libraries - lib/header/catch2/include # Catch2 unit testing library. - lib/header/gzip/include # GZip Compression header library. - ${GLM_HEADERS} # OpenGL Mathematics Library. - ${SOL_HEADERS} # Sol2 Lua Parser library. - lib/header/cute_files/include # Cute file traversal header library. - lib/header/stb_image/include # STB Image processor header library. - lib/header/json/include # JSON header library. - - # Include Static Library Headers + ${GLM_HEADERS} + ${SOL_HEADERS} ${GLEW_HEADERS} ${LUA_HEADERS} ${ASSIMP_HEADERS} ${ENET_HEADERS} ${NOISE_HEADERS} ${PTHREAD_HEADERS} + lib/header/catch2/include + lib/header/gzip/include + lib/header/cute_files/include + lib/header/stb_image/include + lib/header/json/include ) diff --git a/mods/default/script/main.lua b/mods/default/script/main.lua index 1ae46de7..b93bc537 100644 --- a/mods/default/script/main.lua +++ b/mods/default/script/main.lua @@ -27,77 +27,43 @@ for i, v in ipairs(blockTypes) do }); end --- zepha.register_keybind("open_inventory", { --- description = "Open Inventory", --- default = zepha.keys.e, --- on_press = function() --- if zepha.player.menu_state == "" then --- zepha.show_menu("inventory", { --- key = "form_root", --- type = "rect", --- --- size = "1920px, 1080px", --- background = "#0004", --- --- children = {{ --- key = "inv_root", --- type = "rect", --- --- position = "202px, 138px", --- size = "236px, 118px", --- padding = "20px, 10px, 8px, 10px", --- background = "inventory" --- }, { --- key = "hotwheel_root", --- type = "rect", --- --- position = "296px, 268px", --- size = "52px, 52px", --- background = "inventory_wheel" --- } } --- }); --- else --- zepha.close_menu() --- end --- -- print(zepha.player.menu_state) --- -- zepha.show_menu([[ --- -- rect[form_root] --- -- size=1920px 1080px --- -- background=#0004 --- -- --- -- rect[inv_root] --- -- position=202px 138px --- -- size=236px 138px --- -- padding=20px 10px 8px 10px --- -- background=img("inventory") --- -- end --- -- --- -- rect[hotwheel_root] --- -- position=296px 268px --- -- size=52px 52px --- -- background=img("inventory_wheel") --- -- end --- -- end --- -- ]]); --- end --- }) --- --- local chat_down = false --- zepha.register_keybind("open_chat", { --- description = "Open Chat", --- default = zepha.keys.t, --- on_press = function() --- chat_down = true --- end, --- on_release = function() --- chat_down = false --- end --- }) --- zepha.delay(function() --- if chat_down then --- local pos = zepha.player.pos --- pos.y = pos.y - 1 --- zepha.set_block(pos, "default:stone") --- end --- return true --- end, 1/20) \ No newline at end of file +zepha.register_keybind("open_inventory", { + description = "Open Inventory", + default = zepha.keys.e, + on_press = function() + --TODO: Replace menu_state with a better system + if zepha.player.menu_state == "" then + zepha.player:open_menu([[ + body + rect[inv] + position: 50% 50% + position_align: 50% 50% + size: 236px 118px + padding: 20px 10px 8px 10px + background: asset("inventory") + +-- inventory[list_main] +-- list: "main" +-- end + + rect[test] + position: -16px -16px + position_align: 1 1 + size: 16px 16px + background: #fff + end + end + end + ]]) + else zepha.player:close_menu() end + end +}) + +local chat_down = false +zepha.register_keybind("open_chat", { + description = "Open Chat", + default = zepha.keys.t, + on_press = function() + print "Opened chat!" + end +}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6bd996f6..4d41d318 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,7 +20,7 @@ set(ZEPHA_SRC def/item/MeshPart.cpp def/item/MeshPart.h def/item/ShaderMod.h - def/item/BlockModelVertex.h + def/item/BlockModelVertex.h game/scene/world/LocalWorld.cpp game/scene/world/LocalWorld.h world/chunk/BlockChunk.cpp @@ -134,7 +134,7 @@ set(ZEPHA_SRC lua/LuaParser.h lua/server/ServerLuaParser.cpp lua/server/ServerLuaParser.h - util/Log.h + util/Log.h lua/Callback.h lua/api/modules/cRegisterBlock.h lua/api/modules/cDelay.h @@ -202,7 +202,7 @@ set(ZEPHA_SRC game/entity/AnimationState.h game/entity/AnimationSegment.h util/Pointer.h - lua/api/type/cLuaEntity.h + lua/api/type/cLuaEntity.h StartGame.h game/graph/shader/SSAOShader.cpp game/graph/shader/SSAOShader.h @@ -224,8 +224,8 @@ set(ZEPHA_SRC lua/server/ServerRegisterItems.cpp lua/server/ServerRegisterItems.h lua/api/modules/cAddEntity.h - lua/api/type/LuaEntity.cpp - lua/api/type/LuaEntity.h + lua/api/type/LuaEntity.cpp + lua/api/type/LuaEntity.h lua/api/modules/cRegisterEntity.h def/model/SerializedModel.h server/asset/AssetType.h @@ -239,9 +239,20 @@ set(ZEPHA_SRC lua/api/modules/sRegisterKeybind.h lua/client/LuaInputManager.cpp lua/client/LuaInputManager.h - lua/api/type/cLuaPlayer.h - lua/api/type/LuaPlayer.cpp - lua/api/type/LuaPlayer.h - game/entity/Collidable.cpp game/entity/Collidable.h game/hud/components/compound/GUIInventoryList.cpp game/hud/components/compound/GUIInventoryList.h game/hud/components/basic/GUIInventoryItem.cpp game/hud/components/basic/GUIInventoryItem.h game/scene/world/InventoryList.cpp game/scene/world/InventoryList.h game/scene/world/ItemStack.cpp game/scene/world/ItemStack.h server/LocalServerInstance.cpp server/LocalServerInstance.h) + lua/api/type/cLuaLocalPlayer.h + lua/api/type/LuaLocalPlayer.cpp + lua/api/type/LuaLocalPlayer.h + game/entity/Collidable.cpp + game/entity/Collidable.h + game/hud/components/compound/GUIInventoryList.cpp + game/hud/components/compound/GUIInventoryList.h + game/hud/components/basic/GUIInventoryItem.cpp + game/hud/components/basic/GUIInventoryItem.h + game/scene/world/InventoryList.cpp + game/scene/world/InventoryList.h + game/scene/world/ItemStack.cpp + game/scene/world/ItemStack.h + server/LocalServerInstance.cpp + server/LocalServerInstance.h game/hud/SerializedGuiElem.h) add_library (Zepha_Core ${ZEPHA_SRC}) \ No newline at end of file diff --git a/src/game/hud/GameGui.cpp b/src/game/hud/GameGui.cpp index 4de6717e..b882b40e 100644 --- a/src/game/hud/GameGui.cpp +++ b/src/game/hud/GameGui.cpp @@ -3,8 +3,14 @@ // #include "GameGui.h" +#include "../../util/Util.h" + +const static float SCALE_MODIFIER = 3; + +GameGui::GameGui(glm::vec2 bufferSize, TextureAtlas& atlas) : + bufferSize(bufferSize), + atlas(atlas) { -GameGui::GameGui(glm::vec2 bufferSize, TextureAtlas& atlas) { auto crosshair = std::make_shared("__crosshair"); crosshair->create({22, 22}, {}, atlas.getTextureRef("crosshair")); crosshair->setPos({bufferSize.x / 2 - 11, bufferSize.y / 2 - 9}); @@ -19,13 +25,162 @@ GameGui::GameGui(glm::vec2 bufferSize, TextureAtlas& atlas) { } void GameGui::bufferResized(glm::vec2 bufferSize) { + this->bufferSize = bufferSize; + get("__crosshair")->setPos({bufferSize.x / 2 - 11, bufferSize.y / 2 - 9}); get("__viginette")->setScale({bufferSize.x, bufferSize.y}); } -void GameGui::setMenu(const std::string& state, std::shared_ptr component) { - menuState = state; - get("__root")->add(component); +void GameGui::setMenu(const std::string& menu) { + menuState = "aaa"; + + std::vector lines; + { + std::string::size_type pos = 0; + std::string::size_type prev = 0; + + while ((pos = menu.find('\n', prev)) != std::string::npos) { + std::string sub = menu.substr(prev, pos - prev); + if (sub.substr(0, 2) != "--") { + std::string::size_type start = sub.find_first_not_of("\t\v\r "); + if (start == std::string::npos) start = 0; + std::string::size_type end = sub.find_last_not_of("\t\v\r "); + + sub = sub.substr(start, end - start + 1); + + if (!sub.empty()) lines.push_back(sub); + } + prev = pos + 1; + } + } + + components.empty(); + std::vector stack {}; + unsigned int missingKey = 0; + SerializedGuiElem* component = nullptr; + for (const std::string& line : lines) { + if (line.find(':') != std::string::npos) { + //Token + if (component == nullptr) throw "Property before component."; + + std::string::size_type delimiter = line.find(':'); + + std::string name = line.substr(0, delimiter); + std::string::size_type start = name.find_first_not_of("\t\v\r "); + if (start == std::string::npos) start = 0; + std::string::size_type end = name.find_last_not_of("\t\v\r "); + name = name.substr(start, end - start + 1); + + std::string value = line.substr(delimiter + 1, std::string::npos); + start = value.find_first_not_of("\t\v\r "); + if (start == std::string::npos) start = 0; + end = value.find_last_not_of("\t\v\r "); + value = value.substr(start, end - start + 1); + + component->tokens.emplace(name, value); + } + else if (line == "end") { + //End Component Def + if (stack.size() > 0) { + component = stack[stack.size() - 1]; + stack.pop_back(); + } + else { + stack.pop_back(); + component = nullptr; + } + } + else { + //Component + std::string key = ""; + std::string::size_type keyStart; + std::string::size_type keyEnd; + if ((keyStart = line.find('[')) != std::string::npos && + (keyEnd = line.find_last_of(']')) != std::string::npos && keyEnd > keyStart) { + key = line.substr(keyStart + 1, keyEnd - keyStart - 1); + } + else { + key = "__" + std::to_string(missingKey++); + } + std::string type = line.substr(0, keyStart); + + SerializedGuiElem g; + g.key = key; + g.type = type; + g.tokens = {}; + g.children = {}; + + if (component == nullptr) { + components.push_back(std::move(g)); + component = &components[components.size() - 1]; + } + else { + component->children.push_back(std::move(g)); + if (component != nullptr) stack.push_back(component); + component = &component->children[component->children.size() - 1]; + } + } + } + + get("__root")->empty(); + recursivelyCreate(components, get("__root")); +} + +void GameGui::recursivelyCreate(std::vector components, std::shared_ptr parent) { + for (auto& data : components) { + std::shared_ptr component = createComponent(data); + parent->add(component); + + recursivelyCreate(data.children, component); + } +} + +std::shared_ptr GameGui::createComponent(SerializedGuiElem& data) { + if (data.type == "body") { + auto rect = std::make_shared(data.key); + rect->create(bufferSize, {}, {0, 0, 0, 0.2}); + return rect; + } + else if (data.type == "rect") { + auto rect = std::make_shared(data.key); + + glm::vec2 pos {}; + if (data.tokens.count("position")) pos = deserialize2DVector(data.tokens["position"]) * SCALE_MODIFIER; + glm::vec2 size {}; + if (data.tokens.count("size")) size = deserialize2DVector(data.tokens["size"]) * SCALE_MODIFIER; + glm::vec4 padding {}; + if (data.tokens.count("padding")) padding = deserialize4DVector(data.tokens["padding"]) * SCALE_MODIFIER; + std::string background = ""; + if (data.tokens.count("background")) background = data.tokens["background"]; + + size.x -= padding.y + padding.w; + size.y -= padding.x + padding.z; + + if (background[0] == '#') rect->create(size, padding, Util::hexToColorVec(background)); + else if (background.substr(0, 6) == "asset(") rect->create(size, padding, atlas.getTextureRef(background.substr(7, background.length() - 7 - 2))); + else rect->create(size, padding, glm::vec4 {}); + + rect->setPos(pos); + return rect; + } +// else if (type == "text") { +// std::string posStr = element.get("position"); +// std::string sizeStr = element.get("size"); +// std::string padStr = element.get("padding"); +// +// glm::vec2 pos = calc2dVec(posStr) * scale_modifier; +// glm::vec2 scale = glm::vec2(scale_modifier); +// glm::vec4 padding = calc4dVec(padStr) * scale_modifier; +// glm::vec4 bgcolor = Util::hexToColorVec(element.get_or("background_color", "#0000")); +// glm::vec4 color = Util::hexToColorVec(element.get_or("color", "#fff")); +// std::string contents = element.get("contents"); +// +// std::shared_ptr text = std::make_shared(key); +// text->create(scale, padding, bgcolor, color, {defs.textures(), defs.textures().getTextureRef("font")}); +// text->setText(contents); +// component = text; +// text->setPos(pos); +// } } void GameGui::closeMenu() { @@ -39,4 +194,74 @@ const std::string &GameGui::getMenuState() { void GameGui::setVisible(bool visible) { GUIComponent::setVisible(visible); +} + +float GameGui::deserializeNumber(const std::string& input, bool height = false) { + if (input.find("px") != std::string::npos) { + return atof(input.substr(0, input.find("px")).c_str()); + } + if (input.find('%') != std::string::npos) { + return atof(input.substr(0, input.find('%')).c_str()) / 100 * (height ? bufferSize.y : bufferSize.x) / SCALE_MODIFIER; + } + return 0; +} + +glm::vec2 GameGui::deserialize2DVector(const std::string& input) { + glm::vec2 r; + size_t count = std::count(input.begin(), input.end(), ' '); + + if (count == 0) { //1 value + r.x = deserializeNumber(input); + r.y = deserializeNumber(input, true); + return r; + } + + if (count == 1) { //2 values + size_t delim = input.find(' '); + std::string x = input.substr(0, delim); + std::string y = input.substr(delim + 1); + + r.x = deserializeNumber(x); + r.y = deserializeNumber(y, true); + return r; + } + + std::cout << Log::err << "Invalid dimension string " << input << Log::endl; + return r; +} + +glm::vec4 GameGui::deserialize4DVector(const std::string& input) { + glm::vec4 r; + size_t count = std::count(input.begin(), input.end(), ' '); + + if (count == 0) { //1 value + r = glm::vec4(deserializeNumber(input)); + return r; + } + + if (count == 1) { //2 values + size_t delim = input.find(' '); + std::string x = input.substr(0, delim); + std::string y = input.substr(delim + 1); + + r.x = deserializeNumber(x); + r.y = deserializeNumber(y); + r.z = r.x; + r.w = r.y; + return r; + } + + if (count == 3) { //4 values + size_t begin = 0; + for (int i = 0; i < 3; i++) { + size_t end = input.find(' ', begin); + r[i] = deserializeNumber(input.substr(begin, end - begin)); + begin = end + 1; + } + r[3] = deserializeNumber(input.substr(begin)); + return r; + } + + std::cout << Log::err << "Invalid dimension string " << input << Log::endl; + return r; } \ No newline at end of file diff --git a/src/game/hud/GameGui.h b/src/game/hud/GameGui.h index fc3b7639..5c8a7398 100644 --- a/src/game/hud/GameGui.h +++ b/src/game/hud/GameGui.h @@ -8,6 +8,7 @@ #include "components/basic/GUIContainer.h" #include "../graph/drawable/DrawableGroup.h" #include "../entity/Entity.h" +#include "SerializedGuiElem.h" class GameGui : public GUIContainer { public: @@ -16,10 +17,21 @@ public: void setVisible(bool visible) override; - void setMenu(const std::string& state, std::shared_ptr component); + void setMenu(const std::string& menu); void closeMenu(); const std::string& getMenuState(); +private: + void recursivelyCreate(std::vector components, std::shared_ptr parent); + std::shared_ptr createComponent(SerializedGuiElem& component); + std::vector components {}; + + float deserializeNumber(const std::string& input, bool height); + glm::vec2 deserialize2DVector(const std::string& input); + glm::vec4 deserialize4DVector(const std::string& input); + + glm::vec2 bufferSize {}; + TextureAtlas& atlas; std::string menuState = ""; }; diff --git a/src/game/hud/SerializedGuiElem.h b/src/game/hud/SerializedGuiElem.h new file mode 100644 index 00000000..372bd6de --- /dev/null +++ b/src/game/hud/SerializedGuiElem.h @@ -0,0 +1,16 @@ +// +// Created by aurailus on 2019-11-03. +// + +#pragma once + +#include +#include +#include + +struct SerializedGuiElem { + std::string type; + std::string key; + std::map tokens; + std::vector children; +}; \ No newline at end of file diff --git a/src/game/scene/world/Player.cpp b/src/game/scene/world/Player.cpp index 955326c2..6f646b86 100644 --- a/src/game/scene/world/Player.cpp +++ b/src/game/scene/world/Player.cpp @@ -231,8 +231,8 @@ void Player::setActiveBlock(const std::string& block) { activeBlock = defs.defs().blockFromStr(block).index; } -void Player::setMenu(const std::string& state, sptr root) { - gameGui.setMenu(state, root); +void Player::setMenu(const std::string& menu) { + gameGui.setMenu(menu); renderer.getWindow().lockMouse(false); } @@ -245,8 +245,8 @@ void Player::setGuiVisible(bool hudVisible) { gameGui.setVisible(hudVisible); } -GameGui& Player::getGui() { - return gameGui; +std::string Player::getMenuState() { + return gameGui.getMenuState(); } /* diff --git a/src/game/scene/world/Player.h b/src/game/scene/world/Player.h index c8351282..b780de15 100644 --- a/src/game/scene/world/Player.h +++ b/src/game/scene/world/Player.h @@ -51,8 +51,8 @@ public: void setActiveBlock(const std::string& block); - GameGui& getGui(); - void setMenu(const std::string& state, sptr root); + void setMenu(const std::string& menu); + std::string getMenuState(); void closeMenu(); void setGuiVisible(bool hudVisible); diff --git a/src/lua/api/modules/cShowMenu.h b/src/lua/api/modules/cShowMenu.h index 427e9e4b..58563547 100644 --- a/src/lua/api/modules/cShowMenu.h +++ b/src/lua/api/modules/cShowMenu.h @@ -15,135 +15,135 @@ const static float scale_modifier = 3; -float calcNum(std::string input) { - if (input.find("px") != string::npos) { - return atof(input.substr(0, input.find("px")).c_str()); - } -} - -glm::vec2 calc2dVec(std::string input) { - glm::vec2 r; - size_t count = std::count(input.begin(), input.end(), ','); - - if (count == 0) { //1 value - r.x = calcNum(input); - r.y = r.x; - return r; - } - - if (count == 1) { //2 values - size_t delim = input.find(','); - std::string x = input.substr(0, delim); - std::string y = input.substr(delim + 1); - - r.x = calcNum(x); - r.y = calcNum(y); - return r; - } - - std::cout << Log::err << "Invalid dimension string " << input << Log::endl; - return r; -} - -glm::vec4 calc4dVec(std::string input) { - glm::vec4 r; - size_t count = std::count(input.begin(), input.end(), ','); - - if (count == 0) { //1 value - r = glm::vec4(calcNum(input)); - return r; - } - - if (count == 1) { //2 values - size_t delim = input.find(','); - std::string x = input.substr(0, delim); - std::string y = input.substr(delim + 1); - - r.x = calcNum(x); - r.y = calcNum(y); - r.z = r.x; - r.w = r.y; - return r; - } - - if (count == 3) { //4 values - size_t begin = 0; - for (int i = 0; i < 3; i++) { - size_t end = input.find(',', begin); - r[i] = calcNum(input.substr(begin, end - begin)); - begin = end + 1; - } - r[3] = calcNum(input.substr(begin)); - return r; - } - - std::cout << Log::err << "Invalid dimension string " << input << Log::endl; - return r; -} - -void processElement(LocalDefs& defs, sol::table& element, std::shared_ptr parent) { - auto key = element.get("key"); - auto type = element.get("type"); - - std::shared_ptr component; - - if (type == "rect") { - std::string posStr = element.get("position"); - std::string sizeStr = element.get("size"); - std::string padStr = element.get("padding"); - - glm::vec2 pos = calc2dVec(posStr) * scale_modifier; - glm::vec2 size = calc2dVec(sizeStr) * scale_modifier; - glm::vec4 padding = calc4dVec(padStr) * scale_modifier; - std::string background = element.get_or("background", "#0000"); - - std::shared_ptr rect = std::make_shared(key); - - if (background[0] == '#') rect->create(size, padding, Util::hexToColorVec(background)); - else rect->create(size - glm::vec2{padding.y + padding.w, padding.x + padding.z}, padding, defs.textures().getTextureRef(background)); - - component = rect; - rect->setPos(pos); - } - else if (type == "text") { - std::string posStr = element.get("position"); - std::string sizeStr = element.get("size"); - std::string padStr = element.get("padding"); - - glm::vec2 pos = calc2dVec(posStr) * scale_modifier; - glm::vec2 scale = glm::vec2(scale_modifier); - glm::vec4 padding = calc4dVec(padStr) * scale_modifier; - glm::vec4 bgcolor = Util::hexToColorVec(element.get_or("background_color", "#0000")); - glm::vec4 color = Util::hexToColorVec(element.get_or("color", "#fff")); - std::string contents = element.get("contents"); - - std::shared_ptr text = std::make_shared(key); - text->create(scale, padding, bgcolor, color, {defs.textures(), defs.textures().getTextureRef("font")}); - text->setText(contents); - component = text; - text->setPos(pos); - } - - parent->add(component); - - auto children = element.get>("children"); - if (children) { - for (auto &child : *children) { - sol::table elem = child.second; - processElement(defs, elem, component); - } - } -} - -namespace ClientApi { - void show_menu(sol::table &core, LocalDefs& defs, Player& player) { - - core.set_function("show_menu", [&](std::string state, sol::table gui_root) { - auto root = std::make_shared("__lua_root"); - player.setMenu(state, root); - - processElement(defs, gui_root, static_pointer_cast(root)); - }); - } -} - +//float calcNum(std::string input) { +// if (input.find("px") != string::npos) { +// return atof(input.substr(0, input.find("px")).c_str()); +// } +//} +// +//glm::vec2 calc2dVec(std::string input) { +// glm::vec2 r; +// size_t count = std::count(input.begin(), input.end(), ','); +// +// if (count == 0) { //1 value +// r.x = calcNum(input); +// r.y = r.x; +// return r; +// } +// +// if (count == 1) { //2 values +// size_t delim = input.find(','); +// std::string x = input.substr(0, delim); +// std::string y = input.substr(delim + 1); +// +// r.x = calcNum(x); +// r.y = calcNum(y); +// return r; +// } +// +// std::cout << Log::err << "Invalid dimension string " << input << Log::endl; +// return r; +//} +// +//glm::vec4 calc4dVec(std::string input) { +// glm::vec4 r; +// size_t count = std::count(input.begin(), input.end(), ','); +// +// if (count == 0) { //1 value +// r = glm::vec4(calcNum(input)); +// return r; +// } +// +// if (count == 1) { //2 values +// size_t delim = input.find(','); +// std::string x = input.substr(0, delim); +// std::string y = input.substr(delim + 1); +// +// r.x = calcNum(x); +// r.y = calcNum(y); +// r.z = r.x; +// r.w = r.y; +// return r; +// } +// +// if (count == 3) { //4 values +// size_t begin = 0; +// for (int i = 0; i < 3; i++) { +// size_t end = input.find(',', begin); +// r[i] = calcNum(input.substr(begin, end - begin)); +// begin = end + 1; +// } +// r[3] = calcNum(input.substr(begin)); +// return r; +// } +// +// std::cout << Log::err << "Invalid dimension string " << input << Log::endl; +// return r; +//} +// +//void processElement(LocalDefs& defs, sol::table& element, std::shared_ptr parent) { +// auto key = element.get("key"); +// auto type = element.get("type"); +// +// std::shared_ptr component; +// +// if (type == "rect") { +// std::string posStr = element.get("position"); +// std::string sizeStr = element.get("size"); +// std::string padStr = element.get("padding"); +// +// glm::vec2 pos = calc2dVec(posStr) * scale_modifier; +// glm::vec2 size = calc2dVec(sizeStr) * scale_modifier; +// glm::vec4 padding = calc4dVec(padStr) * scale_modifier; +// std::string background = element.get_or("background", "#0000"); +// +// std::shared_ptr rect = std::make_shared(key); +// +// if (background[0] == '#') rect->create(size, padding, Util::hexToColorVec(background)); +// else rect->create(size - glm::vec2{padding.y + padding.w, padding.x + padding.z}, padding, defs.textures().getTextureRef(background)); +// +// component = rect; +// rect->setPos(pos); +// } +// else if (type == "text") { +// std::string posStr = element.get("position"); +// std::string sizeStr = element.get("size"); +// std::string padStr = element.get("padding"); +// +// glm::vec2 pos = calc2dVec(posStr) * scale_modifier; +// glm::vec2 scale = glm::vec2(scale_modifier); +// glm::vec4 padding = calc4dVec(padStr) * scale_modifier; +// glm::vec4 bgcolor = Util::hexToColorVec(element.get_or("background_color", "#0000")); +// glm::vec4 color = Util::hexToColorVec(element.get_or("color", "#fff")); +// std::string contents = element.get("contents"); +// +// std::shared_ptr text = std::make_shared(key); +// text->create(scale, padding, bgcolor, color, {defs.textures(), defs.textures().getTextureRef("font")}); +// text->setText(contents); +// component = text; +// text->setPos(pos); +// } +// +// parent->add(component); +// +// auto children = element.get>("children"); +// if (children) { +// for (auto &child : *children) { +// sol::table elem = child.second; +// processElement(defs, elem, component); +// } +// } +//} +// +//namespace ClientApi { +// void show_menu(sol::table &core, LocalDefs& defs, Player& player) { +// +// core.set_function("show_menu", [&](std::string state, sol::table gui_root) { +// auto root = std::make_shared("__lua_root"); +//// player.setMenu(state, root); +// +// processElement(defs, gui_root, static_pointer_cast(root)); +// }); +// } +//} +// diff --git a/src/lua/api/type/LuaPlayer.cpp b/src/lua/api/type/LuaLocalPlayer.cpp similarity index 50% rename from src/lua/api/type/LuaPlayer.cpp rename to src/lua/api/type/LuaLocalPlayer.cpp index 5b15281c..b8cd5d87 100644 --- a/src/lua/api/type/LuaPlayer.cpp +++ b/src/lua/api/type/LuaLocalPlayer.cpp @@ -2,51 +2,55 @@ // Created by aurailus on 2019-10-19. // -#include "LuaPlayer.h" +#include "LuaLocalPlayer.h" -void LuaPlayer::set_pos(const sol::table &pos) { +void LuaLocalPlayer::set_pos(const sol::table &pos) { player.setPos({pos["x"], pos["y"], pos["z"]}); } -sol::table LuaPlayer::get_pos(sol::this_state s) { +sol::table LuaLocalPlayer::get_pos(sol::this_state s) { glm::vec3 pos = player.getPos(); return sol::state_view(s).create_table_with("x", pos.x, "y", pos.y, "z", pos.z); } -sol::table LuaPlayer::get_block_pos(sol::this_state s) { +sol::table LuaLocalPlayer::get_block_pos(sol::this_state s) { glm::vec3 pos = TransPos::roundPos(player.getPos()); return sol::state_view(s).create_table_with("x", pos.x, "y", pos.y, "z", pos.z); } -void LuaPlayer::set_vel(const sol::table &vel) { +void LuaLocalPlayer::set_vel(const sol::table &vel) { player.setVel({vel["x"], vel["y"], vel["z"]}); } -sol::table LuaPlayer::get_vel(sol::this_state s) { +sol::table LuaLocalPlayer::get_vel(sol::this_state s) { glm::vec3 vel = player.getVel(); return sol::state_view(s).create_table_with("x", vel.x, "y", vel.y, "z", vel.z); } -void LuaPlayer::set_look_yaw(float rot) { +void LuaLocalPlayer::set_look_yaw(float rot) { player.setYaw(rot); } -float LuaPlayer::get_look_yaw() { +float LuaLocalPlayer::get_look_yaw() { return player.getYaw(); } -void LuaPlayer::set_look_pitch(float rot) { +void LuaLocalPlayer::set_look_pitch(float rot) { player.setPitch(rot); } -float LuaPlayer::get_look_pitch() { +float LuaLocalPlayer::get_look_pitch() { return player.getPitch(); } -std::string LuaPlayer::get_menu_state() { - return player.getGui().getMenuState(); +std::string LuaLocalPlayer::get_menu_state() { + return player.getMenuState(); } -void LuaPlayer::set_selected_block(std::string block) { - player.setActiveBlock(block); +void LuaLocalPlayer::open_menu(std::string menu) { + player.setMenu(menu); } + +void LuaLocalPlayer::set_selected_block(std::string block) { + player.setActiveBlock(block); +} \ No newline at end of file diff --git a/src/lua/api/type/LuaPlayer.h b/src/lua/api/type/LuaLocalPlayer.h similarity index 84% rename from src/lua/api/type/LuaPlayer.h rename to src/lua/api/type/LuaLocalPlayer.h index d6e64bb9..ab7ae750 100644 --- a/src/lua/api/type/LuaPlayer.h +++ b/src/lua/api/type/LuaLocalPlayer.h @@ -8,9 +8,9 @@ #include "../../../def/LocalDefs.h" #include "../../../game/scene/world/Player.h" -class LuaPlayer { +class LuaLocalPlayer { public: - LuaPlayer(Player& player) : player(player) {} + LuaLocalPlayer(Player& player) : player(player) {} Player& player; @@ -28,5 +28,7 @@ public: float get_look_pitch(); std::string get_menu_state(); + void open_menu(std::string menu); + void set_selected_block(std::string block); }; diff --git a/src/lua/api/type/cLuaLocalPlayer.h b/src/lua/api/type/cLuaLocalPlayer.h new file mode 100644 index 00000000..7b917285 --- /dev/null +++ b/src/lua/api/type/cLuaLocalPlayer.h @@ -0,0 +1,37 @@ +// +// Created by aurailus on 2019-10-19. +// + +#pragma once + +#include "LuaEntity.h" +#include "../../../game/scene/world/LocalWorld.h" +#include "LuaLocalPlayer.h" +#include + +namespace ClientApi { + void local_player(sol::state& lua, LocalWorld& world) { + lua.new_usertype("LuaLocalPlayer", + "set_pos", &LuaLocalPlayer::set_pos, + "get_pos", &LuaLocalPlayer::get_pos, + "get_block_pos", &LuaLocalPlayer::get_block_pos, + "set_vel", &LuaLocalPlayer::set_vel, + "get_vel", &LuaLocalPlayer::get_vel, + "set_look_yaw", &LuaLocalPlayer::set_look_yaw, + "get_look_yaw", &LuaLocalPlayer::get_look_yaw, + "set_look_pitch", &LuaLocalPlayer::set_look_pitch, + "get_look_pitch", &LuaLocalPlayer::get_look_pitch, + + "pos", sol::property(&LuaLocalPlayer::get_pos, &LuaLocalPlayer::set_pos), + "block_pos", sol::property(&LuaLocalPlayer::get_block_pos, &LuaLocalPlayer::set_pos), + "vel", sol::property(&LuaLocalPlayer::get_vel, &LuaLocalPlayer::set_vel), + "look_yaw", sol::property(&LuaLocalPlayer::get_look_yaw, &LuaLocalPlayer::set_look_yaw), + "look_yaw", sol::property(&LuaLocalPlayer::get_look_pitch, &LuaLocalPlayer::set_look_pitch), + + "menu_state", sol::property(&LuaLocalPlayer::get_menu_state), + "open_menu", &LuaLocalPlayer::open_menu, + + "set_selected_block", &LuaLocalPlayer::set_selected_block + ); + } +} \ No newline at end of file diff --git a/src/lua/api/type/cLuaPlayer.h b/src/lua/api/type/cLuaPlayer.h deleted file mode 100644 index 8571ae64..00000000 --- a/src/lua/api/type/cLuaPlayer.h +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by aurailus on 2019-10-19. -// - -#pragma once - -#include "LuaEntity.h" -#include "../../../game/scene/world/LocalWorld.h" -#include "LuaPlayer.h" -#include - -namespace ClientApi { - void player(sol::state& lua, LocalWorld& world) { - lua.new_usertype("LuaPlayer", - "set_pos", &LuaPlayer::set_pos, - "get_pos", &LuaPlayer::get_pos, - "get_block_pos", &LuaPlayer::get_block_pos, - "set_vel", &LuaPlayer::set_vel, - "get_vel", &LuaPlayer::get_vel, - "set_look_yaw", &LuaPlayer::set_look_yaw, - "get_look_yaw", &LuaPlayer::get_look_yaw, - "set_look_pitch", &LuaPlayer::set_look_pitch, - "get_look_pitch", &LuaPlayer::get_look_pitch, - - "pos", sol::property(&LuaPlayer::get_pos, &LuaPlayer::set_pos), - "block_pos", sol::property(&LuaPlayer::get_block_pos, &LuaPlayer::set_pos), - "vel", sol::property(&LuaPlayer::get_vel, &LuaPlayer::set_vel), - "look_yaw", sol::property(&LuaPlayer::get_look_yaw, &LuaPlayer::set_look_yaw), - "look_yaw", sol::property(&LuaPlayer::get_look_pitch, &LuaPlayer::set_look_pitch), - - "menu_state", sol::property(&LuaPlayer::get_menu_state), - "set_selected_block", &LuaPlayer::set_selected_block - ); - } -} \ No newline at end of file diff --git a/src/lua/client/LocalLuaParser.cpp b/src/lua/client/LocalLuaParser.cpp index 62fc6d55..f93b0516 100644 --- a/src/lua/client/LocalLuaParser.cpp +++ b/src/lua/client/LocalLuaParser.cpp @@ -8,11 +8,11 @@ #include "LocalRegisterKeybinds.h" #include "../../def/LocalDefs.h" -#include "../api/type/LuaPlayer.h" +#include "../api/type/LuaLocalPlayer.h" #include "../../game/scene/world/Player.h" #include "../api/type/cLuaEntity.h" -#include "../api/type/cLuaPlayer.h" +#include "../api/type/cLuaLocalPlayer.h" #include "../api/modules/cDump.h" #include "../api/modules/cPrintE.h" @@ -63,9 +63,9 @@ void LocalLuaParser::loadModules(LocalDefs &defs, LocalWorld &world, Player& pla //Load Types ClientApi::entity(lua, world); - ClientApi::player(lua, world); + ClientApi::local_player(lua, world); - core["player"] = LuaPlayer(player); + core["player"] = LuaLocalPlayer(player); //Load Modules ClientApi::dump(lua); @@ -86,7 +86,7 @@ void LocalLuaParser::loadModules(LocalDefs &defs, LocalWorld &world, Player& pla ClientApi::add_entity(lua, core, defs, world); ClientApi::remove_entity(lua, core, defs, world); - ClientApi::show_menu(core, defs, player); +// ClientApi::show_menu(core, defs, player); ClientApi::close_menu(core, defs, player); ClientApi::register_keybind(lua, core); diff --git a/src/lua/server/ServerLuaParser.cpp b/src/lua/server/ServerLuaParser.cpp index 525eea5d..3cf2890b 100644 --- a/src/lua/server/ServerLuaParser.cpp +++ b/src/lua/server/ServerLuaParser.cpp @@ -52,6 +52,8 @@ void ServerLuaParser::loadModules(ServerDefs &defs, ServerWorld &world) { core = lua.create_table(); lua["zepha"] = core; + core["player"] = sol::nil; + //Load Modules ServerApi::dump(lua); ServerApi::printe(lua);