From 3cfb8730a87f46b3ea0f9613909dc524bfdf22e6 Mon Sep 17 00:00:00 2001 From: elexis Date: Wed, 27 Dec 2017 13:36:28 +0000 Subject: [PATCH] Add helper function to apply arbitrary GUI tags (such as font and color). This avoids the caller having to duplicate the GUI tag format over and over again (equal to 54904b1750), detects syntax errors at compile time and applies the separation-of-concerns pattern (callers only have to specify the tag value agnosticly of the tag format). Differential Revision: https://code.wildfiregames.com/D1167 Patch By: fpre / ffffffff Comments By: bb Refs e1b13dead9 This was SVN commit r20697. --- binaries/data/mods/mod/gui/common/guitags.js | 16 +++++++++++++++- binaries/data/mods/public/gui/common/tooltips.js | 16 ++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/binaries/data/mods/mod/gui/common/guitags.js b/binaries/data/mods/mod/gui/common/guitags.js index 01d7bc5b67..d211f2508e 100644 --- a/binaries/data/mods/mod/gui/common/guitags.js +++ b/binaries/data/mods/mod/gui/common/guitags.js @@ -6,4 +6,18 @@ function coloredText(text, color) return '[color="' + color + '"]' + text + '[/color]'; } -//TODO: font setter +/** + * Set GUI tags on text string. + * + * @param {string} string - String to apply tags to. + * @param {object} tags - Object containing the tags, for instance { "color": "white" } or { "font": "sans-13" }. + */ +function setStringTags(text, tags) +{ + let result = ""; + + for (let tag in tags) + result = '[' + tag + '="' + tags[tag] + '"]' + text + '[/' + tag + ']'; + + return result; +} diff --git a/binaries/data/mods/public/gui/common/tooltips.js b/binaries/data/mods/public/gui/common/tooltips.js index e21cfd199c..843e47581e 100644 --- a/binaries/data/mods/public/gui/common/tooltips.js +++ b/binaries/data/mods/public/gui/common/tooltips.js @@ -1,8 +1,8 @@ var g_TooltipTextFormats = { - "unit": ['[font="sans-10"][color="orange"]', '[/color][/font]'], - "header": ['[font="sans-bold-13"]', '[/font]'], - "body": ['[font="sans-13"]', '[/font]'], - "comma": ['[font="sans-12"]', '[/font]'] + "unit": { "font": "sans-10", "color": "orange" }, + "header": { "font": "sans-bold-13" }, + "body": { "font": "sans-13" }, + "comma": { "font": "sans-12" } }; var g_AttackTypes = { @@ -84,22 +84,22 @@ function getLocalizedResourceAmounts(resources) function bodyFont(text) { - return g_TooltipTextFormats.body[0] + text + g_TooltipTextFormats.body[1]; + return setStringTags(text, g_TooltipTextFormats.body); } function headerFont(text) { - return g_TooltipTextFormats.header[0] + text + g_TooltipTextFormats.header[1]; + return setStringTags(text, g_TooltipTextFormats.header); } function unitFont(text) { - return g_TooltipTextFormats.unit[0] + text + g_TooltipTextFormats.unit[1]; + return setStringTags(text, g_TooltipTextFormats.unit); } function commaFont(text) { - return g_TooltipTextFormats.comma[0] + text + g_TooltipTextFormats.comma[1]; + return setStringTags(text, g_TooltipTextFormats.comma); } function getSecondsString(seconds)