Class: RubySprites::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/lash-sprites/sprite.rb

Overview

This class is main image sprite creator. It allows for reading existing sprites, adding images to sprites, and repacking sprites on updates

Constant Summary collapse

@@DEFAULT_OPTIONS =

A hash of the default options a sprite uses. These should be sufficient for most usage.

{
  :graphics_manager => nil, # The image engine to use, may be :rmagick or :gd2
  :pack_type => 'vertical_split', # Which algorithm should be used to pack images
  :force_update => false, # Should the sprite image be forced to update, even if it appears up to date?
  :write_files => true, # Decides whether the script should actually write the sprite and image files, used mainly for testing.
  :repeat => false # Whether the sprite repeats horizontally or vertically
}
@@managers =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, file_root, options = {}) ⇒ Sprite

Creates a sprite object. Takes an file_root, absolute or relative, a sprite filename relative to the file root, and an options hash.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lash-sprites/sprite.rb', line 34

def initialize(filename, file_root, options = {})
  @options = @@DEFAULT_OPTIONS.merge(options)

  @file_root = File.expand_path(file_root)
  @file_root += '/' unless @file_root[-1, 1] == '/'
  @filename = filename
  @image_file = File.expand_path(@file_root + @filename)
  @sprite_file = "#{@image_file}.sprite"

  @width = 0;
  @height = 0
  @blocks = []
  @image_queue = []
  @images = {}

  if File.exists?(@image_file) && File.exists?(@sprite_file)
    @mtime = File.mtime(@image_file)
    read_file
  end
end

Instance Attribute Details

#file_rootObject (readonly)

Returns the value of attribute file_root.



30
31
32
# File 'lib/lash-sprites/sprite.rb', line 30

def file_root
  @file_root
end

#filenameObject (readonly)

Returns the value of attribute filename.



30
31
32
# File 'lib/lash-sprites/sprite.rb', line 30

def filename
  @filename
end

#heightObject (readonly)

Returns the value of attribute height.



30
31
32
# File 'lib/lash-sprites/sprite.rb', line 30

def height
  @height
end

#image_fileObject (readonly)

Returns the value of attribute image_file.



30
31
32
# File 'lib/lash-sprites/sprite.rb', line 30

def image_file
  @image_file
end

#imagesObject (readonly)

Returns the value of attribute images.



30
31
32
# File 'lib/lash-sprites/sprite.rb', line 30

def images
  @images
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



30
31
32
# File 'lib/lash-sprites/sprite.rb', line 30

def mtime
  @mtime
end

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/lash-sprites/sprite.rb', line 30

def options
  @options
end

#widthObject (readonly)

Returns the value of attribute width.



30
31
32
# File 'lib/lash-sprites/sprite.rb', line 30

def width
  @width
end

Class Method Details

.graphics_managersObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lash-sprites/sprite.rb', line 121

def self.graphics_managers
  
  if @@managers.nil?
    @@managers = {}
    
    dir = File.dirname(__FILE__)

    Dir.foreach("#{dir}/graphics_manager") do |file|
      next unless file.match(/\.rb$/)
      begin
        require("#{dir}/graphics_manager/#{file}")
        class_name = file.gsub('.rb', '').capitalize.gsub(/_([a-z]+)/) {|x| $1.capitalize}
        @@managers[file.gsub('.rb', '').to_sym] = GraphicsManager.const_get(class_name) if GraphicsManager.const_get(class_name).availible?
      rescue Exception => a
        puts a
      end
    end
  end

  return @@managers
end

.set_default(key, value) ⇒ Object

Lets one programatically override the default option values if you are generating multiple sprites



26
27
28
# File 'lib/lash-sprites/sprite.rb', line 26

def self.set_default(key, value)
  @@DEFAULT_OPTIONS[key.to_sym] = value
end

Instance Method Details

#add_image(img_path) ⇒ Object

Adds the image in the relative path to the sprite.



86
87
88
# File 'lib/lash-sprites/sprite.rb', line 86

def add_image(img_path)
  @image_queue.push Image.new(img_path, self, 0, 0) if @images[img_path].nil?
end

#add_images(img_paths) ⇒ Object

Adds the images in the array of relative paths to the sprite.



91
92
93
94
95
# File 'lib/lash-sprites/sprite.rb', line 91

def add_images(img_paths)
  img_paths.each do |path|
    add_image(path)
  end
end

#destroy!Object

Destroys the sprite, deleting its related files and freeing up memory



60
61
62
63
64
# File 'lib/lash-sprites/sprite.rb', line 60

def destroy!
  File.unlink @image_file if File.exists?(@image_file)
  File.unlink @sprite_file if File.exists?(@sprite_file)
  initialize(@filename, @file_root, @options)
end

#graphics_managerObject

Returns a Graphics manager based on the sprite options that will be used for this sprite.



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/lash-sprites/sprite.rb', line 145

def graphics_manager
  if @graphics_manager.nil?
    Sprite.graphics_managers
    if @options[:graphics_manager].nil?
      @graphics_manager = @@managers.values[0].new(self)
    elsif @@managers[@options[:graphics_manager].to_sym].nil?
      throw "Invalid Image Manager"
    else
      @graphics_manager = @@managers[@options[:graphics_manager].to_sym].new(self)
    end
  end
  return @graphics_manager
end

#image_current?(imagepath) ⇒ Boolean

Determines if the image in the relative path exists within the sprite and has been updated since hte sprite was generated.

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/lash-sprites/sprite.rb', line 68

def image_current?(imagepath)
  img = @images[imagepath]
  return !img.nil? && img.exists? && !@mtime.nil? && img.mtime <= @mtime
end

#image_info(imagepath) ⇒ Object

Returns the x position, y position, width, and height of the image if it exists in the sprite.



75
76
77
78
79
80
81
82
83
# File 'lib/lash-sprites/sprite.rb', line 75

def image_info(imagepath)
  return nil if @images[imagepath].nil?
  return {:x => @images[imagepath].x,
          :y => @images[imagepath].y,
          :width => @images[imagepath].width,
          :height => @images[imagepath].height,
          :mtime => @images[imagepath].mtime,
          :path => @images[imagepath].path}
end

#set_option(key, val) ⇒ Object



55
56
57
# File 'lib/lash-sprites/sprite.rb', line 55

def set_option(key, val)
  @options[key.to_sym] = val
end

#updateObject

Updates the sprite files if it detects changes to the sprite or the force option is set.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lash-sprites/sprite.rb', line 99

def update
  update = @options[:force_update] || !@image_queue.empty? || @mtime.nil?
  if update
    @images.each do |id, img|
      if @mtime.nil? || img.mtime.nil? || img.mtime >= @mtime
        update = true
        break
      end
    end
  end
  if update
    pack
    if @options[:write_files]
      write_image unless @height == 0 || @width == 0
      write_sprite_file
    end
    @mtime = Time.now
  end
end