Display: simplify add_overlay interface (#9371)

Instead of passing all the arguments for an overlay to display::add_overlay, just take an rvalue-reference to an existing overlay object.

The overlay ctor was adjusted. It no longer takes a halo::handle argument, and the display class sets that manually.
This commit is contained in:
Charles Dang 2024-09-23 01:47:42 -04:00 committed by GitHub
parent 8a35469a05
commit 85309bd93c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 19 deletions

View file

@ -115,17 +115,17 @@ static int get_zoom_levels_index(unsigned int zoom_level)
return std::distance(zoom_levels.begin(), iter);
}
void display::add_overlay(const map_location& loc, const std::string& img, const std::string& halo, const std::string& team_name, const std::string& item_id, bool visible_under_fog, float submerge, float z_order)
void display::add_overlay(const map_location& loc, overlay&& ov)
{
halo::handle halo_handle;
if(halo != "") {
halo_handle = halo_man_.add(get_location_x(loc) + hex_size() / 2,
get_location_y(loc) + hex_size() / 2, halo, loc);
}
std::vector<overlay>& overlays = get_overlays()[loc];
auto it = std::find_if(overlays.begin(), overlays.end(), [z_order](const overlay& ov) { return ov.z_order > z_order; });
overlays.emplace(it, img, halo, halo_handle, team_name, item_id, visible_under_fog, submerge, z_order);
auto pos = std::find_if(overlays.begin(), overlays.end(),
[new_order = ov.z_order](const overlay& existing) { return existing.z_order > new_order; });
auto inserted = overlays.emplace(pos, std::move(ov));
if(const std::string& halo = inserted->halo; !halo.empty()) {
auto [x, y] = get_location_rect(loc).center();
inserted->halo_handle = halo_man_.add(x, y, halo, loc);
}
}
void display::remove_overlay(const map_location& loc)

View file

@ -167,9 +167,7 @@ public:
* An overlay is an image that is displayed on top of the tile.
* One tile may have multiple overlays.
*/
void add_overlay(const map_location& loc, const std::string& image,
const std::string& halo="", const std::string& team_name="",const std::string& item_id="",
bool visible_under_fog = true, float submerge = 0.0f, float z_order = 0);
void add_overlay(const map_location& loc, overlay&& ov);
/** remove_overlay will remove all overlays on a tile. */
void remove_overlay(const map_location& loc);

View file

@ -50,8 +50,8 @@ std::unique_ptr<editor_action> mouse_action_item::click_left(editor_display& dis
return nullptr;
}
const overlay& item = item_palette_.selected_fg_item();
disp.add_overlay(start_hex_, item.image, item.halo, "", "", item.visible_in_fog, item.submerge);
overlay item = item_palette_.selected_fg_item();
disp.add_overlay(start_hex_, std::move(item));
click_ = true;
return nullptr;

View file

@ -22,7 +22,6 @@ struct overlay
overlay(const std::string& img,
const std::string& halo_img,
halo::handle handle,
const std::string& overlay_team_name,
const std::string& item_id,
const bool fogged,
@ -31,8 +30,9 @@ struct overlay
: image(img)
, halo(halo_img)
, team_name(overlay_team_name)
, name()
, id(item_id)
, halo_handle(handle)
, halo_handle()
, visible_in_fog(fogged)
, submerge(submerge)
, z_order(item_z_order)

View file

@ -64,6 +64,7 @@
#include "map/location.hpp" // for map_location
#include "mouse_events.hpp" // for mouse_handler
#include "mp_game_settings.hpp" // for mp_game_settings
#include "overlay.hpp"
#include "pathfind/pathfind.hpp" // for full_cost_map, plain_route, etc
#include "pathfind/teleport.hpp" // for get_teleport_locations, etc
#include "play_controller.hpp" // for play_controller
@ -4112,9 +4113,15 @@ int game_lua_kernel::intf_add_tile_overlay(lua_State *L)
}
if (game_display_) {
game_display_->add_overlay(loc, cfg["image"], cfg["halo"],
team_name, cfg["name"], cfg["visible_in_fog"].to_bool(true),
cfg["submerge"].to_double(0), cfg["z_order"].to_double(0));
game_display_->add_overlay(loc, overlay(
cfg["image"],
cfg["halo"],
team_name,
cfg["name"], // Name is treated as the ID
cfg["visible_in_fog"].to_bool(true),
cfg["submerge"].to_double(0),
cfg["z_order"].to_double(0)
));
}
return 0;
}