Class: PSD::Node::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Ancestry, BuildPreview, LayerComps, Search
Defined in:
lib/psd/node.rb

Direct Known Subclasses

Group, Layer, Root

Constant Summary collapse

PROPERTIES =

Default properties that all nodes contain

[:name, :left, :right, :top, :bottom, :height, :width]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BuildPreview

#renderer, #save_as_png, #to_png

Methods included from LayerComps

#filter_by_comp, #position_in_comp, #visible_in_comp?

Methods included from Search

#children_at_path

Constructor Details

#initialize(layer, parent = nil) ⇒ Base

Returns a new instance of Base.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/psd/node.rb', line 28

def initialize(layer, parent = nil)
  @layer = layer
  @layer.node = self

  @parent = parent
  @children = []
  
  @force_visible = nil
  @top = @layer.top.to_i
  @bottom = @layer.bottom.to_i
  @left = @layer.left.to_i
  @right = @layer.right.to_i

  @top_offset = 0
  @left_offset = 0
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



21
22
23
# File 'lib/psd/node.rb', line 21

def children
  @children
end

#force_visibleObject

Returns the value of attribute force_visible.



21
22
23
# File 'lib/psd/node.rb', line 21

def force_visible
  @force_visible
end

#layerObject

Returns the value of attribute layer.



21
22
23
# File 'lib/psd/node.rb', line 21

def layer
  @layer
end

#left_offsetObject

Returns the value of attribute left_offset.



21
22
23
# File 'lib/psd/node.rb', line 21

def left_offset
  @left_offset
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/psd/node.rb', line 20

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



20
21
22
# File 'lib/psd/node.rb', line 20

def parent
  @parent
end

#top_offsetObject

Returns the value of attribute top_offset.



21
22
23
# File 'lib/psd/node.rb', line 21

def top_offset
  @top_offset
end

Instance Method Details

#bottomObject



49
50
51
# File 'lib/psd/node.rb', line 49

def bottom
  @bottom + @top_offset
end

#clipping_maskObject Also known as: clipped_by



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/psd/node.rb', line 78

def clipping_mask
  return nil unless @layer.clipped?

  @clipping_mask ||= (
    mask_node = next_sibling
    while mask_node.clipped?
      mask_node = mask_node.next_sibling
    end

    mask_node
  )
end

#debug_nameObject



100
101
102
# File 'lib/psd/node.rb', line 100

def debug_name
  root? ? ":root:" : name
end

#group?(include_root = true) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/psd/node.rb', line 96

def group?(include_root = true)
  is_a?(PSD::Node::Group) || (include_root && is_a?(PSD::Node::Root))
end

#heightObject



65
66
67
# File 'lib/psd/node.rb', line 65

def height
  bottom - top
end

#hidden?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/psd/node.rb', line 69

def hidden?
  !visible?
end

#layer?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/psd/node.rb', line 92

def layer?
  is_a?(PSD::Node::Layer)
end

#leftObject



53
54
55
# File 'lib/psd/node.rb', line 53

def left
  @left + @left_offset
end

#rightObject



57
58
59
# File 'lib/psd/node.rb', line 57

def right
  @right + @left_offset
end

#to_hashObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/psd/node.rb', line 104

def to_hash
  hash = {
    type: nil,
    visible: visible?,
    opacity: @layer.opacity / 255.0,
    blending_mode: @layer.blending_mode,
    layer_comps: {}
  }

  PROPERTIES.each do |p|
    hash[p] = self.send(p)
  end

  root.psd.layer_comps.each do |comp|
    hash[:layer_comps][comp[:name]] = {
      visible: visible_in_comp?(comp[:id]),
      position: position_in_comp(comp[:id])
    }
  end

  hash
end

#topObject



45
46
47
# File 'lib/psd/node.rb', line 45

def top
  @top + @top_offset
end

#visible?Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/psd/node.rb', line 73

def visible?
  return false if @layer.clipped? && !clipping_mask.visible?
  @force_visible.nil? ? @layer.visible? : @force_visible
end

#widthObject



61
62
63
# File 'lib/psd/node.rb', line 61

def width
  right - left
end