Class: TEF::Animation::Animatable

Inherits:
Object
  • Object
show all
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

Box, Eye, StringDisplay

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAnimatable

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_delayNumeric? (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.

Returns:

  • (Numeric, nil)

    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_timeTime?

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.

Returns:

  • (Time, nil)

    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_idString?

Returns Module ID of this object as in SxxMxx, or nil.

Returns:

  • (String, nil)

    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.

Parameters:

  • name (Symbol)

    Name of the attribute, as symbol. Will create getter and setter functions.

  • id (Numeric)

    Address of the animatable attribute. Must match the address defined in the animation C++ code!



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.

Parameters:

  • name (Symbol)

    Name of the color, as symbol. Will create getter and setter functions.

  • id (Numeric)

    Address of the animatable color. Must match the address defined in the animation C++ code!



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.

Parameters:

  • name (Symbol)

    Name of the coordinate, as symbol. Will create getter and setter functions.

  • id (Numeric)

    Starting address of the coordinates. Expects the coordinate parameters to be sequential!



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_listArray<{Symbol, Integer>] List of registered animatable attributes

Returns Array<{Symbol, Integer>] List of registered animatable attributes.

Returns:

  • (Array<{Symbol, Integer>] List of registered animatable attributes)

    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_listArray<Symbol, Integer>

Returns List of registered animatable colours.

Returns:

  • (Array<Symbol, Integer>)

    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_listArray<Symbol, Integer>

Returns List of registered animatable coordinates.

Returns:

  • (Array<Symbol, Integer>)

    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

Examples:

my_box.configure up: 10, down: 5, left: { dampen: 0.4, add: 2 }

Raises:

  • (ArgumentError)

See Also:



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_stringObject

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_stringObject

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.

See Also:



207
208
209
# File 'lib/tef/Animation/Animatable.rb', line 207

def die!
	self.death_time = Time.at(0)
end

#die_in(t) ⇒ Object

Note:

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.

Parameters:

  • t (Numeric, nil)

    The time, in seconds until this object will be killed.

Raises:

  • (ArgumentError)


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_stringsArray<Hash>

Note:

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.

Returns:

  • (Array<Hash>)

    An array containing Hashes outlining each value’s SET string.



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_stringsObject

See Also:



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_stringsObject



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

Returns:

  • (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