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 {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
-
#position ⇒ Object
readonly
The position, size components.
-
#size ⇒ Object
readonly
The position, size components.
Class Method Summary collapse
-
.from_native(struct) ⇒ Object
Returns the self.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Value equality — same position + same size.
-
#bottom ⇒ Object
Y of the bottom edge (
y + height). -
#contains?(point) ⇒ Boolean
trueifpoint(Vector2 or[x, y]) is inside this rect. -
#deconstruct ⇒ Object
Pattern-match support for
in [x, y, w, h]. -
#deconstruct_keys(_keys) ⇒ Object
Pattern-match support for
in {position:, size:}. -
#hash ⇒ Object
Hash key support.
-
#height ⇒ Object
Height.
-
#initialize(position, size) ⇒ Rect
constructor
positionandsizemay each be aVector2OR a[x, y]Array. -
#intersects?(other) ⇒ Boolean
trueif this rect overlapsotherat all (any non-empty intersection counts). -
#right ⇒ Object
X of the right edge (
x + width). -
#to_a ⇒ Object
[x, y, width, height]. -
#to_h ⇒ Object
{position:, size:}. -
#to_s ⇒ Object
(also: #inspect)
String representation for debugging.
-
#width ⇒ Object
Width.
-
#x ⇒ Object
(also: #left)
Top-left X.
-
#y ⇒ Object
(also: #top)
Top-left Y.
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
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.
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).
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. |