Skip to content

Commit

Permalink
Implement the Keychain Plugin (#700)
Browse files Browse the repository at this point in the history
* Start implementing the godot_better_input plugin

* Update ShortcutEdit.gd

* Load & save preset option

* Add some groups and fix action events not being deleted on load

* Add MenuInputAction class for multiple menu accelerators

* Create a proper plugin and a BetterInput autoload

* Update menu accelerators

* Move settings to BetterInput

* Move menu enums to Global, make more MenuInputActions

* Add more menu events

* Add new groups

* Optimize BetterInput _input() method

* Remove a lot of lines of code

* Change some previous events, add ignore actions and a View menu group

* Change update_item_accelerator to update_ui

* Move MenuInputAction initialization to BetterInput.gd

* Update hint tooltips when a shortcut changes

Temporarily comment out some code regarding the configurable modifiers

* Some MenuInputAction variable name changes

* Add handle_input() to InputAction

* Update the shortcuts of buttons

* Fix shortcut selector menu position

* Change plugin name into Keychain

* Fix keyboard input dialog exiting when Enter or Space is being pressed

* Add two more groups

* Make groups folded by default

* Temporarily make tool modifier shortcuts not configurable

A temporary change, they will be made configurable again, with different actions that are currently mapped to the same events, local/independent from each other.

* Add license for Keychain

* Fix issue where a key event would be added in other input types

* Fix bug where the assigned state was not updated when the dialog appeared again

* Update Main.tscn

* Add a disabled line edit in keyboard shortcut selector to grab focus

* Load presets in the Keychain autoload

This way, the input actions get updated from the start, instead of only at the ShortcutEdit scene.

WARNING, this currently causes crashes if the menu items have no shortcut binded to them.

* Move custom settings away from Keychain.gd

To keep it the same as the upstream plugin

* Change menu enum names

* Made action_get_first_key() more general

* Use arrays for menu items instead of dictionaries, fixes crash

* Move moveable panels to Window menu

* Format

* Optimize hint tooltip updating

* Add support for translations in Keychain

* Translation changes

* Made tool modifiers configurable

Needs more testing.

* Made camera arrow key movement configurable & joypad axis support

This commit removes the ability to press Shift and Control+Shift to adjust the camera arrow key movement speed. Instead, the speed depends on the zoom level.

The right joypad analog stick is configured to move the camera by default.

* Rename presets into shortcut profiles, use Resources and let users create their own

* [skip ci] Update addons README

* Update Global.gd
  • Loading branch information
OverloadedOrama authored May 16, 2022
1 parent 1e0eb19 commit f509dfb
Show file tree
Hide file tree
Showing 65 changed files with 2,488 additions and 1,987 deletions.
6 changes: 6 additions & 0 deletions addons/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Addons

## Keychain

- Upstream: https://github.com/Orama-Interactive/Keychain
- Version: Based on git commit 751540203124426947d69bd81f50fea07eded91d with slight modifications in Keychain.gd and ShortcutEdit.tscn.
- License: [MIT](https://github.com/Orama-Interactive/Keychain/blob/main/LICENSE)

## gdgifexporter

- Upstream: https://github.com/jegor377/godot-gdgifexporter
Expand Down
216 changes: 216 additions & 0 deletions addons/keychain/Keychain.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
extends Node

const TRANSLATIONS_PATH := "res://addons/keychain/translations"
const PROFILES_PATH := "user://shortcut_profiles"

# Change these settings
var profiles := [preload("profiles/default.tres")]
var selected_profile: ShortcutProfile = profiles[0]
var profile_index := 0
# Syntax: "action_name": InputAction.new("Action Display Name", "Group", true)
# Note that "action_name" must already exist in the Project's Input Map.
var actions := {}
# Syntax: "Group Name": InputGroup.new("Parent Group Name")
var groups := {}
var ignore_actions := []
var ignore_ui_actions := true
var changeable_types := [true, true, true, true]
var multiple_menu_accelerators := false
var config_path := "user://cache.ini"
var config_file: ConfigFile


class InputAction:
var display_name := ""
var group := ""
var global := true

func _init(_display_name := "", _group := "", _global := true) -> void:
display_name = _display_name
group = _group
global = _global

func update_node(_action: String) -> void:
pass

func handle_input(_event: InputEvent, _action: String) -> bool:
return false


# This class is useful for the accelerators of PopupMenu items
# It's possible for PopupMenu items to have multiple shortcuts by using
# set_item_shortcut(), but we have no control over the accelerator text that appears.
# Thus, we are stuck with using accelerators instead of shortcuts.
# If Godot ever receives the ability to change the accelerator text of the items,
# we could in theory remove this class.
# If you don't care about PopupMenus in the same scene as ShortcutEdit
# such as projects like Pixelorama where everything is in the same scene,
# then you can ignore this class.
class MenuInputAction:
extends InputAction
var node_path := ""
var node: PopupMenu
var menu_item_id := 0
var echo := false

func _init(
_display_name := "",
_group := "",
_global := true,
_node_path := "",
_menu_item_id := 0,
_echo := false
) -> void:
._init(_display_name, _group, _global)
node_path = _node_path
menu_item_id = _menu_item_id
echo = _echo

func get_node(root: Node) -> void:
var temp_node = root.get_node(node_path)
if temp_node is PopupMenu:
node = node
elif temp_node is MenuButton:
node = temp_node.get_popup()

func update_node(action: String) -> void:
if !node:
return
var first_key: InputEventKey = Keychain.action_get_first_key(action)
var accel := first_key.get_scancode_with_modifiers() if first_key else 0
node.set_item_accelerator(menu_item_id, accel)

func handle_input(event: InputEvent, action: String) -> bool:
if not node:
return false
if event.is_action_pressed(action):
if event is InputEventKey:
var acc: int = node.get_item_accelerator(menu_item_id)
# If the event is the same as the menu item's accelerator, skip
if acc == event.get_scancode_with_modifiers():
return true
node.emit_signal("id_pressed", menu_item_id)
return true
if event.is_action(action) and echo:
if event.is_echo():
node.emit_signal("id_pressed", menu_item_id)
return true

return false


class InputGroup:
var parent_group := ""
var folded := true
var tree_item: TreeItem

func _init(_parent_group := "", _folded := true) -> void:
parent_group = _parent_group
folded = _folded


func _ready() -> void:
if !config_file:
config_file = ConfigFile.new()
if !config_path.empty():
config_file.load(config_path)

set_process_input(multiple_menu_accelerators)

# Load shortcut profiles
var profile_dir := Directory.new()
profile_dir.make_dir(PROFILES_PATH)
profile_dir.open(PROFILES_PATH)
profile_dir.list_dir_begin()
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.plus_file(file_name))
if file is ShortcutProfile:
profiles.append(file)
file_name = profile_dir.get_next()

# If there are no profiles besides the default, create one custom
if profiles.size() == 1:
var profile := ShortcutProfile.new()
profile.name = "Custom"
profile.resource_path = PROFILES_PATH.plus_file("custom.tres")
var saved := profile.save()
if saved:
profiles.append(profile)

for profile in profiles:
profile.fill_bindings()

var l18n_dir := Directory.new()
l18n_dir.open(TRANSLATIONS_PATH)
l18n_dir.list_dir_begin()
file_name = l18n_dir.get_next()
while file_name != "":
if !l18n_dir.current_is_dir():
if file_name.get_extension() == "po":
var t: Translation = load(TRANSLATIONS_PATH.plus_file(file_name))
TranslationServer.add_translation(t)
file_name = l18n_dir.get_next()

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

for action in actions:
var input_action: InputAction = actions[action]
if input_action is MenuInputAction:
# Below line has been modified
input_action.get_node(Global.top_menu_container.get_node("MenuItems"))


func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
return

for action in actions:
var input_action: InputAction = actions[action]
var done: bool = input_action.handle_input(event, action)
if done:
return


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:
action_erase_events(action)
for event in selected_profile.bindings[action]:
action_add_event(action, event)
# NOTE: Following line not present in the plugin itself, be careful not to overwrite
Global.update_hint_tooltips()


func action_add_event(action: String, event: InputEvent) -> void:
InputMap.action_add_event(action, event)
if action in actions:
actions[action].update_node(action)


func action_erase_event(action: String, event: InputEvent) -> void:
InputMap.action_erase_event(action, event)
if action in actions:
actions[action].update_node(action)


func action_erase_events(action: String) -> void:
InputMap.action_erase_events(action)
if action in actions:
actions[action].update_node(action)


func action_get_first_key(action: String) -> InputEventKey:
var first_key: InputEventKey = null
var events := InputMap.get_action_list(action)
for event in events:
if event is InputEventKey:
first_key = event
break
return first_key
21 changes: 21 additions & 0 deletions addons/keychain/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Orama Interactive

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit f509dfb

Please sign in to comment.