Class: PSD::Node::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Ancestry, BuildPreview, LayerComps, Locking, 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 Locking

#all_locked?, #any_locked?, #composite_locked?, #position_locked?, #transparency_locked?

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, #find_by_id

Methods included from Ancestry

#ancestors, #childless?, #depth, #descendants, #has_children?, #has_siblings?, #method_missing, #next_sibling, #only_child?, #path, #prev_sibling, #root, #root?, #siblings, #subtree

Constructor Details

#initialize(layer, parent = nil) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/psd/node.rb', line 31

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

  @parent = parent
  @children = []
  
  @id = begin layer.layer_id.id rescue nil end
  @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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PSD::Node::Ancestry

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



25
26
27
# File 'lib/psd/node.rb', line 25

def children
  @children
end

#force_visibleObject

Returns the value of attribute force_visible.



25
26
27
# File 'lib/psd/node.rb', line 25

def force_visible
  @force_visible
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/psd/node.rb', line 24

def id
  @id
end

#layerObject

Returns the value of attribute layer.



25
26
27
# File 'lib/psd/node.rb', line 25

def layer
  @layer
end

#left_offsetObject

Returns the value of attribute left_offset.



25
26
27
# File 'lib/psd/node.rb', line 25

def left_offset
  @left_offset
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/psd/node.rb', line 24

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



24
25
26
# File 'lib/psd/node.rb', line 24

def parent
  @parent
end

#top_offsetObject

Returns the value of attribute top_offset.



25
26
27
# File 'lib/psd/node.rb', line 25

def top_offset
  @top_offset
end

Instance Method Details

#bottomObject



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

def bottom
  @bottom + @top_offset
end

#clipping_maskObject Also known as: clipped_by



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/psd/node.rb', line 82

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

#color_labelObject

Color label is a little tricky. If you set the color of a group, all of it’s descendants inhert the color unless manually overridden. So, if this node has no defined color, we have to walk up the ancestry tree to make sure the color isn’t set somewhere else.



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

def color_label
  color = layer.sheet_color.color
  return color if color != :no_color || node.parent.root?

  parent.color_label
end

#debug_nameObject



115
116
117
# File 'lib/psd/node.rb', line 115

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

#group?(include_root = true) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/psd/node.rb', line 111

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

#heightObject



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

def height
  bottom - top
end

#hidden?Boolean

Returns:

  • (Boolean)


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

def hidden?
  !visible?
end

#layer?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/psd/node.rb', line 107

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

#leftObject



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

def left
  @left + @left_offset
end

#rightObject



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

def right
  @right + @left_offset
end

#to_hashObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/psd/node.rb', line 119

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



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

def top
  @top + @top_offset
end

#visible?Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/psd/node.rb', line 77

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

#widthObject



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

def width
  right - left
end