示例#1
0
class SogalDialog(SogalForm):
    '''
    A dialog that derived from SogalForm
    (Which contains a DirectDialog)
    '''
    def __init__(
            self,
            enableMask=True,  #NOTE THAT IT IS TRUE BY DEFAULT
            autoDestroy=True,
            sortType=0,  #0 for horizontal, 1 for vertical
            margin=0.2,
            textList=['OK', 'Cancel'],
            enablesList=None,
            command=None,
            frameSize=(-0.6, 0.6, -0.45, 0.45),
            buttonSize=BUTTON_SIZE,
            text='',
            textPos=(0, 0.2),
            startPos=(-0.4, 0, -0.2),
            extraArgs=[],
            style=None,
            fadeScreen=0.5,
            backgroundColor=None,
            **kwargs):
        if backgroundColor:
            bgColor = backgroundColor
        elif fadeScreen is not None:
            bgColor = (0, 0, 0, fadeScreen)
        else:
            bgColor = None

        SogalForm.__init__(self,
                           enableMask=enableMask,
                           backgroundColor=bgColor,
                           **kwargs)
        if enableMask:
            self.reparentTo(aspect2d, sort=1000)
        if not style:
            self.__style = base.getStyle()
        else:
            self.__style = color_themes.styles[style]

        self.__frame = DirectFrame(parent=self,
                                   frameSize=frameSize,
                                   **self.__style['hardframe'])
        self.__buttonList = []  #DirectButton(parent = self, s)
        self.__text = OnscreenText(parent=self,
                                   text=text,
                                   pos=textPos,
                                   **self.__style['text'])
        self.__command = command
        self.__autoDestroy = autoDestroy
        self._extraArgs = extraArgs

        if sortType == 0:
            self.__box = HLayout(margin=margin)
        else:
            self.__box = VLayout(margin=margin)
        self.__box.reparentTo(self)
        self.__box.setPos(startPos)

        for i in range(len(textList)):
            btn = DirectButton(text=textList[i],
                               command=self.buttonCommand(i),
                               frameSize=buttonSize,
                               **self.__style['button'])
            self.__buttonList.append(btn)
            self.__box.append(btn)

        if enablesList:
            for i in range(len(enablesList)):
                if i >= len(self.__buttonList):
                    break
                if enablesList[i]:
                    self.__buttonList[i]['state'] = DGG.NORMAL
                else:
                    self.__buttonList[i]['state'] = DGG.DISABLED

        self.show()

    def buttonCommand(self, i):
        commandindex = i

        def process():
            if self.__command:
                self.__command(commandindex, *self._extraArgs)
            if self.__autoDestroy:
                self.close()

        return process

    def close(self):
        for btn in self.__buttonList:
            btn['state'] = DGG.DISABLED
        if self._getFading():
            seq = Sequence(
                Func(self.hide),
                Wait(self._getFadingDuration() + 2),
                Func(self.destroy),
            )
            seq.start()
        else:
            self.destroy()

    def destroy(self):
        for btn in self.__buttonList:
            btn.destroy()
        self.__frame.destroy()
        self.__box.removeNode()
        SogalForm.destroy(self)
