Class: GosuEnhanced::Region
- Inherits:
-
Object
- Object
- GosuEnhanced::Region
- Extended by:
- Forwardable
- Defined in:
- lib/gosu_enhanced/region.rb
Overview
Hold a rectangular region specified by a Point and a Size Most functions are delegated to the constituent Point and Size
Instance Attribute Summary collapse
-
#position ⇒ Object
readonly
Top left corner.
-
#size ⇒ Object
readonly
Overall Width and Height.
Instance Method Summary collapse
-
#contains?(col, row = nil) ⇒ Boolean
Return whether the region contains the specified
row
andcol
Alternatively, can be passed aPoint
. -
#draw(surface, z_order, colour) ⇒ Object
Draw a rectangle on the specified
surface
at the specifiedz_order
and with the specifiedcolour
. -
#dup ⇒ Object
Duplicate a Region, must be done explicitly to avoid aliasing.
-
#initialize(pos, size) ⇒ Region
constructor
Create a new region with specified
pos
as top left corner andsize
as width and height. -
#left ⇒ Object
Return the leftmost co-ordinate.
-
#to_s ⇒ Object
Return a string representation of the region’s position and size.
-
#top ⇒ Object
Return the topmost co-ordinate.
Constructor Details
#initialize(pos, size) ⇒ Region
Create a new region with specified pos
as top left corner and size
as width and height. The stored positions are copies of the passed position and size to avoid aliasing.
Alternatively, can be initialized with 2 Points.
24 25 26 27 28 29 |
# File 'lib/gosu_enhanced/region.rb', line 24 def initialize(pos, size) size = Size.new(size.x - pos.x, size.y - pos.y) if size.respond_to? :x @position = pos.dup @size = size.dup end |
Instance Attribute Details
#position ⇒ Object (readonly)
Top left corner
14 15 16 |
# File 'lib/gosu_enhanced/region.rb', line 14 def position @position end |
#size ⇒ Object (readonly)
Overall Width and Height
16 17 18 |
# File 'lib/gosu_enhanced/region.rb', line 16 def size @size end |
Instance Method Details
#contains?(col, row = nil) ⇒ Boolean
Return whether the region contains the specified row
and col
Alternatively, can be passed a Point
33 34 35 36 37 38 |
# File 'lib/gosu_enhanced/region.rb', line 33 def contains?(col, row = nil) return contains_point?(col) if col.respond_to? :x col.between?(left, left + width - 1) && row.between?(top, top + height - 1) end |
#draw(surface, z_order, colour) ⇒ Object
Draw a rectangle on the specified surface
at the specified z_order
and with the specified colour
57 58 59 |
# File 'lib/gosu_enhanced/region.rb', line 57 def draw(surface, z_order, colour) surface.draw_rectangle(position, size, z_order, colour) end |
#dup ⇒ Object
Duplicate a Region, must be done explicitly to avoid aliasing.
51 52 53 |
# File 'lib/gosu_enhanced/region.rb', line 51 def dup Region.new(position, size) end |
#left ⇒ Object
Return the leftmost co-ordinate
46 47 48 |
# File 'lib/gosu_enhanced/region.rb', line 46 def left position.x end |
#to_s ⇒ Object
Return a string representation of the region’s position and size
62 63 64 |
# File 'lib/gosu_enhanced/region.rb', line 62 def to_s "<GosuEnhanced::Region: #{position}, #{size}>" end |
#top ⇒ Object
Return the topmost co-ordinate
41 42 43 |
# File 'lib/gosu_enhanced/region.rb', line 41 def top position.y end |