even more boilerplate code
This commit is contained in:
parent
7792c1a6f4
commit
5c23b4125d
@ -178,6 +178,32 @@ static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement e
|
||||
return button;
|
||||
}
|
||||
|
||||
static std::shared_ptr<UINode> readCheckBox(UiXmlReader& reader, xml::xmlelement element) {
|
||||
auto text = readAndProcessInnerText(element);
|
||||
bool checked = element->attr("checked", "false").asBool();
|
||||
auto checkbox = std::make_shared<FullCheckBox>(text, glm::vec2(), checked);
|
||||
_readPanel(reader, element, *checkbox);
|
||||
|
||||
if (element->has("consumer")) {
|
||||
auto consumer = scripting::create_bool_consumer(
|
||||
reader.getEnvironment().getId(),
|
||||
element->attr("consumer").getText(),
|
||||
reader.getFilename()+".lua"
|
||||
);
|
||||
checkbox->setConsumer(consumer);
|
||||
}
|
||||
|
||||
if (element->has("supplier")) {
|
||||
auto supplier = scripting::create_bool_supplier(
|
||||
reader.getEnvironment().getId(),
|
||||
element->attr("supplier").getText(),
|
||||
reader.getFilename()+".lua"
|
||||
);
|
||||
checkbox->setSupplier(supplier);
|
||||
}
|
||||
return checkbox;
|
||||
}
|
||||
|
||||
static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement element) {
|
||||
auto placeholder = util::str2wstr_utf8(element->attr("placeholder", "").getText());
|
||||
auto text = readAndProcessInnerText(element);
|
||||
@ -193,6 +219,15 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
|
||||
);
|
||||
textbox->setTextConsumer(consumer);
|
||||
}
|
||||
|
||||
if (element->has("supplier")) {
|
||||
auto supplier = scripting::create_wstring_supplier(
|
||||
reader.getEnvironment().getId(),
|
||||
element->attr("consumer").getText(),
|
||||
reader.getFilename()+".lua"
|
||||
);
|
||||
textbox->setTextSupplier(supplier);
|
||||
}
|
||||
return textbox;
|
||||
}
|
||||
|
||||
@ -242,6 +277,7 @@ UiXmlReader::UiXmlReader(const scripting::Environment& env, AssetsLoader& assets
|
||||
add("panel", readPanel);
|
||||
add("button", readButton);
|
||||
add("textbox", readTextBox);
|
||||
add("chackbox", readCheckBox);
|
||||
add("trackbar", readTrackBar);
|
||||
add("container", readContainer);
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include "lua_util.h"
|
||||
#include "api_lua.h"
|
||||
#include "libgui.h"
|
||||
#include "libplayer.h"
|
||||
#include "libinventory.h"
|
||||
#include "../../../util/stringutil.h"
|
||||
|
||||
@ -172,6 +173,11 @@ int lua::LuaState::pushnumber(luanumber x) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::pushboolean(bool x) {
|
||||
lua_pushboolean(L, x);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int lua::LuaState::pushivec3(luaint x, luaint y, luaint z) {
|
||||
lua::pushivec3(L, x, y, z);
|
||||
return 3;
|
||||
@ -233,6 +239,10 @@ lua::luanumber lua::LuaState::tonumber(int idx) {
|
||||
return lua_tonumber(L, idx);
|
||||
}
|
||||
|
||||
const char* lua::LuaState::tostring(int idx) {
|
||||
return lua_tostring(L, idx);
|
||||
}
|
||||
|
||||
void lua::LuaState::openlib(const std::string& name, const luaL_Reg* libfuncs, int nup) {
|
||||
lua_newtable(L);
|
||||
luaL_setfuncs(L, libfuncs, nup);
|
||||
|
||||
@ -31,6 +31,7 @@ namespace lua {
|
||||
int pushivec3(luaint x, luaint y, luaint z);
|
||||
int pushinteger(luaint x);
|
||||
int pushnumber(luanumber x);
|
||||
int pushboolean(bool x);
|
||||
int pushstring(const std::string& str);
|
||||
int pushenv(int env);
|
||||
int pushvalue(int idx);
|
||||
@ -42,6 +43,7 @@ namespace lua {
|
||||
bool toboolean(int idx);
|
||||
luaint tointeger(int idx);
|
||||
luanumber tonumber(int idx);
|
||||
const char* tostring(int idx);
|
||||
int call(int argc);
|
||||
int callNoThrow(int argc);
|
||||
int execute(int env, const std::string& src, const std::string& file="<string>");
|
||||
|
||||
@ -142,61 +142,6 @@ int l_world_get_seed(lua_State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* == player library ==*/
|
||||
int l_player_get_pos(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
glm::vec3 pos = scripting::level->player->hitbox->position;
|
||||
lua_pushnumber(L, pos.x);
|
||||
lua_pushnumber(L, pos.y);
|
||||
lua_pushnumber(L, pos.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int l_player_get_rot(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
glm::vec2 rot = scripting::level->player->cam;
|
||||
lua_pushnumber(L, rot.x);
|
||||
lua_pushnumber(L, rot.y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int l_player_set_rot(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
lua::luanumber x = lua_tonumber(L, 2);
|
||||
lua::luanumber y = lua_tonumber(L, 3);
|
||||
glm::vec2& cam = scripting::level->player->cam;
|
||||
cam.x = x;
|
||||
cam.y = y;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_player_set_pos(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
lua::luanumber x = lua_tonumber(L, 2);
|
||||
lua::luanumber y = lua_tonumber(L, 3);
|
||||
lua::luanumber z = lua_tonumber(L, 4);
|
||||
scripting::level->player->hitbox->position = glm::vec3(x, y, z);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_player_get_inv(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
Player* player = scripting::level->player;
|
||||
lua_pushinteger(L, player->getInventory()->getId());
|
||||
lua_pushinteger(L, player->getChosenSlot());
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* == item library == */
|
||||
int l_item_name(lua_State* L) {
|
||||
auto indices = scripting::content->getIndices();
|
||||
|
||||
@ -58,22 +58,6 @@ static const luaL_Reg worldlib [] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == player library ==*/
|
||||
extern int l_player_get_pos(lua_State* L);
|
||||
extern int l_player_get_rot(lua_State* L);
|
||||
extern int l_player_set_rot(lua_State* L);
|
||||
extern int l_player_set_pos(lua_State* L);
|
||||
extern int l_player_get_inv(lua_State* L);
|
||||
|
||||
static const luaL_Reg playerlib [] = {
|
||||
{"get_pos", lua_wrap_errors<l_player_get_pos>},
|
||||
{"set_pos", lua_wrap_errors<l_player_set_pos>},
|
||||
{"get_rot", lua_wrap_errors<l_player_get_rot>},
|
||||
{"set_rot", lua_wrap_errors<l_player_set_rot>},
|
||||
{"get_inventory", lua_wrap_errors<l_player_get_inv>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/* == item library == */
|
||||
extern int l_item_name(lua_State* L);
|
||||
extern int l_item_index(lua_State* L);
|
||||
|
||||
@ -93,6 +93,16 @@ static bool getattr(lua_State* L, gui::Label* label, const std::string& attr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool getattr(lua_State* L, gui::FullCheckBox* box, const std::string& attr) {
|
||||
if (box == nullptr)
|
||||
return false;
|
||||
if (attr == "checked") {
|
||||
lua_pushboolean(L, box->isChecked());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool setattr(lua_State* L, gui::Button* button, const std::string& attr) {
|
||||
if (button == nullptr)
|
||||
return false;
|
||||
@ -105,6 +115,16 @@ static bool setattr(lua_State* L, gui::Button* button, const std::string& attr)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool setattr(lua_State* L, gui::FullCheckBox* box, const std::string& attr) {
|
||||
if (box == nullptr)
|
||||
return false;
|
||||
if (attr == "checked") {
|
||||
box->setChecked(lua_toboolean(L, 4));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool setattr(lua_State* L, gui::Label* label, const std::string& attr) {
|
||||
if (label == nullptr)
|
||||
return false;
|
||||
@ -143,6 +163,8 @@ int l_gui_getattr(lua_State* L) {
|
||||
return 1;
|
||||
if (getattr(L, dynamic_cast<gui::TrackBar*>(node), attr))
|
||||
return 1;
|
||||
if (getattr(L, dynamic_cast<gui::FullCheckBox*>(node), attr))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -177,6 +199,8 @@ int l_gui_setattr(lua_State* L) {
|
||||
return 0;
|
||||
if (setattr(L, dynamic_cast<gui::TrackBar*>(node), attr))
|
||||
return 0;
|
||||
if (setattr(L, dynamic_cast<gui::FullCheckBox*>(node), attr))
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
64
src/logic/scripting/lua/libplayer.cpp
Normal file
64
src/logic/scripting/lua/libplayer.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
#include "libplayer.h"
|
||||
#include "../scripting.h"
|
||||
#include "../../../world/Level.h"
|
||||
#include "../../../objects/Player.h"
|
||||
#include "../../../physics/Hitbox.h"
|
||||
#include "../../../window/Camera.h"
|
||||
#include "../../../items/Inventory.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
/* == player library ==*/
|
||||
int l_player_get_pos(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
glm::vec3 pos = scripting::level->player->hitbox->position;
|
||||
lua_pushnumber(L, pos.x);
|
||||
lua_pushnumber(L, pos.y);
|
||||
lua_pushnumber(L, pos.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
int l_player_get_rot(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
glm::vec2 rot = scripting::level->player->cam;
|
||||
lua_pushnumber(L, rot.x);
|
||||
lua_pushnumber(L, rot.y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int l_player_set_rot(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
lua::luanumber x = lua_tonumber(L, 2);
|
||||
lua::luanumber y = lua_tonumber(L, 3);
|
||||
glm::vec2& cam = scripting::level->player->cam;
|
||||
cam.x = x;
|
||||
cam.y = y;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_player_set_pos(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
lua::luanumber x = lua_tonumber(L, 2);
|
||||
lua::luanumber y = lua_tonumber(L, 3);
|
||||
lua::luanumber z = lua_tonumber(L, 4);
|
||||
scripting::level->player->hitbox->position = glm::vec3(x, y, z);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_player_get_inv(lua_State* L) {
|
||||
int playerid = lua_tointeger(L, 1);
|
||||
if (playerid != 1)
|
||||
return 0;
|
||||
Player* player = scripting::level->player;
|
||||
lua_pushinteger(L, player->getInventory()->getId());
|
||||
lua_pushinteger(L, player->getChosenSlot());
|
||||
return 2;
|
||||
}
|
||||
22
src/logic/scripting/lua/libplayer.h
Normal file
22
src/logic/scripting/lua/libplayer.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef LOGIC_SCRIPTING_LUA_LIBPLAYER_H_
|
||||
#define LOGIC_SCRIPTING_LUA_LIBPLAYER_H_
|
||||
|
||||
#include "lua_commons.h"
|
||||
|
||||
/* == player library ==*/
|
||||
extern int l_player_get_pos(lua_State* L);
|
||||
extern int l_player_get_rot(lua_State* L);
|
||||
extern int l_player_set_rot(lua_State* L);
|
||||
extern int l_player_set_pos(lua_State* L);
|
||||
extern int l_player_get_inv(lua_State* L);
|
||||
|
||||
static const luaL_Reg playerlib [] = {
|
||||
{"get_pos", lua_wrap_errors<l_player_get_pos>},
|
||||
{"set_pos", lua_wrap_errors<l_player_set_pos>},
|
||||
{"get_rot", lua_wrap_errors<l_player_get_rot>},
|
||||
{"set_rot", lua_wrap_errors<l_player_set_rot>},
|
||||
{"get_inventory", lua_wrap_errors<l_player_get_inv>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#endif // LOGIC_SCRIPTING_LUA_LIBPLAYER_H_
|
||||
@ -25,4 +25,4 @@ template <lua_CFunction func> int lua_wrap_errors(lua_State *L) {
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // LOGIC_SCRIPTING_LUA_H_
|
||||
#endif // LOGIC_SCRIPTING_LUA_H_
|
||||
|
||||
@ -51,6 +51,49 @@ wstringconsumer scripting::create_wstring_consumer(
|
||||
};
|
||||
}
|
||||
|
||||
wstringsupplier scripting::create_wstring_supplier(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](){
|
||||
if (processCallback(env, src, file)) {
|
||||
state->callNoThrow(0);
|
||||
auto str = state->tostring(-1); state->pop();
|
||||
return util::str2wstr_utf8(str);
|
||||
}
|
||||
return std::wstring(L"");
|
||||
};
|
||||
}
|
||||
|
||||
boolconsumer scripting::create_bool_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](bool x){
|
||||
if (processCallback(env, src, file)) {
|
||||
state->pushboolean(x);
|
||||
state->callNoThrow(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
boolsupplier scripting::create_bool_supplier(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](){
|
||||
if (processCallback(env, src, file)) {
|
||||
state->callNoThrow(0);
|
||||
bool x = state->toboolean(-1); state->pop();
|
||||
return x;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
doubleconsumer scripting::create_number_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
|
||||
@ -18,6 +18,24 @@ namespace scripting {
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
wstringsupplier create_wstring_supplier(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
boolconsumer create_bool_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
boolsupplier create_bool_supplier(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
doubleconsumer create_number_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user