Changeset View
Changeset View
Standalone View
Standalone View
source/gui/IGUITextOwner.cpp
Show All 16 Lines | |||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "IGUITextOwner.h" | #include "IGUITextOwner.h" | ||||
#include "gui/GUI.h" | #include "gui/GUI.h" | ||||
#include "gui/scripting/JSInterface_IGUITextOwner.h" | #include "gui/scripting/JSInterface_IGUITextOwner.h" | ||||
#include <math.h> | |||||
IGUITextOwner::IGUITextOwner(CGUI* pGUI) | IGUITextOwner::IGUITextOwner(CGUI* pGUI) | ||||
: IGUIObject(pGUI), m_GeneratedTextsValid(false) | : IGUIObject(pGUI), m_GeneratedTextsValid(false) | ||||
{ | { | ||||
} | } | ||||
IGUITextOwner::~IGUITextOwner() | IGUITextOwner::~IGUITextOwner() | ||||
{ | { | ||||
for (SGUIText* const& t : m_GeneratedTexts) | |||||
delete t; | |||||
} | } | ||||
void IGUITextOwner::CreateJSObject() | void IGUITextOwner::CreateJSObject() | ||||
{ | { | ||||
IGUIObject::CreateJSObject(); | IGUIObject::CreateJSObject(); | ||||
JSI_IGUITextOwner::RegisterScriptFunctions( | JSI_IGUITextOwner::RegisterScriptFunctions( | ||||
GetGUI()->GetScriptInterface()->GetContext(), m_JSObject); | GetGUI()->GetScriptInterface()->GetContext(), m_JSObject); | ||||
} | } | ||||
void IGUITextOwner::AddText(SGUIText* text) | CGUIText& IGUITextOwner::AddText() | ||||
{ | |||||
m_GeneratedTexts.emplace_back(); | |||||
return m_GeneratedTexts.back(); | |||||
} | |||||
CGUIText& IGUITextOwner::AddText(const CGUIString& Text, const CStrW& Font, const float& Width, const float& BufferZone, const IGUIObject* pObject) | |||||
{ | { | ||||
m_GeneratedTexts.push_back(text); | // Avoids a move constructor | ||||
m_GeneratedTexts.emplace_back(m_pGUI, Text, Font, Width, BufferZone, pObject); | |||||
return m_GeneratedTexts.back(); | |||||
} | } | ||||
void IGUITextOwner::HandleMessage(SGUIMessage& Message) | void IGUITextOwner::HandleMessage(SGUIMessage& Message) | ||||
{ | { | ||||
switch (Message.type) | switch (Message.type) | ||||
{ | { | ||||
case GUIM_SETTINGS_UPDATED: | case GUIM_SETTINGS_UPDATED: | ||||
// Everything that can change the visual appearance. | // Everything that can change the visual appearance. | ||||
Show All 30 Lines | void IGUITextOwner::DrawText(size_t index, const CGUIColor& color, const CPos& pos, float z, const CRect& clipping) | ||||
if (!m_GeneratedTextsValid) | if (!m_GeneratedTextsValid) | ||||
{ | { | ||||
SetupText(); | SetupText(); | ||||
m_GeneratedTextsValid = true; | m_GeneratedTextsValid = true; | ||||
} | } | ||||
ENSURE(index < m_GeneratedTexts.size() && "Trying to draw a Text Index within a IGUITextOwner that doesn't exist"); | ENSURE(index < m_GeneratedTexts.size() && "Trying to draw a Text Index within a IGUITextOwner that doesn't exist"); | ||||
if (GetGUI()) | m_GeneratedTexts.at(index).Draw(m_pGUI, color, pos, z, clipping); | ||||
GetGUI()->DrawText(*m_GeneratedTexts[index], color, pos, z, clipping); | |||||
} | } | ||||
void IGUITextOwner::CalculateTextPosition(CRect& ObjSize, CPos& TextPos, SGUIText& Text) | void IGUITextOwner::CalculateTextPosition(CRect& ObjSize, CPos& TextPos, CGUIText& Text) | ||||
{ | { | ||||
EVAlign valign; | EVAlign valign; | ||||
GUI<EVAlign>::GetSetting(this, "text_valign", valign); | GUI<EVAlign>::GetSetting(this, "text_valign", valign); | ||||
// The horizontal Alignment is now computed in GenerateText in order to not have to | // The horizontal Alignment is now computed in GenerateText in order to not have to | ||||
// loop through all of the TextCall objects again. | // loop through all of the TextCall objects again. | ||||
TextPos.x = ObjSize.left; | TextPos.x = ObjSize.left; | ||||
switch (valign) | switch (valign) | ||||
{ | { | ||||
case EVAlign_Top: | case EVAlign_Top: | ||||
TextPos.y = ObjSize.top; | TextPos.y = ObjSize.top; | ||||
break; | break; | ||||
case EVAlign_Center: | case EVAlign_Center: | ||||
// Round to integer pixel values, else the fonts look awful | // Round to integer pixel values, else the fonts look awful | ||||
TextPos.y = floorf(ObjSize.CenterPoint().y - Text.m_Size.cy/2.f); | TextPos.y = floorf(ObjSize.CenterPoint().y - Text.GetSize().cy / 2.f); | ||||
break; | break; | ||||
case EVAlign_Bottom: | case EVAlign_Bottom: | ||||
TextPos.y = ObjSize.bottom - Text.m_Size.cy; | TextPos.y = ObjSize.bottom - Text.GetSize().cy; | ||||
break; | break; | ||||
default: | default: | ||||
debug_warn(L"Broken EVAlign in CButton::SetupText()"); | debug_warn(L"Broken EVAlign in CButton::SetupText()"); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
CSize IGUITextOwner::CalculateTextSize() | CSize IGUITextOwner::CalculateTextSize() | ||||
{ | { | ||||
if (!m_GeneratedTextsValid) | if (!m_GeneratedTextsValid) | ||||
{ | { | ||||
SetupText(); | SetupText(); | ||||
m_GeneratedTextsValid = true; | m_GeneratedTextsValid = true; | ||||
} | } | ||||
if (m_GeneratedTexts.empty()) | if (m_GeneratedTexts.empty()) | ||||
return CSize(); | return CSize(); | ||||
// GUI Object types that use multiple texts may override this function. | // GUI Object types that use multiple texts may override this function. | ||||
return m_GeneratedTexts[0]->m_Size; | return m_GeneratedTexts[0].GetSize(); | ||||
} | } | ||||
bool IGUITextOwner::MouseOverIcon() | bool IGUITextOwner::MouseOverIcon() | ||||
{ | { | ||||
return false; | return false; | ||||
} | } |
Wildfire Games · Phabricator