Skip to content

Commit

Permalink
StaticText/EnrichedString: Styling support (#9187)
Browse files Browse the repository at this point in the history
* StaticText/EnrichedString: Styling support

* Fix tooltip fg/bgcolor

* Fix default color for substr(), add unittests
  • Loading branch information
SmallJoker committed Jan 22, 2020
1 parent fab3f5f commit 1892ff3
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 203 deletions.
16 changes: 10 additions & 6 deletions games/minimal/mods/test/formspec.lua
@@ -1,3 +1,5 @@
local color = minetest.colorize

local clip_fs = [[
style_type[label;noclip=%c]
style_type[button;noclip=%c]
Expand Down Expand Up @@ -31,8 +33,8 @@ local style_fs = [[
bgcolor_pressed=purple]
button[0,0;2.5,0.8;one_btn1;Button]
style[one_btn2;border=false;textcolor=cyan]
button[0,1.05;2.5,0.8;one_btn2;Text Button]
style[one_btn2;border=false;textcolor=cyan] ]]..
"button[0,1.05;2.5,0.8;one_btn2;Text " .. color("#FF0", "Yellow") .. [[]
style[one_btn3;bgimg=bubble.png;bgimg_hovered=default_apple.png;
bgimg_pressed=heart.png]
Expand Down Expand Up @@ -144,16 +146,18 @@ local pages = {
list[current_player;main;6,8;3,2;1]
button[9,0;2.5,1;name;]
button[9,1;2.5,1;name;]
button[9,2;2.5,1;name;]
label[9,0;This is a label.\nLine\nLine\nLine\nEnd]
button[9,3;1,1;name;]
button[9,2;2.5,1;name;] ]]..
"label[9,0.5;This is a label.\nLine\nLine\nLine\nEnd]"..
[[button[9,3;1,1;name;]
vertlabel[9,4;VERT]
label[10,3;HORIZ]
tabheader[6.5,0;6,0.65;name;Tab 1,Tab 2,Tab 3,Secrets;1;false;false]
]],

"size[12,12]real_coordinates[true]" ..
"label[0.375,0.375;Styled]" ..
("label[0.375,0.375;Styled - %s %s]"):format(
color("#F00", "red text"),
color("#77FF00CC", "green text")) ..
"label[6.375,0.375;Unstyled]" ..
"box[0,0.75;12,0.1;#999]" ..
"box[6,0.85;0.1,11.15;#999]" ..
Expand Down
4 changes: 2 additions & 2 deletions src/client/gameui.cpp
Expand Up @@ -155,7 +155,7 @@ void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_

m_guitext2->setVisible(m_flags.show_debug);

setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
setStaticText(m_guitext_info, m_infotext.c_str());
m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);

static const float statustext_time_max = 1.5f;
Expand All @@ -169,7 +169,7 @@ void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_
}
}

setStaticText(m_guitext_status, translate_string(m_statustext).c_str());
setStaticText(m_guitext_status, m_statustext.c_str());
m_guitext_status->setVisible(!m_statustext.empty());

if (!m_statustext.empty()) {
Expand Down
1 change: 0 additions & 1 deletion src/gui/guiButton.cpp
Expand Up @@ -53,7 +53,6 @@ GUIButton::GUIButton(IGUIEnvironment* environment, IGUIElement* parent,
core::clamp<u32>(Colors[i].getGreen() * COLOR_PRESSED_MOD, 0, 255),
core::clamp<u32>(Colors[i].getBlue() * COLOR_PRESSED_MOD, 0, 255));
}

StaticText = gui::StaticText::add(Environment, Text.c_str(), core::rect<s32>(0,0,rectangle.getWidth(),rectangle.getHeight()), false, false, this, id);
StaticText->setTextAlignment(EGUIA_CENTER, EGUIA_CENTER);
// END PATCH
Expand Down
15 changes: 6 additions & 9 deletions src/gui/guiFormSpecMenu.cpp
Expand Up @@ -3417,19 +3417,16 @@ void GUIFormSpecMenu::drawMenu()
void GUIFormSpecMenu::showTooltip(const std::wstring &text,
const irr::video::SColor &color, const irr::video::SColor &bgcolor)
{
const std::wstring ntext = translate_string(text);
m_tooltip_element->setOverrideColor(color);
m_tooltip_element->setBackgroundColor(bgcolor);
setStaticText(m_tooltip_element, ntext.c_str());
EnrichedString ntext(text);
ntext.setDefaultColor(color);
ntext.setBackground(bgcolor);

setStaticText(m_tooltip_element, ntext);

// Tooltip size and offset
s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
#if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
#else
s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
#endif

v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
int tooltip_offset_x = m_btn_height;
int tooltip_offset_y = m_btn_height;
Expand Down

0 comments on commit 1892ff3

Please sign in to comment.