Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RDY] Allow diagonal movement via keybinding modifiers in SDL builds #22690

Merged
merged 16 commits into from Feb 20, 2018

Conversation

Projects
None yet
6 participants
@ZhilkinSerg
Copy link
Contributor

commented Jan 2, 2018

First approach for #20447.

What was changed:

  • allows diagonal movement with cursor keys using CTRL and SHIFT modifiers via DIAG_MOVE_WITH_MODIFIERS interface option (currently enabled only in SDL builds and disabled in CURSES build);
  • diagonal movement action keys are taken from keybindings, so you need these to be configured for diagonal movements to work.

Default key bindings for diagonal movement:

  • 1 = Move Southwest;
  • 3 = Move Southeast;
  • 7 = Move Northwest;
  • 9 = Move Northeast.

New key bindings:

  • Shift + Cursor Left -> 7 = Move Northwest;
  • Shift + Cursor Right -> 3 = Move Southeast;
  • Shift + Cursor Up -> 9 = Move Northeast;
  • Shift + Cursor Down -> 1 = Move Southwest.

and

  • Ctrl + Cursor Left -> 1 = Move Southwest;
  • Ctrl + Cursor Right -> 9 = Move Northeast;
  • Ctrl + Cursor Up -> 7 = Move Northwest;
  • Ctrl + Cursor Down -> 3 = Move Southeast.

Illustration:

Illustration

Screenshots:

default

