From 1e91239d7e9f9d2e650a58275627b7a860d781d6 Mon Sep 17 00:00:00 2001 From: Lars Ivar Hatledal Date: Sun, 17 Mar 2024 14:10:27 +0100 Subject: [PATCH] refactor hud and text --- examples/demo.cpp | 6 +++--- examples/geometries/heightmap.cpp | 2 +- examples/objects/instancing.cpp | 2 +- examples/objects/lod.cpp | 2 +- examples/objects/sprite.cpp | 2 +- examples/projects/Crane3R/main.cpp | 2 +- examples/projects/MotorControl/main.cpp | 2 +- examples/projects/Snake/main.cpp | 2 +- examples/projects/Youbot/youbot.cpp | 2 +- examples/projects/Youbot/youbot_kine.cpp | 2 +- include/threepp/objects/HUD.hpp | 3 ++- include/threepp/objects/Text.hpp | 19 ++++++++++++++----- include/threepp/scenes/Scene.hpp | 2 +- src/threepp/objects/HUD.cpp | 15 +++++++++------ src/threepp/scenes/Scene.cpp | 2 +- 15 files changed, 39 insertions(+), 26 deletions(-) diff --git a/examples/demo.cpp b/examples/demo.cpp index 8d3f81ec6..97b892489 100644 --- a/examples/demo.cpp +++ b/examples/demo.cpp @@ -118,7 +118,7 @@ int main() { auto planeMaterial = plane->material()->as(); scene->add(plane); - HUD hud(canvas); + HUD hud(canvas.size()); FontLoader fontLoader; const auto font1 = fontLoader.defaultFont(); const auto font2 = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json"); @@ -129,7 +129,7 @@ int main() { hud.add(hudText1, HUD::Options()); TextGeometry::Options opts2(font2, 10, 1); - auto hudText2 = Text2D(opts1, ""); + auto hudText2 = Text2D(opts2); hudText2.setColor(Color::red); hud.add(hudText2, HUD::Options() .setNormalizedPosition({1, 1}) @@ -155,7 +155,7 @@ int main() { box->rotation.y += 0.5f * dt; - hudText2.setText("Delta=" + std::to_string(dt), opts2); + hudText2.setText("Delta=" + std::to_string(dt)); hud.needsUpdate(hudText2); renderer.clear(); diff --git a/examples/geometries/heightmap.cpp b/examples/geometries/heightmap.cpp index 4907b7932..26f4b0e71 100644 --- a/examples/geometries/heightmap.cpp +++ b/examples/geometries/heightmap.cpp @@ -98,7 +98,7 @@ int main() { FontLoader fontLoader; - HUD hud(canvas); + HUD hud(canvas.size()); TextGeometry::Options opts(fontLoader.defaultFont(), 40); Text2D hudText(opts, "Loading terrain.."); hudText.material()->as()->color.setHex(Color::black); diff --git a/examples/objects/instancing.cpp b/examples/objects/instancing.cpp index a3b007b26..0d7734a27 100644 --- a/examples/objects/instancing.cpp +++ b/examples/objects/instancing.cpp @@ -88,7 +88,7 @@ int main() { canvas.setIOCapture(&capture); #endif - HUD hud(canvas); + HUD hud(canvas.size()); FontLoader fontLoader; const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json"); diff --git a/examples/objects/lod.cpp b/examples/objects/lod.cpp index 68b3d92d2..e1f588566 100644 --- a/examples/objects/lod.cpp +++ b/examples/objects/lod.cpp @@ -33,7 +33,7 @@ int main() { renderer.setSize(size); }); - HUD hud(canvas); + HUD hud(canvas.size()); FontLoader fontLoader; const auto font = *fontLoader.load("data/fonts/gentilis_bold.typeface.json"); diff --git a/examples/objects/sprite.cpp b/examples/objects/sprite.cpp index 27ccceb8b..820333156 100644 --- a/examples/objects/sprite.cpp +++ b/examples/objects/sprite.cpp @@ -86,7 +86,7 @@ int main() { auto helper = Mesh::create(SphereGeometry::create(0.1)); scene->add(helper); - HUD hud(canvas); + HUD hud(&canvas); createHudSprites(hud); canvas.onWindowResize([&](WindowSize size) { diff --git a/examples/projects/Crane3R/main.cpp b/examples/projects/Crane3R/main.cpp index ff920c57e..24545e1e8 100644 --- a/examples/projects/Crane3R/main.cpp +++ b/examples/projects/Crane3R/main.cpp @@ -116,7 +116,7 @@ int main() { scene->add(light1); scene->add(light2); - HUD hud(canvas); + HUD hud(canvas.size()); FontLoader fontLoader; const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json"); diff --git a/examples/projects/MotorControl/main.cpp b/examples/projects/MotorControl/main.cpp index 0ebd40eb2..fa3f352c9 100644 --- a/examples/projects/MotorControl/main.cpp +++ b/examples/projects/MotorControl/main.cpp @@ -110,7 +110,7 @@ int main() { auto motorVisuals = VisualisationObject(); scene.add(motorVisuals); - HUD hud(canvas); + HUD hud(canvas.size()); FontLoader fontLoader; auto font = fontLoader.defaultFont(); diff --git a/examples/projects/Snake/main.cpp b/examples/projects/Snake/main.cpp index 84e57b506..f81838bc3 100644 --- a/examples/projects/Snake/main.cpp +++ b/examples/projects/Snake/main.cpp @@ -18,7 +18,7 @@ int main() { auto camera = OrthographicCamera::create(0, game.gridSize(), 0, game.gridSize()); camera->position.z = 1; - HUD hud(canvas); + HUD hud(canvas.size()); FontLoader fontLoader; const auto font = fontLoader.defaultFont(); diff --git a/examples/projects/Youbot/youbot.cpp b/examples/projects/Youbot/youbot.cpp index c6b5a66ff..2f55a25f2 100644 --- a/examples/projects/Youbot/youbot.cpp +++ b/examples/projects/Youbot/youbot.cpp @@ -32,7 +32,7 @@ int main() { auto light2 = AmbientLight::create(0xffffff, 1.f); scene->add(light2); - HUD hud(canvas); + HUD hud(canvas.size()); FontLoader fontLoader; const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json"); diff --git a/examples/projects/Youbot/youbot_kine.cpp b/examples/projects/Youbot/youbot_kine.cpp index 7ea7e0abb..64c1562bb 100644 --- a/examples/projects/Youbot/youbot_kine.cpp +++ b/examples/projects/Youbot/youbot_kine.cpp @@ -93,7 +93,7 @@ int main() { auto targetHelper = AxesHelper::create(2); targetHelper->visible = false; - HUD hud(canvas); + HUD hud(canvas.size()); FontLoader fontLoader; const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json"); diff --git a/include/threepp/objects/HUD.hpp b/include/threepp/objects/HUD.hpp index 23186bb2d..da0255f2a 100644 --- a/include/threepp/objects/HUD.hpp +++ b/include/threepp/objects/HUD.hpp @@ -84,7 +84,8 @@ namespace threepp { HorizontalAlignment horizontalAlignment_; }; - explicit HUD(PeripheralsEventSource& eventSource); + explicit HUD(WindowSize size); + explicit HUD(PeripheralsEventSource* eventSource); void apply(GLRenderer& renderer); diff --git a/include/threepp/objects/Text.hpp b/include/threepp/objects/Text.hpp index ade468d50..f06cfd473 100644 --- a/include/threepp/objects/Text.hpp +++ b/include/threepp/objects/Text.hpp @@ -13,8 +13,8 @@ namespace threepp { class Text2D: public Mesh { public: - Text2D(const TextGeometry::Options& opts, const std::string& str, const std::shared_ptr& material = nullptr) - : Mesh(TextGeometry::create(str, opts), material ? material : SpriteMaterial::create()) {} + Text2D(const TextGeometry::Options& opts, const std::string& str = "", const std::shared_ptr& material = nullptr) + : Mesh(TextGeometry::create(str, opts), material ? material : SpriteMaterial::create()), options(opts) {} void setColor(const Color& color) { @@ -23,22 +23,31 @@ namespace threepp { } } + void setText(const std::string& str) { + + auto geometry = TextGeometry::create(str, options); + setGeometry(geometry); + } + void setText(const std::string& str, const TextGeometry::Options& opts) { auto geometry = TextGeometry::create(str, opts); setGeometry(geometry); } - static std::shared_ptr create(const TextGeometry::Options& opts, const std::string& str, const std::shared_ptr& material = nullptr) { + static std::shared_ptr create(const TextGeometry::Options& opts, const std::string& str = "", const std::shared_ptr& material = nullptr) { return std::make_shared(opts, str, material); } + + private: + TextGeometry::Options options; }; class Text3D: public Mesh { public: - Text3D(const ExtrudeTextGeometry::Options& opts, const std::string& str, const std::shared_ptr& material = nullptr) + Text3D(const ExtrudeTextGeometry::Options& opts, const std::string& str = "", const std::shared_ptr& material = nullptr) : Mesh(ExtrudeTextGeometry::create(str, opts), material ? material : MeshBasicMaterial::create()) {} void setColor(const Color& color) { @@ -54,7 +63,7 @@ namespace threepp { setGeometry(geometry); } - static std::shared_ptr create(const ExtrudeTextGeometry::Options& opts, const std::string& str, const std::shared_ptr& material = nullptr) { + static std::shared_ptr create(const ExtrudeTextGeometry::Options& opts, const std::string& str = "", const std::shared_ptr& material = nullptr) { return std::make_shared(opts, str, material); } diff --git a/include/threepp/scenes/Scene.hpp b/include/threepp/scenes/Scene.hpp index 3fc2c810b..36f30c666 100644 --- a/include/threepp/scenes/Scene.hpp +++ b/include/threepp/scenes/Scene.hpp @@ -30,7 +30,7 @@ namespace threepp { [[nodiscard]] bool isTexture() const; - [[nodiscard]] Color color() const; + [[nodiscard]] Color& color(); [[nodiscard]] std::shared_ptr texture() const; diff --git a/src/threepp/objects/HUD.cpp b/src/threepp/objects/HUD.cpp index 7050cb59e..05edf16d5 100644 --- a/src/threepp/objects/HUD.cpp +++ b/src/threepp/objects/HUD.cpp @@ -43,12 +43,12 @@ void HUD::Options::updateElement(Object3D& o, WindowSize windowSize) { struct HUD::Impl: Scene, MouseListener { - Impl(PeripheralsEventSource* eventSource) + Impl(PeripheralsEventSource* eventSource, const WindowSize& size) : eventSource_(eventSource), - size_(eventSource->size()), + size_(size), camera_(0, size_.width, size_.height, 0, 0.1, 10) { - eventSource->addMouseListener(*this); + if (eventSource) eventSource->addMouseListener(*this); camera_.position.z = 1; } @@ -133,7 +133,7 @@ struct HUD::Impl: Scene, MouseListener { } ~Impl() override { - eventSource_->removeMouseListener(*this); + if (eventSource_) eventSource_->removeMouseListener(*this); } private: @@ -148,9 +148,12 @@ struct HUD::Impl: Scene, MouseListener { std::unordered_map map_; }; +HUD::HUD(WindowSize size) + : pimpl_(std::make_unique(nullptr, size)) {} -HUD::HUD(PeripheralsEventSource& eventSource) - : pimpl_(std::make_unique(&eventSource)) {} + +HUD::HUD(PeripheralsEventSource* eventSource) + : pimpl_(std::make_unique(eventSource, eventSource->size())) {} void HUD::apply(GLRenderer& renderer) { pimpl_->apply(renderer); diff --git a/src/threepp/scenes/Scene.cpp b/src/threepp/scenes/Scene.cpp index 51ac81983..8caa7a0a7 100644 --- a/src/threepp/scenes/Scene.cpp +++ b/src/threepp/scenes/Scene.cpp @@ -32,7 +32,7 @@ bool Background::isTexture() const { return texture_ != nullptr; } -Color Background::color() const { +Color& Background::color() { return *color_; }