From daecca4abe0bd0a73e16c39eedfc9f0f2bd0c4ba Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Mon, 20 Jan 2025 09:56:24 -0600 Subject: [PATCH] dialogxml parseColor use a map built from Colours constants parseColor() was doing a big if-else with string comparisons, and returning values that differed from our Colours constants. Since I couldn't find an instance of the colour attribute in our existing xml, it should be safe to do away with those conflicting values and refactor parseColor() to match the constants and use cleaner code. --- src/dialogxml/dialogs/dialog.cpp | 39 +++++--------------------------- src/gfx/render_shapes.cpp | 21 +++++++++++++++++ src/gfx/render_shapes.hpp | 1 + 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/dialogxml/dialogs/dialog.cpp b/src/dialogxml/dialogs/dialog.cpp index 755ccf3f..1979c327 100644 --- a/src/dialogxml/dialogs/dialog.cpp +++ b/src/dialogxml/dialogs/dialog.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "dialog.hpp" #include "gfx/tiling.hpp" // for bg #include "fileio/resmgr/res_dialog.hpp" @@ -45,6 +46,8 @@ cDialog* cDialog::topWindow = nullptr; void (*cDialog::redraw_everything)() = nullptr; std::mt19937 cDialog::ui_rand; +extern std::map colour_map; + extern bool check_for_interrupt(std::string); std::string cDialog::generateRandomString(){ @@ -86,39 +89,9 @@ sf::Color cControl::parseColor(string what){ } } clr.r = r, clr.g = g, clr.b = b; - }else if(what == "black") - clr.r = 0x00, clr.g = 0x00, clr.b = 0x00; - else if(what == "red") - clr.r = 0xFF, clr.g = 0x00, clr.b = 0x00; - else if(what == "lime") - clr.r = 0x00, clr.g = 0xFF, clr.b = 0x00; - else if(what == "blue") - clr.r = 0x00, clr.g = 0x00, clr.b = 0xFF; - else if(what == "yellow") - clr.r = 0xFF, clr.g = 0xFF, clr.b = 0x00; - else if(what == "aqua") - clr.r = 0x00, clr.g = 0xFF, clr.b = 0xFF; - else if(what == "fuchsia") - clr.r = 0xFF, clr.g = 0x00, clr.b = 0xFF; - else if(what == "white") - clr.r = 0xFF, clr.g = 0xFF, clr.b = 0xFF; - else if(what == "gray" || what == "grey") - clr.r = 0x80, clr.g = 0x80, clr.b = 0x80; - else if(what == "maroon") - clr.r = 0x80, clr.g = 0x00, clr.b = 0x00; - else if(what == "green") - clr.r = 0x00, clr.g = 0x80, clr.b = 0x00; - else if(what == "navy") - clr.r = 0x00, clr.g = 0x00, clr.b = 0x80; - else if(what == "olive") - clr.r = 0x80, clr.g = 0x80, clr.b = 0x00; - else if(what == "teal") - clr.r = 0x00, clr.g = 0x80, clr.b = 0x80; - else if(what == "purple") - clr.r = 0x80, clr.g = 0x00, clr.b = 0x80; - else if(what == "silver") - clr.r = 0xC0, clr.g = 0xC0, clr.b = 0xC0; - else throw -1; + }else if(colour_map.find(what) != colour_map.end()){ + return colour_map[what]; + }else throw -1; return clr; } diff --git a/src/gfx/render_shapes.cpp b/src/gfx/render_shapes.cpp index e284547b..47c41360 100644 --- a/src/gfx/render_shapes.cpp +++ b/src/gfx/render_shapes.cpp @@ -17,6 +17,27 @@ using boost::math::constants::pi; using pt_idx_t = decltype(((sf::Shape*)nullptr)->getPointCount()); +std::map colour_map = { + {"white", Colours::WHITE}, + {"black", Colours::BLACK}, + {"grey", Colours::GREY}, + {"gray", Colours::GREY}, + {"red", Colours::RED}, + {"green", Colours::GREEN}, + {"blue", Colours::BLUE}, + {"teal", Colours::TEAL}, + {"pink", Colours::PINK}, + {"yellow", Colours::YELLOW}, + {"orange", Colours::ORANGE}, + {"shadow", Colours::SHADOW}, + {"title_blue", Colours::TITLE_BLUE}, + {"navy", Colours::NAVY}, + {"dark_blue", Colours::DARK_BLUE}, + {"dark_green", Colours::DARK_GREEN}, + {"light_green", Colours::LIGHT_GREEN}, + {"dark_red", Colours::DARK_RED} +}; + // TODO: Put these classes in a header? class EllipseShape : public sf::Shape { float divSz; diff --git a/src/gfx/render_shapes.hpp b/src/gfx/render_shapes.hpp index e043574b..90e98911 100644 --- a/src/gfx/render_shapes.hpp +++ b/src/gfx/render_shapes.hpp @@ -11,6 +11,7 @@ #include #include +#include #include #include "location.hpp"