Class: Sprite

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

Defined Under Namespace

Classes: Action, DSL, Frame

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Sprite

Returns a new instance of Sprite.



20
21
22
23
24
# File 'lib/rmagick-sprite.rb', line 20

def initialize(&blk)
  @actions, @default_action_options, @default_frame_options = {}, {}, {}
  
  DSL.call(self, &blk)
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



17
18
19
# File 'lib/rmagick-sprite.rb', line 17

def action
  @action
end

#actionsObject (readonly)

Returns the value of attribute actions.



17
18
19
# File 'lib/rmagick-sprite.rb', line 17

def actions
  @actions
end

#default_actionObject

Returns the value of attribute default_action.



18
19
20
# File 'lib/rmagick-sprite.rb', line 18

def default_action
  @default_action
end

#default_action_optionsObject

Returns the value of attribute default_action_options.



18
19
20
# File 'lib/rmagick-sprite.rb', line 18

def default_action_options
  @default_action_options
end

#default_frame_optionsObject

Returns the value of attribute default_frame_options.



18
19
20
# File 'lib/rmagick-sprite.rb', line 18

def default_frame_options
  @default_frame_options
end

#filenameObject

Returns the value of attribute filename.



17
18
19
# File 'lib/rmagick-sprite.rb', line 17

def filename
  @filename
end

Instance Method Details

#add_action(name, options = {}, &blk) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/rmagick-sprite.rb', line 71

def add_action(name, 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
  options = { sprite: self, name: name }.merge(@default_action_options).merge(options)
    
  @actions[name] = Action.new(options, &blk)
end

#current_frameObject



93
94
95
# File 'lib/rmagick-sprite.rb', line 93

def current_frame
  action.current_frame
end

#current_imageObject



97
98
99
# File 'lib/rmagick-sprite.rb', line 97

def current_image
  action.current_image
end

#imageObject



79
80
81
82
83
84
# File 'lib/rmagick-sprite.rb', line 79

def image
  raise 'filename must be set' if @filename.nil?
  reload if @image.nil?
  
  @image
end

#next_frameObject



101
102
103
# File 'lib/rmagick-sprite.rb', line 101

def next_frame
  action.next_frame
end

#next_imageObject



105
106
107
# File 'lib/rmagick-sprite.rb', line 105

def next_image
  action.next_image
end

#reloadObject



86
87
88
89
90
91
# File 'lib/rmagick-sprite.rb', line 86

def reload
  @image = Magick::Image.read(@filename).first
  @actions.each { |name, action| action.frames.each(&:reload) }
  
  @image
end