Exemple #1
0
 def save_pug_settings(self):
     if self.scene:
         self.pug_settings.initial_scene = self.sceneclass.__name__
     if wx.GetApp():
         wx.GetApp().create_frame_settings( self.pug_settings)
     try:
         pug.code_exporter( self.pug_settings, "_pug_settings.py", True, 
                  {'name':'pug_settings'})              
     except:
         if wx.GetApp():
             show_exception_dialog()
         else:
             raise
Exemple #2
0
 def save_pug_settings(self):
     if self.scene:
         self.pug_settings.initial_scene = self.sceneclass.__name__
     if wx.GetApp():
         wx.GetApp().create_frame_settings(self.pug_settings)
     try:
         pug.code_exporter(self.pug_settings, "_pug_settings.py", True,
                           {'name': 'pug_settings'})
     except:
         if wx.GetApp():
             show_exception_dialog()
         else:
             raise
Exemple #3
0
 def copy_selected(self):
     self.clipboard = {}
     selectedDict = list(wx.GetApp().selectedObjectDict)
     for item in selectedDict:
         if isinstance(item, Opioid2D.public.Node.Node):
             exporter = pug.code_exporter(item)
             code = exporter.code
             obj = exporter.objCode.popitem()[0]
             self.clipboard[obj] = code
             exporter = None
Exemple #4
0
 def copy_selected(self):
     self.clipboard = {}
     selectedDict = list(wx.GetApp().selectedObjectDict)
     for item in selectedDict:
         if isinstance(item, Opioid2D.public.Node.Node):
             exporter = pug.code_exporter(item)
             code = exporter.code
             obj = exporter.objCode.popitem()[0]
             self.clipboard[obj] = code
             exporter = None
Exemple #5
0
def save_object(obj, name=None, parentWindow=None):
    """save_object(obj): Export obj as a class to objects folder

name: the name to save the object as. If not provided, a dialog will be opened
parentWindow: the parent window of name dialog. If not provided, the 
    wx.ActiveWindow will be used
"""
    if not isinstance(obj, Node):
        raise TypeError('save_object() arg 1 must be a Node')
    if not name:
        if obj.archetype:
            name = obj.gname
        else:
            if obj.gname:
                name = obj.gname
            else:
                name = "MyClass"
        if not name:
            name = obj.__class__.__name__
        if parentWindow == None:
            parentWindow = wx.GetActiveWindow()
        objName = ''
        # we generally don't want to save with the same name as 
        # a base class of the same object
        superclasses = getmro(obj.__class__)[1:]
        for cls in superclasses:
            if name == cls.__name__ or name=='Sprite':
                name = ''.join(['My',name])
                break
        default = make_valid_attr_name(name)
        dlg = wx.TextEntryDialog( parentWindow, 
                                  "Enter the object's class/file name", 
                                  "Save Object", default)
        while not objName:
            if dlg.ShowModal() == wx.ID_OK:
                name = dlg.GetValue()
                errorDlg = None
                if name == 'Sprite':
                    errorDlg = wx.MessageDialog( dlg, 
                           "You can't use the names 'Sprite'",
                           "Reserved Name",
                           wx.OK)
                elif name != make_valid_attr_name(name):
                    errorDlg = wx.MessageDialog( dlg, 
                           "Name cannot contain spaces or special characters"+\
                           "\nand cannot start with a number",
                           "Invalid Name",
                           wx.OK)
                if errorDlg:   
                    errorDlg.ShowModal()
                    errorDlg.Destroy() 
                    dlg.SetValue(default)
                    dlg.SetFocus()      
                    continue                
                path = os.path.join('objects',''.join([name,'.py']))
                old_module = obj.__class__.__module__.split('.')
                if old_module[-2:-1][0] == 'objects' and \
                        old_module[-1:][0] != name:
                    # verify overwrite
                    try:
                        test = file(path)
                    except:
                        objName = name
                    else:
                        test.close()
                        confirmDlg = wx.MessageDialog( dlg, 
                                "\n".join([path,
                               "File already exists. Overwrite?"]),
                               "Confirm Replace",
                               wx.YES_NO | wx.NO_DEFAULT)
                        if confirmDlg.ShowModal() == wx.ID_YES:
                            if sys.platform == "win32":
                                files = os.listdir('objects')
                                testname = name + '.py'
                                for f in files:
                                    if f.lower() == testname.lower():
                                        sceneName = os.path.splitext(f)[0]
                                        break
                            else:
                                objName = name
                        confirmDlg.Destroy()
                else:
                    objName = name
            else:
                dlg.Destroy()
                return
        dlg.Destroy()
    else:
        name = make_valid_attr_name(name)
        objName = name
        path = os.path.join('objects',''.join([name,'.py']))
    try:
        if getattr(obj, 'archetype', False):
            # we don't want every instance to be an archetype
            obj.archetype = False
            archetype = True
        else:
            archetype = False    
        from pig.Sprite import Sprite
        exporter = code_exporter( obj, path, True, {'name':objName,
                                                  'base_class':Sprite})
        objDict = get_available_objects( True)
        oldclass = obj.__class__
        if oldclass != objDict[objName]:
            obj.__class__ = objDict[objName]            
        if archetype:
            # return archetype status after saving
            obj.archetype = True
            obj.gname = objName
            if exporter.file_changed:
                archetype_changed( obj, oldclass, exporter)
        return exporter
    except:
        show_exception_dialog()
