Class: Sketch::Layout

Inherits:
Group show all
Defined in:
lib/sketch/layout.rb

Constant Summary

Constants inherited from Sketch

Arc, Circle, Line, Path, Polygon, Polyline, Rectangle, Square

Instance Attribute Summary collapse

Attributes inherited from Group

#transformation

Attributes inherited from Sketch

#bounds, #elements, #first, #geometry, #last, #max, #min, #minmax, #size, #transformation

Instance Method Summary collapse

Methods inherited from Group

#rotation, #scale, #translation

Methods inherited from Sketch

#add_arc, #add_circle, #add_line, #add_point, #add_rectangle, #add_square, #add_triangle, #define_parameter, define_parameter

Constructor Details

#initialize(direction = :horizontal, *args) ⇒ Layout

Returns a new instance of Layout.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sketch/layout.rb', line 26

def initialize(direction=:horizontal, *args)
    super

    options, args = args.partition {|a| a.is_a? Hash}
    options = options.reduce({}, :merge)

    @alignment = options.delete(:align)
    @spacing = options.delete(:spacing) || 0

    @direction = direction
end

Instance Attribute Details

#alignmentSymbol (readonly)

Returns alignment the layout alignment.

Returns:

  • (Symbol)

    alignment the layout alignment



18
19
20
# File 'lib/sketch/layout.rb', line 18

def alignment
  @alignment
end

#directionSymbol (readonly)

Returns direction the layout direction (either :horizontal or :vertical).

Returns:

  • (Symbol)

    direction the layout direction (either :horizontal or :vertical)



21
22
23
# File 'lib/sketch/layout.rb', line 21

def direction
  @direction
end

#spacingNumber (readonly)

Returns spacing spacing to add between each element.

Returns:

  • (Number)

    spacing spacing to add between each element



24
25
26
# File 'lib/sketch/layout.rb', line 24

def spacing
  @spacing
end

Instance Method Details

#push(element, *args) ⇒ Layout

Any pushed element that doesn’t have a transformation property will be wrapped in a Group.

Parameters:

  • element (Geometry)

    the geometry element to append

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sketch/layout.rb', line 41

def push(element, *args)
    max = last ? last.max : Point.zero

    offset = make_offset(element, element.min, max)

    if offset == Point.zero
	super element, *args
    else
	if element.respond_to?(:transformation=)
	    super element, *args
	else
	    super Group.new.push element, *args
	end

	last.transformation = Geometry::Transformation.new(origin:offset) + last.transformation
    end

    self
end