Class: RubyTUI::Rect
- Inherits:
-
Object
- Object
- RubyTUI::Rect
- Defined in:
- lib/rubytui/rect.rb
Overview
A rectangle defined by position and size, used throughout the layout and rendering system to define widget areas.
Constant Summary collapse
- ZERO =
Empty rectangle at the origin (0, 0, 0x0).
new(x: 0, y: 0, width: 0, height: 0).freeze
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
-
.[](x, y, width, height) ⇒ Rect
Shorthand positional constructor.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Value equality: same position and size.
-
#area ⇒ Integer
Total cell count (width * height).
-
#bottom ⇒ Integer
Bottom edge (y + height).
-
#empty? ⇒ Boolean
True if width or height is zero.
-
#hash ⇒ Integer
Hash derived from x, y, width and height.
-
#initialize(*args, x: nil, y: nil, width: nil, height: nil) ⇒ Rect
constructor
Create a new Rect.
-
#inner(margin) ⇒ Rect
Return a Rect inset by the given margin on all sides.
-
#inner_rect(top: 0, right: 0, bottom: 0, left: 0) ⇒ Rect
Return a Rect inset by individual margins per side.
-
#inspect ⇒ String
Same as #to_s.
-
#left ⇒ Integer
Left edge (alias for #x).
-
#right ⇒ Integer
Right edge (x + width).
-
#to_s ⇒ String
Human-readable form, e.g.
-
#top ⇒ Integer
Top edge (alias for #y).
Constructor Details
#initialize(x, y, width, height) ⇒ Rect #initialize(x:, y:, width:, height:) ⇒ Rect
Create a new Rect.
Supports both positional and keyword arguments.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rubytui/rect.rb', line 32 def initialize(*args, x: nil, y: nil, width: nil, height: nil) if args.length == 4 @x, @y, w, h = args elsif args.empty? && x && y && width && height @x = x @y = y w = width h = height else raise ArgumentError, "Rect.new(x, y, width, height) or Rect.new(x:, y:, width:, height:)" end @width = [w, 0].max @height = [h, 0].max end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
13 14 15 |
# File 'lib/rubytui/rect.rb', line 13 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
13 14 15 |
# File 'lib/rubytui/rect.rb', line 13 def width @width end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
13 14 15 |
# File 'lib/rubytui/rect.rb', line 13 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
13 14 15 |
# File 'lib/rubytui/rect.rb', line 13 def y @y end |
Class Method Details
.[](x, y, width, height) ⇒ Rect
Shorthand positional constructor.
119 120 121 |
# File 'lib/rubytui/rect.rb', line 119 def self.[](x, y, width, height) new(x, y, width, height) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Value equality: same position and size.
95 96 97 98 |
# File 'lib/rubytui/rect.rb', line 95 def ==(other) other.is_a?(Rect) && @x == other.x && @y == other.y && @width == other.width && @height == other.height end |
#area ⇒ Integer
Returns total cell count (width * height).
56 57 |
# File 'lib/rubytui/rect.rb', line 56 def area = @width * @height # @return [Boolean] true if width or height is zero |
#bottom ⇒ Integer
Returns bottom edge (y + height).
50 51 |
# File 'lib/rubytui/rect.rb', line 50 def bottom = @y + @height # @return [Integer] left edge (alias for {#x}) |
#empty? ⇒ Boolean
Returns true if width or height is zero.
58 |
# File 'lib/rubytui/rect.rb', line 58 def empty? = @width == 0 || @height == 0 |
#hash ⇒ Integer
Returns hash derived from x, y, width and height.
102 |
# File 'lib/rubytui/rect.rb', line 102 def hash = [@x, @y, @width, @height].hash |
#inner(margin) ⇒ Rect
Return a Rect inset by the given margin on all sides.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rubytui/rect.rb', line 64 def inner(margin) margin_h = [margin * 2, @width].min margin_v = [margin * 2, @height].min Rect.new( x: @x + margin_h / 2, y: @y + margin_v / 2, width: @width - margin_h, height: @height - margin_v ) end |
#inner_rect(top: 0, right: 0, bottom: 0, left: 0) ⇒ Rect
Return a Rect inset by individual margins per side.
82 83 84 85 86 87 88 89 |
# File 'lib/rubytui/rect.rb', line 82 def inner_rect(top: 0, right: 0, bottom: 0, left: 0) Rect.new( x: @x + left, y: @y + top, width: @width - left - right, height: @height - top - bottom ) end |
#inspect ⇒ String
Returns same as #to_s.
107 |
# File 'lib/rubytui/rect.rb', line 107 def inspect = to_s |
#left ⇒ Integer
Returns left edge (alias for #x).
52 53 |
# File 'lib/rubytui/rect.rb', line 52 def left = @x # @return [Integer] top edge (alias for {#y}) |
#right ⇒ Integer
Returns right edge (x + width).
48 49 |
# File 'lib/rubytui/rect.rb', line 48 def right = @x + @width # @return [Integer] bottom edge (y + height) |
#to_s ⇒ String
Returns human-readable form, e.g. Rect(0, 0, 80x24).
105 106 |
# File 'lib/rubytui/rect.rb', line 105 def to_s = "Rect(#{@x}, #{@y}, #{@width}x#{@height})" # @return [String] same as {#to_s} |
#top ⇒ Integer
Returns top edge (alias for #y).
54 55 |
# File 'lib/rubytui/rect.rb', line 54 def top = @y # @return [Integer] total cell count (width * height) |