Skip to content

Commit

Permalink
Refactored display member out of map_labels in favor of the singleton…
Browse files Browse the repository at this point in the history
… accessor

This also allowed the display argument to be removed from map_context, since it was only used for
initializing map_labels.
  • Loading branch information
Vultraz committed Apr 21, 2017
1 parent 90de226 commit 86d00a4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/display.cpp
Expand Up @@ -172,7 +172,7 @@ display::display(const display_context * dc, CVideo& video, std::weak_ptr<wb::ma
turbo_speed_(2),
turbo_(false),
invalidateGameStatus_(true),
map_labels_(new map_labels(*this, 0)),
map_labels_(new map_labels(0)),
reports_object_(&reports_object),
scroll_event_("scrolled"),
complete_redraw_event_("completely_redrawn"),
Expand Down
18 changes: 9 additions & 9 deletions src/editor/map/context_manager.cpp
Expand Up @@ -485,7 +485,7 @@ void context_manager::apply_mask_dialog()

if(dlg.show(gui_.video())) {
try {
map_context mask(game_config_, dlg.path(), gui_);
map_context mask(game_config_, dlg.path());
editor_action_apply_mask a(mask.get_map());
perform_refresh(a);
} catch (editor_map_load_exception& e) {
Expand Down Expand Up @@ -528,7 +528,7 @@ void context_manager::create_mask_to_dialog()

if(dlg.show(gui_.video())) {
try {
map_context map(game_config_, dlg.path(), gui_);
map_context map(game_config_, dlg.path());
editor_action_create_mask a(map.get_map());
perform_refresh(a);
} catch (editor_map_load_exception& e) {
Expand Down Expand Up @@ -905,7 +905,7 @@ void context_manager::load_map(const std::string& filename, bool new_context)
LOG_ED << "Load map: " << filename << (new_context ? " (new)" : " (same)") << "\n";
try {
{
context_ptr mc(new map_context(game_config_, filename, gui_));
context_ptr mc(new map_context(game_config_, filename));
if(mc->get_filename() != filename) {
if(new_context && check_switch_open_map(mc->get_filename())) {
return;
Expand Down Expand Up @@ -968,10 +968,10 @@ void context_manager::new_map(int width, int height, const t_translation::terrai
editor_map m(game_config_, width, height, fill);

if(new_context) {
int new_id = add_map_context(m, gui_, true, default_schedule);
int new_id = add_map_context(m, true, default_schedule);
switch_context(new_id);
} else {
replace_map_context(m, gui_, true, default_schedule);
replace_map_context(m, true, default_schedule);
}
}

Expand All @@ -981,10 +981,10 @@ void context_manager::new_scenario(int width, int height, const t_translation::t
editor_map m(game_config_, width, height, fill);

if(new_context) {
int new_id = add_map_context(m, gui_, false, default_schedule);
int new_id = add_map_context(m, false, default_schedule);
switch_context(new_id);
} else {
replace_map_context(m, gui_, false, default_schedule);
replace_map_context(m, false, default_schedule);
}
}

Expand Down Expand Up @@ -1027,10 +1027,10 @@ void context_manager::create_default_context()
t_translation::read_terrain_code(game_config::default_terrain);

const config& default_schedule = game_config_.find_child("editor_times", "id", "default");
add_map_context(editor_map(game_config_, 44, 33, default_terrain), gui_, true, default_schedule);
add_map_context(editor_map(game_config_, 44, 33, default_terrain), true, default_schedule);
} else {
for(const std::string& filename : saved_windows_) {
add_map_context(game_config_, filename, gui_);
add_map_context(game_config_, filename);
}

saved_windows_.clear();
Expand Down
8 changes: 4 additions & 4 deletions src/editor/map/map_context.cpp
Expand Up @@ -58,7 +58,7 @@ editor_team_info::editor_team_info(const team& t)

const size_t map_context::max_action_stack_size_ = 100;

map_context::map_context(const editor_map& map, const display& disp, bool pure_map, const config& schedule)
map_context::map_context(const editor_map& map, bool pure_map, const config& schedule)
: filename_()
, map_data_key_()
, embedded_(false)
Expand All @@ -80,7 +80,7 @@ map_context::map_context(const editor_map& map, const display& disp, bool pure_m
, victory_defeated_(true)
, random_time_(false)
, active_area_(-1)
, labels_(disp, nullptr)
, labels_(nullptr)
, units_()
, teams_()
, tod_manager_(new tod_manager(schedule))
Expand All @@ -90,7 +90,7 @@ map_context::map_context(const editor_map& map, const display& disp, bool pure_m
{
}

map_context::map_context(const config& game_config, const std::string& filename, const display& disp)
map_context::map_context(const config& game_config, const std::string& filename)
: filename_(filename)
, map_data_key_()
, embedded_(false)
Expand All @@ -112,7 +112,7 @@ map_context::map_context(const config& game_config, const std::string& filename,
, victory_defeated_(true)
, random_time_(false)
, active_area_(-1)
, labels_(disp, nullptr)
, labels_(nullptr)
, units_()
, teams_()
, tod_manager_(new tod_manager(game_config.find_child("editor_times", "id", "default")))
Expand Down
4 changes: 2 additions & 2 deletions src/editor/map/map_context.hpp
Expand Up @@ -64,7 +64,7 @@ class map_context : public display_context
* empty, indicating a new map.
* Marked "explicit" to avoid automatic conversions.
*/
explicit map_context(const editor_map& map, const display& disp, bool pure_map, const config& schedule);
explicit map_context(const editor_map& map, bool pure_map, const config& schedule);

/**
* Create map_context from a map file. If the map cannot be loaded, an
Expand All @@ -74,7 +74,7 @@ class map_context : public display_context
* inside scenarios do not change the filename, but set the "embedded" flag
* instead.
*/
map_context(const config& game_config, const std::string& filename, const display& disp);
map_context(const config& game_config, const std::string& filename);

/**
* Map context destructor
Expand Down
62 changes: 36 additions & 26 deletions src/map/label.cpp
Expand Up @@ -26,26 +26,26 @@
//or the tile below is obscured. This is because in the case where the tile
//itself is visible, but the tile below is obscured, the bottom half of the
//tile will still be shrouded, and the label being drawn looks weird.
inline bool is_shrouded(const display& disp, const map_location& loc)
inline bool is_shrouded(const display* disp, const map_location& loc)
{
return disp.shrouded(loc) || disp.shrouded(loc.get_direction(map_location::SOUTH));
return disp->shrouded(loc) || disp->shrouded(loc.get_direction(map_location::SOUTH));
}

/// Rather simple test for a hex being fogged.
/// This only exists because is_shrouded() does. (The code looks nicer if
/// the test for being fogged looks similar to the test for being shrouded.)
inline bool is_fogged(const display& disp, const map_location& loc)
inline bool is_fogged(const display* disp, const map_location& loc)
{
return disp.fogged(loc);
return disp->fogged(loc);
}

map_labels::map_labels(const display &disp, const team *team) :
disp_(disp), team_(team), labels_(), enabled_(true), categories_dirty(true)
map_labels::map_labels(const team *team) :
team_(team), labels_(), enabled_(true), categories_dirty(true)
{
}

map_labels::map_labels(const map_labels& other) :
disp_(other.disp_), team_(other.team_), labels_(), enabled_(true)
team_(other.team_), labels_(), enabled_(true)
{
config cfg;
other.write(cfg);
Expand Down Expand Up @@ -115,12 +115,6 @@ const terrain_label* map_labels::get_label(const map_location& loc) const
return res;
}


const display& map_labels::disp() const
{
return disp_;
}

const std::string& map_labels::team_name() const
{
if (team_)
Expand Down Expand Up @@ -536,32 +530,43 @@ void terrain_label::calculate_shroud()
SDL_Rect terrain_label::get_rect() const
{
SDL_Rect rect;
int hex_size = parent_->disp().hex_size();

rect.x = parent_->disp().get_location_x(loc_) + hex_size / 4;
rect.y = parent_->disp().get_location_y(loc_);
rect.h = parent_->disp().hex_size();
rect.w = parent_->disp().hex_size() - hex_size/2;
display* disp = display::get_singleton();
if(!disp) {
return rect;
}

int hex_size = disp->hex_size();

rect.x = disp->get_location_x(loc_) + hex_size / 4;
rect.y = disp->get_location_y(loc_);
rect.h = disp->hex_size();
rect.w = disp->hex_size() - hex_size/2;

return rect;
}

void terrain_label::draw()
{
display* disp = display::get_singleton();
if(!disp) {
return;
}

if (text_.empty() && tooltip_.empty()) {
return;
}
clear();

if (!viewable(parent_->disp().get_disp_context())) {
if (!viewable(disp->get_disp_context())) {
return;
}
//Note: the y part of loc_nextx is not used at all.
const map_location loc_nextx = loc_.get_direction(map_location::NORTH_EAST);
const map_location loc_nexty = loc_.get_direction(map_location::SOUTH);
const int xloc = (parent_->disp().get_location_x(loc_) +
parent_->disp().get_location_x(loc_nextx)*2)/3;
const int yloc = parent_->disp().get_location_y(loc_nexty) - font::SIZE_NORMAL;
const int xloc = (disp->get_location_x(loc_) +
disp->get_location_x(loc_nextx)*2)/3;
const int yloc = disp->get_location_y(loc_nexty) - font::SIZE_NORMAL;

// If a color is specified don't allow to override it with markup. (prevents faking map labels for example)
// FIXME: @todo Better detect if it's team label and not provided by
Expand All @@ -571,7 +576,7 @@ void terrain_label::draw()
font::floating_label flabel(text_.str());
flabel.set_color(color_);
flabel.set_position(xloc, yloc);
flabel.set_clip_rect(parent_->disp().map_outside_area());
flabel.set_clip_rect(disp->map_outside_area());
flabel.set_width(font::SIZE_NORMAL * 13);
flabel.set_height(font::SIZE_NORMAL * 4);
flabel.set_scroll_mode(font::ANCHOR_LABEL_MAP);
Expand All @@ -590,10 +595,15 @@ void terrain_label::draw()
*/
bool terrain_label::hidden() const
{
display* disp = display::get_singleton();
if(!disp) {
return false;
}

// Respect user's label preferences
std::string category = "cat:" + category_;
std::string creator = "side:" + std::to_string(creator_ + 1);
const std::vector<std::string>& hidden_categories = parent_->disp().get_disp_context().hidden_label_categories();
const std::vector<std::string>& hidden_categories = disp->get_disp_context().hidden_label_categories();

if(std::find(hidden_categories.begin(), hidden_categories.end(), category) != hidden_categories.end())
return true;
Expand All @@ -603,11 +613,11 @@ bool terrain_label::hidden() const
return true;

// Fog can hide some labels.
if ( !visible_in_fog_ && is_fogged(parent_->disp(), loc_) )
if ( !visible_in_fog_ && is_fogged(disp, loc_) )
return true;

// Shroud can hide some labels.
if ( !visible_in_shroud_ && is_shrouded(parent_->disp(), loc_) )
if ( !visible_in_shroud_ && is_shrouded(disp, loc_) )
return true;

return false;
Expand Down
7 changes: 1 addition & 6 deletions src/map/label.hpp
Expand Up @@ -25,7 +25,6 @@
#include <SDL_rect.h>

class config;
class display;
class display_context;
class team;
class terrain_label;
Expand All @@ -38,7 +37,7 @@ class map_labels
typedef std::map<std::string,label_map> team_label_map;

map_labels(const map_labels&);
map_labels(const display& disp, const team*);
map_labels(const team*);
~map_labels();

map_labels& operator=(const map_labels&);
Expand Down Expand Up @@ -73,9 +72,6 @@ class map_labels

bool visible_global_label(const map_location&) const;


const display& disp() const;

const std::string& team_name() const;
const std::vector<std::string>& all_categories() const;

Expand All @@ -92,7 +88,6 @@ class map_labels
// Note: this is not an overload of get_label() so that we do not block
// outsiders from calling get_label for a non-const map_labels object.

const display& disp_;
const team* team_;

team_label_map labels_;
Expand Down

0 comments on commit 86d00a4

Please sign in to comment.