diff --git a/src/ui/GameUI.cpp b/src/ui/GameUI.cpp new file mode 100644 index 0000000..b5547a7 --- /dev/null +++ b/src/ui/GameUI.cpp @@ -0,0 +1,55 @@ +/** + * @file GameUI.cpp + * + * @author Yuzhe Guo + * @date 2025.12.06 + */ +#include "GameUI.h" +#include "MainWindow.h" + +#include + +#include + +namespace UNO::UI { + + GameUI::GameUI(std::function 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 &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 &clientGameState) + { + slint::invoke_from_event_loop([this, clientGameState]() { this->doUpdateUI(clientGameState); }); + } + + + void GameUI::run() + { + window_->run(); + } + +} // namespace UNO::UI \ No newline at end of file diff --git a/src/ui/GameUI.h b/src/ui/GameUI.h new file mode 100644 index 0000000..85732bf --- /dev/null +++ b/src/ui/GameUI.h @@ -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 +#include + +namespace UNO::UI { + class GameUI { + private: + std::function callback_; + slint::ComponentHandle window_; + + void doUpdateUI(const std::shared_ptr &clientGameState); + + public: + explicit GameUI(std::function callback); + + void updateUI(const std::shared_ptr &clientGameState); + + void run(); + }; + +} // namespace UNO::UI \ No newline at end of file