lua: gui library (WIP)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "LuaState.h"
|
||||
|
||||
#include "api_lua.h"
|
||||
#include "api/api_lua.h"
|
||||
#include "api/libgui.h"
|
||||
#include "../../util/stringutil.h"
|
||||
|
||||
lua::luaerror::luaerror(const std::string& message) : std::runtime_error(message) {
|
||||
@@ -98,6 +99,7 @@ void lua::LuaState::createFuncs() {
|
||||
openlib("item", itemlib, 0);
|
||||
openlib("time", timelib, 0);
|
||||
openlib("file", filelib, 0);
|
||||
openlib("gui", guilib, 0);
|
||||
|
||||
addfunc("print", l_print);
|
||||
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
#ifndef LOGIC_SCRIPTING_API_LUA_H_
|
||||
#define LOGIC_SCRIPTING_API_LUA_H_
|
||||
|
||||
#include "scripting.h"
|
||||
#include "lua_util.h"
|
||||
#include "../scripting.h"
|
||||
#include "../lua_util.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include "../../files/files.h"
|
||||
#include "../../physics/Hitbox.h"
|
||||
#include "../../objects/Player.h"
|
||||
#include "../../world/Level.h"
|
||||
#include "../../world/World.h"
|
||||
#include "../../content/Content.h"
|
||||
#include "../../voxels/Block.h"
|
||||
#include "../../voxels/Chunks.h"
|
||||
#include "../../voxels/voxel.h"
|
||||
#include "../../items/ItemDef.h"
|
||||
#include "../../items/ItemStack.h"
|
||||
#include "../../items/Inventory.h"
|
||||
#include "../../items/Inventories.h"
|
||||
#include "../../lighting/Lighting.h"
|
||||
#include "../../logic/BlocksController.h"
|
||||
#include "../../window/Window.h"
|
||||
#include "../../engine.h"
|
||||
#include "../../../files/files.h"
|
||||
#include "../../../physics/Hitbox.h"
|
||||
#include "../../../objects/Player.h"
|
||||
#include "../../../world/Level.h"
|
||||
#include "../../../world/World.h"
|
||||
#include "../../../content/Content.h"
|
||||
#include "../../../voxels/Block.h"
|
||||
#include "../../../voxels/Chunks.h"
|
||||
#include "../../../voxels/voxel.h"
|
||||
#include "../../../items/ItemDef.h"
|
||||
#include "../../../items/ItemStack.h"
|
||||
#include "../../../items/Inventory.h"
|
||||
#include "../../../items/Inventories.h"
|
||||
#include "../../../lighting/Lighting.h"
|
||||
#include "../../../logic/BlocksController.h"
|
||||
#include "../../../window/Window.h"
|
||||
#include "../../../engine.h"
|
||||
|
||||
/* == file library == */
|
||||
static int l_file_resolve(lua_State* L) {
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "libgui.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "../scripting.h"
|
||||
#include "../lua_util.h"
|
||||
|
||||
#include "../../../engine.h"
|
||||
#include "../../../assets/Assets.h"
|
||||
#include "../../../frontend/gui/UINode.h"
|
||||
#include "../../../frontend/gui/controls.h"
|
||||
#include "../../../frontend/UiDocument.h"
|
||||
#include "../../../util/stringutil.h"
|
||||
|
||||
static gui::UINode* getDocumentNode(lua_State* L, const std::string& name, const std::string& nodeName) {
|
||||
auto doc = scripting::engine->getAssets()->getLayout(name);
|
||||
if (doc == nullptr) {
|
||||
luaL_error(L, "document '%s' not found", name.c_str());
|
||||
}
|
||||
auto node = doc->get(nodeName);
|
||||
if (node == nullptr) {
|
||||
luaL_error(L, "document '%s' has no element with id '%s'", name.c_str(), nodeName.c_str());
|
||||
}
|
||||
return node.get();
|
||||
}
|
||||
|
||||
static bool getattr(lua_State* L, gui::Button* button, const std::string& attr) {
|
||||
if (button == nullptr)
|
||||
return false;
|
||||
if (attr == "text") {
|
||||
lua_pushstring(L, util::wstr2str_utf8(button->getText()).c_str());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool setattr(lua_State* L, gui::Button* button, const std::string& attr) {
|
||||
if (button == nullptr)
|
||||
return false;
|
||||
if (attr == "text") {
|
||||
button->setText(util::str2wstr_utf8(lua_tostring(L, 4)));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int l_gui_getattr(lua_State* L) {
|
||||
auto docname = lua_tostring(L, 1);
|
||||
auto element = lua_tostring(L, 2);
|
||||
const std::string attr = lua_tostring(L, 3);
|
||||
auto node = getDocumentNode(L, docname, element);
|
||||
|
||||
if (attr == "color") {
|
||||
return lua::pushcolor_arr(L, node->getColor());
|
||||
} else if (attr == "coord") {
|
||||
return lua::pushvec2_arr(L, node->getCoord());
|
||||
} else if (attr == "size") {
|
||||
return lua::pushvec2_arr(L, node->getSize());
|
||||
}
|
||||
|
||||
if (getattr(L, dynamic_cast<gui::Button*>(node), attr))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_gui_setattr(lua_State* L) {
|
||||
auto docname = lua_tostring(L, 1);
|
||||
auto element = lua_tostring(L, 2);
|
||||
const std::string attr = lua_tostring(L, 3);
|
||||
|
||||
auto node = getDocumentNode(L, docname, element);
|
||||
|
||||
if (setattr(L, dynamic_cast<gui::Button*>(node), attr))
|
||||
return 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef LOGIC_SCRIPTING_API_LIBGUI_H_
|
||||
#define LOGIC_SCRIPTING_API_LIBGUI_H_
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
extern int l_gui_getattr(lua_State* L);
|
||||
extern int l_gui_setattr(lua_State* L);
|
||||
|
||||
static const luaL_Reg guilib [] = {
|
||||
{"getattr", l_gui_getattr},
|
||||
{"setattr", l_gui_setattr},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#endif // LOGIC_SCRIPTING_API_LIBGUI_H_
|
||||
@@ -2,6 +2,8 @@
|
||||
#define LOGIC_SCRIPTING_LUA_UTIL_H_
|
||||
|
||||
#include <lua.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include "LuaState.h"
|
||||
|
||||
namespace lua {
|
||||
inline int pushivec3(lua_State* L, luaint x, luaint y, luaint z) {
|
||||
@@ -10,6 +12,79 @@ namespace lua {
|
||||
lua_pushinteger(L, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline int pushvec3(lua_State* L, glm::vec3 vec) {
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_pushnumber(L, vec.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline int pushvec4(lua_State* L, glm::vec4 vec) {
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_pushnumber(L, vec.w);
|
||||
return 4;
|
||||
}
|
||||
|
||||
inline int pushvec2_arr(lua_State* L, glm::vec2 vec) {
|
||||
lua_createtable(L, 2, 0);
|
||||
lua_getglobal(L, "vec2_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushvec3_arr(lua_State* L, glm::vec3 vec) {
|
||||
lua_createtable(L, 3, 0);
|
||||
lua_getglobal(L, "vec3_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushvec4_arr(lua_State* L, glm::vec4 vec) {
|
||||
lua_createtable(L, 4, 0);
|
||||
lua_getglobal(L, "vec4_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
lua_pushnumber(L, vec.w);
|
||||
lua_rawseti(L, -2, 4);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushcolor_arr(lua_State* L, glm::vec4 vec) {
|
||||
lua_createtable(L, 4, 0);
|
||||
lua_getglobal(L, "color_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushinteger(L, vec.x*255);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushinteger(L, vec.y*255);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushinteger(L, vec.z*255);
|
||||
lua_rawseti(L, -2, 3);
|
||||
lua_pushinteger(L, vec.w*255);
|
||||
lua_rawseti(L, -2, 4);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // LOGIC_SCRIPTING_LUA_UTIL_H_
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "../../engine.h"
|
||||
#include "LuaState.h"
|
||||
#include "../../util/stringutil.h"
|
||||
#include "../../util/timeutil.h"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
@@ -245,6 +246,7 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
|
||||
}
|
||||
|
||||
void scripting::on_ui_open(UiDocument* layout, Inventory* inventory) {
|
||||
timeutil::ScopeLogTimer log(555);
|
||||
std::string name = layout->getId()+".open";
|
||||
if (state->getglobal(name)) {
|
||||
state->pushinteger(inventory->getId());
|
||||
|
||||
Reference in New Issue
Block a user