make otml simpler and easier to use, improve error handling/exceptions

This commit is contained in:
Eduardo Bart
2011-08-19 15:53:23 -03:00
parent f9e7d52ac0
commit 033f14780d
32 changed files with 646 additions and 622 deletions

View File

@@ -37,10 +37,10 @@ void UIButton::loadStyleFromOTML(const OTMLNodePtr& styleNode)
if(OTMLNodePtr node = styleNode->get("state.down"))
loadStateStyle(m_statesStyle[ButtonDown], node);
m_text = styleNode->readAt("text", fw::empty_string);
m_text = styleNode->valueAt("text", fw::empty_string);
if(OTMLNodePtr node = styleNode->get("onClick")) {
g_lua.loadFunction(node->read<std::string>(), "@" + node->source() + "[" + node->tag() + "]");
g_lua.loadFunction(node->value<std::string>(), "@" + node->source() + "[" + node->tag() + "]");
luaSetField("onClick");
}
}
@@ -51,9 +51,9 @@ void UIButton::loadStateStyle(ButtonStateStyle& stateStyle, const OTMLNodePtr& s
stateStyle.image = BorderImage::loadFromOTML(node);
if(OTMLNodePtr node = stateStyleNode->get("image"))
stateStyle.image = Image::loadFromOTML(node);
stateStyle.textTranslate = stateStyleNode->readAt("text-translate", Point());
stateStyle.color = stateStyleNode->readAt("font-color", m_fontColor);
stateStyle.color = stateStyleNode->readAt("color", m_color);
stateStyle.textTranslate = stateStyleNode->valueAt("text-translate", Point());
stateStyle.color = stateStyleNode->valueAt("font-color", m_fontColor);
stateStyle.color = stateStyleNode->valueAt("color", m_color);
}
void UIButton::render()

View File

