Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember tile_size/zoom factor between play sessions #4423

Merged
merged 9 commits into from Oct 16, 2019
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -45,6 +45,7 @@
* Hide the "Suppose Dead" key from the hotkeys list (it does nothing since 1.9.12)
* Sidebar: In replays with "View: Full Map", show all enemy units in "Damage versus:" tooltip
* Multiplayer Create Game screen now shows map previews for scenarios that use map_file=. (PR#4407)
* Remember zoom level between play sessions (#1518) and add zoom options to context menu (#1213)
### Lua API
* Accessing wesnoth.theme_items.unit_status no longer prevents the unit
status (poisoned/slowed/etc) from being shown in the sidebar. (Issue#4079)
Expand Down
2 changes: 2 additions & 0 deletions data/game_config.cfg
Expand Up @@ -32,6 +32,8 @@

hp_bar_scaling=0.6
xp_bar_scaling=0.5

# zoom factors must be a sorted list of ascending multipliers
zoom_levels = 0.25, 0.33333333333333333, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0, 3.0, 4.0

#temporary disable hex brightening
Expand Down
2 changes: 2 additions & 0 deletions data/themes/default.cfg
Expand Up @@ -118,6 +118,8 @@
"wbexecuteaction,wbdeleteaction,wbbumpupaction,wbbumpdownaction,wbsupposedead," +
# Information
"describeterrain,describeunit,renameunit," +
# Zoom
"zoomin,zoomout,zoomdefault," +
# Debug commands
"createunit,changeside,killunit," +
# Labels
Expand Down
51 changes: 32 additions & 19 deletions src/display.cpp
Expand Up @@ -89,6 +89,28 @@ namespace {
unsigned int display::zoom_ = DefaultZoom;
unsigned int display::last_zoom_ = SmallZoom;

// Returns index of zoom_levels which is closest match to input zoom_level
// Assumption: zoom_levels is a sorted vector of ascending tile sizes
int get_zoom_levels_index(unsigned int zoom_level)
{
zoom_level = utils::clamp(zoom_level, MinZoom, MaxZoom); // ensure zoom_level is within zoom_levels bounds
auto iter = std::lower_bound(zoom_levels.begin(), zoom_levels.end(), zoom_level);

// find closest match
if(iter != zoom_levels.begin() && iter != zoom_levels.end()) {
jostephd marked this conversation as resolved.
Show resolved Hide resolved
float diff = *iter - *(iter - 1);
float lower = (zoom_level - *(iter - 1)) / diff;
float upper = (*iter - zoom_level) / diff;

// the previous element is closer to zoom_level than the current one
if(lower < upper) {
iter--;
}
}

return std::distance(zoom_levels.begin(), iter);
}

void display::parse_team_overlays()
{
const team& curr_team = dc_->teams()[playing_team()];
Expand Down Expand Up @@ -233,7 +255,13 @@ display::display(const display_context * dc, std::weak_ptr<wb::manager> wb, repo

set_idle_anim_rate(preferences::idle_anim_rate());

zoom_index_ = std::distance(zoom_levels.begin(), std::find(zoom_levels.begin(), zoom_levels.end(), zoom_));
unsigned int tile_size = preferences::tile_size();
if(tile_size < MinZoom || tile_size > MaxZoom)
tile_size = DefaultZoom;
zoom_index_ = get_zoom_levels_index(tile_size);
zoom_ = zoom_levels[zoom_index_];
Wedge009 marked this conversation as resolved.
Show resolved Hide resolved
if(zoom_ != preferences::tile_size()) // correct saved tile_size if necessary
preferences::set_tile_size(zoom_);

image::set_zoom(zoom_);

Expand Down Expand Up @@ -1995,25 +2023,9 @@ bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_in
return false;
}

// Confirm this is indeed a valid zoom level.
if(validate_value_and_set_index) {
auto iter = std::lower_bound(zoom_levels.begin(), zoom_levels.end(), new_zoom);

if(iter == zoom_levels.end()) {
// This should never happen, since the value was already clamped earlier
return false;
} else if(iter != zoom_levels.begin()) {
float diff = *iter - *(iter - 1);
float lower = (new_zoom - *(iter - 1)) / diff;
float upper = (*iter - new_zoom) / diff;
if(lower < upper) {
// It's actually closer to the previous element.
iter--;
}
}

new_zoom = *iter;
zoom_index_ = std::distance(zoom_levels.begin(), iter);
zoom_index_ = get_zoom_levels_index (new_zoom);
new_zoom = zoom_levels[zoom_index_];
}

const SDL_Rect& outside_area = map_outside_area();
Expand Down Expand Up @@ -2045,6 +2057,7 @@ bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_in
last_zoom_ = zoom_;
}

preferences::set_tile_size(zoom_);
image::set_zoom(zoom_);

labels().recalculate_labels();
Expand Down
1 change: 1 addition & 0 deletions src/display.hpp
Expand Up @@ -646,6 +646,7 @@ class display : public video2::draw_layering
{
reports_object_ = &reports_object;
}

private:
void init_flags_for_side_internal(std::size_t side, const std::string& side_color);

Expand Down
10 changes: 10 additions & 0 deletions src/preferences/general.cpp
Expand Up @@ -602,6 +602,16 @@ void set_UI_volume(int vol)
sound::set_UI_volume(UI_volume());
}

unsigned int tile_size()
{
return prefs["tile_size"].to_unsigned();
}

void set_tile_size(const unsigned int size)
{
prefs["tile_size"] = size;
}

bool turn_bell()
{
return get("turn_bell", true);
Expand Down
3 changes: 3 additions & 0 deletions src/preferences/general.hpp
Expand Up @@ -141,6 +141,9 @@ namespace preferences {
bool stop_music_in_background();
void set_stop_music_in_background(bool ison);

unsigned int tile_size();
void set_tile_size(const unsigned int size);

bool turn_bell();
bool set_turn_bell(bool ison);

Expand Down