Skip to content

Commit

Permalink
Add code to set color
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 18, 2025
1 parent 93dcb5d commit 4ea12d6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 14 deletions.
6 changes: 3 additions & 3 deletions UICONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
} |
```

### buttonImage
### button_image

```
{ |
Expand All @@ -78,7 +78,7 @@
} |
```

### buttonText
### button_text

```
{ |
Expand All @@ -96,7 +96,7 @@
} |
```

### textEntry
### text_entry

```
{ |
Expand Down
2 changes: 2 additions & 0 deletions client/src/menu/gamemenu/GameMenu.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "GameMenu.hpp"
#include <memory>
#include "INetwork.hpp"
#include "Logger.hpp"
#include "UIConf.hpp"

GameMenu::GameMenu(raylib::Window & /* unused */)
Expand Down Expand Up @@ -64,6 +65,7 @@ void GameMenu::update(raylib::Window &window)
continue;
}
const auto name = message.at("name").template get<std::string>();
Logger::debug("GAME_MENU: name[" + name + "] | file[" + UIConf::toFile(name) + "]");
_uiConf = std::make_shared<UIConf>(UIConf::toFile(name));
}
}
Expand Down
28 changes: 19 additions & 9 deletions client/src/uiconf/UIConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "INetwork.hpp"
#include "Logger.hpp"
#include "PathResolver.hpp"
#include "Rectangle.hpp"
#include "httplib.h"

template <typename T>
Expand Down Expand Up @@ -89,8 +90,10 @@ std::optional<raylib::Color> json_get<raylib::Color>(const nlohmann::json &json,
return json_get<raylib::Color>(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)");
}
Expand Down Expand Up @@ -120,13 +123,13 @@ UIConf::UIConf(const std::string &file) : _json(nlohmann::json::parse(file))
if (uiType == "div") {
_page.push_back(std::make_shared<UIDiv>(elem, _assets));
_pageIndexes[id] = _page.size() - 1;
} else if (uiType == "buttonImage") {
} else if (uiType == "button_image") {
_page.push_back(std::make_shared<UIButtonImage>(elem, _assets));
_pageIndexes[id] = _page.size() - 1;
} else if (uiType == "buttonText") {
} else if (uiType == "button_text") {
_page.push_back(std::make_shared<UIButtonText>(elem));
_pageIndexes[id] = _page.size() - 1;
} else if (uiType == "textEntry") {
} else if (uiType == "text_entry") {
_page.push_back(std::make_shared<UITextEntry>(elem));
_pageIndexes[id] = _page.size() - 1;
} else {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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({
Expand All @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions client/src/uiconf/UIConf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unordered_map>
#include <vector>
#include "raylib-cpp.hpp"
#include "Rectangle.hpp"
#include "nlohmann/json.hpp"

class UIConf {
Expand Down Expand Up @@ -107,6 +108,7 @@ class UIConf {
bool _visible;
bool _clickable;
raylib::Text _textR;
raylib::Rectangle _rectR;
};

class UITextEntry : public IUI {
Expand Down
11 changes: 11 additions & 0 deletions server/assets/uiconf/catan.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
}
Expand Down
16 changes: 14 additions & 2 deletions server/src/game/games/catan/Catan.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "Catan.hpp"
#include <memory>
#include <string>
#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");
Expand All @@ -10,7 +14,7 @@ Catan::Catan() : _players(_tmpPlayers)
void Catan::init(std::unordered_map<std::string, Player> &players)
{
_players = players;
_configUi = std::make_unique<ConfigUI>("catan.json");
_configUi = std::make_unique<ConfigUI>(game_identifier);
for (const auto &[key, pLayer] : _players) {
_playersState[key] = PlayerState();
_configUi->addPeer(pLayer._peer);
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 4ea12d6

Please sign in to comment.