ADD: Main Menu Buttons + Quit Game Event
All checks were successful
Tests / test (push) Successful in 2m33s

This commit is contained in:
Sebastian Böckelmann 2026-04-25 08:24:42 +02:00
parent 363b6ca39b
commit 5923f53ce4
13 changed files with 83 additions and 3 deletions

View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

View File

@ -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<Keyboard>(*window);
mouse = std::make_unique<Mouse>(*window);
stateManager = std::make_unique<StateManager>(StateRegistry::get().empty, StateRegistry::get().game);
EventBus::getInstance().subscribe<QuitEvent>([this](const QuitEvent& e) {
window->close();
});
}
Application::~Application()

View File

@ -31,6 +31,7 @@ public:
static Window* Create(const WindowProps& props);
virtual bool shouldClose() const = 0;
virtual void close() const = 0;
};

View File

@ -30,7 +30,6 @@ void ClickableUiComponent::checkMouseButton(MouseButton button, Mouse &mouse) {
void ClickableUiComponent::onUpdate(float dt) {
if (blocked) return;
checkMouseOver();
if (mousedOver) {
checkClicks();

View File

@ -7,6 +7,7 @@
#include <iostream>
#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;

View File

@ -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);

View File

@ -8,6 +8,7 @@
#include <vector>
#include "../model/GUIText.h"
#include "spdlog/spdlog.h"
class UiText;
#include "../model/GUITexture.h"

View File

@ -19,5 +19,6 @@ void MainMenu::onExit() {
std::vector<AssetRequest> MainMenu::getRequiredAssets() {
std::vector<AssetRequest> requests;
requests.emplace_back(TextureRequest("mainCover", "assets/background.png"));
requests.emplace_back(TextureRequest("btn_background", "assets/ui/btn/background.png"));
return requests;
}

View File

@ -6,7 +6,6 @@
#define MAINMENU_H
#include "../../../engine/core/scenes/Scene.h"
class MainMenu: public Scene {
public:
void onEnter() override;

View File

@ -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<UiContainer>();
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<UiButton>(std::make_unique<UiButton>(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<UiButton>(std::make_unique<UiButton>(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<UiButton>(std::make_unique<UiButton>(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<UiButton>(std::make_unique<UiButton>(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<float>(Application::getInstance().getWindow().GetWidth()),
static_cast<float>(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);
}

View File

@ -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<GUIRenderer>(Loader::instance())) {}
MainUiLayer(): guiRenderer(std::make_unique<GUIRenderer>(Loader::instance())), textRenderer(std::make_unique<TextRenderer>()) {}
void onAttach() override;
void onDetach() override;
void onRender() override;
void onUpdate() override;
private:
std::unique_ptr<GUIRenderer> guiRenderer;
std::unique_ptr<TextRenderer> textRenderer;
std::unique_ptr<UiContainer> rootContainer;
};

View File

@ -0,0 +1,11 @@
//
// Created by sebastian on 25.04.26.
//
#ifndef QUITEVENT_H
#define QUITEVENT_H
struct QuitEvent {
};
#endif //QUITEVENT_H