1 /* Copyright (C) 2015 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 "GUI.h"
21 
IGUITextOwner()22 IGUITextOwner::IGUITextOwner() : m_GeneratedTextsValid(false)
23 {
24 }
25 
~IGUITextOwner()26 IGUITextOwner::~IGUITextOwner()
27 {
28 	for (SGUIText* const& t : m_GeneratedTexts)
29 		delete t;
30 }
31 
AddText(SGUIText * text)32 void IGUITextOwner::AddText(SGUIText* text)
33 {
34 	m_GeneratedTexts.push_back(text);
35 }
36 
HandleMessage(SGUIMessage & Message)37 void IGUITextOwner::HandleMessage(SGUIMessage& Message)
38 {
39 	switch (Message.type)
40 	{
41 	case GUIM_SETTINGS_UPDATED:
42 		// Everything that can change the visual appearance.
43 		//  it is assumed that the text of the object will be dependent on
44 		//  these. Although that is not certain, but one will have to manually
45 		//  change it and disregard this function.
46 		// TODO Gee: (2004-09-07) Make sure this is all options that can affect the text.
47 		if (Message.value == "size" || Message.value == "z" ||
48 			Message.value == "absolute" || Message.value == "caption" ||
49 			Message.value == "font" || Message.value == "textcolor" ||
50 			Message.value == "buffer_zone")
51 		{
52 			m_GeneratedTextsValid = false;
53 		}
54 		break;
55 
56 	default:
57 		break;
58 	}
59 }
60 
UpdateCachedSize()61 void IGUITextOwner::UpdateCachedSize()
62 {
63 	// If an ancestor's size changed, this will let us intercept the change and
64 	// update our text positions
65 
66 	IGUIObject::UpdateCachedSize();
67 	m_GeneratedTextsValid = false;
68 }
69 
DrawText(size_t index,const CColor & color,const CPos & pos,float z,const CRect & clipping)70 void IGUITextOwner::DrawText(size_t index, const CColor& color, const CPos& pos, float z, const CRect& clipping)
71 {
72 	if (!m_GeneratedTextsValid)
73 	{
74 		SetupText();
75 		m_GeneratedTextsValid = true;
76 	}
77 
78 	ENSURE(index < m_GeneratedTexts.size() && "Trying to draw a Text Index within a IGUITextOwner that doesn't exist");
79 
80 	if (GetGUI())
81 		GetGUI()->DrawText(*m_GeneratedTexts[index], color, pos, z, clipping);
82 }
83 
CalculateTextPosition(CRect & ObjSize,CPos & TextPos,SGUIText & Text)84 void IGUITextOwner::CalculateTextPosition(CRect& ObjSize, CPos& TextPos, SGUIText& Text)
85 {
86 	EVAlign valign;
87 	GUI<EVAlign>::GetSetting(this, "text_valign", valign);
88 
89 	// The horizontal Alignment is now computed in GenerateText in order to not have to
90 	// loop through all of the TextCall objects again.
91 	TextPos.x = ObjSize.left;
92 
93 	switch (valign)
94 	{
95 	case EVAlign_Top:
96 		TextPos.y = ObjSize.top;
97 		break;
98 	case EVAlign_Center:
99 		// Round to integer pixel values, else the fonts look awful
100 		TextPos.y = floorf(ObjSize.CenterPoint().y - Text.m_Size.cy/2.f);
101 		break;
102 	case EVAlign_Bottom:
103 		TextPos.y = ObjSize.bottom - Text.m_Size.cy;
104 		break;
105 	default:
106 		debug_warn(L"Broken EVAlign in CButton::SetupText()");
107 		break;
108 	}
109 }
110 
MouseOverIcon()111 bool IGUITextOwner::MouseOverIcon()
112 {
113 	return false;
114 }
115