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 {x:, y:, size: w, y: h}` is what the 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

Returns a new instance of Rect.



16
17
18
19
20
# File 'lib/sfml/system/rect.rb', line 16

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

#positionObject (readonly)

Returns the value of attribute position.



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

def position
  @position
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Class Method Details

.from_native(struct) ⇒ Object

:nodoc:



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

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?



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

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

#bottomObject



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

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

#contains?(point) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#deconstructObject



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

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

#deconstruct_keys(_keys) ⇒ Object



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

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

#hashObject



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

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

#heightObject



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

def height = @size.y

#intersects?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/sfml/system/rect.rb', line 36

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

#rightObject



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

def right  = @position.x + @size.x

#to_aObject



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

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

#to_hObject



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

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

#to_sObject Also known as: inspect



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

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

#widthObject



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

def width  = @size.x

#xObject Also known as: left



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

def x      = @position.x

#yObject Also known as: top



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

def y      = @position.y