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

Implement the Keychain Plugin #700

Merged
merged 46 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c8576c2
Start implementing the godot_better_input plugin
OverloadedOrama May 6, 2022
b5ccb17
Update ShortcutEdit.gd
OverloadedOrama May 6, 2022
bfb3cf2
Load & save preset option
OverloadedOrama May 6, 2022
360c9d2
Add some groups and fix action events not being deleted on load
OverloadedOrama May 6, 2022
aa0821c
Add MenuInputAction class for multiple menu accelerators
OverloadedOrama May 9, 2022
a4cdab3
Create a proper plugin and a BetterInput autoload
OverloadedOrama May 9, 2022
c4792a9
Update menu accelerators
OverloadedOrama May 9, 2022
925cf60
Move settings to BetterInput
OverloadedOrama May 9, 2022
0236b92
Move menu enums to Global, make more MenuInputActions
OverloadedOrama May 9, 2022
e3bdaee
Add more menu events
OverloadedOrama May 9, 2022
b82a187
Add new groups
OverloadedOrama May 9, 2022
461fb6c
Optimize BetterInput _input() method
OverloadedOrama May 9, 2022
bea2574
Remove a lot of lines of code
OverloadedOrama May 9, 2022
add233b
Change some previous events, add ignore actions and a View menu group
OverloadedOrama May 9, 2022
29e202e
Change update_item_accelerator to update_ui
OverloadedOrama May 9, 2022
96540e1
Move MenuInputAction initialization to BetterInput.gd
OverloadedOrama May 9, 2022
7af02db
Update hint tooltips when a shortcut changes
OverloadedOrama May 9, 2022
3389967
Some MenuInputAction variable name changes
OverloadedOrama May 9, 2022
f990473
Add handle_input() to InputAction
OverloadedOrama May 9, 2022
72ea440
Update the shortcuts of buttons
OverloadedOrama May 10, 2022
5bfdb6b
Fix shortcut selector menu position
OverloadedOrama May 10, 2022
6a1c138
Change plugin name into Keychain
OverloadedOrama May 10, 2022
498b50c
Fix keyboard input dialog exiting when Enter or Space is being pressed
OverloadedOrama May 10, 2022
edf1112
Add two more groups
OverloadedOrama May 10, 2022
f06050e
Make groups folded by default
OverloadedOrama May 10, 2022
c0bb84c
Temporarily make tool modifier shortcuts not configurable
OverloadedOrama May 10, 2022
6cd5391
Add license for Keychain
OverloadedOrama May 10, 2022
9f0c614
Fix issue where a key event would be added in other input types
OverloadedOrama May 10, 2022
8ef4879
Fix bug where the assigned state was not updated when the dialog appe…
OverloadedOrama May 11, 2022
e27b6d4
Update Main.tscn
OverloadedOrama May 11, 2022
4dfce77
Add a disabled line edit in keyboard shortcut selector to grab focus
OverloadedOrama May 12, 2022
464e459
Load presets in the Keychain autoload
OverloadedOrama May 12, 2022
37e0d38
Move custom settings away from Keychain.gd
OverloadedOrama May 12, 2022
9dbea86
Change menu enum names
OverloadedOrama May 13, 2022
49f7507
Made action_get_first_key() more general
OverloadedOrama May 13, 2022
fc2c917
Use arrays for menu items instead of dictionaries, fixes crash
OverloadedOrama May 13, 2022
eeeb678
Move moveable panels to Window menu
OverloadedOrama May 13, 2022
ed8de77
Format
OverloadedOrama May 13, 2022
3b8fddd
Optimize hint tooltip updating
OverloadedOrama May 13, 2022
c287981
Add support for translations in Keychain
OverloadedOrama May 13, 2022
98021e7
Translation changes
OverloadedOrama May 13, 2022
21738dc
Made tool modifiers configurable
OverloadedOrama May 13, 2022
fb1ff17
Made camera arrow key movement configurable & joypad axis support
OverloadedOrama May 14, 2022
86672fc
Rename presets into shortcut profiles, use Resources and let users cr…
OverloadedOrama May 16, 2022
7c0c609
[skip ci] Update addons README
OverloadedOrama May 16, 2022
3f3692f
Update Global.gd
OverloadedOrama May 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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