2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* 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-05-29 06:06:50 +02:00
|
|
|
/*
|
|
|
|
IGUITextOwner
|
|
|
|
*/
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
2004-05-29 06:06:50 +02:00
|
|
|
#include "GUI.h"
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Constructor / Destructor
|
|
|
|
//-------------------------------------------------------------------
|
2010-04-19 21:43:05 +02:00
|
|
|
IGUITextOwner::IGUITextOwner() : m_GeneratedTextsValid(false)
|
2004-05-29 06:06:50 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IGUITextOwner::~IGUITextOwner()
|
|
|
|
{
|
2005-04-07 11:13:10 +02:00
|
|
|
// Delete all generated texts.
|
2008-07-13 23:22:03 +02:00
|
|
|
std::vector<SGUIText*>::iterator it;
|
2004-05-29 06:06:50 +02:00
|
|
|
for (it=m_GeneratedTexts.begin(); it!=m_GeneratedTexts.end(); ++it)
|
|
|
|
{
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IGUITextOwner::AddText(SGUIText * text)
|
|
|
|
{
|
|
|
|
m_GeneratedTexts.push_back(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IGUITextOwner::HandleMessage(const SGUIMessage &Message)
|
|
|
|
{
|
|
|
|
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-31 04:09:58 +02:00
|
|
|
// these. Although that is not certain, but one will have to manually
|
2004-05-29 06:06:50 +02:00
|
|
|
// change it and disregard this function.
|
2004-10-14 12:09:26 +02:00
|
|
|
// TODO Gee: (2004-09-07) Make sure this is all options that can affect the text.
|
2004-12-21 14:37:24 +01:00
|
|
|
if (Message.value == "size" || Message.value == "z" ||
|
|
|
|
Message.value == "absolute" || Message.value == "caption" ||
|
|
|
|
Message.value == "font" || Message.value == "textcolor" ||
|
2005-01-01 13:06:17 +01:00
|
|
|
Message.value == "buffer_zone")
|
2004-05-29 06:06:50 +02:00
|
|
|
{
|
2010-04-19 21:43:05 +02:00
|
|
|
m_GeneratedTextsValid = false;
|
2004-05-29 06:06:50 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-19 21:43:05 +02:00
|
|
|
void IGUITextOwner::UpdateCachedSize()
|
|
|
|
{
|
|
|
|
// If an ancestor's size changed, this will let us intercept the change and
|
|
|
|
// update our text positions
|
|
|
|
|
|
|
|
IGUIObject::UpdateCachedSize();
|
|
|
|
m_GeneratedTextsValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IGUITextOwner::Draw(const int &index, const CColor &color, const CPos &pos,
|
2005-07-24 02:01:41 +02:00
|
|
|
const float &z, const CRect &clipping)
|
2004-05-29 06:06:50 +02:00
|
|
|
{
|
2010-04-19 21:43:05 +02:00
|
|
|
if (!m_GeneratedTextsValid)
|
|
|
|
{
|
|
|
|
SetupText();
|
|
|
|
m_GeneratedTextsValid = true;
|
|
|
|
}
|
|
|
|
|
2004-05-29 13:59:59 +02:00
|
|
|
if (index < 0 || index >= (int)m_GeneratedTexts.size())
|
2004-05-29 06:06:50 +02:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"Trying to draw a Text Index within a IGUITextOwner that doesn't exist");
|
2004-05-29 06:06:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetGUI())
|
|
|
|
{
|
2005-07-24 02:01:41 +02:00
|
|
|
GetGUI()->DrawText(*m_GeneratedTexts[index], color, pos, z, clipping);
|
2004-05-29 06:06:50 +02:00
|
|
|
}
|
|
|
|
}
|
2004-12-28 13:17:04 +01:00
|
|
|
|
|
|
|
void IGUITextOwner::CalculateTextPosition(CRect &ObjSize, CPos &TextPos, SGUIText &Text)
|
|
|
|
{
|
|
|
|
EAlign align;
|
|
|
|
EVAlign valign;
|
2005-01-01 13:06:17 +01:00
|
|
|
GUI<EAlign>::GetSetting(this, "text_align", align);
|
|
|
|
GUI<EVAlign>::GetSetting(this, "text_valign", valign);
|
2004-12-28 13:17:04 +01:00
|
|
|
|
|
|
|
switch (align)
|
|
|
|
{
|
|
|
|
case EAlign_Left:
|
2004-12-28 13:38:45 +01:00
|
|
|
TextPos.x = ObjSize.left;
|
2004-12-28 13:17:04 +01:00
|
|
|
break;
|
|
|
|
case EAlign_Center:
|
|
|
|
// Round to integer pixel values, else the fonts look awful
|
|
|
|
TextPos.x = floorf(ObjSize.CenterPoint().x - Text.m_Size.cx/2.f);
|
|
|
|
break;
|
|
|
|
case EAlign_Right:
|
2004-12-28 13:38:45 +01:00
|
|
|
TextPos.x = ObjSize.right - Text.m_Size.cx;
|
2004-12-28 13:17:04 +01:00
|
|
|
break;
|
|
|
|
default:
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"Broken EAlign in CButton::SetupText()");
|
2004-12-28 13:17:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (valign)
|
|
|
|
{
|
|
|
|
case EVAlign_Top:
|
2004-12-28 13:38:45 +01:00
|
|
|
TextPos.y = ObjSize.top;
|
2004-12-28 13:17:04 +01:00
|
|
|
break;
|
|
|
|
case EVAlign_Center:
|
|
|
|
// Round to integer pixel values, else the fonts look awful
|
|
|
|
TextPos.y = floorf(ObjSize.CenterPoint().y - Text.m_Size.cy/2.f);
|
|
|
|
break;
|
|
|
|
case EVAlign_Bottom:
|
2004-12-28 13:38:45 +01:00
|
|
|
TextPos.y = ObjSize.bottom - Text.m_Size.cy;
|
2004-12-28 13:17:04 +01:00
|
|
|
break;
|
|
|
|
default:
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_warn(L"Broken EVAlign in CButton::SetupText()");
|
2004-12-28 13:17:04 +01:00
|
|
|
break;
|
|
|
|
}
|
2005-01-17 05:52:02 +01:00
|
|
|
}
|