Class: Glimmer::SWT::Custom::Animation

Inherits:
Object
  • Object
show all
Includes:
Properties
Defined in:
lib/glimmer/swt/custom/animation.rb

Overview

Represents an animation declaratively

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Properties

attribute_getter, #attribute_getter, attribute_setter, #attribute_setter, normalized_attribute, #normalized_attribute, ruby_attribute_getter, #ruby_attribute_setter, ruby_attribute_setter

Constructor Details

#initialize(parent) ⇒ Animation

TODO consider supporting an async: false option



85
86
87
88
89
90
91
92
93
# File 'lib/glimmer/swt/custom/animation.rb', line 85

def initialize(parent)
  @parent = parent
  @parent.requires_shape_disposal = true
  @started = true
  @frame_index = 0
  @cycle_count_index = 0
  @start_number = 0 # denotes the number of starts (increments on every start)
  self.class.swt_display # ensures initializing variable to set from GUI thread
end

Instance Attribute Details

#cycleObject

Returns the value of attribute cycle.



79
80
81
# File 'lib/glimmer/swt/custom/animation.rb', line 79

def cycle
  @cycle
end

#cycle_countObject

Returns the value of attribute cycle_count.



81
82
83
# File 'lib/glimmer/swt/custom/animation.rb', line 81

def cycle_count
  @cycle_count
end

#duration_limitObject

Returns the value of attribute duration_limit.



81
82
83
# File 'lib/glimmer/swt/custom/animation.rb', line 81

def duration_limit
  @duration_limit
end

#everyObject

Returns the value of attribute every.



81
82
83
# File 'lib/glimmer/swt/custom/animation.rb', line 81

def every
  @every
end

#frame_blockObject

Returns the value of attribute frame_block.



81
82
83
# File 'lib/glimmer/swt/custom/animation.rb', line 81

def frame_block
  @frame_block
end

#frame_countObject

Returns the value of attribute frame_count.



81
82
83
# File 'lib/glimmer/swt/custom/animation.rb', line 81

def frame_count
  @frame_count
end

#frame_indexObject (readonly) Also known as: current_frame_index

Returns the value of attribute frame_index.



79
80
81
# File 'lib/glimmer/swt/custom/animation.rb', line 79

def frame_index
  @frame_index
end

#optionsObject (readonly)

Returns the value of attribute options.



79
80
81
# File 'lib/glimmer/swt/custom/animation.rb', line 79

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



79
80
81
# File 'lib/glimmer/swt/custom/animation.rb', line 79

def parent
  @parent
end

#startedObject Also known as: started?

Returns the value of attribute started.



81
82
83
# File 'lib/glimmer/swt/custom/animation.rb', line 81

def started
  @started
end

Class Method Details

.frame_animation_queue(animation) ⇒ Object



67
68
69
# File 'lib/glimmer/swt/custom/animation.rb', line 67

def frame_animation_queue(animation)
  frame_animation_queues[animation] ||= []
end

.frame_animation_queuesObject



60
61
62
63
64
65
# File 'lib/glimmer/swt/custom/animation.rb', line 60

def frame_animation_queues
  unless defined? @@frame_animation_queues
    @@frame_animation_queues = {}
  end
  @@frame_animation_queues
end

.next_animationObject



39
40
41
42
43
44
45
46
# File 'lib/glimmer/swt/custom/animation.rb', line 39

def next_animation
  animation = nil
  while frame_animation_queues.values.reduce(:+)&.any? && (animation.nil? || frame_animation_queue(animation).last.nil?)
    animation = frame_animation_queues.keys[next_animation_index]
    frame_animation_queues.delete(animation) if frame_animation_queues.values.reduce(:+)&.any? && !animation.nil? && frame_animation_queue(animation).empty?
  end
  animation
end

.next_animation_indexObject



48
49
50
# File 'lib/glimmer/swt/custom/animation.rb', line 48

def next_animation_index
  next_schedule_index % frame_animation_queues.keys.size
end

.next_schedule_indexObject



52
53
54
55
56
57
58
# File 'lib/glimmer/swt/custom/animation.rb', line 52

def next_schedule_index
  unless defined? @@next_schedule_index
    @@next_schedule_index = 0
  else
    @@next_schedule_index += 1
  end
end

.schedule_frame_animation(animation, &frame_animation_block) ⇒ Object



32
33
34
35
36
37
# File 'lib/glimmer/swt/custom/animation.rb', line 32

