Class: CyberarmEngine::NotificationManager

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

Defined Under Namespace

Classes: Driver

Constant Summary collapse

EDGE_TOP =
:top
EDGE_BOTTOM =
:bottom
EDGE_RIGHT =
:right
EDGE_LEFT =
:left
MODE_DEFAULT =
:slide
MODE_CIRCLE =
:circle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edge: EDGE_RIGHT, mode: MODE_DEFAULT, window:, max_visible: 1) ⇒ NotificationManager

Returns a new instance of NotificationManager.



12
13
14
15
16
17
18
19
20
21
# File 'lib/cyberarm_engine/notification_manager.rb', line 12

def initialize(edge: EDGE_RIGHT, mode: MODE_DEFAULT, window:, max_visible: 1)
  @edge = edge
  @mode = mode
  @window = window
  @max_visible = max_visible

  @notifications = []
  @drivers = []
  @slots = Array.new(max_visible, nil)
end

Instance Attribute Details

#edgeObject (readonly)

Returns the value of attribute edge.



11
12
13
# File 'lib/cyberarm_engine/notification_manager.rb', line 11

def edge
  @edge
end

#max_visibleObject (readonly)

Returns the value of attribute max_visible.



11
12
13
# File 'lib/cyberarm_engine/notification_manager.rb', line 11

def max_visible
  @max_visible
end

#modeObject (readonly)

Returns the value of attribute mode.



11
12
13
# File 'lib/cyberarm_engine/notification_manager.rb', line 11

def mode
  @mode
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



11
12
13
# File 'lib/cyberarm_engine/notification_manager.rb', line 11

def notifications
  @notifications
end

Instance Method Details

#available_slot_indexObject



69
70
71
72
73
74
75
# File 'lib/cyberarm_engine/notification_manager.rb', line 69

def available_slot_index
  @slots.each_with_index do |slot, i|
    return i unless slot
  end

  return -1
end

#create_notification(**args) ⇒ Object



94
95
96
97
# File 'lib/cyberarm_engine/notification_manager.rb', line 94

def create_notification(**args)
  notification = Notification.new(host: self, **args)
  @notifications << notification
end

#drawObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cyberarm_engine/notification_manager.rb', line 23

def draw
  @drivers.each do |driver|
    case @edge
    when :left, :right
      x = @edge == :right ? @window.width + driver.x : -Notification::WIDTH + driver.x
      y = driver.y + Notification::HEIGHT / 2

      Gosu.translate(x, y + (Notification::HEIGHT + Notification::PADDING) * driver.slot) do
        driver.draw
      end

    when :top, :bottom
      x = @window.width / 2 - Notification::WIDTH / 2
      y = @edge == :top ? driver.y - Notification::HEIGHT : @window.height + driver.y
      slot_position = (Notification::HEIGHT + Notification::PADDING) * driver.slot
      slot_position *= -1 if @edge == :bottom

      Gosu.translate(x, y + slot_position) do
        driver.draw
      end
    end
  end
end

#highest_used_slotObject



85
86
87
88
89
90
91
92
# File 'lib/cyberarm_engine/notification_manager.rb', line 85

def highest_used_slot
  _slot = -1
  @slots.each_with_index do |slot, i|
    _slot = i if slot
  end

  return _slot
end

#lowest_used_slotObject



77
78
79
80
81
82
83
# File 'lib/cyberarm_engine/notification_manager.rb', line 77

def lowest_used_slot
  @slots.each_with_index do |slot, i|
    return i if slot
  end

  return -1
end

#show_next_notificationObject



59
60
61
62
63
64
65
66
67
# File 'lib/cyberarm_engine/notification_manager.rb', line 59

def show_next_notification
  notification = @notifications.sort { |n| n.priority }.reverse.shift
  return unless notification
  return if available_slot_index < lowest_used_slot
  @notifications.delete(notification)

  @drivers << Driver.new(edge: @edge, mode: @mode, notification: notification, slot: available_slot_index)
  slot = @slots[available_slot_index] = @drivers.last
end

#updateObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cyberarm_engine/notification_manager.rb', line 47

def update
  show_next_notification if @drivers.size < @max_visible
  @drivers.each do |driver|
    if driver.done?
      @slots[driver.slot] = nil
      @drivers.delete(driver)
    end
  end

  @drivers.each(&:update)
end