Class: Metro::Model
- Inherits:
-
Object
- Object
- Metro::Model
- Includes:
- HasEvents, KeyValueCoding, PropertyOwner, Units
- Defined in:
- lib/metro/models/model.rb,
lib/metro/models/properties/property.rb,
lib/metro/models/properties/font_property.rb,
lib/metro/models/properties/song_property.rb,
lib/metro/models/properties/text_property.rb,
lib/metro/models/properties/array_property.rb,
lib/metro/models/properties/color_property.rb,
lib/metro/models/properties/image_property.rb,
lib/metro/models/properties/scale_property.rb,
lib/metro/models/properties/sample_property.rb,
lib/metro/models/properties/boolean_property.rb,
lib/metro/models/properties/numeric_property.rb,
lib/metro/models/properties/position_property.rb,
lib/metro/models/properties/animation_property.rb,
lib/metro/models/properties/dimensions_property.rb,
lib/metro/models/properties/options_property/options.rb,
lib/metro/models/properties/options_property/no_option.rb,
lib/metro/models/properties/options_property/options_property.rb
Overview
The Model is a basic, generic representation of a game object that has a visual representation within the scene’s window.
Model is designed to be an abstract class, to be subclassed by other models.
Direct Known Subclasses
Hero, Audio::Song, UI::Generic, UI::GridDrawer, UI::Image, UI::Label, UI::Menu, UI::Rectangle
Defined Under Namespace
Classes: AnimationProperty, ArrayProperty, BooleanProperty, ColorProperty, DimensionsProperty, FontProperty, ImageProperty, NumericProperty, OptionsProperty, PositionProperty, Property, SampleProperty, ScaleProperty, SongProperty, TextProperty
Constant Summary
Constants included from Units
Instance Attribute Summary collapse
-
#scene ⇒ Object
The scene that this model is currently being displayed.
-
#window ⇒ Object
The window that this model that this window is currently being displayed.
Class Method Summary collapse
-
.hierarchy ⇒ Object
An array of all ancestor models by name.
-
.inherited(model) ⇒ Object
Captures all classes that subclass Model.
-
.metro_name ⇒ Object
A common name that can be used through the system as a common identifier.
-
.model(name) ⇒ Object
Convert the specified model name into the class of the model.
- .models_hash ⇒ Object
Instance Method Summary collapse
-
#_load(options = {}) ⇒ Object
Loads a hash of content into the model.
-
#_save ⇒ Object
Generate a hash export of all the fields that were previously stored within the model.
-
#after_initialize ⇒ Object
This is an entry point for customization.
-
#completed? ⇒ Boolean
This is called after an update.
-
#contains?(x, y) ⇒ Boolean
Belongs to positionable items only.
-
#create(model_name, options = {}) ⇒ Metro::Model
A helper method that allows the current model to generate another model.
-
#draw ⇒ Object
This is called after every #update and when the OS wants the window to repaint itself.
-
#initialize(options = {}) ⇒ Model
constructor
Create an instance of a model.
-
#model ⇒ String
The name of the model class.
-
#name ⇒ String
The name of model as it is used within the view or the scene.
-
#notification(event) ⇒ Object
Generate a custom notification event with the given name.
-
#offset(x, y) ⇒ Object
Belongs to positionable items only.
- #saveable? ⇒ Boolean
-
#show ⇒ Object
This is an entry point for customization.
-
#to_hash ⇒ Object
Generate a hash representation of the model.
-
#update ⇒ Object
This is called every update interval while the actor is in the scene.
Methods included from HasEvents
Methods included from KeyValueCoding
Methods included from PropertyOwner
Constructor Details
#initialize(options = {}) ⇒ Model
Overridding initialize method should be avoided, using the {#aftter_initialize)
Create an instance of a model.
method or done with care to ensure that functionality is preserved.
154 155 156 157 |
# File 'lib/metro/models/model.rb', line 154 def initialize( = {}) _load() after_initialize end |
Instance Attribute Details
#scene ⇒ Object
The scene that this model is currently being displayed.
The current value of scene is managed by the scene as this is set when the scene is created.
98 99 100 |
# File 'lib/metro/models/model.rb', line 98 def scene @scene end |
#window ⇒ Object
The window that this model that this window is currently being displayed.
The current value of window is managed by the scene as this is set when the Scene is added to the window. All the models gain access to the window.
89 90 91 |
# File 'lib/metro/models/model.rb', line 89 def window @window end |
Class Method Details
.hierarchy ⇒ Object
Returns an array of all ancestor models by name.
208 209 210 |
# File 'lib/metro/models/model.rb', line 208 def self.hierarchy ancestors.find_all {|a| a.respond_to? :metro_name }.map(&:metro_name) end |
.inherited(model) ⇒ Object
Captures all classes that subclass Model.
217 218 219 220 221 |
# File 'lib/metro/models/model.rb', line 217 def self.inherited(model) models_hash[model.to_s] = model.to_s models_hash[model.to_s.downcase] = model.to_s models_hash[model.to_s.underscore] = model.to_s end |
.metro_name ⇒ Object
Returns a common name that can be used through the system as a common identifier.
201 202 203 |
# File 'lib/metro/models/model.rb', line 201 def self.metro_name name.underscore end |
.model(name) ⇒ Object
Convert the specified model name into the class of the model.
227 228 229 |
# File 'lib/metro/models/model.rb', line 227 def self.model(name) models_hash[name] end |
.models_hash ⇒ Object
231 232 233 |
# File 'lib/metro/models/model.rb', line 231 def self.models_hash @models_hash ||= HashWithIndifferentAccess.new("Metro::UI::Generic") end |
Instance Method Details
#_load(options = {}) ⇒ Object
Loads a hash of content into the model. This process will convert the hash of content into setter and getter methods with appropriate ruby style names.
This is used internally when the model is created for the Scene. It is loaded with the contents of the view.
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/metro/models/model.rb', line 166 def _load( = {}) # Clean up and symbolize all the keys then merge that with the existing properties .keys.each do |key| property_name = key.to_s.underscore.to_sym if respond_to? "#{property_name}=" send("#{property_name}=",.delete(key)) else [property_name] = .delete(key) end end properties.merge! end |
#_save ⇒ Object
Generate a hash export of all the fields that were previously stored within the model.
This is used internally within the scene to transfer the data from one model to another model.
187 188 189 |
# File 'lib/metro/models/model.rb', line 187 def _save properties end |
#after_initialize ⇒ Object
This method should be implemented in the Model subclass.
This is an entry point for customization. As the model’s #initialize method performs may perform some initialization that may be necessary.
At this point the model has been created. However, the window and scene of the model will not have been defined and defined properties rely on the window or scene will return nil values. Other properties also will likely not be set.
30 |
# File 'lib/metro/models/model.rb', line 30 def after_initialize ; end |
#completed? ⇒ Boolean
This method should be implemented in the Model sublclass if you are interested in having the model be removed from the scene.
This is called after an update. A model normally is not removed after an update, however if the model responds true to #completed? then it will be removed.
57 |
# File 'lib/metro/models/model.rb', line 57 def completed? ; false ; end |
#contains?(x, y) ⇒ Boolean
Belongs to positionable items only
138 139 140 |
# File 'lib/metro/models/model.rb', line 138 def contains?(x,y) false end |
#create(model_name, options = {}) ⇒ Metro::Model
A helper method that allows the current model to generate another model. This is useful as it allows for the current model to pass window and scene state to the created model.
124 125 126 127 128 129 130 131 |
# File 'lib/metro/models/model.rb', line 124 def create(model_name,={}) # @TODO: this is another path that parallels the ModelFactory model_class = Metro::Model.model(model_name).constantize mc = model_class.new mc.scene = scene mc.window = window mc end |
#draw ⇒ Object
This method should be implemented in the Model subclass.
This is called after every #update and when the OS wants the window to repaint itself.
65 |
# File 'lib/metro/models/model.rb', line 65 def draw ; end |
#model ⇒ String
Returns the name of the model class.
70 |
# File 'lib/metro/models/model.rb', line 70 property :model, type: :text |
#name ⇒ String
Returns the name of model as it is used within the view or the scene. This is the common name, the key within the view file, or the name symbol name specified in the scene.
77 |
# File 'lib/metro/models/model.rb', line 77 property :name, type: :text |
#notification(event) ⇒ Object
Generate a custom notification event with the given name.
107 108 109 |
# File 'lib/metro/models/model.rb', line 107 def notification(event) scene.notification(event.to_sym,self) end |
#offset(x, y) ⇒ Object
Belongs to positionable items only
143 144 145 146 |
# File 'lib/metro/models/model.rb', line 143 def offset(x,y) self.x += x self.y += y end |
#saveable? ⇒ Boolean
133 134 135 |
# File 'lib/metro/models/model.rb', line 133 def saveable? true end |
#show ⇒ Object
This method may be implemented in the Model subclass.
This is an entry point for customization. After the model’s properties have been set and the model has been assigned to the window and scene this method is called. Here is where customization of properties or final positioning can be performed.
40 |
# File 'lib/metro/models/model.rb', line 40 def show ; end |
#to_hash ⇒ Object
Generate a hash representation of the model.
194 195 196 |
# File 'lib/metro/models/model.rb', line 194 def to_hash { name => properties.except(:name) } end |
#update ⇒ Object
This method should be implemented in the Model subclass
This is called every update interval while the actor is in the scene
47 |
# File 'lib/metro/models/model.rb', line 47 def update ; end |