mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(scene): implement pause menu with UI manager and escape key toggling
Add PauseMenuUIManager class and integrate pause state into WorldScene. Escape key toggles pause in ClientPlayer and PauseMenuUIManager. Refactor Window mouse and imgui control to support pause state.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include "Cubed/gameplay/client_world.hpp"
|
#include "Cubed/gameplay/client_world.hpp"
|
||||||
#include "Cubed/gameplay/network_server.hpp"
|
#include "Cubed/gameplay/network_server.hpp"
|
||||||
#include "Cubed/scene/scene.hpp"
|
#include "Cubed/scene/scene.hpp"
|
||||||
|
#include "Cubed/ui/pasue_menu_ui_manager.hpp"
|
||||||
#include "Cubed/ui/world_ui_manager.hpp"
|
#include "Cubed/ui/world_ui_manager.hpp"
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
class SceneManager;
|
class SceneManager;
|
||||||
@@ -28,6 +29,8 @@ public:
|
|||||||
SceneManager& scene_manager();
|
SceneManager& scene_manager();
|
||||||
ClientWorld& client_world();
|
ClientWorld& client_world();
|
||||||
ServerWorld& server_world();
|
ServerWorld& server_world();
|
||||||
|
bool pause() const;
|
||||||
|
void set_pause(bool pause);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SceneManager& m_scene_manager;
|
SceneManager& m_scene_manager;
|
||||||
@@ -36,6 +39,8 @@ private:
|
|||||||
NetworkServer m_server;
|
NetworkServer m_server;
|
||||||
std::shared_ptr<NetworkClient> m_client;
|
std::shared_ptr<NetworkClient> m_client;
|
||||||
ClientWorld m_client_world;
|
ClientWorld m_client_world;
|
||||||
|
bool m_paused = false;
|
||||||
|
PauseMenuUIManager m_pasue_menu;
|
||||||
WorldUIManager m_ui_manager;
|
WorldUIManager m_ui_manager;
|
||||||
const Argument& m_argument;
|
const Argument& m_argument;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ namespace Cubed {
|
|||||||
class TextureManager;
|
class TextureManager;
|
||||||
class Image : public Widget {
|
class Image : public Widget {
|
||||||
public:
|
public:
|
||||||
|
Image(const Image&) = delete;
|
||||||
|
Image(Image&&) = delete;
|
||||||
|
Image& operator=(const Image&) = delete;
|
||||||
|
Image& operator=(Image&&) = delete;
|
||||||
Image(Widget* parent);
|
Image(Widget* parent);
|
||||||
|
|
||||||
Image& set_image(const std::string& path, TextureManager& texture_manager);
|
Image& set_image(const std::string& path, TextureManager& texture_manager);
|
||||||
|
|||||||
20
include/Cubed/ui/pasue_menu_ui_manager.hpp
Normal file
20
include/Cubed/ui/pasue_menu_ui_manager.hpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Cubed/ui/ui_manager.hpp"
|
||||||
|
namespace Cubed {
|
||||||
|
class WorldScene;
|
||||||
|
class PauseMenuUIManager : public UIManager {
|
||||||
|
public:
|
||||||
|
PauseMenuUIManager(const PauseMenuUIManager&) = delete;
|
||||||
|
PauseMenuUIManager(PauseMenuUIManager&&) = delete;
|
||||||
|
PauseMenuUIManager& operator=(const PauseMenuUIManager&) = delete;
|
||||||
|
PauseMenuUIManager& operator=(PauseMenuUIManager&&) = delete;
|
||||||
|
PauseMenuUIManager(WorldScene& scene);
|
||||||
|
void init() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
WorldScene& m_scene;
|
||||||
|
|
||||||
|
bool handle_key_event(const KeyEvent& e) override;
|
||||||
|
};
|
||||||
|
} // namespace Cubed
|
||||||
@@ -25,7 +25,9 @@ public:
|
|||||||
void hot_reload();
|
void hot_reload();
|
||||||
|
|
||||||
void toggle_fullscreen();
|
void toggle_fullscreen();
|
||||||
void toggle_mouse_able();
|
|
||||||
|
void enable_mouse();
|
||||||
|
void disable_mouse();
|
||||||
|
|
||||||
void set_camera(Camera* camera);
|
void set_camera(Camera* camera);
|
||||||
Camera* camera();
|
Camera* camera();
|
||||||
@@ -34,6 +36,10 @@ public:
|
|||||||
|
|
||||||
void should_close_window();
|
void should_close_window();
|
||||||
|
|
||||||
|
bool is_enable_imgui() const;
|
||||||
|
|
||||||
|
void set_imgui_enabled(bool enable);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_mouse_enable = false;
|
bool m_mouse_enable = false;
|
||||||
bool m_imgui_init = false;
|
bool m_imgui_init = false;
|
||||||
@@ -43,6 +49,7 @@ private:
|
|||||||
int m_window_height;
|
int m_window_height;
|
||||||
Config& m_config;
|
Config& m_config;
|
||||||
Camera* m_camera = nullptr;
|
Camera* m_camera = nullptr;
|
||||||
|
bool m_imgui_enable = false;
|
||||||
bool handle_key_event(const KeyEvent& e);
|
bool handle_key_event(const KeyEvent& e);
|
||||||
bool handle_window_resize_event(const WindowResizeEvent& e);
|
bool handle_window_resize_event(const WindowResizeEvent& e);
|
||||||
bool handle_mouse_button_event(const MouseButtonEvent& e);
|
bool handle_mouse_button_event(const MouseButtonEvent& e);
|
||||||
|
|||||||
@@ -70,4 +70,5 @@ target_sources(${PROJECT_NAME}
|
|||||||
ui/column_layout.cpp
|
ui/column_layout.cpp
|
||||||
ui/rect.cpp
|
ui/rect.cpp
|
||||||
ui/ui_manager.cpp
|
ui/ui_manager.cpp
|
||||||
|
ui/pause_menu_ui_manager.cpp
|
||||||
)
|
)
|
||||||
22
src/app.cpp
22
src/app.cpp
@@ -28,14 +28,13 @@ void App::cursor_position_callback(GLFWwindow* window, double xpos,
|
|||||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||||
|
|
||||||
ASSERT_MSG(app, "nullptr");
|
ASSERT_MSG(app, "nullptr");
|
||||||
if (io.WantCaptureMouse && app->m_window.is_mouse_enable()) {
|
if (io.WantCaptureMouse && app->m_window.is_enable_imgui()) {
|
||||||
ImGui_ImplGlfw_CursorPosCallback(window, xpos, ypos);
|
ImGui_ImplGlfw_CursorPosCallback(window, xpos, ypos);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!app->m_window.is_mouse_enable()) {
|
|
||||||
app->m_scene_manager.handle_event(
|
app->m_scene_manager.handle_event(
|
||||||
MouseMoveEvent{static_cast<float>(xpos), static_cast<float>(ypos)});
|
MouseMoveEvent{static_cast<float>(xpos), static_cast<float>(ypos)});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void App::init(int argc, char** argv) {
|
void App::init(int argc, char** argv) {
|
||||||
handle_toml();
|
handle_toml();
|
||||||
@@ -173,7 +172,7 @@ void App::key_callback(GLFWwindow* window, int key, int scancode, int action,
|
|||||||
ASSERT_MSG(app, "nullptr");
|
ASSERT_MSG(app, "nullptr");
|
||||||
// ImGui_ImplGlfw_CursorEnterCallback(window,
|
// ImGui_ImplGlfw_CursorEnterCallback(window,
|
||||||
// !app->m_window.is_mouse_enable());
|
// !app->m_window.is_mouse_enable());
|
||||||
if (io.WantCaptureKeyboard && app->m_window.is_mouse_enable()) {
|
if (io.WantCaptureKeyboard && app->m_window.is_enable_imgui()) {
|
||||||
if ((key == GLFW_KEY_LEFT_ALT) && action == GLFW_PRESS) {
|
if ((key == GLFW_KEY_LEFT_ALT) && action == GLFW_PRESS) {
|
||||||
app->dispatch_event(KeyEvent{Key::LEFT_ALT, KeyAction::PRESS});
|
app->dispatch_event(KeyEvent{Key::LEFT_ALT, KeyAction::PRESS});
|
||||||
return;
|
return;
|
||||||
@@ -532,7 +531,7 @@ void App::mouse_button_callback(GLFWwindow* window, int button, int action,
|
|||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||||
ASSERT_MSG(app, "nullptr");
|
ASSERT_MSG(app, "nullptr");
|
||||||
if (io.WantCaptureMouse && app->m_window.is_mouse_enable()) {
|
if (io.WantCaptureMouse && app->m_window.is_enable_imgui()) {
|
||||||
ImGui_ImplGlfw_MouseButtonCallback(window, button, action, mods);
|
ImGui_ImplGlfw_MouseButtonCallback(window, button, action, mods);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -582,7 +581,7 @@ void App::window_focus_callback(GLFWwindow* window, int focused) {
|
|||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||||
ASSERT_MSG(app, "nullptr");
|
ASSERT_MSG(app, "nullptr");
|
||||||
if (io.WantCaptureMouse && app->m_window.is_mouse_enable()) {
|
if (io.WantCaptureMouse && app->m_window.is_enable_imgui()) {
|
||||||
ImGui_ImplGlfw_WindowFocusCallback(window, focused);
|
ImGui_ImplGlfw_WindowFocusCallback(window, focused);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -616,7 +615,7 @@ void App::mouse_scroll_callback(GLFWwindow* window, double xoffset,
|
|||||||
|
|
||||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||||
ASSERT_MSG(app, "nullptr");
|
ASSERT_MSG(app, "nullptr");
|
||||||
if (io.WantCaptureMouse && app->m_window.is_mouse_enable()) {
|
if (io.WantCaptureMouse && app->m_window.is_enable_imgui()) {
|
||||||
ImGui_ImplGlfw_ScrollCallback(window, xoffset, yoffset);
|
ImGui_ImplGlfw_ScrollCallback(window, xoffset, yoffset);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -627,7 +626,7 @@ void App::cursor_enter_callback(GLFWwindow* window, int entered) {
|
|||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||||
ASSERT_MSG(app, "nullptr");
|
ASSERT_MSG(app, "nullptr");
|
||||||
if (io.WantCaptureMouse && app->m_window.is_mouse_enable()) {
|
if (io.WantCaptureMouse && app->m_window.is_enable_imgui()) {
|
||||||
ImGui_ImplGlfw_CursorEnterCallback(window, entered);
|
ImGui_ImplGlfw_CursorEnterCallback(window, entered);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -637,8 +636,9 @@ void App::char_callback(GLFWwindow* window, unsigned int c) {
|
|||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||||
ASSERT_MSG(app, "nullptr");
|
ASSERT_MSG(app, "nullptr");
|
||||||
if (io.WantCaptureKeyboard && app->m_window.is_mouse_enable()) {
|
if (io.WantCaptureKeyboard && app->m_window.is_enable_imgui()) {
|
||||||
ImGui_ImplGlfw_CharCallback(window, c);
|
ImGui_ImplGlfw_CharCallback(window, c);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "Cubed/config.hpp"
|
#include "Cubed/config.hpp"
|
||||||
#include "Cubed/debug_collector.hpp"
|
#include "Cubed/debug_collector.hpp"
|
||||||
#include "Cubed/gameplay/client_world.hpp"
|
#include "Cubed/gameplay/client_world.hpp"
|
||||||
|
#include "Cubed/scene/world_scene.hpp"
|
||||||
|
|
||||||
namespace {} // namespace
|
namespace {} // namespace
|
||||||
|
|
||||||
@@ -582,6 +583,12 @@ bool ClientPlayer::handle_mouse_button_event(const MouseButtonEvent& e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ClientPlayer::handle_key_event(const KeyEvent& e) {
|
bool ClientPlayer::handle_key_event(const KeyEvent& e) {
|
||||||
|
|
||||||
|
if (e.key == Key::ESCAPE && e.action == KeyAction::PRESS) {
|
||||||
|
m_world.world_scene().set_pause(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (update_player_move_state(e.key, e.action)) {
|
if (update_player_move_state(e.key, e.action)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ WorldScene::WorldScene(SceneManager& scene_manager)
|
|||||||
: m_scene_manager(scene_manager), m_dev_panel(*this),
|
: m_scene_manager(scene_manager), m_dev_panel(*this),
|
||||||
m_client_world(scene_manager.app().audio(), scene_manager.app().config(),
|
m_client_world(scene_manager.app().audio(), scene_manager.app().config(),
|
||||||
*this),
|
*this),
|
||||||
m_ui_manager(*this), m_argument(scene_manager.app().argument()) {}
|
m_pasue_menu(*this), m_ui_manager(*this),
|
||||||
|
m_argument(scene_manager.app().argument()) {}
|
||||||
|
|
||||||
WorldScene::~WorldScene() {
|
WorldScene::~WorldScene() {
|
||||||
if (m_client) {
|
if (m_client) {
|
||||||
@@ -17,45 +18,61 @@ WorldScene::~WorldScene() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WorldScene::update(float dt) {
|
void WorldScene::update(float dt) {
|
||||||
m_client_world.update(dt);
|
if (m_paused) {
|
||||||
m_camera.update_move_camera();
|
m_pasue_menu.update(dt);
|
||||||
m_client_world.get_audio().update_listener(m_camera.get_camera_pos(),
|
} else {
|
||||||
m_camera.get_camera_front(),
|
m_client_world.update(dt);
|
||||||
glm::vec3(0, 1, 0));
|
m_camera.update_move_camera();
|
||||||
|
m_client_world.get_audio().update_listener(m_camera.get_camera_pos(),
|
||||||
|
m_camera.get_camera_front(),
|
||||||
|
glm::vec3(0, 1, 0));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
const auto& player = m_client_world.get_player();
|
const auto& player = m_client_world.get_player();
|
||||||
if (player_gait != player.get_gait()) {
|
if (player_gait != player.get_gait()) {
|
||||||
player_gait = player.get_gait();
|
player_gait = player.get_gait();
|
||||||
float fov = m_client_world.get("player.fov", 70.0f);
|
float fov = m_client_world.get("player.fov", 70.0f);
|
||||||
if (player_gait == Gait::WALK) {
|
if (player_gait == Gait::WALK) {
|
||||||
m_renderer.update_fov(fov);
|
m_renderer.update_fov(fov);
|
||||||
}
|
}
|
||||||
if (player_gait == Gait::RUN) {
|
if (player_gait == Gait::RUN) {
|
||||||
m_renderer.update_fov(fov + 5.0f);
|
m_renderer.update_fov(fov + 5.0f);
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
m_ui_manager.update(dt);
|
m_ui_manager.update(dt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldScene::render(Renderer& renderer) {
|
void WorldScene::render(Renderer& renderer) {
|
||||||
|
|
||||||
renderer.render_world(m_client_world);
|
renderer.render_world(m_client_world);
|
||||||
|
|
||||||
renderer.render_dev_panel(m_dev_panel);
|
|
||||||
|
|
||||||
m_ui_manager.render(renderer);
|
m_ui_manager.render(renderer);
|
||||||
|
|
||||||
|
if (m_paused) {
|
||||||
|
m_pasue_menu.render(renderer);
|
||||||
|
} else {
|
||||||
|
renderer.render_dev_panel(m_dev_panel);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bool WorldScene::handle_event(const Event& e) {
|
bool WorldScene::handle_event(const Event& e) {
|
||||||
if (m_ui_manager.handle_event(e)) {
|
if (m_paused) {
|
||||||
return true;
|
|
||||||
}
|
if (m_pasue_menu.handle_event(e)) {
|
||||||
if (m_camera.handle_event(e)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// world event needs to be processed last
|
} else {
|
||||||
if (m_client_world.handle_event(e)) {
|
if (m_ui_manager.handle_event(e)) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
if (m_camera.handle_event(e)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// world event needs to be processed last
|
||||||
|
if (m_client_world.handle_event(e)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -76,6 +93,7 @@ void WorldScene::on_enter() {
|
|||||||
m_camera.camera_init(&m_client_world.get_player());
|
m_camera.camera_init(&m_client_world.get_player());
|
||||||
m_scene_manager.app().window().set_camera(&m_camera);
|
m_scene_manager.app().window().set_camera(&m_camera);
|
||||||
m_dev_panel.init();
|
m_dev_panel.init();
|
||||||
|
m_pasue_menu.init();
|
||||||
m_ui_manager.init();
|
m_ui_manager.init();
|
||||||
|
|
||||||
m_scene_manager.app().window().set_game_running(true);
|
m_scene_manager.app().window().set_game_running(true);
|
||||||
@@ -95,4 +113,11 @@ Camera& WorldScene::camera() { return m_camera; }
|
|||||||
SceneManager& WorldScene::scene_manager() { return m_scene_manager; }
|
SceneManager& WorldScene::scene_manager() { return m_scene_manager; }
|
||||||
ClientWorld& WorldScene::client_world() { return m_client_world; }
|
ClientWorld& WorldScene::client_world() { return m_client_world; }
|
||||||
ServerWorld& WorldScene::server_world() { return m_server.server_world(); }
|
ServerWorld& WorldScene::server_world() { return m_server.server_world(); }
|
||||||
|
bool WorldScene::pause() const { return m_paused; }
|
||||||
|
void WorldScene::set_pause(bool pause) {
|
||||||
|
m_paused = pause;
|
||||||
|
auto& window = m_scene_manager.app().window();
|
||||||
|
Logger::info("pause {}", m_paused);
|
||||||
|
window.set_game_running(!m_paused);
|
||||||
|
}
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
@@ -23,11 +23,9 @@ void Button::on_render(Renderer& renderer) {
|
|||||||
|
|
||||||
bool Button::handle_mouse_move_event(const MouseMoveEvent& e) {
|
bool Button::handle_mouse_move_event(const MouseMoveEvent& e) {
|
||||||
auto pos = m_background->pos();
|
auto pos = m_background->pos();
|
||||||
|
|
||||||
if (e.xpos >= pos.x && e.xpos <= pos.x + width() && e.ypos >= pos.y &&
|
if (e.xpos >= pos.x && e.xpos <= pos.x + width() && e.ypos >= pos.y &&
|
||||||
e.ypos <= pos.y + height()) {
|
e.ypos <= pos.y + height()) {
|
||||||
m_hovered = true;
|
m_hovered = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
m_hovered = false;
|
m_hovered = false;
|
||||||
|
|||||||
41
src/ui/pause_menu_ui_manager.cpp
Normal file
41
src/ui/pause_menu_ui_manager.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include "Cubed/app.hpp"
|
||||||
|
#include "Cubed/render/renderer.hpp"
|
||||||
|
#include "Cubed/scene/scene_manager.hpp"
|
||||||
|
#include "Cubed/scene/world_scene.hpp"
|
||||||
|
#include "Cubed/ui/button.hpp"
|
||||||
|
#include "Cubed/ui/pasue_menu_ui_manager.hpp"
|
||||||
|
#include "Cubed/ui/rect.hpp"
|
||||||
|
namespace Cubed {
|
||||||
|
PauseMenuUIManager::PauseMenuUIManager(WorldScene& scene) : m_scene(scene) {}
|
||||||
|
void PauseMenuUIManager::init() {
|
||||||
|
auto rect = std::make_unique<Rect>(nullptr);
|
||||||
|
|
||||||
|
rect->set_fill(true);
|
||||||
|
rect->set_color(Color::BLACK);
|
||||||
|
rect->set_alpha(0.5f);
|
||||||
|
rect->set_anchor(Anchor::TOP_LEFT);
|
||||||
|
auto& renderer = m_scene.scene_manager().app().renderer();
|
||||||
|
rect->set_window_size(renderer.window_width(), renderer.window_height());
|
||||||
|
auto& back_main = rect->add_child<Button>();
|
||||||
|
|
||||||
|
auto& bg = back_main.set_background<Image>();
|
||||||
|
bg.set_image("texture/ui/button001.png",
|
||||||
|
m_scene.scene_manager().app().texture_manager());
|
||||||
|
auto& fg = back_main.set_foreground<Label>();
|
||||||
|
fg.set_text("return to main menu");
|
||||||
|
fg.set_scale(0.5f);
|
||||||
|
back_main.set_scale(5.0f);
|
||||||
|
back_main.set_anchor(Anchor::CENTER);
|
||||||
|
back_main.set_clicked([this]() { m_scene.scene_manager().request_pop(); });
|
||||||
|
m_root_widget = std::move(rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PauseMenuUIManager::handle_key_event(const KeyEvent& e) {
|
||||||
|
if (e.key == Key::ESCAPE && e.action == KeyAction::PRESS) {
|
||||||
|
m_scene.set_pause(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return UIManager::handle_key_event(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Cubed
|
||||||
@@ -73,14 +73,18 @@ 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);
|
|
||||||
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();
|
if (m_game_running) {
|
||||||
return true;
|
if (m_mouse_enable) {
|
||||||
|
disable_mouse();
|
||||||
|
set_imgui_enabled(false);
|
||||||
|
} else {
|
||||||
|
enable_mouse();
|
||||||
|
set_imgui_enabled(true);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -94,9 +98,12 @@ bool Window::handle_window_resize_event(const WindowResizeEvent& e) {
|
|||||||
|
|
||||||
bool Window::handle_mouse_button_event(const MouseButtonEvent& e) {
|
bool Window::handle_mouse_button_event(const MouseButtonEvent& e) {
|
||||||
if (e.key == MouseKey::LEFT_BUTTON && e.action == KeyAction::PRESS) {
|
if (e.key == MouseKey::LEFT_BUTTON && e.action == KeyAction::PRESS) {
|
||||||
if (is_mouse_enable()) {
|
if (m_game_running) {
|
||||||
toggle_mouse_able();
|
if (m_mouse_enable) {
|
||||||
return true;
|
disable_mouse();
|
||||||
|
set_imgui_enabled(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -209,39 +216,37 @@ void Window::toggle_fullscreen() {
|
|||||||
m_config.set("window.height", windowed_height);
|
m_config.set("window.height", windowed_height);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::toggle_mouse_able() {
|
void Window::enable_mouse() {
|
||||||
if (!m_game_running) {
|
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||||
m_mouse_enable = true;
|
m_mouse_enable = true;
|
||||||
}
|
}
|
||||||
// auto& io = ImGui::GetIO();
|
void Window::disable_mouse() {
|
||||||
if (m_mouse_enable) {
|
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
// io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
|
|
||||||
// Logger::info("ImGuiConfigFlags_NoMouseCursorChange");
|
|
||||||
// ImGui::SetMouseCursor(ImGuiMouseCursor_None);
|
|
||||||
// glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
|
|
||||||
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
||||||
m_mouse_enable = false;
|
|
||||||
} else {
|
|
||||||
// io.ConfigFlags &= ~ImGuiConfigFlags_NoMouseCursorChange;
|
|
||||||
// Logger::info("Disable ImGuiConfigFlags_NoMouseCursorChange");
|
|
||||||
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
||||||
m_mouse_enable = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_camera) {
|
if (m_camera) {
|
||||||
m_camera->reset_camera();
|
m_camera->reset_camera();
|
||||||
}
|
}
|
||||||
|
m_mouse_enable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::set_camera(Camera* camera) { m_camera = camera; }
|
void Window::set_camera(Camera* camera) { m_camera = camera; }
|
||||||
Camera* Window::camera() { return m_camera; }
|
Camera* Window::camera() { return m_camera; }
|
||||||
void Window::set_game_running(bool running) {
|
void Window::set_game_running(bool running) {
|
||||||
m_game_running = running;
|
m_game_running = running;
|
||||||
m_mouse_enable = running;
|
if (running) {
|
||||||
toggle_mouse_able();
|
disable_mouse();
|
||||||
|
} else {
|
||||||
|
enable_mouse();
|
||||||
|
}
|
||||||
|
set_imgui_enabled(false);
|
||||||
|
Logger::info("Window Set Game Running {}", m_game_running);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::should_close_window() { glfwSetWindowShouldClose(m_window, true); }
|
void Window::should_close_window() { glfwSetWindowShouldClose(m_window, true); }
|
||||||
|
|
||||||
|
bool Window::is_enable_imgui() const { return m_imgui_enable; }
|
||||||
|
|
||||||
|
void Window::set_imgui_enabled(bool enable) { m_imgui_enable = enable; }
|
||||||
|
|
||||||
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