From 61aae7bccc0c892b4a25d644ca74cea2ec37e0ab Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Sat, 30 Sep 2023 22:49:22 +0200 Subject: [PATCH] code review --- client/gui/InterfaceObjectConfigurable.cpp | 29 ++++++++ client/gui/InterfaceObjectConfigurable.h | 4 ++ client/windows/CMapOverview.cpp | 82 +++++++--------------- client/windows/CMapOverview.h | 7 +- config/widgets/mapOverview.json | 32 +++++---- 5 files changed, 77 insertions(+), 77 deletions(-) diff --git a/client/gui/InterfaceObjectConfigurable.cpp b/client/gui/InterfaceObjectConfigurable.cpp index 3a1fd45cb..24d20d3fc 100644 --- a/client/gui/InterfaceObjectConfigurable.cpp +++ b/client/gui/InterfaceObjectConfigurable.cpp @@ -56,6 +56,8 @@ InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset) REGISTER_BUILDER("layout", &InterfaceObjectConfigurable::buildLayout); REGISTER_BUILDER("comboBox", &InterfaceObjectConfigurable::buildComboBox); REGISTER_BUILDER("textInput", &InterfaceObjectConfigurable::buildTextInput); + REGISTER_BUILDER("transparentFilledRectangle", &InterfaceObjectConfigurable::buildTransparentFilledRectangle); + REGISTER_BUILDER("textBox", &InterfaceObjectConfigurable::buildTextBox); } void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f) @@ -684,6 +686,33 @@ std::shared_ptr InterfaceObjectConfigurable::buildAnimation(const return anim; } +std::shared_ptr InterfaceObjectConfigurable::buildTransparentFilledRectangle(const JsonNode & config) const +{ + logGlobal->debug("Building widget TransparentFilledRectangle"); + + auto rect = readRect(config["rect"]); + auto color = readColor(config["color"]); + if(!config["colorLine"].isNull()) + { + auto colorLine = readColor(config["colorLine"]); + return std::make_shared(rect, color, colorLine); + } + return std::make_shared(rect, color); +} + +std::shared_ptr InterfaceObjectConfigurable::buildTextBox(const JsonNode & config) const +{ + logGlobal->debug("Building widget CTextBox"); + + auto rect = readRect(config["rect"]); + auto font = readFont(config["font"]); + auto alignment = readTextAlignment(config["alignment"]); + auto color = readColor(config["color"]); + auto text = readText(config["text"]); + + return std::make_shared(text, rect, 0, font, alignment, color); +} + std::shared_ptr InterfaceObjectConfigurable::buildWidget(JsonNode config) const { assert(!config.isNull()); diff --git a/client/gui/InterfaceObjectConfigurable.h b/client/gui/InterfaceObjectConfigurable.h index 30b96bfb4..cc812299e 100644 --- a/client/gui/InterfaceObjectConfigurable.h +++ b/client/gui/InterfaceObjectConfigurable.h @@ -29,6 +29,8 @@ class CShowableAnim; class CFilledTexture; class ComboBox; class CTextInput; +class TransparentFilledRectangle; +class CTextBox; #define REGISTER_BUILDER(type, method) registerBuilder(type, std::bind(method, this, std::placeholders::_1)) @@ -105,6 +107,8 @@ protected: std::shared_ptr buildLayout(const JsonNode &); std::shared_ptr buildComboBox(const JsonNode &); std::shared_ptr buildTextInput(const JsonNode &) const; + std::shared_ptr buildTransparentFilledRectangle(const JsonNode & config) const; + std::shared_ptr buildTextBox(const JsonNode & config) const; //composite widgets std::shared_ptr buildWidget(JsonNode config) const; diff --git a/client/windows/CMapOverview.cpp b/client/windows/CMapOverview.cpp index 0f2afef00..bf4d893de 100644 --- a/client/windows/CMapOverview.cpp +++ b/client/windows/CMapOverview.cpp @@ -134,20 +134,6 @@ std::vector> CMapOverview::CMapOverviewWidget::createMin return ret; } -std::shared_ptr CMapOverview::CMapOverviewWidget::CMapOverviewWidget::buildDrawTransparentRect(const JsonNode & config) const -{ - logGlobal->debug("Building widget drawTransparentRect"); - - auto rect = readRect(config["rect"]); - auto color = readColor(config["color"]); - if(!config["colorLine"].isNull()) - { - auto colorLine = readColor(config["colorLine"]); - return std::make_shared(rect, color, colorLine); - } - return std::make_shared(rect, color); -} - std::shared_ptr CMapOverview::CMapOverviewWidget::buildDrawMinimap(const JsonNode & config) const { logGlobal->debug("Building widget drawMinimap"); @@ -158,11 +144,11 @@ std::shared_ptr CMapOverview::CMapOverviewWidget::buildDrawMinimap(con if(!renderImage) return nullptr; - ResourcePath res = ResourcePath(parent.resource.getName(), EResType::MAP); + ResourcePath res = ResourcePath(p.resource.getName(), EResType::MAP); std::unique_ptr campaignMap = nullptr; - if(parent.tabType != ESelectionScreen::newGame) + if(p.tabType != ESelectionScreen::newGame) { - CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(parent.resource.getName(), EResType::SAVEGAME)), MINIMAL_SERIALIZATION_VERSION); + CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::SAVEGAME)), MINIMAL_SERIALIZATION_VERSION); lf.checkMagicBytes(SAVEGAME_MAGIC); std::unique_ptr mapHeader = std::make_unique(); @@ -187,54 +173,34 @@ std::shared_ptr CMapOverview::CMapOverviewWidget::buildDrawMinimap(con return std::make_shared(images[id], Point(rect.x, rect.y)); } -std::shared_ptr CMapOverview::CMapOverviewWidget::buildDrawPath(const JsonNode & config) const -{ - logGlobal->debug("Building widget drawPath"); - - auto rect = readRect(config["rect"]); - auto font = readFont(config["font"]); - auto alignment = readTextAlignment(config["alignment"]); - auto color = readColor(config["color"]); - - return std::make_shared(parent.fileName, rect, 0, font, alignment, color); -} - -std::shared_ptr CMapOverview::CMapOverviewWidget::buildDrawString(const JsonNode & config) const -{ - logGlobal->debug("Building widget drawString"); - - auto font = readFont(config["font"]); - auto alignment = readTextAlignment(config["alignment"]); - auto color = readColor(config["color"]); - std::string text = ""; - if("mapname" == config["text"].String()) - text = parent.mapName; - if("date" == config["text"].String()) - { - if(parent.date.empty()) - { - std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(parent.resource.getName(), EResType::MAP))); - text = vstd::getFormattedDateTime(time); - } - else - text = parent.date; - } - auto position = readPosition(config["position"]); - return std::make_shared(position.x, position.y, font, alignment, color, text); -} - CMapOverview::CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent): - InterfaceObjectConfigurable(), parent(parent) + InterfaceObjectConfigurable(), p(parent) { - drawPlayerElements = parent.tabType == ESelectionScreen::newGame; + drawPlayerElements = p.tabType == ESelectionScreen::newGame; renderImage = settings["lobby"]["mapPreview"].Bool(); const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json")); - REGISTER_BUILDER("drawTransparentRect", &CMapOverview::CMapOverviewWidget::buildDrawTransparentRect); REGISTER_BUILDER("drawMinimap", &CMapOverview::CMapOverviewWidget::buildDrawMinimap); - REGISTER_BUILDER("drawPath", &CMapOverview::CMapOverviewWidget::buildDrawPath); - REGISTER_BUILDER("drawString", &CMapOverview::CMapOverviewWidget::buildDrawString); build(config); + + if(auto w = widget("fileName")) + { + w->setText(p.fileName); + } + if(auto w = widget("mapName")) + { + w->setText(p.mapName); + } + if(auto w = widget("date")) + { + if(p.date.empty()) + { + std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::MAP))); + w->setText(vstd::getFormattedDateTime(time)); + } + else + w->setText(p.date); + } } \ No newline at end of file diff --git a/client/windows/CMapOverview.h b/client/windows/CMapOverview.h index 7c70210d8..0628852d7 100644 --- a/client/windows/CMapOverview.h +++ b/client/windows/CMapOverview.h @@ -30,7 +30,7 @@ class CMapOverview : public CWindowObject { class CMapOverviewWidget : public InterfaceObjectConfigurable { - CMapOverview& parent; + CMapOverview& p; bool drawPlayerElements; bool renderImage; @@ -38,12 +38,9 @@ class CMapOverview : public CWindowObject std::vector> createMinimaps(ResourcePath resource, Point size) const; std::vector> createMinimaps(std::unique_ptr & map, Point size) const; - std::shared_ptr buildDrawTransparentRect(const JsonNode & config) const; std::shared_ptr buildDrawMinimap(const JsonNode & config) const; - std::shared_ptr buildDrawPath(const JsonNode & config) const; - std::shared_ptr buildDrawString(const JsonNode & config) const; public: - CMapOverviewWidget(CMapOverview& parent); + CMapOverviewWidget(CMapOverview& p); }; std::shared_ptr widget; diff --git a/config/widgets/mapOverview.json b/config/widgets/mapOverview.json index 8ce7a2b97..bed9130f5 100644 --- a/config/widgets/mapOverview.json +++ b/config/widgets/mapOverview.json @@ -8,7 +8,7 @@ "rect": {"w": 428, "h": 379} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 5, "y": 5, "w": 418, "h": 20}, "color": [0, 0, 0, 75], "colorLine": [128, 100, 75, 255] @@ -22,21 +22,22 @@ "position": {"x": 214, "y": 15} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 5, "y": 30, "w": 418, "h": 20}, "color": [0, 0, 0, 75], "colorLine": [128, 100, 75, 255] }, { - "type": "drawString", + "type": "label", + "name": "mapName", "font": "small", "alignment": "center", "color": "white", - "text": "mapname", + "text": "", "position": {"x": 214, "y": 40} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 5, "y": 55, "w": 418, "h": 20}, "color": [0, 0, 0, 75], "colorLine": [128, 100, 75, 255] @@ -50,7 +51,7 @@ "position": {"x": 214, "y": 65} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 29, "y": 79, "w": 171, "h": 171}, "color": [0, 0, 0, 255], "colorLine": [128, 100, 75, 255] @@ -61,7 +62,7 @@ "rect": {"x": 30, "y": 80, "w": 169, "h": 169} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 228, "y": 79, "w": 171, "h": 171}, "color": [0, 0, 0, 255], "colorLine": [128, 100, 75, 255] @@ -72,7 +73,7 @@ "rect": {"x": 229, "y": 80, "w": 169, "h": 169} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 5, "y": 254, "w": 418, "h": 20}, "color": [0, 0, 0, 75], "colorLine": [128, 100, 75, 255] @@ -86,21 +87,22 @@ "position": {"x": 214, "y": 264} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 5, "y": 279, "w": 418, "h": 20}, "color": [0, 0, 0, 75], "colorLine": [128, 100, 75, 255] }, { - "type": "drawString", + "type": "label", + "name": "date", "font": "small", "alignment": "center", "color": "white", - "text": "date", + "text": "", "position": {"x": 214, "y": 289} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 5, "y": 304, "w": 418, "h": 20}, "color": [0, 0, 0, 75], "colorLine": [128, 100, 75, 255] @@ -114,16 +116,18 @@ "position": {"x": 214, "y": 314} }, { - "type": "drawTransparentRect", + "type": "transparentFilledRectangle", "rect": {"x": 5, "y": 329, "w": 418, "h": 45}, "color": [0, 0, 0, 75], "colorLine": [128, 100, 75, 255] }, { - "type": "drawPath", + "type": "textBox", + "name": "fileName", "font": "small", "alignment": "center", "color": "white", + "text": "", "rect": {"x": 10, "y": 334, "w": 408, "h": 35} } ]