feat(ui): add GameUI class for handling game UI and player actions

- Implemented `GameUI` for managing game state transitions and player interactions.
- Integrated Slint `MainWindow` for UI rendering and event handling.
This commit is contained in:
Kieran Kihn
2025-12-09 20:06:13 +08:00
parent 095a34af1c
commit 4eecc0a269
2 changed files with 86 additions and 0 deletions

55
src/ui/GameUI.cpp Normal file
View File

@@ -0,0 +1,55 @@
/**
* @file GameUI.cpp
*
* @author Yuzhe Guo
* @date 2025.12.06
*/
#include "GameUI.h"
#include "MainWindow.h"
#include <utility>
#include <slint.h>
namespace UNO::UI {
GameUI::GameUI(std::function<void(CLIENT::PlayerAction)> callback) : callback_(std::move(callback)), window_(MainWindow::create())
{
window_->on_request_connect([this](const auto &ui_addr, const auto &ui_port, const auto &ui_name) {
std::string host(ui_addr);
uint16_t port = std::stoi(std::string(ui_port));
std::string name(ui_name);
CLIENT::PlayerConnectPayload payload(name, host, port);
this->callback_({CLIENT::PlayerActionType::CONNECT, payload});
this->window_->set_is_connecting(true);
});
window_->on_request_start([this]() {
CLIENT::PlayerStartGamePayload payload;
this->callback_({CLIENT::PlayerActionType::START_GAME, payload});
this->window_->set_is_ready(true);
});
}
void GameUI::doUpdateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState)
{
if (clientGameState->getClientGameStage() == GAME::ClientGameStage::PENDING_CONNECTION) {
window_->set_active_page(PageType::ConnectPage);
}
if (clientGameState->getClientGameStage() == GAME::ClientGameStage::PRE_GAME) {
window_->set_active_page(PageType::StartPage);
}
}
void GameUI::updateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState)
{
slint::invoke_from_event_loop([this, clientGameState]() { this->doUpdateUI(clientGameState); });
}
void GameUI::run()
{
window_->run();
}
} // namespace UNO::UI

31
src/ui/GameUI.h Normal file
View File

@@ -0,0 +1,31 @@
/**
* @file GameUI.h
*
* @author Yuzhe Guo
* @date 2025.12.06
*/
#pragma once
#include "../client/PlayerAction.h"
#include "../game/GameState.h"
#include "MainWindow.h"
#include <functional>
#include <memory>
namespace UNO::UI {
class GameUI {
private:
std::function<void(CLIENT::PlayerAction)> callback_;
slint::ComponentHandle<MainWindow> window_;
void doUpdateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState);
public:
explicit GameUI(std::function<void(CLIENT::PlayerAction)> callback);
void updateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState);
void run();
};
} // namespace UNO::UI