Porytiles
Loading...
Searching...
No Matches
ascii_tile_printer.cpp
Go to the documentation of this file.
2
3#include <set>
4#include <sstream>
5#include <string>
6#include <utility>
7#include <vector>
8
12
13namespace {
14
15void push_to_stream(std::stringstream &ss, const std::string_view s, const std::size_t n)
16{
17 for (std::size_t i = 0; i < n; i++) {
18 ss << s;
19 }
20}
21
22void reset_stream(std::stringstream &ss)
23{
24 ss.clear();
25 ss.str(std::string{});
26}
27
43std::vector<std::string> render_metatile_with_highlights(
45 const std::set<std::pair<std::size_t, std::size_t>> &highlight_coords,
48{
49 std::vector<std::string> highlight{};
50 std::stringstream ss{};
51
52 auto styled_x = format->format(" {} ", porytiles2::FormatParam{"X", porytiles2::Style::bold | color});
53 auto styled_star = format->format(" {} ", porytiles2::FormatParam{"*", porytiles2::Style::bold});
54
55 for (std::size_t i = 0; i < porytiles2::metatile::side_length_pix; i++) {
56 for (std::size_t j = 0; j < porytiles2::metatile::side_length_pix; j++) {
57 bool is_in_subtile = false;
58 std::size_t subtile_row = 0;
59 std::size_t subtile_col = 0;
60
61 // Determine if (i, j) is in the target subtile and compute subtile-local coordinates
62 if (subtile == porytiles2::metatile::Subtile::northwest && i < 8 && j < 8) {
63 is_in_subtile = true;
64 subtile_row = i;
65 subtile_col = j;
66 }
67 else if (subtile == porytiles2::metatile::Subtile::northeast && i < 8 && j >= 8) {
68 is_in_subtile = true;
69 subtile_row = i;
70 subtile_col = j - 8;
71 }
72 else if (subtile == porytiles2::metatile::Subtile::southwest && i >= 8 && j < 8) {
73 is_in_subtile = true;
74 subtile_row = i - 8;
75 subtile_col = j;
76 }
77 else if (subtile == porytiles2::metatile::Subtile::southeast && i >= 8 && j >= 8) {
78 is_in_subtile = true;
79 subtile_row = i - 8;
80 subtile_col = j - 8;
81 }
82
83 if (is_in_subtile) {
84 // Check if this coordinate should be highlighted
85 if (highlight_coords.contains({subtile_row, subtile_col})) {
86 ss << format->format("{}", porytiles2::FormatParam{styled_x});
87 }
88 else {
89 ss << format->format("{}", porytiles2::FormatParam{styled_star});
90 }
91 }
92 else {
93 ss << " - ";
94 }
95
96 // If we're at the midpoint cell, add an extra space.
97 if (j == 7) {
98 ss << " ";
99 }
100
101 // Reset once this row is exhausted
102 if (j == 15) {
103 highlight.push_back(ss.str());
104 reset_stream(ss);
105 }
106 }
107
108 // Insert a spacer line between top and bottom tiles
109 if (i == 7) {
110 highlight.push_back(ss.str());
111 reset_stream(ss);
112 }
113 }
114
115 return highlight;
116}
117
118} // namespace
119
120namespace porytiles2 {
121
123 metatile::Subtile subtile, std::size_t row, std::size_t col, Style color) const
124{
125 std::set<std::pair<std::size_t, std::size_t>> highlight_coords{};
126 highlight_coords.insert({row, col});
127 return render_metatile_with_highlights(subtile, highlight_coords, color, format_);
128}
129
131 metatile::Subtile subtile, const std::vector<std::size_t> &indexes, Style color) const
132{
133 std::set<std::pair<std::size_t, std::size_t>> highlight_coords{};
134 for (std::size_t index : indexes) {
135 auto [row, col] = tile::index_to_row_col(index);
136 highlight_coords.insert({row, col});
137 }
138 return render_metatile_with_highlights(subtile, highlight_coords, color, format_);
139}
140
141} // namespace porytiles2
std::vector< std::string > print_metatile_highlight(metatile::Subtile subtile, std::size_t row, std::size_t col, Style color) const override
std::vector< std::string > print_metatile_highlights(metatile::Subtile subtile, const std::vector< std::size_t > &indexes, Style color) const override
A text parameter with associated styling for formatted output.
Abstract base class for applying text styling with context-aware formatting.
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > &params) const
Formats a string with styled parameters using fmtlib syntax.
constexpr std::size_t side_length_pix
Definition metatile.hpp:16
constexpr std::pair< std::size_t, std::size_t > index_to_row_col(std::size_t index)
Converts a linear index to row and column coordinates.
Style
Bitmask flags for text styling options.
@ bold
Bold text formatting.