Class: Ray::Animation::ColorVariation

Inherits:
Ray::Animation show all
Defined in:
lib/ray/animation/color_variation.rb

Overview

This animates the transition from a color to another one. Registered as #color_variation.

Instance Attribute Summary collapse

Attributes inherited from Ray::Animation

#duration, #end_time, #target

Instance Method Summary collapse

Methods inherited from Ray::Animation

#+, #-, #bounce!, #end_animation, #initialize, #loop!, #pause, #paused?, #progression, register_for, #resume, #running?, #start, #update

Methods included from Helper

#create_event_runner, #disable_event_group, effect_generator, #enable_event_group, #event_runner, #event_runner=, font, holding?, image, image_target, mouse_pos, music, #remove_event_group, #rotation, #scale_variation, sound, sound_buffer, sprite, text, #translation

Methods included from Matchers

key, key_mod, where

Methods included from DSL::EventListener

#add_hook, #current_event_group, #current_event_group=, #event_group, #listener_runner, #listener_runner=, #on

Methods included from DSL::EventRaiser

#raise_event, #raiser_runner, #raiser_runner=

Constructor Details

This class inherits a constructor from Ray::Animation

Instance Attribute Details

#attributeSymbol (readonly)

Returns Attribut modified by the animation.

Returns:

  • (Symbol)

    Attribut modified by the animation.



88
89
90
# File 'lib/ray/animation/color_variation.rb', line 88

def attribute
  @attribute
end

#current_valueRay::Color

Returns Current color.

Returns:



79
80
81
# File 'lib/ray/animation/color_variation.rb', line 79

def current_value
  @current_value
end

#initial_valueRay::Color (readonly)

Returns Initial color.

Returns:



82
83
84
# File 'lib/ray/animation/color_variation.rb', line 82

def initial_value
  @initial_value
end

#variationArray<Integer> (readonly)

Returns Variation between initial and last value.

Returns:

  • (Array<Integer>)

    Variation between initial and last value.



85
86
87
# File 'lib/ray/animation/color_variation.rb', line 85

def variation
  @variation
end

Instance Method Details

#-@ColorVariation

Returns Opposite color variation.

Returns:



67
68
69
70
71
72
73
74
75
76
# File 'lib/ray/animation/color_variation.rb', line 67

def -@
  if @initial_value
    color_variation(:from => last_value, :to => @initial_value,
                    :attribute => @attribute, :duration => duration)
  else
    rev_variation = @variation.map { |o| -o }
    color_variation(:of => rev_variation, :attribute => @attribute,
                    :duration => duration)
  end
end

#last_valueRay::Color

Returns Last color.

Returns:



91
92
93
94
95
96
97
98
99
# File 'lib/ray/animation/color_variation.rb', line 91

def last_value
  if @initial_value
    initial_array = @initial_value.to_a

    Ray::Color.new(*initial_array.each_with_index.map { |o, i|
                     o + @variation[i]
                   })
  end
end

#pause_animationObject



53
54
55
# File 'lib/ray/animation/color_variation.rb', line 53

def pause_animation
  @animations.each { |anim| anim.pause }
end

#resume_animationObject



57
58
59
# File 'lib/ray/animation/color_variation.rb', line 57

def resume_animation
  @animations.each { |anim| anim.resume }
end

#setup(opts) ⇒ Object

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :of (Array)
    r, g, b, a

    Variation for the color.

    Required unless :from and :to are set.

  • :from (Ray::Color)

    Initial value for the color.

  • :to (Ray::Color)

    Last value of the color.

  • :attribute (Symbol) — default: :color

    Attribute of the target to change.

  • :duration (Float)

    Duration in seconds.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ray/animation/color_variation.rb', line 14

def setup(opts)
  if opts[:of]
    @current_value = @initial_value = nil
    @variation = opts[:of].to_a
  else
    @initial_value = opts[:from].to_color

    value_array = @initial_value.to_a

    @variation = opts[:to].to_a.each_with_index.map do |o, i|
      o - value_array[i]
    end
  end

  @attribute = opts[:attribute] || :color

  self.duration = opts[:duration]
end

#setup_targetObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ray/animation/color_variation.rb', line 33

def setup_target
  unless @initial_value
    @current_value = target.send(@attribute)
  else
    @current_value = @initial_value.dup
  end

  @animations = []
  @animations << float_variation(:of => @variation[0], :attribute => :r,
                                 :duration => duration)
  @animations << float_variation(:of => @variation[1], :attribute => :g,
                                 :duration => duration)
  @animations << float_variation(:of => @variation[2], :attribute => :b,
                                 :duration => duration)
  @animations << float_variation(:of => @variation[3], :attribute => :a,
                                 :duration => duration)

  @animations.each { |anim| anim.start(@current_value) }
end

#update_targetObject



61
62
63
64
# File 'lib/ray/animation/color_variation.rb', line 61

def update_target
  @animations.each { |anim| anim.update }
  target.send("#{@attribute}=", @current_value)
end