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

Inherits:
Object
  • Object
show all
Includes:
DataBinding::ObservableModel, 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



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

def initialize(parent)
  @parent = parent
  @parent.requires_shape_disposal = true
  self.started = true
  self.frame_index = 0
  self.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.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def cycle
  @cycle
end

#cycle_countObject

Returns the value of attribute cycle_count.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def cycle_count
  @cycle_count
end

#cycle_count_indexObject

Returns the value of attribute cycle_count_index.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def cycle_count_index
  @cycle_count_index
end

#durationObject

Returns the value of attribute duration.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def duration
  @duration
end

#duration_limitObject

Returns the value of attribute duration_limit.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def duration_limit
  @duration_limit
end

#everyObject

Returns the value of attribute every.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def every
  @every
end

#finishedObject Also known as: finished?

Returns the value of attribute finished.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def finished
  @finished
end

#fpsObject Also known as: frame_rate

Returns the value of attribute fps.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def fps
  @fps
end

#frame_blockObject

Returns the value of attribute frame_block.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def frame_block
  @frame_block
end

#frame_countObject

Returns the value of attribute frame_count.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def frame_count
  @frame_count
end

#frame_indexObject Also known as: current_frame_index

Returns the value of attribute frame_index.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def frame_index
  @frame_index
end

#optionsObject (readonly)

Returns the value of attribute options.



83
84
85
# File 'lib/glimmer/swt/custom/animation.rb', line 83

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



83
84
85
# File 'lib/glimmer/swt/custom/animation.rb', line 83

def parent
  @parent
end

#startedObject Also known as: started?

Returns the value of attribute started.



84
85
86
# File 'lib/glimmer/swt/custom/animation.rb', line 84

def started
  @started
end

Class Method Details

.frame_animation_queue(animation) ⇒ Object



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

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

.frame_animation_queuesObject



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

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

.next_animationObject



43
44
45
46
47
48
49
50
# File 'lib/glimmer/swt/custom/animation.rb', line 43

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



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

def next_animation_index
  next_schedule_index % frame_animation_queues.keys.size
end

.next_schedule_indexObject



56
57
58
59
60
61
62
# File 'lib/glimmer/swt/custom/animation.rb', line 56

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



36
37
38
39
40
41
# File 'lib/glimmer/swt/custom/animation.rb', line 36

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



75
76
77
78
79
80
# File 'lib/glimmer/swt/custom/animation.rb', line 75

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

Instance Method Details

#content(&block) ⇒ Object



109
110
111
112
113
# File 'lib/glimmer/swt/custom/animation.rb', line 109

def content(&block)
  Glimmer::SWT::DisplayProxy.instance.auto_exec do
    Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::SWT::AnimationExpression.new, 'animation', &block)
  end
end

#cycle_enabled?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/glimmer/swt/custom/animation.rb', line 212

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

#cycle_limited?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/glimmer/swt/custom/animation.rb', line 216

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

#duration_limited?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/glimmer/swt/custom/animation.rb', line 220

def duration_limited?
  @duration_limit.is_a?(Numeric) && @duration_limit > 0
end

#finite?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/glimmer/swt/custom/animation.rb', line 161

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

#frame_count_limited?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/glimmer/swt/custom/animation.rb', line 224

def frame_count_limited?
  @frame_count.is_a?(Integer) && @frame_count > 0
end

#get_attribute(attribute_name) ⇒ Object



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

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

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/glimmer/swt/custom/animation.rb', line 170

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)


165
166
167
# File 'lib/glimmer/swt/custom/animation.rb', line 165

def infinite?
  !finite?
end

#post_add_contentObject



102
103
104
105
106
107
# File 'lib/glimmer/swt/custom/animation.rb', line 102

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)



148
149
150
151
152
153
154
155
# File 'lib/glimmer/swt/custom/animation.rb', line 148

def restart
  @original_start_time = @start_time = Time.now
  self.duration = 0
  self.frame_index = 0
  self.cycle_count_index = 0
  stop
  start
end

#set_attribute(attribute_name, *args) ⇒ Object



174
175
176
# File 'lib/glimmer/swt/custom/animation.rb', line 174

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.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/glimmer/swt/custom/animation.rb', line 117

def start
  return if @start_number > 0 && started?
  @start_number += 1
  @start_time = Time.now
  @duration = 0
  @original_start_time = @start_time if @duration.nil?
  self.finished = false if finished?
  self.started = true
  # 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



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

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

#stopped?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/glimmer/swt/custom/animation.rb', line 157

def stopped?
  !started?
end

#surpassed_duration_limit?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/glimmer/swt/custom/animation.rb', line 228

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

#within_duration_limit?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/glimmer/swt/custom/animation.rb', line 232

def within_duration_limit?
  !surpassed_duration_limit?
end