示例#2
0
class MainMenu(SogalForm):
    '''
    main menu class
    '''



    
    
    def __init__(self, entry = 'ifselectionjumptest'):
        '''
        Constructor
        '''
        self.entry = entry
        self.closed = True
        
        SogalForm.__init__(self, fading = True, fading_duration = 1.0, backgroundImage = None, backgroundColor = (0,0,0,1), 
                           enableMask = True,
                           hiddenFunc = self.closedFunc, shownFunc = self.openedFunc)
        

        self.addButtonBar()
        
        self.fadinglock = Lock()        #This lock is to prevent open or close failure when the main menu is on fading

        
    def addButtonBar(self):
        self.bar = VLayout(margin= 0.1)
        self.bar.reparentTo(self)        
        


    
    
    def addButtons(self):
        '''
        override this if you want custom buttons
        '''
        #TODO:Continue Button 注意继续的位置可能是从硬盘上读入的也可能是从游戏中返回标题画面的 也有可能是在游戏正常结束之后所以没有Continue数据
        self.addButton(text = 'New Game', state = DGG.NORMAL, command = self._startGame)
        self.addButton(text = 'Load', state = DGG.NORMAL, command = self._load)
        self.addButton(text = 'Options', state = DGG.NORMAL, command = self._config)
        #TODO:Gallery Button
        self.addButton(text = 'Exit', state = DGG.NORMAL, command = self._exit)       

            
    def close(self):
        '''Called by SogalBase. Do something and hide, you will need it if you want a more complex main menu'''
        self.fadinglock.acquire()
        
        if self.closed:
            return
        SogalForm.hide(self)
        self.closed = True
        if self.bar:
            for btn in self.bar:
                btn['state'] = DGG.DISABLED
        
    def open(self):
        '''Called by SogalBase. Do something and show, you will need it if you want a more complex main menu'''
        self.fadinglock.acquire()
        
        if not self.closed:
            return
        SogalForm.show(self)
        self.addButtons()
        self.closed = False
    
    def show(self):
        #redirect show/hide to open/close
        self.open()
    
    def hide(self):
        #redirect show/hide to open/close
        self.close()
        
    def closedFunc(self):
        self.cleanup()
        self.fadinglock.release()
    
    def cleanup(self):
        for btn in self.bar:
            btn.destroy()
        self.bar.removeNode()
        self.addButtonBar()
    
    def openedFunc(self):
        self.fadinglock.release()
        
    def addButton(self,**args):
        '''Add a button and return it'''
        btn = DirectButton(**dict(base.getStyle()['mainMenuButton'], frameSize = BUTTON_SIZE,**args))  # @UndefinedVariable
        
        self.bar.append(btn)
        #self.vbox.pack(btn)
        return btn
        
    def _startGame(self, scene = None):
        if self.closed:
            return
        if not scene:
            messenger.send('start_game', [self.entry])
        else:
            messenger.send('start_game', [scene])
            
    def _load(self):
        if self.closed:
            return
        messenger.send('load_game')
        
    def _exit(self):
        if self.closed:
            return
        messenger.send('exit_game')
        
    def _config(self):
        messenger.send('config_form')
示例#3
0
class MainMenu(SogalForm):
    '''
    main menu class
    '''
    def __init__(self, entry='ifselectionjumptest'):
        '''
        Constructor
        '''
        self.entry = entry
        self.closed = True

        SogalForm.__init__(self,
                           fading=True,
                           fading_duration=1.0,
                           backgroundImage=None,
                           backgroundColor=(0, 0, 0, 1),
                           enableMask=True,
                           hiddenFunc=self.closedFunc,
                           shownFunc=self.openedFunc)

        self.addButtonBar()

        self.fadinglock = Lock(
        )  #This lock is to prevent open or close failure when the main menu is on fading

    def addButtonBar(self):
        self.bar = VLayout(margin=0.1)
        self.bar.reparentTo(self)

    def addButtons(self):
        '''
        override this if you want custom buttons
        '''
        #TODO:Continue Button 注意继续的位置可能是从硬盘上读入的也可能是从游戏中返回标题画面的 也有可能是在游戏正常结束之后所以没有Continue数据
        self.addButton(text='New Game',
                       state=DGG.NORMAL,
                       command=self._startGame)
        self.addButton(text='Load', state=DGG.NORMAL, command=self._load)
        self.addButton(text='Options', state=DGG.NORMAL, command=self._config)
        #TODO:Gallery Button
        self.addButton(text='Exit', state=DGG.NORMAL, command=self._exit)

    def close(self):
        '''Called by SogalBase. Do something and hide, you will need it if you want a more complex main menu'''
        self.fadinglock.acquire()

        if self.closed:
            return
        SogalForm.hide(self)
        self.closed = True
        if self.bar:
            for btn in self.bar:
                btn['state'] = DGG.DISABLED

    def open(self):
        '''Called by SogalBase. Do something and show, you will need it if you want a more complex main menu'''
        self.fadinglock.acquire()

        if not self.closed:
            return
        SogalForm.show(self)
        self.addButtons()
        self.closed = False

    def show(self):
        #redirect show/hide to open/close
        self.open()

    def hide(self):
        #redirect show/hide to open/close
        self.close()

    def closedFunc(self):
        self.cleanup()
        self.fadinglock.release()

    def cleanup(self):
        for btn in self.bar:
            btn.destroy()
        self.bar.removeNode()
        self.addButtonBar()

    def openedFunc(self):
        self.fadinglock.release()

    def addButton(self, **args):
        '''Add a button and return it'''
        btn = DirectButton(**dict(base.getStyle()['mainMenuButton'],
                                  frameSize=BUTTON_SIZE,
                                  **args))  # @UndefinedVariable

        self.bar.append(btn)
        #self.vbox.pack(btn)
        return btn

    def _startGame(self, scene=None):
        if self.closed:
            return
        if not scene:
            messenger.send('start_game', [self.entry])
        else:
            messenger.send('start_game', [scene])

    def _load(self):
        if self.closed:
            return
        messenger.send('load_game')

    def _exit(self):
        if self.closed:
            return
        messenger.send('exit_game')

    def _config(self):
        messenger.send('config_form')
