help_impl.cpp cleanup

This commit is contained in:
Subhraman Sarkar 2024-09-19 15:36:02 +05:30 committed by Celtic Minstrel
parent 069a0c659d
commit f611188cd4
2 changed files with 24 additions and 117 deletions

View file

@ -415,11 +415,11 @@ std::vector<topic> generate_time_of_day_topics(const bool /*sort_generated*/)
for (const time_of_day& time : times)
{
const std::string id = "time_of_day_" + time.id;
const std::string image = "<img>src='" + time.image + "'</img>";
const std::string image_lawful = "<img>src='icons/alignments/alignment_lawful_30.png'</img>";
const std::string image_neutral = "<img>src='icons/alignments/alignment_neutral_30.png'</img>";
const std::string image_chaotic = "<img>src='icons/alignments/alignment_chaotic_30.png'</img>";
const std::string image_liminal = "<img>src='icons/alignments/alignment_liminal_30.png'</img>";
const std::string image = "<img src='" + time.image + "'/>";
const std::string image_lawful = "<img src='icons/alignments/alignment_lawful_30.png'/>";
const std::string image_neutral = "<img src='icons/alignments/alignment_neutral_30.png'/>";
const std::string image_chaotic = "<img src='icons/alignments/alignment_chaotic_30.png'/>";
const std::string image_liminal = "<img src='icons/alignments/alignment_liminal_30.png'/>";
std::stringstream text;
const int lawful_bonus = generic_combat_modifier(time.lawful_bonus, unit_alignments::type::lawful, false, resources::tod_manager->get_max_liminal_bonus());
@ -427,10 +427,10 @@ std::vector<topic> generate_time_of_day_topics(const bool /*sort_generated*/)
const int chaotic_bonus = generic_combat_modifier(time.lawful_bonus, unit_alignments::type::chaotic, false, resources::tod_manager->get_max_liminal_bonus());
const int liminal_bonus = generic_combat_modifier(time.lawful_bonus, unit_alignments::type::liminal, false, resources::tod_manager->get_max_liminal_bonus());
toplevel << make_link(time.name.str(), id) << jump_to(160) << image << jump(30) <<
image_lawful << time_of_day_bonus_colored(lawful_bonus) << jump_to(390) <<
image_neutral << time_of_day_bonus_colored(neutral_bonus) << jump_to(450) <<
image_chaotic << time_of_day_bonus_colored(chaotic_bonus) << jump_to(520) <<
toplevel << make_link(time.name.str(), id) << " " << image << " " <<
image_lawful << time_of_day_bonus_colored(lawful_bonus) << " " <<
image_neutral << time_of_day_bonus_colored(neutral_bonus) << " " <<
image_chaotic << time_of_day_bonus_colored(chaotic_bonus) << " " <<
image_liminal << time_of_day_bonus_colored(liminal_bonus) << '\n';
text << image << '\n' << time.description.str() << '\n' <<
@ -1857,74 +1857,6 @@ bool is_valid_id(const std::string &id) {
return true;
}
// Return the width for the image with filename.
unsigned image_width(const std::string &filename)
{
image::locator loc(filename);
surface surf(image::get_surface(loc));
if (surf != nullptr) {
return surf->w;
}
return 0;
}
void push_tab_pair(std::vector<help::item> &v, const std::string &s, const utils::optional<std::string> &image, unsigned padding)
{
help::item item(s, font::pango_line_width(s, normal_font_size));
if (image) {
// If the image doesn't exist, don't add padding.
auto width = image_width(*image);
padding = (width ? padding : 0);
item.first = "<img>src='" + *image + "'</img>" + (padding ? jump(padding) : "") + s;
item.second += width + padding;
}
v.emplace_back(item);
}
std::string generate_table(const table_spec &tab, const unsigned int spacing)
{
table_spec::const_iterator row_it;
std::vector<std::pair<std::string, unsigned>>::const_iterator col_it;
unsigned int num_cols = 0;
for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
if (row_it->size() > num_cols) {
num_cols = row_it->size();
}
}
std::vector<unsigned int> col_widths(num_cols, 0);
// Calculate the width of all columns, including spacing.
for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
unsigned int col = 0;
for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
if (col_widths[col] < col_it->second + spacing) {
col_widths[col] = col_it->second + spacing;
}
++col;
}
}
std::vector<unsigned int> col_starts(num_cols);
// Calculate the starting positions of all columns
for (unsigned int i = 0; i < num_cols; ++i) {
unsigned int this_col_start = 0;
for (unsigned int j = 0; j < i; ++j) {
this_col_start += col_widths[j];
}
col_starts[i] = this_col_start;
}
std::stringstream ss;
for (row_it = tab.begin(); row_it != tab.end(); ++row_it) {
unsigned int col = 0;
for (col_it = row_it->begin(); col_it != row_it->end(); ++col_it) {
ss << jump_to(col_starts[col]) << col_it->first;
++col;
}
ss << "\n";
}
return ss.str();
}
/** Prepend all chars with meaning inside attributes with a backslash. */
std::string escape(const std::string &s)
{

View file

@ -304,9 +304,11 @@ const topic *find_topic(const section &sec, const std::string &id);
const section *find_section(const section &sec, const std::string &id);
section *find_section(section &sec, const std::string &id);
/// Parse a xml style marked up text string. Return a config with the different parts of the
/// text. Each markup item is a separate part while the text between
/// markups are separate parts.
/**
* Parse a xml style marked up text string. Return a config with the different parts of the
* text. Each markup item is a separate part while the text between
* markups are separate parts.
*/
config parse_text(const std::string &text);
/** Make a best effort to word wrap s. All parts are less than width. */
@ -366,49 +368,22 @@ bool is_visible_id(const std::string &id);
*/
bool is_valid_id(const std::string &id);
// Helpers for making generation of topics easier.
// Helpers for making generation of topics easier.
inline std::string make_link(const std::string& text, const std::string& dst)
{
// some sorting done on list of links may rely on the fact that text is first
return "<ref>text='" + help::escape(text) + "' dst='" + help::escape(dst) + "'</ref>";
}
inline std::string jump_to(const unsigned pos)
{
std::stringstream ss;
ss << "<jump>to=" << pos << "</jump>";
return ss.str();
}
inline std::string jump(const unsigned amount)
{
std::stringstream ss;
ss << "<jump>amount=" << amount << "</jump>";
return ss.str();
}
{
// some sorting done on list of links may rely on the fact that text is first
return "<ref dst='" + help::escape(dst) + "'>" + help::escape(text) + "</ref>";
}
inline std::string bold(const std::string &s)
{
std::stringstream ss;
ss << "<bold>text='" << help::escape(s) << "'</bold>";
return ss.str();
}
{
std::stringstream ss;
ss << "<b>" << help::escape(s) << "</b>";
return ss.str();
}
// A string to be displayed and its width.
typedef std::pair< std::string, unsigned > item;
typedef std::vector<std::vector<help::item>> table_spec;
// Create a table using the table specs. Return markup with jumps
// that create a table. The table spec contains a vector with
// vectors with pairs. The pairs are the markup string that should
// be in a cell, and the width of that cell.
std::string generate_table(const table_spec &tab, const unsigned int spacing=font::relative_size(20));
// Return the width for the image with filename.
unsigned image_width(const std::string &filename);
// Add to the vector v an help::item for the string s, preceded by the given image if any.
void push_tab_pair(std::vector<help::item> &v, const std::string &s, const utils::optional<std::string> &image = {}, unsigned padding = 0);
} // end namespace help