Class: Metro::Model::FontProperty

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

Overview

A font property maintains a Gosu::Font.

A font property also defines a ‘font_size` property and a `font_name` property which allows a more direct interface. Changing these values will update the font the next time that it is drawn.

A font is stored in the properties as a hash representation and is converted into a Gosu::Font when it is retrieved within the system. When retrieving a font the Font Property will attempt to use a font that already exists that meets that criteria.

The fonts are cached within the font property to help performance by reducing the unncessary creation of similar fonts.

Examples:

Defining a font property


class Scoreboard < Metro::Model
  property :font

  def draw
    font.draw text, x, y, z_order, x_factor, y_factor, color
  end

end

Defining a font property providing a default


class Hero < Metro::Model
  property :font, default: { name: 'Comic Sans', size: 80 }
end

Using the ‘font_size` and `font_name` properties


class Hero < Metro::Model
  property :font, default: { name: 'Comic Sans', size: 80 }
  property :color, default: "rgba(255,0,0,1.0)"

  def dignified
    self.font_size = 45
    self.font_name = 'Helvetica'
  end
end

Using a font property with a different property name


class Hero < Metro::Model
  property :alt_font, type: :font, default: { name: 'Helvetica', size: 80 }

  def draw
    puts "Font: #{alt_font_name}:#{alt_font_size}"
    alt_font.draw text, x, y, z_order, x_factor, y_factor, color
  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

.font_for(value) ⇒ Object

Return a font that matches the specified criteria. Usig the name, size, and window a font will be generated or retrieved from the cache.

Parameters:

  • value (Hash)

    the hash that contains the ‘name`, `size` and `window` that describe the font.



123
124
125
# File 'lib/metro/models/properties/font_property.rb', line 123

def self.font_for(value)
  Font.find_or_create(value)
end

Instance Method Details

#default_fontObject

Returns the default font to use when a value has not been provided.

Returns:

  • the default font to use when a value has not been provided.



97
98
99
100
101
# File 'lib/metro/models/properties/font_property.rb', line 97

def default_font
  self.class.font_for name: default_font_name,
    size: default_font_size,
    window: model.window
end

#default_font_nameObject

Use the specified default font name or fall back to Gosu’s default font name.



113
114
115
# File 'lib/metro/models/properties/font_property.rb', line 113

def default_font_name
  (options[:default] and options[:default][:name]) ? options[:default][:name] : Gosu::default_font_name
end

#default_font_sizeObject

Use the specified default font size or fall back to 40.



106
107
108
# File 'lib/metro/models/properties/font_property.rb', line 106

def default_font_size
  (options[:default] and options[:default][:size]) ? options[:default][:size] : 40
end