Skip to content

Commit

Permalink
Tooltips: Unify the tooltip[] and list[] description tooltip display …
Browse files Browse the repository at this point in the history
…functions (#5848)

* Tooltips: Unify the tooltip[] and list[] description tooltip display functions
  • Loading branch information
SmallJoker authored and nerzhul committed Jun 3, 2017
1 parent e91ffa0 commit 7a2b9df
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
125 changes: 62 additions & 63 deletions src/guiFormSpecMenu.cpp
Expand Up @@ -2399,37 +2399,9 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
if (!item.name.empty() && tooltip_text.empty()) if (!item.name.empty() && tooltip_text.empty())
tooltip_text = utf8_to_wide(item.name); tooltip_text = utf8_to_wide(item.name);
} }
if (tooltip_text != L"") { if (!tooltip_text.empty()) {
std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n'); showTooltip(tooltip_text, m_default_tooltip_color,
m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor); m_default_tooltip_bgcolor);
m_tooltip_element->setOverrideColor(m_default_tooltip_color);
m_tooltip_element->setVisible(true);
this->bringToFront(m_tooltip_element);
setStaticText(m_tooltip_element, tooltip_text.c_str());
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
s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
#else
s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
#endif
v2u32 screenSize = driver->getScreenSize();
int tooltip_offset_x = m_btn_height;
int tooltip_offset_y = m_btn_height;
#ifdef __ANDROID__
tooltip_offset_x *= 3;
tooltip_offset_y = 0;
if (m_pointer.X > (s32)screenSize.X / 2)
tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
#endif
s32 tooltip_x = m_pointer.X + tooltip_offset_x;
s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
if (tooltip_x + tooltip_width > (s32)screenSize.X)
tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
if (tooltip_y + tooltip_height > (s32)screenSize.Y)
tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
m_tooltip_element->setRelativePosition(core::rect<s32>(
core::position2d<s32>(tooltip_x, tooltip_y),
core::dimension2d<s32>(tooltip_width, tooltip_height)));
} }
} }
} }
Expand Down Expand Up @@ -2672,40 +2644,20 @@ void GUIFormSpecMenu::drawMenu()
} }
} }


// Find and update the current tooltip
if (id != -1 && delta >= m_tooltip_show_delay) { if (id != -1 && delta >= m_tooltip_show_delay) {
for(std::vector<FieldSpec>::iterator iter = m_fields.begin(); for (std::vector<FieldSpec>::iterator iter = m_fields.begin();
iter != m_fields.end(); ++iter) { iter != m_fields.end(); ++iter) {
if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
if (m_old_tooltip != m_tooltips[iter->fname].tooltip) { if (iter->fid != id)
m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor); continue;
m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
m_old_tooltip = m_tooltips[iter->fname].tooltip; const std::wstring &text = m_tooltips[iter->fname].tooltip;
setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str()); if (!text.empty())
std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n'); showTooltip(text, m_tooltips[iter->fname].color,
s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height; m_tooltips[iter->fname].bgcolor);
s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
int tooltip_offset_x = m_btn_height; break;
int tooltip_offset_y = m_btn_height;
#ifdef __ANDROID__
tooltip_offset_x *= 3;
tooltip_offset_y = 0;
if (m_pointer.X > (s32)screenSize.X / 2)
tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
#endif
s32 tooltip_x = m_pointer.X + tooltip_offset_x;
s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
if (tooltip_x + tooltip_width > (s32)screenSize.X)
tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
if (tooltip_y + tooltip_height > (s32)screenSize.Y)
tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
m_tooltip_element->setRelativePosition(core::rect<s32>(
core::position2d<s32>(tooltip_x, tooltip_y),
core::dimension2d<s32>(tooltip_width, tooltip_height)));
}
m_tooltip_element->setVisible(true);
this->bringToFront(m_tooltip_element);
break;
}
} }
} }
} }
Expand All @@ -2720,6 +2672,53 @@ void GUIFormSpecMenu::drawMenu()
skin->setFont(old_font); skin->setFont(old_font);
} }



void GUIFormSpecMenu::showTooltip(const std::wstring &text,
const irr::video::SColor &color, const irr::video::SColor &bgcolor)
{
m_tooltip_element->setOverrideColor(color);
m_tooltip_element->setBackgroundColor(bgcolor);
m_old_tooltip = text;
setStaticText(m_tooltip_element, text.c_str());

// 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(text, 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;
#ifdef __ANDROID__
tooltip_offset_x *= 3;
tooltip_offset_y = 0;
if (m_pointer.X > (s32)screenSize.X / 2)
tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
#endif

// Calculate and set the tooltip position
s32 tooltip_x = m_pointer.X + tooltip_offset_x;
s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
if (tooltip_x + tooltip_width > (s32)screenSize.X)
tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
if (tooltip_y + tooltip_height > (s32)screenSize.Y)
tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;

m_tooltip_element->setRelativePosition(
core::rect<s32>(
core::position2d<s32>(tooltip_x, tooltip_y),
core::dimension2d<s32>(tooltip_width, tooltip_height)
)
);

// Display the tooltip
m_tooltip_element->setVisible(true);
bringToFront(m_tooltip_element);
}

void GUIFormSpecMenu::updateSelectedItem() void GUIFormSpecMenu::updateSelectedItem()
{ {
// If the selected stack has become empty for some reason, deselect it. // If the selected stack has become empty for some reason, deselect it.
Expand Down
3 changes: 3 additions & 0 deletions src/guiFormSpecMenu.h
Expand Up @@ -517,6 +517,9 @@ class GUIFormSpecMenu : public GUIModalMenu


void tryClose(); void tryClose();


void showTooltip(const std::wstring &text, const irr::video::SColor &color,
const irr::video::SColor &bgcolor);

/** /**
* check if event is part of a double click * check if event is part of a double click
* @param event event to evaluate * @param event event to evaluate
Expand Down

0 comments on commit 7a2b9df

Please sign in to comment.