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"
|
2019-09-20 06:11:18 -07:00
|
|
|
|
|
|
|
|
#include "gui/CGUI.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2019-08-21 03:12:33 -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");
|
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
|
|
|
AddSetting<i32>("delay"); // in milliseconds
|
2019-08-03 19:20:08 -07:00
|
|
|
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
|
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
|
|
|
SetSetting<i32>("delay", 500, true);
|
|
|
|
|
SetSetting<EVAlign>("anchor", EVAlign_Bottom, true);
|
|
|
|
|
SetSetting<EAlign>("text_align", EAlign_Left, true);
|
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
|
|
|
|
2019-08-26 05:25:07 -07:00
|
|
|
const CGUIString& caption = GetSetting<CGUIString>("caption");
|
|
|
|
|
const CStrW& font = GetSetting<CStrW>("font");
|
|
|
|
|
const float max_width = GetSetting<float>("maxwidth");
|
|
|
|
|
const float buffer_zone = GetSetting<float>("buffer_zone");
|
2006-04-23 16:14:18 -07:00
|
|
|
|
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:
|
|
|
|
|
|
2019-08-26 05:25:07 -07:00
|
|
|
const CPos& mousepos = GetSetting<bool>("independent") ?
|
2019-08-23 07:43:10 -07:00
|
|
|
m_pGUI.GetMousePos() :
|
2019-08-26 05:25:07 -07:00
|
|
|
GetSetting<CPos>("_mousepos");
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2019-08-26 05:25:07 -07:00
|
|
|
const CPos& offset = GetSetting<CPos>("offset");
|
2006-04-23 16:14:18 -07:00
|
|
|
|
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;
|
2019-08-23 07:43:10 -07:00
|
|
|
|
2019-08-26 05:25:07 -07:00
|
|
|
switch (GetSetting<EVAlign>("anchor"))
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
Move static GUI<>::SetSetting operating on IGUIObject to a member IGUIObject::SetSetting.
Remove PSERROR codes from SetSetting (let std::map throw an out_of_range
if a caller wants to Set a setting that doesn't exist without having
checked with SettingExists, equal to GetSetting from 92b6cdfeab).
That also simplifies std::function SetSettingWrap construct from
0a7d0ecdde to void IGUIObject::SettingChanged.
Don't trigger debug_warn or exceptions in GUITooltip::ShowTooltip if the
XML author specified wrong tooltip input, and dodge another
dynamic_cast.
Rename existing IGUIObject::SetSetting to
IGUIObject::SetSettingFromString and comment that it is purposed for
parsing XML files.
Remove SetSetting default value, so that authors are made aware
explicitly of the need to decide the function broadcasting a message,
refs d87057b1c0, 719f2d7967, ...
Change const bool& SkipMessage to const bool SendMessage, so that a
positive value relates to a positive action.
Clean AddSettings whitespace and integer types.
Differential Revision: https://code.wildfiregames.com/D2231
Tested on: gcc 9.1.0, clang 8.0.1, Jenkins
Comments By: Philip on IRC on 2010-07-24 on GUIUtil being ugly, in case
that one counts
This was SVN commit r22796.
2019-08-28 04:21:11 -07:00
|
|
|
SetSetting<CClientArea>("size", size, true);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
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-26 05:25:07 -07:00
|
|
|
CGUISpriteInstance& sprite = GetSetting<CGUISpriteInstance>("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-21 03:12:33 -07:00
|
|
|
m_pGUI.DrawSprite(sprite, 0, z, m_CachedActualSize);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2019-08-26 05:25:07 -07:00
|
|
|
const CGUIColor& color = GetSetting<CGUIColor>("textcolor");
|
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
|
|
|
}
|