New XML System, first pass.

This commit is contained in:
2023-07-12 10:34:51 -07:00
parent 1c7abbf140
commit 19c6575aaf
9 changed files with 126 additions and 156 deletions

View File

@ -122,6 +122,7 @@ void UILabel::rebufferQuads(const std::vector<struct UILabelText> newTexts) {
// Reset
lines.clear();
textureMap.clear();
// Determine font dimensions.
auto itText = newTexts.begin();

View File

@ -29,33 +29,33 @@ void UIRichTextLabel::onStart() {
}
std::function<void(Xml*)> parseChildren = [&](Xml *node) {
if(node->children.empty()) {
if(node->node == "root") return;
struct UILabelText text;
text.style = current;
text.text = node->value;
bufferTexts.push_back(text);
} else {
auto itNode = node->children.begin();
while(itNode != node->children.end()) {
auto child = *itNode;
assertTrue(child->node == "font");
auto itChildren = node->childNodes.begin();
while(itChildren != node->childNodes.end()) {
auto child = *itChildren;
if(child.nodeType == XML_NODE_TYPE_TEXT) {
struct UILabelText text;
text.style = current;
text.text = child.value;
bufferTexts.push_back(text);
} else if(child.nodeType == XML_NODE_TYPE_ELEMENT) {
auto node = child.child;
assertTrue(node->node == "font");
struct UILabelStyle style;
if(child->attributes.contains("font")) {
style.font = this->getGame()->assetManager.get<TrueTypeAsset>(child->attributes["font"]);
if(node->attributes.contains("font")) {
style.font = this->getGame()->assetManager.get<TrueTypeAsset>(node->attributes["font"]);
} else {
style.font = current.font;
}
if(child->attributes.contains("size")) {
style.size = std::stoi(child->attributes["size"]);
if(node->attributes.contains("size")) {
style.size = std::stoi(node->attributes["size"]);
} else {
style.size = current.size;
}
if(child->attributes.contains("style")) {
std::string s = child->attributes["style"];
if(node->attributes.contains("style")) {
std::string s = node->attributes["style"];
style.style = 0;
if(s.find("bold") != std::string::npos) style.style |= TRUE_TYPE_VARIANT_BOLD;
if(s.find("italic") != std::string::npos) style.style |= TRUE_TYPE_VARIANT_ITALICS;
@ -63,25 +63,23 @@ void UIRichTextLabel::onStart() {
style.style = current.style;
}
if(child->attributes.contains("color")) {
style.color = Color::fromString(child->attributes["color"]);
if(node->attributes.contains("color")) {
style.color = Color::fromString(node->attributes["color"]);
} else {
style.color = current.color;
}
styleStack.push_back(style);
current = style;
parseChildren(child);
parseChildren(node);
styleStack.pop_back();
current = styleStack.back();
++itNode;
}
++itChildren;
}
};
auto root = Xml::load("<root>" + ((std::string)this->richText) + "</root>");
auto root = Xml::load("<root><font font=\"font_main\">" + ((std::string)this->richText) + "</font></root>");
parseChildren(&root);
this->rebufferQuads(bufferTexts);
}, this->richText)();