Module: GTK::Easing

Defined in:
dragon/easing.rb

Class Method Summary collapse

Class Method Details

.cube(x) ⇒ Object


82
83
84
# File 'dragon/easing.rb', line 82

def self.cube x
  x * x * x
end

.ease(start_tick, current_tick, duration, *definitions) ⇒ Object


8
9
10
11
12
13
14
15
# File 'dragon/easing.rb', line 8

def self.ease start_tick, current_tick, duration, *definitions
  ease_extended start_tick,
                current_tick,
                start_tick + duration,
                (initial_value *definitions),
                (final_value *definitions),
                *definitions
end

.ease_extended(start_tick, current_tick, end_tick, default_before, default_after, *definitions) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'dragon/easing.rb', line 17

def self.ease_extended start_tick, current_tick, end_tick, default_before, default_after, *definitions
  definitions.flatten!
  definitions = [:identity] if definitions.length == 0
  duration = end_tick - start_tick
  elapsed  = current_tick - start_tick
  y = elapsed.percentage_of(duration).cap_min_max(0, 1)

  definitions.map do |definition|
    y = Easing.exec_definition(definition, start_tick, duration, y)
  end

  y
end

.ease_spline(start_tick, current_tick, duration, spline) ⇒ Object


31
32
33
# File 'dragon/easing.rb', line 31

def self.ease_spline start_tick, current_tick, duration, spline
  ease_spline_extended start_tick, current_tick, start_tick + duration, spline
end

.ease_spline_extended(start_tick, current_tick, end_tick, spline) ⇒ Object


35
36
37
38
39
40
41
42
43
44
# File 'dragon/easing.rb', line 35

def self.ease_spline_extended start_tick, current_tick, end_tick, spline
  return spline[-1][-1] if current_tick >= end_tick
  duration = end_tick - start_tick
  t = (current_tick - start_tick).fdiv duration
  time_allocation_per_curve = 1.fdiv(spline.length)
  curve_index, curve_t = t.fdiv(time_allocation_per_curve).let do |spline_t|
    [spline_t.to_i, spline_t - spline_t.to_i]
  end
  Geometry.cubic_bezier curve_t, *spline[curve_index]
end

.exec_definition(definition, start_tick, duration, x) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'dragon/easing.rb', line 56

def self.exec_definition definition, start_tick, duration, x
  if definition.is_a? Symbol
    return Easing.send(definition, x).cap_min_max(0, 1)
  elsif definition.is_a? Proc
    return definition.call(x, start_tick, duration).cap_min_max(0, 1)
  end

  raise <<-S
* ERROR:
I don't know how to execute easing function with definition #{definition}.

S
end

.final_value(*definitions) ⇒ Object


51
52
53
54
# File 'dragon/easing.rb', line 51

def self.final_value *definitions
  definitions.flatten!
  return Easing.exec_definition (definitions.at(-1) || :identity), 0, 10, 1.0
end

.flip(x) ⇒ Object


74
75
76
# File 'dragon/easing.rb', line 74

def self.flip x
  1 - x
end

.identity(x) ⇒ Object


70
71
72
# File 'dragon/easing.rb', line 70

def self.identity x
  x
end

.initial_value(*definitions) ⇒ Object


46
47
48
49
# File 'dragon/easing.rb', line 46

def self.initial_value *definitions
  definitions.flatten!
  return Easing.exec_definition (definitions.at(-1) || :identity), 0, 10, 0
end

.quad(x) ⇒ Object


78
79
80
# File 'dragon/easing.rb', line 78

def self.quad x
  x * x
end

.quart(x) ⇒ Object


86
87
88
# File 'dragon/easing.rb', line 86

def self.quart x
  x * x * x * x * x
end

.quint(x) ⇒ Object


90
91
92
# File 'dragon/easing.rb', line 90

def self.quint x
  x * x * x * x * x * x
end