Class: SFML::Rect
- Inherits:
-
Object
- Object
- SFML::Rect
- 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
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
-
.from_native(struct) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #bottom ⇒ Object
- #contains?(point) ⇒ Boolean
- #deconstruct ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #hash ⇒ Object
- #height ⇒ Object
-
#initialize(position, size) ⇒ Rect
constructor
A new instance of Rect.
- #intersects?(other) ⇒ Boolean
- #right ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #width ⇒ Object
- #x ⇒ Object (also: #left)
- #y ⇒ Object (also: #top)
Constructor Details
Instance Attribute Details
#position ⇒ Object (readonly)
Returns the value of attribute position.
14 15 16 |
# File 'lib/sfml/system/rect.rb', line 14 def position @position end |
#size ⇒ Object (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
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 |
#bottom ⇒ Object
29 |
# File 'lib/sfml/system/rect.rb', line 29 def bottom = @position.y + @size.y |
#contains?(point) ⇒ 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 |
#deconstruct ⇒ Object
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 } |
#hash ⇒ Object
47 |
# File 'lib/sfml/system/rect.rb', line 47 def hash = [@position, @size].hash |
#height ⇒ Object
25 |
# File 'lib/sfml/system/rect.rb', line 25 def height = @size.y |
#intersects?(other) ⇒ 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 |
#right ⇒ Object
28 |
# File 'lib/sfml/system/rect.rb', line 28 def right = @position.x + @size.x |
#to_a ⇒ Object
49 |
# File 'lib/sfml/system/rect.rb', line 49 def to_a = [x, y, width, height] |
#to_h ⇒ Object
50 |
# File 'lib/sfml/system/rect.rb', line 50 def to_h = { position: @position, size: @size } |
#to_s ⇒ Object 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})" |
#width ⇒ Object
24 |
# File 'lib/sfml/system/rect.rb', line 24 def width = @size.x |
#x ⇒ Object Also known as: left
22 |
# File 'lib/sfml/system/rect.rb', line 22 def x = @position.x |
#y ⇒ Object Also known as: top
23 |
# File 'lib/sfml/system/rect.rb', line 23 def y = @position.y |