misc hud-related updates
This commit is contained in:
@@ -217,12 +217,16 @@ void lua::LuaState::setfield(const std::string& name, int idx) {
|
||||
lua_setfield(L, idx, name.c_str());
|
||||
}
|
||||
|
||||
bool lua::LuaState::toboolean(int index) {
|
||||
return lua_toboolean(L, index);
|
||||
bool lua::LuaState::toboolean(int idx) {
|
||||
return lua_toboolean(L, idx);
|
||||
}
|
||||
|
||||
lua::luaint lua::LuaState::tointeger(int index) {
|
||||
return lua_tointeger(L, index);
|
||||
lua::luaint lua::LuaState::tointeger(int idx) {
|
||||
return lua_tointeger(L, idx);
|
||||
}
|
||||
|
||||
lua::luanumber lua::LuaState::tonumber(int idx) {
|
||||
return lua_tonumber(L, idx);
|
||||
}
|
||||
|
||||
void lua::LuaState::openlib(const std::string& name, const luaL_Reg* libfuncs, int nup) {
|
||||
|
||||
@@ -42,8 +42,9 @@ namespace lua {
|
||||
void pop(int n=1);
|
||||
bool getfield(const std::string& name);
|
||||
void setfield(const std::string& name, int idx=-2);
|
||||
bool toboolean(int index);
|
||||
luaint tointeger(int index);
|
||||
bool toboolean(int idx);
|
||||
luaint tointeger(int idx);
|
||||
luanumber tonumber(int idx);
|
||||
int call(int argc);
|
||||
int callNoThrow(int argc);
|
||||
int execute(int env, const std::string& src, const std::string& file="<string>");
|
||||
|
||||
@@ -85,6 +85,11 @@ int l_gui_getattr(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_gui_getviewport(lua_State* L) {
|
||||
lua::pushvec2_arr(L, scripting::engine->getGUI()->getContainer()->getSize());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int l_gui_setattr(lua_State* L) {
|
||||
auto docname = lua_tostring(L, 1);
|
||||
auto element = lua_tostring(L, 2);
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
|
||||
#include <lua.hpp>
|
||||
|
||||
extern int l_gui_getviewport(lua_State* L);
|
||||
extern int l_gui_getattr(lua_State* L);
|
||||
extern int l_gui_setattr(lua_State* L);
|
||||
|
||||
static const luaL_Reg guilib [] = {
|
||||
{"get_viewport", l_gui_getviewport},
|
||||
{"getattr", l_gui_getattr},
|
||||
{"setattr", l_gui_setattr},
|
||||
{NULL, NULL}
|
||||
|
||||
@@ -60,70 +60,6 @@ void scripting::initialize(Engine* engine) {
|
||||
load_script(fs::path("stdlib.lua"));
|
||||
}
|
||||
|
||||
runnable scripting::create_runnable(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](){
|
||||
state->execute(env, src, file);
|
||||
};
|
||||
}
|
||||
|
||||
static bool processCallback(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
try {
|
||||
return state->eval(env, src, file) != 0;
|
||||
} catch (lua::luaerror& err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
wstringconsumer scripting::create_wstring_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](const std::wstring& x){
|
||||
if (processCallback(env, src, file)) {
|
||||
state->pushstring(util::wstr2str_utf8(x));
|
||||
state->callNoThrow(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
doubleconsumer scripting::create_number_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](double x){
|
||||
if (processCallback(env, src, file)) {
|
||||
state->pushnumber(x);
|
||||
state->callNoThrow(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int_array_consumer scripting::create_int_array_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](const int arr[], size_t len){
|
||||
if (processCallback(env, src, file)) {
|
||||
for (uint i = 0; i < len; i++) {
|
||||
state->pushinteger(arr[i]);
|
||||
}
|
||||
state->callNoThrow(len);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_ptr<Environment> scripting::create_environment(int parent) {
|
||||
return std::make_unique<Environment>(state->createEnvironment(parent));
|
||||
}
|
||||
@@ -274,7 +210,7 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
|
||||
void scripting::on_ui_open(UiDocument* layout, Inventory* inventory) {
|
||||
std::string name = layout->getId()+".open";
|
||||
if (state->getglobal(name)) {
|
||||
state->pushinteger(inventory->getId());
|
||||
state->pushinteger(inventory == nullptr ? 0 : inventory->getId());
|
||||
state->callNoThrow(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
#include "../../delegates.h"
|
||||
|
||||
#include "scripting_functional.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
class LuaState;
|
||||
@@ -23,8 +25,6 @@ struct uidocscript;
|
||||
class BlocksController;
|
||||
|
||||
namespace scripting {
|
||||
using int_array_consumer = std::function<void(const int[], size_t)>;
|
||||
|
||||
extern Engine* engine;
|
||||
extern const Content* content;
|
||||
extern const ContentIndices* indices;
|
||||
@@ -43,30 +43,6 @@ namespace scripting {
|
||||
|
||||
void initialize(Engine* engine);
|
||||
|
||||
runnable create_runnable(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
wstringconsumer create_wstring_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
doubleconsumer create_number_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
int_array_consumer create_int_array_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
std::unique_ptr<Environment> create_environment(int parent=0);
|
||||
std::unique_ptr<Environment> create_pack_environment(const ContentPack& pack);
|
||||
std::unique_ptr<Environment> create_doc_environment(int parent, const std::string& name);
|
||||
@@ -81,10 +57,10 @@ namespace scripting {
|
||||
void on_block_broken(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);
|
||||
|
||||
/* Called on RMB click on block with the item selected
|
||||
/** Called on RMB click on block with the item selected
|
||||
@return true if prevents default action */
|
||||
bool on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||
/* Called on LMB click on block with the item selected
|
||||
/** Called on LMB click on block with the item selected
|
||||
@return true if prevents default action */
|
||||
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
#include "scripting_functional.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "LuaState.h"
|
||||
#include "../../util/stringutil.h"
|
||||
|
||||
namespace scripting {
|
||||
extern lua::LuaState* state;
|
||||
}
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
runnable scripting::create_runnable(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](){
|
||||
state->execute(env, src, file);
|
||||
};
|
||||
}
|
||||
|
||||
static bool processCallback(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
try {
|
||||
return state->eval(env, src, file) != 0;
|
||||
} catch (lua::luaerror& err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
wstringconsumer scripting::create_wstring_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](const std::wstring& x){
|
||||
if (processCallback(env, src, file)) {
|
||||
state->pushstring(util::wstr2str_utf8(x));
|
||||
state->callNoThrow(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
doubleconsumer scripting::create_number_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](double x){
|
||||
if (processCallback(env, src, file)) {
|
||||
state->pushnumber(x);
|
||||
state->callNoThrow(1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int_array_consumer scripting::create_int_array_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](const int arr[], size_t len){
|
||||
if (processCallback(env, src, file)) {
|
||||
for (uint i = 0; i < len; i++) {
|
||||
state->pushinteger(arr[i]);
|
||||
}
|
||||
state->callNoThrow(len);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
vec2supplier scripting::create_vec2_supplier(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](){
|
||||
if (processCallback(env, src, file)) {
|
||||
state->callNoThrow(0);
|
||||
lua::luanumber y = state->tonumber(-1); state->pop();
|
||||
lua::luanumber x = state->tonumber(-1); state->pop();
|
||||
return glm::vec2(x, y);
|
||||
}
|
||||
return glm::vec2(0, 0);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef LOGIC_SCRIPTING_SCRIPTING_FUNCTIONAL_H_
|
||||
#define LOGIC_SCRIPTING_SCRIPTING_FUNCTIONAL_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <string>
|
||||
#include "../../delegates.h"
|
||||
|
||||
namespace scripting {
|
||||
runnable create_runnable(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
wstringconsumer create_wstring_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
doubleconsumer create_number_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
int_array_consumer create_int_array_consumer(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
vec2supplier create_vec2_supplier(
|
||||
int env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
}
|
||||
|
||||
#endif // LOGIC_SCRIPTING_SCRIPTING_FUNCTIONAL_H_
|
||||
Reference in New Issue
Block a user