Class: Vedeu::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/support/geometry.rb

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Geometry

Returns a new instance of Geometry.



8
9
10
11
12
13
14
15
16
17
# File 'lib/vedeu/support/geometry.rb', line 8

def initialize(attrs = {})
  @attrs           = attrs
  @height          = attrs.fetch(:height)
  @width           = attrs.fetch(:width)
  @terminal_height = attrs.fetch(:terminal_height, Terminal.height)
  @terminal_width  = attrs.fetch(:terminal_width,  Terminal.width)
  @y               = attrs.fetch(:y, 1)
  @x               = attrs.fetch(:x, 1)
  @centred         = attrs.fetch(:centred, true)
end

Instance Method Details

#bottomObject



107
108
109
# File 'lib/vedeu/support/geometry.rb', line 107

def bottom
  top + height
end

#centreObject



79
80
81
# File 'lib/vedeu/support/geometry.rb', line 79

def centre
  [(terminal_height / 2), (terminal_width / 2)]
end

#centre_xObject



87
88
89
# File 'lib/vedeu/support/geometry.rb', line 87

def centre_x
  centre.last
end

#centre_yObject



83
84
85
# File 'lib/vedeu/support/geometry.rb', line 83

def centre_y
  centre.first
end

#centredObject



75
76
77
# File 'lib/vedeu/support/geometry.rb', line 75

def centred
  !!(@centred)
end

#heightObject



33
34
35
36
37
38
39
# File 'lib/vedeu/support/geometry.rb', line 33

def height
  fail_if_less_than_one(@height)

  fail_if_out_of_bounds('height', @height, terminal_height)

  @height
end

#leftObject



99
100
101
102
103
104
105
# File 'lib/vedeu/support/geometry.rb', line 99

def left
  if centred
    centre_x - (width / 2)
  else
    x
  end
end

#origin(index = 0) ⇒ Object



19
20
21
# File 'lib/vedeu/support/geometry.rb', line 19

def origin(index = 0)
  Esc.set_position(virtual_y(index), virtual_x)
end

#positionObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vedeu/support/geometry.rb', line 115

def position
  {
    y:       top,
    x:       left,
    height:  height,
    width:   width,
    centred: centred,
    top:     top,
    bottom:  bottom,
    left:    left,
    right:   right,
  }
end

#rightObject



111
112
113
# File 'lib/vedeu/support/geometry.rb', line 111

def right
  left + width
end

#terminal_heightObject



23
24
25
26
27
28
29
30
31
# File 'lib/vedeu/support/geometry.rb', line 23

def terminal_height
  fail_if_less_than_one(@terminal_height)

  fail_if_out_of_bounds('terminal_height',
                        @terminal_height,
                        Terminal.height)

  @terminal_height
end

#terminal_widthObject



49
50
51
52
53
54
55
56
57
# File 'lib/vedeu/support/geometry.rb', line 49

def terminal_width
  fail_if_less_than_one(@terminal_width)

  fail_if_out_of_bounds('terminal_width',
                        @terminal_width,
                        Terminal.width)

  @terminal_width
end

#topObject



91
92
93
94
95
96
97
# File 'lib/vedeu/support/geometry.rb', line 91

def top
  if centred
    centre_y - (height / 2)
  else
    y
  end
end

#widthObject



59
60
61
62
63
64
65
# File 'lib/vedeu/support/geometry.rb', line 59

def width
  fail_if_less_than_one(@width)

  fail_if_out_of_bounds('width', @width, terminal_width)

  @width
end

#xObject



67
68
69
70
71
72
73
# File 'lib/vedeu/support/geometry.rb', line 67

def x
  fail_if_less_than_one(@x)

  fail_if_out_of_bounds('x', @x, terminal_width)

  @x
end

#yObject



41
42
43
44
45
46
47
# File 'lib/vedeu/support/geometry.rb', line 41

def y
  fail_if_less_than_one(@y)

  fail_if_out_of_bounds('y', @y, terminal_height)

  @y
end