From ce7037e9d0fc9bc8b558961fb00e5857049c86ec Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 12 Jul 2024 07:06:31 +0300 Subject: [PATCH] add new cameras.* functions --- res/layouts/console.xml.lua | 5 +- src/logic/scripting/lua/libcamera.cpp | 94 ++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/res/layouts/console.xml.lua b/res/layouts/console.xml.lua index 7462eeee..77cdf56f 100644 --- a/res/layouts/console.xml.lua +++ b/res/layouts/console.xml.lua @@ -7,7 +7,10 @@ function setup_variables() console.set('pos.x', x) console.set('pos.y', y) console.set('pos.z', z) - console.set('entity.id', player.get_entity(pid):get_uid()) + local pentity = player.get_entity(pid) + if pentity ~= nil then + console.set('entity.id', pentity:get_uid()) + end end function on_history_up() diff --git a/src/logic/scripting/lua/libcamera.cpp b/src/logic/scripting/lua/libcamera.cpp index 65ca32f4..cedff991 100644 --- a/src/logic/scripting/lua/libcamera.cpp +++ b/src/logic/scripting/lua/libcamera.cpp @@ -2,17 +2,107 @@ #include "../../../content/Content.hpp" #include "../../../world/Level.hpp" +#include "../../../window/Camera.hpp" using namespace scripting; -int l_index(lua::State* L) { +template +static int l_camera_getter(lua::State* L) { + size_t index = static_cast(lua::tointeger(L, 1)); + return getterfunc(L, *level->cameras.at(index)); +} + +template +static int l_camera_setter(lua::State* L) { + size_t index = static_cast(lua::tointeger(L, 1)); + setterfunc(L, *level->cameras.at(index), 2); + return 0; +} + +static int l_index(lua::State* L) { auto name = lua::require_string(L, 1); auto& indices = content->getIndices(ResourceType::CAMERA); - std::cout << "index: " << name << std::endl; return lua::pushinteger(L, indices.indexOf(name)); } +static int l_name(lua::State* L) { + size_t index = static_cast(lua::tointeger(L, 1)); + auto& indices = content->getIndices(ResourceType::CAMERA); + return lua::pushstring(L, indices.getName(index)); +} + +static int getter_pos(lua::State* L, const Camera& camera) { + return lua::pushvec3_arr(L, camera.position); +} + +static void setter_pos(lua::State* L, Camera& camera, int idx) { + camera.position = lua::tovec<3>(L, idx); +} + +static int getter_rot(lua::State* L, const Camera& camera) { + return lua::pushmat4(L, camera.rotation); +} + +static void setter_rot(lua::State* L, Camera& camera, int idx) { + camera.rotation = lua::tomat4(L, idx); + camera.updateVectors(); +} + +static int getter_zoom(lua::State* L, const Camera& camera) { + return lua::pushnumber(L, camera.zoom); +} +static void setter_zoom(lua::State* L, Camera& camera, int idx) { + camera.zoom = lua::tonumber(L, idx); +} + +static int getter_fov(lua::State* L, const Camera& camera) { + return lua::pushnumber(L, camera.getFov()); +} +static void setter_fov(lua::State* L, Camera& camera, int idx) { + camera.setFov(lua::tonumber(L, idx)); +} + +static int getter_perspective(lua::State* L, const Camera& camera) { + return lua::pushboolean(L, camera.perspective); +} +static void setter_perspective(lua::State* L, Camera& camera, int idx) { + camera.perspective = lua::toboolean(L, idx); +} + +static int getter_flipped(lua::State* L, const Camera& camera) { + return lua::pushboolean(L, camera.flipped); +} +static void setter_flipped(lua::State* L, Camera& camera, int idx) { + camera.flipped = lua::toboolean(L, idx); +} + +static int getter_front(lua::State* L, const Camera& camera) { + return lua::pushvec3_arr(L, camera.front); +} +static int getter_right(lua::State* L, const Camera& camera) { + return lua::pushvec3_arr(L, camera.right); +} +static int getter_up(lua::State* L, const Camera& camera) { + return lua::pushvec3_arr(L, camera.up); +} + const luaL_Reg cameralib [] = { {"index", lua::wrap}, + {"name", lua::wrap}, + {"get_pos", lua::wrap>}, + {"set_pos", lua::wrap>}, + {"get_rot", lua::wrap>}, + {"set_rot", lua::wrap>}, + {"get_zoom", lua::wrap>}, + {"set_zoom", lua::wrap>}, + {"get_fov", lua::wrap>}, + {"set_fov", lua::wrap>}, + {"is_perspective", lua::wrap>}, + {"set_perspective", lua::wrap>}, + {"is_flipped", lua::wrap>}, + {"set_flipped", lua::wrap>}, + {"get_front", lua::wrap>}, + {"get_right", lua::wrap>}, + {"get_up", lua::wrap>}, {NULL, NULL} };