Properly validate and set index when calling set_zoom with value

This fixes an issue where the zoom index wasn't properly reset when set_default_zoom was set. It also ensures
this function will only work with the set zoom levels.
This commit is contained in:
Charles Dang 2017-03-14 12:53:16 +11:00
parent 0becb66a8c
commit 6c994a5a73
2 changed files with 17 additions and 3 deletions

View file

@ -2005,10 +2005,12 @@ bool display::set_zoom(bool increase)
// Ensure we don't try to access nonexistant vector indices.
zoom_index_ = util::clamp(increase ? zoom_index_ + 1 : zoom_index_ - 1, 0, final_zoom_index);
return set_zoom(zoom_levels[zoom_index_]);
// No validation check is needed in the next step since we've already set the index here and
// know the new zoom value is indeed valid.
return set_zoom(zoom_levels[zoom_index_], false);
}
bool display::set_zoom(unsigned int amount)
bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_index)
{
const unsigned int new_zoom = util::clamp(amount, MinZoom, MaxZoom);
@ -2018,6 +2020,18 @@ bool display::set_zoom(unsigned int amount)
return false;
}
// Confirm this is indeed a valid zoom level.
if(validate_value_and_set_index) {
auto iter = std::find(zoom_levels.begin(), zoom_levels.end(), new_zoom);
// TODO: do we need an error?
if(iter == zoom_levels.end()) {
return false;
}
zoom_index_ = iter - zoom_levels.begin();
}
const SDL_Rect& area = map_area();
xpos_ += (xpos_ + area.w / 2) * amount / zoom_;

View file

@ -498,7 +498,7 @@ public:
bool set_zoom(bool increase);
/** Sets the display zoom to the specified amount. */
bool set_zoom(unsigned int amount);
bool set_zoom(unsigned int amount, const bool validate_value_and_set_index = true);
bool zoom_at_max() const;
bool zoom_at_min() const;