Sha256: 77106e2b816df98c9e5a398f64fc33e61d2b574a64650538de0fd8dbb6830803

Contents?: true

Size: 1.72 KB

Versions: 20

Compression:

Stored size: 1.72 KB

Contents

require_relative '../face_parser'

module GridGenerator
  module Skewb
    class SkewbGrid
      COLOURS = {
        fill: "#d0d0d0",
        stroke: "#404040"
      }

      def initialize(x:, y:, units: , elements: )
        @x, @y = x, y
        @units = units
        @elements = case elements 
        when String
          FaceParser.new(elements).parse
        when Array
          elements 
        else
          raise ArgumentError, "squares must be array or string"
        end
      end
  
      attr_reader :x, :y, :units, :elements
  
      def side_size 
        elements.size - 1
      end
  
      def build_element(row_num, col_num, data)
        if data
          factory_class.new(
            grid_x: x,
            grid_y: y,
            row_num: row_num,
            col_num: col_num,
            side_size: side_size,
            units: units,
            colour: data[:colour],
            opacity: data[:opacity]
          ).build
        else
          nil
        end
      end
  
      def element_shapes
        elements.each_with_index.map do |row, row_num|
          row.each_with_index.map do |col, col_num|
            build_element(row_num, col_num, col)
          end
        end.flatten.compact
      end

      def base_shape_style
        GridGenerator::Svg::Style.new(fill: COLOURS[:fill], stroke: COLOURS[:stroke])
      end

      def base_shape
        GridGenerator::Svg::Polygon.new(points: points, style: base_shape_style)
      end

      def to_svg
        output = base_shape.to_svg

        rows.each { |row| output += row.to_svg }
        columns.each { |col| output += col.to_svg }
        element_shapes.each { |element| output += element.to_svg if element } 

        output
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
grid_generator-0.6.7 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.6.6 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.6.5 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.6.4 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.6.3 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.6.2 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.6.1 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.6.0 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.5.3 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.5.2 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.5.1 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.5.0 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.4.5 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.4.4 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.4.3 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.4.2 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.4.1 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.4.0 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.3.3 lib/grid_generator/skewb/skewb_grid.rb
grid_generator-0.3.2 lib/grid_generator/skewb/skewb_grid.rb