Skip to content

Commit

Permalink
add very rudimentary right-alignment text option
Browse files Browse the repository at this point in the history
  • Loading branch information
NQNStudios committed Nov 10, 2024
1 parent 01ef169 commit a5eecd4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
10 changes: 8 additions & 2 deletions src/dialogxml/widgets/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/dialogxml/widgets/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ class cTextMsg : public cControl {
std::string msg;
void calculate_layout();
bool calculated = false;
bool right_align = false;
};
#endif
25 changes: 17 additions & 8 deletions src/gfx/render_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct text_params_t {
std::vector<snippet_t> 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) {
Expand Down Expand Up @@ -112,23 +113,25 @@ 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] == ' ')
last_word_break = i + 1;
}

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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down
9 changes: 5 additions & 4 deletions src/gfx/render_text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define BoE_RENDER_TEXT_HPP

#include <utility>
#include <tuple>
#include <vector>
#include <string>

Expand Down Expand Up @@ -37,8 +38,8 @@ struct TextStyle {
void applyTo(sf::Text& text);
};

// elements: std::make_pair(last_line_break, last_word_break)
typedef std::vector<std::pair<unsigned short, unsigned short>> break_info_t;
// elements: std::make_tuple(last_line_break, last_word_break, line_width)
typedef std::vector<std::tuple<unsigned short, unsigned short, unsigned short>> break_info_t;

struct snippet_t {
std::string text;
Expand All @@ -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<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, 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);

Expand Down

0 comments on commit a5eecd4

Please sign in to comment.