DialogXML: Support <key> in <text> with optional ref=

This commit is contained in:
2020-02-23 23:02:16 -05:00
parent 3a193afb98
commit c8549ca680
4 changed files with 39 additions and 9 deletions

View File

@@ -64,6 +64,13 @@ bool cTextMsg::parseContent(ticpp::Node& content, int n, std::string tagName, st
// TODO: De-magic the | character
text += '|'; // because vertical bar is replaced by a newline when drawing strings
return true;
} else if(content.Value() == "key") {
auto elem = dynamic_cast<ticpp::Element&>(content);
if(elem.HasAttribute("ref"))
keyRefs.push_back(elem.GetAttribute("ref"));
else keyRefs.push_back(boost::none);
text += '\a';
return true;
}
return cControl::parseContent(content, n, tagName, fname, text);
}
@@ -120,15 +127,23 @@ void cTextMsg::draw(){
draw_color.g = 256 - draw_color.g;
draw_color.b = 256 - draw_color.b;
}
std::string msg = lbl;
for(const auto& key : keyRefs) {
size_t pos = msg.find_first_of('\a');
if(pos == std::string::npos) break;
if(key && !parent->hasControl(*key)) continue;
cControl& ctrl = key ? parent->getControl(*key) : *this;
msg.replace(pos, 1, ctrl.getAttachedKeyDescription());
}
style.colour = draw_color;
if(to_rect.bottom - to_rect.top < 20) { // essentially, it's a single line
style.lineHeight = 12;
to_rect.left += 3;
win_draw_string(*inWindow,to_rect,lbl,eTextMode::LEFT_BOTTOM,style);
win_draw_string(*inWindow,to_rect,msg,eTextMode::LEFT_BOTTOM,style);
}else {
style.lineHeight = textSize + 2;
to_rect.inset(4,4);
win_draw_string(*inWindow,to_rect,lbl,eTextMode::WRAP,style);
win_draw_string(*inWindow,to_rect,msg,eTextMode::WRAP,style);
}
}
}

View File

@@ -15,6 +15,7 @@
#include <SFML/Graphics.hpp>
#include <string>
#include <boost/optional.hpp>
#include "control.hpp"
#include "render_text.hpp"
@@ -49,6 +50,7 @@ private:
short textSize;
eFont textFont;
sf::Color color;
std::vector<boost::optional<std::string>> keyRefs;
std::string fromList;
};
#endif