Class: Metro::Image

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/metro/image.rb

Overview

Image is a wrapper class for a Gosu Image. This allows for additional data to be stored without relying on monkey-patching on functionality.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gosu_image, path, tileable) ⇒ Image

Returns a new instance of Image.



9
10
11
12
13
# File 'lib/metro/image.rb', line 9

def initialize(gosu_image,path,tileable)
  super(gosu_image)
  @path = path
  @tileable = tileable
end

Instance Attribute Details

#pathObject (readonly)

The relative path of the image



16
17
18
# File 'lib/metro/image.rb', line 16

def path
  @path
end

#tileableObject (readonly)

The tileability of the image



19
20
21
# File 'lib/metro/image.rb', line 19

def tileable
  @tileable
end

Class Method Details

.create(options) ⇒ Object

Create an image given the window, path, and tileability.

Examples:

Creating an Image


Metro::Image.create window: model.window,
  path: "asset_path", tileable: tileable


53
54
55
56
57
# File 'lib/metro/image.rb', line 53

def self.create(options)
  window, asset_path, tileable = create_params(options)
  gosu_image = Gosu::Image.new(window,asset_path.filepath,tileable)
  new gosu_image, asset_path.path, tileable
end

.crop(window, image, bounds) ⇒ Object



59
60
61
62
63
64
# File 'lib/metro/image.rb', line 59

def self.crop(window,image,bounds)
  cropped_image = TexPlay.create_image(window,bounds.width,bounds.height)
  cropped_image.refresh_cache
  cropped_image.splice image, 0, 0, crop: [ bounds.left, bounds.top, bounds.right, bounds.bottom ]
  cropped_image
end

.find_or_create(options) ⇒ Object

Finds an existing image or creates a new image given the window, path, and tileablilty.

Examples:

Finding or creating an Image


Metro::Image.find_or_create window: model.window,
  path: "asset_path", tileable: tileable


34
35
36
37
38
39
40
41
42
43
# File 'lib/metro/image.rb', line 34

def self.find_or_create(options)
  begin
    File.open(File.join("assets", options[:path]), "r")
  rescue Exception
    puts $! # <- make this prettier
    options[:path] = "missing.png" # <- make this file installed by default into the assets folder of the game
  end
  path = AssetPath.with(options[:path])
  images[path.to_s] or (images[path.to_s] = create(options))
end

Instance Method Details

#dimensionsObject



21
22
23
# File 'lib/metro/image.rb', line 21

def dimensions
  Metro::Units::Dimensions.of width, height
end