def schedule_frame_animation(animation, &frame_animation_block)
  frame_animation_queue(animation).prepend(frame_animation_block)
  swt_display.async_exec do
    frame_animation_queue(next_animation)&.pop&.call
  end
end

.swt_displayObject



71
72
73
74
75
76
# File 'lib/glimmer/swt/custom/animation.rb', line 71

def swt_display
  unless defined? @@swt_display
    @@swt_display = DisplayProxy.instance.swt_display
  end
  @@swt_display
end

Instance Method Details

#cycle_enabled?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/glimmer/swt/custom/animation.rb', line 179

def cycle_enabled?
  @cycle.is_a?(Array)
end

#cycle_limited?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/glimmer/swt/custom/animation.rb', line 183

def cycle_limited?
  cycle_enabled? && @cycle_count.is_a?(Integer)
end

#duration_limited?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/glimmer/swt/custom/animation.rb', line 187

def duration_limited?
  @duration_limit.is_a?(Integer)
end

#finite?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/glimmer/swt/custom/animation.rb', line 146

def finite?
  frame_count_limited? || cycle_limited? || duration_limited?
end

#frame_count_limited?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/glimmer/swt/custom/animation.rb', line 191

def frame_count_limited?
  @frame_count.is_a?(Integer)
end

#get_attribute(attribute_name) ⇒ Object



163
164
165
# File 'lib/glimmer/swt/custom/animation.rb', line 163

def get_attribute(attribute_name)
  send(ruby_attribute_getter(attribute_name))
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/glimmer/swt/custom/animation.rb', line 155

def has_attribute?(attribute_name, *args)
  respond_to?(ruby_attribute_setter(attribute_name)) && respond_to?(ruby_attribute_getter(attribute_name))
end

#infinite?Boolean Also known as: indefinite?

Returns:

  • (Boolean)


150
151
152
# File 'lib/glimmer/swt/custom/animation.rb', line 150

def infinite?
  !finite?
end

#post_add_contentObject



95
96
97
98
99
100
# File 'lib/glimmer/swt/custom/animation.rb', line 95

def post_add_content
  if @dispose_listener_registration.nil?
    @dispose_listener_registration = @parent.on_widget_disposed { stop }
    start if started?
  end
end

#restartObject

Restarts an animation (whether indefinite or not and whether stopped or not)



133
134
135
136
137
138
139
140
# File 'lib/glimmer/swt/custom/animation.rb', line 133

def restart
  @original_start_time = @start_time = nil
  @duration = nil
  @frame_index = 0
  @cycle_count_index = 0
  stop
  start
end

#set_attribute(attribute_name, *args) ⇒ Object



159
160
161
# File 'lib/glimmer/swt/custom/animation.rb', line 159

def set_attribute(attribute_name, *args)
  send(ruby_attribute_setter(attribute_name), *args)
end

#startObject

Starts an animation that is indefinite or has never been started before (i.e. having ‘started: false` option). Otherwise, resumes a stopped animation that has not been completed.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/glimmer/swt/custom/animation.rb', line 104

def start
  return if @start_number > 0 && started?
  @start_number += 1
  @started = true
  @start_time = Time.now
  @original_start_time = @start_time if @duration.nil?
  # TODO track when finished in a variable for finite animations (whether by frame count, cycle count, or duration limit)
  Thread.new do
    start_number = @start_number
    if cycle_count.is_a?(Integer) && cycle.is_a?(Array)
      (cycle_count * cycle.length).times do
        break unless draw_frame(start_number)
      end
    else
      loop do
        # this code has to be duplicated to break from a loop (break keyword only works when literally in a loop block)
        break unless draw_frame(start_number)
      end
    end
  end
end

#stopObject



126
127
128
129
130
# File 'lib/glimmer/swt/custom/animation.rb', line 126

def stop
  return if stopped?
  @started = false
  @duration = (Time.now - @start_time) + @duration.to_f if duration_limited? && !@start_time.nil?
end

#stopped?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/glimmer/swt/custom/animation.rb', line 142

def stopped?
  !started?
end

#surpassed_duration_limit?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/glimmer/swt/custom/animation.rb', line 195

def surpassed_duration_limit?
  duration_limited? && ((Time.now - @start_time) > (@duration_limit - @duration.to_f))
end

#within_duration_limit?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/glimmer/swt/custom/animation.rb', line 199

def within_duration_limit?
  !surpassed_duration_limit?
end