示例#4
0
class SogalDialog(SogalForm):
    '''
    A dialog that derived from SogalForm
    (Which contains a DirectDialog)
    '''
    def __init__(self,
                 enableMask = True, #NOTE THAT IT IS TRUE BY DEFAULT
                 autoDestroy = True,
                 sortType = 0, #0 for horizontal, 1 for vertical
                 margin = 0.2,
                 textList = ['OK','Cancel'],
                 enablesList = None,
                 command = None,
                 frameSize = (-0.6,0.6,-0.45,0.45),
                 buttonSize = BUTTON_SIZE,
                 text = '',
                 textPos = (0,0.2),
                 startPos = (-0.4,0,-0.2),
                 extraArgs = [],
                 style = None,
                 fadeScreen = 0.5,
                 backgroundColor = None,
                 **kwargs):
        if backgroundColor:
            bgColor = backgroundColor
        elif fadeScreen is not None:
            bgColor = (0,0,0,fadeScreen)
        else:
            bgColor = None
        
        SogalForm.__init__(self,enableMask = enableMask,backgroundColor=bgColor, **kwargs)
        if enableMask:
            self.reparentTo(aspect2d, sort = 1000)
        if not style:
            self.__style = base.getStyle()
        else:
            self.__style = color_themes.styles[style]
        
        self.__frame = DirectFrame(parent = self,frameSize = frameSize,**self.__style['hardframe'])
        self.__buttonList = []#DirectButton(parent = self, s)
        self.__text = OnscreenText(parent = self,text = text ,pos = textPos, **self.__style['text'])
        self.__command = command
        self.__autoDestroy = autoDestroy
        self._extraArgs = extraArgs
        
        if sortType == 0:
            self.__box = HLayout(margin = margin)
        else: self.__box = VLayout(margin = margin)
        self.__box.reparentTo(self)
        self.__box.setPos(startPos)
        
        for i in range(len(textList)):
            btn = DirectButton(text = textList[i], command = self.buttonCommand(i),frameSize = buttonSize, **self.__style['button'])
            self.__buttonList.append(btn)
            self.__box.append(btn)
            
        if enablesList:
            for i in range(len(enablesList)):
                if i >= len(self.__buttonList):
                    break
                if enablesList[i]:
                    self.__buttonList[i]['state'] = DGG.NORMAL
                else: self.__buttonList[i]['state'] = DGG.DISABLED
                
        self.show()
        
    def buttonCommand(self, i):
        commandindex = i
        def process():
            if self.__command:
                self.__command(commandindex, *self._extraArgs)
            if self.__autoDestroy:
                self.close()
        return process

    def close(self):
        for btn in self.__buttonList:
            btn['state'] = DGG.DISABLED
        if self._getFading():
            seq = Sequence(Func(self.hide),
                           Wait(self._getFadingDuration()+2),
                           Func(self.destroy),
                           )
            seq.start()
        else: self.destroy()        
    
            
    def destroy(self):
        for btn in self.__buttonList:
            btn.destroy()
        self.__frame.destroy()
        self.__box.removeNode()
        SogalForm.destroy(self)