countercontroller.py (3863B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\spacecomponents\countercontroller.py 2 import uthread 3 from eve.client.script.ui.control.countdownTimer import CountdownTimer, TIMER_RUNNING_OUT_NO_ANIMATION 4 import localization 5 import blue 6 import logging 7 logger = logging.getLogger(__name__) 8 9 class BaseCounterController(object): 10 __componentClass__ = 'None' 11 __counterColor__ = (1.0, 1.0, 1.0, 1.0) 12 __pausedColor__ = (1.0, 1.0, 1.0, 1.0) 13 __counterLabel__ = '' 14 __counterLabelPaused__ = None 15 __timerFunc__ = blue.os.GetWallclockTime 16 __countsDown__ = False 17 __soundInitialEvent__ = None 18 __soundLoopPlayEvent__ = None 19 __soundLoopStopEvent__ = None 20 __soundFinishedEvent__ = None 21 22 def __init__(self, bracket, componentRegistry, slimItem): 23 logger.debug('%s created for item %s', self.__class__.__name__, slimItem.itemID) 24 self.itemID = slimItem.itemID 25 self.bracket = bracket 26 self.componentRegistry = componentRegistry 27 self.timer = None 28 self.endTime = None 29 self.pausedTime = None 30 self.CreateTimerIfAppropriate(slimItem) 31 32 def CreateTimerIfAppropriate(self, slimItem): 33 instance = self.componentRegistry.GetComponentForItem(slimItem.itemID, self.__componentClass__) 34 self.UpdateTimerState(instance, slimItem) 35 36 def AddTimer(self, endTime, duration, pausedTime = None): 37 self.endTime = endTime 38 self.pausedTime = pausedTime 39 self.timer = CountdownTimer(name='ComponentCounter', parent=self.bracket, color=self.__counterColor__ if not self.pausedTime else self.__pausedColor__, countsDown=self.__countsDown__, timerFunc=self.__timerFunc__, timerRunningOutAnimation=TIMER_RUNNING_OUT_NO_ANIMATION) 40 self.PlayInitialSound() 41 self.timer.SetSoundLoop(self.__soundLoopPlayEvent__, self.__soundLoopStopEvent__) 42 self.timer.SetExpiryTime(endTime, long(duration * uthread.SEC), timerPaused=pausedTime) 43 self.bracket.SetSubLabelCallback(self.UpdateTimerLabel) 44 45 def ChangeTimer(self, endTime, duration, pausedTime = None): 46 self.endTime = endTime 47 self.pausedTime = pausedTime 48 if self.pausedTime: 49 self.timer.SetTimerColor(self.__pausedColor__) 50 else: 51 self.timer.SetTimerColor(self.__counterColor__) 52 self.timer.SetExpiryTime(endTime, duration * uthread.SEC, pausedTime) 53 54 def RemoveTimer(self): 55 if self.timer: 56 uthread.new(self.timer.EndAnimation) 57 self.timer = None 58 self.bracket.RemoveSubLabelCallback(self.UpdateTimerLabel) 59 self.PlayEndSound() 60 61 def UpdateTimerState(self, instance, slimItem): 62 raise NotImplementedError('You need to implement the specific component logic') 63 64 def GetCounterLabel(self): 65 if self.pausedTime is not None and self.__counterLabelPaused__ is not None: 66 return localization.GetByLabel(self.__counterLabelPaused__) 67 else: 68 return localization.GetByLabel(self.__counterLabel__) 69 70 def UpdateTimerLabel(self): 71 if self.timer is None or self.endTime is None: 72 return 73 timeLeft = long(max(0, self.endTime - self.timer.GetCurrentTime())) 74 return u'{label}: {time}'.format(label=self.GetCounterLabel(), time=localization.formatters.FormatTimeIntervalShortWritten(timeLeft, showFrom='day', showTo='second')) 75 76 def GetHorizontalLabelPixelOffset(self): 77 if self.timer is None: 78 return 0 79 return 6 80 81 def PlayEndSound(self): 82 if self.__soundFinishedEvent__ is not None: 83 sm.GetService('audio').SendUIEvent(self.__soundFinishedEvent__) 84 85 def PlayInitialSound(self): 86 if self.__soundInitialEvent__ is not None: 87 sm.GetService('audio').SendUIEvent(self.__soundInitialEvent__)