Mostly working now, just had to manually calculate the grid. Needs a cleanup but I'm moving on for now.

This commit is contained in:
Zed A. Shaw 2024-11-17 12:37:07 -05:00
parent 89a70f398a
commit 34c84343db
4 changed files with 65 additions and 56 deletions

View file

@ -43,7 +43,7 @@ SFMLRender::SFMLRender() :
$ui_text.setPosition(0,0);
$ui_text.setCharacterSize(UI_FONT_SIZE);
$ui_text.setFillColor(color(Value::LIGHT_MID));
sf::Glyph glyph = $font.getGlyph(L'', UI_FONT_SIZE, false);
sf::Glyph glyph = $font.getGlyph(UI_BASE_CHAR, UI_FONT_SIZE, false);
$ui_bounds = glyph.bounds;
}
@ -148,11 +148,22 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
});
}
inline sf::FloatRect draw_chunk(sf::RenderWindow& window, sf::Text& text, float x, float y, std::wstring &out) {
inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
sf::FloatRect ui_bounds, sf::Text& text,
sf::Color bgcolor, float x, float y, std::wstring &out)
{
text.setString(out);
text.setPosition(x, y);
sf::FloatRect bounds = text.getLocalBounds();
// need left,top,width,height for box to be accurate
text.setPosition({x, y});
// get a base character for the cell size
sf::FloatRect bounds(x, y, ui_bounds.width * out.size(), ui_bounds.height);
sf::RectangleShape backing({bounds.width, bounds.height});
backing.setFillColor(bgcolor);
backing.setPosition({bounds.left, bounds.top + BG_BOX_OFFSET});
#ifdef DEBUG
backing.setOutlineColor(int(y) % 4 ? sf::Color::Red : sf::Color::Yellow);
backing.setOutlineThickness(1);
#endif
window.draw(backing);
window.draw(text);
out.clear();
return bounds;
@ -162,10 +173,7 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star
std::wstring out;
float x = start_x;
float y = start_y;
enum State { START=1, BUILDING=2 };
enum Event { NL=1, CHAR=3, SKIP=5 };
State state = START;
Event event = CHAR;
sf::Color bgcolor = $default_bg;
// start with the default_fg until it's changed
$ui_text.setFillColor($default_fg);
@ -173,42 +181,38 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star
$ansi.parse(text,
[&](sf::Color fg, sf::Color bg){
if(out.size() > 0 ) {
auto bounds = draw_chunk($window, $ui_text, x, y, out);
auto bounds = draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out);
x += bounds.width;
}
bgcolor = bg;
$ui_text.setFillColor(fg);
},
[&](wchar_t tile) {
if(tile == '\r') event = SKIP;
else if(tile == '\n') event = NL;
else event = CHAR;
switch(tile) {
case '\r': break; // ignore it
case '\n': {
sf::FloatRect bounds;
switch(event) {
case SKIP: break; // ignore it
case NL: {
if(state == START) {
state = BUILDING;
} else if(state == BUILDING) {
sf::FloatRect bounds;
if(out.size() > 0) {
bounds = draw_chunk($window, $ui_text, x, y, out);
} else {
bounds = $ui_text.getLocalBounds();
}
y += bounds.height;
x = start_x; // reset to the original position
if(out.size() > 0) {
bounds = draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out);
} else {
bounds = $ui_text.getLocalBounds();
}
y += bounds.height;
x = start_x; // reset to the original position
}
break;
case CHAR:
state = BUILDING;
default:
out += tile;
break;
}
}
);
if(out.size() > 0) {
draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out);
}
}
void SFMLRender::draw_text(Panel &panel, bool with_border) {