Skip to content

Commit

Permalink
Moved the XML utility functions to UIArea so they can be used by panels
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 29, 2011
1 parent de21444 commit 8db39ac
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 162 deletions.
10 changes: 1 addition & 9 deletions UIDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ UIDialog::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
return false;
}

attr = node->first_attribute("expand", 0, false);
if (attr) {
const char *value = attr->value();
if (*value == '0' || *value == 'f' || *value == 'F') {
m_expand = false;
} else {
m_expand = true;
}
}
LoadBool(node, "expand", m_expand);

return true;
}
Expand Down
170 changes: 110 additions & 60 deletions screenlib/UIArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,10 @@
slouken@libsdl.org
*/

#include "SDL_FrameBuf.h"
#include "UIArea.h"


static AnchorLocation ParseAnchorLocation(const char *text)
{
AnchorLocation value = TOPLEFT;

if (strcasecmp(text, "TOPLEFT") == 0) {
value = TOPLEFT;
} else if (strcasecmp(text, "TOP") == 0) {
value = TOP;
} else if (strcasecmp(text, "TOPRIGHT") == 0) {
value = TOPRIGHT;
} else if (strcasecmp(text, "LEFT") == 0) {
value = LEFT;
} else if (strcasecmp(text, "CENTER") == 0) {
value = CENTER;
} else if (strcasecmp(text, "RIGHT") == 0) {
value = RIGHT;
} else if (strcasecmp(text, "BOTTOMLEFT") == 0) {
value = BOTTOMLEFT;
} else if (strcasecmp(text, "BOTTOM") == 0) {
value = BOTTOM;
} else if (strcasecmp(text, "BOTTOMRIGHT") == 0) {
value = BOTTOMRIGHT;
}
return value;

}

UIArea::UIArea(FrameBuf *screen, UIArea *anchor, int w, int h) : ErrorBase()
{
m_screen = screen;
Expand All @@ -75,26 +49,12 @@ UIArea::Load(rapidxml::xml_node<> *node)
rapidxml::xml_attribute<> *attr;
SDL_Rect rect = m_rect;

attr = node->first_attribute("show", 0, false);
if (attr) {
const char *value = attr->value();
if (*value == '0' || *value == 'f' || *value == 'F') {
m_shown = false;
} else {
m_shown = true;
}
}
LoadBool(node, "show", m_shown);

child = node->first_node("size", 0, false);
if (child) {
attr = child->first_attribute("w", 0, false);
if (attr) {
m_rect.w = SDL_atoi(attr->value());
}
attr = child->first_attribute("h", 0, false);
if (attr) {
m_rect.h = SDL_atoi(attr->value());
}
LoadNumber(child, "w", m_rect.w);
LoadNumber(child, "h", m_rect.h);
}

child = node->first_node("anchor", 0, false);
Expand All @@ -114,23 +74,11 @@ UIArea::Load(rapidxml::xml_node<> *node)
return false;
}

attr = child->first_attribute("anchorFrom", 0, false);
if (attr) {
m_anchor.anchorFrom = ParseAnchorLocation(attr->value());
}
attr = child->first_attribute("anchorTo", 0, false);
if (attr) {
m_anchor.anchorTo = ParseAnchorLocation(attr->value());
}
LoadAnchorLocation(child, "anchorFrom", m_anchor.anchorFrom);
LoadAnchorLocation(child, "anchorTo", m_anchor.anchorTo);

attr = child->first_attribute("x", 0, false);
if (attr) {
m_anchor.offsetX = SDL_atoi(attr->value());
}
attr = child->first_attribute("y", 0, false);
if (attr) {
m_anchor.offsetY = SDL_atoi(attr->value());
}
LoadNumber(child, "x", m_anchor.offsetX);
LoadNumber(child, "y", m_anchor.offsetY);
}

CalculateAnchor(false);
Expand Down Expand Up @@ -185,6 +133,108 @@ UIArea::SetHeight(int h)
}
}

bool
UIArea::LoadBool(rapidxml::xml_node<> *node, const char *name, bool &value)
{
rapidxml::xml_attribute<> *attr;

attr = node->first_attribute(name, 0, false);
if (attr) {
const char *text = attr->value();
if (*text == '\0' || *text == '0' ||
*text == 'f' || *text == 'F') {
value = false;
} else {
value = true;
}
return true;
}
return false;
}

bool
UIArea::LoadNumber(rapidxml::xml_node<> *node, const char *name, int &value)
{
rapidxml::xml_attribute<> *attr;

attr = node->first_attribute(name, 0, false);
if (attr) {
value = (int)strtol(attr->value(), NULL, 0);
return true;
}
return false;
}

bool
UIArea::LoadString(rapidxml::xml_node<> *node, const char *name, char *&value)
{
rapidxml::xml_attribute<> *attr;

attr = node->first_attribute(name, 0, false);
if (attr) {
if (value) {
SDL_free(value);
}
value = SDL_strdup(attr->value());
return true;
}
return false;
}

bool
UIArea::LoadAnchorLocation(rapidxml::xml_node<> *node, const char *name, AnchorLocation &value)
{
rapidxml::xml_attribute<> *attr;

attr = node->first_attribute(name, 0, false);
if (attr) {
const char *text = attr->value();

if (strcasecmp(text, "TOPLEFT") == 0) {
value = TOPLEFT;
} else if (strcasecmp(text, "TOP") == 0) {
value = TOP;
} else if (strcasecmp(text, "TOPRIGHT") == 0) {
value = TOPRIGHT;
} else if (strcasecmp(text, "LEFT") == 0) {
value = LEFT;
} else if (strcasecmp(text, "CENTER") == 0) {
value = CENTER;
} else if (strcasecmp(text, "RIGHT") == 0) {
value = RIGHT;
} else if (strcasecmp(text, "BOTTOMLEFT") == 0) {
value = BOTTOMLEFT;
} else if (strcasecmp(text, "BOTTOM") == 0) {
value = BOTTOM;
} else if (strcasecmp(text, "BOTTOMRIGHT") == 0) {
value = BOTTOMRIGHT;
} else {
/* Failed to parse */
return false;
}
return true;
}
return false;
}

