[calref/cboe]: Implement automatic height calculation for text labels

This commit is contained in:
ALONSO Laurent
2021-09-23 14:37:37 +02:00
committed by Celtic Minstrel
parent 62282b6cbf
commit 4707fd7532
4 changed files with 67 additions and 5 deletions

View File

@@ -3,7 +3,7 @@
<dialog defbtn='okay'>
<pict type='dlog' num='16' top='6' left='6'/>
<text top='4' left='136' width='265' height='16'>WELCOME TO OPEN BLADES OF EXILE!</text>
<text top='22' left='50' width='413' height='48'>
<text top='22' left='50' width='413'>
Welcome to the evolving world of Open Blades of Exile!<br/>
Open Blades of Exile is the open-source variant of the
Blades of Exile game developed by Spiderweb Software in
@@ -12,7 +12,7 @@
<text name='spidweb' relative='pos-in pos' rel-anchor='prev' top='0' left='16' width='390' height='12' colour='link' underline='true'>
http://www.spidweb.com/blades/opensource.html
</text>
<text relative='neg pos' rel-anchor='prev' top='12' left='16' width='413' height='36'>
<text relative='neg pos' rel-anchor='prev' top='12' left='16' width='413'>
Three exciting original scenarios await you, filled
with many hours of puzzles, fighting and adventure!
In addition to these, hundreds more written by
@@ -21,7 +21,7 @@
<text name='scen' relative='pos-in pos' rel-anchor='prev' top='0' left='16' width='390' height='12' colour='link' underline='true'>
http://openboe.com/scenarios
</text>
<text relative='neg pos' rel-anchor='prev' top='12' left='16' width='413' height='84'>
<text relative='neg pos' rel-anchor='prev' top='12' left='16' width='413'>
Even better, when you are through with those adventures,
you can make your own using the Blades of Exile scenario
editor!
@@ -36,7 +36,7 @@
<text name='forum' relative='pos-in pos' rel-anchor='prev' top = '0' left='16' width='390' height='12' colour='link' underline='true'>
http://spiderwebforums.ipbhost.com/
</text>
<text relative='neg pos' rel-anchor='prev' top='12' left='16' width='413' height='96'>
<text relative='neg pos' rel-anchor='prev' top='12' left='16' width='413'>
Also be sure to look at the Preferences Window, where
you can change animation speed, game difficulty, display
options and special effects settings.

View File

@@ -265,6 +265,8 @@ void cDialog::loadFromFile(std::string path){
}
}
prevCtrl = *inserted;
// Needed to correctly resolve relative positioning
inserted->second->recalcRect();
}
// Resolve relative positioning

View File

@@ -9,6 +9,7 @@
#include "message.hpp"
#include "mathutil.hpp"
#include "dialog.hpp"
#include <numeric>
extern sf::Texture bg_gworld;
@@ -85,6 +86,64 @@ void cTextMsg::validatePostParse(ticpp::Element& who, std::string fname, const s
cControl::validatePostParse(who, fname, attrs, nodes);
if(!attrs.count("color") && !attrs.count("colour") && parent->getBg() == cDialog::BG_DARK)
setColour(sf::Color::White);
if(attrs.count("width")) fixedWidth = true;
if(attrs.count("height")) fixedHeight = true;
}
void cTextMsg::recalcRect() {
if(fixedWidth && fixedHeight) return;
TextStyle style;
style.font = textFont;
style.pointSize = textSize;
style.underline = underlined;
style.lineHeight = textSize + 2;
std::string test = lbl;
size_t lines = 1, cur_line_chars = 0, max_line_chars = 0;
// Substitute | with newlines for measuring
for(auto& c : test) {
if(c == '|') {
c = '\n';
lines++;
max_line_chars = max(max_line_chars, cur_line_chars);
cur_line_chars = 0;
} else {
cur_line_chars++;
}
}
max_line_chars = max(max_line_chars, cur_line_chars);
std::vector<hilite_t> hilites;
std::vector<rectangle> rects;
hilites.emplace_back(0,test.size());
rectangle calc_rect = frame;
if(lines == 1) {
calc_rect.left += 3;
} else {
calc_rect.inset(4,4);
}
if(!fixedHeight) {
// Fix the width and calculate the height
calc_rect.height() = lines * style.lineHeight * 10;
} else if(!fixedWidth) {
// Fix the height and calculate the width
calc_rect.width() = 100 * max_line_chars;
} else return; // This case should be impossible, but just in case...
sf::RenderTexture temp;
temp.create(frame.width(), frame.height());
rectangle test_rect = calc_rect;
test_rect.offset(-test_rect.left, -test_rect.top);
rects = draw_string_hilite(temp, test_rect, lbl, style, hilites, sf::Color::Black);
if(rects.empty()) return;
// Basically take the the union of the rects, and add 8 to its height or width
rectangle combo = rects.back();
if(rects.size() > 1) {
combo = std::accumulate(rects.begin(), rects.end() - 1, combo, rectunion);
}
if(!fixedHeight) {
calc_rect.height() = combo.height() + 8;
} else if(!fixedWidth) {
calc_rect.width() = combo.width() + 8;
}
frame = calc_rect;
}
cTextMsg::cTextMsg(cDialog& parent) :

View File

@@ -38,6 +38,7 @@ public:
bool isScrollable();
virtual ~cTextMsg();
void draw();
void recalcRect() override;
/// @copydoc cControl::getSupportedHandlers
///
/// @todo Document possible handlers
@@ -52,6 +53,6 @@ private:
sf::Color color;
std::vector<boost::optional<std::string>> keyRefs;
std::string fromList;
bool underlined = false;
bool underlined = false, fixedWidth = false, fixedHeight = false;
};
#endif