Module: SFML::Mouse

Defined in:
lib/sfml/window/mouse.rb

Overview

Global mouse state — peer to SFML::Keyboard. Use it for “is this button held right now?” and for current pointer coordinates outside of the event loop.

SFML::Mouse.button_pressed?(:left)    #=> true while LMB is held
SFML::Mouse.position                  #=> Vector2 — desktop coords
SFML::Mouse.position(window)          #=> Vector2 — relative to window
SFML::Mouse.set_position([400, 300], window)

Buttons are addressed by symbol; the raw sfMouseButton enum order is exposed via BUTTONS for users who need it.

Constant Summary collapse

BUTTONS =
%i[left right middle extra1 extra2].freeze
BUTTON_INDEX =
BUTTONS.each_with_index.to_h.freeze
ALIASES =

Friendly aliases — SFML 2 used “X-button” terminology; some users still reach for it.

{
  x1:        :extra1,
  x2:        :extra2,
  x_button1: :extra1,
  x_button2: :extra2,
}.freeze

Class Method Summary collapse

Class Method Details

.button_pressed?(button) ⇒ Boolean

Returns true if the named mouse button is currently held.

Returns:

  • (Boolean)


29
30
31
# File 'lib/sfml/window/mouse.rb', line 29

def button_pressed?(button)
  C::Window.sfMouse_isButtonPressed(_code(button))
end

.position(window = nil) ⇒ Object

Pointer position. With no argument, returns desktop-relative coordinates. With a RenderWindow, returns coordinates relative to that window’s client area (top-left = 0, 0).



36
37
38
39
40
41
42
43
# File 'lib/sfml/window/mouse.rb', line 36

def position(window = nil)
  vec = if window
          C::Graphics.sfMouse_getPositionRenderWindow(window.handle)
        else
          C::Window.sfMouse_getPosition(nil)
        end
  Vector2.new(vec[:x], vec[:y])
end

.set_position(point, window = nil) ⇒ Object

Move the OS pointer. Without ‘window`, the coordinates are desktop- relative. Useful for FPS-style mouse-look (warp the cursor back to screen centre each frame).



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sfml/window/mouse.rb', line 48

def set_position(point, window = nil)
  px, py = point.is_a?(Vector2) ? [point.x, point.y] : point
  vec = C::System::Vector2i.new
  vec[:x] = Integer(px)
  vec[:y] = Integer(py)

  if window
    C::Graphics.sfMouse_setPositionRenderWindow(vec, window.handle)
  else
    C::Window.sfMouse_setPosition(vec, nil)
  end
end