ADD: Main Menu Buttons + Quit Game Event
All checks were successful
Tests / test (push) Successful in 2m33s
All checks were successful
Tests / test (push) Successful in 2m33s
This commit is contained in:
parent
363b6ca39b
commit
5923f53ce4
@ -325,6 +325,7 @@ if(BUILD_GAME)
|
|||||||
src/game/scenes/main/MainMenu.h
|
src/game/scenes/main/MainMenu.h
|
||||||
src/game/scenes/main/MainUiLayer.cpp
|
src/game/scenes/main/MainUiLayer.cpp
|
||||||
src/game/scenes/main/MainUiLayer.h
|
src/game/scenes/main/MainUiLayer.h
|
||||||
|
src/game/scenes/main/events/QuitEvent.h
|
||||||
)
|
)
|
||||||
target_compile_options(Dicewars_Siedler PRIVATE
|
target_compile_options(Dicewars_Siedler PRIVATE
|
||||||
-Wall
|
-Wall
|
||||||
|
|||||||
BIN
assets/ui/btn/background.png
Normal file
BIN
assets/ui/btn/background.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 31 KiB |
@ -5,6 +5,7 @@
|
|||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
|
|
||||||
#include "EngineTime.h"
|
#include "EngineTime.h"
|
||||||
|
#include "../../game/scenes/main/events/QuitEvent.h"
|
||||||
#include "../platform/glfw/InputManager.h"
|
#include "../platform/glfw/InputManager.h"
|
||||||
#include "inputsOutputs/stateControl/StateRegistry.h"
|
#include "inputsOutputs/stateControl/StateRegistry.h"
|
||||||
#include "../core/scenes/SceneManager.h"
|
#include "../core/scenes/SceneManager.h"
|
||||||
@ -35,6 +36,10 @@ Application::Application()
|
|||||||
keyboard = std::make_unique<Keyboard>(*window);
|
keyboard = std::make_unique<Keyboard>(*window);
|
||||||
mouse = std::make_unique<Mouse>(*window);
|
mouse = std::make_unique<Mouse>(*window);
|
||||||
stateManager = std::make_unique<StateManager>(StateRegistry::get().empty, StateRegistry::get().game);
|
stateManager = std::make_unique<StateManager>(StateRegistry::get().empty, StateRegistry::get().game);
|
||||||
|
|
||||||
|
EventBus::getInstance().subscribe<QuitEvent>([this](const QuitEvent& e) {
|
||||||
|
window->close();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::~Application()
|
Application::~Application()
|
||||||
|
|||||||
@ -31,6 +31,7 @@ public:
|
|||||||
|
|
||||||
static Window* Create(const WindowProps& props);
|
static Window* Create(const WindowProps& props);
|
||||||
virtual bool shouldClose() const = 0;
|
virtual bool shouldClose() const = 0;
|
||||||
|
virtual void close() const = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -30,7 +30,6 @@ void ClickableUiComponent::checkMouseButton(MouseButton button, Mouse &mouse) {
|
|||||||
|
|
||||||
void ClickableUiComponent::onUpdate(float dt) {
|
void ClickableUiComponent::onUpdate(float dt) {
|
||||||
if (blocked) return;
|
if (blocked) return;
|
||||||
|
|
||||||
checkMouseOver();
|
checkMouseOver();
|
||||||
if (mousedOver) {
|
if (mousedOver) {
|
||||||
checkClicks();
|
checkClicks();
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "../../core/events/EventBus.h"
|
#include "../../core/events/EventBus.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
static bool s_GLFWINITIALIZED = false;
|
static bool s_GLFWINITIALIZED = false;
|
||||||
|
|
||||||
@ -26,6 +27,11 @@ GLFWWindow::~GLFWWindow()
|
|||||||
Shutdown();
|
Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GLFWWindow::close() const {
|
||||||
|
spdlog::info("Closing window");
|
||||||
|
glfwSetWindowShouldClose(m_Window, true);
|
||||||
|
}
|
||||||
|
|
||||||
void GLFWWindow::Init(const WindowProps& props)
|
void GLFWWindow::Init(const WindowProps& props)
|
||||||
{
|
{
|
||||||
m_Data.Title = props.Title;
|
m_Data.Title = props.Title;
|
||||||
|
|||||||
@ -29,6 +29,7 @@ public:
|
|||||||
bool shouldClose() const override;
|
bool shouldClose() const override;
|
||||||
|
|
||||||
void* GetNativeWindow() const override { return m_Window; }
|
void* GetNativeWindow() const override { return m_Window; }
|
||||||
|
void close() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Init(const WindowProps& props);
|
void Init(const WindowProps& props);
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../model/GUIText.h"
|
#include "../model/GUIText.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
class UiText;
|
class UiText;
|
||||||
#include "../model/GUITexture.h"
|
#include "../model/GUITexture.h"
|
||||||
|
|||||||
@ -19,5 +19,6 @@ void MainMenu::onExit() {
|
|||||||
std::vector<AssetRequest> MainMenu::getRequiredAssets() {
|
std::vector<AssetRequest> MainMenu::getRequiredAssets() {
|
||||||
std::vector<AssetRequest> requests;
|
std::vector<AssetRequest> requests;
|
||||||
requests.emplace_back(TextureRequest("mainCover", "assets/background.png"));
|
requests.emplace_back(TextureRequest("mainCover", "assets/background.png"));
|
||||||
|
requests.emplace_back(TextureRequest("btn_background", "assets/ui/btn/background.png"));
|
||||||
return requests;
|
return requests;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
#define MAINMENU_H
|
#define MAINMENU_H
|
||||||
#include "../../../engine/core/scenes/Scene.h"
|
#include "../../../engine/core/scenes/Scene.h"
|
||||||
|
|
||||||
|
|
||||||
class MainMenu: public Scene {
|
class MainMenu: public Scene {
|
||||||
public:
|
public:
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
|
|||||||
@ -3,12 +3,59 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "MainUiLayer.h"
|
#include "MainUiLayer.h"
|
||||||
|
|
||||||
|
#include "../../../engine/core/EngineTime.h"
|
||||||
|
#include "../../../engine/core/gui/uiComponent/UiButton.h"
|
||||||
#include "../../../engine/renderer/loader/AssetManager.h"
|
#include "../../../engine/renderer/loader/AssetManager.h"
|
||||||
|
#include "events/QuitEvent.h"
|
||||||
|
|
||||||
void MainUiLayer::onAttach() {
|
void MainUiLayer::onAttach() {
|
||||||
|
AssetManager::loadUiTheme("default", "assets/ui/uiTheme.json"); //Todo: Move to AssetLoader
|
||||||
|
|
||||||
rootContainer = std::make_unique<UiContainer>();
|
rootContainer = std::make_unique<UiContainer>();
|
||||||
rootContainer->setBackgroundTexture(AssetManager::getTexture("mainCover")->getTextureID());
|
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() {
|
void MainUiLayer::onDetach() {
|
||||||
@ -23,9 +70,15 @@ void MainUiLayer::onRender() {
|
|||||||
|
|
||||||
auto guis = renderBundle.getGUITextures();
|
auto guis = renderBundle.getGUITextures();
|
||||||
guiRenderer->render(guis);
|
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() {
|
void MainUiLayer::onUpdate() {
|
||||||
Dimensions rootParent {0.0, 0.0, 1.0, 1.0f};
|
Dimensions rootParent {0.0, 0.0, 1.0, 1.0f};
|
||||||
rootContainer->uiPositioner.compute(rootParent);
|
rootContainer->uiPositioner.compute(rootParent);
|
||||||
|
|
||||||
|
rootContainer->update(EngineTime::deltaTime);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,16 +8,18 @@
|
|||||||
#include "../../../engine/renderer/GUIRenderer.h"
|
#include "../../../engine/renderer/GUIRenderer.h"
|
||||||
#include "../../../engine/renderer/loader/Loader.h"
|
#include "../../../engine/renderer/loader/Loader.h"
|
||||||
#include "../../../engine/core/gui/uiMain/UiContainer.h"
|
#include "../../../engine/core/gui/uiMain/UiContainer.h"
|
||||||
|
#include "../../../engine/renderer/TextRenderer.h"
|
||||||
|
|
||||||
class MainUiLayer: public Layer {
|
class MainUiLayer: public Layer {
|
||||||
public:
|
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 onAttach() override;
|
||||||
void onDetach() override;
|
void onDetach() override;
|
||||||
void onRender() override;
|
void onRender() override;
|
||||||
void onUpdate() override;
|
void onUpdate() override;
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<GUIRenderer> guiRenderer;
|
std::unique_ptr<GUIRenderer> guiRenderer;
|
||||||
|
std::unique_ptr<TextRenderer> textRenderer;
|
||||||
std::unique_ptr<UiContainer> rootContainer;
|
std::unique_ptr<UiContainer> rootContainer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
11
src/game/scenes/main/events/QuitEvent.h
Normal file
11
src/game/scenes/main/events/QuitEvent.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
//
|
||||||
|
// Created by sebastian on 25.04.26.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef QUITEVENT_H
|
||||||
|
#define QUITEVENT_H
|
||||||
|
struct QuitEvent {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //QUITEVENT_H
|
||||||
Loading…
Reference in New Issue
Block a user