add very rudimentary right-alignment text option

This commit is contained in:
2024-11-10 13:00:14 -06:00
parent af2253e5ce
commit a551da6299
4 changed files with 30 additions and 14 deletions

View File

@@ -53,12 +53,18 @@ bool cTextMsg::parseAttribute(ticpp::Attribute& attr, std::string tagName, std::
throw xBadVal(tagName, attr.Name(), val, attr.Row(), attr.Column(), fname); throw xBadVal(tagName, attr.Name(), val, attr.Row(), attr.Column(), fname);
} }
return true; return true;
} else if(attr.Name() == "underline") { }else if(attr.Name() == "underline"){
std::string val = attr.Value(); std::string val = attr.Value();
if(val == "true") underlined = true; if(val == "true") underlined = true;
else if(val == "false") underlined = false; else if(val == "false") underlined = false;
else throw xBadVal(tagName, attr.Name(), val, attr.Row(), attr.Column(), fname); else throw xBadVal(tagName, attr.Name(), val, attr.Row(), attr.Column(), fname);
return true; return true;
}else if(attr.Name() == "align"){
std::string val = attr.Value();
if(val == "right") right_align = true;
else if(val == "left") right_align = false;
else throw xBadVal(tagName, attr.Name(), val, attr.Row(), attr.Column(), fname);
return true;
} }
return cControl::parseAttribute(attr, tagName, fname); return cControl::parseAttribute(attr, tagName, fname);
} }
@@ -233,7 +239,7 @@ void cTextMsg::draw(){
} }
style.colour = draw_color; style.colour = draw_color;
if (!calculated) calculate_layout(); if (!calculated) calculate_layout();
win_draw_string(*inWindow,to_rect,msg,text_mode,style,break_info); win_draw_string(*inWindow,to_rect,msg,text_mode,style,break_info,right_align);
} }
} }

View File

@@ -63,5 +63,6 @@ private:
std::string msg; std::string msg;
void calculate_layout(); void calculate_layout();
bool calculated = false; bool calculated = false;
bool right_align = false;
}; };
#endif #endif

View File

