Skip to content

Commit

Permalink
Add WML tag to change the map zoom level
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Apr 24, 2017
1 parent 0620467 commit 66f4c2b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelog
Expand Up @@ -15,6 +15,7 @@ Version 1.13.7+dev:
* Small change to animator API - facing parameter replaced with target and required to be
a space adjacent to the unit.
* New modifiable theme attribute in wesnoth.game_config
* New wesnoth.zoom() function allows changing the zoom level
* Multiplayer:
* Fixed statistics being lost when reloading an MP game.
* Performance:
Expand All @@ -40,6 +41,7 @@ Version 1.13.7+dev:
the color as a list of its components, eg "([r, g, b, a])"
* Empty tags are no longer written to the configs of [unit]s and [side]s.
* New [change_theme] tag to change the theme mid-scenario
* New [zoom] tag allows changing the zoom level from an event

Version 1.13.7:
* AI:
Expand Down
4 changes: 4 additions & 0 deletions data/lua/wml-tags.lua
Expand Up @@ -1417,6 +1417,10 @@ function wesnoth.wml_actions.change_theme(cfg)
wesnoth.game_config.theme = cfg.theme
end

function wesnoth.wml_actions.zoom(cfg)
wesnoth.zoom(cfg.factor, cfg.relative)
end

function wesnoth.wml_conditionals.proceed_to_next_scenario(cfg)
local endlevel_data = wesnoth.get_end_level_data()
if not endlevel_data then
Expand Down
15 changes: 12 additions & 3 deletions src/display.cpp
Expand Up @@ -2006,7 +2006,7 @@ bool display::set_zoom(bool increase)

bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_index)
{
const unsigned int new_zoom = utils::clamp(amount, MinZoom, MaxZoom);
unsigned int new_zoom = utils::clamp(amount, MinZoom, MaxZoom);

LOG_DP << "new_zoom = " << new_zoom << std::endl;

Expand All @@ -2016,13 +2016,22 @@ bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_in

// 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);
auto iter = std::lower_bound(zoom_levels.begin(), zoom_levels.end(), new_zoom);

// TODO: do we need an error?
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_ = iter - zoom_levels.begin();
}

Expand Down
18 changes: 18 additions & 0 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -1394,6 +1394,23 @@ int game_lua_kernel::intf_open_help(lua_State *L)
return 0;
}

int game_lua_kernel::intf_zoom(lua_State* L)
{
if(!game_display_) {
return 0;
}
double factor = luaL_checknumber(L, 1);
bool relative = luaW_toboolean(L, 2);
if(relative) {
factor *= game_display_->get_zoom_factor();
}
// Passing true explicitly to avoid casting to int.
// Without doing one of the two, the call is ambiguous.
game_display_->set_zoom(factor * game_config::tile_size, true);
lua_pushnumber(L, game_display_->get_zoom_factor());
return 1;
}

/**
* Dumps a wml table or userdata wml object into a pretty string.
* - Arg 1: wml table or vconfig userdata
Expand Down Expand Up @@ -4181,6 +4198,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "switch_ai", &intf_switch_ai },
{ "synchronize_choice", &intf_synchronize_choice },
{ "synchronize_choices", &intf_synchronize_choices },
{ "zoom", &dispatch<&game_lua_kernel::intf_zoom > },
{ "teleport", &dispatch<&game_lua_kernel::intf_teleport > },
{ "unit_ability", &dispatch<&game_lua_kernel::intf_unit_ability > },
{ "view_locked", &dispatch<&game_lua_kernel::intf_view_locked > },
Expand Down
1 change: 1 addition & 0 deletions src/scripting/game_lua_kernel.hpp
Expand Up @@ -172,6 +172,7 @@ class game_lua_kernel : public lua_kernel_base
int intf_toggle_fog(lua_State *L, const bool clear);
int intf_get_fog_or_shroud(lua_State *L, bool fog);
int intf_log_replay(lua_State* L);
int intf_zoom(lua_State* L);

//private helpers
std::string synced_state();
Expand Down

0 comments on commit 66f4c2b

Please sign in to comment.