From 0fd7e73310c1c780b0f2db2e1258a0ceea15292c Mon Sep 17 00:00:00 2001 From: mathalaxy Date: Sun, 2 Feb 2020 23:59:41 +0100 Subject: [PATCH] Added joystick button events and queries --- shard.yml | 2 +- src/bindings.cr | 33 +++++++++++--------------- src/crsfml-input.cr | 2 +- src/static_functions.cr | 51 ++++++++++++++++++++++++++++------------- 4 files changed, 51 insertions(+), 37 deletions(-) diff --git a/shard.yml b/shard.yml index 665e4e5..831d9c5 100644 --- a/shard.yml +++ b/shard.yml @@ -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 diff --git a/src/bindings.cr b/src/bindings.cr index e5abc15..338e740 100644 --- a/src/bindings.cr +++ b/src/bindings.cr @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/src/crsfml-input.cr b/src/crsfml-input.cr index 11bec35..4e06183 100644 --- a/src/crsfml-input.cr +++ b/src/crsfml-input.cr @@ -13,6 +13,6 @@ require "crsfml" require "./bindings" require "./static_functions" -{% if flag?(:libtest) %} +{% if flag? :"libtest-crsfml-input" %} require "../test" {% end %} diff --git a/src/static_functions.cr b/src/static_functions.cr index 26805a0..81e3cd3 100644 --- a/src/static_functions.cr +++ b/src/static_functions.cr @@ -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 @@ -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 @@ -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 @@ -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