@@ -48,6 +48,7 @@ struct text_params_t {
std::vector<snippet_t> snippets; std::vector<snippet_t> snippets;
// Pre-calculated line wrapping: // Pre-calculated line wrapping:
break_info_t break_info; break_info_t break_info;
bool right_align = false;
}; };
static void push_snippets(size_t start, size_t end, text_params_t& options, size_t& iHilite, const std::string& str, location loc) { static void push_snippets(size_t start, size_t end, text_params_t& options, size_t& iHilite, const std::string& str, location loc) {
@@ -110,13 +111,14 @@ break_info_t calculate_line_wrapping(rectangle dest_rect, std::string str, TextS
short i; short i;
for(i = 0; text_len(i) != text_len(i + 1) && i < str_len; i++) { for(i = 0; text_len(i) != text_len(i + 1) && i < str_len; i++) {
if(((text_len(i) - text_len(last_line_break) > (dest_rect.width() - 6)) unsigned short line_width = text_len(i) - text_len(last_line_break);
if(((line_width > (dest_rect.width() - 6))
&& (last_word_break >= last_line_break)) || (str[i] == '|')) { && (last_word_break >= last_line_break)) || (str[i] == '|')) {
if(str[i] == '|') { if(str[i] == '|') {
last_word_break = i + 1; last_word_break = i + 1;
} else if(last_line_break == last_word_break) } else if(last_line_break == last_word_break)
last_word_break = i; last_word_break = i;
break_info.push_back(std::make_pair(last_line_break, last_word_break)); break_info.push_back(std::make_tuple(last_line_break, last_word_break, line_width));
last_line_break = last_word_break; last_line_break = last_word_break;
} }
if(str[i] == ' ') if(str[i] == ' ')
@@ -124,9 +126,10 @@ break_info_t calculate_line_wrapping(rectangle dest_rect, std::string str, TextS
} }
if(i - last_line_break > 0) { if(i - last_line_break > 0) {
unsigned short line_width = text_len(i) - text_len(last_line_break);
std::string snippet = str.substr(last_line_break); std::string snippet = str.substr(last_line_break);
if(!snippet.empty()) if(!snippet.empty())
break_info.push_back(std::make_pair(last_line_break, str.length() + 1)); break_info.push_back(std::make_tuple(last_line_break, str.length() + 1, line_width));
} }
return break_info; return break_info;
@@ -179,9 +182,13 @@ static void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,st
moveTo = location(dest_rect.left + adjust_x, dest_rect.top + adjust_y); moveTo = location(dest_rect.left + adjust_x, dest_rect.top + adjust_y);
for(auto break_info_pair : break_info){ for(auto break_info_tuple : break_info){
push_snippets(break_info_pair.first, break_info_pair.second, options, iHilite, str, moveTo); if(options.right_align){
moveTo.x += (dest_rect.width() - std::get<2>(break_info_tuple));
}
push_snippets(std::get<0>(break_info_tuple), std::get<1>(break_info_tuple), options, iHilite, str, moveTo);
moveTo.y += line_height; moveTo.y += line_height;
moveTo.x = dest_rect.left;
} }
} else { } else {
switch(mode) { switch(mode) {
@@ -229,16 +236,17 @@ static void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,st
} }
} }
void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style) { void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style,bool right_align) {
break_info_t break_info; break_info_t break_info;
win_draw_string(dest_window, dest_rect, str, mode, style, break_info); win_draw_string(dest_window, dest_rect, str, mode, style, break_info, right_align);
} }
void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style,break_info_t break_info) { void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style,break_info_t break_info,bool right_align) {
text_params_t params; text_params_t params;
params.mode = mode; params.mode = mode;
params.style = style; params.style = style;
params.break_info = break_info; params.break_info = break_info;
params.right_align = right_align;
win_draw_string(dest_window, dest_rect, str, params); win_draw_string(dest_window, dest_rect, str, params);
} }

View File

@@ -10,6 +10,7 @@
#define BoE_RENDER_TEXT_HPP #define BoE_RENDER_TEXT_HPP
#include <utility> #include <utility>
#include <tuple>
#include <vector> #include <vector>
#include <string> #include <string>
@@ -37,8 +38,8 @@ struct TextStyle {
void applyTo(sf::Text& text); void applyTo(sf::Text& text);
}; };
// elements: std::make_pair(last_line_break, last_word_break) // elements: std::make_tuple(last_line_break, last_word_break, line_width)
typedef std::vector<std::pair<unsigned short, unsigned short>> break_info_t; typedef std::vector<std::tuple<unsigned short, unsigned short, unsigned short>> break_info_t;
struct snippet_t { struct snippet_t {
std::string text; std::string text;
@@ -55,8 +56,8 @@ enum class eTextMode {
std::vector<rectangle> draw_string_hilite(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,TextStyle style,std::vector<hilite_t> hilites,sf::Color hiliteClr); std::vector<rectangle> draw_string_hilite(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,TextStyle style,std::vector<hilite_t> hilites,sf::Color hiliteClr);
std::vector<snippet_t> draw_string_sel(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,TextStyle style,std::vector<hilite_t> hilites,sf::Color hiliteClr); std::vector<snippet_t> draw_string_sel(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,TextStyle style,std::vector<hilite_t> hilites,sf::Color hiliteClr);
void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style); void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style,bool right_align = false);
void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style, break_info_t break_info); void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style, break_info_t break_info,bool right_align = false);
break_info_t calculate_line_wrapping(rectangle dest_rect, std::string str, TextStyle style); break_info_t calculate_line_wrapping(rectangle dest_rect, std::string str, TextStyle style);
size_t string_length(std::string str, TextStyle style, short* height = nullptr); size_t string_length(std::string str, TextStyle style, short* height = nullptr);