Skip to content

Commit 7a2b9df

Browse files
SmallJokernerzhul
authored andcommittedJun 3, 2017
Tooltips: Unify the tooltip[] and list[] description tooltip display functions (#5848)
* Tooltips: Unify the tooltip[] and list[] description tooltip display functions
1 parent e91ffa0 commit 7a2b9df

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed
 

‎src/guiFormSpecMenu.cpp

+62-63
Original file line numberDiff line numberDiff line change
@@ -2399,37 +2399,9 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
23992399
if (!item.name.empty() && tooltip_text.empty())
24002400
tooltip_text = utf8_to_wide(item.name);
24012401
}
2402-
if (tooltip_text != L"") {
2403-
std::vector<std::wstring> tt_rows = str_split(tooltip_text, L'\n');
2404-
m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
2405-
m_tooltip_element->setOverrideColor(m_default_tooltip_color);
2406-
m_tooltip_element->setVisible(true);
2407-
this->bringToFront(m_tooltip_element);
2408-
setStaticText(m_tooltip_element, tooltip_text.c_str());
2409-
s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2410-
#if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
2411-
s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2412-
#else
2413-
s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2414-
#endif
2415-
v2u32 screenSize = driver->getScreenSize();
2416-
int tooltip_offset_x = m_btn_height;
2417-
int tooltip_offset_y = m_btn_height;
2418-
#ifdef __ANDROID__
2419-
tooltip_offset_x *= 3;
2420-
tooltip_offset_y = 0;
2421-
if (m_pointer.X > (s32)screenSize.X / 2)
2422-
tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2423-
#endif
2424-
s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2425-
s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2426-
if (tooltip_x + tooltip_width > (s32)screenSize.X)
2427-
tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2428-
if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2429-
tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2430-
m_tooltip_element->setRelativePosition(core::rect<s32>(
2431-
core::position2d<s32>(tooltip_x, tooltip_y),
2432-
core::dimension2d<s32>(tooltip_width, tooltip_height)));
2402+
if (!tooltip_text.empty()) {
2403+
showTooltip(tooltip_text, m_default_tooltip_color,
2404+
m_default_tooltip_bgcolor);
24332405
}
24342406
}
24352407
}
@@ -2672,40 +2644,20 @@ void GUIFormSpecMenu::drawMenu()
26722644
}
26732645
}
26742646

2647+
// Find and update the current tooltip
26752648
if (id != -1 && delta >= m_tooltip_show_delay) {
2676-
for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2649+
for (std::vector<FieldSpec>::iterator iter = m_fields.begin();
26772650
iter != m_fields.end(); ++iter) {
2678-
if (iter->fid == id && m_tooltips[iter->fname].tooltip != L"") {
2679-
if (m_old_tooltip != m_tooltips[iter->fname].tooltip) {
2680-
m_tooltip_element->setBackgroundColor(m_tooltips[iter->fname].bgcolor);
2681-
m_tooltip_element->setOverrideColor(m_tooltips[iter->fname].color);
2682-
m_old_tooltip = m_tooltips[iter->fname].tooltip;
2683-
setStaticText(m_tooltip_element, m_tooltips[iter->fname].tooltip.c_str());
2684-
std::vector<std::wstring> tt_rows = str_split(m_tooltips[iter->fname].tooltip, L'\n');
2685-
s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2686-
s32 tooltip_height = m_tooltip_element->getTextHeight() * tt_rows.size() + 5;
2687-
int tooltip_offset_x = m_btn_height;
2688-
int tooltip_offset_y = m_btn_height;
2689-
#ifdef __ANDROID__
2690-
tooltip_offset_x *= 3;
2691-
tooltip_offset_y = 0;
2692-
if (m_pointer.X > (s32)screenSize.X / 2)
2693-
tooltip_offset_x = (tooltip_offset_x + tooltip_width) * -1;
2694-
#endif
2695-
s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2696-
s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2697-
if (tooltip_x + tooltip_width > (s32)screenSize.X)
2698-
tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2699-
if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2700-
tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2701-
m_tooltip_element->setRelativePosition(core::rect<s32>(
2702-
core::position2d<s32>(tooltip_x, tooltip_y),
2703-
core::dimension2d<s32>(tooltip_width, tooltip_height)));
2704-
}
2705-
m_tooltip_element->setVisible(true);
2706-
this->bringToFront(m_tooltip_element);
2707-
break;
2708-
}
2651+
2652+
if (iter->fid != id)
2653+
continue;
2654+
2655+
const std::wstring &text = m_tooltips[iter->fname].tooltip;
2656+
if (!text.empty())
2657+
showTooltip(text, m_tooltips[iter->fname].color,
2658+
m_tooltips[iter->fname].bgcolor);
2659+
2660+
break;
27092661
}
27102662
}
27112663
}
@@ -2720,6 +2672,53 @@ void GUIFormSpecMenu::drawMenu()
27202672
skin->setFont(old_font);
27212673
}
27222674

2675+
2676+
void GUIFormSpecMenu::showTooltip(const std::wstring &text,
2677+
const irr::video::SColor &color, const irr::video::SColor &bgcolor)
2678+
{
2679+
m_tooltip_element->setOverrideColor(color);
2680+
m_tooltip_element->setBackgroundColor(bgcolor);
2681+
m_old_tooltip = text;
2682+
setStaticText(m_tooltip_element, text.c_str());
2683+
2684+
// Tooltip size and offset
2685+
s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
2686+
#if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
2687+
std::vector<std::wstring> text_rows = str_split(text, L'\n');
2688+
s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
2689+
#else
2690+
s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2691+
#endif
2692+
v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
2693+
int tooltip_offset_x = m_btn_height;
2694+
int tooltip_offset_y = m_btn_height;
2695+
#ifdef __ANDROID__
2696+
tooltip_offset_x *= 3;
2697+
tooltip_offset_y = 0;
2698+
if (m_pointer.X > (s32)screenSize.X / 2)
2699+
tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
2700+
#endif
2701+
2702+
// Calculate and set the tooltip position
2703+
s32 tooltip_x = m_pointer.X + tooltip_offset_x;
2704+
s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
2705+
if (tooltip_x + tooltip_width > (s32)screenSize.X)
2706+
tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
2707+
if (tooltip_y + tooltip_height > (s32)screenSize.Y)
2708+
tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
2709+
2710+
m_tooltip_element->setRelativePosition(
2711+
core::rect<s32>(
2712+
core::position2d<s32>(tooltip_x, tooltip_y),
2713+
core::dimension2d<s32>(tooltip_width, tooltip_height)
2714+
)
2715+
);
2716+
2717+
// Display the tooltip
2718+
m_tooltip_element->setVisible(true);
2719+
bringToFront(m_tooltip_element);
2720+
}
2721+
27232722
void GUIFormSpecMenu::updateSelectedItem()
27242723
{
27252724
// If the selected stack has become empty for some reason, deselect it.

‎src/guiFormSpecMenu.h

+3
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,9 @@ class GUIFormSpecMenu : public GUIModalMenu
517517

518518
void tryClose();
519519

520+
void showTooltip(const std::wstring &text, const irr::video::SColor &color,
521+
const irr::video::SColor &bgcolor);
522+
520523
/**
521524
* check if event is part of a double click
522525
* @param event event to evaluate

0 commit comments

Comments
 (0)
Please sign in to comment.