@@ -18,10 +18,10 @@ void UILabel::loadStyleFromOTML(const OTMLNodePtr& styleNode)
{
UIWidget::loadStyleFromOTML(styleNode);
m_text = styleNode->readAt("text", m_text);
m_text = styleNode->valueAt("text", m_text);
if(styleNode->hasChild("align"))
m_align = fw::translateAlignment(styleNode->readAt<std::string>("align"));
if(styleNode->hasChildAt("align"))
m_align = fw::translateAlignment(styleNode->valueAt("align"));
// auto resize if no size supplied
if(!m_text.empty() && !getGeometry().isValid())

View File

@@ -25,7 +25,7 @@ void UILineEdit::loadStyleFromOTML(const OTMLNodePtr& styleNode)
{
UIWidget::loadStyleFromOTML(styleNode);
setText(styleNode->readAt("text", getText()));
setText(styleNode->valueAt("text", getText()));
}
void UILineEdit::render()

View File

@@ -93,7 +93,7 @@ bool UIManager::importStyles(const std::string& file)
try {
OTMLDocumentPtr doc = OTMLDocument::parse(file);
for(const OTMLNodePtr& styleNode : doc->childNodes())
for(const OTMLNodePtr& styleNode : doc->children())
importStyleFromOTML(styleNode);
return true;
} catch(std::exception& e) {
@@ -129,14 +129,14 @@ void UIManager::importStyleFromOTML(const OTMLNodePtr& styleNode)
OTMLNodePtr UIManager::getStyle(const std::string& styleName)
{
if(boost::starts_with(styleName, "UI")) {
OTMLNodePtr node(new OTMLNode());
OTMLNodePtr node = OTMLNode::create();
node->writeAt("__widgetType", styleName);
return node;
}
auto it = m_styles.find(styleName);
if(it == m_styles.end())
throw std::logic_error(fw::mkstr("style '", styleName, "' is not a defined style"));
throw std::runtime_error(fw::mkstr("style '", styleName, "' is not a defined style"));
return m_styles[styleName];
}
@@ -145,7 +145,7 @@ UIWidgetPtr UIManager::loadUI(const std::string& file)
try {
OTMLDocumentPtr doc = OTMLDocument::parse(file);
UIWidgetPtr widget;
for(const OTMLNodePtr& node : doc->childNodes()) {
for(const OTMLNodePtr& node : doc->children()) {
std::string tag = node->tag();
// import styles in these files too
@@ -153,7 +153,7 @@ UIWidgetPtr UIManager::loadUI(const std::string& file)
importStyleFromOTML(node);
else {
if(widget)
throw OTMLException(node, "cannot have multiple main widgets in .otui files");
throw std::runtime_error("cannot have multiple main widgets in .otui files");
widget = loadWidgetFromOTML(node);
}
}
@@ -172,7 +172,7 @@ UIWidgetPtr UIManager::loadWidgetFromOTML(const OTMLNodePtr& widgetNode)
OTMLNodePtr styleNode = getStyle(widgetNode->tag())->clone();
styleNode->merge(widgetNode);
std::string widgetType = styleNode->readAt<std::string>("__widgetType");
std::string widgetType = styleNode->valueAt("__widgetType");
UIWidgetPtr widget;
if(widgetType == "UIWidget")
@@ -191,7 +191,7 @@ UIWidgetPtr UIManager::loadWidgetFromOTML(const OTMLNodePtr& widgetNode)
widget->loadStyleFromOTML(styleNode);
widget->updateGeometry();
for(const OTMLNodePtr& childNode : widgetNode->childNodes()) {
for(const OTMLNodePtr& childNode : widgetNode->children()) {
if(!childNode->isUnique())
widget->addChild(loadWidgetFromOTML(childNode));
}

View File

@@ -103,7 +103,7 @@ void UIWidget::loadStyleFromOTML(const OTMLNodePtr& styleNode)
assert(!m_destroyed);
// load styles used by all widgets
for(const OTMLNodePtr& node : styleNode->childNodes()) {
for(const OTMLNodePtr& node : styleNode->children()) {
// id
if(node->tag() == "id") {
setId(node->value());
@@ -121,48 +121,48 @@ void UIWidget::loadStyleFromOTML(const OTMLNodePtr& styleNode)
}
// font color
else if(node->tag() == "font-color") {
setFontColor(node->read<Color>());
setFontColor(node->value<Color>());
}
// color
else if(node->tag() == "color") {
setColor(node->read<Color>());
setColor(node->value<Color>());
}
// opacity
else if(node->tag() == "opacity") {
setOpacity(node->read<int>());
setOpacity(node->value<int>());
}
// size
else if(node->tag() == "size") {
resize(node->read<Size>());
resize(node->value<Size>());
}
else if(node->tag() == "width") {
setWidth(node->read<int>());
setWidth(node->value<int>());
}
else if(node->tag() == "height") {
setHeight(node->read<int>());
setHeight(node->value<int>());
}
// position
else if(node->tag() == "position") {
move(node->read<Point>());
move(node->value<Point>());
}
else if(node->tag() == "x") {
setX(node->read<int>());
setX(node->value<int>());
}
else if(node->tag() == "y") {
setY(node->read<int>());
setY(node->value<int>());
}
// margins
else if(node->tag() == "margin.left") {
setMarginLeft(node->read<int>());
setMarginLeft(node->value<int>());
}
else if(node->tag() == "margin.right") {
setMarginRight(node->read<int>());
setMarginRight(node->value<int>());
}
else if(node->tag() == "margin.top") {
setMarginTop(node->read<int>());
setMarginTop(node->value<int>());
}
else if(node->tag() == "margin.bottom") {
setMarginBottom(node->read<int>());
setMarginBottom(node->value<int>());
}
// anchors
else if(boost::starts_with(node->tag(), "anchors.")) {
@@ -193,7 +193,7 @@ void UIWidget::loadStyleFromOTML(const OTMLNodePtr& styleNode)
}
}
else if(node->tag() == "onLoad") {
g_lua.loadFunction(node->read<std::string>(), "@" + node->source() + "[" + node->tag() + "]");
g_lua.loadFunction(node->value<std::string>(), "@" + node->source() + "[" + node->tag() + "]");
luaSetField("onLoad");
}
}
@@ -437,8 +437,12 @@ UIWidgetPtr UIWidget::recursiveGetChildById(const std::string& childId)
for(const UIWidgetPtr& child : m_children) {
if(child->getId() == childId)
return child;
else
return child->recursiveGetChildById(childId);
}
for(const UIWidgetPtr& child : m_children) {
if(UIWidgetPtr subChild = child->recursiveGetChildById(childId)) {
if(subChild->getId() == childId)
return subChild;
}
}
}
return nullptr;
@@ -522,6 +526,9 @@ void UIWidget::addChild(const UIWidgetPtr& childToAdd)
{
assert(!m_destroyed);
if(!childToAdd)
return;
assert(!hasChild(childToAdd));
m_children.push_back(childToAdd);
childToAdd->setParent(asUIWidget());
@@ -538,6 +545,9 @@ void UIWidget::removeChild(const UIWidgetPtr& childToRemove)
{
assert(!m_destroyed);
if(!childToRemove)
return;
// defocus if needed
if(m_focusedChild == childToRemove)
focusChild(nullptr, ActiveFocusReason);

View File

@@ -23,9 +23,9 @@ void UIWindow::loadStyleFromOTML(const OTMLNodePtr& styleNode)
if(OTMLNodePtr headNode = styleNode->get("head")) {
if(OTMLNodePtr node = headNode->get("border-image"))
m_headImage = BorderImage::loadFromOTML(node);
m_headHeight = headNode->readAt("height", m_headImage->getDefaultSize().height());
m_headMargin = headNode->readAt("margin", 0);
m_titleAlign = fw::translateAlignment(headNode->readAt("text align", std::string("center")));
m_headHeight = headNode->valueAt("height", m_headImage->getDefaultSize().height());
m_headMargin = headNode->valueAt("margin", 0);
m_titleAlign = fw::translateAlignment(headNode->valueAt("text align", std::string("center")));
} else {
m_headHeight = 0;
m_headMargin = 0;
@@ -37,7 +37,7 @@ void UIWindow::loadStyleFromOTML(const OTMLNodePtr& styleNode)
m_bodyImage = BorderImage::loadFromOTML(node);
}
m_title = styleNode->readAt("title", fw::empty_string);
m_title = styleNode->valueAt("title", fw::empty_string);
}
void UIWindow::render()