Class: TEF::Animation::Animatable
- Inherits:
-
Object
- Object
- TEF::Animation::Animatable
- Defined in:
- lib/tef/Animation/Animatable.rb
Overview
Animatable base class.
This class implements all necessary functions to write a custom animatable object with ease. It provides a DSL to easily register new animatable colours, values and coordinates, and handles updating and configuring them.
By inheriting from this base class, the user must only define the animatable properties of their object by using:
The object must also be passed to the Animation handler, by calling: handler = your_instance;
Direct Known Subclasses
Instance Attribute Summary collapse
-
#death_delay ⇒ Numeric?
readonly
If set, returns the time (in s) that this object will live for.
-
#death_time ⇒ Time?
If set, returns the time this object will auto-delete.
-
#module_id ⇒ String?
Module ID of this object as in SxxMxx, or nil.
Class Method Summary collapse
-
.animatable_attr(name, id) ⇒ Object
Defines a new animatable attribute.
-
.animatable_color(name, id) ⇒ Object
Defines a new animatable color.
-
.animatable_coordinate(name, start) ⇒ Object
Defines a new animatable coordinate.
-
.get_attr_list ⇒ Array<{Symbol, Integer>] List of registered animatable attributes
Array<{Symbol, Integer>] List of registered animatable attributes.
-
.get_color_list ⇒ Array<Symbol, Integer>
List of registered animatable colours.
-
.get_coordinate_list ⇒ Array<Symbol, Integer>
List of registered animatable coordinates.
Instance Method Summary collapse
-
#configure(h = nil, **opts) ⇒ Object
Quickly configure this object.
-
#creation_string ⇒ Object
Return a default creation string.
-
#death_time_string ⇒ Object
Returns a String to be sent via FurComs to configure the object to die in a certain number of seconds.
-
#die! ⇒ Object
Instantly deletes this object from the animation slaves.
-
#die_in(t) ⇒ Object
Make this object die in a given number of seconds.
-
#get_set_strings ⇒ Array<Hash>
Returns an array of ‘SET’ Hashes, to be sent over the FurComs bus, ‘SET’ topic.
- #get_setc_strings ⇒ Object
- #get_setss_strings ⇒ Object
-
#initialize ⇒ Animatable
constructor
Initialize a generic animatable object.
- #is_dead? ⇒ Boolean
- #send_string(str) ⇒ Object
Constructor Details
#initialize ⇒ Animatable
Initialize a generic animatable object.
This will initialize the necessary internal hashes that contain the animatable attributes, colors and coordinates.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/tef/Animation/Animatable.rb', line 147 def initialize() @animatable_attributes = {} @animatable_colors = {} @animatable_coordinates = {} @animatable_pending_strings = []; self.class.get_attr_list.each do |key, val| @animatable_attributes[key] = Value.new(val) end self.class.get_color_list.each do |key, val| @animatable_colors[key] = Color.new(val) end self.class.get_coordinate_list.each do |key, offset| @animatable_coordinates[key] = Coordinate.new(offset) end end |
Instance Attribute Details
#death_delay ⇒ Numeric? (readonly)
Returns If set, returns the time (in s) that this object will live for. If the object is currently a live animation, setting this will make the object die in the given number of seconds. If it is not currently animated, it will make the object die after the given number of seconds, starting from when it was registered with the handler.
48 49 50 |
# File 'lib/tef/Animation/Animatable.rb', line 48 def death_delay @death_delay end |
#death_time ⇒ Time?
Returns If set, returns the time this object will auto-delete. This will not delete the Ruby object, but it will send a delete request to the animation slaves.
40 41 42 |
# File 'lib/tef/Animation/Animatable.rb', line 40 def death_time @death_time end |
#module_id ⇒ String?
Returns Module ID of this object as in SxxMxx, or nil.
35 36 37 |
# File 'lib/tef/Animation/Animatable.rb', line 35 def module_id @module_id end |
Class Method Details
.animatable_attr(name, id) ⇒ Object
Defines a new animatable attribute.
The defined attribute will become accessible via a getter and convenience setter function, giving the user access to the created Value instance.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/tef/Animation/Animatable.rb', line 80 def self.animatable_attr(name, id) get_attr_list()[name] = id define_method(name.to_s) do @animatable_attributes[name] end define_method("#{name}=") do |arg| if arg.is_a? Numeric @animatable_attributes[name].add = arg else @animatable_attributes[name].from = arg end end end |
.animatable_color(name, id) ⇒ Object
Defines a new animatable color
The defined color will become accessible via a getter and convenience setter function, giving the user access to the created Color instance.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tef/Animation/Animatable.rb', line 109 def self.animatable_color(name, id) get_color_list()[name] = id define_method(name.to_s) do @animatable_colors[name] end define_method("#{name}=") do |arg| @animatable_colors[name].target = arg end end |
.animatable_coordinate(name, start) ⇒ Object
Defines a new animatable coordinate.
The defined coordinate will become accessible via a getter and convenience setter function, giving the user access to the created Value instance.
135 136 137 138 139 140 141 |
# File 'lib/tef/Animation/Animatable.rb', line 135 def self.animatable_coordinate(name, start) get_coordinate_list()[name] = start define_method(name.to_s) do @animatable_coordinates[name] end end |
.get_attr_list ⇒ Array<{Symbol, Integer>] List of registered animatable attributes
Returns Array<{Symbol, Integer>] List of registered animatable attributes.
52 53 54 |
# File 'lib/tef/Animation/Animatable.rb', line 52 def self.get_attr_list @class_attribute_list ||= {} end |
.get_color_list ⇒ Array<Symbol, Integer>
Returns List of registered animatable colours.
57 58 59 |
# File 'lib/tef/Animation/Animatable.rb', line 57 def self.get_color_list @class_color_list ||= {} end |
.get_coordinate_list ⇒ Array<Symbol, Integer>
Returns List of registered animatable coordinates.
62 63 64 |
# File 'lib/tef/Animation/Animatable.rb', line 62 def self.get_coordinate_list @class_coordinate_list ||= {} end |
Instance Method Details
#configure(h = nil, **opts) ⇒ Object
Quickly configure this object.
This is a convenience function to very quickly and easily (re)configure this animatable object. It will take a hash or named options, and will pass the values of the hash to the matching Value, Color or Coordinate
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/tef/Animation/Animatable.rb', line 229 def configure(h = nil, **opts) h ||= opts; raise ArgumentError, 'Config must be a hash!' unless h.is_a? Hash h.each do |key, data| if(key == :die_after) die_in(data.to_f); next; end value = @animatable_attributes[key] || @animatable_colors[key] || @animatable_coordinates[key] raise ArgumentError, "Parameter #{key} does not exist!" unless value value.configure(data); end end |
#creation_string ⇒ Object
Return a default creation string.
This function MUST be overwritten by the user to provide a proper creation string! It will be sent over FurComs via the ‘NEW’ topic and must contain all necessary information for the slaves to construct the matching object.
256 257 258 |
# File 'lib/tef/Animation/Animatable.rb', line 256 def creation_string '' end |
#death_time_string ⇒ Object
Returns a String to be sent via FurComs to configure the object to die in a certain number of seconds.
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/tef/Animation/Animatable.rb', line 295 def death_time_string return nil unless @death_time_changed @death_time_changed = false return "#{@module_id} N;" if @death_time.nil? remaining_time = (@death_time - Time.now()).round(2) "#{@module_id} #{remaining_time};" end |
#die! ⇒ Object
Instantly deletes this object from the animation slaves.
207 208 209 |
# File 'lib/tef/Animation/Animatable.rb', line 207 def die! self.death_time = Time.at(0) end |
#die_in(t) ⇒ Object
This will not delete the Ruby object, but it will be unregistered from the Handler. It can safely be re-registered to re-create a new object.
Make this object die in a given number of seconds. After the timer set here expires, the object will automatically be deleted. As the slaves will do it automatically, it is good practice to give any temporary object a death time, if possible, to have them automatically cleaned up.
It is possible to remove a death time or extend it, by calling this function another time.
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tef/Animation/Animatable.rb', line 191 def die_in(t) if t.nil? self.death_time = nil @death_delay = nil return end raise ArgumentError, "Time must be num!" unless t.is_a? Numeric self.death_time = Time.now() + t @death_delay = t end |
#get_set_strings ⇒ Array<Hash>
Never call this unless you are the Handler! It will mark the changes as already transmitted, so manually calling this will cause data loss!
Returns an array of ‘SET’ Hashes, to be sent over the FurComs bus, ‘SET’ topic. They represent raw value configurations of the animatable values.
The Handler that called this function has the duty of packing them into complete commands, as the initial module ID can be left out for sequential value access, saving a few bytes each transfer.
324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/tef/Animation/Animatable.rb', line 324 def get_set_strings() return [] unless @module_id out_elements = [] all_animatable_attributes.each do |val| o_str = val.set_string next if o_str.nil? out_elements << { module: @module_id, value: val.ID, str: o_str }; end out_elements end |
#get_setc_strings ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/tef/Animation/Animatable.rb', line 342 def get_setc_strings() return [] unless @module_id out_elements = [] @animatable_colors.values.each do |val| o_str = val.set_string next if o_str.nil? out_elements << "#{@module_id}#{o_str}" end out_elements end |
#get_setss_strings ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/tef/Animation/Animatable.rb', line 357 def get_setss_strings() return [] unless @module_id out = @animatable_pending_strings.map do |str| "#{@module_id} #{str}" end @animatable_pending_strings.clear return out; end |
#is_dead? ⇒ Boolean
211 212 213 214 |
# File 'lib/tef/Animation/Animatable.rb', line 211 def is_dead? return false if @death_time.nil? return @death_time < Time.now(); end |
#send_string(str) ⇒ Object
260 261 262 |
# File 'lib/tef/Animation/Animatable.rb', line 260 def send_string(str) @animatable_pending_strings << str; end |