Module: SFML::InputQueries

Included in:
App, Scene
Defined in:
lib/sfml/input_actions.rb

Overview

Instance-side helpers — included into App and Scene so user code can call action_pressed? / axis from #update etc.

Instance Method Summary collapse

Instance Method Details

#action_pressed?(action_name) ⇒ Boolean

true if any input bound to action_name is currently held. Looks up bindings on self.class.action_bindings (App-style) or falls back to the host App when called from a Scene.

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
# File 'lib/sfml/input_actions.rb', line 74

def action_pressed?(action_name)
  bindings = _resolve_action_bindings[action_name.to_sym]
  return false unless bindings

  bindings[:keys].any?           { |k| Keyboard.key_pressed?(k) }       ||
    bindings[:scancodes].any?    { |s| Keyboard.scancode_pressed?(s) }  ||
    bindings[:mouse_buttons].any? { |b| Mouse.button_pressed?(b) }      ||
    bindings[:joy_buttons].any?  { |(j, b)| Joystick.button_pressed?(j, b) }
end

#axis(negative:, positive:) ⇒ Object

Synthetic digital axis from two opposing actions. Returns -1.0, 0.0, or +1.0 — never both 1 and -1 (positive wins if both held).



86
87
88
89
90
# File 'lib/sfml/input_actions.rb', line 86

def axis(negative:, positive:)
  pos = action_pressed?(positive) ? 1.0 : 0.0
  neg = action_pressed?(negative) ? 1.0 : 0.0
  pos - neg
end