if( keysym.mod & KMOD_SHIFT ){
switch (keysym.sym) {
case SDLK_LEFT:
return '7'; //LEFTUP;

This comment has been minimized.

Copy link
@codemime

codemime Jan 6, 2018

Member

Numeric constants in src/input.h would be better than the magic numbers.

This comment has been minimized.

Copy link
@ZhilkinSerg

ZhilkinSerg Jan 6, 2018

Author Contributor

I agree. My final aim is to be able to load keys from keybindings.

This comment has been minimized.

Copy link
@ZhilkinSerg

ZhilkinSerg Jan 31, 2018

Author Contributor

Movement keys are now taken from input manager.

}
//Ctrl + Arrow (diagonal counter-clockwise)
if( keysym.mod & KMOD_CTRL ){
switch (keysym.sym) {

This comment has been minimized.

Copy link
@codemime

codemime Jan 6, 2018

Member

Formatting (here and there).

This comment has been minimized.

Copy link
@ZhilkinSerg

ZhilkinSerg Jan 6, 2018

Author Contributor

Thanks. Astyled my changes.

@codemime

This comment has been minimized.

Copy link
Member

commented Jan 6, 2018

Any luck with ncurses?

@AMurkin

This comment has been minimized.

Copy link
Contributor

commented Jan 6, 2018

Illustration:
image

@ZhilkinSerg

This comment has been minimized.

Copy link
Contributor Author

commented Jan 6, 2018

Any luck with ncurses?

Didn't look into ncurses yet.

@AMurkin, thanks for the picture.

@ZhilkinSerg ZhilkinSerg changed the title Allow diagonal movement via keybinding modifiers [WIPAllow diagonal movement via keybinding modifiers Jan 6, 2018

@ZhilkinSerg ZhilkinSerg changed the title [WIPAllow diagonal movement via keybinding modifiers [WIP] Allow diagonal movement via keybinding modifiers Jan 6, 2018

@BevapDin

This comment has been minimized.

Copy link
Contributor

commented Jan 7, 2018

My final aim is to be able to load keys from keybindings.

In that case, you should probably change "input.cpp" and add support for modifier keys. input_event already has an (unused) member modifiers. This should be filled with the status of the shift/control/(maybe)alt keys.

At least for SDL builds, the modifiers can be set in "sdltiles.cpp" (maybe where last_input is set input_event( the_key, CATA_INPUT_KEYBOARD)). You'll have to change functions like popup_getkey to return an input_event instead of the plain character to get that information into the key binding setup in "input.cpp". You'll also have to enhance the loading of keybindings (input_manager::load) to load the modifiers from JSON.

@ZhilkinSerg

This comment has been minimized.

Copy link
Contributor Author

commented Jan 31, 2018

In that case, you should probably change "input.cpp" and add support for modifier keys. input_event already has an (unused) member modifiers. This should be filled with the status of the shift/control/(maybe)alt keys.
At least for SDL builds, the modifiers can be set in "sdltiles.cpp" (maybe where last_input is set input_event( the_key, CATA_INPUT_KEYBOARD)). You'll have to change functions like popup_getkey to return an input_event instead of the plain character to get that information into the key binding setup in "input.cpp". You'll also have to enhance the loading of keybindings (input_manager::load) to load the modifiers from JSON.

Thanks, but it happened to be too hard for me to implement this, so I decided not to save modifiers together with keybindings, but simply read action characters from input_manager.

@ZhilkinSerg ZhilkinSerg changed the title [WIP] Allow diagonal movement via keybinding modifiers [RDY] Allow diagonal movement via keybinding modifiers in SDL builds Jan 31, 2018

@ZhilkinSerg

This comment has been minimized.

Copy link
Contributor Author

commented Jan 31, 2018

Should be good to go for sdl builds.

I've started research for ncurses support in separate branch.

{
const input_event first_input_event = get_input_for_action( action_descriptor, context )[0];
long first_input = first_input_event.get_first_input();
char first_input_char = ( char )first_input;

This comment has been minimized.

Copy link
@BevapDin

BevapDin Feb 1, 2018

Contributor

Why are you explicitly casting this using a C-style cast (there is already an implicit conversion for the assignment)?

Why the local variable, why not return it directly?

Why return a char, when the return value (first_input) is a long and the result of the function is promoted to a long again (see return statement in sdl_keysym_to_curse, which returns a long)?

This comment has been minimized.

Copy link
@ZhilkinSerg

ZhilkinSerg Feb 1, 2018

Author Contributor

That is true. I've simplified new function to remove redundant type cast.

char input_manager::get_first_char_for_action( const std::string &action_descriptor,
const std::string context )
{
const input_event first_input_event = get_input_for_action( action_descriptor, context )[0];

This comment has been minimized.

Copy link
@BevapDin

BevapDin Feb 1, 2018

Contributor

How can you be sure that the vector returned by get_input_for_action always has at least one entry?

This comment has been minimized.

Copy link
@ZhilkinSerg

ZhilkinSerg Feb 1, 2018

Author Contributor

I've added vector size check.

src/input.h Outdated
* Return first char associated with an action ID in a given context.
*/
char get_first_char_for_action( const std::string &action_descriptor,
const std::string context = "default" );

This comment has been minimized.

Copy link
@BevapDin

BevapDin Feb 1, 2018

Contributor

Why is there a default value for the second parameter? All calls to this function supply a specific value anyway, the default is never used. And why is it a const value, but the first parameter is a reference to const?

This comment has been minimized.

Copy link
@ZhilkinSerg

ZhilkinSerg Feb 1, 2018

Author Contributor

That was copied from get_input_for_action.

if( keysym.mod & KMOD_SHIFT ) {
switch( keysym.sym ) {
case SDLK_LEFT:
return inp_mngr.get_first_char_for_action( "LEFTUP", "keyboard" );

This comment has been minimized.

Copy link
@BevapDin

BevapDin Feb 1, 2018

Contributor

I can't find any input context "keyboard", did you mean "default"?

This comment has been minimized.

Copy link
@ZhilkinSerg

ZhilkinSerg Feb 1, 2018

Author Contributor

Context is unnecessary, so I removed it.

@@ -1030,6 +1030,13 @@ void options_manager::init()

mOptionsSort["interface"]++;

add( "DIAG_MOVE_WITH_MODIFIERS", "interface", translate_marker( "Diagonal movement with cursor keys and modifiers" ),
translate_marker( "If true, allows diagonal movement with cursor keys using CTRL and SHIFT modifiers (<color_red>currently only in SDL builds</color>). Diagonal movement action keys are taken from keybindings, so you need these to be configured." ),

This comment has been minimized.

Copy link
@BevapDin

BevapDin Feb 1, 2018

Contributor

No other build specific option mentions that is only available in certain build types. (Note that COPT_CURSES_HIDE already hides it on curses builds.) For the sake of consistency, this should not be mentioned at all, or be mentioned by all options, preferably automated by checking cOpt::hide.

This comment has been minimized.

Copy link
@ZhilkinSerg

ZhilkinSerg Feb 1, 2018

Author Contributor

I've removed SDL build mention from option description.

@ZhilkinSerg

This comment has been minimized.

Copy link
Contributor Author

commented Feb 8, 2018

Any luck with ncurses?

I have to admit ncurses is out of my expertise now.

@codemime codemime self-assigned this Feb 19, 2018

@codemime

This comment has been minimized.

Copy link
Member

commented Feb 19, 2018

Works as expected (although, it took some time to get used to it). I'd only change two things:

  • The menu option shouldn't appear in ncurses builds since it doesn't affect them for now;
  • I'd make the option default in SDL builds (at first I thought the feature didn't work).
@ZhilkinSerg

This comment has been minimized.

Copy link
Contributor Author

commented Feb 20, 2018

The menu option shouldn't appear in ncurses builds since it doesn't affect them for now;

There is COPT_CURSES_HIDE modifier, so option won't appear in CURSES builds (similar to USE_TILES option).

    add( "DIAG_MOVE_WITH_MODIFIERS", "interface", translate_marker( "Diagonal movement with cursor keys and modifiers" ),
        translate_marker( "If true, allows diagonal movement with cursor keys using CTRL and SHIFT modifiers.  Diagonal movement action keys are taken from keybindings, so you need these to be configured." ),
        true, COPT_CURSES_HIDE
        );

I'd make the option default in SDL builds (at first I thought the feature didn't work).

I've changed default value of DIAG_MOVE_WITH_MODIFIERS to true.

@codemime codemime merged commit f78e2e4 into CleverRaven:master Feb 20, 2018

2 of 3 checks passed

gorgon-ghprb Build finished.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage decreased (-0.1%) to 23.251%
Details

@ZhilkinSerg ZhilkinSerg deleted the ZhilkinSerg:sdl-diag-move-mod branch Feb 25, 2018

@Litppunk

This comment has been minimized.

Copy link

commented Apr 30, 2018

does this also affect menus? My game bugged last time I loaded so the diagonals were default. But on save/exit and reload worked fine, unless maybe caps lock got clicked or something, I'm not sure what might have caused it. Something to look out for.

@kevingranade

This comment has been minimized.

Copy link
Member

commented Jan 7, 2019

This pull request has been mentioned on Cataclysm: Dark Days Ahead. There might be relevant details there:

https://discourse.cataclysmdda.org/t/dropping-things-on-the-wrong-tile-constantly/18223/8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.