Class: Sprite::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/rmagick-sprite/action.rb

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, &blk) ⇒ Action

Returns a new instance of Action.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rmagick-sprite/action.rb', line 13

def initialize(options, &blk)
  raise 'options must be a Hash' unless options.respond_to?(:to_hash) || options.respond_to?(:to_h)
  options = options.to_hash rescue options.to_h
  
  raise 'options[:name] must be given' if options[:name].nil?
  raise 'options[:sprite] must be given' if options[:sprite].nil?
  raise 'options[:sprite] must be a Sprite' unless options[:sprite].is_a?(Sprite)
  
  @sprite, @name = options.values_at(:sprite, :name)
  @current_index, @frames = 0, []
  
  DSL.call(self, &blk)
end

Instance Attribute Details

#current_indexObject

Returns the value of attribute current_index.



11
12
13
# File 'lib/rmagick-sprite/action.rb', line 11

def current_index
  @current_index
end

#framesObject (readonly)

Returns the value of attribute frames.



10
11
12
# File 'lib/rmagick-sprite/action.rb', line 10

def frames
  @frames
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/rmagick-sprite/action.rb', line 10

def name
  @name
end

#spriteObject (readonly)

Returns the value of attribute sprite.



10
11
12
# File 'lib/rmagick-sprite/action.rb', line 10

def sprite
  @sprite
end

Instance Method Details

#add_frame(options) ⇒ Object



27
28
29
30
31
32
# File 'lib/rmagick-sprite/action.rb', line 27

def add_frame(options)
  raise 'options must be a Hash' unless options.respond_to?(:to_hash) || options.respond_to?(:to_h)
  options = options.to_hash rescue options.to_h
  options = { action: self }.merge(@sprite.default_frame_options).merge(options)
  @frames << Frame.new(options)
end

#current_frameObject



34
35
36
# File 'lib/rmagick-sprite/action.rb', line 34

def current_frame
  @frames[@current_index]
end

#current_imageObject



38
39
40
# File 'lib/rmagick-sprite/action.rb', line 38

def current_image
  current_frame.image
end

#increase_indexObject



42
43
44
45
# File 'lib/rmagick-sprite/action.rb', line 42

def increase_index
  @current_index += 1
  @current_index = 0 if @current_index >= @frames.length
end

#next_frameObject



47
48
49
50
# File 'lib/rmagick-sprite/action.rb', line 47

def next_frame
  increase_index
  current_frame
end

#next_imageObject



52
53
54
55
# File 'lib/rmagick-sprite/action.rb', line 52

def next_image
  increase_index
  current_image
end

#write(filename, &blk) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rmagick-sprite/action.rb', line 57

def write(filename, &blk)
 image_list = Magick::ImageList.new
 @frames.each do |frame|
   image = frame.image
   image_list << image
 end
 
 blk.call(image_list) if block_given?
 
 image_list.write(filename)
end