diff --git a/UICONFIG.md b/UICONFIG.md index 0ff4b5a..01263c8 100644 --- a/UICONFIG.md +++ b/UICONFIG.md @@ -62,7 +62,7 @@ } | ``` -### buttonImage +### button_image ``` { | @@ -78,7 +78,7 @@ } | ``` -### buttonText +### button_text ``` { | @@ -96,7 +96,7 @@ } | ``` -### textEntry +### text_entry ``` { | diff --git a/client/src/menu/gamemenu/GameMenu.cpp b/client/src/menu/gamemenu/GameMenu.cpp index 644466e..7db2303 100644 --- a/client/src/menu/gamemenu/GameMenu.cpp +++ b/client/src/menu/gamemenu/GameMenu.cpp @@ -1,6 +1,7 @@ #include "GameMenu.hpp" #include #include "INetwork.hpp" +#include "Logger.hpp" #include "UIConf.hpp" GameMenu::GameMenu(raylib::Window & /* unused */) @@ -64,6 +65,7 @@ void GameMenu::update(raylib::Window &window) continue; } const auto name = message.at("name").template get(); + Logger::debug("GAME_MENU: name[" + name + "] | file[" + UIConf::toFile(name) + "]"); _uiConf = std::make_shared(UIConf::toFile(name)); } } diff --git a/client/src/uiconf/UIConf.cpp b/client/src/uiconf/UIConf.cpp index 13c6904..e03fab6 100644 --- a/client/src/uiconf/UIConf.cpp +++ b/client/src/uiconf/UIConf.cpp @@ -8,6 +8,7 @@ #include "INetwork.hpp" #include "Logger.hpp" #include "PathResolver.hpp" +#include "Rectangle.hpp" #include "httplib.h" template @@ -89,8 +90,10 @@ std::optional json_get(const nlohmann::json &json, return json_get(json.at(key)); } -UIConf::UIConf(const std::string &file) : _json(nlohmann::json::parse(file)) +UIConf::UIConf(const std::string &file) { + std::ifstream f(file); + _json = nlohmann::json::parse(f); if (!_json.contains("name") || !_json.at("name").is_string()) { throw std::invalid_argument("`name` not in json or bad format (string expected)"); } @@ -120,13 +123,13 @@ UIConf::UIConf(const std::string &file) : _json(nlohmann::json::parse(file)) if (uiType == "div") { _page.push_back(std::make_shared(elem, _assets)); _pageIndexes[id] = _page.size() - 1; - } else if (uiType == "buttonImage") { + } else if (uiType == "button_image") { _page.push_back(std::make_shared(elem, _assets)); _pageIndexes[id] = _page.size() - 1; - } else if (uiType == "buttonText") { + } else if (uiType == "button_text") { _page.push_back(std::make_shared(elem)); _pageIndexes[id] = _page.size() - 1; - } else if (uiType == "textEntry") { + } else if (uiType == "text_entry") { _page.push_back(std::make_shared(elem)); _pageIndexes[id] = _page.size() - 1; } else { @@ -556,9 +559,17 @@ bool UIConf::UIButtonText::modify( void UIConf::UIButtonText::update(raylib::Window &window, float parentX, float parentY) { - if (_text != _textR.text) { + if (_text != _textR.text) _textR.text = _text; - } + if (_fgColor != raylib::Color(_textR.GetColor())) + _textR.SetColor(_fgColor); + const auto textSize = _textR.MeasureEx(); + if (_rectR.width != textSize.x) + _rectR.SetWidth(textSize.x); + if (_rectR.height != textSize.y) + _rectR.SetHeight(textSize.y); + _rectR.SetX(parentX + _topLeftX); + _rectR.SetY(parentY + _topLeftY); if (!window.IsFocused()) { return; } @@ -569,9 +580,7 @@ void UIConf::UIButtonText::update(raylib::Window &window, float parentX, float p return; } const auto mouse = raylib::Mouse::GetPosition(); - const auto textSize = _textR.MeasureEx(); - const auto rect = raylib::Rectangle(parentX + _topLeftX, parentY + _topLeftY, textSize.x, textSize.y); - if (!rect.CheckCollision(mouse)) { + if (!_rectR.CheckCollision(mouse)) { return; } network.send({ @@ -585,6 +594,7 @@ void UIConf::UIButtonText::draw(raylib::Window & /* unused */, float parentX, fl if (!_visible) { return; } + _rectR.Draw(_bgColor); _textR.Draw(parentX + _topLeftX, parentY + _topLeftY); } diff --git a/client/src/uiconf/UIConf.hpp b/client/src/uiconf/UIConf.hpp index 104fcde..b5871ea 100644 --- a/client/src/uiconf/UIConf.hpp +++ b/client/src/uiconf/UIConf.hpp @@ -6,6 +6,7 @@ #include #include #include "raylib-cpp.hpp" +#include "Rectangle.hpp" #include "nlohmann/json.hpp" class UIConf { @@ -107,6 +108,7 @@ class UIConf { bool _visible; bool _clickable; raylib::Text _textR; + raylib::Rectangle _rectR; }; class UITextEntry : public IUI { diff --git a/server/assets/uiconf/catan.json b/server/assets/uiconf/catan.json index c2ec3f9..b87bbfa 100644 --- a/server/assets/uiconf/catan.json +++ b/server/assets/uiconf/catan.json @@ -3,6 +3,17 @@ "assets": { }, "page": [ + { + "id": "title", + "type": "button_text", + "topLeftX": 10.0, + "topLeftY": 10.0, + "bgColor": [0, 0, 0, 0], + "fgColor": [124, 252, 0, 0], + "text": "Catan", + "visible": true, + "clickable": false + } ], "popups": { } diff --git a/server/src/game/games/catan/Catan.cpp b/server/src/game/games/catan/Catan.cpp index ac5c373..5852e25 100644 --- a/server/src/game/games/catan/Catan.cpp +++ b/server/src/game/games/catan/Catan.cpp @@ -1,7 +1,11 @@ -#include "Catan.hpp" #include +#include +#include "Catan.hpp" +#include "INetwork.hpp" #include "Logger.hpp" +static const std::string game_identifier = "catan.json"; + Catan::Catan() : _players(_tmpPlayers) { Logger::debug("Catan game created"); @@ -10,7 +14,7 @@ Catan::Catan() : _players(_tmpPlayers) void Catan::init(std::unordered_map &players) { _players = players; - _configUi = std::make_unique("catan.json"); + _configUi = std::make_unique(game_identifier); for (const auto &[key, pLayer] : _players) { _playersState[key] = PlayerState(); _configUi->addPeer(pLayer._peer); @@ -22,6 +26,14 @@ void Catan::update() if (!_allConfigOk && _configUi) { _configUi->update(); _allConfigOk = _configUi->isPeersOk(); + if (_allConfigOk) { + for (const auto &[_, pLayer] : _players) { + network.send(pLayer._peer, { + {"type", "setConfig"}, + {"name", game_identifier} + }); + } + } return; } }