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.
This commit is contained in:
elexis 2017-12-27 13:36:28 +00:00
parent e4cc4da1d2
commit 3cfb8730a8
2 changed files with 23 additions and 9 deletions

View file

@ -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;
}

View file

@ -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)