// CTooltip: GUI object used for tooltips #include "precompiled.h" #include "CTooltip.h" #include "CGUI.h" #include CTooltip::CTooltip() { // If the tooltip is an object by itself: AddSetting(GUIST_float, "buffer_zone"); AddSetting(GUIST_CGUIString, "caption"); AddSetting(GUIST_CStr, "font"); AddSetting(GUIST_CGUISpriteInstance, "sprite"); AddSetting(GUIST_int, "delay"); AddSetting(GUIST_CColor, "textcolor"); AddSetting(GUIST_float, "maxwidth"); AddSetting(GUIST_CPos, "offset"); AddSetting(GUIST_EVAlign, "anchor"); // If the tooltip is just a reference to another object: AddSetting(GUIST_CStr, "use_object"); AddSetting(GUIST_bool, "hide_object"); // Private settings: AddSetting(GUIST_CPos, "_mousepos"); // Defaults GUI::SetSetting(this, "delay", 500); GUI::SetSetting(this, "anchor", EVAlign_Bottom); // Set up a blank piece of text, to be replaced with a more // interesting message later AddText(new SGUIText()); } CTooltip::~CTooltip() { } void CTooltip::SetupText() { if (!GetGUI()) return; debug_assert(m_GeneratedTexts.size()==1); CStr font; if (GUI::GetSetting(this, "font", font) != PS_OK || font.empty()) font = "default"; float buffer_zone = 0.f; GUI::GetSetting(this, "buffer_zone", buffer_zone); CGUIString caption; GUI::GetSetting(this, "caption", caption); float max_width = 0.f; GUI::GetSetting(this, "maxwidth", max_width); *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, max_width, buffer_zone, this); // Position the tooltip relative to the mouse: CPos mousepos, offset; EVAlign anchor; GUI::GetSetting(this, "_mousepos", mousepos); GUI::GetSetting(this, "offset", offset); GUI::GetSetting(this, "anchor", anchor); // TODO: Calculate the actual width of the wrapped text and use that. // (m_Size.cx is >max_width if the text wraps, which is not helpful) float textwidth = std::min(m_GeneratedTexts[0]->m_Size.cx, (float)max_width); float textheight = m_GeneratedTexts[0]->m_Size.cy; CClientArea size; size.pixel.left = mousepos.x + offset.x; size.pixel.right = size.pixel.left + textwidth; switch (anchor) { case EVAlign_Top: size.pixel.top = mousepos.y + offset.y; size.pixel.bottom = size.pixel.top + textheight; break; case EVAlign_Bottom: size.pixel.bottom = mousepos.y + offset.y; size.pixel.top = size.pixel.bottom - textheight; break; case EVAlign_Center: size.pixel.top = mousepos.y + offset.y - textheight/2.f; size.pixel.bottom = size.pixel.top + textwidth; break; default: debug_warn("Invalid EVAlign!"); } // Reposition the tooltip if it's falling off the screen: extern int g_xres, g_yres; float screenw = (float)g_xres, screenh = (float)g_yres; if (size.pixel.top < 0.f) size.pixel.bottom -= size.pixel.top, size.pixel.top = 0.f; else if (size.pixel.bottom > screenh) size.pixel.top -= (size.pixel.bottom-screenh), size.pixel.bottom = screenh; else if (size.pixel.left < 0.f) size.pixel.right -= size.pixel.left, size.pixel.left = 0.f; else if (size.pixel.right > screenw) size.pixel.left -= (size.pixel.right-screenw), size.pixel.right = screenw; GUI::SetSetting(this, "size", size); UpdateCachedSize(); } void CTooltip::HandleMessage(const SGUIMessage &Message) { switch (Message.type) { case GUIM_SETTINGS_UPDATED: // Don't update the text when the size changes, because the size is // changed whenever the text is updated ( => infinite recursion) if (/*Message.value == "size" ||*/ Message.value == "caption" || Message.value == "font" || Message.value == "buffer_zone") { SetupText(); } break; case GUIM_LOAD: SetupText(); break; default: break; } } void CTooltip::Draw() { float z = 900.f; // TODO: Find a nicer way of putting the tooltip on top of everything else if (GetGUI()) { CGUISpriteInstance *sprite; GUI::GetSettingPointer(this, "sprite", sprite); GetGUI()->DrawSprite(*sprite, 0, z, m_CachedActualSize); CColor color; GUI::GetSetting(this, "textcolor", color); // Draw text IGUITextOwner::Draw(0, color, m_CachedActualSize.TopLeft(), z+0.1f); } }