From 5f60910aebdbe9e730ca3d0688d942e1da3c8a93 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Fri, 7 Aug 2026 18:09:30 +0800 Subject: [PATCH] feat(ui): add lightbox viewer for screenshots Add a lightbox to preview screenshots on click. Introduce Button::set_texture for image-backed buttons, add widget getters for window size and visibility, and block background input while the lightbox is open. Close the lightbox with Escape or a left-click. --- include/Cubed/ui/button.hpp | 1 + include/Cubed/ui/screenshot_ui.hpp | 18 ++++- include/Cubed/ui/widget.hpp | 9 ++- src/ui/button.cpp | 13 +++- src/ui/screenshot_ui.cpp | 118 ++++++++++++++++++++++------- src/ui/widget.cpp | 4 + 6 files changed, 134 insertions(+), 29 deletions(-) diff --git a/include/Cubed/ui/button.hpp b/include/Cubed/ui/button.hpp index 1d8b306..a1f40f3 100644 --- a/include/Cubed/ui/button.hpp +++ b/include/Cubed/ui/button.hpp @@ -28,6 +28,7 @@ public: Button& set_background_image(const std::string& path, TextureManager& texture_manager); Button& set_default_image(TextureManager& texture_manager); + Button& set_texture(const Texture* texture, bool change_button_size); virtual Button& set_text(const std::string& text); Button& set_auto_scale(bool auto_scale); diff --git a/include/Cubed/ui/screenshot_ui.hpp b/include/Cubed/ui/screenshot_ui.hpp index 4167c7f..edf0625 100644 --- a/include/Cubed/ui/screenshot_ui.hpp +++ b/include/Cubed/ui/screenshot_ui.hpp @@ -1,7 +1,9 @@ #pragma once +#include "Cubed/ui/image.hpp" #include "Cubed/ui/ui_manager.hpp" namespace Cubed { +class Texture; class ScreenshotScene; class ScreenshotUI : public UIManager { public: @@ -17,9 +19,23 @@ public: void update_layout(int width, int height); + void open_lightbox(const Texture* image); + private: - bool handle_window_resize_event(const WindowResizeEvent& e) override; + struct ImageInfo { + const Texture* texture = nullptr; + int width = 0; + int height = 0; + }; ScreenshotScene& m_scene; + std::vector m_image_infos; + Widget* m_lightbox = nullptr; + Image* m_lightbox_image = nullptr; + bool handle_window_resize_event(const WindowResizeEvent& e) override; + bool handle_key_event(const KeyEvent& e) override; + bool handle_mouse_button_event(const MouseButtonEvent& e) override; + bool handle_mouse_move_event(const MouseMoveEvent& e) override; + bool handle_mouse_wheel_event(const MouseWheelEvent& e) override; }; } // namespace Cubed \ No newline at end of file diff --git a/include/Cubed/ui/widget.hpp b/include/Cubed/ui/widget.hpp index 49f67b5..65aed0f 100644 --- a/include/Cubed/ui/widget.hpp +++ b/include/Cubed/ui/widget.hpp @@ -28,6 +28,11 @@ public: Widget& operator=(Widget&&) = delete; Widget(Widget* parent); + + static void set_window_size(int width, int height); + static int get_window_height(); + static int get_window_width(); + virtual ~Widget() = default; virtual void update(float dt); virtual void render(Renderer& renderer); @@ -35,7 +40,7 @@ public: virtual Widget& set_offset(glm::vec2 offset); virtual Widget& add_offset(glm::vec2 offset); virtual glm::vec2 get_offset() const; - static void set_window_size(int width, int height); + virtual Widget& set_visible(bool visible); // Returns the final display size @@ -58,6 +63,8 @@ public: virtual bool handle_window_resize_event(const WindowResizeEvent& e); virtual bool handle_mouse_move_event(const MouseMoveEvent& e); virtual bool handle_text_input_event(const TextInputEvent& e); + + virtual bool is_visible() const; void set_order(TraversalOrder order); template T& add_child(Args&&... args) { diff --git a/src/ui/button.cpp b/src/ui/button.cpp index 6b8091a..72aa13e 100644 --- a/src/ui/button.cpp +++ b/src/ui/button.cpp @@ -103,7 +103,18 @@ Button& Button::set_background_image(const std::string& path, Button& Button::set_default_image(TextureManager& texture_manager) { return set_background_image(DEFAULT_BUTTON_IMAGE, texture_manager); } - +Button& Button::set_texture(const Texture* texture, bool change_button_size) { + if (!texture) { + Logger::error("Button Background Image is nullptr"); + return *this; + } + m_background->set_texture(texture, false); + if (change_button_size) { + set_width(texture->width()); + set_height(texture->height()); + } + return *this; +} Button& Button::set_text(const std::string& text) { m_foreground->set_text(text); update_text_scale(); diff --git a/src/ui/screenshot_ui.cpp b/src/ui/screenshot_ui.cpp index 8b166ef..86db2a6 100644 --- a/src/ui/screenshot_ui.cpp +++ b/src/ui/screenshot_ui.cpp @@ -27,6 +27,9 @@ void ScreenshotUI::init() { update_layout(renderer.window_width(), renderer.window_height()); } void ScreenshotUI::update_layout(int width, int height) { + + m_image_infos.clear(); + auto& texture_manager = m_scene.scene_manager().app().texture_manager(); m_root_widget.reset(); auto bg = std::make_unique(nullptr); @@ -37,25 +40,11 @@ void ScreenshotUI::update_layout(int width, int height) { title.set_text(tr("menu.screenshot")).set_anchor(Anchor::TOP_CENTER); title.set_offset({0, 5}); - struct ImageInfo { - const Texture* texture = nullptr; - int width = 0; - int height = 0; - }; - - auto& return_button = bg->add_child