Line data Source code
1 : /* Copyright (C) 2021 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/CGUI.h"
23 : #include "gui/SGUIMessage.h"
24 : #include "gui/ObjectBases/IGUIObject.h"
25 : #include "gui/SettingTypes/CGUIString.h"
26 : #include "maths/Vector2D.h"
27 :
28 : #include <math.h>
29 :
30 0 : IGUITextOwner::IGUITextOwner(IGUIObject& pObject)
31 : : m_pObject(pObject),
32 : m_GeneratedTextsValid(),
33 : m_TextAlign(&pObject, "text_align", EAlign::LEFT),
34 0 : m_TextVAlign(&pObject, "text_valign", EVAlign::TOP)
35 : {
36 0 : }
37 :
38 0 : IGUITextOwner::~IGUITextOwner()
39 : {
40 0 : }
41 :
42 0 : CGUIText& IGUITextOwner::AddText()
43 : {
44 0 : m_GeneratedTexts.emplace_back();
45 0 : return m_GeneratedTexts.back();
46 : }
47 :
48 0 : CGUIText& IGUITextOwner::AddText(const CGUIString& Text, const CStrW& Font, const float& Width, const float& BufferZone)
49 : {
50 : // Avoids a move constructor
51 0 : m_GeneratedTexts.emplace_back(m_pObject.GetGUI(), Text, Font, Width, BufferZone, m_TextAlign, &m_pObject);
52 0 : return m_GeneratedTexts.back();
53 : }
54 :
55 0 : void IGUITextOwner::HandleMessage(SGUIMessage& Message)
56 : {
57 0 : switch (Message.type)
58 : {
59 0 : case GUIM_SETTINGS_UPDATED:
60 : // Everything that can change the visual appearance.
61 : // it is assumed that the text of the object will be dependent on
62 : // these. Although that is not certain, but one will have to manually
63 : // change it and disregard this function.
64 0 : if (Message.value == "size" || Message.value == "tooltip" ||
65 0 : Message.value == "absolute" || Message.value == "caption" ||
66 0 : Message.value == "font" || Message.value == "textcolor" ||
67 0 : Message.value == "text_align" || Message.value == "text_valign" ||
68 0 : Message.value == "buffer_zone")
69 : {
70 0 : m_GeneratedTextsValid = false;
71 : }
72 0 : break;
73 :
74 0 : default:
75 0 : break;
76 : }
77 0 : }
78 :
79 0 : void IGUITextOwner::UpdateCachedSize()
80 : {
81 : // update our text positions
82 0 : m_GeneratedTextsValid = false;
83 0 : }
84 :
85 0 : void IGUITextOwner::UpdateText()
86 : {
87 0 : if (!m_GeneratedTextsValid)
88 : {
89 0 : SetupText();
90 0 : m_GeneratedTextsValid = true;
91 : }
92 0 : }
93 :
94 0 : void IGUITextOwner::DrawText(CCanvas2D& canvas, size_t index, const CGUIColor& color, const CVector2D& pos, const CRect& clipping)
95 : {
96 0 : UpdateText();
97 :
98 0 : ENSURE(index < m_GeneratedTexts.size() && "Trying to draw a Text Index within a IGUITextOwner that doesn't exist");
99 :
100 0 : m_GeneratedTexts.at(index).Draw(m_pObject.GetGUI(), canvas, color, pos, clipping);
101 0 : }
102 :
103 0 : void IGUITextOwner::CalculateTextPosition(CRect& ObjSize, CVector2D& TextPos, CGUIText& Text)
104 : {
105 : // The horizontal Alignment is now computed in GenerateText in order to not have to
106 : // loop through all of the TextCall objects again.
107 0 : TextPos.X = ObjSize.left;
108 :
109 0 : switch (m_TextVAlign)
110 : {
111 0 : case EVAlign::TOP:
112 0 : TextPos.Y = ObjSize.top;
113 0 : break;
114 0 : case EVAlign::CENTER:
115 : // Round to integer pixel values, else the fonts look awful
116 0 : TextPos.Y = floorf(ObjSize.CenterPoint().Y - Text.GetSize().Height / 2.f);
117 0 : break;
118 0 : case EVAlign::BOTTOM:
119 0 : TextPos.Y = ObjSize.bottom - Text.GetSize().Height;
120 0 : break;
121 0 : default:
122 0 : debug_warn(L"Broken EVAlign in CButton::SetupText()");
123 0 : break;
124 : }
125 0 : }
|