Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/gui/CTooltip.cpp
Show All 14 Lines | |||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "CTooltip.h" | #include "CTooltip.h" | ||||
#include "gui/CGUI.h" | #include "gui/CGUI.h" | ||||
#include "gui/CGUIString.h" | |||||
#include "gui/CGUIText.h" | #include "gui/CGUIText.h" | ||||
#include <algorithm> | #include <algorithm> | ||||
CTooltip::CTooltip(CGUI& pGUI) | CTooltip::CTooltip(CGUI& pGUI) | ||||
: IGUIObject(pGUI), IGUITextOwner(pGUI) | : IGUIObject(pGUI), | ||||
IGUITextOwner(pGUI), | |||||
m_BufferZone(), | |||||
m_Caption(), | |||||
m_Font(), | |||||
m_Sprite(), | |||||
m_Delay(), | |||||
m_TextColor(), | |||||
m_MaxWidth(), | |||||
m_Offset(), | |||||
m_Anchor(), | |||||
m_TextAlign(), | |||||
m_Independent(), | |||||
m_MousePos(), | |||||
m_UseObject(), | |||||
m_HideObject() | |||||
{ | { | ||||
// If the tooltip is an object by itself: | // If the tooltip is an object by itself: | ||||
AddSetting<float>("buffer_zone"); | RegisterSetting("buffer_zone", m_BufferZone); | ||||
AddSetting<CGUIString>("caption"); | RegisterSetting("caption", m_Caption); | ||||
AddSetting<CStrW>("font"); | RegisterSetting("font", m_Font); | ||||
AddSetting<CGUISpriteInstance>("sprite"); | RegisterSetting("sprite", m_Sprite); | ||||
AddSetting<i32>("delay"); // in milliseconds | RegisterSetting("delay", m_Delay); // in milliseconds | ||||
AddSetting<CGUIColor>("textcolor"); | RegisterSetting("textcolor", m_TextColor); | ||||
AddSetting<float>("maxwidth"); | RegisterSetting("maxwidth", m_MaxWidth); | ||||
AddSetting<CPos>("offset"); | RegisterSetting("offset", m_Offset); | ||||
AddSetting<EVAlign>("anchor"); | RegisterSetting("anchor", m_Anchor); | ||||
AddSetting<EAlign>("text_align"); | 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 | ||||
AddSetting<bool>("independent"); | RegisterSetting("independent", m_Independent); | ||||
// If the tooltip is just a reference to another object: | |||||
AddSetting<CStr>("use_object"); | |||||
AddSetting<bool>("hide_object"); | |||||
// Private settings: | // Private settings: | ||||
// This is set by GUITooltip | // This is set by GUITooltip | ||||
AddSetting<CPos>("_mousepos"); | RegisterSetting("_mousepos", m_MousePos); | ||||
// If the tooltip is just a reference to another object: | |||||
RegisterSetting("use_object", m_UseObject); | |||||
RegisterSetting("hide_object", m_HideObject); | |||||
// Defaults | // Defaults | ||||
SetSetting<i32>("delay", 500, true); | SetSetting<i32>("delay", 500, true); | ||||
SetSetting<EVAlign>("anchor", EVAlign_Bottom, true); | SetSetting<EVAlign>("anchor", EVAlign_Bottom, true); | ||||
SetSetting<EAlign>("text_align", EAlign_Left, 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); | ||||
const CGUIString& caption = GetSetting<CGUIString>("caption"); | m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, m_MaxWidth, m_BufferZone, this); | ||||
const CStrW& font = GetSetting<CStrW>("font"); | |||||
const float max_width = GetSetting<float>("maxwidth"); | |||||
const float buffer_zone = GetSetting<float>("buffer_zone"); | |||||
m_GeneratedTexts[0] = CGUIText(m_pGUI, caption, font, max_width, buffer_zone, this); | |||||
// Position the tooltip relative to the mouse: | // Position the tooltip relative to the mouse: | ||||
const CPos& mousepos = GetSetting<bool>("independent") ? | const CPos& mousepos = m_Independent ? m_pGUI.GetMousePos() : m_MousePos; | ||||
m_pGUI.GetMousePos() : | |||||
GetSetting<CPos>("_mousepos"); | |||||
const CPos& offset = GetSetting<CPos>("offset"); | |||||
float textwidth = m_GeneratedTexts[0].GetSize().cx; | float textwidth = m_GeneratedTexts[0].GetSize().cx; | ||||
float textheight = m_GeneratedTexts[0].GetSize().cy; | float textheight = m_GeneratedTexts[0].GetSize().cy; | ||||
CClientArea size; | CClientArea size; | ||||
size.pixel.left = mousepos.x + 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 (GetSetting<EVAlign>("anchor")) | switch (m_Anchor) | ||||
{ | { | ||||
case EVAlign_Top: | case EVAlign_Top: | ||||
size.pixel.top = mousepos.y + 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 + 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 + 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: | ||||
Show All 20 Lines | |||||
{ | { | ||||
IGUITextOwner::HandleMessage(Message); | IGUITextOwner::HandleMessage(Message); | ||||
} | } | ||||
void CTooltip::Draw() | void CTooltip::Draw() | ||||
{ | { | ||||
float z = 900.f; // TODO: Find a nicer way of putting the tooltip on top of everything else | float z = 900.f; // TODO: Find a nicer way of putting the tooltip on top of everything else | ||||
CGUISpriteInstance& sprite = GetSetting<CGUISpriteInstance>("sprite"); | |||||
// Normally IGUITextOwner will handle this updating but since SetupText can modify the position | // Normally IGUITextOwner will handle this updating but since SetupText can modify the position | ||||
// we need to call it now *before* we do the rest of the drawing | // we need to call it now *before* we do the rest of the drawing | ||||
if (!m_GeneratedTextsValid) | if (!m_GeneratedTextsValid) | ||||
{ | { | ||||
SetupText(); | SetupText(); | ||||
m_GeneratedTextsValid = true; | m_GeneratedTextsValid = true; | ||||
} | } | ||||
m_pGUI.DrawSprite(sprite, 0, z, m_CachedActualSize); | m_pGUI.DrawSprite(m_Sprite, 0, z, m_CachedActualSize); | ||||
const CGUIColor& color = GetSetting<CGUIColor>("textcolor"); | DrawText(0, m_TextColor, m_CachedActualSize.TopLeft(), z + 0.1f); | ||||
DrawText(0, color, m_CachedActualSize.TopLeft(), z+0.1f); | |||||
} | } |
Wildfire Games · Phabricator