Skip to content

Commit

Permalink
Added joystick button events and queries
Browse files Browse the repository at this point in the history
  • Loading branch information
feritarou committed Feb 2, 2020
1 parent e8341c0 commit 0fd7e73
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: |
version: 0.2.0
dependencies:
crsfml:
path: /home/felix/Programme/crsfml/src
path: /home/mtlx1/mtlx/crsfml/src
authors:
- mathalaxy <[email protected]>

Expand Down
33 changes: 14 additions & 19 deletions src/bindings.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Input
# =======================================================================================

# One particular key, or key/modifiers combination, that can be bound to an input event/input query.
alias KeyBinding = {Modifiers, SF::Keyboard::Key}
record KeyBinding, modifiers : Modifiers, code : SF::Keyboard::Key

# One particular button of one particular joystick that can be bound to an input event/input query.
record JoystickButtonBinding, joystick_id : Int32, button : Int32
Expand All @@ -35,12 +35,16 @@ module Input
# Instance properties
# =======================================================================================

# 1. Event bindings

# Returns all registered key bindings for input events.
getter key_pressed_bindings = {} of KeyBinding => String

# Returns all registered joystick button bindings for input events.
getter joystick_button_pressed_bindings = {} of JoystickButtonBinding => String

# 2. Query bindings

# Returns all registered bindings for input queries.
getter query_bindings = {} of String => Set(AnyBinding)

Expand All @@ -49,26 +53,24 @@ module Input
# =======================================================================================

def add_key_pressed_binding(name : String, binding : String)
tuples = parse_key_binding(binding)
tuples.each do |t|
bindings = parse_key_binding(binding)
bindings.each do |t|
@key_pressed_bindings[t] = name
end
end

def add_key_query_binding(name : String, binding : String)
tuples_array = parse_key_binding(binding)
bindings_array = parse_key_binding(binding)
if @query_bindings.has_key? name
@query_bindings[name].concat tuples_array
@query_bindings[name].concat bindings_array
else
@query_bindings[name] = Set(AnyBinding).new tuples_array
@query_bindings[name] = Set(AnyBinding).new bindings_array
end
end

def add_joystick_button_pressed_binding(name : String, binding : String)
tuples = parse_joystick_button_binding(binding)
tuples.each do |t|
@joystick_button_pressed_bindings[t] = name
end
def add_joystick_button_pressed_binding(name : String, joystick_id : Int32, button : Int32)
t = JoystickButtonBinding.new joystick_id, button
@joystick_button_pressed_bindings[t] = name
end

def add_joystick_axis_query_binding(name : String, joystick_id : Int32, binding : String)
Expand Down Expand Up @@ -113,14 +115,7 @@ module Input
string = string.lchop(word)
end

mod_sets.map { |mods| {mods, code} }
end

private def parse_joystick_button_binding(string)
# This is a stub!
[ JoystickButtonBinding.new(0, 0)]
#TODO: Implement joystick button bindings as named backreferences
# to a definition of the particular button within the same YAML data
mod_sets.map { |mods| KeyBinding.new mods, code }
end

private def parse_joystick_axis_binding(string)
Expand Down
2 changes: 1 addition & 1 deletion src/crsfml-input.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ require "crsfml"
require "./bindings"
require "./static_functions"

{% if flag?(:libtest) %}
{% if flag? :"libtest-crsfml-input" %}
require "../test"
{% end %}
51 changes: 35 additions & 16 deletions src/static_functions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,25 @@ module Input
{% for which in [:L, :R] %} {% for key in [:Shift, :Control, :Alt, :System] %}
modifiers |= Bindings::Modifiers:{{which}}{% if key == :Control %}Ctrl{% else %}{{key.id}}{% end %} if SF::Keyboard.key_pressed?(SF::Keyboard:{{which}}{{key.id}})
{% end %} {% end %}
key = sfml_event.code
code = sfml_event.code
binding = Bindings::KeyBinding.new modifiers, code

if event = @@bindings.key_pressed_bindings[{modifiers, key}]?
if event = @@bindings.key_pressed_bindings[binding]?
event
elsif default_event = @@default_bindings.key_pressed_bindings[{modifiers, key}]?
elsif default_event = @@default_bindings.key_pressed_bindings[binding]?
if @@bindings.key_pressed_bindings.values.includes? default_event
nil
nil # if the event was overridden by custom bindings, do not fallback to defaults
else
default_event
end
end
when SF::Event::JoystickButtonPressed
binding = Bindings::JoystickButtonBinding.new sfml_event.joystick_id.to_i32, sfml_event.button.to_i32
if event = @@bindings.joystick_button_pressed_bindings[binding]?
event
elsif default_event = @@default_bindings.joystick_button_pressed_bindings[binding]?
if @@bindings.joystick_button_pressed_bindings.values.includes? default_event
nil # if the event was overridden by custom bindings, do not fallback to defaults
else
default_event
end
Expand All @@ -85,7 +97,7 @@ module Input
bindings.any? do |binding|
case binding
when Bindings::KeyBinding
modifiers, key = binding
modifiers, key = binding.modifiers, binding.code
mods_pressed = true
modifiers.each do |modifier|
code = SF::Keyboard::Key.parse modifier.to_s
Expand Down Expand Up @@ -120,7 +132,7 @@ module Input
bindings.any? do |binding|
case binding
when Bindings::KeyBinding
modifiers, key = binding
modifiers, key = binding.modifiers, binding.code
mods_pressed = true
modifiers.each do |modifier|
code = SF::Keyboard::Key.parse modifier.to_s
Expand Down Expand Up @@ -159,17 +171,24 @@ module Input
end
end

#TODO: Integrate within joystick section below
# 2. Joystick button bindings
# if joystick_button_bindings = binding_types["joystick_button"]?
# if array = joystick_button_bindings.as_a?
# array.each do |joystick_button_binding|
# bindings.add_joystick_button_pressed_binding event.as_s, joystick_button_binding.as_s
# end
# else
# bindings.add_joystick_button_pressed_binding event.as_s, joystick_button_bindings.as_s
# end
# end
if joystick_bindings = binding_types["joystick"]?
joystick_bindings = joystick_bindings.as_h
joystick_id = if (id = joystick_bindings["id"]?)
id.as_i
else
0
end
if button_bindings = joystick_bindings["button"]?
if array = button_bindings.as_a?
array.each do |button_binding|
bindings.add_joystick_button_pressed_binding event.as_s, joystick_id, button_binding.as_i
end
else
bindings.add_joystick_button_pressed_binding event.as_s, joystick_id, button_bindings.as_i
end
end
end

end
end
Expand Down

0 comments on commit 0fd7e73

Please sign in to comment.