Class: SFML::Rect

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/system/rect.rb

Overview

An axis-aligned rectangle. Mirrors sfFloatRect / sfIntRect — those structs are just (position: Vector2, size: Vector2). We deliberately keep one Ruby class for both the float and int variants since pattern-matching case bounds in {position: {x:, y:}, size: {x: w, y: h}} is what users reach for either way.

r = SFML::Rect.new([10, 20], [100, 50])
r.contains?([50, 30])    #=> true
r.right                  #=> 110

Used by Text#local_bounds, Text#global_bounds, Sprite#texture_rect, etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, size) ⇒ Rect

position and size may each be a Vector2 OR a [x, y] Array.



18
19
20
21
22
# File 'lib/sfml/system/rect.rb', line 18

def initialize(position, size)
  @position = position.is_a?(Vector2) ? position : Vector2.new(*position)
  @size     = size.is_a?(Vector2) ? size : Vector2.new(*size)
  freeze
end

Instance Attribute Details

#position ⇒ Object (readonly)

The position, size components.



15
16
17
# File 'lib/sfml/system/rect.rb', line 15

def position
  @position
end

#size ⇒ Object (readonly)

The position, size components.



15
16
17
# File 'lib/sfml/system/rect.rb', line 15

def size
  @size
end

Class Method Details

.from_native(struct) ⇒ Object

Returns the self.



81
82
83
84
85
86
# File 'lib/sfml/system/rect.rb', line 81

def self.from_native(struct) # :nodoc:
  new(
    Vector2.new(struct[:position][:x], struct[:position][:y]),
    Vector2.new(struct[:size][:x], struct[:size][:y]),
  )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Value equality — same position + same size.



57
58
59
# File 'lib/sfml/system/rect.rb', line 57

def ==(other)
  other.is_a?(Rect) && @position == other.position && @size == other.size
end

#bottom ⇒ Object

Y of the bottom edge (y + height).



38
# File 'lib/sfml/system/rect.rb', line 38

def bottom = @position.y + @size.y

#contains?(point) ⇒ Boolean

true if point (Vector2 or [x, y]) is inside this rect. The right and bottom edges are exclusive.

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/sfml/system/rect.rb', line 42

def contains?(point)
  px, py = point.is_a?(Vector2) ? [point.x, point.y] : [point[0], point[1]]
  px >= left && px < right && py >= top && py < bottom
end

#deconstruct ⇒ Object

Pattern-match support for in [x, y, w, h].



71
# File 'lib/sfml/system/rect.rb', line 71

def deconstruct = [x, y, width, height]

#deconstruct_keys(_keys) ⇒ Object

Pattern-match support for in {position:, size:}.



74
# File 'lib/sfml/system/rect.rb', line 74

def deconstruct_keys(_keys) = { position: @position, size: @size }

#hash ⇒ Object

Hash key support.



62
# File 'lib/sfml/system/rect.rb', line 62

def hash = [@position, @size].hash

#height ⇒ Object

Height.



31
# File 'lib/sfml/system/rect.rb', line 31

def height = @size.y

#intersects?(other) ⇒ Boolean

true if this rect overlaps other at all (any non-empty intersection counts).

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/sfml/system/rect.rb', line 49

def intersects?(other)
  left   < other.right  &&
    right  > other.left  &&
    top    < other.bottom &&
    bottom > other.top
end

#right ⇒ Object

X of the right edge (x + width).



36
37
# File 'lib/sfml/system/rect.rb', line 36

def right  = @position.x + @size.x
# Y of the bottom edge (`y + height`).

#to_a ⇒ Object

[x, y, width, height].



65
# File 'lib/sfml/system/rect.rb', line 65

def to_a = [x, y, width, height]

#to_h ⇒ Object

{position:, size:}.



68
# File 'lib/sfml/system/rect.rb', line 68

def to_h = { position: @position, size: @size }

#to_s ⇒ Object Also known as: inspect

String representation for debugging.



77
# File 'lib/sfml/system/rect.rb', line 77

def to_s = "Rect(x=#{x}, y=#{y}, w=#{width}, h=#{height})"

#width ⇒ Object

Width.



29
30
# File 'lib/sfml/system/rect.rb', line 29

def width  = @size.x
# Height.

#x ⇒ Object Also known as: left

Top-left X.



25
26
# File 'lib/sfml/system/rect.rb', line 25

def x      = @position.x
# Top-left Y.

#y ⇒ Object Also known as: top

Top-left Y.



27
28
# File 'lib/sfml/system/rect.rb', line 27

def y      = @position.y
# Width.