Changeset View
Changeset View
Standalone View
Standalone View
source/gui/ObjectTypes/CTooltip.cpp
Show All 22 Lines | |||||
#include "gui/SettingTypes/CGUIString.h" | #include "gui/SettingTypes/CGUIString.h" | ||||
#include "gui/CGUIText.h" | #include "gui/CGUIText.h" | ||||
#include <algorithm> | #include <algorithm> | ||||
CTooltip::CTooltip(CGUI& pGUI) | CTooltip::CTooltip(CGUI& pGUI) | ||||
: IGUIObject(pGUI), | : IGUIObject(pGUI), | ||||
IGUITextOwner(*static_cast<IGUIObject*>(this)), | IGUITextOwner(*static_cast<IGUIObject*>(this)), | ||||
m_BufferZone(), | m_BufferZone(this, "buffer_zone"), | ||||
m_Caption(), | m_Caption(this, "caption"), | ||||
m_Font(), | m_Font(this, "font"), | ||||
m_Sprite(), | m_Sprite(this, "sprite"), | ||||
m_Delay(), | m_Delay(this, "delay", 500), // in milliseconds | ||||
m_TextColor(), | m_TextColor(this, "textcolor"), | ||||
m_MaxWidth(), | m_MaxWidth(this, "maxwidth"), | ||||
m_Offset(), | m_Offset(this, "offset"), | ||||
m_Anchor(), | m_Anchor(this, "anchor", EVAlign::BOTTOM), | ||||
m_TextAlign(), | |||||
m_Independent(), | |||||
m_MousePos(), | |||||
m_UseObject(), | |||||
m_HideObject() | |||||
{ | |||||
// If the tooltip is an object by itself: | |||||
RegisterSetting("buffer_zone", m_BufferZone); | |||||
RegisterSetting("caption", m_Caption); | |||||
RegisterSetting("font", m_Font); | |||||
RegisterSetting("sprite", m_Sprite); | |||||
RegisterSetting("delay", m_Delay); // in milliseconds | |||||
RegisterSetting("textcolor", m_TextColor); | |||||
RegisterSetting("maxwidth", m_MaxWidth); | |||||
RegisterSetting("offset", m_Offset); | |||||
RegisterSetting("anchor", m_Anchor); | |||||
RegisterSetting("text_align", m_TextAlign); | |||||
// This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism | // This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism | ||||
RegisterSetting("independent", m_Independent); | m_Independent(this, "independent"), | ||||
// Private settings: | // Private settings: | ||||
// This is set by GUITooltip | // This is set by GUITooltip | ||||
RegisterSetting("_mousepos", m_MousePos); | m_MousePos(this, "_mousepos"), | ||||
// If the tooltip is just a reference to another object: | // If the tooltip is just a reference to another object: | ||||
RegisterSetting("use_object", m_UseObject); | m_UseObject(this, "use_object"), | ||||
RegisterSetting("hide_object", m_HideObject); | m_HideObject(this, "hide_object") | ||||
{ | |||||
// Defaults | |||||
SetSetting<i32>("delay", 500, true); | |||||
SetSetting<EVAlign>("anchor", EVAlign::BOTTOM, true); | |||||
SetSetting<EAlign>("text_align", EAlign::LEFT, true); | |||||
// Set up a blank piece of text, to be replaced with a more | // Set up a blank piece of text, to be replaced with a more | ||||
// interesting message later | // interesting message later | ||||
AddText(); | AddText(); | ||||
} | } | ||||
CTooltip::~CTooltip() | CTooltip::~CTooltip() | ||||
{ | { | ||||
} | } | ||||
void CTooltip::SetupText() | void CTooltip::SetupText() | ||||
{ | { | ||||
ENSURE(m_GeneratedTexts.size() == 1); | ENSURE(m_GeneratedTexts.size() == 1); | ||||
m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, m_MaxWidth, m_BufferZone, this); | m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, m_MaxWidth, m_BufferZone, m_TextAlign, this); | ||||
// Position the tooltip relative to the mouse: | // Position the tooltip relative to the mouse: | ||||
const CVector2D& mousepos = m_Independent ? m_pGUI.GetMousePos() : m_MousePos; | const CVector2D& mousepos = m_Independent ? m_pGUI.GetMousePos() : m_MousePos; | ||||
float textwidth = m_GeneratedTexts[0].GetSize().Width; | float textwidth = m_GeneratedTexts[0].GetSize().Width; | ||||
float textheight = m_GeneratedTexts[0].GetSize().Height; | float textheight = m_GeneratedTexts[0].GetSize().Height; | ||||
CGUISize size; | CGUISize size; | ||||
size.pixel.left = mousepos.X + m_Offset.X; | size.pixel.left = mousepos.X + m_Offset->X; | ||||
size.pixel.right = size.pixel.left + textwidth; | size.pixel.right = size.pixel.left + textwidth; | ||||
switch (m_Anchor) | switch (m_Anchor) | ||||
{ | { | ||||
case EVAlign::TOP: | case EVAlign::TOP: | ||||
size.pixel.top = mousepos.Y + m_Offset.Y; | size.pixel.top = mousepos.Y + m_Offset->Y; | ||||
size.pixel.bottom = size.pixel.top + textheight; | size.pixel.bottom = size.pixel.top + textheight; | ||||
break; | break; | ||||
case EVAlign::BOTTOM: | case EVAlign::BOTTOM: | ||||
size.pixel.bottom = mousepos.Y + m_Offset.Y; | size.pixel.bottom = mousepos.Y + m_Offset->Y; | ||||
size.pixel.top = size.pixel.bottom - textheight; | size.pixel.top = size.pixel.bottom - textheight; | ||||
break; | break; | ||||
case EVAlign::CENTER: | case EVAlign::CENTER: | ||||
size.pixel.top = mousepos.Y + m_Offset.Y - textheight/2.f; | size.pixel.top = mousepos.Y + m_Offset->Y - textheight/2.f; | ||||
size.pixel.bottom = size.pixel.top + textwidth; | size.pixel.bottom = size.pixel.top + textwidth; | ||||
break; | break; | ||||
default: | default: | ||||
debug_warn(L"Invalid EVAlign!"); | debug_warn(L"Invalid EVAlign!"); | ||||
} | } | ||||
// Reposition the tooltip if it's falling off the screen: | // Reposition the tooltip if it's falling off the screen: | ||||
extern int g_xres, g_yres; | extern int g_xres, g_yres; | ||||
extern float g_GuiScale; | extern float g_GuiScale; | ||||
float screenw = g_xres / g_GuiScale; | float screenw = g_xres / g_GuiScale; | ||||
float screenh = g_yres / g_GuiScale; | float screenh = g_yres / g_GuiScale; | ||||
if (size.pixel.top < 0.f) | if (size.pixel.top < 0.f) | ||||
size.pixel.bottom -= size.pixel.top, size.pixel.top = 0.f; | size.pixel.bottom -= size.pixel.top, size.pixel.top = 0.f; | ||||
else if (size.pixel.bottom > screenh) | else if (size.pixel.bottom > screenh) | ||||
size.pixel.top -= (size.pixel.bottom-screenh), size.pixel.bottom = screenh; | size.pixel.top -= (size.pixel.bottom-screenh), size.pixel.bottom = screenh; | ||||
if (size.pixel.left < 0.f) | if (size.pixel.left < 0.f) | ||||
size.pixel.right -= size.pixel.left, size.pixel.left = 0.f; | size.pixel.right -= size.pixel.left, size.pixel.left = 0.f; | ||||
else if (size.pixel.right > screenw) | else if (size.pixel.right > screenw) | ||||
size.pixel.left -= (size.pixel.right-screenw), size.pixel.right = screenw; | size.pixel.left -= (size.pixel.right-screenw), size.pixel.right = screenw; | ||||
SetSetting<CGUISize>("size", size, true); | m_Size.Set(size, true); | ||||
} | } | ||||
void CTooltip::UpdateCachedSize() | void CTooltip::UpdateCachedSize() | ||||
{ | { | ||||
IGUIObject::UpdateCachedSize(); | IGUIObject::UpdateCachedSize(); | ||||
IGUITextOwner::UpdateCachedSize(); | IGUITextOwner::UpdateCachedSize(); | ||||
} | } | ||||
Show All 26 Lines |
Wildfire Games · Phabricator