Class: Hokusai::Commands::Rect

Inherits:
Base
  • Object
show all
Defined in:
ui/src/hokusai/commands/rect.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after_draw, draw, #draw, on_draw

Constructor Details

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

Returns a new instance of Rect.



7
8
9
10
11
12
13
14
15
16
17
# File 'ui/src/hokusai/commands/rect.rb', line 7

def initialize(x, y, width, height)
  @x = x.to_f
  @y = y.to_f
  @width = width.to_f
  @height = height.to_f
  @outline = Outline.default
  @rounding = 0.0
  @color = Color.new(0, 0, 0, 0)
  @outline_color = Color.new(0, 0, 0, 255)
  @padding = Padding.new(0.0, 0.0, 0.0, 0.0)
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def color
  @color
end

#heightObject (readonly)

Returns the value of attribute height.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def height
  @height
end

#outlineObject

Returns the value of attribute outline.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def outline
  @outline
end

#outline_colorObject

Returns the value of attribute outline_color.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def outline_color
  @outline_color
end

#paddingObject

Returns the value of attribute padding.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def padding
  @padding
end

#roundingObject (readonly)

Returns the value of attribute rounding.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def rounding
  @rounding
end

#widthObject (readonly)

Returns the value of attribute width.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



3
4
5
# File 'ui/src/hokusai/commands/rect.rb', line 3

def y
  @y
end

Instance Method Details

#background_boundaryObject

Returns a tuple with the computed geometric inner boundary for this rectangle with outlines subtracted



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ui/src/hokusai/commands/rect.rb', line 119

def background_boundary
  nx = x.dup
  ny = y.dup
  nw = width.dup
  nh = height.dup

  if outline.top > 0.0
    ny += outline.top
    nh -= outline.top
  end

  if outline.left > 0.0
    nx += outline.left
    nw -= outline.left
  end

  if outline.bottom > 0.0
    nh -= outline.bottom
  end

  if outline.right > 0.0
    nw -= outline.right
  end

  [nx, ny, nw, nh]
end

#boundaryObject

Returns a tuple with the geometric boundary for this rectangle



112
113
114
# File 'ui/src/hokusai/commands/rect.rb', line 112

def boundary
  [x, y, width, height]
end

#hObject

Shorthand for #height



43
44
45
# File 'ui/src/hokusai/commands/rect.rb', line 43

def h
  height
end

#hashObject



19
20
21
# File 'ui/src/hokusai/commands/rect.rb', line 19

def hash
  [self.class, x, y, width, height, rounding, color.hash, outline.hash, outline_color.hash, padding.hash].hash
end

#outline?Boolean

Returns true if this rectangle has an outline

Returns:

  • (Boolean)


148
149
150
# File 'ui/src/hokusai/commands/rect.rb', line 148

def outline?
  outline.present?
end

#outline_uniform?Boolean

Returns true if this rectangle’s outline is uniform

Returns:

  • (Boolean)


154
155
156
# File 'ui/src/hokusai/commands/rect.rb', line 154

def outline_uniform?
  outline.uniform?
end

#padding?Boolean

Returns true if the rectangle has any padding

Returns:

  • (Boolean)


104
105
106
107
108
# File 'ui/src/hokusai/commands/rect.rb', line 104

def padding?
  [padding.t, padding.r, padding.b, padding.l].any? do |p|
    p != 0.0
  end
end

#round=(amount) ⇒ Object

Rounding amount for this rect



97
98
99
100
101
# File 'ui/src/hokusai/commands/rect.rb', line 97

def round=(amount)
  @rounding = amount

  self
end

#trim_canvas(canvas) ⇒ Object

Modifies the parameter Canvas to offset the boundary with this rectangle’s computed geometry



26
27
28
29
30
31
32
33
34
35
# File 'ui/src/hokusai/commands/rect.rb', line 26

def trim_canvas(canvas)
  x, y, w, h = background_boundary

  canvas.x = x + padding.left + outline.left
  canvas.y = y + padding.top + outline.top
  canvas.width = w - (padding.left + padding.right + outline.left + outline.right)
  canvas.height = h - (padding.top + padding.bottom + outline.top + outline.bottom)

  canvas
end

#wObject

Shorthand for #width



38
39
40
# File 'ui/src/hokusai/commands/rect.rb', line 38

def w
  width
end