diff --git a/CMakeLists.txt b/CMakeLists.txt index 87b4e6e..2f9ec57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -325,6 +325,7 @@ if(BUILD_GAME) src/game/scenes/main/MainMenu.h src/game/scenes/main/MainUiLayer.cpp src/game/scenes/main/MainUiLayer.h + src/game/scenes/main/events/QuitEvent.h ) target_compile_options(Dicewars_Siedler PRIVATE -Wall diff --git a/assets/ui/btn/background.png b/assets/ui/btn/background.png new file mode 100644 index 0000000..f5032b5 Binary files /dev/null and b/assets/ui/btn/background.png differ diff --git a/src/engine/core/Application.cpp b/src/engine/core/Application.cpp index 400c097..e67e4d4 100644 --- a/src/engine/core/Application.cpp +++ b/src/engine/core/Application.cpp @@ -5,6 +5,7 @@ #include "Application.h" #include "EngineTime.h" +#include "../../game/scenes/main/events/QuitEvent.h" #include "../platform/glfw/InputManager.h" #include "inputsOutputs/stateControl/StateRegistry.h" #include "../core/scenes/SceneManager.h" @@ -35,6 +36,10 @@ Application::Application() keyboard = std::make_unique(*window); mouse = std::make_unique(*window); stateManager = std::make_unique(StateRegistry::get().empty, StateRegistry::get().game); + + EventBus::getInstance().subscribe([this](const QuitEvent& e) { + window->close(); + }); } Application::~Application() diff --git a/src/engine/core/Window.h b/src/engine/core/Window.h index 0de59a1..3f2bcb2 100644 --- a/src/engine/core/Window.h +++ b/src/engine/core/Window.h @@ -31,6 +31,7 @@ public: static Window* Create(const WindowProps& props); virtual bool shouldClose() const = 0; + virtual void close() const = 0; }; diff --git a/src/engine/core/gui/uiComponent/ClickableUiComponent.cpp b/src/engine/core/gui/uiComponent/ClickableUiComponent.cpp index 57c8833..5fd5625 100644 --- a/src/engine/core/gui/uiComponent/ClickableUiComponent.cpp +++ b/src/engine/core/gui/uiComponent/ClickableUiComponent.cpp @@ -30,7 +30,6 @@ void ClickableUiComponent::checkMouseButton(MouseButton button, Mouse &mouse) { void ClickableUiComponent::onUpdate(float dt) { if (blocked) return; - checkMouseOver(); if (mousedOver) { checkClicks(); diff --git a/src/engine/platform/glfw/GLFWWindow.cpp b/src/engine/platform/glfw/GLFWWindow.cpp index 3d100c8..5fd14b3 100644 --- a/src/engine/platform/glfw/GLFWWindow.cpp +++ b/src/engine/platform/glfw/GLFWWindow.cpp @@ -7,6 +7,7 @@ #include #include "../../core/events/EventBus.h" +#include "spdlog/spdlog.h" static bool s_GLFWINITIALIZED = false; @@ -26,6 +27,11 @@ GLFWWindow::~GLFWWindow() Shutdown(); } +void GLFWWindow::close() const { + spdlog::info("Closing window"); + glfwSetWindowShouldClose(m_Window, true); +} + void GLFWWindow::Init(const WindowProps& props) { m_Data.Title = props.Title; diff --git a/src/engine/platform/glfw/GLFWWindow.h b/src/engine/platform/glfw/GLFWWindow.h index 050d3ad..704ae01 100644 --- a/src/engine/platform/glfw/GLFWWindow.h +++ b/src/engine/platform/glfw/GLFWWindow.h @@ -29,6 +29,7 @@ public: bool shouldClose() const override; void* GetNativeWindow() const override { return m_Window; } + void close() const override; private: void Init(const WindowProps& props); diff --git a/src/engine/renderer/components/UiRenderBundle.h b/src/engine/renderer/components/UiRenderBundle.h index 123fe58..d781925 100644 --- a/src/engine/renderer/components/UiRenderBundle.h +++ b/src/engine/renderer/components/UiRenderBundle.h @@ -8,6 +8,7 @@ #include #include "../model/GUIText.h" +#include "spdlog/spdlog.h" class UiText; #include "../model/GUITexture.h" diff --git a/src/game/scenes/main/MainMenu.cpp b/src/game/scenes/main/MainMenu.cpp index 74099cd..a348a1c 100644 --- a/src/game/scenes/main/MainMenu.cpp +++ b/src/game/scenes/main/MainMenu.cpp @@ -19,5 +19,6 @@ void MainMenu::onExit() { std::vector MainMenu::getRequiredAssets() { std::vector requests; requests.emplace_back(TextureRequest("mainCover", "assets/background.png")); + requests.emplace_back(TextureRequest("btn_background", "assets/ui/btn/background.png")); return requests; } diff --git a/src/game/scenes/main/MainMenu.h b/src/game/scenes/main/MainMenu.h index 0043bba..54eb78e 100644 --- a/src/game/scenes/main/MainMenu.h +++ b/src/game/scenes/main/MainMenu.h @@ -6,7 +6,6 @@ #define MAINMENU_H #include "../../../engine/core/scenes/Scene.h" - class MainMenu: public Scene { public: void onEnter() override; diff --git a/src/game/scenes/main/MainUiLayer.cpp b/src/game/scenes/main/MainUiLayer.cpp index c2c9f40..27a2d6a 100644 --- a/src/game/scenes/main/MainUiLayer.cpp +++ b/src/game/scenes/main/MainUiLayer.cpp @@ -3,12 +3,59 @@ // #include "MainUiLayer.h" + +#include "../../../engine/core/EngineTime.h" +#include "../../../engine/core/gui/uiComponent/UiButton.h" #include "../../../engine/renderer/loader/AssetManager.h" +#include "events/QuitEvent.h" void MainUiLayer::onAttach() { + AssetManager::loadUiTheme("default", "assets/ui/uiTheme.json"); //Todo: Move to AssetLoader + rootContainer = std::make_unique(); rootContainer->setBackgroundTexture(AssetManager::getTexture("mainCover")->getTextureID()); + LayoutStyle btnStyle; + btnStyle.margin.top = SizeValue(0.3, SizeUnit::Percent); + btnStyle.margin.left = SizeValue(0.05, SizeUnit::Percent); + btnStyle.width = SizeValue(350, SizeUnit::Pixels); + btnStyle.height = SizeValue(90, SizeUnit::Pixels); + + rootContainer->addChild(std::make_unique(AssetManager::getTexture("btn_background")->getTextureID(), "Play", + *AssetManager::getUiTheme("default")->large, btnStyle)); + + LayoutStyle btnStyle2; + btnStyle2.margin.top = SizeValue(15, SizeUnit::Pixels); + btnStyle2.margin.left = SizeValue(0.05, SizeUnit::Percent); + btnStyle2.width = SizeValue(350, SizeUnit::Pixels); + btnStyle2.height = SizeValue(90, SizeUnit::Pixels); + + rootContainer->addChild(std::make_unique(AssetManager::getTexture("btn_background")->getTextureID(), "New Game", + *AssetManager::getUiTheme("default")->large, btnStyle2)); + + LayoutStyle btnStyle3; + btnStyle3.margin.top = SizeValue(15, SizeUnit::Pixels); + btnStyle3.margin.left = SizeValue(0.05, SizeUnit::Percent); + btnStyle3.width = SizeValue(350, SizeUnit::Pixels); + btnStyle3.height = SizeValue(90, SizeUnit::Pixels); + rootContainer->addChild(std::make_unique(AssetManager::getTexture("btn_background")->getTextureID(), "Settings", + *AssetManager::getUiTheme("default")->large, btnStyle3)); + + LayoutStyle btnStyle4; + btnStyle4.margin.top = SizeValue(15, SizeUnit::Pixels); + btnStyle4.margin.left = SizeValue(0.05, SizeUnit::Percent); + btnStyle4.width = SizeValue(350, SizeUnit::Pixels); + btnStyle4.height = SizeValue(90, SizeUnit::Pixels); + auto quitButton = rootContainer->addChild(std::make_unique(AssetManager::getTexture("btn_background")->getTextureID(), "Quit", + *AssetManager::getUiTheme("default")->large, btnStyle4)); + + quitButton->addMouseListener([this](const MouseEventData& eventData) { + if (eventData.isCompleteClick(MouseButton::LEFT)) { + spdlog::info("Quit Button clicked"); + EventBus::getInstance().emit(QuitEvent{}); + } + }); + } void MainUiLayer::onDetach() { @@ -23,9 +70,15 @@ void MainUiLayer::onRender() { auto guis = renderBundle.getGUITextures(); guiRenderer->render(guis); + + const auto renderTexts = renderBundle.getGUITexts(); + textRenderer->renderGuiTexts(renderTexts, static_cast(Application::getInstance().getWindow().GetWidth()), + static_cast(Application::getInstance().getWindow().GetHeight())); } void MainUiLayer::onUpdate() { Dimensions rootParent {0.0, 0.0, 1.0, 1.0f}; rootContainer->uiPositioner.compute(rootParent); + + rootContainer->update(EngineTime::deltaTime); } diff --git a/src/game/scenes/main/MainUiLayer.h b/src/game/scenes/main/MainUiLayer.h index 6c65adc..788b666 100644 --- a/src/game/scenes/main/MainUiLayer.h +++ b/src/game/scenes/main/MainUiLayer.h @@ -8,16 +8,18 @@ #include "../../../engine/renderer/GUIRenderer.h" #include "../../../engine/renderer/loader/Loader.h" #include "../../../engine/core/gui/uiMain/UiContainer.h" +#include "../../../engine/renderer/TextRenderer.h" class MainUiLayer: public Layer { public: - MainUiLayer(): guiRenderer(std::make_unique(Loader::instance())) {} + MainUiLayer(): guiRenderer(std::make_unique(Loader::instance())), textRenderer(std::make_unique()) {} void onAttach() override; void onDetach() override; void onRender() override; void onUpdate() override; private: std::unique_ptr guiRenderer; + std::unique_ptr textRenderer; std::unique_ptr rootContainer; }; diff --git a/src/game/scenes/main/events/QuitEvent.h b/src/game/scenes/main/events/QuitEvent.h new file mode 100644 index 0000000..d48ae19 --- /dev/null +++ b/src/game/scenes/main/events/QuitEvent.h @@ -0,0 +1,11 @@ +// +// Created by sebastian on 25.04.26. +// + +#ifndef QUITEVENT_H +#define QUITEVENT_H +struct QuitEvent { + +}; + +#endif //QUITEVENT_H