Exemple #6
0
def save_scene_as( sceneName=None, fileName=None):#, parentWindow=None):
    """save_scene_as( sceneName=None, fileName=None)->Filename or False if fail 
    Save the current scene as a class in the scenes folder
        
sceneName: string with name to save as. If None, a dialog will be opened.
parentWindow: the parent window of name dialog. If not provided, the 
    wx.ActiveWindow will be used
"""
    wx.GetApp().apply()
    if get_scene_errors():
        return False
    if _DEBUG: print "util: save_scene_as"
    scene = PigDirector.scene
    if not sceneName:
        name = scene.__class__.__name__
        if name == 'Scene':
            name = 'MyScene'
        name = make_valid_attr_name(name)
        name.capitalize()
        if _DEBUG: print "util: save_scene_as 1"
        parentWindow=None
        if parentWindow == None:
            parentWindow = wx.GetActiveWindow()
        default = make_valid_attr_name(name)
        dlg = wx.TextEntryDialog( parentWindow, 
                                  "Enter the scene's class/file name", 
                                  "Save Scene", default)
        if _DEBUG: print "util: save_scene_as 2"
        while not sceneName:
            if dlg.ShowModal() == wx.ID_OK:
                name = dlg.GetValue()
                errorDlg = None
                if name == 'Scene':
                    errorDlg = wx.MessageDialog( dlg, 
                           "You can't use the name 'Scene'",
                           "Reserved Name",
                           wx.OK)
                elif name != make_valid_attr_name(name):
                    errorDlg = wx.MessageDialog( dlg, 
                           "Name cannot contain spaces or special characters"+\
                           "\nand cannot start with a number",
                           "Invalid Name",
                           wx.OK)
                if errorDlg:    
                    errorDlg.ShowModal()
                    errorDlg.Destroy() 
                    dlg.SetValue(default)
                    dlg.SetFocus()      
                    continue
                path = os.path.join('scenes', name + '.py')
                if name != scene.__class__.__name__:
                    try:
                        test = file(path)
                    except:
                        sceneName = name
                    else:
                        test.close()
                        confirmDlg = wx.MessageDialog( dlg, 
                               "Scene file already exists. Overwrite?",
                               "Confirm Replace",
                               wx.YES_NO | wx.NO_DEFAULT)
                        if confirmDlg.ShowModal() == wx.ID_YES:
                            if sys.platform == "win32":
                                files = os.listdir('scenes')
                                testname = name + '.py'
                                for f in files:
                                    if f.lower() == testname.lower():
                                        sceneName = os.path.splitext(f)[0]
                                        break
                            else:
                                sceneName = name
                        confirmDlg.Destroy()   
                else:    
                    sceneName = name             
            else:
                dlg.Destroy()
                return False
        dlg.Destroy()
    else:
        if sceneName == 'Scene':
            raise ValueError("Can't save over baseclass 'Scene'")
