2026-03-05 03:31:47 -08:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2004-06-03 11:38:14 -07:00
|
|
|
#include "precompiled.h"
|
2004-05-28 21:06:50 -07:00
|
|
|
|
2019-08-02 09:55:15 -07:00
|
|
|
#include "IGUITextOwner.h"
|
|
|
|
|
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "gui/CGUIText.h"
|
2020-12-09 06:39:14 -08:00
|
|
|
#include "gui/ObjectBases/IGUIObject.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "gui/SGUIMessage.h"
|
|
|
|
|
#include "gui/SettingTypes/EAlign.h"
|
|
|
|
|
#include "lib/debug.h"
|
|
|
|
|
#include "maths/Size2D.h"
|
2021-03-28 14:55:13 -07:00
|
|
|
#include "maths/Vector2D.h"
|
2025-06-19 15:06:41 -07:00
|
|
|
#include "ps/CStr.h"
|
2004-05-28 21:06:50 -07:00
|
|
|
|
2025-06-19 15:06:41 -07:00
|
|
|
#include <string>
|
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
|
|
|
|
2019-09-30 07:08:14 -07:00
|
|
|
IGUITextOwner::IGUITextOwner(IGUIObject& pObject)
|
2021-05-06 01:22:37 -07:00
|
|
|
: m_pObject(pObject),
|
|
|
|
|
m_GeneratedTextsValid(),
|
|
|
|
|
m_TextAlign(&pObject, "text_align", EAlign::LEFT),
|
|
|
|
|
m_TextVAlign(&pObject, "text_valign", EVAlign::TOP)
|
2004-05-28 21:06:50 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IGUITextOwner::~IGUITextOwner()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
CGUIText& IGUITextOwner::AddText()
|
|
|
|
|
{
|
|
|
|
|
m_GeneratedTexts.emplace_back();
|
|
|
|
|
return m_GeneratedTexts.back();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-28 04:46:06 -07:00
|
|
|
CGUIText& IGUITextOwner::AddText(const CGUIString& Text, const CStrW& Font, const float& Width, const float& BufferZone)
|
2004-05-28 21:06:50 -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
|
|
|
// Avoids a move constructor
|
2021-05-06 01:22:37 -07:00
|
|
|
m_GeneratedTexts.emplace_back(m_pObject.GetGUI(), Text, Font, Width, BufferZone, m_TextAlign, &m_pObject);
|
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
|
|
|
return m_GeneratedTexts.back();
|
2004-05-28 21:06:50 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
void IGUITextOwner::HandleMessage(SGUIMessage& Message)
|
2004-05-28 21:06:50 -07:00
|
|
|
{
|
|
|
|
|
switch (Message.type)
|
|
|
|
|
{
|
|
|
|
|
case GUIM_SETTINGS_UPDATED:
|
|
|
|
|
// Everything that can change the visual appearance.
|
|
|
|
|
// it is assumed that the text of the object will be dependent on
|
2004-08-30 19:09:58 -07:00
|
|
|
// these. Although that is not certain, but one will have to manually
|
2004-05-28 21:06:50 -07:00
|
|
|
// change it and disregard this function.
|
2021-05-01 05:38:05 -07:00
|
|
|
if (Message.value == "size" || Message.value == "tooltip" ||
|
2004-12-21 05:37:24 -08:00
|
|
|
Message.value == "absolute" || Message.value == "caption" ||
|
|
|
|
|
Message.value == "font" || Message.value == "textcolor" ||
|
2019-07-26 06:45:14 -07:00
|
|
|
Message.value == "text_align" || Message.value == "text_valign" ||
|
2005-01-01 04:06:17 -08:00
|
|
|
Message.value == "buffer_zone")
|
2004-05-28 21:06:50 -07:00
|
|
|
{
|
2010-04-19 12:43:05 -07:00
|
|
|
m_GeneratedTextsValid = false;
|
2004-05-28 21:06:50 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 03:31:47 -08:00
|
|
|
void IGUITextOwner::HandleSizeChanged()
|
2010-04-19 12:43:05 -07:00
|
|
|
{
|
|
|
|
|
// update our text positions
|
|
|
|
|
m_GeneratedTextsValid = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-30 07:08:14 -07:00
|
|
|
void IGUITextOwner::UpdateText()
|
2004-05-28 21:06:50 -07:00
|
|
|
{
|
2010-04-19 12:43:05 -07:00
|
|
|
if (!m_GeneratedTextsValid)
|
|
|
|
|
{
|
|
|
|
|
SetupText();
|
|
|
|
|
m_GeneratedTextsValid = true;
|
|
|
|
|
}
|
2019-09-30 07:08:14 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-29 12:47:36 -07:00
|
|
|
void IGUITextOwner::DrawText(CCanvas2D& canvas, size_t index, const CGUIColor& color, const CVector2D& pos, const CRect& clipping)
|
2019-09-30 07:08:14 -07:00
|
|
|
{
|
|
|
|
|
UpdateText();
|
2010-04-19 12:43:05 -07:00
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
ENSURE(index < m_GeneratedTexts.size() && "Trying to draw a Text Index within a IGUITextOwner that doesn't exist");
|
2004-05-28 21:06:50 -07:00
|
|
|
|
2021-05-29 12:47:36 -07:00
|
|
|
m_GeneratedTexts.at(index).Draw(m_pObject.GetGUI(), canvas, color, pos, clipping);
|
2004-05-28 21:06:50 -07:00
|
|
|
}
|
2004-12-28 04:17:04 -08:00
|
|
|
|
2026-03-05 03:31:47 -08:00
|
|
|
void IGUITextOwner::CalculateTextPosition(const CRect& ObjSize, CVector2D& TextPos, CGUIText& Text)
|
2004-12-28 04:17:04 -08:00
|
|
|
{
|
2013-01-05 17:46:44 -08:00
|
|
|
// The horizontal Alignment is now computed in GenerateText in order to not have to
|
|
|
|
|
// loop through all of the TextCall objects again.
|
2021-03-28 14:55:13 -07:00
|
|
|
TextPos.X = ObjSize.left;
|
2004-12-28 04:17:04 -08:00
|
|
|
|
2021-05-06 01:22:37 -07:00
|
|
|
switch (m_TextVAlign)
|
2004-12-28 04:17:04 -08:00
|
|
|
{
|
2021-03-27 05:07:38 -07:00
|
|
|
case EVAlign::TOP:
|
2021-03-28 14:55:13 -07:00
|
|
|
TextPos.Y = ObjSize.top;
|
2004-12-28 04:17:04 -08:00
|
|
|
break;
|
2021-03-27 05:07:38 -07:00
|
|
|
case EVAlign::CENTER:
|
2004-12-28 04:17:04 -08:00
|
|
|
// Round to integer pixel values, else the fonts look awful
|
2025-05-16 16:02:40 -07:00
|
|
|
TextPos.Y = ObjSize.CenterPoint().Y - Text.GetSize().Height / 2.f;
|
2004-12-28 04:17:04 -08:00
|
|
|
break;
|
2021-03-27 05:07:38 -07:00
|
|
|
case EVAlign::BOTTOM:
|
2021-03-28 14:55:13 -07:00
|
|
|
TextPos.Y = ObjSize.bottom - Text.GetSize().Height;
|
2004-12-28 04:17:04 -08:00
|
|
|
break;
|
|
|
|
|
default:
|
2009-11-03 13:46:35 -08:00
|
|
|
debug_warn(L"Broken EVAlign in CButton::SetupText()");
|
2004-12-28 04:17:04 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2005-01-16 20:52:02 -08:00
|
|
|
}
|