Class: SFML::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/window/cursor.rb

Overview

A mouse cursor — either a built-in system shape, or a custom bitmap. Apply it with ‘window.cursor = cursor`.

window.cursor = SFML::Cursor.system(:hand)
window.cursor = SFML::Cursor.system(:not_allowed)

pixels = "..." # 32×32 RGBA bytes
window.cursor = SFML::Cursor.from_pixels(32, 32, pixels, hotspot: [16, 16])

See SFML::Cursor::TYPES for the full list of system shapes.

Constant Summary collapse

TYPES =

Order matches sfCursorType in CSFML/Window/Cursor.h.

%i[
  arrow arrow_wait wait text hand
  size_horizontal size_vertical
  size_top_left_bottom_right size_bottom_left_top_right
  size_left size_right size_top size_bottom
  size_top_left size_bottom_right size_bottom_left size_top_right
  size_all cross help not_allowed
].freeze
TYPE_INDEX =
TYPES.each_with_index.to_h.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#handleObject (readonly)

:nodoc:



59
60
61
# File 'lib/sfml/window/cursor.rb', line 59

def handle
  @handle
end

Class Method Details

.from_pixels(width, height, pixels, hotspot: [0, 0]) ⇒ Object

Build a cursor from raw RGBA pixels. ‘pixels` must be exactly width*height*4 bytes. `hotspot:` is the [x, y] within the cursor image that maps to the actual click point (e.g. tip of an arrow).

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sfml/window/cursor.rb', line 41

def self.from_pixels(width, height, pixels, hotspot: [0, 0])
  expected = Integer(width) * Integer(height) * 4
  raise ArgumentError, "pixels must be #{expected} bytes, got #{pixels.bytesize}" if pixels.bytesize != expected

  buf = FFI::MemoryPointer.new(:uint8, expected)
  buf.write_bytes(pixels)

  size = C::System::Vector2u.new
  size[:x] = Integer(width); size[:y] = Integer(height)

  hot = C::System::Vector2u.new
  hot[:x] = Integer(hotspot[0]); hot[:y] = Integer(hotspot[1])

  ptr = C::Window.sfCursor_createFromPixels(buf, size, hot)
  raise Error, "sfCursor_createFromPixels returned NULL" if ptr.null?
  _wrap(ptr)
end

.system(type) ⇒ Object

Build a cursor matching one of the OS-supplied shapes. Some types (the directional ‘size_*` family beyond Horizontal/Vertical) only render distinct shapes on Linux — on macOS / Windows they fall back to the closest equivalent.

Raises:



28
29
30
31
32
33
34
35
36
# File 'lib/sfml/window/cursor.rb', line 28

def self.system(type)
  code = TYPE_INDEX.fetch(type) do
    raise ArgumentError, "Unknown cursor type: #{type.inspect}. " \
                         "Expected one of: #{TYPES.inspect}"
  end
  ptr = C::Window.sfCursor_createFromSystem(code)
  raise Error, "sfCursor_createFromSystem returned NULL for #{type.inspect}" if ptr.null?
  _wrap(ptr)
end