def __parse_position_data(self, yaml_struct, for_text=False):
        """ returns a tuple of x/y values for placement or x/y/horiz_justify/vert_justify
            if for_text is True; expects data to be found in yaml_struct.
            Fallback values are different for_text (centered) vs. other types of
            content (top left -- 0,0)
        """

        if (for_text):
            fallback = 0.5
        else:
            fallback = int(0)

        x = self.__parse_relative_num(yaml_struct,
                                      key='x',
                                      relative_to=self.game.dmd.width,
                                      default=fallback,
                                      relative_match="width")
        y = self.__parse_relative_num(yaml_struct,
                                      key='y',
                                      relative_to=self.game.dmd.height,
                                      default=fallback,
                                      relative_match="height")

        if (for_text):
            vj = value_for_key(yaml_struct, 'v_justify', "center")
            hj = value_for_key(yaml_struct, 'h_justify', "center")
            return (x, y, hj, vj)
        return (x, y)
    def __parse_relative_num(self,
                             yaml_struct,
                             key,
                             relative_to,
                             default,
                             relative_match=None):
        """ parses the key from the given yaml_struct and computes
            its value relative to the value passed for relative_to

            if a positive int is specified, the value is taken as-is
            if a negative int is specified, the value is taken as offset from the relative value
            if a float is specified the value is taken as a percentage of the relative value
            if a string value matching the key is specified, the relative value is returned
                (equiv to specifying 1.0 as 100%)
        """
        if (relative_match is None):
            relative_match = key

        tmp = value_for_key(yaml_struct, key, default)
        if (isinstance(tmp, int)):
            # offset values -- use the values as given unless negative!
            if (tmp < 0):
                tmp = relative_to + tmp
            pass
        elif (isinstance(tmp, float)):
            # percentage values - set to appropriate percentage of 'relative_to' value
            tmp = relative_to * tmp
        elif (isinstance(tmp, basestring) and (tmp == relative_match)):
            tmp = relative_to
        return tmp
    def __parse_relative_num(self, yaml_struct, key, relative_to, default, relative_match=None):
        """ parses the key from the given yaml_struct and computes
            its value relative to the value passed for relative_to

            if a positive int is specified, the value is taken as-is
            if a negative int is specified, the value is taken as offset from the relative value
            if a float is specified the value is taken as a percentage of the relative value
            if a string value matching the key is specified, the relative value is returned
                (equiv to specifying 1.0 as 100%)
        """
        if(relative_match is None):
            relative_match = key

        tmp = value_for_key(yaml_struct, key, default)
        if(isinstance(tmp,int)):
            # offset values -- use the values as given unless negative!
            if(tmp < 0):
                tmp = relative_to + tmp
            pass
        elif(isinstance(tmp,float)):
            # percentage values - set to appropriate percentage of 'relative_to' value
            tmp = relative_to * tmp
        elif(isinstance(tmp, basestring) and (tmp==relative_match)):
            tmp = relative_to
        return tmp
    def genLayerFromYAML(self, yamlStruct):
        duration = None
        lampshow = None
        sound = None
        lyrTmp = None

        if('Combo' in yamlStruct):
            v = yamlStruct['Combo']

            fsV = value_for_key(v, 'FontStyle')
            if(fsV is not None):
                font_style=self.game.fontstyles[fsV]
            else:
                font_style=None
            lyrTmp = self.game.generateLayer(value_for_key(v,'Text'), value_for_key(v,'Animation'), font_key=value_for_key(v,'Font'), font_style=font_style)
            duration = value_for_key(v,'duration')
        elif ('Animation' in yamlStruct):
            v = yamlStruct['Animation']
            lyrTmp = self.game.animations[value_for_key(v,'Name')]
            lyrTmp.reset()
            duration = lyrTmp.duration()

        if(v is not None):
            lampshow = value_for_key(v, 'lampshow')
            sound = value_for_key(v, 'sound')

        return (lyrTmp, duration, lampshow, sound)
    def __parse_position_data(self, yaml_struct, for_text=False):
        """ returns a tuple of x/y values for placement or x/y/horiz_justify/vert_justify
            if for_text is True; expects data to be found in yaml_struct.
            Fallback values are different for_text (centered) vs. other types of
            content (top left -- 0,0)
        """
        
        if(for_text):
            fallback = 0.5
        else:
            fallback = int(0)

        x = self.__parse_relative_num(yaml_struct, key='x', relative_to=self.game.dmd.width, default=fallback, relative_match="width")
        y = self.__parse_relative_num(yaml_struct, key='y', relative_to=self.game.dmd.height, default=fallback, relative_match="height")

        if(for_text):
            vj = value_for_key(yaml_struct, 'v_justify', "center")
            hj = value_for_key(yaml_struct, 'h_justify', "center")
            return (x,y,hj,vj)
        return (x,y)
    def generateTextLayerFromYaml(self, yaml_struct):
        """ parses a text descriptor format yaml and generates a text layer to be filled with text via set_text() 
              For now, the score_display.yaml example(s) should suffice; better documentation forthcoming
        """
        enabled = value_for_key(yaml_struct, 'enabled', True)
        if(not enabled):
            return None

        (f, font_style) = self.parse_font_data(yaml_struct)
        (x,y,hj,vj) = self.__parse_position_data(yaml_struct, for_text=True)
        opaque = value_for_key(yaml_struct, 'opaque', False)

        # create the layer -- it matters if we have an HD font or not...
        if(isinstance(f,HDFont)):
            tL = dmd.HDTextLayer(x=x, y=y, font=f, justify=hj, vert_justify=vj, opaque=opaque, width=None, height=None)
            tL.style = font_style            
        else:
            tL = dmd.TextLayer(x=x, y=y, font=f, justify=hj, opaque=opaque, width=None, height=None, fill_color=None) 
              
        tL.enabled = value_for_key(yaml_struct,"visible",True)

        return tL
    def generateTextLayerFromYaml(self, yaml_struct):
        """ parses a text descriptor format yaml and generates a text layer to be filled with text via set_text() 
              For now, the score_display.yaml example(s) should suffice; better documentation forthcoming
        """
        enabled = value_for_key(yaml_struct, 'enabled', True)
        if(not enabled):
            return None

        (f, font_style) = self.parse_font_data(yaml_struct)
        (x,y,hj,vj) = self.__parse_position_data(yaml_struct, for_text=True)
        opaque = value_for_key(yaml_struct, 'opaque', False)

        # create the layer -- it matters if we have an HD font or not...
        if(isinstance(f,HDFont)):
            tL = dmd.HDTextLayer(x=x, y=y, font=f, justify=hj, vert_justify=vj, opaque=opaque, width=None, height=None)
            tL.style = font_style            
        else:
            tL = dmd.TextLayer(x=x, y=y, font=f, justify=hj, opaque=opaque, width=None, height=None, fill_color=None) 
              
        tL.enabled = value_for_key(yaml_struct,"visible",True)

        return tL
    def parse_font_data(self, yaml_struct, required=True):
        """ returns a Font and FontStyle as loaded from a yaml based descriptor of
            Font and FontStyle information. """
        # get font
        fname = value_for_key(yaml_struct, 'font',
                              value_for_key(yaml_struct, 'Font'))
        if (fname is None):
            if (required):
                raise ValueError, "Cannot find required Font tag in yaml segment [%s]" % (
                    yaml_struct)
            else:
                f = None
        else:
            if (fname not in self.game.fonts):
                self.logger.error(
                    "yaml refers to a font '%s' that does not exist.  Please check the assetList to ensure this font is present"
                    % fname)

            # the assetManager will take care of providing a default font even if the above doesn't work
            f = self.game.fonts[fname]

        # get font style
        font_style = value_for_key(yaml_struct, 'font_style',
                                   value_for_key(yaml_struct, 'FontStyle'))
        if (isinstance(font_style, dict)):
            # dive deeper into this struct, making a new font_style on the fly for the user
            ic = value_for_key(font_style, 'interior_color')
            lc = value_for_key(font_style, 'line_color')
            lw = value_for_key(font_style, 'line_width')
            # k = value_for_key(font_style, 'key')
            font_style = dmd.HDFontStyle(interior_color=ic,
                                         line_width=lw,
                                         line_color=lc)
            #self.fontstyles[k] = font_style

        elif (isinstance(font_style, basestring)):
            font_style = self.game.fontstyles[font_style]
        else:
            # no font style specified or value is none
            font_style = None

        return f, font_style
    def parse_font_data(self, yaml_struct, required=True):
        """ returns a Font and FontStyle as loaded from a yaml based descriptor of
            Font and FontStyle information. """
        # get font
        fname = value_for_key(yaml_struct, 'font', value_for_key(yaml_struct, 'Font'))
        if(fname is None):
            if(required):
                raise ValueError, "Cannot find required Font tag in yaml segment [%s]" % (yaml_struct)
            else:
                f = None 
        else:
            if(fname not in self.game.fonts):
                self.logger.error("yaml refers to a font '%s' that does not exist.  Please check the assetList to ensure this font is present" % fname)

            # the assetManager will take care of providing a default font even if the above doesn't work
            f = self.game.fonts[fname]

        # get font style
        font_style = value_for_key(yaml_struct, 'font_style', value_for_key(yaml_struct, 'FontStyle'))
        if(isinstance(font_style,dict)):
            # dive deeper into this struct, making a new font_style on the fly for the user
            ic = value_for_key(font_style, 'interior_color')
            lc = value_for_key(font_style, 'line_color')
            lw = value_for_key(font_style, 'line_width')
            # k = value_for_key(font_style, 'key')
            font_style = dmd.HDFontStyle( interior_color=ic, 
                                    line_width=lw, 
                                    line_color=lc )
            #self.fontstyles[k] = font_style

        elif(isinstance(font_style,basestring)):
            font_style=self.game.fontstyles[font_style]
        else:
            # no font style specified or value is none
            font_style = None

        return f, font_style
    def load(self):
        anims = self.value_for_key_path(keypath='Animations', default={}) or list()
        fonts = self.value_for_key_path(keypath='Fonts', default={}) or list()
        hfonts = value_for_key(fonts,'HDFonts',{}) or list()
        rfonts = value_for_key(fonts,'DMDFonts',{}) or list()
        fontstyles = value_for_key(fonts,'FontStyles',{}) or list()
        lamps = self.value_for_key_path(keypath='LampShows', default={}) or list() 
        sounds = self.value_for_key_path(keypath='Audio', default={}) or list()
        music = value_for_key(sounds,'Music',{}) or list()
        effects = value_for_key(sounds,'Effects',{}) or list() 
        voice = value_for_key(sounds,'Voice',{}) or list()
        
        # self.total = str(len(anims)+len(hfonts)+len(rfonts)+len(music)+len(effects)+len(voice))
        self.total = (len(lamps) + len(fontstyles) + len(anims)+len(hfonts)+len(rfonts)+len(music)+len(effects)+len(voice))

        try:
            current = ""            
            for l in lamps:
                k  = value_for_key(l,'key')
                fname = value_for_key(l,'file')
                self.updateProgressBar("Lampshows", fname)
                # self.lampshows = self.game.sound.register_music(k,self.game.music_path+fname, volume=volume)            
                f = self.game.lampshow_path + fname
                current = 'Lampshow: [%s]: %s, %s ' % (k, f, fname)
                self.game.lampctrl.register_show(k, f)

                # Validate the lampshow --as best as possible
                self.game.lampctrl.show.load(f, False, None)
                for tr in self.game.lampctrl.show.lampshow.tracks:
                    if tr.driver == None: # Check drivers.
                        tr.resolve_driver_with_game(self.game)
                    if tr.driver == None:
                        raise ValueError, "Name '%s' specified in lampshow does not match a driver in the machine yaml." % tr.name

                self.numLoaded += 1            
            for f in hfonts:
                k  = value_for_key(f,'key')
                sname = value_for_key(f,'systemName',k)
                size  = value_for_key(f,'size')
                file_path = value_for_key(f, 'file', None)
                self.updateProgressBar("HD Fonts", sname)
                current = 'HD font: [%s]: %s, %d ' % (k, sname, size)

                if(file_path is not None):
                    file_path = self.game.hdfont_path + file_path
                    if(not os.path.isfile(file_path)):
                        raise ValueError, "Could not load font as specified in yaml\n %s\n File [%s] does not exist." % (current, file_path)


                self.fonts[k] = dmd.hdfont_named(sname,size, font_file_path=file_path)
                self.numLoaded += 1

            for f in rfonts:
                k  = value_for_key(f,'key')
                fname = value_for_key(f,'file')
                self.updateProgressBar("DMD Fonts", fname)
                current = 'Font: [%s]: %s ' % (k, fname)
                self.fonts[k] = dmd.font_named(fname)
                self.numLoaded += 1

            for f in fontstyles:
                ic = value_for_key(f, 'interior_color')
                lc = value_for_key(f, 'line_color')
                lw = value_for_key(f, 'line_width')
                k = value_for_key(f, 'key')
                font_style = dmd.HDFontStyle( interior_color=ic, 
                                        line_width=lw, 
                                        line_color=lc )
                self.fontstyles[k] = font_style

            for anim in anims:
                k  = value_for_key(anim,'key')
                ft = value_for_key(anim,'frame_time',2)
                f  = value_for_key(anim,'file')
                r  = value_for_key(anim,'repeatAnim',False)
                h  = value_for_key(anim,'holdLastFrame',False)
                o  = value_for_key(anim,'opaque',False)
                c  = value_for_key(anim,'composite_op')
                x  = value_for_key(anim, 'x_loc', 0)
                y  = value_for_key(anim, 'y_loc', 0)
                streaming_load  = value_for_key(anim, 'streamingMovie', False)
                current = 'Animation: [%s]: %s' % (k, f)
                self.loadIntoCache(k,ft,f,r,h,o,c,x,y,streaming_load)

        except:
            self.logger.error("===ASSET MANAGER - ASSET FAILURE===")
            self.logger.error(current)
            self.logger.error("======")
            raise

        for s in music:
            k  = value_for_key(s,'key')
            fname = value_for_key(s,'file')
            volume = value_for_key(s,'volume',.5)
            self.updateProgressBar("Audio: Music", fname)
            self.game.sound.register_music(k,self.game.music_path+fname, volume=volume)
            self.numLoaded += 1

        for s in effects:
            k  = value_for_key(s,'key')
            fname = value_for_key(s,'file')
            volume = value_for_key(s,'volume',.5)
            self.updateProgressBar("Audio SFX", fname)
            self.game.sound.register_sound(k,self.game.sfx_path+fname, volume=volume)
            self.numLoaded += 1

        for s in voice:
            k  = value_for_key(s,'key')
            fname = value_for_key(s,'file')
            channel = value_for_key(s,'channel',0)
            volume = value_for_key(s,'volume',.5)
            self.updateProgressBar("Audio Voices", fname)
            # self.game.sound.register_sound(k,self.game.voice_path+fname, channel=channel, volume=volume)
            self.game.sound.register_sound(k,self.game.voice_path+fname, volume=volume)
            self.numLoaded += 1
 def value_for_key_path(self, keypath, default=None):
     return value_for_key(self.values,keypath, default)
    def load(self):
        l = logging.getLogger("PIL.PngImagePlugin")
        l.setLevel(logging.WARNING)
        l = logging.getLogger("game.assets")
        l.setLevel(logging.WARNING)        
        l = logging.getLogger("game.sound")
        l.setLevel(logging.WARNING)        
        l = logging.getLogger("game.dmdcache")
        l.setLevel(logging.WARNING)        
        anims = self.value_for_key_path(keypath='Animations', default={}) or list()
        fonts = self.value_for_key_path(keypath='Fonts', default={}) or list()
        hfonts = value_for_key(fonts,'HDFonts',{}) or list()
        rfonts = value_for_key(fonts,'DMDFonts',{}) or list()
        fontstyles = value_for_key(fonts,'FontStyles',{}) or list()
        lamps = self.value_for_key_path(keypath='LampShows', default={}) or list() 
        rgbshows = self.value_for_key_path(keypath='RGBShows', default={}) or list() 
        sounds = self.value_for_key_path(keypath='Audio', default={}) or list()
        music = value_for_key(sounds,'Music',{}) or list()
        effects = value_for_key(sounds,'Effects',{}) or list() 
        voice = value_for_key(sounds,'Voice',{}) or list()
        
        paths = self.value_for_key_path(keypath='AssetListFiles', default={}) or list()
        #if there was a list of files to load, then load those
        for path in paths:
            self.loaded_assets_files.append(path)
            self.values = yaml.load(open(path, 'r'))
            anims += self.value_for_key_path(keypath='Animations', default={}) or list()
            fonts = self.value_for_key_path(keypath='Fonts') or list()
            hfonts += value_for_key(fonts,'HDFonts',{}) 
            rfonts += value_for_key(fonts,'DMDFonts',{}) or list()
            fontstyles += value_for_key(fonts,'FontStyles',{}) or list()
            lamps += self.value_for_key_path(keypath='LampShows', default={}) or list() 
            rgbshows += self.value_for_key_path(keypath='RGBShows', default={}) or list() 
            sounds = self.value_for_key_path(keypath='Audio', default={}) or list()
            music += value_for_key(sounds,'Music',{}) or list()
            effects += value_for_key(sounds,'Effects',{}) or list() 
            voice += value_for_key(sounds,'Voice',{}) or list()

        # self.total = str(len(anims)+len(hfonts)+len(rfonts)+len(music)+len(effects)+len(voice))
        self.total = (len(lamps) + len(fontstyles) + len(anims)+len(hfonts)+len(rfonts)+len(music)+len(effects)+len(voice))

        try:
            current = ""            
            for l in lamps:
                k  = value_for_key(l,'key')
                fname = value_for_key(l,'file')
                self.updateProgressBar("Lampshows", fname)
                f = self.game.lampshow_path + fname
                current = 'Lampshow: [%s]: %s, %s ' % (k, f, fname)
                self.game.lampctrl.register_show(k, f)

                # Validate the lampshow --as best as possible
                self.game.lampctrl.show.load(f, False, None)
                for tr in self.game.lampctrl.show.lampshow.tracks:
                    if tr.driver == None: # Check drivers.
                        tr.resolve_driver_with_game(self.game)
                    if tr.driver == None:
                        raise ValueError, "Name '%s' specified in lampshow does not match a driver in the machine yaml." % tr.name

                self.numLoaded += 1            

            for l in rgbshows:
                k  = value_for_key(l,'key')
                fname = value_for_key(l,'file')
                self.updateProgressBar("RGBShows", fname)
                f = self.game.lampshow_path + fname
                current = 'RGBshow: [%s]: %s, %s ' % (k, f, fname)
                self.game.rgbshow_player.load(k, f)
                self.numLoaded += 1            

            for f in hfonts:
                k  = value_for_key(f,'key')
                sname = value_for_key(f,'systemName',k)
                size  = value_for_key(f,'size')
                file_path = value_for_key(f, 'file', None)
                self.updateProgressBar("HD Fonts", sname)
                current = 'HD font: [%s]: %s, %d ' % (k, sname, size)

                if(file_path is not None):
                    file_path = self.game.hdfont_path + file_path
                    if(not os.path.isfile(file_path)):
                        raise ValueError, "Could not load font as specified in yaml\n %s\n File [%s] does not exist." % (current, file_path)


                self.fonts[k] = dmd.hdfont_named(sname,size, font_file_path=file_path)
                self.numLoaded += 1

            for f in rfonts:
                k  = value_for_key(f,'key')
                fname = value_for_key(f,'file')
                self.updateProgressBar("DMD Fonts", fname)
                current = 'Font: [%s]: %s ' % (k, fname)
                self.fonts[k] = dmd.font_named(fname)
                self.numLoaded += 1

            for f in fontstyles:
                ic = value_for_key(f, 'interior_color')
                lc = value_for_key(f, 'line_color')
                lw = value_for_key(f, 'line_width')
                k = value_for_key(f, 'key')
                font_style = dmd.HDFontStyle( interior_color=ic, 
                                        line_width=lw, 
                                        line_color=lc )
                self.fontstyles[k] = font_style

            for anim in anims:
                k  = value_for_key(anim,'key')
                ft = value_for_key(anim,'frame_time',2)
                f  = value_for_key(anim,'file')
                r  = value_for_key(anim,'repeatAnim',False)
                h  = value_for_key(anim,'holdLastFrame',False)
                o  = value_for_key(anim,'opaque',False)
                c  = value_for_key(anim,'composite_op')
                x  = value_for_key(anim, 'x_loc', 0)
                y  = value_for_key(anim, 'y_loc', 0)
                streaming_load  = value_for_key(anim, 'streamingMovie', False)
                current = 'Animation: [%s]: %s' % (k, f)
                # started = timeit.time.time()
                started = timeit.time.time()
                self.loadIntoCache(k,ft,f,r,h,o,c,x,y,streaming_load)
                time_taken = timeit.time.time() - started
                self.logger.info("loading visual asset took %.3f seconds" % time_taken)
        except:
            self.logger.error("===ASSET MANAGER - ASSET FAILURE===")
            self.logger.error(current)
            self.logger.error("======")
            raise

        for s in music:
            k  = value_for_key(s,'key')
            fname = value_for_key(s,'file')
            volume = value_for_key(s,'volume',.5)
            streaming_load = value_for_key(s,'streaming_load',True)
            self.updateProgressBar("Audio: Music", fname)
            self.game.sound.register_music(k,self.game.music_path+fname, volume=volume) #, streaming_load=streaming_load)
            self.numLoaded += 1

        for s in effects:
            k  = value_for_key(s,'key')
            fname = value_for_key(s,'file')
            volume = value_for_key(s,'volume',.5)
            is_voice = value_for_key(s, 'voice', False)
            self.updateProgressBar("Audio SFX", fname)
            self.game.sound.register_sound(k,self.game.sfx_path+fname, volume=volume, is_voice=is_voice)
            self.numLoaded += 1

        for s in voice:
            k  = value_for_key(s,'key')
            fname = value_for_key(s,'file')
            volume = value_for_key(s,'volume',.5)
            self.updateProgressBar("Audio Voices", fname)
            self.game.sound.register_sound(k,self.game.voice_path+fname, volume=volume, is_voice=True)
            self.numLoaded += 1
