From 3a1e90f517170fa95f255310c20059845d57a980 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 14 Nov 2024 21:49:43 +0300 Subject: [PATCH] add gfx.text3d OOP wrapper --- res/content/base/scripts/hud.lua | 6 +++--- res/scripts/hud_classes.lua | 13 +++++++++++++ src/logic/scripting/scripting_hud.cpp | 10 ++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 res/scripts/hud_classes.lua diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 55b78fa64..f00fa1208 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -1,7 +1,7 @@ local DROP_FORCE = 8 local DROP_INIT_VEL = {0, 3, 0} -local textid +local note function on_hud_open() input.add_callback("player.drop", function () @@ -27,7 +27,7 @@ function on_hud_open() drop.rigidbody:set_vel(velocity) end) - textid = gfx.text3d.show({0.5, 99.5, 0.0015}, "Segmentation fault", { + note = gfx.text3d.new({0.5, 99.5, 0.0015}, "Segmentation fault", { scale=0.005, color={0, 0, 0, 1}, displayMode="static_billboard" @@ -35,7 +35,7 @@ function on_hud_open() end function on_hud_render() - gfx.text3d.update_settings(textid, { + note:update_settings({ color={math.sin(time.uptime() * 12) * 0.5 + 0.5, 0, 0, 1} }) end diff --git a/res/scripts/hud_classes.lua b/res/scripts/hud_classes.lua new file mode 100644 index 000000000..084eb0088 --- /dev/null +++ b/res/scripts/hud_classes.lua @@ -0,0 +1,13 @@ +local Text3D = {__index={ + hide=function(self) return gfx.text3d.hide(self.id) end, + get_pos=function(self) return gfx.text3d.get_pos(self.id) end, + set_pos=function(self, v) return gfx.text3d.set_pos(self.id, v) end, + get_text=function(self) return gfx.text3d.get_text(self.id) end, + set_text=function(self, s) return gfx.text3d.set_text(self.id, s) end, + update_settings=function(self, t) return gfx.text3d.update_settings(self.id, t) end, +}} + +gfx.text3d.new = function(pos, text, preset, extension) + local id = gfx.text3d.show(pos, text, preset, extension) + return setmetatable({id=id}, Text3D) +end diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index 78e7bb129..41fb163f0 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -17,6 +17,14 @@ static debug::Logger logger("scripting-hud"); Hud* scripting::hud = nullptr; WorldRenderer* scripting::renderer = nullptr; +static void load_script(const std::string& name) { + auto file = engine->getPaths()->getResourcesFolder() / "scripts" / name; + std::string src = files::read_string(file); + logger.info() << "loading script " << file.u8string(); + + lua::execute(lua::get_main_state(), 0, src, file.u8string()); +} + void scripting::on_frontend_init(Hud* hud, WorldRenderer* renderer) { scripting::hud = hud; scripting::renderer = renderer; @@ -27,6 +35,8 @@ void scripting::on_frontend_init(Hud* hud, WorldRenderer* renderer) { lua::openlib(L, "gfx", "particles", particleslib); lua::openlib(L, "gfx", "text3d", text3dlib); + load_script("hud_classes.lua"); + if (lua::getglobal(L, "__vc_create_hud_rules")) { lua::call_nothrow(L, 0, 0); }