#            save_scene_as( sceneName, fileName)
    if not fileName:
        fileName = ''.join([sceneName, '.py'])
    path = os.path.join('scenes',fileName)
    app = wx.GetApp()
    if _DEBUG: print "util: save_scene_as 4"
    selection = app.selectedObjectDict.keys()
    oldscene = PigDirector.scene
    from pig.editor.util import wait_for_state
    wait_for_state( None)
    if _DEBUG: print "util: save_scene_as 5"
    wx.GetApp().set_busy_state(True)
    saved = False
    try:
        if _DEBUG: print "util: save_scene_as enter code_exporter"
        code_exporter( scene, path, True, {'name':sceneName})
        if _DEBUG: print "util: save_scene_as exit code_exporter"
    except:
        if _DEBUG: print "util: save_scene_as 6"        
        show_exception_dialog()
    else:
        if _DEBUG: print "util: save_scene_as 7"        
        sceneDict = get_available_scenes(True)
        if '__Working__' not in path:
            PigDirector.scene.__class__ = sceneDict[sceneName]
        saved = True
        if _DEBUG: print "util: save_scene_as 8"        
    finally:
        if _DEBUG: print "util: save_scene_as 9"                
        wx.GetApp().set_busy_state(False)        
        if PigDirector.scene != oldscene:
            wx.GetApp().set_selection([])
            if _DEBUG: print "util: save_scene_as reset select:", selection
        from pig.editor.EditorState import EditorState
        wait_for_state(EditorState)
        if _DEBUG: print "util: save_scene_as 10"                
        wx.GetApp().refresh()
    if _DEBUG: print "util: save_scene_as 11"                
    if saved:
        return fileName
    else:
        return False
Exemple #7
0
def save_object(obj, name=None, parentWindow=None):
    """save_object(obj): Export obj as a class to objects folder

name: the name to save the object as. If not provided, a dialog will be opened
parentWindow: the parent window of name dialog. If not provided, the 
    wx.ActiveWindow will be used
"""
    if not isinstance(obj, Node):
        raise TypeError('save_object() arg 1 must be a Node')
    if not name:
        if obj.archetype:
            name = obj.gname
        else:
            if obj.gname:
                name = obj.gname
            else:
                name = "MyClass"
        if not name:
            name = obj.__class__.__name__
        if parentWindow == None:
            parentWindow = wx.GetActiveWindow()
        objName = ''
        # we generally don't want to save with the same name as
        # a base class of the same object
        superclasses = getmro(obj.__class__)[1:]
        for cls in superclasses:
            if name == cls.__name__ or name == 'Sprite':
                name = ''.join(['My', name])
                break
        default = make_valid_attr_name(name)
        dlg = wx.TextEntryDialog(parentWindow,
                                 "Enter the object's class/file name",
                                 "Save Object", default)
        while not objName:
            if dlg.ShowModal() == wx.ID_OK:
                name = dlg.GetValue()
                errorDlg = None
                if name == 'Sprite':
                    errorDlg = wx.MessageDialog(
                        dlg, "You can't use the names 'Sprite'",
                        "Reserved Name", wx.OK)
                elif name != make_valid_attr_name(name):
                    errorDlg = wx.MessageDialog( dlg,
                           "Name cannot contain spaces or special characters"+\
                           "\nand cannot start with a number",
                           "Invalid Name",
                           wx.OK)
                if errorDlg:
                    errorDlg.ShowModal()
                    errorDlg.Destroy()
                    dlg.SetValue(default)
                    dlg.SetFocus()
                    continue
                path = os.path.join('objects', ''.join([name, '.py']))
                old_module = obj.__class__.__module__.split('.')
                if old_module[-2:-1][0] == 'objects' and \
                        old_module[-1:][0] != name:
                    # verify overwrite
                    try:
                        test = file(path)
                    except:
                        objName = name
                    else:
                        test.close()
                        confirmDlg = wx.MessageDialog(
                            dlg, "\n".join(
                                [path, "File already exists. Overwrite?"]),
                            "Confirm Replace", wx.YES_NO | wx.NO_DEFAULT)
                        if confirmDlg.ShowModal() == wx.ID_YES:
                            if sys.platform == "win32":
                                files = os.listdir('objects')
                                testname = name + '.py'
                                for f in files:
                                    if f.lower() == testname.lower():
                                        sceneName = os.path.splitext(f)[0]
                                        break
                            else:
                                objName = name
                        confirmDlg.Destroy()
                else:
                    objName = name
            else:
                dlg.Destroy()
                return
        dlg.Destroy()
    else:
        name = make_valid_attr_name(name)
        objName = name
        path = os.path.join('objects', ''.join([name, '.py']))
    try:
        if getattr(obj, 'archetype', False):
            # we don't want every instance to be an archetype
            obj.archetype = False
            archetype = True
        else:
            archetype = False
        from pig.Sprite import Sprite
        exporter = code_exporter(obj, path, True, {
            'name': objName,
            'base_class': Sprite
        })
        objDict = get_available_objects(True)
        oldclass = obj.__class__
        if oldclass != objDict[objName]:
            obj.__class__ = objDict[objName]
        if archetype:
            # return archetype status after saving
            obj.archetype = True
            obj.gname = objName
            if exporter.file_changed:
                archetype_changed(obj, oldclass, exporter)
        return exporter
    except:
        show_exception_dialog()
