Sha256: 80d8b4d762bd6441b3c0bd0cc505b1eadc409b35cde8039abc3362283c3889bc

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

require_relative 'node'

class PSD::Node
  class Layer < PSD::Node
    include PSD::Node::LockToOrigin

    attr_reader :layer

    # Stores a reference to the PSD::Layer
    def initialize(layer)
      super([])
      
      @layer = layer
      layer.node = self
    end

    # Delegates some methods to the PSD::Layer
    (PROPERTIES + [:text, :ref_x, :ref_y]).each do |meth|
      define_method meth do
        @layer.send(meth)
      end

      define_method "#{meth}=" do |val|
        @layer.send("#{meth}=", val)
      end
    end

    # Attempt to translate the layer.
    def translate(x=0, y=0)
      @layer.translate x, y
    end

    # Attempt to scale the path components of the layer.
    def scale_path_components(xr, yr)
      @layer.scale_path_components(xr, yr)
    end

    # Tries to hide the layer by moving it way off canvas.
    def hide!
      # TODO actually mess with the blend modes instead of
      # just putting things way off canvas
      return if @hidden_by_kelly
      translate(100000, 10000)
      @hidden_by_kelly = true
    end

    # Tries to re-show the canvas by moving it back to it's original position.
    def show!
      if @hidden_by_kelly
        translate(-100000, -10000)
        @hidden_by_kelly = false
      end
    end

    # Exports this layer to a Hash.
    def to_hash
      super.merge({
        type: :layer,
        text: @layer.text,
        ref_x: @layer.reference_point.x,
        ref_y: @layer.reference_point.y,
        mask: @layer.mask.snapshot,
        image: {
          width: @layer.image.width,
          height: @layer.image.height,
          channels: @layer.channels_info
        }
      })
    end

    # If the method is missing, we blindly send it to the layer.
    # The layer handles the case in which the method doesn't exist.
    def method_missing(method, *args, &block)
      @layer.send(method, *args, &block)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
psd-1.4.4 lib/psd/node_layer.rb
psd-1.4.3 lib/psd/node_layer.rb
psd-1.4.2 lib/psd/node_layer.rb
psd-1.4.1 lib/psd/node_layer.rb
psd-1.4.0 lib/psd/node_layer.rb