Exemple #13
0
    def genLayerFromYAML(self, yamlStruct):
        """ a helper that parses the 'attract sequence' format -- an augmented version
            of the yaml serialized layer format, however also includes coordination 
            of lampshows and sound playback """

        duration = None
        lampshow = None
        sound = None
        lyrTmp = None
        v = None
        try:
            if ('HighScores' in yamlStruct):
                v = yamlStruct['HighScores']

                fields = value_for_key(v, 'Order')
                duration = value_for_key(v, 'duration', 2.0)
                lampshow = value_for_key(v, 'lampshow')
                sound = value_for_key(v, 'sound')
                (fnt, font_style) = self.parse_font_data(v, required=False)

                background = value_for_key(v, 'Background',
                                           value_for_key(v, 'Animation'))

                lyrTmp = dmd.ScriptlessLayer(self.game.dmd.width,
                                             self.game.dmd.height)
                entry_ct = len(self.game.get_highscore_data())
                for rec in self.game.get_highscore_data():
                    if fields is not None:
                        records = [rec[f] for f in fields]
                    else:
                        records = [
                            rec['category'], rec['player'], rec['score']
                        ]
                    lT = self.genMsgFrame(records,
                                          background,
                                          font_key=fnt,
                                          font_style=font_style)

                    lyrTmp.append(lT, duration)

                duration = entry_ct * duration

            elif ('LastScores' in yamlStruct):
                v = yamlStruct['LastScores']

                duration = value_for_key(v, 'duration', 2.0)
                lampshow = value_for_key(v, 'lampshow')
                sound = value_for_key(v, 'sound')

                (fnt, font_style) = self.parse_font_data(v, required=False)

                last_score_count = len(self.game.old_players)

                if (last_score_count == 0):
                    return None

                background = value_for_key(v, 'Background',
                                           value_for_key(v, 'Animation'))

                lyrTmp = dmd.ScriptlessLayer(self.game.dmd.width,
                                             self.game.dmd.height)
                lyrTmp.append(
                    self.genMsgFrame(["Last Game", "Final Scores"],
                                     background,
                                     font_key=fnt,
                                     font_style=font_style), duration)

                for player in self.game.old_players:
                    lT = self.genMsgFrame([
                        player.name,
                        self.game.score_display.format_score(player.score)
                    ],
                                          background,
                                          font_key=fnt,
                                          font_style=font_style)
                    lyrTmp.append(lT, duration)

                duration = (last_score_count + 1) * duration

            elif ('RandomText' in yamlStruct):
                v = yamlStruct['RandomText']
                (fnt, font_style) = self.parse_font_data(v, required=False)
                randomText = value_for_key(v,
                                           'TextOptions',
                                           exception_on_miss=True)
                headerText = value_for_key(v, 'Header', None)
                duration = value_for_key(v, 'duration')

                rndmLayers = []
                for line in randomText:
                    selectedRandomText = line['Text']
                    if (type(selectedRandomText) is list):
                        completeText = selectedRandomText
                    else:
                        completeText = [selectedRandomText]

                    if (headerText is not None):
                        completeText[:0] = [
                            headerText
                        ]  # prepend the header text entry at the start of the list

                    rndmLayers.append(
                        self.genMsgFrame(completeText,
                                         value_for_key(v, 'Animation'),
                                         font_key=fnt,
                                         font_style=font_style))

                if (len(rndmLayers) > 0):
                    lyrTmp = RandomizedLayer(layers=rndmLayers)
            else:
                lyrTmp = self.generateLayerFromYaml(
                    yamlStruct
                )  # not sure what this is, let the other method parse it
                v = yamlStruct[yamlStruct.keys(
                )[0]]  # but we reach in and grab it to pull duration, lampshow and sound.
                duration = value_for_key(v, 'duration', None)

            if (v is not None):
                lampshow = value_for_key(v, 'lampshow')
                sound = value_for_key(v, 'sound')

        except Exception, e:
            current_tag = None
            if (yamlStruct is not None and len(yamlStruct.keys()) > 0):
                current_tag = yamlStruct.keys()[0]
            self.logger.critical(
                "YAML processing failure occured within tag '%s' of yaml section: \n'%s'"
                % (current_tag, yamlStruct))
            raise e
        try:
            values = yaml.load(open(yaml_file, 'r'))
        except yaml.scanner.ScannerError, e:
            self.game.log('Attract mode: Error loading yaml file from %s; the file has a syntax error in it!\nDetails: %s' % (yaml_file, e))
            raise
        except Exception, e:
            self.game.log('Attract mode: Error loading yaml file from %s: %s' % (yaml_file, e))
            raise

        if "Sequence" in values:
            s = values["Sequence"]
            t = 0
            for l in s:
                if ('HighScores' in l):
                    v = l['HighScores']
                    fields = value_for_key(v,'Order')
                    duration =  value_for_key(v,'duration', 2.0)
                    lampshow = value_for_key(v, 'lampshow')
                    for rec in self.game.get_highscore_data():
                        if fields is not None:
                            records = [rec[f] for f in fields]
                        else:
                            records = [rec['category'], rec['player'], rec['score']]
                        lyrTmp = self.game.generateLayer(records, value_for_key(v,'Background'), font_key=value_for_key(v,'Font'))

                        if(lampshow is not None):
                            self.shows.append(lampshow)
                            sl.append(lyrTmp, duration, callback=self.next_show)
                            lampshow = None
                        else:
                            sl.append(lyrTmp, duration)
