Changeset View
Changeset View
Standalone View
Standalone View
source/gui/CGUI.cpp
Show First 20 Lines • Show All 282 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
CGUI::CGUI(const shared_ptr<ScriptRuntime>& runtime) | CGUI::CGUI(const shared_ptr<ScriptRuntime>& runtime) | ||||
: m_MouseButtons(0), m_FocusedObject(NULL), m_InternalNameNumber(0) | : m_MouseButtons(0), m_FocusedObject(NULL), m_InternalNameNumber(0) | ||||
{ | { | ||||
m_ScriptInterface.reset(new ScriptInterface("Engine", "GUIPage", runtime)); | m_ScriptInterface.reset(new ScriptInterface("Engine", "GUIPage", runtime)); | ||||
m_ScriptInterface->SetCallbackData(this); | m_ScriptInterface->SetCallbackData(this); | ||||
GuiScriptingInit(*m_ScriptInterface); | |||||
m_ScriptInterface->LoadGlobalScripts(); | |||||
m_BaseObject = new CGUIDummyObject; | m_BaseObject = new CGUIDummyObject; | ||||
m_BaseObject->SetGUI(this); | m_BaseObject->SetGUI(this); | ||||
GuiScriptingInit(*m_ScriptInterface); | |||||
m_ScriptInterface->LoadGlobalScripts(); | |||||
} | } | ||||
CGUI::~CGUI() | CGUI::~CGUI() | ||||
{ | { | ||||
Destroy(); | Destroy(); | ||||
if (m_BaseObject) | if (m_BaseObject) | ||||
delete m_BaseObject; | delete m_BaseObject; | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | |||||
void CGUI::DrawSprite(const CGUISpriteInstance& Sprite, int CellID, const float& Z, const CRect& Rect, const CRect& UNUSED(Clipping)) | void CGUI::DrawSprite(const CGUISpriteInstance& Sprite, int CellID, const float& Z, const CRect& Rect, const CRect& UNUSED(Clipping)) | ||||
{ | { | ||||
// If the sprite doesn't exist (name == ""), don't bother drawing anything | // If the sprite doesn't exist (name == ""), don't bother drawing anything | ||||
if (Sprite.IsEmpty()) | if (Sprite.IsEmpty()) | ||||
return; | return; | ||||
// TODO: Clipping? | // TODO: Clipping? | ||||
Sprite.Draw(Rect, CellID, m_Sprites, Z); | Sprite.Draw(this, Rect, CellID, m_Sprites, Z); | ||||
} | } | ||||
void CGUI::Destroy() | void CGUI::Destroy() | ||||
{ | { | ||||
// We can use the map to delete all | // We can use the map to delete all | ||||
// now we don't want to cancel all if one Destroy fails | // now we don't want to cancel all if one Destroy fails | ||||
for (const std::pair<CStr, IGUIObject*>& p : m_pAllObjects) | for (const std::pair<CStr, IGUIObject*>& p : m_pAllObjects) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 611 Lines • ▼ Show 20 Lines | void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject* pParent, std::vector<std::pair<CStr, CStr> >& NameSubst, boost::unordered_set<VfsPath>& Paths, u32 nesting_depth) | ||||
CStr type(attributes.GetNamedItem(pFile->GetAttributeID("type"))); | CStr type(attributes.GetNamedItem(pFile->GetAttributeID("type"))); | ||||
if (type.empty()) | if (type.empty()) | ||||
type = "empty"; | type = "empty"; | ||||
// Construct object from specified type | // Construct object from specified type | ||||
// henceforth, we need to do a rollback before aborting. | // henceforth, we need to do a rollback before aborting. | ||||
// i.e. releasing this object | // i.e. releasing this object | ||||
IGUIObject* object = ConstructObject(type); | IGUIObject* object = ConstructObject(type); | ||||
object->SetGUI(this); | |||||
if (!object) | if (!object) | ||||
{ | { | ||||
LOGERROR("GUI: Unrecognized object type \"%s\"", type.c_str()); | LOGERROR("GUI: Unrecognized object type \"%s\"", type.c_str()); | ||||
return; | return; | ||||
} | } | ||||
// Cache some IDs for element attribute names, to avoid string comparisons | // Cache some IDs for element attribute names, to avoid string comparisons | ||||
▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | else if (element_name == elmt_action) | ||||
code += grandchild.GetText(); | code += grandchild.GetText(); | ||||
} | } | ||||
else // It’s pure JavaScript code. | else // It’s pure JavaScript code. | ||||
// Read the inline code (concatenating to the file code, if both are specified) | // Read the inline code (concatenating to the file code, if both are specified) | ||||
code += CStr(child.GetText()); | code += CStr(child.GetText()); | ||||
CStr action = CStr(child.GetAttributes().GetNamedItem(attr_on)); | CStr action = CStr(child.GetAttributes().GetNamedItem(attr_on)); | ||||
// We need to set the GUI this object belongs to because RegisterScriptHandler requires an associated GUI. | |||||
object->SetGUI(this); | |||||
object->RegisterScriptHandler(action.LowerCase(), code, this); | object->RegisterScriptHandler(action.LowerCase(), code, this); | ||||
} | } | ||||
else if (element_name == elmt_repeat) | else if (element_name == elmt_repeat) | ||||
{ | { | ||||
Xeromyces_ReadRepeat(child, pFile, object, NameSubst, Paths, nesting_depth); | Xeromyces_ReadRepeat(child, pFile, object, NameSubst, Paths, nesting_depth); | ||||
} | } | ||||
else if (element_name == elmt_translatableAttribute) | else if (element_name == elmt_translatableAttribute) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 288 Lines • ▼ Show 20 Lines | for (XMBAttribute attr : Element.GetAttributes()) | ||||
if (attr_name == "texture") | if (attr_name == "texture") | ||||
{ | { | ||||
Image->m_TextureName = VfsPath("art/textures/ui") / attr_value; | Image->m_TextureName = VfsPath("art/textures/ui") / attr_value; | ||||
} | } | ||||
else if (attr_name == "size") | else if (attr_name == "size") | ||||
{ | { | ||||
CClientArea ca; | CClientArea ca; | ||||
if (!GUI<CClientArea>::ParseString(attr_value, ca)) | if (!GUI<CClientArea>::ParseString(this, attr_value, ca)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_Size = ca; | Image->m_Size = ca; | ||||
} | } | ||||
else if (attr_name == "texture_size") | else if (attr_name == "texture_size") | ||||
{ | { | ||||
CClientArea ca; | CClientArea ca; | ||||
if (!GUI<CClientArea>::ParseString(attr_value, ca)) | if (!GUI<CClientArea>::ParseString(this, attr_value, ca)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_TextureSize = ca; | Image->m_TextureSize = ca; | ||||
} | } | ||||
else if (attr_name == "real_texture_placement") | else if (attr_name == "real_texture_placement") | ||||
{ | { | ||||
CRect rect; | CRect rect; | ||||
if (!GUI<CRect>::ParseString(attr_value, rect)) | if (!GUI<CRect>::ParseString(this, attr_value, rect)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_TexturePlacementInFile = rect; | Image->m_TexturePlacementInFile = rect; | ||||
} | } | ||||
else if (attr_name == "cell_size") | else if (attr_name == "cell_size") | ||||
{ | { | ||||
CSize size; | CSize size; | ||||
if (!GUI<CSize>::ParseString(attr_value, size)) | if (!GUI<CSize>::ParseString(this, attr_value, size)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_CellSize = size; | Image->m_CellSize = size; | ||||
} | } | ||||
else if (attr_name == "fixed_h_aspect_ratio") | else if (attr_name == "fixed_h_aspect_ratio") | ||||
{ | { | ||||
float val; | float val; | ||||
if (!GUI<float>::ParseString(attr_value, val)) | if (!GUI<float>::ParseString(this, attr_value, val)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_FixedHAspectRatio = val; | Image->m_FixedHAspectRatio = val; | ||||
} | } | ||||
else if (attr_name == "round_coordinates") | else if (attr_name == "round_coordinates") | ||||
{ | { | ||||
bool b; | bool b; | ||||
if (!GUI<bool>::ParseString(attr_value, b)) | if (!GUI<bool>::ParseString(this, attr_value, b)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_RoundCoordinates = b; | Image->m_RoundCoordinates = b; | ||||
} | } | ||||
else if (attr_name == "wrap_mode") | else if (attr_name == "wrap_mode") | ||||
{ | { | ||||
if (attr_value == L"repeat") | if (attr_value == L"repeat") | ||||
Image->m_WrapMode = GL_REPEAT; | Image->m_WrapMode = GL_REPEAT; | ||||
else if (attr_value == L"mirrored_repeat") | else if (attr_value == L"mirrored_repeat") | ||||
Image->m_WrapMode = GL_MIRRORED_REPEAT; | Image->m_WrapMode = GL_MIRRORED_REPEAT; | ||||
else if (attr_value == L"clamp_to_edge") | else if (attr_value == L"clamp_to_edge") | ||||
Image->m_WrapMode = GL_CLAMP_TO_EDGE; | Image->m_WrapMode = GL_CLAMP_TO_EDGE; | ||||
else | else | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
} | } | ||||
else if (attr_name == "z_level") | else if (attr_name == "z_level") | ||||
{ | { | ||||
float z_level; | float z_level; | ||||
if (!GUI<float>::ParseString(attr_value, z_level)) | if (!GUI<float>::ParseString(this, attr_value, z_level)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_DeltaZ = z_level/100.f; | Image->m_DeltaZ = z_level/100.f; | ||||
} | } | ||||
else if (attr_name == "backcolor") | else if (attr_name == "backcolor") | ||||
{ | { | ||||
CColor color; | CColor color; | ||||
if (!GUI<CColor>::ParseString(attr_value, color)) | if (!GUI<CColor>::ParseString(this, attr_value, color)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_BackColor = color; | Image->m_BackColor = color; | ||||
} | } | ||||
else if (attr_name == "bordercolor") | else if (attr_name == "bordercolor") | ||||
{ | { | ||||
CColor color; | CColor color; | ||||
if (!GUI<CColor>::ParseString(attr_value, color)) | if (!GUI<CColor>::ParseString(this, attr_value, color)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_BorderColor = color; | Image->m_BorderColor = color; | ||||
} | } | ||||
else if (attr_name == "border") | else if (attr_name == "border") | ||||
{ | { | ||||
bool b; | bool b; | ||||
if (!GUI<bool>::ParseString(attr_value, b)) | if (!GUI<bool>::ParseString(this, attr_value, b)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else | else | ||||
Image->m_Border = b; | Image->m_Border = b; | ||||
} | } | ||||
else | else | ||||
debug_warn(L"Invalid data - DTD shouldn't allow this"); | debug_warn(L"Invalid data - DTD shouldn't allow this"); | ||||
} | } | ||||
Show All 23 Lines | void CGUI::Xeromyces_ReadEffects(XMBElement Element, CXeromyces* pFile, SGUIImageEffects& effects) | ||||
for (XMBAttribute attr : Element.GetAttributes()) | for (XMBAttribute attr : Element.GetAttributes()) | ||||
{ | { | ||||
CStr attr_name(pFile->GetAttributeString(attr.Name)); | CStr attr_name(pFile->GetAttributeString(attr.Name)); | ||||
CStrW attr_value(attr.Value.FromUTF8()); | CStrW attr_value(attr.Value.FromUTF8()); | ||||
if (attr_name == "add_color") | if (attr_name == "add_color") | ||||
{ | { | ||||
CColor color; | CColor color; | ||||
if (!GUI<int>::ParseColor(attr_value, color, 0)) | if (!GUI<int>::ParseColor(this, attr_value, color, 0)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, utf8_from_wstring(attr_value)); | ||||
else effects.m_AddColor = color; | else effects.m_AddColor = color; | ||||
} | } | ||||
else if (attr_name == "grayscale") | else if (attr_name == "grayscale") | ||||
effects.m_Greyscale = true; | effects.m_Greyscale = true; | ||||
else | else | ||||
debug_warn(L"Invalid data - DTD shouldn't allow this"); | debug_warn(L"Invalid data - DTD shouldn't allow this"); | ||||
} | } | ||||
Show All 38 Lines | for (XMBAttribute attr : Element.GetAttributes()) | ||||
if (attr_value == "null") | if (attr_value == "null") | ||||
continue; | continue; | ||||
if (attr_name == "name") | if (attr_name == "name") | ||||
name = attr_value; | name = attr_value; | ||||
else if (attr_name == "show_edge_buttons") | else if (attr_name == "show_edge_buttons") | ||||
{ | { | ||||
bool b; | bool b; | ||||
if (!GUI<bool>::ParseString(attr_value.FromUTF8(), b)) | if (!GUI<bool>::ParseString(this, attr_value.FromUTF8(), b)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); | ||||
else | else | ||||
scrollbar.m_UseEdgeButtons = b; | scrollbar.m_UseEdgeButtons = b; | ||||
} | } | ||||
else if (attr_name == "width") | else if (attr_name == "width") | ||||
{ | { | ||||
float f; | float f; | ||||
if (!GUI<float>::ParseString(attr_value.FromUTF8(), f)) | if (!GUI<float>::ParseString(this, attr_value.FromUTF8(), f)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); | ||||
else | else | ||||
scrollbar.m_Width = f; | scrollbar.m_Width = f; | ||||
} | } | ||||
else if (attr_name == "minimum_bar_size") | else if (attr_name == "minimum_bar_size") | ||||
{ | { | ||||
float f; | float f; | ||||
if (!GUI<float>::ParseString(attr_value.FromUTF8(), f)) | if (!GUI<float>::ParseString(this, attr_value.FromUTF8(), f)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); | ||||
else | else | ||||
scrollbar.m_MinimumBarSize = f; | scrollbar.m_MinimumBarSize = f; | ||||
} | } | ||||
else if (attr_name == "maximum_bar_size") | else if (attr_name == "maximum_bar_size") | ||||
{ | { | ||||
float f; | float f; | ||||
if (!GUI<float>::ParseString(attr_value.FromUTF8(), f)) | if (!GUI<float>::ParseString(this, attr_value.FromUTF8(), f)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); | LOGERROR("GUI: Error parsing '%s' (\"%s\")", attr_name, attr_value); | ||||
else | else | ||||
scrollbar.m_MaximumBarSize = f; | scrollbar.m_MaximumBarSize = f; | ||||
} | } | ||||
else if (attr_name == "sprite_button_top") | else if (attr_name == "sprite_button_top") | ||||
scrollbar.m_SpriteButtonTop = attr_value; | scrollbar.m_SpriteButtonTop = attr_value; | ||||
else if (attr_name == "sprite_button_top_pressed") | else if (attr_name == "sprite_button_top_pressed") | ||||
scrollbar.m_SpriteButtonTopPressed = attr_value; | scrollbar.m_SpriteButtonTopPressed = attr_value; | ||||
Show All 37 Lines | for (XMBAttribute attr : Element.GetAttributes()) | ||||
if (attr_name == "name") | if (attr_name == "name") | ||||
name = attr_value; | name = attr_value; | ||||
else if (attr_name == "sprite") | else if (attr_name == "sprite") | ||||
icon.m_SpriteName = attr_value; | icon.m_SpriteName = attr_value; | ||||
else if (attr_name == "size") | else if (attr_name == "size") | ||||
{ | { | ||||
CSize size; | CSize size; | ||||
if (!GUI<CSize>::ParseString(attr_value.FromUTF8(), size)) | if (!GUI<CSize>::ParseString(this, attr_value.FromUTF8(), size)) | ||||
LOGERROR("Error parsing '%s' (\"%s\") inside <icon>.", attr_name, attr_value); | LOGERROR("Error parsing '%s' (\"%s\") inside <icon>.", attr_name, attr_value); | ||||
else | else | ||||
icon.m_Size = size; | icon.m_Size = size; | ||||
} | } | ||||
else if (attr_name == "cell_id") | else if (attr_name == "cell_id") | ||||
{ | { | ||||
int cell_id; | int cell_id; | ||||
if (!GUI<int>::ParseString(attr_value.FromUTF8(), cell_id)) | if (!GUI<int>::ParseString(this, attr_value.FromUTF8(), cell_id)) | ||||
LOGERROR("GUI: Error parsing '%s' (\"%s\") inside <icon>.", attr_name, attr_value); | LOGERROR("GUI: Error parsing '%s' (\"%s\") inside <icon>.", attr_name, attr_value); | ||||
else | else | ||||
icon.m_CellID = cell_id; | icon.m_CellID = cell_id; | ||||
} | } | ||||
else | else | ||||
debug_warn(L"Invalid data - DTD shouldn't allow this"); | debug_warn(L"Invalid data - DTD shouldn't allow this"); | ||||
} | } | ||||
m_Icons[name] = icon; | m_Icons[name] = icon; | ||||
} | } | ||||
void CGUI::Xeromyces_ReadTooltip(XMBElement Element, CXeromyces* pFile) | void CGUI::Xeromyces_ReadTooltip(XMBElement Element, CXeromyces* pFile) | ||||
{ | { | ||||
IGUIObject* object = new CTooltip; | // TODO: seems like this should know about the UI to begin with | ||||
IGUIObject* object = new CTooltip(); | |||||
object->SetGUI(this); | |||||
for (XMBAttribute attr : Element.GetAttributes()) | for (XMBAttribute attr : Element.GetAttributes()) | ||||
{ | { | ||||
CStr attr_name(pFile->GetAttributeString(attr.Name)); | CStr attr_name(pFile->GetAttributeString(attr.Name)); | ||||
CStr attr_value(attr.Value); | CStr attr_value(attr.Value); | ||||
if (attr_name == "name") | if (attr_name == "name") | ||||
object->SetName("__tooltip_" + attr_value); | object->SetName("__tooltip_" + attr_value); | ||||
Show All 28 Lines |
Wildfire Games · Phabricator