Exemple #8
0
def save_scene_as(sceneName=None, fileName=None):  #, parentWindow=None):
    """save_scene_as( sceneName=None, fileName=None)->Filename or False if fail 
    Save the current scene as a class in the scenes folder
        
sceneName: string with name to save as. If None, a dialog will be opened.
parentWindow: the parent window of name dialog. If not provided, the 
    wx.ActiveWindow will be used
"""
    wx.GetApp().apply()
    if get_scene_errors():
        return False
    if _DEBUG: print "util: save_scene_as"
    scene = PigDirector.scene
    if not sceneName:
        name = scene.__class__.__name__
        if name == 'Scene':
            name = 'MyScene'
        name = make_valid_attr_name(name)
        name.capitalize()
        if _DEBUG: print "util: save_scene_as 1"
        parentWindow = None
        if parentWindow == None:
            parentWindow = wx.GetActiveWindow()
        default = make_valid_attr_name(name)
        dlg = wx.TextEntryDialog(parentWindow,
                                 "Enter the scene's class/file name",
                                 "Save Scene", default)
        if _DEBUG: print "util: save_scene_as 2"
        while not sceneName:
            if dlg.ShowModal() == wx.ID_OK:
                name = dlg.GetValue()
                errorDlg = None
                if name == 'Scene':
                    errorDlg = wx.MessageDialog(
                        dlg, "You can't use the name 'Scene'", "Reserved Name",
                        wx.OK)
                elif name != make_valid_attr_name(name):
                    errorDlg = wx.MessageDialog( dlg,
                           "Name cannot contain spaces or special characters"+\
                           "\nand cannot start with a number",
                           "Invalid Name",
                           wx.OK)
                if errorDlg:
                    errorDlg.ShowModal()
                    errorDlg.Destroy()
                    dlg.SetValue(default)
                    dlg.SetFocus()
                    continue
                path = os.path.join('scenes', name + '.py')
                if name != scene.__class__.__name__:
                    try:
                        test = file(path)
                    except:
                        sceneName = name
                    else:
                        test.close()
                        confirmDlg = wx.MessageDialog(
                            dlg, "Scene file already exists. Overwrite?",
                            "Confirm Replace", wx.YES_NO | wx.NO_DEFAULT)
                        if confirmDlg.ShowModal() == wx.ID_YES:
                            if sys.platform == "win32":
                                files = os.listdir('scenes')
                                testname = name + '.py'
                                for f in files:
                                    if f.lower() == testname.lower():
                                        sceneName = os.path.splitext(f)[0]
                                        break
                            else:
                                sceneName = name
                        confirmDlg.Destroy()
                else:
                    sceneName = name
            else:
                dlg.Destroy()
                return False
        dlg.Destroy()
    else:
        if sceneName == 'Scene':
            raise ValueError("Can't save over baseclass 'Scene'")


#            save_scene_as( sceneName, fileName)
    if not fileName:
        fileName = ''.join([sceneName, '.py'])
    path = os.path.join('scenes', fileName)
    app = wx.GetApp()
    if _DEBUG: print "util: save_scene_as 4"
    selection = app.selectedObjectDict.keys()
    oldscene = PigDirector.scene
    from pig.editor.util import wait_for_state
    wait_for_state(None)
    if _DEBUG: print "util: save_scene_as 5"
    wx.GetApp().set_busy_state(True)
    saved = False
    try:
        if _DEBUG: print "util: save_scene_as enter code_exporter"
        code_exporter(scene, path, True, {'name': sceneName})
        if _DEBUG: print "util: save_scene_as exit code_exporter"
    except:
        if _DEBUG: print "util: save_scene_as 6"
        show_exception_dialog()
    else:
        if _DEBUG: print "util: save_scene_as 7"
        sceneDict = get_available_scenes(True)
        if '__Working__' not in path:
            PigDirector.scene.__class__ = sceneDict[sceneName]
        saved = True
        if _DEBUG: print "util: save_scene_as 8"
    finally:
        if _DEBUG: print "util: save_scene_as 9"
        wx.GetApp().set_busy_state(False)
        if PigDirector.scene != oldscene:
            wx.GetApp().set_selection([])
            if _DEBUG: print "util: save_scene_as reset select:", selection
        from pig.editor.EditorState import EditorState
        wait_for_state(EditorState)
        if _DEBUG: print "util: save_scene_as 10"
        wx.GetApp().refresh()
    if _DEBUG: print "util: save_scene_as 11"
    if saved:
        return fileName
    else:
        return False