Implement specific zoom levels instead of constant +/-4 (FR #24469)

This commit is contained in:
Charles Dang 2017-03-07 22:41:41 +11:00
parent 12a18e9f74
commit 9f43913ee1
5 changed files with 66 additions and 51 deletions

View file

@ -63,11 +63,15 @@ static lg::log_domain log_display("display");
#define DBG_DP LOG_STREAM(debug, log_display)
namespace {
const int DefaultZoom = game_config::tile_size;
const int SmallZoom = DefaultZoom / 2;
std::vector<unsigned int> zoom_levels {18, 24, 36, 54, 72, 108, 144, 216, 288};
const int MinZoom = 4;
const int MaxZoom = 288;
const int final_zoom_index = static_cast<int>(zoom_levels.size()) - 1;
const unsigned int DefaultZoom = game_config::tile_size;
const unsigned int SmallZoom = DefaultZoom / 2;
const unsigned int MinZoom = zoom_levels.front();
const unsigned int MaxZoom = zoom_levels.back();
size_t sunset_delay = 0;
bool benchmark = false;
@ -75,7 +79,7 @@ namespace {
bool debug_foreground = false;
}
int display::last_zoom_ = SmallZoom;
unsigned int display::last_zoom_ = SmallZoom;
void display::parse_team_overlays()
{
@ -158,6 +162,7 @@ display::display(const display_context * dc, CVideo& video, std::weak_ptr<wb::ma
view_locked_(false),
theme_(theme_cfg, screen_area()),
zoom_(DefaultZoom),
zoom_index_(0),
fake_unit_man_(new fake_unit_manager(*this)),
builder_(new terrain_builder(level, &dc_->map(), theme_.border().tile_image)),
minimap_(nullptr),
@ -236,6 +241,8 @@ display::display(const display_context * dc, CVideo& video, std::weak_ptr<wb::ma
set_idle_anim_rate(preferences::idle_anim_rate());
zoom_index_ = std::find(zoom_levels.begin(), zoom_levels.end(), zoom_) - zoom_levels.begin();
image::set_zoom(zoom_);
init_flags();
@ -2008,41 +2015,46 @@ bool display::zoom_at_min() const
return zoom_ == MinZoom;
}
bool display::set_zoom(int amount, bool absolute)
bool display::set_zoom(bool increase)
{
int new_zoom = zoom_ + amount;
if (absolute)
new_zoom = amount;
if (new_zoom < MinZoom) {
new_zoom = MinZoom;
}
if (new_zoom > MaxZoom) {
new_zoom = MaxZoom;
}
// 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_]);
}
bool display::set_zoom(unsigned int amount)
{
const unsigned int new_zoom = util::clamp(amount, MinZoom, MaxZoom);
LOG_DP << "new_zoom = " << new_zoom << std::endl;
if (new_zoom != zoom_) {
SDL_Rect const &area = map_area();
xpos_ += (xpos_ + area.w / 2) * (absolute ? new_zoom - zoom_ : amount) / zoom_;
ypos_ += (ypos_ + area.h / 2) * (absolute ? new_zoom - zoom_ : amount) / zoom_;
zoom_ = new_zoom;
bounds_check_position();
if (zoom_ != DefaultZoom) {
last_zoom_ = zoom_;
}
image::set_zoom(zoom_);
labels().recalculate_labels();
redraw_background_ = true;
invalidate_all();
// Forces a redraw after zooming.
// This prevents some graphic glitches from occurring.
draw();
return true;
} else {
if(new_zoom == zoom_) {
return false;
}
const SDL_Rect& area = map_area();
xpos_ += (xpos_ + area.w / 2) * amount / zoom_;
ypos_ += (ypos_ + area.h / 2) * amount / zoom_;
zoom_ = new_zoom;
bounds_check_position();
if(zoom_ != DefaultZoom) {
last_zoom_ = zoom_;
}
image::set_zoom(zoom_);
labels().recalculate_labels();
redraw_background_ = true;
invalidate_all();
// Forces a redraw after zooming.
// This prevents some graphic glitches from occurring.
draw();
return true;
}
void display::set_default_zoom()
@ -2308,7 +2320,7 @@ void display::scroll_to_tiles(const std::vector<map_location>::const_iterator &
void display::bounds_check_position()
{
const int orig_zoom = zoom_;
const unsigned int orig_zoom = zoom_;
if(zoom_ < MinZoom) {
zoom_ = MinZoom;

View file

@ -503,14 +503,11 @@ public:
*/
bool scroll(int xmov, int ymov, bool force = false);
/**
* Zooms the display by the specified amount.
* Negative values zoom out.
* Note the amount should be a multiple of four,
* otherwise the images might start to look odd
* (hex_width() gets rounding errors).
*/
bool set_zoom(int amount, bool absolute = false);
/** Zooms the display in (true) or out (false). */
bool set_zoom(bool increase);
/** Sets the display zoom to the specified amount. */
bool set_zoom(unsigned int amount);
bool zoom_at_max() const;
bool zoom_at_min() const;
@ -736,8 +733,9 @@ protected:
int xpos_, ypos_;
bool view_locked_;
theme theme_;
int zoom_;
static int last_zoom_;
unsigned int zoom_;
int zoom_index_;
static unsigned int last_zoom_;
const std::unique_ptr<fake_unit_manager> fake_unit_man_;
const std::unique_ptr<terrain_builder> builder_;
surface minimap_;

View file

@ -689,12 +689,12 @@ bool editor_controller::execute_command(const hotkey::hotkey_command& cmd, int i
//Zoom
case HOTKEY_ZOOM_IN:
gui_->set_zoom(zoom_amount);
gui_->set_zoom(true);
context_manager_->get_map_context().get_labels().recalculate_labels();
toolkit_->set_mouseover_overlay(*gui_);
return true;
case HOTKEY_ZOOM_OUT:
gui_->set_zoom(-zoom_amount);
gui_->set_zoom(false);
context_manager_->get_map_context().get_labels().recalculate_labels();
toolkit_->set_mouseover_overlay(*gui_);
return true;

View file

@ -716,10 +716,12 @@ void command_executor_default::recalculate_minimap()
{
get_display().recalculate_minimap();
}
CVideo& command_executor_default::get_video()
{
return get_display().video();
}
void command_executor_default::lua_console()
{
if (get_display().in_game()) {
@ -729,6 +731,7 @@ void command_executor_default::lua_console()
}
}
void command_executor::lua_console()
{
gui2::dialogs::lua_interpreter::display(get_video(), gui2::dialogs::lua_interpreter::APP);
@ -736,16 +739,19 @@ void command_executor::lua_console()
void command_executor_default::zoom_in()
{
get_display().set_zoom(zoom_amount);
get_display().set_zoom(true);
}
void command_executor_default::zoom_out()
{
get_display().set_zoom(-zoom_amount);
get_display().set_zoom(false);
}
void command_executor_default::zoom_default()
{
get_display().set_default_zoom();
}
void command_executor_default::map_screenshot()
{
make_screenshot(_("Map-Screenshot"), get_video(),

View file

@ -141,7 +141,6 @@ public:
class command_executor_default : public command_executor
{
protected:
static const int zoom_amount = 4;
virtual display& get_display() = 0;
public:
CVideo& get_video();