Removed hardcoded enum for report generators. Used their theme names instead.

This commit is contained in:
Guillaume Melquiond 2010-12-26 20:00:54 +00:00
parent 1160a2b5e1
commit f7dae4410c
7 changed files with 118 additions and 199 deletions

View file

@ -129,8 +129,6 @@ display::display(CVideo& video, const gamemap* map, const config& theme_cfg, con
set_idle_anim_rate(preferences::idle_anim_rate());
std::fill(reportRects_,reportRects_+reports::NUM_REPORTS,empty_rect);
image::set_zoom(zoom_);
}
@ -1929,11 +1927,9 @@ void display::redraw_everything()
invalidateGameStatus_ = true;
for(size_t n = 0; n != reports::NUM_REPORTS; ++n) {
reportRects_[n] = empty_rect;
reportSurfaces_[n].assign(NULL);
reports_[n] = reports::report();
}
reportRects_.clear();
reportSurfaces_.clear();
reports_.clear();
bounds_check_position();
@ -2222,25 +2218,24 @@ void display::draw_image_for_report(surface& img, SDL_Rect& rect)
}
}
void display::refresh_report(reports::TYPE report_num, reports::report report)
void display::refresh_report(std::string const &report_name, reports::report report)
{
const theme::status_item* const item = theme_.get_status_item(reports::report_name(report_num));
const theme::status_item *item = theme_.get_status_item(report_name);
if (!item) {
reportSurfaces_[report_num].assign(NULL);
reportSurfaces_[report_name].assign(NULL);
return;
}
SDL_Rect &rect = reportRects_[report_num];
SDL_Rect &rect = reportRects_[report_name];
const SDL_Rect &new_rect = item->location(screen_area());
surface &surf = reportSurfaces_[report_name];
// Report and its location is unchanged since last time. Do nothing.
if (rect == new_rect && reports_[report_num] == report) {
if (surf && rect == new_rect && reports_[report_name] == report) {
return;
}
reports_[report_num] = report;
surface &surf = reportSurfaces_[report_num];
reports_[report_name] = report;
if (surf) {
sdl_blit(surf, NULL, screen_.getSurface(), &rect);
@ -2248,7 +2243,7 @@ void display::refresh_report(reports::TYPE report_num, reports::report report)
}
// If the rectangle has just changed, assign the surface to it
if (new_rect != rect || !surf)
if (!surf || new_rect != rect)
{
surf.assign(NULL);
rect = new_rect;
@ -2259,7 +2254,7 @@ void display::refresh_report(reports::TYPE report_num, reports::report report)
// unless they are transperant, but that is done later).
if (rect.w > 0 && rect.h > 0) {
surf.assign(get_surface_portion(screen_.getSurface(), rect));
if (reportSurfaces_[report_num] == NULL) {
if (reportSurfaces_[report_name] == NULL) {
ERR_DP << "Could not backup background for report!\n";
}
}

View file

@ -264,7 +264,7 @@ public:
void create_buttons();
void invalidate_theme() { panelsDrawn_ = false; }
void refresh_report(reports::TYPE report_num, reports::report report);
void refresh_report(std::string const &report_name, reports::report report);
// Will be overridden in the display subclass
virtual void draw_minimap_units() {};
@ -576,9 +576,9 @@ protected:
int nextDraw_;
// Not set by the initializer:
SDL_Rect reportRects_[reports::NUM_REPORTS];
surface reportSurfaces_[reports::NUM_REPORTS];
reports::report reports_[reports::NUM_REPORTS];
std::map<std::string, SDL_Rect> reportRects_;
std::map<std::string, surface> reportSurfaces_;
std::map<std::string, reports::report> reports_;
std::vector<gui::button> buttons_;
std::set<map_location> invalidated_;
std::set<map_location> previous_invalidated_;

View file

@ -99,11 +99,11 @@ void editor_display::draw_sidebar()
{
// Fill in the terrain report
if(get_map().on_board_with_border(mouseoverHex_)) {
refresh_report(reports::TERRAIN, reports::report(get_map().get_terrain_editor_string(mouseoverHex_)));
refresh_report(reports::POSITION, reports::report(lexical_cast<std::string>(mouseoverHex_)));
refresh_report("terrain", reports::report(get_map().get_terrain_editor_string(mouseoverHex_)));
refresh_report("position", reports::report(str_cast(mouseoverHex_)));
}
refresh_report(reports::VILLAGES, reports::report(lexical_cast<std::string>(get_map().villages().size())));
refresh_report(reports::EDITOR_TOOL_HINT, reports::report(toolbar_hint_));
refresh_report("villages", reports::report(str_cast(get_map().villages().size())));
refresh_report("editor_tool_hint", reports::report(toolbar_hint_));
}
} //end namespace editor

View file

@ -426,7 +426,7 @@ bool game_display::has_time_area() const
return tod_manager_.has_time_area();
}
void game_display::draw_report(reports::TYPE report_num)
void game_display::draw_report(const std::string &report_name)
{
if(!team_valid()) {
return;
@ -438,44 +438,34 @@ void game_display::draw_report(reports::TYPE report_num)
observers_, level_, !viewpoint_
};
reports::report report = reports::generate_report(report_num, data);
refresh_report(report_num, report);
}
void game_display::draw_game_status()
{
if(teams_.empty()) {
return;
}
for(size_t r = reports::STATUS_REPORTS_BEGIN; r != reports::STATUS_REPORTS_END; ++r) {
draw_report(reports::TYPE(r));
}
reports::report report = reports::generate_report(report_name, data);
refresh_report(report_name, report);
}
void game_display::draw_sidebar()
{
wb::scoped_planned_pathfind_map future; //< Lasts for whole method.
draw_report(reports::REPORT_CLOCK);
draw_report(reports::REPORT_COUNTDOWN);
draw_report("report_clock");
draw_report("report_countdown");
if(teams_.empty()) {
return;
}
if(invalidateUnit_) {
if (invalidateUnit_) {
// We display the unit the mouse is over if it is over a unit,
// otherwise we display the unit that is selected.
for(size_t r = reports::UNIT_REPORTS_BEGIN; r != reports::UNIT_REPORTS_END; ++r) {
draw_report(reports::TYPE(r));
foreach (const std::string &name, reports::report_list(true)) {
draw_report(name);
}
invalidateUnit_ = false;
}
if(invalidateGameStatus_) {
draw_game_status();
if (invalidateGameStatus_) {
foreach (const std::string &name, reports::report_list(false)) {
draw_report(name);
}
invalidateGameStatus_ = false;
}
}

View file

@ -130,7 +130,7 @@ public:
/** Draws the movement info (turns available) for a given location. */
void draw_movement_info(const map_location& loc);
void draw_report(reports::TYPE report_num);
void draw_report(const std::string &report_name);
/** Function to invalidate that unit status displayed on the sidebar. */
void invalidate_unit() { invalidateUnit_ = true; }
@ -313,7 +313,6 @@ private:
void operator=(const game_display&);
void draw_sidebar();
void draw_game_status();
// This surface must be freed by the caller
surface get_flag(const map_location& loc);

View file

@ -33,30 +33,8 @@
#include <cassert>
#include <ctime>
namespace {
const std::string report_names[] = {
"unit_name", "unit_type",
"unit_race", "unit_level", "unit_side", "unit_amla",
"unit_traits", "unit_status", "unit_alignment", "unit_abilities",
"unit_hp", "unit_xp", "unit_advancement_options", "unit_defense", "unit_moves",
"unit_weapons", "unit_image", "unit_profile", "time_of_day",
"turn", "gold", "villages", "num_units", "upkeep", "expenses",
"income", "terrain", "position", "side_playing", "observers",
"report_countdown", "report_clock",
"selected_terrain", "edit_left_button_function", "editor_tool_hint"
};
}
namespace reports {
const std::string& report_name(TYPE type)
{
assert(sizeof(report_names)/sizeof(*report_names) == NUM_REPORTS);
assert(type < NUM_REPORTS);
return report_names[type];
}
void report::add_text(const std::string& text,
const std::string& tooltip, const std::string& action) {
this->push_back(element(text,"",tooltip,action));
@ -89,6 +67,31 @@ static std::string flush(std::ostringstream &s)
return r;
}
struct report_generator
{
typedef report (*fun)(report_data const &);
fun generator;
bool for_units;
report_generator(fun g, bool u): generator(g), for_units(u) {}
};
typedef std::map<std::string, report_generator> report_generators;
static report_generators static_generators;
struct report_generator_helper
{
report_generator_helper(const char *name, report_generator::fun g, bool u)
{
static_generators.insert(report_generators::value_type(name,
report_generator(g, u)));
}
};
#define REPORT_GENERATOR(n, u, data) \
static report report_##n(const report_data &); \
static report_generator_helper reg_gen_##n(#n, &report_##n, u); \
static report report_##n(const report_data &data)
static char const *naps = "</span>";
static unit *get_visible_unit(const report_data &data)
@ -104,7 +107,7 @@ static report gray_inactive(const report_data &data, const std::string &str)
return report(span_color(font::GRAY_COLOR) + str + naps);
}
static report report_unit_name(const report_data &data)
REPORT_GENERATOR(unit_name, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -114,7 +117,7 @@ static report report_unit_name(const report_data &data)
return report(str.str(), "", tooltip.str());
}
static report report_unit_type(const report_data &data)
REPORT_GENERATOR(unit_type, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -125,7 +128,7 @@ static report report_unit_type(const report_data &data)
return report(str.str(), "", tooltip.str(), "unit_" + u->type_id());
}
static report report_unit_race(const report_data &data)
REPORT_GENERATOR(unit_race, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -135,7 +138,7 @@ static report report_unit_race(const report_data &data)
return report(str.str(), "", tooltip.str(), "..race_" + u->race()->id());
}
static report report_unit_side(const report_data &data)
REPORT_GENERATOR(unit_side, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -150,7 +153,7 @@ static report report_unit_side(const report_data &data)
return report("", flag_icon_img, u_team.current_player());
}
static report report_unit_level(const report_data &data)
REPORT_GENERATOR(unit_level, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -166,7 +169,7 @@ static report report_unit_level(const report_data &data)
return report(str.str(), "", tooltip.str());
}
static report report_unit_amla(const report_data &data)
REPORT_GENERATOR(unit_amla, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -178,7 +181,7 @@ static report report_unit_amla(const report_data &data)
return res;
}
static report report_unit_traits(const report_data &data)
REPORT_GENERATOR(unit_traits, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -198,7 +201,7 @@ static report report_unit_traits(const report_data &data)
return res;
}
static report report_unit_status(const report_data &data)
REPORT_GENERATOR(unit_status, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -224,7 +227,7 @@ static report report_unit_status(const report_data &data)
return res;
}
static report report_unit_alignment(const report_data &data)
REPORT_GENERATOR(unit_alignment, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -238,7 +241,7 @@ static report report_unit_alignment(const report_data &data)
return report(str.str(), "", tooltip.str(), "time_of_day");
}
static report report_unit_abilities(const report_data &data)
REPORT_GENERATOR(unit_abilities, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -258,7 +261,7 @@ static report report_unit_abilities(const report_data &data)
return res;
}
static report report_unit_hp(const report_data &data)
REPORT_GENERATOR(unit_hp, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -296,7 +299,7 @@ static report report_unit_hp(const report_data &data)
return report(str.str(), "", tooltip.str());
}
static report report_unit_xp(const report_data &data)
REPORT_GENERATOR(unit_xp, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -309,7 +312,7 @@ static report report_unit_xp(const report_data &data)
return report(str.str(), "", tooltip.str());
}
static report report_unit_advancement_options(const report_data &data)
REPORT_GENERATOR(unit_advancement_options, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -321,7 +324,7 @@ static report report_unit_advancement_options(const report_data &data)
return res;
}
static report report_unit_defense(const report_data &data)
REPORT_GENERATOR(unit_defense, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -358,7 +361,7 @@ static report report_unit_defense(const report_data &data)
return report(str.str(), "", tooltip.str());
}
static report report_unit_moves(const report_data &data)
REPORT_GENERATOR(unit_moves, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -376,7 +379,7 @@ static report report_unit_moves(const report_data &data)
return report(str.str());
}
static report report_unit_weapons(const report_data &data)
REPORT_GENERATOR(unit_weapons, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
@ -533,21 +536,21 @@ static report report_unit_weapons(const report_data &data)
return res;
}
static report report_unit_image(const report_data &data)
REPORT_GENERATOR(unit_image, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
return report("", image::locator(u->absolute_image(), u->image_mods()), "");
}
static report report_unit_profile(const report_data &data)
REPORT_GENERATOR(unit_profile, true, data)
{
unit *u = get_visible_unit(data);
if (!u) return report();
return report("", u->small_profile(), "");
}
static report report_time_of_day(const report_data &data)
REPORT_GENERATOR(time_of_day, false, data)
{
std::ostringstream tooltip;
time_of_day tod;
@ -579,7 +582,7 @@ static report report_time_of_day(const report_data &data)
return report("", tod_image, tooltip.str(), "time_of_day");
}
static report report_turn(const report_data &)
REPORT_GENERATOR(turn, false, /*data*/)
{
std::ostringstream str;
str << resources::tod_manager->turn();
@ -588,7 +591,7 @@ static report report_turn(const report_data &)
return report(str.str());
}
static report report_gold(const report_data &data)
REPORT_GENERATOR(gold, false, data)
{
std::ostringstream str;
// Suppose the full/"pathfind" unit map is applied.
@ -605,7 +608,7 @@ static report report_gold(const report_data &data)
return report(str.str());
}
static report report_villages(const report_data &data)
REPORT_GENERATOR(villages, false, data)
{
std::ostringstream str;
const team &viewing_team = (*resources::teams)[data.viewing_side - 1];
@ -624,12 +627,12 @@ static report report_villages(const report_data &data)
return gray_inactive(data, str.str());
}
static report report_num_units(const report_data &data)
REPORT_GENERATOR(num_units, false, data)
{
return gray_inactive(data, str_cast(side_units(data.current_side)));
}
static report report_upkeep(const report_data &data)
REPORT_GENERATOR(upkeep, false, data)
{
std::ostringstream str;
const team &viewing_team = (*resources::teams)[data.viewing_side - 1];
@ -638,14 +641,14 @@ static report report_upkeep(const report_data &data)
return gray_inactive(data, str.str());
}
static report report_expenses(const report_data &data)
REPORT_GENERATOR(expenses, false, data)
{
const team &viewing_team = (*resources::teams)[data.viewing_side - 1];
team_data td = calculate_team_data(viewing_team, data.current_side);
return gray_inactive(data, str_cast(td.expenses));
}
static report report_income(const report_data &data)
REPORT_GENERATOR(income, false, data)
{
std::ostringstream str;
const team &viewing_team = (*resources::teams)[data.viewing_side - 1];
@ -661,7 +664,7 @@ static report report_income(const report_data &data)
return report(str.str());
}
static report report_terrain(const report_data &data)
REPORT_GENERATOR(terrain, false, data)
{
gamemap &map = *resources::game_map;
const team &viewing_team = (*resources::teams)[data.viewing_side - 1];
@ -707,7 +710,7 @@ static report report_terrain(const report_data &data)
return report(str.str());
}
static report report_position(const report_data &data)
REPORT_GENERATOR(position, false, data)
{
gamemap &map = *resources::game_map;
if (!map.on_board(data.mouseover_hex))
@ -739,7 +742,7 @@ static report report_position(const report_data &data)
return report(str.str());
}
static report report_side_playing(const report_data &data)
REPORT_GENERATOR(side_playing, false, data)
{
const team &active_team = (*resources::teams)[data.active_side - 1];
std::string flag_icon = active_team.flag_icon();
@ -752,7 +755,7 @@ static report report_side_playing(const report_data &data)
return report("", flag_icon_img, active_team.current_player());
}
static report report_observers(const report_data &data)
REPORT_GENERATOR(observers, false, data)
{
if (data.observers.empty())
return report();
@ -766,12 +769,12 @@ static report report_observers(const report_data &data)
}
#ifdef DISABLE_EDITOR
static report report_editor_selected_terrain(const report_data &)
REPORT_GENERATOR(selected_terrain, false, data)
{
return report();
}
static report report_editor_left_button_function(const report_data &)
REPORT_GENERATOR(edit_left_button_function, false, data)
{
return report();
}
@ -780,7 +783,7 @@ namespace editor {
extern std::string selected_terrain, left_button_function;
}
static report report_editor_selected_terrain(const report_data &)
REPORT_GENERATOR(selected_terrain, false, /*data*/)
{
if (editor::selected_terrain.empty())
return report();
@ -788,7 +791,7 @@ static report report_editor_selected_terrain(const report_data &)
return report(editor::selected_terrain);
}
static report report_editor_left_button_function(const report_data &)
REPORT_GENERATOR(edit_left_button_function, false, /*data*/)
{
if (editor::left_button_function.empty())
return report();
@ -797,7 +800,12 @@ static report report_editor_left_button_function(const report_data &)
}
#endif
static report report_clock(const report_data &)
REPORT_GENERATOR(editor_tool_hint, false, /*data*/)
{
return report();
}
REPORT_GENERATOR(report_clock, false, /*data*/)
{
time_t t = std::time(NULL);
struct tm *lt = std::localtime(&t);
@ -808,12 +816,12 @@ static report report_clock(const report_data &)
return report(temp);
}
static report report_countdown(const report_data &data)
REPORT_GENERATOR(report_countdown, false, data)
{
const team &viewing_team = (*resources::teams)[data.viewing_side - 1];
int min, sec;
if (viewing_team.countdown_time() == 0)
return report_clock(data);
return report_report_clock(data);
std::ostringstream str;
sec = viewing_team.countdown_time() / 1000;
char const *end = naps;
@ -833,79 +841,21 @@ static report report_countdown(const report_data &data)
return report(str.str());
}
report reports::generate_report(TYPE type, const report_data & data)
report reports::generate_report(const std::string &name, const report_data &data)
{
switch(type) {
case UNIT_NAME:
return report_unit_name(data);
case UNIT_TYPE:
return report_unit_type(data);
case UNIT_RACE:
return report_unit_race(data);
case UNIT_SIDE:
return report_unit_side(data);
case UNIT_LEVEL:
return report_unit_level(data);
case UNIT_AMLA:
return report_unit_amla(data);
case UNIT_TRAITS:
return report_unit_traits(data);
case UNIT_STATUS:
return report_unit_status(data);
case UNIT_ALIGNMENT:
return report_unit_alignment(data);
case UNIT_ABILITIES:
return report_unit_abilities(data);
case UNIT_HP:
return report_unit_hp(data);
case UNIT_XP:
return report_unit_xp(data);
case UNIT_ADVANCEMENT_OPTIONS:
return report_unit_advancement_options(data);
case UNIT_DEFENSE:
return report_unit_defense(data);
case UNIT_MOVES:
return report_unit_moves(data);
case UNIT_WEAPONS:
return report_unit_weapons(data);
case UNIT_IMAGE:
return report_unit_image(data);
case UNIT_PROFILE:
return report_unit_profile(data);
case TIME_OF_DAY:
return report_time_of_day(data);
case TURN:
return report_turn(data);
case GOLD:
return report_gold(data);
case VILLAGES:
return report_villages(data);
case NUM_UNITS:
return report_num_units(data);
case UPKEEP:
return report_upkeep(data);
case EXPENSES:
return report_expenses(data);
case INCOME:
return report_income(data);
case TERRAIN:
return report_terrain(data);
case POSITION:
return report_position(data);
case SIDE_PLAYING:
return report_side_playing(data);
case OBSERVERS:
return report_observers(data);
case EDITOR_SELECTED_TERRAIN:
return report_editor_selected_terrain(data);
case EDITOR_LEFT_BUTTON_FUNCTION:
return report_editor_left_button_function(data);
case REPORT_COUNTDOWN:
return report_countdown(data);
case REPORT_CLOCK:
return report_clock(data);
default:
assert(false);
return report();
}
report_generators::const_iterator i = static_generators.find(name);
if (i == static_generators.end()) return report();
return (i->second.generator)(data);
}
static std::set<std::string> unit_reports, status_reports;
const std::set<std::string> &reports::report_list(bool for_units)
{
std::set<std::string> &l = *(for_units ? &unit_reports : &status_reports);
if (!l.empty()) return l;
foreach (const report_generators::value_type &v, static_generators) {
if (v.second.for_units == for_units) l.insert(v.first);
}
return l;
}

View file

@ -23,22 +23,6 @@ class team;
//this module is responsible for outputting textual reports of
//various game and unit statistics
namespace reports {
enum TYPE { UNIT_NAME, UNIT_TYPE, UNIT_RACE, UNIT_LEVEL,
UNIT_SIDE, UNIT_AMLA, UNIT_TRAITS, UNIT_STATUS,
UNIT_ALIGNMENT, UNIT_ABILITIES, UNIT_HP, UNIT_XP,
UNIT_ADVANCEMENT_OPTIONS, UNIT_DEFENSE, UNIT_MOVES, UNIT_WEAPONS,
UNIT_IMAGE, UNIT_PROFILE, TIME_OF_DAY,
TURN, GOLD, VILLAGES, NUM_UNITS, UPKEEP, EXPENSES,
INCOME, TERRAIN, POSITION, SIDE_PLAYING, OBSERVERS,
REPORT_COUNTDOWN, REPORT_CLOCK, EDITOR_SELECTED_TERRAIN,
EDITOR_LEFT_BUTTON_FUNCTION, EDITOR_TOOL_HINT, NUM_REPORTS
};
enum { UNIT_REPORTS_BEGIN=UNIT_NAME, UNIT_REPORTS_END=UNIT_PROFILE+1 };
enum { STATUS_REPORTS_BEGIN=TIME_OF_DAY, STATUS_REPORTS_END=EDITOR_TOOL_HINT };
const std::string& report_name(TYPE type);
struct element {
explicit element(const std::string& text) :
image(),
@ -104,8 +88,9 @@ struct report_data
bool show_everything;
};
report generate_report(TYPE type, const report_data &data);
report generate_report(const std::string &name, const report_data &data);
const std::set<std::string> &report_list(bool for_units);
}
#endif