class Timer(Clock): def __init__(self, instanceId, sceneId=None): super(Clock, self).__init__() self.id = instanceId self.sceneId = sceneId self.instanceObj = None self.callback = None self.timerLimit = None self.scene = Scene(sceneId).getscene() if 'timer_instances' not in logic.globalDict: logic.globalDict['timer_instances'] = {} def load(self): instance = logic.globalDict['timer_instances'][self.id] self.sceneId = instance['scene_id'] self.callback = instance['callback'] self.scene = Scene(self.sceneId).getscene() self.timerLimit = instance['time_limit'] self.instanceObj = ObjProperties().getObjByPropVal( 'instance_id', self.id, self.scene.objects) Clock.__init__(self, self.instanceObj) def _addInstance(self): log.debug("Creating timer instance %s", self.id) obj = ObjProperties() idleInstanceList = obj.getPropObjGroup('timer_instance', self.scene, 0) idleInstanceObj = idleInstanceList[0] idleInstanceObj['instance_id'] = self.id self.scene.addObject(idleInstanceObj) timerObj = obj.getObjByPropVal('instance_id', self.id, self.scene.objects) return timerObj def _setGlobals(self): logic.globalDict['timer_instances'][self.id] = { 'callback': self.callback, 'scene_id': self.sceneId, 'time_limit': self.timerLimit } def isAlive(self): return self.id in logic.globalDict['timer_instances'] def destroy(self): log.debug("destroying timer instance %s", self.id) if not self.instanceObj: return self.instanceObj.endObject() del logic.globalDict['timer_instances'][self.id] def setTimer(self, time, func, *args, **kwargs): instanceObj = self._addInstance() self.timerLimit = time self.callback = lambda: func(*args, **kwargs) self.instanceObj = instanceObj self._setGlobals() Clock.__init__(self, instanceObj)
def load(self): instance = logic.globalDict['timer_instances'][self.id] self.sceneId = instance['scene_id'] self.callback = instance['callback'] self.scene = Scene(self.sceneId).getscene() self.timerLimit = instance['time_limit'] self.instanceObj = ObjProperties().getObjByPropVal( 'instance_id', self.id, self.scene.objects) Clock.__init__(self, self.instanceObj)
def __init__(self, instanceId, sceneId=None): super(Clock, self).__init__() self.id = instanceId self.sceneId = sceneId self.instanceObj = None self.callback = None self.timerLimit = None self.scene = Scene(sceneId).getscene() if 'timer_instances' not in logic.globalDict: logic.globalDict['timer_instances'] = {}
def _attachBg(targetObj, col, sceneName, visible=True): from objproperties import ObjProperties from scene_helper import Scene scene = Scene(sceneName).getscene() bg = ObjProperties().getPropObjGroup('background_view', scene, 0)[0] bg['target_obj'] = str(targetObj) bg.visible = visible bg.color = col scene.addObject(bg) bg = ObjProperties().getObjByPropVal('target_obj', str(targetObj), scene.objects) bg.position = targetObj.position # push it back alittle bg.position[2] -= 0.4 bg.setParent(targetObj)
def loadCursorObjs(): ''' Loads all cursor objects from hidden layer into active layer. Cursors are parented to an empty cursor object and visibility set to False. ''' from objproperties import ObjProperties scene = Scene('HUD').getscene() parentCursor = getCursorObj() cursorList = ObjProperties().getPropObjGroup('mouse_pointer', scene, 0) for cursor in cursorList: cursor.visible = False scene.addObject(cursor, parentCursor) scene.objects[str(cursor)].setParent(parentCursor)
def __init__(self, canvas, position, isVisible=True): self.canvas = canvas if not self.canvas.isset(): self.canvas.add( Scene('HUD').getscene().objects[position], isVisible ) else: self.canvas.load()
def addChallengeViewer(pattern): from objproperties import ObjProperties from pattern_loader import PatternLoader from challenge_viewer_canvas import ChallengeViewerCanvas from menu import PopUpMenu, CENTER_POSITION_NODE menu = PopUpMenu(ChallengeViewerCanvas(), CENTER_POSITION_NODE) PatternLoader(Scene('HUD').getscene(), pattern).load() return menu
def init(): def preloadScenes(): if preload(Scene('PRELOAD').getscene()): Scene('HUD').addOverlay() Scene(DEFAULT_SCENE).addBackground() if 'preload_scene_init' not in logic.globalDict: ''' Add an overlay and set a timer to start preloading scenes ''' logic.globalDict['preload_scene_init'] = True Scene('PRELOAD_DUMMY').addOverlay() timer = Timer('Scene_Preloader', 'PRELOAD') timer.setTimer(6.0, preloadScenes) timer.start() return preloadScenes()
def animateVisualBlocks(challengeName, timerDelay=0.3): from button_effects import clickAnimation from block_effects import animate_match_color from objproperties import ObjProperties from timer import Timer id = 'pattern_visualiser_data' scene = Scene('HUD').getscene() if id not in logic.globalDict: logic.globalDict[id] = { 'total': len(ObjProperties().getPropObjGroup('visual_block', scene)), 'index': 0 } else: logic.globalDict[id]['index'] = 0 def markVisualBlocks(): data = logic.globalDict[id] data['index'] += 1 if data['index'] <= data['total']: vsBlock = ObjProperties().getObjByPropVal('visual_block', data['index'], scene.objects) if vsBlock: timer = Timer(id, 'HUD') timer.setTimer( timerDelay, lambda: clickAnimation( vsBlock, lambda: animate_match_color( vsBlock, onfinishAction=markVisualBlocks))) timer.start() else: logic.globalDict[id]['index'] = 0 markVisualBlocks()
def showNotification(message, duration=8.0, callback=None, sound=None): def onFinish(notification, timer): from audio_files import NOTIFICATION_CHIME from audio import Audio timer.setTimer( duration, lambda: fadeOut(notification, onFinishAction=notification.remove)) timer.start() Audio(NOTIFICATION_CHIME).play() scene = Scene('HUD').getscene() notification = NotificationCanvas('HUD') if not notification.isset(): notification.add(scene.objects['notification_position_node'], False) timer = Timer(HUD_NOTIFICATION_ID, 'HUD') if timer.isAlive(): timer.load() timer.destroy() Text(notification.infoTxtObj, text=message, limit=80, width=30) fadeIn(notification, onFinishAction=lambda: onFinish(notification, timer))
def closeOverlay(name): scene = Scene(name) if scene.isset(): scene.remove()
def overlay(name): Scene(name).addOverlay()
def navigate(name): Scene(name).addBackground()
def resumeScene(name): scene = Scene(name) if scene.isset(): scene.resume()
def __getScene(self): return Scene(self.getScene()).getscene()
def preloadScenes(): if preload(Scene('PRELOAD').getscene()): Scene('HUD').addOverlay() Scene(DEFAULT_SCENE).addBackground()
def _getBackgroundScene(self): id = SceneGlobalData().getBackgroundSceneId() if id: return Scene(id) return None
def init(): from scene_helper import Scene from challenge_menu_listerners import OnStartMenuListingListerner scene = Scene('CHALLENGES_MENU').getscene() setChallengeMenus(scene)
def getCursorObj(self, cursor=None): cur = self.cursor if not cursor else cursor return Scene('HUD').getscene().objects[cur]
def getCursorObj(): ''' Returns Empty cursor object ''' return Scene('HUD').getscene().objects['mouse_cursor']
def suspendScene(name): scene = Scene(name) if scene.isset(): scene.suspend()
def getHudScene(): from scene_helper import Scene return Scene('HUD')