mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(main_menu): add exit button and refactor layout with ColumnLayout
- Add `should_close_window()` method to force window close from UI. - Replace standalone button with ColumnLayout for proper centering. - Add "Exit" button that calls `should_close_window()`. - Update ESC key handling to only close window when game is not running. - Fix ColumnLayout to respect parent's anchor instead of hardcoded TOP_LEFT. - Remove unused parameter name in Image::update for consistency.
This commit is contained in:
@@ -32,6 +32,8 @@ public:
|
|||||||
|
|
||||||
void set_game_running(bool running);
|
void set_game_running(bool running);
|
||||||
|
|
||||||
|
void should_close_window();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_mouse_enable = false;
|
bool m_mouse_enable = false;
|
||||||
bool m_imgui_init = false;
|
bool m_imgui_init = false;
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ void ColumnLayout::layout() {
|
|||||||
int y = 0;
|
int y = 0;
|
||||||
for (auto& child : children) {
|
for (auto& child : children) {
|
||||||
child->set_offset({0, y});
|
child->set_offset({0, y});
|
||||||
child->set_anchor(Anchor::TOP_LEFT);
|
child->set_anchor(m_anchor);
|
||||||
y += child->height() + m_spacing;
|
y += child->height() + m_spacing;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
Image::Image(Widget* parent) : Widget(parent) {}
|
Image::Image(Widget* parent) : Widget(parent) {}
|
||||||
void Image::update(float dt) { (void)dt; }
|
void Image::update(float) {}
|
||||||
void Image::render(Renderer& renderer) { renderer.render_image(*this); }
|
void Image::render(Renderer& renderer) { renderer.render_image(*this); }
|
||||||
|
|
||||||
Image& Image::set_image(const std::string& path,
|
Image& Image::set_image(const std::string& path,
|
||||||
|
|||||||
@@ -5,29 +5,48 @@
|
|||||||
#include "Cubed/scene/main_menu_scene.hpp"
|
#include "Cubed/scene/main_menu_scene.hpp"
|
||||||
#include "Cubed/scene/scene_manager.hpp"
|
#include "Cubed/scene/scene_manager.hpp"
|
||||||
#include "Cubed/ui/button.hpp"
|
#include "Cubed/ui/button.hpp"
|
||||||
|
#include "Cubed/ui/column_layout.hpp"
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|
||||||
MainMenuUIManager::MainMenuUIManager(MainMenuScene& scene) : m_scene(scene) {}
|
MainMenuUIManager::MainMenuUIManager(MainMenuScene& scene) : m_scene(scene) {}
|
||||||
MainMenuUIManager::~MainMenuUIManager() {}
|
MainMenuUIManager::~MainMenuUIManager() {}
|
||||||
|
|
||||||
void MainMenuUIManager::init() {
|
void MainMenuUIManager::init() {
|
||||||
|
auto& renderer = m_scene.scene_manager().app().renderer();
|
||||||
|
auto layout = std::make_unique<ColumnLayout>(nullptr);
|
||||||
|
layout->set_spacing(20);
|
||||||
|
layout->set_anchor(Anchor::CENTER);
|
||||||
|
layout->set_window_size(renderer.window_width(), renderer.window_height());
|
||||||
|
{
|
||||||
|
auto& start_game_button = layout->add_child<Button>();
|
||||||
|
|
||||||
auto start_game_button = std::make_unique<Button>(nullptr);
|
auto& back = start_game_button.set_background<Image>();
|
||||||
|
|
||||||
auto& back = start_game_button->set_background<Image>();
|
|
||||||
back.set_image("texture/ui/button001.png",
|
back.set_image("texture/ui/button001.png",
|
||||||
m_scene.scene_manager().app().texture_manager());
|
m_scene.scene_manager().app().texture_manager());
|
||||||
auto& fore = start_game_button->set_foreground<Label>();
|
auto& fore = start_game_button.set_foreground<Label>();
|
||||||
fore.set_text("Start Game");
|
fore.set_text("Start Game");
|
||||||
fore.set_scale(0.6f);
|
fore.set_scale(0.6f);
|
||||||
start_game_button->set_window_size(
|
start_game_button.set_clicked([this]() {
|
||||||
m_scene.scene_manager().app().renderer().window_width(),
|
m_scene.scene_manager().request_push(SceneType::WORLD);
|
||||||
m_scene.scene_manager().app().renderer().window_height());
|
});
|
||||||
start_game_button->set_clicked(
|
start_game_button.set_scale(4.0f);
|
||||||
[this]() { m_scene.scene_manager().request_push(SceneType::WORLD); });
|
}
|
||||||
start_game_button->set_scale(3.0f);
|
|
||||||
start_game_button->set_anchor(Anchor::CENTER);
|
auto& exit_game = layout->add_child<Button>();
|
||||||
m_widgets.try_emplace("start game", std::move(start_game_button));
|
|
||||||
|
{
|
||||||
|
auto& back = exit_game.set_background<Image>();
|
||||||
|
back.set_image("texture/ui/button001.png",
|
||||||
|
m_scene.scene_manager().app().texture_manager());
|
||||||
|
auto& fore = exit_game.set_foreground<Label>();
|
||||||
|
fore.set_scale(0.6f).set_text("Exit");
|
||||||
|
exit_game.set_scale(4.0f);
|
||||||
|
exit_game.set_clicked([this]() {
|
||||||
|
m_scene.scene_manager().app().window().should_close_window();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
m_widgets.try_emplace("main menu layout", std::move(layout));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainMenuUIManager::update(float dt) {
|
void MainMenuUIManager::update(float dt) {
|
||||||
|
|||||||
@@ -73,9 +73,11 @@ bool Window::handle_key_event(const KeyEvent& e) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (e.key == Key::ESCAPE && e.action == KeyAction::PRESS) {
|
if (e.key == Key::ESCAPE && e.action == KeyAction::PRESS) {
|
||||||
|
if (!m_game_running) {
|
||||||
glfwSetWindowShouldClose(m_window, GLFW_TRUE);
|
glfwSetWindowShouldClose(m_window, GLFW_TRUE);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (e.key == Key::LEFT_ALT && e.action == KeyAction::PRESS) {
|
if (e.key == Key::LEFT_ALT && e.action == KeyAction::PRESS) {
|
||||||
toggle_mouse_able();
|
toggle_mouse_able();
|
||||||
return true;
|
return true;
|
||||||
@@ -237,6 +239,9 @@ void Window::set_game_running(bool running) {
|
|||||||
m_mouse_enable = running;
|
m_mouse_enable = running;
|
||||||
toggle_mouse_able();
|
toggle_mouse_able();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::should_close_window() { glfwSetWindowShouldClose(m_window, true); }
|
||||||
|
|
||||||
void Window::imgui_init() {
|
void Window::imgui_init() {
|
||||||
float dpi_scale_x, dpi_scale_y;
|
float dpi_scale_x, dpi_scale_y;
|
||||||
glfwGetWindowContentScale(m_window, &dpi_scale_x, &dpi_scale_y);
|
glfwGetWindowContentScale(m_window, &dpi_scale_x, &dpi_scale_y);
|
||||||
|
|||||||
Reference in New Issue
Block a user