From a5eecd43114a4ca15572e02a23bb4bae0b00e456 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 10 Nov 2024 13:00:14 -0600 Subject: [PATCH] add very rudimentary right-alignment text option --- src/dialogxml/widgets/message.cpp | 10 ++++++++-- src/dialogxml/widgets/message.hpp | 1 + src/gfx/render_text.cpp | 25 +++++++++++++++++-------- src/gfx/render_text.hpp | 9 +++++---- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/dialogxml/widgets/message.cpp b/src/dialogxml/widgets/message.cpp index 7ab8b3c34..618749180 100644 --- a/src/dialogxml/widgets/message.cpp +++ b/src/dialogxml/widgets/message.cpp @@ -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); } return true; - } else if(attr.Name() == "underline") { + }else if(attr.Name() == "underline"){ std::string val = attr.Value(); if(val == "true") underlined = true; else if(val == "false") underlined = false; else throw xBadVal(tagName, attr.Name(), val, attr.Row(), attr.Column(), fname); 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); } @@ -233,7 +239,7 @@ void cTextMsg::draw(){ } style.colour = draw_color; 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); } } diff --git a/src/dialogxml/widgets/message.hpp b/src/dialogxml/widgets/message.hpp index 34ddaeecc..7098a7213 100644 --- a/src/dialogxml/widgets/message.hpp +++ b/src/dialogxml/widgets/message.hpp @@ -63,5 +63,6 @@ class cTextMsg : public cControl { std::string msg; void calculate_layout(); bool calculated = false; + bool right_align = false; }; #endif diff --git a/src/gfx/render_text.cpp b/src/gfx/render_text.cpp index 03385671c..6caea9aaa 100644 --- a/src/gfx/render_text.cpp +++ b/src/gfx/render_text.cpp @@ -48,6 +48,7 @@ struct text_params_t { std::vector snippets; // Pre-calculated line wrapping: 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) { @@ -112,13 +113,14 @@ break_info_t calculate_line_wrapping(rectangle dest_rect, std::string str, TextS short 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] == '|')) { if(str[i] == '|') { last_word_break = i + 1; } else if(last_line_break == last_word_break) 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; } if(str[i] == ' ') @@ -126,9 +128,10 @@ break_info_t calculate_line_wrapping(rectangle dest_rect, std::string str, TextS } 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); 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; @@ -177,9 +180,14 @@ 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); - for(auto break_info_pair : break_info){ - push_snippets(break_info_pair.first, break_info_pair.second, options, iHilite, str, moveTo); + for(auto break_info_tuple : break_info){ + if(options.right_align){ + moveTo.x += (dest_rect.width() - std::get<2>(break_info_tuple)); + std::cout << "x: " << moveTo.x << std::endl; + } + push_snippets(std::get<0>(break_info_tuple), std::get<1>(break_info_tuple), options, iHilite, str, moveTo); moveTo.y += line_height; + moveTo.x = dest_rect.left; } } else { switch(mode) { @@ -227,16 +235,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; - 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; params.mode = mode; params.style = style; params.break_info = break_info; + params.right_align = right_align; win_draw_string(dest_window, dest_rect, str, params); } diff --git a/src/gfx/render_text.hpp b/src/gfx/render_text.hpp index c556bf906..d8663ab69 100644 --- a/src/gfx/render_text.hpp +++ b/src/gfx/render_text.hpp @@ -10,6 +10,7 @@ #define BoE_RENDER_TEXT_HPP #include +#include #include #include @@ -37,8 +38,8 @@ struct TextStyle { void applyTo(sf::Text& text); }; -// elements: std::make_pair(last_line_break, last_word_break) -typedef std::vector> break_info_t; +// elements: std::make_tuple(last_line_break, last_word_break, line_width) +typedef std::vector> break_info_t; struct snippet_t { std::string text; @@ -55,8 +56,8 @@ enum class eTextMode { std::vector draw_string_hilite(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,TextStyle style,std::vector hilites,sf::Color hiliteClr); std::vector draw_string_sel(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,TextStyle style,std::vector 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, break_info_t break_info); +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,bool right_align = false); 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);