A barely working tool to find font characters and pick their color.
This commit is contained in:
parent
6b3ce5eb3d
commit
0edd948101
17 changed files with 406 additions and 72 deletions
45
render.cpp
45
render.cpp
|
@ -32,7 +32,7 @@ SFMLRender::SFMLRender() :
|
|||
$ui_text.setCharacterSize($config.ui_font_size);
|
||||
$ui_text.setFillColor(ColorValue::LIGHT_MID);
|
||||
sf::Glyph glyph = $font.getGlyph($config.ui_base_char, $config.ui_font_size, false);
|
||||
$ui_bounds = glyph.bounds;
|
||||
$text_bounds = glyph.bounds;
|
||||
}
|
||||
|
||||
sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) {
|
||||
|
@ -62,18 +62,18 @@ void SFMLRender::resize_grid(int new_size, Panel &panel_out) {
|
|||
$sprites.clear(); // need to reset the sprites for the new size
|
||||
$line_spacing = $font.getLineSpacing($map_font_size);
|
||||
$bg_sprite = get_text_sprite($config.bg_tile);
|
||||
$bg_bounds = $bg_sprite.getLocalBounds();
|
||||
$grid_bounds = $bg_sprite.getLocalBounds();
|
||||
panel_out.resize(view_x, view_y);
|
||||
}
|
||||
|
||||
inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect bg_bounds, float &width_delta, float &height_delta) {
|
||||
inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect grid_bounds, float &width_delta, float &height_delta) {
|
||||
// BUG: I think I could create a struct that kept this info for all sprites loaded
|
||||
// should look into caching all this instead of calcing it each time
|
||||
sp_bounds = sprite.getLocalBounds();
|
||||
|
||||
// calculate where to center the sprite, but only if it's smaller
|
||||
width_delta = bg_bounds.width > sp_bounds.width ? (bg_bounds.width - sp_bounds.width) / 2 : 0;
|
||||
height_delta = bg_bounds.height > sp_bounds.width ? (bg_bounds.height - sp_bounds.height) / 2 : 0;
|
||||
width_delta = grid_bounds.width > sp_bounds.width ? (grid_bounds.width - sp_bounds.width) / 2 : 0;
|
||||
height_delta = grid_bounds.height > sp_bounds.width ? (grid_bounds.height - sp_bounds.height) / 2 : 0;
|
||||
}
|
||||
|
||||
void SFMLRender::render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y) {
|
||||
|
@ -106,7 +106,7 @@ void SFMLRender::render_grid(const std::wstring &text, sf::Color default_fg, sf:
|
|||
// only get a new sprite if the tile changed
|
||||
if(last_tile != tile) {
|
||||
sprite = get_text_sprite(tile);
|
||||
configure_tile(sprite, sp_bounds, $bg_bounds, width_delta, height_delta);
|
||||
configure_tile(sprite, sp_bounds, $grid_bounds, width_delta, height_delta);
|
||||
last_tile = tile; // update last tile seen
|
||||
}
|
||||
|
||||
|
@ -129,13 +129,13 @@ void SFMLRender::render_grid(const std::wstring &text, sf::Color default_fg, sf:
|
|||
}
|
||||
|
||||
inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
|
||||
sf::FloatRect ui_bounds, sf::Text& text, sf::Color default_bg,
|
||||
sf::FloatRect text_bounds, sf::Text& text, sf::Color default_bg,
|
||||
sf::Color bgcolor, int bg_box_offset, float x, float y, std::wstring &out)
|
||||
{
|
||||
text.setString(out);
|
||||
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::FloatRect bounds(x, y, text_bounds.width * out.size(), text_bounds.height);
|
||||
|
||||
if(default_bg != bgcolor) {
|
||||
sf::RectangleShape backing({bounds.width, bounds.height});
|
||||
|
@ -162,7 +162,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, sf:
|
|||
[&](auto fg, auto bg) {
|
||||
if(out.size() > 0 ) {
|
||||
auto bounds = draw_chunk($window,
|
||||
$ui_bounds, $ui_text,
|
||||
$text_bounds, $ui_text,
|
||||
default_bg, cur_bg, $config.bg_box_offset, x, y, out);
|
||||
x += bounds.width;
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, sf:
|
|||
sf::FloatRect bounds;
|
||||
|
||||
if(out.size() > 0) {
|
||||
bounds = draw_chunk($window, $ui_bounds,
|
||||
bounds = draw_chunk($window, $text_bounds,
|
||||
$ui_text, default_bg, cur_bg, $config.bg_box_offset, x, y, out);
|
||||
} else {
|
||||
bounds = $ui_text.getLocalBounds();
|
||||
|
@ -194,7 +194,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, sf:
|
|||
);
|
||||
|
||||
if(out.size() > 0) {
|
||||
draw_chunk($window, $ui_bounds, $ui_text, default_bg, cur_bg, $config.bg_box_offset, x, y, out);
|
||||
draw_chunk($window, $text_bounds, $ui_text, default_bg, cur_bg, $config.bg_box_offset, x, y, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -205,8 +205,7 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, sf:
|
|||
void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) {
|
||||
const std::wstring &panelout = panel.to_string();
|
||||
|
||||
// BUG: ui vs bg doesn't make sense. maybe grid vs. text?
|
||||
auto bounds = panel.grid ? $bg_bounds : $ui_bounds;
|
||||
auto bounds = panel.grid ? $grid_bounds : $text_bounds;
|
||||
|
||||
sf::RectangleShape backing(
|
||||
sf::Vector2f(bounds.width * panel.width + panel.border_px,
|
||||
|
@ -229,13 +228,23 @@ void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) {
|
|||
}
|
||||
}
|
||||
|
||||
Point SFMLRender::mouse_position() {
|
||||
bool SFMLRender::mouse_position(Panel &panel, Point &out) {
|
||||
sf::Vector2i pos = sf::Mouse::getPosition($window);
|
||||
auto bounds = panel.grid ? $grid_bounds : $text_bounds;
|
||||
|
||||
return {
|
||||
size_t(pos.x / $ui_bounds.width),
|
||||
size_t(pos.y / $ui_bounds.height)
|
||||
};
|
||||
if(pos.x >= panel.x && pos.y >= panel.y
|
||||
&& pos.x <= (panel.x + panel.width * bounds.width)
|
||||
&& pos.y <= (panel.y + panel.height * bounds.height))
|
||||
{
|
||||
out = {
|
||||
size_t((pos.x - panel.x) / bounds.width),
|
||||
size_t((pos.y - panel.y) / bounds.height)
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void SFMLRender::init_terminal() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue