Skip to content

Commit

Permalink
Add Keychain action grouping organization
Browse files Browse the repository at this point in the history
Change default action map_zoom_in from Q to Shift+Q
Change default action map_zoom_out from E to Shift+E
Add `Keychain.reload_keychain` signal
Add `keep_binding_check` Callable to Keychain.gd
	Prevents removal of unfounded action bindings

Update Orama-Interactive/Keychain@0ddeb6f
  • Loading branch information
Spartan322 committed Dec 15, 2024
1 parent d17e159 commit bd46e99
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 28 deletions.
24 changes: 21 additions & 3 deletions extension/src/openvic-extension/utility/UITools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ Array get_events_from_shortcut_key(String const& key) {
return events;
}

Error try_create_shortcut_action_for_button(GUIButton* gui_button, String const& shortcut_key_name, String const& shortcut_hotkey_name = "") {
Error try_create_shortcut_action_for_button(
GUIButton* gui_button, String const& shortcut_key_name, String const& shortcut_hotkey_name = ""
) {
Array event_array = get_events_from_shortcut_key(shortcut_key_name);

ERR_FAIL_COND_V_MSG(
Expand All @@ -155,7 +157,20 @@ Error try_create_shortcut_action_for_button(GUIButton* gui_button, String const&
action_event->set_pressed(true);

if (im->has_action(action_name)) {
WARN_PRINT(vformat("'%s' already found in InputMap, reusing hotkey", action_name));
TypedArray<InputEvent> events = im->action_get_events(action_name);
bool skip_warning = events.size() != event_array.size();
if (!skip_warning) {
for (std::size_t index = 0; index < events.size(); index++) {
if (!event_array.has(events[index])) {
skip_warning = false;
break;
}
}
}

if (!skip_warning) {
WARN_PRINT(vformat("'%s' already found in InputMap with different values, reusing hotkey", action_name));
}
} else {
im->add_action(action_name);
for (std::size_t index = 0; index < event_array.size(); index++) {
Expand All @@ -165,9 +180,12 @@ Error try_create_shortcut_action_for_button(GUIButton* gui_button, String const&
}
}

Array shortcut_array;
shortcut_array.push_back(action_event);

Ref<Shortcut> shortcut;
shortcut.instantiate();
shortcut->set_events(event_array);
shortcut->set_events(shortcut_array);
gui_button->set_shortcut(shortcut);

return OK;
Expand Down
26 changes: 21 additions & 5 deletions game/addons/keychain/Keychain.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ extends Node

const PROFILES_PATH := "user://shortcut_profiles"

signal reload_keychain()

## [Array] of [ShortcutProfile]s.
var profiles: Array[ShortcutProfile] = [preload("profiles/default.tres")]
var selected_profile := profiles[0] ## The currently selected [ShortcutProfile].
Expand All @@ -22,9 +24,12 @@ var ignore_ui_actions := true
## and the fourth for [InputEventJoypadMotion]s.
var changeable_types: PackedByteArray = [true, true, true, true]
## The file path of the [code]config_file[/code].
var config_path := "user://cache.ini"
var config_path := "user://config.ini"
## Used to store the settings to the filesystem.
var config_file: ConfigFile
## Used to check if unused binding check should be ignored for action
var keep_binding_check : Callable = func(action_name : StringName) -> bool:
return action_name.begins_with("button_") and action_name.ends_with("_hotkey")


class InputAction:
Expand All @@ -48,6 +53,10 @@ class InputGroup:
folded = _folded


func _init() -> void:
for locale in TranslationServer.get_loaded_locales():
load_translation(locale)

func _ready() -> void:
if !config_file:
config_file = ConfigFile.new()
Expand All @@ -58,11 +67,11 @@ func _ready() -> void:
DirAccess.make_dir_recursive_absolute(PROFILES_PATH)
var profile_dir := DirAccess.open(PROFILES_PATH)
profile_dir.list_dir_begin()
var file_name = profile_dir.get_next()
var file_name := profile_dir.get_next()
while file_name != "":
if !profile_dir.current_is_dir():
if file_name.get_extension() == "tres":
var file = load(PROFILES_PATH.path_join(file_name))
var file := load(PROFILES_PATH.path_join(file_name))
if file is ShortcutProfile:
profiles.append(file)
file_name = profile_dir.get_next()
Expand All @@ -76,19 +85,23 @@ func _ready() -> void:
if saved:
profiles.append(profile)

initialize_profiles()

func initialize_profiles() -> void:
for profile in profiles:
profile.fill_bindings()

profile_index = config_file.get_value("shortcuts", "shortcuts_profile", 0)
change_profile(profile_index)

Keychain.reload_keychain.emit()

func change_profile(index: int) -> void:
if index >= profiles.size():
index = profiles.size() - 1
profile_index = index
selected_profile = profiles[index]
for action in selected_profile.bindings:
if not InputMap.has_action(action): continue
action_erase_events(action)
for event in selected_profile.bindings[action]:
action_add_event(action, event)
Expand All @@ -107,7 +120,10 @@ func action_erase_events(action: StringName) -> void:


func load_translation(locale: String) -> void:
var translation = load("res://addons/keychain/translations".path_join(locale + ".po"))
var translation_file_path := "res://addons/keychain/translations".path_join(locale + ".po")
if not ResourceLoader.exists(translation_file_path, "Translation"):
return
var translation := load(translation_file_path)
if is_instance_valid(translation) and translation is Translation:
TranslationServer.add_translation(translation)

Expand Down
10 changes: 7 additions & 3 deletions game/addons/keychain/ShortcutEdit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const MOUSE_BUTTON_NAMES: PackedStringArray = [
"Wheel Down Button",
"Wheel Left Button",
"Wheel Right Button",
"X Button 1",
"X Button 2",
"Mouse Thumb Button 1",
"Mouse Thumb Button 2",
]

const JOY_BUTTON_NAMES: PackedStringArray = [
Expand Down Expand Up @@ -110,6 +110,10 @@ func _ready() -> void:
if OS.get_name() == "Web":
$VBoxContainer/HBoxContainer/OpenProfileFolder.queue_free()

Keychain.reload_keychain.connect(_on_reload_keychain)

func _on_reload_keychain() -> void:
_on_ProfileOptionButton_item_selected(Keychain.profile_index)

func _construct_tree() -> void:
var buttons_disabled := false if Keychain.selected_profile.customizable else true
Expand Down Expand Up @@ -271,7 +275,7 @@ func _on_shortcut_tree_button_clicked(item: TreeItem, _column: int, id: int, _mb
rect.position.y += 42 - tree.get_scroll().y
rect.position += global_position
rect.size = Vector2(110, 23 * shortcut_type_menu.get_item_count())
shortcut_type_menu.popup(rect)
shortcut_type_menu.popup_on_parent(rect)
elif id == 1: # Delete
Keychain.action_erase_events(action)
Keychain.selected_profile.change_action(action)
Expand Down
5 changes: 5 additions & 0 deletions game/addons/keychain/ShortcutEdit.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,30 @@ text = "Shortcut profile:"

[node name="ProfileOptionButton" type="OptionButton" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
mouse_default_cursor_shape = 2

[node name="NewProfile" type="Button" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
mouse_default_cursor_shape = 2
text = "New"

[node name="RenameProfile" type="Button" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
mouse_default_cursor_shape = 2
text = "Rename"

[node name="DeleteProfile" type="Button" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
mouse_default_cursor_shape = 2
text = "Delete"

[node name="OpenProfileFolder" type="Button" parent="VBoxContainer/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
mouse_default_cursor_shape = 2
text = "Open Folder"

Expand Down
3 changes: 3 additions & 0 deletions game/addons/keychain/ShortcutProfile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func fill_bindings() -> void:
bindings[action] = InputMap.action_get_events(action)
unnecessary_actions.erase(action)
for action in unnecessary_actions:
if Keychain.keep_binding_check.call(action):
unnecessary_actions.erase(action)
continue
bindings.erase(action)
save()

Expand Down
2 changes: 1 addition & 1 deletion game/addons/keychain/assets/add.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion game/addons/keychain/assets/close.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion game/addons/keychain/assets/edit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion game/addons/keychain/assets/folder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion game/addons/keychain/assets/keyboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion game/addons/keychain/assets/keyboard_physical.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion game/addons/keychain/assets/mouse.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion game/addons/keychain/assets/shortcut.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions game/addons/keychain/translations/Translations.pot
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ msgstr ""
msgid "Wheel Right Button"
msgstr ""

msgid "X Button 1"
msgid "Mouse Thumb Button 1"
msgstr ""

msgid "X Button 2"
msgid "Mouse Thumb Button 2"
msgstr ""

msgid "DualShock Cross, Xbox A, Nintendo B"
Expand Down
8 changes: 4 additions & 4 deletions game/addons/keychain/translations/el_GR.po
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ msgstr "Τρόχος αριστερά"
msgid "Wheel Right Button"
msgstr "Τρόχος δεξιά"

msgid "X Button 1"
msgstr "Κουμπί X 1"
msgid "Mouse Thumb Button 1"
msgstr "Κουμπί αντίχειρα ποντικού 1"

msgid "X Button 2"
msgstr "Κουμπί X 2"
msgid "Mouse Thumb Button 2"
msgstr "Κουμπί αντίχειρα ποντικιού 2"

msgid "DualShock Cross, Xbox A, Nintendo B"
msgstr "DualShock Σταυρός, Xbox A, Nintendo B"
Expand Down
4 changes: 2 additions & 2 deletions game/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ map_west={
map_zoom_in={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":8,"position":Vector2(174, 17),"global_position":Vector2(180, 80),"factor":1.0,"button_index":4,"canceled":false,"pressed":true,"double_click":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":81,"physical_keycode":0,"key_label":0,"unicode":113,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":81,"physical_keycode":0,"key_label":0,"unicode":113,"location":0,"echo":false,"script":null)
]
}
map_zoom_out={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":16,"position":Vector2(325, 24),"global_position":Vector2(331, 87),"factor":1.0,"button_index":5,"canceled":false,"pressed":true,"double_click":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":69,"physical_keycode":0,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":69,"physical_keycode":0,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null)
]
}
map_drag={
Expand Down
2 changes: 1 addition & 1 deletion game/src/Game/GameSession/GameSession.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func _ready() -> void:
MusicConductor.generate_playlist()
MusicConductor.select_next_song()

Keychain._ready()
Keychain.initialize_profiles()

func _process(_delta : float) -> void:
GameSingleton.update_clock()
Expand Down
26 changes: 26 additions & 0 deletions game/src/Game/GameStart.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ var _settings_base_path : String = ""
var _compatibility_path_list : PackedStringArray = []

func _ready() -> void:
Keychain.actions = {
# Map Group
&"map_north": Keychain.InputAction.new("Move North", "Map", true),
&"map_east": Keychain.InputAction.new("Move East", "Map", true),
&"map_south": Keychain.InputAction.new("Move South", "Map", true),
&"map_west": Keychain.InputAction.new("Move West", "Map", true),
&"map_zoom_in": Keychain.InputAction.new("Zoom In", "Map", true),
&"map_zoom_out": Keychain.InputAction.new("Zoom Out", "Map", true),
&"map_drag": Keychain.InputAction.new("Mouse Drag", "Map", true),
&"map_click": Keychain.InputAction.new("Mouse Click", "Map", true),
&"map_right_click": Keychain.InputAction.new("Mouse Right Click", "Map", true),
# Time Group
&"time_pause": Keychain.InputAction.new("Pause", "Time", true),
&"time_speed_increase": Keychain.InputAction.new("Speed Increase", "Time", true),
&"time_speed_decrease": Keychain.InputAction.new("Speed Decrease", "Time", true),
# UI Group
&"menu_pause": Keychain.InputAction.new("Open Pause Menu", "UI", true),
}

Keychain.groups = {
"Map": Keychain.InputGroup.new("", false),
"Time": Keychain.InputGroup.new("", false),
"UI": Keychain.InputGroup.new("", false),
"Hotkeys": Keychain.InputGroup.new("UI")
}

Localisation.initialize()
if ArgumentParser.get_argument(&"help"):
ArgumentParser._print_help()
Expand Down
9 changes: 9 additions & 0 deletions game/src/Game/Menu/OptionMenu/OptionsMenu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ func _ready() -> void:
_tab_container.set_tab_title(3, "OPTIONS_CONTROLS")
_tab_container.set_tab_title(4, "OPTIONS_OTHER")

# Setup Keychain for Hotkeys
for action : StringName in InputMap.get_actions():
if not Keychain.keep_binding_check.call(action):
continue
Keychain.actions[action] = Keychain.InputAction.new(
action.erase(0, "button_".length()).left(-"_hotkey".length()).capitalize(),
"Hotkeys")
Keychain.profiles[0].bindings[action] = InputMap.action_get_events(action)

# Prepare options menu before loading user settings
var tab_bar : TabBar = _tab_container.get_child(0, true)

Expand Down

0 comments on commit bd46e99

Please sign in to comment.