Class: RubyTUI::Rect

Inherits:
Object
  • Object
show all
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.

Examples:

Create with keyword arguments

Rect.new(x: 0, y: 0, width: 80, height: 24)

Create with positional arguments

Rect.new(0, 0, 80, 24)

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width, height) ⇒ Rect #initialize(x:, y:, width:, height:) ⇒ Rect

Create a new Rect.

Supports both positional and keyword arguments.

Overloads:

  • #initialize(x, y, width, height) ⇒ Rect

    Parameters:

    • x (Integer) —

      left edge position

    • y (Integer) —

      top edge position

    • width (Integer) —

      width (clamped to >= 0)

    • height (Integer) —

      height (clamped to >= 0)

  • #initialize(x:, y:, width:, height:) ⇒ Rect

    Parameters:

    • x (Integer) —

      left edge position

    • y (Integer) —

      top edge position

    • width (Integer) —

      width (clamped to >= 0)

    • height (Integer) —

      height (clamped to >= 0)

Raises:

  • (ArgumentError) —

    if argument format is invalid



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.

Examples:

Rect[0, 0, 80, 24]

Parameters:

  • x (Integer) —

    left edge

  • y (Integer) —

    top edge

  • width (Integer) —

    width

  • height (Integer) —

    height

Returns:



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.

Parameters:

  • other (Object) —

    object to compare

Returns:

  • (Boolean) —

    true if other is a Rect with equal x, y, width and height



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).

Returns:

  • (Integer) —

    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).

Returns:

  • (Integer) —

    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.

Returns:

  • (Boolean) —

    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.

Returns:

  • (Integer) —

    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.

Parameters:

  • margin (Integer) —

    inset distance for all four sides

Returns:

  • (Rect) —

    the inner rectangle



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.

Parameters:

  • top (Integer) (defaults to: 0) —

    inset from top

  • right (Integer) (defaults to: 0) —

    inset from right

  • bottom (Integer) (defaults to: 0) —

    inset from bottom

  • left (Integer) (defaults to: 0) —

    inset from left

Returns:

  • (Rect) —

    the inner rectangle



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.

Returns:

  • (String) —

    same as #to_s



107
# File 'lib/rubytui/rect.rb', line 107

def inspect = to_s

#left ⇒ Integer

Returns left edge (alias for #x).

Returns:

  • (Integer) —

    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).

Returns:

  • (Integer) —

    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).

Returns:

  • (String) —

    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).

Returns:

  • (Integer) —

    top edge (alias for #y)



54
55
# File 'lib/rubytui/rect.rb', line 54

def top    = @y
# @return [Integer] total cell count (width * height)