bool
UIArea::LoadColor(rapidxml::xml_node<> *node, const char *name, Uint32 &value)
{
rapidxml::xml_node<> *child;

child = node->first_node("color", 0, false);
if (child) {
rapidxml::xml_attribute<> *attr;
int r = 0xFF, g = 0xFF, b = 0xFF;

LoadNumber(child, "r", r);
LoadNumber(child, "g", g);
LoadNumber(child, "b", b);
value = m_screen->MapRGB(r, g, b);
return true;
}
return false;
}
void
UIArea::GetAnchorLocation(AnchorLocation spot, int *x, int *y) const
{
Expand Down
6 changes: 6 additions & 0 deletions screenlib/UIArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ class UIArea : public ErrorBase
}

protected:
bool LoadBool(rapidxml::xml_node<> *node, const char *name, bool &value);
bool LoadNumber(rapidxml::xml_node<> *node, const char *name, int &value);
bool LoadString(rapidxml::xml_node<> *node, const char *name, char *&value);
bool LoadAnchorLocation(rapidxml::xml_node<> *node, const char *name, AnchorLocation &value);
bool LoadColor(rapidxml::xml_node<> *node, const char *name, Uint32 &value);

void GetAnchorLocation(AnchorLocation spot, int *x, int *y) const;
void CalculateAnchor(bool triggerRectChanged = true);
virtual void OnRectChanged();
Expand Down
61 changes: 0 additions & 61 deletions screenlib/UIElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
slouken@libsdl.org
*/

#include "SDL_FrameBuf.h"
#include "UIPanel.h"
#include "UIElement.h"

UIElementType UIElement::s_elementType;
Expand All @@ -45,62 +43,3 @@ UIElement::Load(rapidxml::xml_node<> *node, const UITemplates *templates)

return true;
}

bool
UIElement::LoadBool(rapidxml::xml_node<> *node, const char *name, bool &value)
{
rapidxml::xml_attribute<> *attr;

attr = node->first_attribute(name, 0, false);
if (attr) {
const char *text = attr->value();
if (*text == '\0' || *text == '0' ||
*text == 'f' || *text == 'F') {
value = false;
} else {
value = true;
}
}
}

bool
UIElement::LoadNumber(rapidxml::xml_node<> *node, const char *name, int &value)
{
rapidxml::xml_attribute<> *attr;

attr = node->first_attribute(name, 0, false);
if (attr) {
value = (int)strtol(attr->value(), NULL, 0);
}
}

bool
UIElement::LoadString(rapidxml::xml_node<> *node, const char *name, char *&value)
{
rapidxml::xml_attribute<> *attr;

attr = node->first_attribute(name, 0, false);
if (attr) {
if (value) {
SDL_free(value);
}
value = SDL_strdup(attr->value());
}
}

bool
UIElement::LoadColor(rapidxml::xml_node<> *node, const char *name, Uint32 &value)
{
rapidxml::xml_node<> *child;

child = node->first_node("color", 0, false);
if (child) {
rapidxml::xml_attribute<> *attr;
int r = 0xFF, g = 0xFF, b = 0xFF;

LoadNumber(child, "r", r);
LoadNumber(child, "g", g);
LoadNumber(child, "b", b);
value = m_screen->MapRGB(r, g, b);
}
}
6 changes: 0 additions & 6 deletions screenlib/UIElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ class UIElement : public UIBaseElement

virtual bool Load(rapidxml::xml_node<> *node, const UITemplates *templates);

protected:
bool LoadBool(rapidxml::xml_node<> *node, const char *name, bool &value);
bool LoadNumber(rapidxml::xml_node<> *node, const char *name, int &value);
bool LoadString(rapidxml::xml_node<> *node, const char *name, char *&value);
bool LoadColor(rapidxml::xml_node<> *node, const char *name, Uint32 &value);

protected:
static UIElementType s_elementType;

Expand Down
30 changes: 4 additions & 26 deletions screenlib/UIPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,10 @@ UIPanel::Load(rapidxml::xml_node<> *node, const UITemplates *templates)
return false;
}

attr = node->first_attribute("fullscreen", 0, false);
if (attr) {
const char *value = attr->value();
if (*value == '0' || *value == 'f' || *value == 'F') {
m_fullscreen = false;
} else {
m_fullscreen = true;
}
}
attr = node->first_attribute("cursor", 0, false);
if (attr) {
const char *value = attr->value();
if (*value == '0' || *value == 'f' || *value == 'F') {
m_cursorVisible = false;
} else {
m_cursorVisible = true;
}
}
attr = node->first_attribute("enterSound", 0, false);
if (attr) {
m_enterSound = atoi(attr->value());
}
attr = node->first_attribute("leaveSound", 0, false);
if (attr) {
m_leaveSound = atoi(attr->value());
}
LoadBool(node, "fullscreen", m_fullscreen);
LoadBool(node, "cursor", m_cursorVisible);
LoadNumber(node, "enterSound", m_enterSound);
LoadNumber(node, "leaveSound", m_leaveSound);

return true;
}
Expand Down

0 comments on commit 8db39ac

Please sign in to comment.