Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "IGUITextOwner.h" 21 : 22 : #include "gui/CGUIText.h" 23 : #include "gui/ObjectBases/IGUIObject.h" 24 : #include "gui/SGUIMessage.h" 25 : #include "gui/SettingTypes/EAlign.h" 26 : #include "lib/debug.h" 27 : #include "maths/Size2D.h" 28 : #include "maths/Vector2D.h" 29 : #include "ps/CStr.h" 30 : 31 : #include <string> 32 : 33 0 : IGUITextOwner::IGUITextOwner(IGUIObject& pObject) 34 0 : : m_pObject(pObject), 35 0 : m_GeneratedTextsValid(), 36 0 : m_TextAlign(&pObject, "text_align", EAlign::LEFT), 37 0 : m_TextVAlign(&pObject, "text_valign", EVAlign::TOP) 38 : { 39 0 : } 40 : 41 0 : IGUITextOwner::~IGUITextOwner() 42 : { 43 0 : } 44 : 45 0 : CGUIText& IGUITextOwner::AddText() 46 : { 47 0 : m_GeneratedTexts.emplace_back(); 48 0 : return m_GeneratedTexts.back(); 49 : } 50 : 51 0 : CGUIText& IGUITextOwner::AddText(const CGUIString& Text, const CStrW& Font, const float& Width, const float& BufferZone) 52 : { 53 : // Avoids a move constructor 54 0 : m_GeneratedTexts.emplace_back(m_pObject.GetGUI(), Text, Font, Width, BufferZone, m_TextAlign, &m_pObject); 55 0 : return m_GeneratedTexts.back(); 56 : } 57 : 58 0 : void IGUITextOwner::HandleMessage(SGUIMessage& Message) 59 : { 60 0 : switch (Message.type) 61 : { 62 0 : case GUIM_SETTINGS_UPDATED: 63 : // Everything that can change the visual appearance. 64 : // it is assumed that the text of the object will be dependent on 65 : // these. Although that is not certain, but one will have to manually 66 : // change it and disregard this function. 67 0 : if (Message.value == "size" || Message.value == "tooltip" || 68 0 : Message.value == "absolute" || Message.value == "caption" || 69 0 : Message.value == "font" || Message.value == "textcolor" || 70 0 : Message.value == "text_align" || Message.value == "text_valign" || 71 0 : Message.value == "buffer_zone") 72 : { 73 0 : m_GeneratedTextsValid = false; 74 : } 75 : break; 76 : 77 : default: 78 : break; 79 : } 80 0 : } 81 : 82 0 : void IGUITextOwner::HandleSizeChanged() 83 : { 84 : // update our text positions 85 0 : m_GeneratedTextsValid = false; 86 0 : } 87 : 88 0 : void IGUITextOwner::UpdateText() 89 : { 90 0 : if (!m_GeneratedTextsValid) 91 : { 92 0 : SetupText(); 93 0 : m_GeneratedTextsValid = true; 94 : } 95 0 : } 96 : 97 0 : void IGUITextOwner::DrawText(CCanvas2D& canvas, size_t index, const CGUIColor& color, const CVector2D& pos, const CRect& clipping) 98 : { 99 0 : UpdateText(); 100 : 101 0 : ENSURE(index < m_GeneratedTexts.size() && "Trying to draw a Text Index within a IGUITextOwner that doesn't exist"); 102 : 103 0 : m_GeneratedTexts.at(index).Draw(m_pObject.GetGUI(), canvas, color, pos, clipping); 104 0 : } 105 : 106 0 : void IGUITextOwner::CalculateTextPosition(const CRect& ObjSize, CVector2D& TextPos, CGUIText& Text) 107 : { 108 : // The horizontal Alignment is now computed in GenerateText in order to not have to 109 : // loop through all of the TextCall objects again. 110 0 : TextPos.X = ObjSize.left; 111 : 112 0 : switch (m_TextVAlign) 113 : { 114 0 : case EVAlign::TOP: 115 0 : TextPos.Y = ObjSize.top; 116 0 : break; 117 0 : case EVAlign::CENTER: 118 : // Round to integer pixel values, else the fonts look awful 119 0 : TextPos.Y = ObjSize.CenterPoint().Y - Text.GetSize().Height / 2.f; 120 0 : break; 121 0 : case EVAlign::BOTTOM: 122 0 : TextPos.Y = ObjSize.bottom - Text.GetSize().Height; 123 0 : break; 124 0 : default: 125 0 : debug_warn(L"Broken EVAlign in CButton::SetupText()"); 126 : break; 127 : } 128 0 : }