Skip to content

Commit

Permalink
Add an option to select Command key as a modifier on MacOS (fixes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldpl committed Mar 16, 2024
1 parent ab02905 commit a33a26c
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/citymania/cm_hotkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ bool HasSeparateRemoveMod() {
return (_settings_client.gui.cm_fn_mod != _settings_client.gui.cm_remove_mod);
}

void UpdateModKeys(bool shift_pressed, bool ctrl_pressed, bool alt_pressed) {
void UpdateModKeys(bool shift_pressed, bool ctrl_pressed, bool alt_pressed, bool command_pressed) {
bool mod_pressed[(size_t)ModKey::END] = {false};
if (shift_pressed) mod_pressed[(size_t)ModKey::SHIFT] = true;
if (ctrl_pressed) mod_pressed[(size_t)ModKey::CTRL] = true;
if (alt_pressed) mod_pressed[(size_t)ModKey::ALT] = true;
if (command_pressed) mod_pressed[(size_t)ModKey::COMMAND] = true;
bool fn_mod_prev = _fn_mod;
bool remove_mod_prev = _remove_mod;
_fn_mod = mod_pressed[(size_t)_settings_client.gui.cm_fn_mod];
Expand Down
2 changes: 1 addition & 1 deletion src/citymania/cm_hotkeys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum class ToolRemoveMode : uint8 {
MOD = 2,
};

void UpdateModKeys(bool shift_pressed, bool ctrl_pressed, bool alt_pressed);
void UpdateModKeys(bool shift_pressed, bool ctrl_pressed, bool alt_pressed, bool command_pressed);

bool RailToolbar_IsRemoveInverted(int widget);
void RailToolbar_UpdateRemoveWidgetStatus(Window *w, int widged, bool remove_active);
Expand Down
1 change: 1 addition & 0 deletions src/citymania/cm_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum class ModKey : uint8 {
SHIFT = 1,
CTRL = 2,
ALT = 3,
COMMAND = 4,
END,
};

Expand Down
1 change: 1 addition & 0 deletions src/lang/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6171,6 +6171,7 @@ CM_STR_CONFIG_SETTING_MODIFIER_NONE :None
CM_STR_CONFIG_SETTING_MODIFIER_SHIFT :Shift
CM_STR_CONFIG_SETTING_MODIFIER_CTRL :Ctrl
CM_STR_CONFIG_SETTING_MODIFIER_ALT :Alt
CM_STR_CONFIG_SETTING_MODIFIER_COMMAND :Command (on MacOS)

CM_STR_CONFIG_SETTING_SHADED_TREES :Shaded trees: {STRING2}
CM_STR_CONFIG_SETTING_SHADED_TREES_HELPTEXT :Change tree brightness depending on a slope. Enhances visual appeal of mountainous forests.
Expand Down
8 changes: 4 additions & 4 deletions src/table/settings/cmclient_settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ static void cm_v_RedrawStatusBar(int32 new_value);
static void cm_v_RedrawGraphs(int32 new_value);

static std::initializer_list<const char*> _order_mod_actions{"nothing", "full_load", "transfer", "unload_all", "feeder_load", "feeder_unload", "no_load"};
static std::initializer_list<const char*> _mod_keys{"none", "shift", "ctrl", "alt"};
static std::initializer_list<const char*> _mod_keys{"none", "shift", "ctrl", "alt", "command"};
static std::initializer_list<const char*> _shaded_tree_options{"always_off", "always_on", "as_server"};
static std::initializer_list<const char*> _graph_background_options{"black", "grey"};

Expand Down Expand Up @@ -182,7 +182,7 @@ flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC | SF_GUI_DROPDOWN | CM_SF_CITYMAN
full = _mod_keys
def = 2
min = 0
max = 3
max = 4
str = CM_STR_CONFIG_SETTING_MODIFIER_FN
strhelp = CM_STR_CONFIG_SETTING_MODIFIER_FN_HELPTEXT
strval = CM_STR_CONFIG_SETTING_MODIFIER_NONE
Expand All @@ -195,7 +195,7 @@ flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC | SF_GUI_DROPDOWN | CM_SF_CITYMAN
full = _mod_keys
def = 2
min = 0
max = 3
max = 4
str = CM_STR_CONFIG_SETTING_MODIFIER_REMOVE
strhelp = CM_STR_CONFIG_SETTING_MODIFIER_REMOVE_HELPTEXT
strval = CM_STR_CONFIG_SETTING_MODIFIER_NONE
Expand All @@ -208,7 +208,7 @@ flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC | SF_GUI_DROPDOWN | CM_SF_CITYMAN
full = _mod_keys
def = 1
min = 0
max = 3
max = 4
str = CM_STR_CONFIG_SETTING_MODIFIER_ESTIMATE
strhelp = CM_STR_CONFIG_SETTING_MODIFIER_ESTIMATE_HELPTEXT
strval = CM_STR_CONFIG_SETTING_MODIFIER_NONE
Expand Down
2 changes: 1 addition & 1 deletion src/video/allegro_v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ void VideoDriver_Allegro::InputLoop()
(key[KEY_DOWN] ? 8 : 0);

// CM if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed);
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed, false);
}

void VideoDriver_Allegro::MainLoop()
Expand Down
6 changes: 4 additions & 2 deletions src/video/cocoa/cocoa_v.mm
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,16 @@

bool old_ctrl_pressed = _ctrl_pressed;

_ctrl_pressed = (cur_mods & ( _settings_client.gui.right_mouse_btn_emulation != RMBE_CONTROL ? NSEventModifierFlagControl : NSEventModifierFlagCommand)) != 0;
// _ctrl_pressed = (cur_mods & ( _settings_client.gui.right_mouse_btn_emulation != RMBE_CONTROL ? NSEventModifierFlagControl : NSEventModifierFlagCommand)) != 0;
bool command_pressed = (cur_mods & NSEventModifierFlagCommand) != 0;
_ctrl_pressed = (cur_mods & NSEventModifierFlagControl) != 0;
_alt_pressed = (cur_mods & NSEventModifierFlagOption) != 0;
_shift_pressed = (cur_mods & NSEventModifierFlagShift) != 0;

this->fast_forward_key_pressed = _tab_is_down;

// if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed);
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed, command_pressed);
}

