Class: CyberarmEngine::NotificationManager::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/notification_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edge:, mode:, notification:, slot:) ⇒ Driver

Returns a new instance of Driver.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cyberarm_engine/notification_manager.rb', line 101

def initialize(edge:, mode:, notification:, slot:)
  @edge = edge
  @mode = mode
  @notification = notification
  @slot = slot

  @x, @y = 0, 0
  @delta = Gosu.milliseconds
  @accumulator = 0.0

  @born_at = Gosu.milliseconds
  @duration_completed_at = Float::INFINITY
  @transition_completed_at = Float::INFINITY
end

Instance Attribute Details

#notificationObject (readonly)

Returns the value of attribute notification.



100
101
102
# File 'lib/cyberarm_engine/notification_manager.rb', line 100

def notification
  @notification
end

#slotObject (readonly)

Returns the value of attribute slot.



100
101
102
# File 'lib/cyberarm_engine/notification_manager.rb', line 100

def slot
  @slot
end

#xObject (readonly)

Returns the value of attribute x.



100
101
102
# File 'lib/cyberarm_engine/notification_manager.rb', line 100

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



100
101
102
# File 'lib/cyberarm_engine/notification_manager.rb', line 100

def y
  @y
end

Instance Method Details

#animation_ratioObject



214
215
216
217
218
219
220
221
222
223
# File 'lib/cyberarm_engine/notification_manager.rb', line 214

def animation_ratio
  x = (@accumulator / @notification.transition_duration)

  case @notification.transition_type
  when Notification::LINEAR_TRANSITION
    x.clamp(0.0, 1.0)
  when Notification::EASE_IN_OUT_TRANSITION # https://easings.net/#easeInOutQuint

    (x < 0.5 ? 16 * x * x * x * x * x : 1 - ((-2 * x + 2) ** 5) / 2).clamp(0.0, 1.0)
  end
end

#done?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/cyberarm_engine/notification_manager.rb', line 124

def done?
  Gosu.milliseconds - @duration_completed_at >= @notification.transition_duration
end

#drawObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cyberarm_engine/notification_manager.rb', line 128

def draw
  ratio = 0.0

  if not transition_in_complete?
    ratio = animation_ratio
  elsif transition_in_complete? and not duration_completed?
    ratio = 1.0
  elsif duration_completed?
    ratio = 1.0 - animation_ratio
  end

  case @mode
  when MODE_DEFAULT
    Gosu.clip_to(0, 0, Notification::WIDTH, Notification::HEIGHT * ratio) do
      @notification.draw
    end
  when MODE_CIRCLE
    half = Notification::WIDTH / 2

    Gosu.clip_to(half - (half * ratio), 0, Notification::WIDTH * ratio, Notification::HEIGHT) do
      @notification.draw
    end
  end
end

#duration_completed?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/cyberarm_engine/notification_manager.rb', line 120

def duration_completed?
  Gosu.milliseconds - @transition_completed_at >= @notification.time_to_live
end

#transition_in_complete?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/cyberarm_engine/notification_manager.rb', line 116

def transition_in_complete?
  Gosu.milliseconds - @born_at >= @notification.transition_duration
end

#updateObject



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cyberarm_engine/notification_manager.rb', line 153

def update
  case @mode
  when MODE_DEFAULT
    update_default
  when MODE_CIRCLE
    update_circle
  end

  @accumulator += Gosu.milliseconds - @delta
  @delta = Gosu.milliseconds
end

#update_circleObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/cyberarm_engine/notification_manager.rb', line 198

def update_circle
  case @edge
  when :top, :bottom
    @y = @edge == :top ? Notification::HEIGHT : -Notification::HEIGHT
  when :left, :right
    @x = @edge == :right ? -Notification::WIDTH : Notification::WIDTH
  end

  if transition_in_complete? and not duration_completed?
    @transition_completed_at = Gosu.milliseconds if @transition_completed_at == Float::INFINITY
    @accumulator = 0.0
  elsif duration_completed?
    @duration_completed_at = Gosu.milliseconds if @duration_completed_at == Float::INFINITY
  end
end

#update_defaultObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/cyberarm_engine/notification_manager.rb', line 166

def update_default
  case @edge
  when :left, :right
    if not transition_in_complete? # Slide In

      @x = @edge == :right ? -x_offset : x_offset
    elsif transition_in_complete? and not duration_completed?
      @x = @edge == :right ? -Notification::WIDTH : Notification::WIDTH if @x.abs != Notification::WIDTH
      @transition_completed_at = Gosu.milliseconds if @transition_completed_at == Float::INFINITY
      @accumulator = 0.0
    elsif duration_completed? # Slide Out

      @x = @edge == :right ? x_offset - Notification::WIDTH : Notification::WIDTH - x_offset
      @x = 0 if @edge == :left and @x <= 0
      @x = 0 if @edge == :right and @x >= 0
      @duration_completed_at = Gosu.milliseconds if @duration_completed_at == Float::INFINITY
    end

  when :top, :bottom
    if not transition_in_complete? # Slide In

      @y = @edge == :top ? y_offset : -y_offset
    elsif transition_in_complete? and not duration_completed?
      @y = @edge == :top ? Notification::HEIGHT : -Notification::HEIGHT if @x.abs != Notification::HEIGHT
      @transition_completed_at = Gosu.milliseconds if @transition_completed_at == Float::INFINITY
      @accumulator = 0.0
    elsif duration_completed? # Slide Out

      @y = @edge == :top ? Notification::HEIGHT - y_offset : y_offset - Notification::HEIGHT
      @y = 0 if @edge == :top and @y <= 0
      @y = 0 if @edge == :bottom and @y >= 0
      @duration_completed_at = Gosu.milliseconds if @duration_completed_at == Float::INFINITY
    end
  end
end

#x_offsetObject



225
226
227
228
229
230
231
# File 'lib/cyberarm_engine/notification_manager.rb', line 225

def x_offset
  if not transition_in_complete? or duration_completed?
    Notification::WIDTH * animation_ratio
  else
    0
  end
end

#y_offsetObject



233
234
235
236
237
238
239
# File 'lib/cyberarm_engine/notification_manager.rb', line 233

def y_offset
  if not transition_in_complete? or duration_completed?
    Notification::HEIGHT * animation_ratio
  else
    0
  end
end