NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

service.py (3867B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\heronotification\service.py
      2 import collections
      3 import uthread2
      4 from carbon.common.script.sys.service import Service
      5 from carbonui.uianimations import animations
      6 from eve.client.script.ui.heronotification.cancel import CancellationToken
      7 from eve.client.script.ui.heronotification.layer import HeroNotificationLayer
      8 from eve.client.script.ui.heronotification.player import HeroNotificationPlayer
      9 
     10 class HeroNotificationService(Service):
     11     __guid__ = 'svc.heroNotification'
     12     __servicename__ = 'heroNotification'
     13 
     14     def __init__(self):
     15         self._active = None
     16         self._active_priority = 0
     17         self._layer = None
     18         self._queue = []
     19         self._queue_monitor_tasklet = None
     20         self._visible = True
     21         super(HeroNotificationService, self).__init__()
     22 
     23     def play(self, hero_notification, priority = 0):
     24         self._prepare_layer()
     25         cancellation_token = CancellationToken()
     26         if self._active is not None and not self._active.done:
     27             self._add_to_queue(hero_notification, priority, cancellation_token)
     28         else:
     29             self._play_hero_notification(hero_notification, priority, cancellation_token)
     30         return cancellation_token
     31 
     32     def show_notifications(self, animate = True):
     33         if not self._visible:
     34             self._visible = True
     35             self._update_layer_opacity(animate)
     36 
     37     def hide_notifications(self, animate = True):
     38         if self._visible:
     39             self._visible = False
     40             self._update_layer_opacity(animate)
     41 
     42     def _add_to_queue(self, hero_notification, priority, cancellation_token):
     43         if self._active is not None and self._active_priority < priority:
     44             self._active.cancel()
     45         self._queue.append(QueueEntry(priority, hero_notification, cancellation_token))
     46         self._start_queue_monitor_maybe()
     47         return cancellation_token
     48 
     49     def _start_queue_monitor_maybe(self):
     50         if self._queue_monitor_tasklet is None:
     51             self._queue_monitor_tasklet = uthread2.start_tasklet(self._monitor_queue)
     52 
     53     def _monitor_queue(self):
     54         try:
     55             while len(self._queue) > 0:
     56                 if self._active is not None:
     57                     self._active.wait()
     58                 self._queue = sorted(self._queue, key=lambda e: e.priority or 0)
     59                 entry = self._queue.pop(-1)
     60                 if entry.cancellation_token.cancelled:
     61                     continue
     62                 self._play_hero_notification(hero_notification=entry.hero_notification, priority=entry.priority, cancellation_token=entry.cancellation_token)
     63 
     64         finally:
     65             self._queue_monitor_tasklet = None
     66 
     67     def _play_hero_notification(self, hero_notification, priority, cancellation_token):
     68         self._layer.cleanup()
     69         self._active_priority = priority
     70         self._active = HeroNotificationPlayer(hero_notification, container=self._layer.create_hero_notification_container(), cancellation_token=cancellation_token)
     71         self._active.start()
     72 
     73     def _get_layer_opacity(self):
     74         if self._visible:
     75             return 1.0
     76         return 0.0
     77 
     78     def _prepare_layer(self):
     79         if self._layer is not None and self._layer.destroyed:
     80             self._layer = None
     81         if self._layer is None:
     82             self._layer = HeroNotificationLayer(opacity=self._get_layer_opacity())
     83 
     84     def _update_layer_opacity(self, animate = True):
     85         if self._layer is not None:
     86             if animate:
     87                 animations.FadeTo(self._layer, startVal=self._layer.opacity, endVal=self._get_layer_opacity(), duration=0.5)
     88             else:
     89                 self._layer.opacity = self._get_layer_opacity()
     90 
     91 
     92 QueueEntry = collections.namedtuple('QueueEntry', ['priority', 'hero_notification', 'cancellation_token'])