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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-06-03 11:38:14 -07:00
|
|
|
#include "precompiled.h"
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2004-05-28 21:06:50 -07:00
|
|
|
#include "CText.h"
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
#include "gui/CGUIScrollBarVertical.h"
|
|
|
|
|
#include "gui/GUI.h"
|
2006-06-01 19:10:27 -07:00
|
|
|
#include "lib/ogl.h"
|
2003-12-26 22:26:03 -08:00
|
|
|
|
2019-08-01 13:20:24 -07:00
|
|
|
CText::CText(CGUI* pGUI)
|
2019-08-01 16:55:10 -07:00
|
|
|
: IGUIObject(pGUI), IGUIScrollBarOwner(pGUI), IGUITextOwner(pGUI)
|
2003-12-26 22:26:03 -08:00
|
|
|
{
|
2019-08-03 19:20:08 -07:00
|
|
|
AddSetting<float>("buffer_zone");
|
|
|
|
|
AddSetting<CGUIString>("caption");
|
|
|
|
|
AddSetting<int>("cell_id");
|
|
|
|
|
AddSetting<bool>("clip");
|
|
|
|
|
AddSetting<CStrW>("font");
|
|
|
|
|
AddSetting<bool>("scrollbar");
|
|
|
|
|
AddSetting<CStr>("scrollbar_style");
|
|
|
|
|
AddSetting<bool>("scroll_bottom");
|
|
|
|
|
AddSetting<bool>("scroll_top");
|
|
|
|
|
AddSetting<CGUISpriteInstance>("sprite");
|
|
|
|
|
AddSetting<EAlign>("text_align");
|
|
|
|
|
AddSetting<EVAlign>("text_valign");
|
|
|
|
|
AddSetting<CGUIColor>("textcolor");
|
|
|
|
|
AddSetting<CGUIColor>("textcolor_disabled");
|
|
|
|
|
AddSetting<CStrW>("tooltip");
|
|
|
|
|
AddSetting<CStr>("tooltip_style");
|
2003-12-26 22:26:03 -08:00
|
|
|
|
2011-02-18 14:36:39 -08:00
|
|
|
// Private settings
|
2019-08-03 19:20:08 -07:00
|
|
|
AddSetting<CStrW>("_icon_tooltip");
|
|
|
|
|
AddSetting<CStr>("_icon_tooltip_style");
|
2010-10-29 21:02:42 -07:00
|
|
|
|
2004-05-28 21:06:50 -07:00
|
|
|
//GUI<bool>::SetSetting(this, "ghost", true);
|
|
|
|
|
GUI<bool>::SetSetting(this, "scrollbar", false);
|
2010-05-30 06:42:56 -07:00
|
|
|
GUI<bool>::SetSetting(this, "clip", true);
|
2003-12-26 22:26:03 -08:00
|
|
|
|
|
|
|
|
// Add scroll-bar
|
2019-08-01 13:20:24 -07:00
|
|
|
CGUIScrollBarVertical* bar = new CGUIScrollBarVertical(pGUI);
|
2003-12-26 22:26:03 -08:00
|
|
|
bar->SetRightAligned(true);
|
|
|
|
|
AddScrollBar(bar);
|
2004-05-28 21:06:50 -07:00
|
|
|
|
|
|
|
|
// Add text
|
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();
|
2003-12-26 22:26:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CText::~CText()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-28 21:06:50 -07:00
|
|
|
void CText::SetupText()
|
|
|
|
|
{
|
2019-08-01 13:20:24 -07:00
|
|
|
if (m_GeneratedTexts.empty())
|
2004-05-28 21:06:50 -07:00
|
|
|
return;
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
CStrW font;
|
|
|
|
|
if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
|
2004-07-31 12:52:15 -07:00
|
|
|
// Use the default if none is specified
|
2004-08-30 19:09:58 -07:00
|
|
|
// TODO Gee: (2004-08-14) Don't define standard like this. Do it with the default style.
|
2009-11-03 13:46:35 -08:00
|
|
|
font = L"default";
|
2004-07-31 12:52:15 -07:00
|
|
|
|
2004-05-28 21:06:50 -07:00
|
|
|
bool scrollbar;
|
|
|
|
|
GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
|
|
|
|
|
|
2004-09-02 22:48:47 -07:00
|
|
|
float width = m_CachedActualSize.GetWidth();
|
2004-05-28 21:06:50 -07:00
|
|
|
// remove scrollbar if applicable
|
|
|
|
|
if (scrollbar && GetScrollBar(0).GetStyle())
|
|
|
|
|
width -= GetScrollBar(0).GetStyle()->m_Width;
|
|
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
const CGUIString& caption = GUI<CGUIString>::GetSetting(this, "caption");
|
|
|
|
|
const float buffer_zone = GUI<float>::GetSetting(this, "buffer_zone");
|
2004-08-30 20:25:36 -07:00
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
m_GeneratedTexts[0] = CGUIText(m_pGUI, caption, font, width, buffer_zone, this);
|
2004-05-28 21:06:50 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
if (!scrollbar)
|
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
|
|
|
CalculateTextPosition(m_CachedActualSize, m_TextPos, m_GeneratedTexts[0]);
|
2004-12-28 04:17:04 -08:00
|
|
|
|
2004-05-28 21:06:50 -07:00
|
|
|
// Setup scrollbar
|
|
|
|
|
if (scrollbar)
|
|
|
|
|
{
|
2016-12-11 13:15:50 -08:00
|
|
|
bool scroll_top = false, scroll_bottom = false;
|
|
|
|
|
GUI<bool>::GetSetting(this, "scroll_bottom", scroll_bottom);
|
|
|
|
|
GUI<bool>::GetSetting(this, "scroll_top", scroll_top);
|
2010-07-02 14:28:48 -07:00
|
|
|
|
|
|
|
|
// If we are currently scrolled to the bottom of the text,
|
|
|
|
|
// then add more lines of text, update the scrollbar so we
|
|
|
|
|
// stick to the bottom.
|
|
|
|
|
// (Use 1.5px delta so this triggers the first time caption is set)
|
|
|
|
|
bool bottom = false;
|
2016-12-11 13:15:50 -08:00
|
|
|
if (scroll_bottom && GetScrollBar(0).GetPos() > GetScrollBar(0).GetMaxPos() - 1.5f)
|
2010-07-02 14:28:48 -07:00
|
|
|
bottom = true;
|
|
|
|
|
|
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
|
|
|
GetScrollBar(0).SetScrollRange(m_GeneratedTexts[0].GetSize().cy);
|
2010-07-02 14:28:48 -07:00
|
|
|
GetScrollBar(0).SetScrollSpace(m_CachedActualSize.GetHeight());
|
|
|
|
|
|
2010-07-03 06:12:44 -07:00
|
|
|
GetScrollBar(0).SetX(m_CachedActualSize.right);
|
|
|
|
|
GetScrollBar(0).SetY(m_CachedActualSize.top);
|
|
|
|
|
GetScrollBar(0).SetZ(GetBufferedZ());
|
|
|
|
|
GetScrollBar(0).SetLength(m_CachedActualSize.bottom - m_CachedActualSize.top);
|
|
|
|
|
|
2010-07-02 14:28:48 -07:00
|
|
|
if (bottom)
|
|
|
|
|
GetScrollBar(0).SetPos(GetScrollBar(0).GetMaxPos());
|
2016-12-11 13:15:50 -08:00
|
|
|
if (scroll_top)
|
|
|
|
|
GetScrollBar(0).SetPos(0.0f);
|
2004-05-28 21:06:50 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
void CText::HandleMessage(SGUIMessage& Message)
|
2003-12-26 22:26:03 -08:00
|
|
|
{
|
|
|
|
|
IGUIScrollBarOwner::HandleMessage(Message);
|
2005-02-04 23:25:16 -08:00
|
|
|
//IGUITextOwner::HandleMessage(Message); <== placed it after the switch instead!
|
2003-12-26 22:26:03 -08:00
|
|
|
|
|
|
|
|
switch (Message.type)
|
|
|
|
|
{
|
|
|
|
|
case GUIM_SETTINGS_UPDATED:
|
2010-07-03 06:12:44 -07:00
|
|
|
if (Message.value == "scrollbar")
|
2005-02-04 23:25:16 -08:00
|
|
|
SetupText();
|
|
|
|
|
|
2004-05-28 21:06:50 -07:00
|
|
|
// Update scrollbar
|
2010-07-03 06:12:44 -07:00
|
|
|
if (Message.value == "scrollbar_style")
|
2003-12-26 22:26:03 -08:00
|
|
|
{
|
2004-05-28 21:06:50 -07:00
|
|
|
CStr scrollbar_style;
|
|
|
|
|
GUI<CStr>::GetSetting(this, Message.value, scrollbar_style);
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
GetScrollBar(0).SetScrollBarStyle(scrollbar_style);
|
2005-04-07 02:13:10 -07:00
|
|
|
|
|
|
|
|
SetupText();
|
2003-12-26 22:26:03 -08:00
|
|
|
}
|
2004-05-28 21:06:50 -07:00
|
|
|
|
2003-12-26 22:26:03 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case GUIM_MOUSE_WHEEL_DOWN:
|
2015-08-21 10:08:41 -07:00
|
|
|
{
|
2005-10-20 08:27:39 -07:00
|
|
|
GetScrollBar(0).ScrollPlus();
|
2004-05-28 21:06:50 -07:00
|
|
|
// Since the scroll was changed, let's simulate a mouse movement
|
|
|
|
|
// to check if scrollbar now is hovered
|
2011-04-28 13:42:11 -07:00
|
|
|
SGUIMessage msg(GUIM_MOUSE_MOTION);
|
|
|
|
|
HandleMessage(msg);
|
2003-12-26 22:26:03 -08:00
|
|
|
break;
|
2015-08-21 10:08:41 -07:00
|
|
|
}
|
2003-12-26 22:26:03 -08:00
|
|
|
case GUIM_MOUSE_WHEEL_UP:
|
2015-08-21 10:08:41 -07:00
|
|
|
{
|
2005-10-20 08:27:39 -07:00
|
|
|
GetScrollBar(0).ScrollMinus();
|
2004-05-28 21:06:50 -07:00
|
|
|
// Since the scroll was changed, let's simulate a mouse movement
|
|
|
|
|
// to check if scrollbar now is hovered
|
2011-04-28 13:42:11 -07:00
|
|
|
SGUIMessage msg(GUIM_MOUSE_MOTION);
|
|
|
|
|
HandleMessage(msg);
|
2003-12-26 22:26:03 -08:00
|
|
|
break;
|
2015-08-21 10:08:41 -07:00
|
|
|
}
|
2005-02-04 23:25:16 -08:00
|
|
|
case GUIM_LOAD:
|
2015-08-21 10:08:41 -07:00
|
|
|
{
|
|
|
|
|
GetScrollBar(0).SetX(m_CachedActualSize.right);
|
|
|
|
|
GetScrollBar(0).SetY(m_CachedActualSize.top);
|
|
|
|
|
GetScrollBar(0).SetZ(GetBufferedZ());
|
|
|
|
|
GetScrollBar(0).SetLength(m_CachedActualSize.bottom - m_CachedActualSize.top);
|
2005-02-04 23:25:16 -08:00
|
|
|
|
|
|
|
|
CStr scrollbar_style;
|
|
|
|
|
GUI<CStr>::GetSetting(this, "scrollbar_style", scrollbar_style);
|
2015-08-21 10:08:41 -07:00
|
|
|
GetScrollBar(0).SetScrollBarStyle(scrollbar_style);
|
2005-02-04 23:25:16 -08:00
|
|
|
break;
|
2015-08-21 10:08:41 -07:00
|
|
|
}
|
2005-02-04 23:25:16 -08:00
|
|
|
|
2003-12-26 22:26:03 -08:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-02-04 23:25:16 -08:00
|
|
|
|
|
|
|
|
IGUITextOwner::HandleMessage(Message);
|
2003-12-26 22:26:03 -08:00
|
|
|
}
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
void CText::Draw()
|
2003-12-26 22:26:03 -08:00
|
|
|
{
|
2004-05-28 21:06:50 -07:00
|
|
|
float bz = GetBufferedZ();
|
|
|
|
|
|
2003-12-26 22:26:03 -08:00
|
|
|
// First call draw on ScrollBarOwner
|
2004-05-28 21:06:50 -07:00
|
|
|
bool scrollbar;
|
|
|
|
|
GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
|
2003-12-26 22:26:03 -08:00
|
|
|
|
2004-05-28 21:06:50 -07:00
|
|
|
if (scrollbar)
|
|
|
|
|
// Draw scrollbar
|
|
|
|
|
IGUIScrollBarOwner::Draw();
|
|
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
CGUISpriteInstance& sprite = GUI<CGUISpriteInstance>::GetSetting(this, "sprite");
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
int cell_id;
|
|
|
|
|
bool clip;
|
|
|
|
|
GUI<int>::GetSetting(this, "cell_id", cell_id);
|
|
|
|
|
GUI<bool>::GetSetting(this, "clip", clip);
|
2005-07-24 01:29:02 -07:00
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
m_pGUI->DrawSprite(sprite, cell_id, bz, m_CachedActualSize);
|
2010-05-30 06:42:56 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
float scroll = 0.f;
|
|
|
|
|
if (scrollbar)
|
|
|
|
|
scroll = GetScrollBar(0).GetPos();
|
2004-05-28 21:06:50 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
// Clipping area (we'll have to subtract the scrollbar)
|
|
|
|
|
CRect cliparea;
|
|
|
|
|
if (clip)
|
|
|
|
|
{
|
|
|
|
|
cliparea = m_CachedActualSize;
|
2004-05-28 21:06:50 -07:00
|
|
|
|
2004-12-28 04:17:04 -08:00
|
|
|
if (scrollbar)
|
2015-08-21 10:08:41 -07:00
|
|
|
{
|
|
|
|
|
// subtract scrollbar from cliparea
|
|
|
|
|
if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
|
|
|
|
|
cliparea.right <= GetScrollBar(0).GetOuterRect().right)
|
|
|
|
|
cliparea.right = GetScrollBar(0).GetOuterRect().left;
|
|
|
|
|
|
|
|
|
|
if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
|
|
|
|
|
cliparea.left < GetScrollBar(0).GetOuterRect().right)
|
|
|
|
|
cliparea.left = GetScrollBar(0).GetOuterRect().right;
|
|
|
|
|
}
|
2004-05-28 21:06:50 -07:00
|
|
|
}
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2016-12-05 09:21:51 -08:00
|
|
|
bool enabled;
|
|
|
|
|
GUI<bool>::GetSetting(this, "enabled", enabled);
|
|
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
CGUIColor color;
|
|
|
|
|
GUI<CGUIColor>::GetSetting(this, enabled ? "textcolor" : "textcolor_disabled", color);
|
2015-08-21 10:08:41 -07:00
|
|
|
|
|
|
|
|
if (scrollbar)
|
|
|
|
|
DrawText(0, color, m_CachedActualSize.TopLeft() - CPos(0.f, scroll), bz+0.1f, cliparea);
|
|
|
|
|
else
|
|
|
|
|
DrawText(0, color, m_TextPos, bz+0.1f, cliparea);
|
2003-12-26 22:26:03 -08:00
|
|
|
}
|
2010-10-29 21:02:42 -07:00
|
|
|
|
|
|
|
|
bool CText::MouseOverIcon()
|
|
|
|
|
{
|
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
|
|
|
for (const CGUIText& guitext : m_GeneratedTexts)
|
|
|
|
|
for (const CGUIText::SSpriteCall& spritecall : guitext.GetSpriteCalls())
|
2010-10-29 21:02:42 -07:00
|
|
|
{
|
2015-08-21 10:08:41 -07:00
|
|
|
// Check mouse over sprite
|
2019-08-10 05:51:27 -07:00
|
|
|
if (!spritecall.m_Area.PointInside(m_pGUI->GetMousePos() - m_CachedActualSize.TopLeft()))
|
2015-08-21 10:08:41 -07:00
|
|
|
continue;
|
2010-10-29 21:02:42 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
// If tooltip exists, set the property
|
|
|
|
|
if (!spritecall.m_Tooltip.empty())
|
2010-10-29 21:02:42 -07:00
|
|
|
{
|
2015-08-21 10:08:41 -07:00
|
|
|
SetSetting("_icon_tooltip_style", spritecall.m_TooltipStyle);
|
|
|
|
|
SetSetting("_icon_tooltip", spritecall.m_Tooltip);
|
2010-10-29 21:02:42 -07:00
|
|
|
}
|
2015-08-21 10:08:41 -07:00
|
|
|
|
|
|
|
|
return true;
|
2010-10-29 21:02:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|