Created
May 26, 2020 07:52
-
-
Save sojastar/def416136df84c0905b1c72858681be7 to your computer and use it in GitHub Desktop.
Key mapping swap for DragonRuby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module KeyMap | |
def self.set(mapping) | |
mapping.each_pair do |command,key| | |
GTK::KeyboardKeys.send :alias_method, command, key | |
end | |
end | |
def self.unset(mapping) | |
mapping.each_pair do |command,key| | |
GTK::KeyboardKeys.send :undef_method, command | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'lib/keymap.rb' | |
QWERTY_MAPPING = { forward: :w, | |
backward: :s, | |
straf_left: :a, | |
straf_right: :d, | |
action1: :e } | |
AZERTY_MAPPING = { forward: :z, | |
backward: :s, | |
straf_left: :q, | |
straf_right: :d, | |
action1: :e } | |
def setup(args) | |
args.state.mapping = :qwerty | |
KeyMap::set QWERTY_MAPPING | |
args.state.last_command = 'no command' | |
args.state.setup_done = true | |
end | |
def tick(args) | |
# --- 1. Setup : --- | |
setup(args) unless args.state.setup_done | |
# --- 2. Inputs : --- | |
# 2.1 Commands defined in the mappings : | |
args.state.last_command = 'no command' | |
args.state.last_command = 'forward' if args.inputs.keyboard.key_held.forward | |
args.state.last_command = 'backward' if args.inputs.keyboard.key_held.backward | |
args.state.last_command = 'straf left' if args.inputs.keyboard.key_held.straf_left | |
args.state.last_command = 'straf right' if args.inputs.keyboard.key_held.straf_right | |
args.state.last_command = 'action1' if args.inputs.keyboard.key_held.action1 | |
# 2.2 Switching between mappings : | |
if args.inputs.keyboard.key_down.space then | |
if args.state.mapping == :qwerty then | |
puts 'switched to azerty' | |
KeyMap::unset QWERTY_MAPPING | |
args.state.mapping = :azerty | |
KeyMap::set AZERTY_MAPPING | |
elsif args.state.mapping == :azerty then | |
puts 'switched to qwerty' | |
KeyMap::unset AZERTY_MAPPING | |
args.state.mapping = :qwerty | |
KeyMap::set QWERTY_MAPPING | |
end | |
end | |
# --- 3. Outputs : --- | |
args.outputs.labels << [ 20, 680, "current mapping: #{args.state.mapping.to_s}" ] | |
args.outputs.labels << [ 20, 640, args.state.last_command ] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment