Skip to content

Commit

Permalink
Make zoom validation a local function instead of private member and u…
Browse files Browse the repository at this point in the history
…pdate comments.
  • Loading branch information
Wedge009 committed Oct 16, 2019
1 parent 8018554 commit 30f3608
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
47 changes: 25 additions & 22 deletions src/display.cpp
Expand Up @@ -89,6 +89,31 @@ namespace {
unsigned int display::zoom_ = DefaultZoom;
unsigned int display::last_zoom_ = SmallZoom;

// Factored out of set_zoom() so it can be re-used in constructor (loading from preferences)
// new_zoom is the zoom factor (corresponding to display::zoom_)
// new_zoom_index is the index of zoom_levels corresponding to zoom (corresponding to display::zoom_index_)
// Returns true when validated and and zoom_index set and false if zoom cannot be validated (in which case parameters remain unchanged)
static bool validate_zoom_level_and_set_index(unsigned int& new_zoom, int& new_zoom_index)
{
auto iter = std::lower_bound(zoom_levels.begin(), zoom_levels.end(), new_zoom);

if(iter == zoom_levels.end()) {
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;
new_zoom_index = std::distance(zoom_levels.begin(), iter);
return true;
}

void display::parse_team_overlays()
{
const team& curr_team = dc_->teams()[playing_team()];
Expand Down Expand Up @@ -2048,28 +2073,6 @@ bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_in
return true;
}

bool display::validate_zoom_level_and_set_index(unsigned int& new_zoom, int &new_zoom_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;
new_zoom_index = std::distance(zoom_levels.begin(), iter);
return true;
}

void display::set_default_zoom()
{
if (zoom_ != DefaultZoom) {
Expand Down
1 change: 0 additions & 1 deletion src/display.hpp
Expand Up @@ -635,7 +635,6 @@ class display : public video2::draw_layering

private:
void read(const config& cfg);
bool validate_zoom_level_and_set_index(unsigned int &new_zoom, int &new_zoom_index);

public:
/** Init the flag list and the team colors used by ~TC */
Expand Down

0 comments on commit 30f3608

Please sign in to comment.