Display: formatting, minor code cleanup

This commit is contained in:
Charles Dang 2022-04-01 02:23:28 -04:00
parent 0f8ffad0cd
commit 2f3927733d

View file

@ -398,20 +398,18 @@ void display::set_team(std::size_t teamindex, bool show_everything)
{
assert(teamindex < dc_->teams().size());
currentTeam_ = teamindex;
if (!show_everything)
{
if(!show_everything) {
labels().set_team(&dc_->teams()[teamindex]);
dont_show_all_ = true;
}
else
{
} else {
labels().set_team(nullptr);
dont_show_all_ = false;
}
labels().recalculate_labels();
if(std::shared_ptr<wb::manager> w = wb_.lock())
if(std::shared_ptr<wb::manager> w = wb_.lock()) {
w->on_viewer_change(teamindex);
}
}
void display::set_playing_team(std::size_t teamindex)
{
@ -422,13 +420,10 @@ void display::set_playing_team(std::size_t teamindex)
bool display::add_exclusive_draw(const map_location& loc, unit& unit)
{
if (loc.valid() && exclusive_unit_draw_requests_.find(loc) == exclusive_unit_draw_requests_.end())
{
if(loc.valid() && exclusive_unit_draw_requests_.find(loc) == exclusive_unit_draw_requests_.end()) {
exclusive_unit_draw_requests_[loc] = unit.id();
return true;
}
else
{
} else {
return false;
}
}
@ -536,7 +531,6 @@ bool display::is_blindfolded() const
return blindfold_ctr_ > 0;
}
const SDL_Rect& display::max_map_area() const
{
static SDL_Rect max_area{0, 0, 0, 0};
@ -586,8 +580,7 @@ bool display::outside_area(const SDL_Rect& area, const int x, const int y)
{
const int x_thresh = hex_size();
const int y_thresh = hex_size();
return (x < area.x || x > area.x + area.w - x_thresh ||
y < area.y || y > area.y + area.h - y_thresh);
return (x < area.x || x > area.x + area.w - x_thresh || y < area.y || y > area.y + area.h - y_thresh);
}
// This function uses the screen as reference
@ -604,7 +597,6 @@ const map_location display::hex_clicked_on(int xclick, int yclick) const
return pixel_position_to_hex(xpos_ + xclick, ypos_ + yclick);
}
// This function uses the rect of map_area as reference
const map_location display::pixel_position_to_hex(int x, int y) const
{
@ -769,15 +761,17 @@ map_location display::minimap_location_on(int x, int y)
int py = (y - minimap_location_.y) * get_map().h() * hex_size() / std::max(minimap_location_.h, 1);
map_location loc = pixel_position_to_hex(px, py);
if (loc.x < 0)
if(loc.x < 0) {
loc.x = 0;
else if (loc.x >= get_map().w())
} else if(loc.x >= get_map().w()) {
loc.x = get_map().w() - 1;
}
if (loc.y < 0)
if(loc.y < 0) {
loc.y = 0;
else if (loc.y >= get_map().h())
} else if(loc.y >= get_map().h()) {
loc.y = get_map().h() - 1;
}
return loc;
}
@ -826,9 +820,9 @@ surface display::screenshot(bool map_screenshot)
std::shared_ptr<gui::button> display::find_action_button(const std::string& id)
{
for (std::size_t i = 0; i < action_buttons_.size(); ++i) {
if(action_buttons_[i]->id() == id) {
return action_buttons_[i];
for(auto& b : action_buttons_) {
if(b->id() == id) {
return b;
}
}
return nullptr;
@ -836,9 +830,9 @@ std::shared_ptr<gui::button> display::find_action_button(const std::string& id)
std::shared_ptr<gui::button> display::find_menu_button(const std::string& id)
{
for (std::size_t i = 0; i < menu_buttons_.size(); ++i) {
if(menu_buttons_[i]->id() == id) {
return menu_buttons_[i];
for(auto& b : menu_buttons_) {
if(b->id() == id) {
return b;
}
}
return nullptr;
@ -848,8 +842,7 @@ void display::layout_buttons()
{
DBG_DP << "positioning menu buttons...\n";
for(const auto& menu : theme_.menus()) {
std::shared_ptr<gui::button> b = find_menu_button(menu.get_id());
if(b) {
if(auto b = find_menu_button(menu.get_id())) {
const SDL_Rect& loc = menu.location(screen_.screen_area());
b->set_location(loc);
b->set_measurements(0,0);
@ -860,8 +853,7 @@ void display::layout_buttons()
DBG_DP << "positioning action buttons...\n";
for(const auto& action : theme_.actions()) {
std::shared_ptr<gui::button> b = find_action_button(action.get_id());
if(b) {
if(auto b = find_action_button(action.get_id())) {
const SDL_Rect& loc = action.location(screen_.screen_area());
b->set_location(loc);
b->set_measurements(0,0);
@ -898,15 +890,18 @@ const std::string& get_direction(std::size_t n)
void display::create_buttons()
{
std::vector<std::shared_ptr<gui::button>> menu_work;
std::vector<std::shared_ptr<gui::button>> action_work;
menu_buttons_.clear();
action_buttons_.clear();
DBG_DP << "creating menu buttons...\n";
for(const auto& menu : theme_.menus()) {
if (!menu.is_button()) continue;
if(!menu.is_button()) {
continue;
}
auto b = std::make_shared<gui::button>(screen_, menu.title(), gui::button::TYPE_PRESS, menu.image(),
gui::button::DEFAULT_SPACE, false, menu.overlay(), font::SIZE_BUTTON_SMALL);
std::shared_ptr<gui::button> b(new gui::button(screen_, menu.title(), gui::button::TYPE_PRESS, menu.image(),
gui::button::DEFAULT_SPACE, false, menu.overlay(), font::SIZE_BUTTON_SMALL));
DBG_DP << "drawing button " << menu.get_id() << "\n";
b->join_same(this);
b->set_id(menu.get_id());
@ -914,18 +909,17 @@ void display::create_buttons()
b->set_tooltip_string(menu.tooltip());
}
std::shared_ptr<gui::button> b_prev = find_menu_button(b->id());
if(b_prev) {
if(auto b_prev = find_menu_button(b->id())) {
b->enable(b_prev->enabled());
}
menu_work.push_back(b);
menu_buttons_.push_back(std::move(b));
}
DBG_DP << "creating action buttons...\n";
for(const auto& action : theme_.actions()) {
std::shared_ptr<gui::button> b(new gui::button(screen_, action.title(), string_to_button_type(action.type()), action.image(),
gui::button::DEFAULT_SPACE, false, action.overlay(), font::SIZE_BUTTON_SMALL));
auto b = std::make_shared<gui::button>(screen_, action.title(), string_to_button_type(action.type()),
action.image(), gui::button::DEFAULT_SPACE, false, action.overlay(), font::SIZE_BUTTON_SMALL);
DBG_DP << "drawing button " << action.get_id() << "\n";
b->set_id(action.get_id());
@ -934,35 +928,28 @@ void display::create_buttons()
b->set_tooltip_string(action.tooltip(0));
}
std::shared_ptr<gui::button> b_prev = find_action_button(b->id());
if(b_prev) {
if(auto b_prev = find_action_button(b->id())) {
b->enable(b_prev->enabled());
if(b_prev->get_type() == gui::button::TYPE_CHECK) {
b->set_check(b_prev->checked());
}
}
action_work.push_back(b);
action_buttons_.push_back(std::move(b));
}
menu_buttons_.clear();
menu_buttons_.assign(menu_work.begin(), menu_work.end());
action_buttons_.clear();
action_buttons_.assign(action_work.begin(), action_work.end());
layout_buttons();
DBG_DP << "buttons created\n";
}
void display::render_buttons()
{
for (std::shared_ptr<gui::button> btn : menu_buttons_) {
for(auto& btn : menu_buttons_) {
btn->set_dirty(true);
btn->draw();
}
for (std::shared_ptr<gui::button> btn : action_buttons_) {
for(auto& btn : action_buttons_) {
btn->set_dirty(true);
btn->draw();
}
@ -974,10 +961,9 @@ std::vector<surface> display::get_fog_shroud_images(const map_location& loc, ima
const auto adjacent = get_adjacent_tiles(loc);
enum visibility { FOG = 0, SHROUD = 1, CLEAR = 2 };
visibility tiles[6];
std::array<visibility, 6> tiles;
const std::string* image_prefix[] =
{ &game_config::fog_prefix, &game_config::shroud_prefix};
const std::array image_prefix{&game_config::fog_prefix, &game_config::shroud_prefix};
for(int i = 0; i < 6; ++i) {
if(shrouded(adjacent[i])) {
@ -1046,36 +1032,27 @@ std::vector<surface> display::get_fog_shroud_images(const map_location& loc, ima
// now get the surfaces
std::vector<surface> res;
for (std::string& name : names) {
surface surf(image::get_image(name, image_type));
if (surf)
for(const std::string& name : names) {
if(surface surf = image::get_image(name, image_type)) {
res.push_back(std::move(surf));
}
}
return res;
}
void display::get_terrain_images(const map_location &loc,
const std::string& timeid,
TERRAIN_TYPE terrain_type)
void display::get_terrain_images(const map_location& loc, const std::string& timeid, TERRAIN_TYPE terrain_type)
{
terrain_image_vector_.clear();
terrain_builder::TERRAIN_TYPE builder_terrain_type =
(terrain_type == FOREGROUND ?
terrain_builder::FOREGROUND : terrain_builder::BACKGROUND);
const terrain_builder::imagelist* const terrains = builder_->get_terrain_at(loc,
timeid, builder_terrain_type);
image::light_string lt;
const time_of_day& tod = get_time_of_day(loc);
// get all the light transitions
const auto adjs = get_adjacent_tiles(loc);
std::array<const time_of_day*, 6> atods;
for(size_t d = 0; d < adjs.size(); ++d){
std::array<const time_of_day*, adjs.size()> atods;
for(std::size_t d = 0; d < adjs.size(); ++d) {
atods[d] = &get_time_of_day(adjs[d]);
}
@ -1096,8 +1073,9 @@ void display::get_terrain_images(const map_location &loc,
const time_of_day& atod1 = *atods[d];
const time_of_day& atod2 = *atods[(d + 1) % 6];
if(atod1.color == tod.color || atod2.color == tod.color || atod1.color != atod2.color)
if(atod1.color == tod.color || atod2.color == tod.color || atod1.color != atod2.color) {
continue;
}
if(lt.empty()) {
// color the full hex before adding transitions
@ -1109,6 +1087,7 @@ void display::get_terrain_images(const map_location &loc,
tod_color acol = atod1.color + color_adjust_;
lt += image::get_light_string(d + 1, acol.r, acol.g, acol.b);
}
for(int d = 0; d < 6; ++d) {
/*
convex 1
@ -1126,8 +1105,9 @@ void display::get_terrain_images(const map_location &loc,
const time_of_day& atod1 = *atods[d];
const time_of_day& atod2 = *atods[(d + 1) % 6];
if(atod1.color == tod.color || atod1.color == atod2.color)
if(atod1.color == tod.color || atod1.color == atod2.color) {
continue;
}
if(lt.empty()) {
// color the full hex before adding transitions
@ -1139,6 +1119,7 @@ void display::get_terrain_images(const map_location &loc,
tod_color acol = atod1.color + color_adjust_;
lt += image::get_light_string(d + 7, acol.r, acol.g, acol.b);
}
for(int d = 0; d < 6; ++d) {
/*
convex 2
@ -1156,8 +1137,9 @@ void display::get_terrain_images(const map_location &loc,
const time_of_day& atod1 = *atods[d];
const time_of_day& atod2 = *atods[(d + 1) % 6];
if(atod2.color == tod.color || atod1.color == atod2.color)
if(atod2.color == tod.color || atod1.color == atod2.color) {
continue;
}
if(lt.empty()) {
// color the full hex before adding transitions
@ -1178,21 +1160,23 @@ void display::get_terrain_images(const map_location &loc,
}
}
if(terrains != nullptr) {
// Cache the offmap name.
// Since it is themeable it can change,
// so don't make it static.
const terrain_builder::TERRAIN_TYPE builder_terrain_type = terrain_type == FOREGROUND
? terrain_builder::FOREGROUND
: terrain_builder::BACKGROUND;
if(const terrain_builder::imagelist* const terrains = builder_->get_terrain_at(loc, timeid, builder_terrain_type)) {
// Cache the offmap name. Since it is themeable it can change, so don't make it static.
const std::string off_map_name = "terrain/" + theme_.border().tile_image;
for(const auto& terrain : *terrains) {
const image::locator &image = animate_map_ ?
terrain.get_current_frame() : terrain.get_first_frame();
const image::locator& image = animate_map_ ? terrain.get_current_frame() : terrain.get_first_frame();
// We prevent ToD coloring and brightening of off-map tiles,
// We need to test for the tile to be rendered and
// not the location, since the transitions are rendered
// over the offmap-terrain and these need a ToD coloring.
surface surf;
const bool off_map = (image.get_filename() == off_map_name || image.get_modifications().find("NO_TOD_SHIFT()") != std::string::npos);
const bool off_map = (image.get_filename() == off_map_name
|| image.get_modifications().find("NO_TOD_SHIFT()") != std::string::npos);
if(off_map) {
surf = image::get_image(image, image::SCALED_TO_HEX);
@ -1305,8 +1289,7 @@ void display::drawing_buffer_commit()
// to pass to each call to sdl_blit.
SDL_Rect dstrect{blit.x(), blit.y(), 0, 0};
SDL_Rect srcrect = blit.clip();
SDL_Rect *srcrectArg = (srcrect.x | srcrect.y | srcrect.w | srcrect.h)
? &srcrect : nullptr;
SDL_Rect* srcrectArg = (srcrect.x | srcrect.y | srcrect.w | srcrect.h) ? &srcrect : nullptr;
sdl_blit(surf, srcrectArg, screen, &dstrect);
// NOTE: the screen part should already be marked as 'to update'
}
@ -1756,8 +1739,8 @@ void display::announce(const std::string& message, const color_t& color, const a
font::floating_label flabel(message);
flabel.set_font_size(font::SIZE_FLOAT_LABEL);
flabel.set_color(color);
flabel.set_position(map_outside_area().x + map_outside_area().w/2,
map_outside_area().y + map_outside_area().h/3);
flabel.set_position(
map_outside_area().x + map_outside_area().w / 2, map_outside_area().y + map_outside_area().h / 3);
flabel.set_lifetime(options.lifetime);
flabel.set_clip_rect(map_outside_area());
@ -2441,16 +2424,18 @@ void display::clear_redraw_observers()
redraw_observers_.clear();
}
void display::draw() {
void display::draw()
{
draw(true, false);
}
void display::draw(bool update) {
void display::draw(bool update)
{
draw(update, false);
}
void display::draw(bool update,bool force) {
void display::draw(bool update, bool force)
{
// log_scope("display::draw");
if(screen_.update_locked()) {
@ -2478,8 +2463,6 @@ void display::draw(bool update,bool force) {
invalidate_animations();
if(!get_map().empty()) {
//int simulate_delay = 0;
/*
* draw_invalidated() also invalidates the halos, so also needs to be
* ran if invalidated_.empty() == true.
@ -2491,9 +2474,6 @@ void display::draw(bool update,bool force) {
drawing_buffer_commit();
post_commit();
draw_sidebar();
// Simulate slow PC:
//SDL_Delay(2*simulate_delay + rand() % 20);
}
draw_wrap(update, force);
post_draw();
@ -2533,9 +2513,8 @@ void display::draw_invalidated() {
}
invalidated_hexes_ += invalidated_.size();
if (dc_->teams().empty())
{
// The unit drawer can't function without teams
if(dc_->teams().empty()) {
return;
}
@ -2545,13 +2524,14 @@ void display::draw_invalidated() {
unit_map::const_iterator u_it = dc_->units().find(loc);
exclusive_unit_draw_requests_t::iterator request = exclusive_unit_draw_requests_.find(loc);
if(u_it != dc_->units().end()
&& (request == exclusive_unit_draw_requests_.end() || request->second == u_it->id()))
&& (request == exclusive_unit_draw_requests_.end() || request->second == u_it->id())) {
drawer.redraw_unit(*u_it);
}
}
}
void display::draw_hex(const map_location& loc) {
void display::draw_hex(const map_location& loc)
{
int xpos = get_location_x(loc);
int ypos = get_location_y(loc);
image::TYPE image_type = get_image_type(loc);
@ -3054,7 +3034,8 @@ bool display::invalidate_locations_in_rect(const SDL_Rect& rect)
return result;
}
void display::invalidate_animations_location(const map_location& loc) {
void display::invalidate_animations_location(const map_location& loc)
{
if(get_map().is_village(loc)) {
const int owner = dc_->village_owner(loc) - 1;
if(owner >= 0 && flags_[owner].need_update()
@ -3069,9 +3050,9 @@ void display::invalidate_animations()
new_animation_frame();
animate_map_ = preferences::animate_map();
if(animate_map_) {
for (const map_location &loc : get_visible_hexes())
{
if (shrouded(loc)) continue;
for(const map_location& loc : get_visible_hexes()) {
if(shrouded(loc))
continue;
if(builder_->update_animation(loc)) {
invalidate(loc);
} else {
@ -3087,7 +3068,6 @@ void display::invalidate_animations()
u->anim_comp().refresh();
}
bool new_inval;
do {
new_inval = false;
@ -3109,32 +3089,25 @@ void display::reset_standing_animations()
void display::add_arrow(arrow& arrow)
{
const arrow_path_t & arrow_path = arrow.get_path();
for (const map_location& loc : arrow_path)
{
for(const map_location& loc : arrow.get_path()) {
arrows_map_[loc].push_back(&arrow);
}
}
void display::remove_arrow(arrow& arrow)
{
const arrow_path_t & arrow_path = arrow.get_path();
for (const map_location& loc : arrow_path)
{
for(const map_location& loc : arrow.get_path()) {
arrows_map_[loc].remove(&arrow);
}
}
void display::update_arrow(arrow & arrow)
{
const arrow_path_t & previous_path = arrow.get_previous_path();
for (const map_location& loc : previous_path)
{
for(const map_location& loc : arrow.get_previous_path()) {
arrows_map_[loc].remove(&arrow);
}
const arrow_path_t & arrow_path = arrow.get_path();
for (const map_location& loc : arrow_path)
{
for(const map_location& loc : arrow.get_path()) {
arrows_map_[loc].push_back(&arrow);
}
}
@ -3201,7 +3174,8 @@ void display::process_reachmap_changes()
reach_map_changed_ = false;
}
void display::handle_window_event(const SDL_Event& event) {
void display::handle_window_event(const SDL_Event& event)
{
if(event.type == SDL_WINDOWEVENT) {
switch(event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
@ -3212,11 +3186,10 @@ void display::handle_window_event(const SDL_Event& event) {
break;
}
}
}
void display::handle_event(const SDL_Event& event) {
void display::handle_event(const SDL_Event& event)
{
if(gui2::dialogs::loading_screen::displaying()) {
return;
}