Exemple #15
0
    def load(self):
        l = logging.getLogger("PIL.PngImagePlugin")
        l.setLevel(logging.WARNING)
        l = logging.getLogger("game.assets")
        l.setLevel(logging.WARNING)
        l = logging.getLogger("game.sound")
        l.setLevel(logging.WARNING)
        l = logging.getLogger("game.dmdcache")
        l.setLevel(logging.WARNING)
        anims = self.value_for_key_path(keypath='Animations',
                                        default={}) or list()
        fonts = self.value_for_key_path(keypath='Fonts', default={}) or list()
        hfonts = value_for_key(fonts, 'HDFonts', {}) or list()
        rfonts = value_for_key(fonts, 'DMDFonts', {}) or list()
        fontstyles = value_for_key(fonts, 'FontStyles', {}) or list()
        lamps = self.value_for_key_path(keypath='LampShows',
                                        default={}) or list()
        rgbshows = self.value_for_key_path(keypath='RGBShows',
                                           default={}) or list()
        sounds = self.value_for_key_path(keypath='Audio', default={}) or list()
        music = value_for_key(sounds, 'Music', {}) or list()
        effects = value_for_key(sounds, 'Effects', {}) or list()
        voice = value_for_key(sounds, 'Voice', {}) or list()

        paths = self.value_for_key_path(keypath='AssetListFiles',
                                        default={}) or list()
        #if there was a list of files to load, then load those
        for path in paths:
            self.loaded_assets_files.append(path)
            self.values = yaml.load(open(path, 'r'))
            anims += self.value_for_key_path(keypath='Animations',
                                             default={}) or list()
            fonts = self.value_for_key_path(keypath='Fonts') or list()
            hfonts += value_for_key(fonts, 'HDFonts', {})
            rfonts += value_for_key(fonts, 'DMDFonts', {}) or list()
            fontstyles += value_for_key(fonts, 'FontStyles', {}) or list()
            lamps += self.value_for_key_path(keypath='LampShows',
                                             default={}) or list()
            rgbshows += self.value_for_key_path(keypath='RGBShows',
                                                default={}) or list()
            sounds = self.value_for_key_path(keypath='Audio',
                                             default={}) or list()
            music += value_for_key(sounds, 'Music', {}) or list()
            effects += value_for_key(sounds, 'Effects', {}) or list()
            voice += value_for_key(sounds, 'Voice', {}) or list()

        # self.total = str(len(anims)+len(hfonts)+len(rfonts)+len(music)+len(effects)+len(voice))
        self.total = (len(lamps) + len(fontstyles) + len(anims) + len(hfonts) +
                      len(rfonts) + len(music) + len(effects) + len(voice))

        try:
            current = ""
            for l in lamps:
                k = value_for_key(l, 'key')
                fname = value_for_key(l, 'file')
                self.updateProgressBar("Lampshows", fname)
                f = self.game.lampshow_path + fname
                current = 'Lampshow: [%s]: %s, %s ' % (k, f, fname)
                self.game.lampctrl.register_show(k, f)

                # Validate the lampshow --as best as possible
                self.game.lampctrl.show.load(f, False, None)
                for tr in self.game.lampctrl.show.lampshow.tracks:
                    if tr.driver == None:  # Check drivers.
                        tr.resolve_driver_with_game(self.game)
                    if tr.driver == None:
                        raise ValueError, "Name '%s' specified in lampshow does not match a driver in the machine yaml." % tr.name

                self.numLoaded += 1

            for l in rgbshows:
                k = value_for_key(l, 'key')
                fname = value_for_key(l, 'file')
                self.updateProgressBar("RGBShows", fname)
                f = self.game.lampshow_path + fname
                current = 'RGBshow: [%s]: %s, %s ' % (k, f, fname)
                self.game.rgbshow_player.load(k, f)
                self.numLoaded += 1

            for f in hfonts:
                k = value_for_key(f, 'key')
                sname = value_for_key(f, 'systemName', k)
                size = value_for_key(f, 'size')
                file_path = value_for_key(f, 'file', None)
                self.updateProgressBar("HD Fonts", sname)
                current = 'HD font: [%s]: %s, %d ' % (k, sname, size)

                if (file_path is not None):
                    file_path = self.game.hdfont_path + file_path
                    if (not os.path.isfile(file_path)):
                        raise ValueError, "Could not load font as specified in yaml\n %s\n File [%s] does not exist." % (
                            current, file_path)

                self.fonts[k] = dmd.hdfont_named(sname,
                                                 size,
                                                 font_file_path=file_path)
                self.numLoaded += 1

            for f in rfonts:
                k = value_for_key(f, 'key')
                fname = value_for_key(f, 'file')
                self.updateProgressBar("DMD Fonts", fname)
                current = 'Font: [%s]: %s ' % (k, fname)
                self.fonts[k] = dmd.font_named(fname)
                self.numLoaded += 1

            for f in fontstyles:
                ic = value_for_key(f, 'interior_color')
                lc = value_for_key(f, 'line_color')
                lw = value_for_key(f, 'line_width')
                k = value_for_key(f, 'key')
                font_style = dmd.HDFontStyle(interior_color=ic,
                                             line_width=lw,
                                             line_color=lc)
                self.fontstyles[k] = font_style

            for anim in anims:
                k = value_for_key(anim, 'key')
                ft = value_for_key(anim, 'frame_time', 2)
                f = value_for_key(anim, 'file')
                r = value_for_key(anim, 'repeatAnim', False)
                h = value_for_key(anim, 'holdLastFrame', False)
                o = value_for_key(anim, 'opaque', False)
                c = value_for_key(anim, 'composite_op')
                x = value_for_key(anim, 'x_loc', 0)
                y = value_for_key(anim, 'y_loc', 0)
                streaming_load = value_for_key(anim, 'streamingMovie', False)
                png_stream_cache = value_for_key(anim, 'streamingPNG_Cached',
                                                 False)
                streaming_png = value_for_key(anim, 'streamingPNG',
                                              png_stream_cache)
                current = 'Animation: [%s]: %s' % (k, f)
                # started = timeit.time.time()
                started = timeit.time.time()
                self.loadIntoCache(k, ft, f, r, h, o, c, x, y, streaming_load,
                                   streaming_png, png_stream_cache)
                time_taken = timeit.time.time() - started
                self.logger.info("loading visual asset took %.3f seconds" %
                                 time_taken)
        except:
            self.logger.error("===ASSET MANAGER - ASSET FAILURE===")
            self.logger.error(current)
            self.logger.error("======")
            raise

        for s in music:
            k = value_for_key(s, 'key')
            fname = value_for_key(s, 'file')
            volume = value_for_key(s, 'volume', .5)
            streaming_load = value_for_key(s, 'streaming_load', True)
            self.updateProgressBar("Audio: Music", fname)
            self.game.sound.register_music(
                k, self.game.music_path + fname,
                volume=volume)  #, streaming_load=streaming_load)
            self.numLoaded += 1

        for s in effects:
            k = value_for_key(s, 'key')
            fname = value_for_key(s, 'file')
            volume = value_for_key(s, 'volume', .5)
            is_voice = value_for_key(s, 'voice', False)
            self.updateProgressBar("Audio SFX", fname)
            self.game.sound.register_sound(k,
                                           self.game.sfx_path + fname,
                                           volume=volume,
                                           is_voice=is_voice)
            self.numLoaded += 1

        for s in voice:
            k = value_for_key(s, 'key')
            fname = value_for_key(s, 'file')
            volume = value_for_key(s, 'volume', .5)
            self.updateProgressBar("Audio Voices", fname)
            self.game.sound.register_sound(k,
                                           self.game.voice_path + fname,
                                           volume=volume,
                                           is_voice=True)
            self.numLoaded += 1
    def generateLayerFromYaml(self, yaml_struct):
        """ a helper to generate Display Layers given properly formatted YAML """
        new_layer = None

        if(yaml_struct is None or (isinstance(yaml_struct,basestring) and yaml_struct=='None')):
            return None

        try:
            if('display' in yaml_struct ):
                yaml_struct = yaml_struct['display']
                return self.generateLayerFromYaml(yaml_struct)

            elif('Combo' in yaml_struct):
                v = yaml_struct['Combo']

                (fnt, font_style) = self.parse_font_data(v, required=False)
                msg = value_for_key(v,'Text')
                if(msg is None):
                    self.logger.warning("Processing YAML, Combo section contains no 'Text' tag.  Consider using Animation instead.")

                new_layer = self.genMsgFrame(msg, value_for_key(v,'Animation'), font_key=fnt, font_style=font_style)
                
            elif ('Animation' in yaml_struct):
                v = yaml_struct['Animation']
                
                new_layer = self.game.animations[value_for_key(v,'Name', value_for_key(v,'Animation'), exception_on_miss=True)]
                new_layer.reset()
                
                if(value_for_key(v,'duration') is None): # no value found, set it so it will be later.
                    v['duration'] = new_layer.duration()

            elif('sequence_layer' in yaml_struct):                
                v = yaml_struct['sequence_layer']
                
                new_layer = dmd.ScriptlessLayer(self.game.dmd.width,self.game.dmd.height)
                repeat = value_for_key(v, 'repeat', True)

                for c in v['contents']:
                    if not 'item' in c:
                        raise ValueError, "malformed YAML file; sequence must contain a list of 'item's"
                    c = c['item']
                    l = self.generateLayerFromYaml(c)
                    if(hasattr(l,'duration') and callable(l.duration)):
                        def_duration = l.duration()
                    else:
                        def_duration = 2.0
                    d = value_for_key(c,'duration',def_duration)
                    new_layer.append(l,d)
                #sl.set_target_position(x, y)
                new_layer.hold = not repeat
            
            elif('panning_layer' in yaml_struct):
                v = yaml_struct['panning_layer']

                w = self.__parse_relative_num(v, 'width', self.game.dmd.width, None)
                h = self.__parse_relative_num(v, 'height', self.game.dmd.height, None)

                origin_x = value_for_key(v,'origin_x',0)
                origin_y = value_for_key(v,'origin_y',0)
                scroll_x = value_for_key(v,'scroll_x', exception_on_miss=True)
                scroll_y = value_for_key(v,'scroll_y', exception_on_miss=True)
                frames_per_movement = value_for_key(v,'frames_per_movement', 1)
                bounce = value_for_key(v,'bounce',False)

                c = self.generateLayerFromYaml(value_for_key(v,'contents', exception_on_miss=True))

                new_layer = dmd.PanningLayer(width=w, height=h, frame=c, origin=(origin_x, origin_y), translate=(scroll_x, scroll_y), numFramesDrawnBetweenMovementUpdate=frames_per_movement, bounce=bounce)
                opaque = value_for_key(v,'opaque',None)
                if opaque:
                    new_layer.opaque = opaque

            elif('group_layer' in yaml_struct):
                v = yaml_struct['group_layer']

                (x,y) = self.__parse_position_data(v)

                w = self.__parse_relative_num(v, 'width', self.game.dmd.width, None)
                h = self.__parse_relative_num(v, 'height', self.game.dmd.height, None)

                contents = value_for_key(v,'contents', exception_on_miss=True)
                opaque = value_for_key(v, 'opaque', None)
                fill_color = value_for_key(v, 'fill_color', None)

                lyrs = []

                duration = 0
                for c in contents:
                    l = self.generateLayerFromYaml(c)
                    lyrs.append(l)
                    if(hasattr(l,'duration') and callable(l.duration)):
                        def_duration = l.duration()
                    else:
                        def_duration = 0.0
                    d = value_for_key(c,'duration',def_duration)
                    duration=max(d,duration)

                new_layer = dmd.GroupedLayer(w, h, lyrs, fill_color=fill_color)
                if(opaque):
                    new_layer.opaque = opaque
                new_layer.set_target_position(x, y)

            elif('animation_layer' in yaml_struct):
                v = yaml_struct['animation_layer']

                (x,y) = self.__parse_position_data(v)

                source_layer = self.game.animations[value_for_key(v,'name',value_for_key(v,'Name'), exception_on_miss=True)]

                opaque = value_for_key(v, 'opaque', source_layer.opaque)
                repeat = value_for_key(v, 'repeat', source_layer.repeat)
                hold_last_frame = value_for_key(v, 'hold_last_frame', source_layer.hold)

                frame_list = value_for_key(v, 'frame_list', {})
                
                if(len(frame_list)==0):
                    new_layer = source_layer
                else:
                    new_layer = AnimatedLayer(frame_time=source_layer.frame_time, frames=[source_layer.frames[idx] for idx in frame_list])

                new_layer.opaque=opaque
                new_layer.repeat = repeat
                new_layer.hold = (hold_last_frame or len(frame_list)==1)

                new_layer.reset()
                new_layer.set_target_position(x, y)

            elif ('text_layer' in yaml_struct):
                v = yaml_struct['text_layer']

                new_layer = self.generateTextLayerFromYaml(v)
                txt = value_for_key(v,'Text', exception_on_miss=True) 

                w = self.__parse_relative_num(v, 'width', self.game.dmd.width, default=None)
                h = self.__parse_relative_num(v, 'height', self.game.dmd.height, default=None)

                blink_frames = value_for_key(v,'blink_frames', None) 

                new_layer.set_text(txt, blink_frames=blink_frames)

                if(w is None):
                    new_layer.width = new_layer.text_width
                if(h is None):
                    new_layer.height = new_layer.text_height

                # fill_color = value_for_key(v,'fill_color',(0,0,0))

            elif ('markup_layer' in yaml_struct):
                v = yaml_struct['markup_layer']

                w = self.__parse_relative_num(v, 'width', self.game.dmd.width, None)
                (bold_font, bold_style) = self.parse_font_data(value_for_key(v, 'Bold', exception_on_miss=True))
                (plain_font, plain_style) = self.parse_font_data(value_for_key(v, 'Normal', exception_on_miss=True))
                txt = value_for_key(v, "Message", exception_on_miss=True)
                if(isinstance(txt,list)):
                    txt = "\n".join(txt)

                gen = dmd.MarkupFrameGenerator(game=self.game, font_plain=plain_font, font_bold=bold_font, width=w)
                gen.set_bold_font(bold_font, interior_color=bold_style.interior_color, border_width=bold_style.line_width, border_color=bold_style.line_color)
                gen.set_plain_font(plain_font, interior_color=plain_style.interior_color, border_width=plain_style.line_width, border_color=plain_style.line_color)

                frm = gen.frame_for_markup(txt)
                new_layer = dmd.FrameLayer(frame=frm)

            else:
                unknown_tag = None
                if(yaml_struct is not None and len(yaml_struct.keys())>0):
                    unknown_tag = yaml_struct.keys()[0]
                raise ValueError, "Unknown tag '%s' in yaml section.  Check spelling/caps/etc." % (unknown_tag)

        except Exception, e:
            current_tag = None
            if(yaml_struct is not None and len(yaml_struct.keys())>0):
                current_tag = yaml_struct.keys()[0]
            self.logger.critical("YAML processing failure occured within tag '%s' of yaml section: \n'%s'" % (current_tag,yaml_struct))
            raise e
    def genLayerFromYAML(self, yamlStruct):
        """ a helper that parses the 'attract sequence' format -- an augmented version
            of the yaml serialized layer format, however also includes coordination 
            of lampshows and sound playback """

        duration = None
        lampshow = None
        sound = None
        lyrTmp = None
        v = None
        try:
            if ('HighScores' in yamlStruct):
                v = yamlStruct['HighScores']

                duration =  value_for_key(v,'duration', 2.0)
                lampshow = value_for_key(v, 'lampshow')
                sound = value_for_key(v, 'sound')

                fields = value_for_key(v,'Order')
                (fnt, font_style) = self.parse_font_data(v, required=False)

                background = value_for_key(v,'Background', value_for_key(v,'Animation'))

                # lyrTmp = dmd.ScriptlessLayer(self.game.dmd.width,self.game.dmd.height)
                # entry_ct = len(self.game.get_highscore_data())
                # for rec in self.game.get_highscore_data():
                #     if fields is not None:
                #         records = [rec[f] for f in fields]
                #     else:
                #         records = [rec['category'], rec['player'], rec['score']]
                #     lT = self.genMsgFrame(records, background, font_key=fnt, font_style=font_style)

                #     lyrTmp.append(lT, duration)

                # duration = entry_ct*duration
                lyrTmp = dmd.ScoresLayer(self.game, fields, fnt, font_style, background, duration)
                duration = lyrTmp.regenerate()

            elif('LastScores' in yamlStruct):
                v = yamlStruct['LastScores']

                duration =  value_for_key(v,'duration', 2.0)
                lampshow = value_for_key(v, 'lampshow')
                sound = value_for_key(v, 'sound')

                (fnt, font_style) = self.parse_font_data(v, required=False)

                last_score_count = len(self.game.old_players)

                if(last_score_count==0):
                    return None

                background = value_for_key(v,'Background', value_for_key(v,'Animation'))

                multiple_screens = value_for_key(v, 'multiple_screens', False)

                if(multiple_screens):
                    lyrTmp = dmd.ScriptlessLayer(self.game.dmd.width,self.game.dmd.height)
                    lyrTmp.append(self.genMsgFrame(["Last Game","Final Scores"], background, font_key=fnt, font_style=font_style), duration)

                    for player in self.game.old_players:
                        lT = self.genMsgFrame([player.name, self.game.score_display.format_score(player.score)], background,  font_key=fnt, font_style=font_style)
                        lyrTmp.append(lT, duration)

                    duration = (last_score_count+1)*duration
                else:
                    lyrTmp = dmd.GroupedLayer(self.game.dmd.width,self.game.dmd.height)
                    lyrTmp.opaque = True

                    if background != None:
                        lyrTmp.layers += [self.game.animations[background]]
                    spacing = self.game.dmd.height/7   #was 7 before steamwreck
                    offset = spacing
                    title = dmd.HDTextLayer(self.game.dmd.width/2, offset, self.game.fonts[fnt], 'center', fontstyle=font_style).set_text('FINAL RESULTS LAST GAME')
                    title.opaque = False
                    lyrTmp.layers += [title]
                    #print 'now go get scores for each player in last game'
                    for p in self.game.old_players:
                        offset += spacing
                        layer = dmd.HDTextLayer(self.game.dmd.width/2, offset, self.game.fonts[fnt], 'center', opaque = False)
                        layer.style = font_style
                        #print 'created the layer, now set text'
                        layer.set_text("{0:<18}    {1:>18}".format(p.name, self.game.score_display.format_score(p.score)))
                        lyrTmp.layers += [layer]

            elif('RandomText' in yamlStruct):
                v = yamlStruct['RandomText']
                (fnt, font_style) = self.parse_font_data(v, required=False)
                randomText = value_for_key(v,'TextOptions', exception_on_miss=True)
                headerText = value_for_key(v,'Header', None)
                duration = value_for_key(v,'duration')

                rndmLayers = []
                for line in randomText:
                    selectedRandomText = line['Text']
                    if(type(selectedRandomText) is list):
                        completeText = selectedRandomText
                    else:
                        completeText = [selectedRandomText]

                    if (headerText is not None):
                        completeText[:0] = [headerText] # prepend the header text entry at the start of the list

                    rndmLayers.append(self.genMsgFrame(completeText, value_for_key(v,'Animation'), font_key=fnt, font_style=font_style))

                if(len(rndmLayers) > 0):
                    lyrTmp = RandomizedLayer(layers=rndmLayers)            
            else:
                lyrTmp = self.generateLayerFromYaml(yamlStruct) # not sure what this is, let the other method parse it
                v = yamlStruct[yamlStruct.keys()[0]]    # but we reach in and grab it to pull duration, lampshow and sound.
                duration = value_for_key(v,'duration',None)

            if(v is not None):
                lampshow = value_for_key(v, 'lampshow')
                sound = value_for_key(v, 'sound')

        except Exception, e:
            current_tag = None
            if(yamlStruct is not None and len(yamlStruct.keys())>0):
                current_tag = yamlStruct.keys()[0]
            self.logger.critical("YAML processing failure occured within tag '%s' of yaml section: \n'%s'" % (current_tag,yamlStruct))
            raise e