Class: Metro::Model::ImageProperty

Inherits:
Property
  • Object
show all
Defined in:
lib/metro/models/properties/image_property.rb

Overview

A image property maintains a Gosu::Image.

An image is stored in the properties as a string to the specified image path and is converted into a Gosu::Image when it is retrieved within the system. When retrieving an image the Image Property will attempt to use a image at that path that already meets that criteria if it has been loaded.

Examples:

Defining a image property


class Player < Metro::Model
  property :image

  def draw
    image.draw x, y, z_order, x_factor, y_factor, color, :add)
  end

end

Defining an image property providing a path


class Player < Metro::Model
  property :image, path: "player.png"
end

Using an image property with a different property name


class Player < Metro::Model
  property :disconnected_image, type: :image, path: "disconnected_player.png"

  def draw
    disconnected_image.draw x, y, z_order, x_factor, y_factor, color, :add)
  end
end

Constant Summary

Constants included from Units

Units::Bounds

Instance Attribute Summary

Attributes inherited from Property

#block, #model, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Property

define_property, defined_properties, get, #get, get_or_set, gets, hash_with_default_to_nil, inherited, #initialize, properties, properties_hash, property, #set, set, sets

Constructor Details

This class inherits a constructor from Metro::Model::Property

Class Method Details

.image_for(options) ⇒ Object

Return an image for the specified path. On first request it will be loaded from the file-system. On subsequent requests it will be pulled from the cache.

Parameters:

  • options (Hash)

    the relative ‘path` to the image and the window for which it will be displayed.



89
90
91
# File 'lib/metro/models/properties/image_property.rb', line 89

def self.image_for(options)
  Metro::Image.find_or_create(options)
end

Instance Method Details

#default_imageObject

Returns the default image based on the default image path specified.

Returns:

  • the default image based on the default image path specified.



70
71
72
# File 'lib/metro/models/properties/image_property.rb', line 70

def default_image
  self.class.image_for path: default_image_path, window: model.window
end

#default_image_pathObject

the default “missing.png”

Returns:

  • the path provided as the default or if one has not been specified



78
79
80
# File 'lib/metro/models/properties/image_property.rb', line 78

def default_image_path
  options[:path] or "missing.png"
end