New inventory format and inventory parsing algorithm in GameGui.
parent
3d5b705031
commit
ae286f84eb
|
@ -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
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
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
|
||||
})
|
||||
|
|
|
@ -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})
|
|
@ -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<GUIRect>("__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<GUIRect>("__crosshair")->setPos({bufferSize.x / 2 - 11, bufferSize.y / 2 - 9});
|
||||
get<GUIRect>("__viginette")->setScale({bufferSize.x, bufferSize.y});
|
||||
}
|
||||
|
||||
void GameGui::setMenu(const std::string& state, std::shared_ptr<GUIComponent> component) {
|
||||
menuState = state;
|
||||
get<GUIContainer>("__root")->add(component);
|
||||
void GameGui::setMenu(const std::string& menu) {
|
||||
menuState = "aaa";
|
||||
|
||||
std::vector<std::string> 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<SerializedGuiElem*> 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<GUIContainer>("__root")->empty();
|
||||
recursivelyCreate(components, get<GUIContainer>("__root"));
|
||||
}
|
||||
|
||||
void GameGui::recursivelyCreate(std::vector<SerializedGuiElem> components, std::shared_ptr<GUIComponent> parent) {
|
||||
for (auto& data : components) {
|
||||
std::shared_ptr<GUIComponent> component = createComponent(data);
|
||||
parent->add(component);
|
||||
|
||||
recursivelyCreate(data.children, component);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<GUIComponent> GameGui::createComponent(SerializedGuiElem& data) {
|
||||
if (data.type == "body") {
|
||||
auto rect = std::make_shared<GUIRect>(data.key);
|
||||
rect->create(bufferSize, {}, {0, 0, 0, 0.2});
|
||||
return rect;
|
||||
}
|
||||
else if (data.type == "rect") {
|
||||
auto rect = std::make_shared<GUIRect>(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<std::string>("position");
|
||||
// std::string sizeStr = element.get<std::string>("size");
|
||||
// std::string padStr = element.get<std::string>("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<std::string>("background_color", "#0000"));
|
||||
// glm::vec4 color = Util::hexToColorVec(element.get_or<std::string>("color", "#fff"));
|
||||
// std::string contents = element.get<std::string>("contents");
|
||||
//
|
||||
// std::shared_ptr<GUIText> text = std::make_shared<GUIText>(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;
|
||||
}
|
|
@ -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<GUIComponent> component);
|
||||
void setMenu(const std::string& menu);
|
||||
void closeMenu();
|
||||
const std::string& getMenuState();
|
||||
|
||||
private:
|
||||
void recursivelyCreate(std::vector<SerializedGuiElem> components, std::shared_ptr<GUIComponent> parent);
|
||||
std::shared_ptr<GUIComponent> createComponent(SerializedGuiElem& component);
|
||||
std::vector<SerializedGuiElem> 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 = "";
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
//
|
||||
// Created by aurailus on 2019-11-03.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
struct SerializedGuiElem {
|
||||
std::string type;
|
||||
std::string key;
|
||||
std::map<std::string, std::string> tokens;
|
||||
std::vector<SerializedGuiElem> children;
|
||||
};
|
|
@ -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<GUIComponent> 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();
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -51,8 +51,8 @@ public:
|
|||
|
||||
void setActiveBlock(const std::string& block);
|
||||
|
||||
GameGui& getGui();
|
||||
void setMenu(const std::string& state, sptr<GUIComponent> root);
|
||||
void setMenu(const std::string& menu);
|
||||
std::string getMenuState();
|
||||
void closeMenu();
|
||||
void setGuiVisible(bool hudVisible);
|
||||
|
||||
|
|
|
@ -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<GUIComponent> parent) {
|
||||
auto key = element.get<std::string>("key");
|
||||
auto type = element.get<std::string>("type");
|
||||
|
||||
std::shared_ptr<GUIComponent> component;
|
||||
|
||||
if (type == "rect") {
|
||||
std::string posStr = element.get<std::string>("position");
|
||||
std::string sizeStr = element.get<std::string>("size");
|
||||
std::string padStr = element.get<std::string>("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<std::string>("background", "#0000");
|
||||
|
||||
std::shared_ptr<GUIRect> rect = std::make_shared<GUIRect>(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<std::string>("position");
|
||||
std::string sizeStr = element.get<std::string>("size");
|
||||
std::string padStr = element.get<std::string>("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<std::string>("background_color", "#0000"));
|
||||
glm::vec4 color = Util::hexToColorVec(element.get_or<std::string>("color", "#fff"));
|
||||
std::string contents = element.get<std::string>("contents");
|
||||
|
||||
std::shared_ptr<GUIText> text = std::make_shared<GUIText>(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<sol::optional<sol::table>>("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<GUIContainer>("__lua_root");
|
||||
player.setMenu(state, root);
|
||||
|
||||
processElement(defs, gui_root, static_pointer_cast<GUIComponent>(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<GUIComponent> parent) {
|
||||
// auto key = element.get<std::string>("key");
|
||||
// auto type = element.get<std::string>("type");
|
||||
//
|
||||
// std::shared_ptr<GUIComponent> component;
|
||||
//
|
||||
// if (type == "rect") {
|
||||
// std::string posStr = element.get<std::string>("position");
|
||||
// std::string sizeStr = element.get<std::string>("size");
|
||||
// std::string padStr = element.get<std::string>("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<std::string>("background", "#0000");
|
||||
//
|
||||
// std::shared_ptr<GUIRect> rect = std::make_shared<GUIRect>(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<std::string>("position");
|
||||
// std::string sizeStr = element.get<std::string>("size");
|
||||
// std::string padStr = element.get<std::string>("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<std::string>("background_color", "#0000"));
|
||||
// glm::vec4 color = Util::hexToColorVec(element.get_or<std::string>("color", "#fff"));
|
||||
// std::string contents = element.get<std::string>("contents");
|
||||
//
|
||||
// std::shared_ptr<GUIText> text = std::make_shared<GUIText>(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<sol::optional<sol::table>>("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<GUIContainer>("__lua_root");
|
||||
//// player.setMenu(state, root);
|
||||
//
|
||||
// processElement(defs, gui_root, static_pointer_cast<GUIComponent>(root));
|
||||
// });
|
||||
// }
|
||||
//}
|
||||
//
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
};
|
|
@ -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 <sol2/sol.hpp>
|
||||
|
||||
namespace ClientApi {
|
||||
void local_player(sol::state& lua, LocalWorld& world) {
|
||||
lua.new_usertype<LuaLocalPlayer>("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
|
||||
);
|
||||
}
|
||||
}
|
|
@ -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 <sol2/sol.hpp>
|
||||
|
||||
namespace ClientApi {
|
||||
void player(sol::state& lua, LocalWorld& world) {
|
||||
lua.new_usertype<LuaPlayer>("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
|
||||
);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue