class Phantom::SVG::Parser::AbstractAnimationReader

AnimationReader base.

Attributes

frames[RW]
loops[RW]
skip_first[RW]

Public Class Methods

new(path = nil) click to toggle source

Construct AbstractAnimationReader object.

# File lib/phantom/parser/abstract_animation_reader.rb, line 11
def initialize(path = nil)
  reset
  read(path) unless path.nil?
end

Public Instance Methods

read(path, do_reset = true) click to toggle source

Read and create frames from animation information file. Return array of Phantom::SVG::Frame.

# File lib/phantom/parser/abstract_animation_reader.rb, line 18
def read(path, do_reset = true)
  reset if do_reset

  # Read parameter from spec file.
  read_parameter(path)

  # Change current directory to animation information file's directory.
  old_dir = Dir.pwd
  Dir.chdir(File.dirname(path))

  # Create frames from animation information file parameter.
  create_frames

  # Change current directory to default.
  Dir.chdir(old_dir)

  # Return frames.
  @frames
end

Private Instance Methods

add_delay(delay) click to toggle source
# File lib/phantom/parser/abstract_animation_reader.rb, line 112
def add_delay(delay)
  @delays << str2delay(delay)
end
add_frame_info(name, delay = nil) click to toggle source
# File lib/phantom/parser/abstract_animation_reader.rb, line 108
def add_frame_info(name, delay = nil)
  @frame_infos << { name: name.to_s, delay: (delay.nil? ? delay : str2delay(delay)) }
end
create_file_list(path) click to toggle source

Create file list.

# File lib/phantom/parser/abstract_animation_reader.rb, line 80
def create_file_list(path)
  result = Dir.glob(path).sort_by { |k| k[/\d+/].to_i }
  if result.empty?
    result <<
      if    File.exist?(path)           then  path
      elsif File.exist?("#{path}.svg")  then  "#{path}.svg"
      elsif File.exist?("#{path}.png")  then  "#{path}.png"
      else                                    path # Illegal path.
      end
  end
  result
end
create_frames() click to toggle source

Create frames from parameter.

# File lib/phantom/parser/abstract_animation_reader.rb, line 52
def create_frames
  i = 0
  @frame_infos.each do |frame_info|
    create_file_list(frame_info[:name]).each do |file|
      reader = Parser::SVGReader.new(file, create_options(i, frame_info[:delay]))
      @frames += reader.frames
      @width = reader.width if @width.to_i < reader.width.to_i
      @height = reader.height if @height.to_i < reader.height.to_i
      i += 1
    end
  end
end
create_options(index, delay_override) click to toggle source

Create frame options.

# File lib/phantom/parser/abstract_animation_reader.rb, line 66
def create_options(index, delay_override)
  result = {}
  result[:duration] =
    if delay_override.nil?
      if @delays.empty? then  @default_delay
      else                    @delays[index % @delays.length]
      end
    else
      delay_override
    end
  result
end
reset() click to toggle source

Reset fields.

# File lib/phantom/parser/abstract_animation_reader.rb, line 41
def reset
  @name = ''
  @loops = 0
  @skip_first = false
  @default_delay = 100.0 / 1000.0
  @frame_infos = []
  @delays = []
  @frames = []
end
set_parameter(key, val) click to toggle source
# File lib/phantom/parser/abstract_animation_reader.rb, line 99
def set_parameter(key, val)
  case key
  when 'name'           then  @name = val.to_s
  when 'loops'          then  @loops = val.to_i
  when 'skip_first'     then  @skip_first = (val.to_s == 'true' ? true : false)
  when 'default_delay'  then  @default_delay = str2delay(val)
  end
end
str2delay(str) click to toggle source

Convert string to delay.

# File lib/phantom/parser/abstract_animation_reader.rb, line 94
def str2delay(str)
  tmp = str.to_s.split('/', 2)
  tmp[0].to_f / (tmp.length > 1 ? tmp[1].to_f : 1000.0)
end