Skip to content

Commit

Permalink
Merge pull request #631 from rcorre/wasd_scrolling
Browse files Browse the repository at this point in the history
Add preference for WASD scrolling.
  • Loading branch information
CelticMinstrel committed Apr 11, 2016
2 parents 53f1b5a + 5aecbdb commit cdf6f4a
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 38 deletions.
2 changes: 2 additions & 0 deletions changelog
Expand Up @@ -279,6 +279,8 @@ Version 1.13.4+dev:
* Miscellaneous and bug fixes:
* Resolve translated logo images not being used (bug #24357)
* Ported the "hexometer" tool from Bash to Python 3
* Recognize hotkey release events
* Allow changing keybindings for scrolling the map.

Version 1.13.4:
* Language and i18n:
Expand Down
4 changes: 4 additions & 0 deletions data/core/about.cfg
Expand Up @@ -1357,6 +1357,10 @@
[entry]
name = "Ryan Henszey"
[/entry]
[entry]
name = "Ryan Roden-Corrent (rcorre)"
comment = "Hotkey release and scroll key rebinding"
[/entry]
[entry]
name = "Sachith Seneviratne (sachith500)"
email = "sachith500@gmail.com"
Expand Down
17 changes: 17 additions & 0 deletions data/core/hotkeys.cfg
Expand Up @@ -13,6 +13,23 @@
#endif
#enddef

[hotkey]
command=scroll-up
key=UP
[/hotkey]
[hotkey]
command=scroll-down
key=DOWN
[/hotkey]
[hotkey]
command=scroll-left
key=LEFT
[/hotkey]
[hotkey]
command=scroll-right
key=RIGHT
[/hotkey]

[hotkey]
button=1
command="selectmoveaction"
Expand Down
2 changes: 2 additions & 0 deletions players_changelog
Expand Up @@ -5,6 +5,8 @@ changelog: https://github.com/wesnoth/wesnoth/blob/master/changelog
Version 1.13.4+dev:
* Language and i18n:
* Updated translations:
* Miscellaneous and bug fixes:
* Allow changing keybindings for scrolling the map.

Version 1.13.4:
* Language and i18n:
Expand Down
77 changes: 53 additions & 24 deletions src/controller_base.cpp
Expand Up @@ -34,6 +34,10 @@ controller_base::controller_base(
: game_config_(game_config)
, key_()
, scrolling_(false)
, scroll_up_(false)
, scroll_down_(false)
, scroll_left_(false)
, scroll_right_(false)
, joystick_manager_()
{
}
Expand Down Expand Up @@ -61,13 +65,14 @@ void controller_base::handle_event(const SDL_Event& event)

process_keydown_event(event);
hotkey::key_event(event, get_hotkey_command_executor());
process_keyup_event(event);
} else {
process_focus_keydown_event(event);
break;
}
// intentionally fall-through
break;
case SDL_KEYUP:
process_keyup_event(event);
hotkey::key_event(event, get_hotkey_command_executor());
break;
case SDL_JOYBUTTONDOWN:
process_keydown_event(event);
Expand Down Expand Up @@ -128,11 +133,10 @@ void controller_base::process_keyup_event(const SDL_Event& /*event*/) {
//no action by default
}

bool controller_base::handle_scroll(CKey& key, int mousex, int mousey, int mouse_flags, double x_axis, double y_axis)
bool controller_base::handle_scroll(int mousex, int mousey, int mouse_flags, double x_axis, double y_axis)
{
bool mouse_in_window = (SDL_GetAppState() & SDL_APPMOUSEFOCUS) != 0
|| preferences::get("scroll_when_mouse_outside", true);
bool keyboard_focus = have_keyboard_focus();
int scroll_speed = preferences::scroll_speed();
int dx = 0, dy = 0;
int scroll_threshold = (preferences::mouse_scroll_enabled())
Expand All @@ -142,26 +146,30 @@ bool controller_base::handle_scroll(CKey& key, int mousex, int mousey, int mouse
scroll_threshold = 0;
}
}
if ((key[SDLK_UP] && keyboard_focus) ||
(mousey < scroll_threshold && mouse_in_window))
{
dy -= scroll_speed;
}
if ((key[SDLK_DOWN] && keyboard_focus) ||
(mousey > get_display().h() - scroll_threshold && mouse_in_window))
{
dy += scroll_speed;
}
if ((key[SDLK_LEFT] && keyboard_focus) ||
(mousex < scroll_threshold && mouse_in_window))
{
dx -= scroll_speed;
}
if ((key[SDLK_RIGHT] && keyboard_focus) ||
(mousex > get_display().w() - scroll_threshold && mouse_in_window))
{
dx += scroll_speed;

// apply keyboard scrolling
dy -= scroll_up_ * scroll_speed;
dy += scroll_down_ * scroll_speed;
dx -= scroll_left_ * scroll_speed;
dx += scroll_right_ * scroll_speed;

// scroll if mouse is placed near the edge of the screen
if (mouse_in_window) {
if (mousey < scroll_threshold) {
dy -= scroll_speed;
}
if (mousey > get_display().h() - scroll_threshold) {
dy += scroll_speed;
}
if (mousex < scroll_threshold) {
dx -= scroll_speed;
}
if (mousex > get_display().w() - scroll_threshold) {
dx += scroll_speed;
}
}

// scroll with middle-mouse if enabled
if ((mouse_flags & SDL_BUTTON_MMASK) != 0 && preferences::middle_click_scrolls()) {
const map_location original_loc = get_mouse_handler_base().get_scroll_start();

Expand All @@ -185,6 +193,7 @@ bool controller_base::handle_scroll(CKey& key, int mousex, int mousey, int mouse
}
}

// scroll with joystick
dx += round_double( x_axis * scroll_speed);
dy += round_double( y_axis * scroll_speed);

Expand Down Expand Up @@ -238,7 +247,7 @@ void controller_base::play_slice(bool is_delay_enabled)
mousey += values.second * 10;
SDL_WarpMouse(mousex, mousey);
*/
scrolling_ = handle_scroll(key, mousex, mousey, mouse_flags, joystickx, joysticky);
scrolling_ = handle_scroll(mousex, mousey, mouse_flags, joystickx, joysticky);

map_location highlighted_hex = get_display().mouseover_hex();

Expand Down Expand Up @@ -341,3 +350,23 @@ const config& controller_base::get_theme(const config& game_config, std::string
static config empty;
return empty;
}

void controller_base::set_scroll_up(bool on)
{
scroll_up_ = on;
}

void controller_base::set_scroll_down(bool on)
{
scroll_down_ = on;
}

void controller_base::set_scroll_left(bool on)
{
scroll_left_ = on;
}

void controller_base::set_scroll_right(bool on)
{
scroll_right_ = on;
}
14 changes: 12 additions & 2 deletions src/controller_base.hpp
Expand Up @@ -64,6 +64,12 @@ class controller_base : public video2::draw_layering
void play_slice(bool is_delay_enabled = true);

static const config &get_theme(const config& game_config, std::string theme_name);

void apply_keyboard_scroll(int x, int y);
void set_scroll_up(bool on);
void set_scroll_down(bool on);
void set_scroll_left(bool on);
void set_scroll_right(bool on);
protected:
virtual bool is_browsing() const
{ return false; }
Expand Down Expand Up @@ -102,10 +108,10 @@ class controller_base : public video2::draw_layering

/**
* Handle scrolling by keyboard, joystick and moving mouse near map edges
* @see is_keyboard_scroll_active
* @see scrolling_, which is set if the display is being scrolled
* @return true when there was any scrolling, false otherwise
*/
bool handle_scroll(CKey& key, int mousex, int mousey, int mouse_flags, double joystickx, double joysticky);
bool handle_scroll(int mousex, int mousey, int mouse_flags, double joystickx, double joysticky);

/**
* Process mouse- and keypress-events from SDL.
Expand Down Expand Up @@ -141,6 +147,10 @@ class controller_base : public video2::draw_layering
const config& game_config_;
CKey key_;
bool scrolling_;
bool scroll_up_;
bool scroll_down_;
bool scroll_left_;
bool scroll_right_;
joystick_manager joystick_manager_;
};

Expand Down
34 changes: 32 additions & 2 deletions src/editor/controller/editor_controller.cpp
Expand Up @@ -286,6 +286,10 @@ bool editor_controller::can_execute_command(const hotkey::hotkey_command& cmd, i
case HOTKEY_PREFERENCES:
case HOTKEY_HELP:
case HOTKEY_QUIT_GAME:
case HOTKEY_SCROLL_UP:
case HOTKEY_SCROLL_DOWN:
case HOTKEY_SCROLL_LEFT:
case HOTKEY_SCROLL_RIGHT:
return true; //general hotkeys we can always do

case hotkey::HOTKEY_UNIT_LIST:
Expand Down Expand Up @@ -584,12 +588,18 @@ hotkey::ACTION_STATE editor_controller::get_action_state(hotkey::HOTKEY_COMMAND
}
}

bool editor_controller::execute_command(const hotkey::hotkey_command& cmd, int index)
bool editor_controller::execute_command(const hotkey::hotkey_command& cmd, int index, bool press)
{
const int zoom_amount = 4;
hotkey::HOTKEY_COMMAND command = cmd.id;
SCOPE_ED;
using namespace hotkey;

// nothing here handles release; fall through to base implementation
if (!press) {
return hotkey::command_executor::execute_command(cmd, index, press);
}

switch (command) {
case HOTKEY_NULL:
switch (active_menu_) {
Expand Down Expand Up @@ -972,7 +982,7 @@ bool editor_controller::execute_command(const hotkey::hotkey_command& cmd, int i
gui().invalidate_all();
return true;
default:
return hotkey::command_executor::execute_command(cmd, index);
return hotkey::command_executor::execute_command(cmd, index, press);
}
}

Expand Down Expand Up @@ -1356,4 +1366,24 @@ hotkey::command_executor * editor_controller::get_hotkey_command_executor() {
return this;
}

void editor_controller::scroll_up(bool on)
{
controller_base::set_scroll_up(on);
}

void editor_controller::scroll_down(bool on)
{
controller_base::set_scroll_down(on);
}

void editor_controller::scroll_left(bool on)
{
controller_base::set_scroll_left(on);
}

void editor_controller::scroll_right(bool on)
{
controller_base::set_scroll_right(on);
}

} //end namespace editor
8 changes: 7 additions & 1 deletion src/editor/controller/editor_controller.hpp
Expand Up @@ -107,7 +107,7 @@ class editor_controller : public controller_base,
hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command, int index) const;

/** command_executor override */
bool execute_command(const hotkey::hotkey_command& command, int index = -1);
bool execute_command(const hotkey::hotkey_command& command, int index = -1, bool press=true);

/** controller_base override */
void show_menu(const std::vector<std::string>& items_arg, int xloc, int yloc, bool context_menu, display& disp);
Expand All @@ -118,6 +118,12 @@ class editor_controller : public controller_base,
/** Show the preferences dialog */
void preferences();

/** Handle hotkeys to scroll map */
void scroll_up(bool on);
void scroll_down(bool on);
void scroll_left(bool on);
void scroll_right(bool on);

/** Grid toggle */
void toggle_grid();

Expand Down

0 comments on commit cdf6f4a

Please sign in to comment.