Class: GridGenerator::Skewb::ElementFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/skewb/element_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid_x:, grid_y:, row_num:, col_num:, side_size:, units:, colour:, opacity:) ⇒ ElementFactory

Returns a new instance of ElementFactory.



7
8
9
10
11
12
# File 'lib/grid_generator/skewb/element_factory.rb', line 7

def initialize(grid_x:, grid_y:, row_num:, col_num:, side_size:, units:, colour:, opacity:)
  @grid_x, @grid_y = grid_x, grid_y
  @row_num, @col_num = row_num, col_num
  @side_size, @units = side_size, units
  @colour, @opacity = colour, opacity
end

Instance Attribute Details

#col_numObject (readonly)

Returns the value of attribute col_num.



14
15
16
# File 'lib/grid_generator/skewb/element_factory.rb', line 14

def col_num
  @col_num
end

#colourObject (readonly)

Returns the value of attribute colour.



14
15
16
# File 'lib/grid_generator/skewb/element_factory.rb', line 14

def colour
  @colour
end

#grid_xObject (readonly)

Returns the value of attribute grid_x.



14
15
16
# File 'lib/grid_generator/skewb/element_factory.rb', line 14

def grid_x
  @grid_x
end

#grid_yObject (readonly)

Returns the value of attribute grid_y.



14
15
16
# File 'lib/grid_generator/skewb/element_factory.rb', line 14

def grid_y
  @grid_y
end

#opacityObject (readonly)

Returns the value of attribute opacity.



14
15
16
# File 'lib/grid_generator/skewb/element_factory.rb', line 14

def opacity
  @opacity
end

#row_numObject (readonly)

Returns the value of attribute row_num.



14
15
16
# File 'lib/grid_generator/skewb/element_factory.rb', line 14

def row_num
  @row_num
end

#side_sizeObject (readonly)

Returns the value of attribute side_size.



14
15
16
# File 'lib/grid_generator/skewb/element_factory.rb', line 14

def side_size
  @side_size
end

#unitsObject (readonly)

Returns the value of attribute units.



14
15
16
# File 'lib/grid_generator/skewb/element_factory.rb', line 14

def units
  @units
end

Instance Method Details

#buildObject



59
60
61
# File 'lib/grid_generator/skewb/element_factory.rb', line 59

def build
  GridGenerator::BaseElement.new(points: points, colour: colour, opacity: opacity)
end

#offsetObject



16
17
18
# File 'lib/grid_generator/skewb/element_factory.rb', line 16

def offset
  @offset ||= Matrix.column_vector([grid_x, grid_y])
end

#pointsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/grid_generator/skewb/element_factory.rb', line 20

def points
  _points = case [row_num, col_num]
  when [0, 0] # top left corner
    [
      anchors[:top_left_corner],
      anchors[:top_middle],
      anchors[:left_middle]
    ]
  when [0, 1] # top right corner
    [
      anchors[:top_middle],
      anchors[:top_right_corner],
      anchors[:right_middle]
    ]
  when [1, 0] # center
    [
      anchors[:top_middle],
      anchors[:right_middle],
      anchors[:bottom_middle],
      anchors[:left_middle]
    ]
  when [2, 0] # bottom left corner
    [
      anchors[:left_middle],
      anchors[:bottom_middle],
      anchors[:bottom_left_corner]
    ]
  when [2, 1] # bottom right corner
    [
      anchors[:right_middle],
      anchors[:bottom_right_corner],
      anchors[:bottom_middle]
    ]
  else
    []
  end
  _points.map { |p| p + offset }
end