Class: TEF::Animation::Handler

Inherits:
Object
  • Object
show all
Includes:
XasLogger::Mix
Defined in:
lib/tef/Animation/Animation_Handler.rb

Overview

Animation object handler.

This class is the handler for one coherent animation system. Its main purpose is to (de)register animatable objects, distributing IDs and handing out packed update messages onto a FurComs bus.

Instance Method Summary collapse

Constructor Details

#initialize(furcoms_bus) ⇒ Handler

Note:

#update_tick MUST be called after performing any changes, be it adding a new Animatable or changing values of a known animatable. It is recommended to call this function only after all changes for a given tick have been performed, so that they can be sent over as a batch. Sequencing::Player#after_exec can be used to register a callback to call #update_tick.

Initialize a Handler.

This will initialize a handler and connect it to the passed FurComs bus. The user may immediately start registering objects after creating this class, though if the FurComs bus is not connected yet this may cause very early messages to be lost!

Parameters:

  • furcoms_bus (FurComs::Base)

    The FurComs Bus connecting instance. Must support send_message(topic, data), nothing else.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tef/Animation/Animation_Handler.rb', line 36

def initialize(furcoms_bus)
	@furcoms = furcoms_bus

	@animation_mutex = Mutex.new
	@active_animations = {}

	@pending_deletions = {}
	@pending_creations = {}

	init_x_log('Animation Handler')
end

Instance Method Details

#[](key) ⇒ Animatable

Returns the Animatable with matching key.

Returns:

  • (Animatable)

    Returns the Animatable with matching key.



86
87
88
# File 'lib/tef/Animation/Animation_Handler.rb', line 86

def [](key)
	@active_animations[clean_key key]
end

#[]=(key, new_obj) ⇒ Object

Register or replace a Animatable.

This lets the user register a new Animatable object with given key, or replace or delete a pre-existing animation object.

Parameters:

  • key (String, Hash)

    The key to write into.

  • Will (Animatable, nil)

    delete any pre-existing animation, then either replace it with the given Animatable, or, if nil was given, will delete the animation entry.



99
100
101
102
103
# File 'lib/tef/Animation/Animation_Handler.rb', line 99

def []=(key, new_obj)
	@animation_mutex.synchronize do
		internal_set_object(key, new_obj)
	end
end

#append_to_set(new_obj, set_no = 200) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/tef/Animation/Animation_Handler.rb', line 105

def append_to_set(new_obj, set_no = 200)
	@animation_mutex.synchronize do
		start_no = { S: set_no, M: 0 };

		until(@active_animations[clean_key start_no].nil? ||
				@active_animations[clean_key start_no].is_dead?) do
			start_no[:M] += 1
		end

		if(start_no[:M] >= 255)
			raise ArgumentError, 'No more space for new animations!'
		end

		internal_set_object(start_no, new_obj)
	end

	new_obj
end

#clean_key(key) ⇒ String

Create a String key representation.

This will take either a hash or a String, convert it to a standard String key representation, and then verify it for correctness.

Parameters:

  • key (String, Hash)

    the key to clean up.

Returns:

  • (String)

    Cleaned string



55
56
57
58
59
60
61
62
63
# File 'lib/tef/Animation/Animation_Handler.rb', line 55

def clean_key(key)
	key = 'S%<S>dM%<M>d' % key if key.is_a? Hash

	unless key =~ /^S[\d]{1,3}M[\d]{1,3}$/
		raise ArgumentError, 'Target must be a valid Animation Value'
	end

	key
end

#update_tickObject

Update tick.

Calling this function will send all updates and changes over the FurComs bus. It is ensured that they occour in the following order:

  • All modules that have actively been deleted in Ruby will be deleted.

  • Newly created and registered Animatables will be created on the animation slaves.

  • All Value changes of all Animatables will be sent.

  • All Color changes will be sent.



282
283
284
285
286
287
288
289
290
# File 'lib/tef/Animation/Animation_Handler.rb', line 282

def update_tick()
	update_creations

	update_deaths

	update_values
	update_colors
	update_strings
end