/** Main game loop. */
Expand Down
5 changes: 3 additions & 2 deletions src/video/cocoa/event.mm
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,12 @@ static bool QZ_PollEvent()

bool old_ctrl_pressed = _ctrl_pressed;

_ctrl_pressed = !!(_current_mods & ( _settings_client.gui.right_mouse_btn_emulation != RMBE_CONTROL ? NSControlKeyMask : NSCommandKeyMask));
_ctrl_pressed = !!(_current_mods & NSControlKeyMask);
bool command_pressed = !!(_current_mods & NSCommandKeyMask);
_shift_pressed = !!(_current_mods & NSShiftKeyMask);

// CM if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed);
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed, command_pressed);

::GameLoop();

Expand Down
2 changes: 1 addition & 1 deletion src/video/sdl2_v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ void VideoDriver_SDL_Base::InputLoop()
(keys[SDL_SCANCODE_DOWN] ? 8 : 0);

// CM if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed);
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed, false);
}

void VideoDriver_SDL_Base::LoopOnce()
Expand Down
2 changes: 1 addition & 1 deletion src/video/sdl_v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ void VideoDriver_SDL::InputLoop()
(keys[SDLK_DOWN] ? 8 : 0);

// CM if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed);
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed, false);
}

void VideoDriver_SDL::MainLoop()
Expand Down
2 changes: 1 addition & 1 deletion src/video/win32_v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ void VideoDriver_Win32Base::InputLoop()
}

// CM if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed);
citymania::UpdateModKeys(_shift_pressed, _ctrl_pressed, _alt_pressed, false);
}

bool VideoDriver_Win32Base::PollEvent()
Expand Down

0 comments on commit a33a26c

Please sign in to comment.