2019-07-26 11:57:28 -07:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "CTooltip.h"
|
|
|
|
|
#include "CGUI.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2019-08-01 13:20:24 -07:00
|
|
|
CTooltip::CTooltip(CGUI* pGUI)
|
2019-08-01 16:55:10 -07:00
|
|
|
: IGUIObject(pGUI), IGUITextOwner(pGUI)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
// If the tooltip is an object by itself:
|
2019-08-03 19:20:08 -07:00
|
|
|
AddSetting<float>("buffer_zone");
|
|
|
|
|
AddSetting<CGUIString>("caption");
|
|
|
|
|
AddSetting<CStrW>("font");
|
|
|
|
|
AddSetting<CGUISpriteInstance>("sprite");
|
|
|
|
|
AddSetting<int>("delay");
|
|
|
|
|
AddSetting<CGUIColor>("textcolor");
|
|
|
|
|
AddSetting<float>("maxwidth");
|
|
|
|
|
AddSetting<CPos>("offset");
|
|
|
|
|
AddSetting<EVAlign>("anchor");
|
|
|
|
|
AddSetting<EAlign>("text_align");
|
2012-03-08 12:42:28 -08:00
|
|
|
// This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism
|
2019-08-03 19:20:08 -07:00
|
|
|
AddSetting<bool>("independent");
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// If the tooltip is just a reference to another object:
|
2019-08-03 19:20:08 -07:00
|
|
|
AddSetting<CStr>("use_object");
|
|
|
|
|
AddSetting<bool>("hide_object");
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// Private settings:
|
2019-07-29 04:56:11 -07:00
|
|
|
// This is set by GUITooltip
|
2019-08-03 19:20:08 -07:00
|
|
|
AddSetting<CPos>("_mousepos");
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
|
GUI<int>::SetSetting(this, "delay", 500);
|
|
|
|
|
GUI<EVAlign>::SetSetting(this, "anchor", EVAlign_Bottom);
|
2013-01-05 17:46:44 -08:00
|
|
|
GUI<EAlign>::SetSetting(this, "text_align", EAlign_Left);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// Set up a blank piece of text, to be replaced with a more
|
|
|
|
|
// interesting message later
|
Move CGUI::GenerateText to CGUIText constructor, CGUI::DrawText to CGUIText::Draw, SGenerateTextImage from CGUI to CGUIText.
Makes GUI text construction 30x faster for empty strings, otherwise less
than 1% faster.
Split the constructor into smaller helper functions to reduce nesting
and improve readability.
Change m_GeneratedTexts from pointer to reference, so that one doesn't
have to keep track to delete it correctly in several places, without
having to resort to copy or move assignments but constructing in place.
Mark CGUIText as NONCOPYABLE and MOVABLE which is already implicitly the
case due to the CGUISpriteInstance members, refs 0a7d0ecdde/D2133,
D2163/3028551b91.
Differential Revision: https://code.wildfiregames.com/D2168
Prepared by the GUIText.h file split in 838889ab12 / D2167.
Comments By: Vladislav
Tested on: gcc 9, clang 8, VS2015, Jenkins
Inlining tested using: clang -Rpass=inline and gcc -Winline
This was SVN commit r22679.
2019-08-16 18:32:11 -07:00
|
|
|
AddText();
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CTooltip::~CTooltip()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CTooltip::SetupText()
|
|
|
|
|
{
|
2015-08-21 10:08:41 -07:00
|
|
|
ENSURE(m_GeneratedTexts.size() == 1);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
CStrW font;
|
|
|
|
|
if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
|
|
|
|
|
font = L"default";
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
float buffer_zone = 0.f;
|
|
|
|
|
GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
|
|
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
const CGUIString& caption = GUI<CGUIString>::GetSetting(this, "caption");
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
float max_width = 0.f;
|
|
|
|
|
GUI<float>::GetSetting(this, "maxwidth", max_width);
|
|
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
m_GeneratedTexts[0] = CGUIText(m_pGUI, caption, font, max_width, buffer_zone, this);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// Position the tooltip relative to the mouse:
|
|
|
|
|
|
|
|
|
|
CPos mousepos, offset;
|
|
|
|
|
EVAlign anchor;
|
2012-03-08 12:42:28 -08:00
|
|
|
bool independent;
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2012-03-08 12:42:28 -08:00
|
|
|
GUI<bool>::GetSetting(this, "independent", independent);
|
|
|
|
|
if (independent)
|
2019-08-10 05:51:27 -07:00
|
|
|
mousepos = m_pGUI->GetMousePos();
|
2012-03-08 12:42:28 -08:00
|
|
|
else
|
|
|
|
|
GUI<CPos>::GetSetting(this, "_mousepos", mousepos);
|
2012-05-05 12:22:22 -07:00
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
GUI<CPos>::GetSetting(this, "offset", offset);
|
|
|
|
|
GUI<EVAlign>::GetSetting(this, "anchor", anchor);
|
|
|
|
|
|
Move CGUI::GenerateText to CGUIText constructor, CGUI::DrawText to CGUIText::Draw, SGenerateTextImage from CGUI to CGUIText.
Makes GUI text construction 30x faster for empty strings, otherwise less
than 1% faster.
Split the constructor into smaller helper functions to reduce nesting
and improve readability.
Change m_GeneratedTexts from pointer to reference, so that one doesn't
have to keep track to delete it correctly in several places, without
having to resort to copy or move assignments but constructing in place.
Mark CGUIText as NONCOPYABLE and MOVABLE which is already implicitly the
case due to the CGUISpriteInstance members, refs 0a7d0ecdde/D2133,
D2163/3028551b91.
Differential Revision: https://code.wildfiregames.com/D2168
Prepared by the GUIText.h file split in 838889ab12 / D2167.
Comments By: Vladislav
Tested on: gcc 9, clang 8, VS2015, Jenkins
Inlining tested using: clang -Rpass=inline and gcc -Winline
This was SVN commit r22679.
2019-08-16 18:32:11 -07:00
|
|
|
float textwidth = m_GeneratedTexts[0].GetSize().cx;
|
|
|
|
|
float textheight = m_GeneratedTexts[0].GetSize().cy;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
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:
|
2009-11-03 13:46:35 -08:00
|
|
|
debug_warn(L"Invalid EVAlign!");
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Reposition the tooltip if it's falling off the screen:
|
|
|
|
|
|
|
|
|
|
extern int g_xres, g_yres;
|
2017-07-29 11:30:59 -07:00
|
|
|
extern float g_GuiScale;
|
|
|
|
|
float screenw = g_xres / g_GuiScale;
|
|
|
|
|
float screenh = g_yres / g_GuiScale;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
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;
|
2011-02-13 04:16:31 -08:00
|
|
|
|
|
|
|
|
if (size.pixel.left < 0.f)
|
2006-04-23 16:14:18 -07:00
|
|
|
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;
|
2014-02-19 13:59:07 -08:00
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
GUI<CClientArea>::SetSetting(this, "size", size);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
void CTooltip::HandleMessage(SGUIMessage& Message)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2010-04-19 12:43:05 -07:00
|
|
|
IGUITextOwner::HandleMessage(Message);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
void CTooltip::Draw()
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
float z = 900.f; // TODO: Find a nicer way of putting the tooltip on top of everything else
|
|
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
CGUISpriteInstance& sprite = GUI<CGUISpriteInstance>::GetSetting(this, "sprite");
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
// 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
|
|
|
|
|
if (!m_GeneratedTextsValid)
|
|
|
|
|
{
|
|
|
|
|
SetupText();
|
|
|
|
|
m_GeneratedTextsValid = true;
|
|
|
|
|
}
|
2010-04-19 12:43:05 -07:00
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
m_pGUI->DrawSprite(sprite, 0, z, m_CachedActualSize);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
CGUIColor color;
|
|
|
|
|
GUI<CGUIColor>::GetSetting(this, "textcolor", color);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
DrawText(0, color, m_CachedActualSize.TopLeft(), z+0.1f);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|