def __init__(self, parent=None): if Weld.__instance is None: Weld.__instance = self else: raise Exception('can\'t declare new Weld instance. Weld is a singleton.') Savable.__init__(self, savepath=os.path.join(user.home, '.weld', 'weld.cache')) self.savelist += ['current_project_path'] # this is used to reload last opened project self.current_project_path = None Ui(self) # setup internal resource handlers self.project = None # list of resources we can ask steel to load self.resMan = ResourceManager(os.path.join(Config.instance().weld_data_path, 'resources')) self.resMan.attach_to_Ui(Ui.instance().res_browser['library']) # entity responsible for behavior trees manipulations self.BTMan = BTManager() # ready Ui.instance().show_status('ready', 1000) self.load() if Config.instance().on_weld_start_reopen_last_project: if self.current_project_path is not None: print 'auto reopening project \'%s\'.' % self.current_project_path p, f = os.path.split(self.current_project_path) self.open_project(self.current_project_path, f) else: print 'no project to reopen.' else: print 'skipping project reopening.'
def save_project(self): if self.project is None: Ui.instance().show_status('no project to save.') else: print 'Weld.save_project()' self.project.save() Ui.instance().show_status('project saved.')
def save(self): """ Retrieve some data before saving them. """ self.camera_position = self.qsteelwidget.cameraPosition() self.camera_rotation = self.qsteelwidget.cameraRotation() Savable.save(self) if self.qsteelwidget.saveCurrentLevel(): s='%(self)s saved successfuly' else: s='%(self)s failed to save' Ui.instance().show_status(s)
def save(self): """ Retrieve some data before saving them. """ self.camera_position = self.qsteelwidget.cameraPosition() self.camera_rotation = self.qsteelwidget.cameraRotation() Savable.save(self) if self.qsteelwidget.saveCurrentLevel(): s = '%(self)s saved successfuly' else: s = '%(self)s failed to save' Ui.instance().show_status(s)
def new_level(self, props={}): """ Triggered by the ui when the corresponding menu item is clicked. It opens a level configuration form in a tabbed widget, that calls it again with the properties ('props') filled by the user. """ if self.project is None: Ui.instance().show_status('please create a project first.') return if props: self.project.new_level(props) Ui.instance().show_status('new level created') else: return Ui.instance().open_level_creation_dialog(self.new_level)
def on_steel_ready(self): print "Weld.on_steel_ready()" qsteelwidget = Ui.instance().qsteelwidget #make sure we know when to clean what follows qsteelwidget.onSteelClosing.connect(self.on_steel_closing) qsteelwidget.onItemDropped.connect(self.on_item_dropped) qsteelwidget.onAgentsSelected.connect(self.on_agents_selected) #add editing specific resources location p = os.path.join(self.current_project_path, Config.instance().weld_data_path, 'resources') qsteelwidget.addResourceLocation(p, 'FileSystem', Config.instance().weld_resource_group) if self.project is not None: self.project.on_steel_ready(qsteelwidget) Ui.instance().on_steel_ready(qsteelwidget)
def on_steel_ready(self, qsteelwidget): print "%(self)s.on_steel_ready()" % locals() Ui.instance().qsteelwidget.setRootDir(self.rootdir) if 0: while self.on_steel_ready_actions: action = self.on_steel_ready_actions[0] self.on_steel_ready_actions.pop(0) if 'load_level' in action: assert(self.level is not None) self.level.load() else: raise Exception('%(self)s: unrecognized action in '\ 'self.on_steel_ready_actions: %(action)s' % \ locals()) if self.level is not None: self.level.on_steel_ready(qsteelwidget)
def on_steel_ready(self, qsteelwidget): print "%(self)s.on_steel_ready()" % locals() Ui.instance().qsteelwidget.setRootDir(self.rootdir) if 0: while self.on_steel_ready_actions: action = self.on_steel_ready_actions[0] self.on_steel_ready_actions.pop(0) if 'load_level' in action: assert (self.level is not None) self.level.load() else: raise Exception('%(self)s: unrecognized action in '\ 'self.on_steel_ready_actions: %(action)s' % \ locals()) if self.level is not None: self.level.on_steel_ready(qsteelwidget)
def attach_to_Ui(self): """ Links the level with its views. """ print '<Level \'%s\'>.attach_to_Ui():' % (self.name) self.resMan = ResourceManager(self.path, level=self) self.resMan.attach_to_Ui(Ui.instance().res_browser['level']) self.qsteelwidget = Ui.instance().qsteelwidget if self.qsteelwidget.isSteelReady(): print 'loading now.' weld.Weld.instance().on_steel_ready() else: print 'will wait for steel to be ready before loading.' self.qsteelwidget.onSteelReady.connect(weld.Weld.instance().on_steel_ready) Ui.instance().level_name = self.name
def attach_to_Ui(self): """ Links the level with its views. """ print '<Level \'%s\'>.attach_to_Ui():' % (self.name) self.resMan = ResourceManager(self.path, level=self) self.resMan.attach_to_Ui(Ui.instance().res_browser['level']) self.qsteelwidget = Ui.instance().qsteelwidget if self.qsteelwidget.isSteelReady(): print 'loading now.' weld.Weld.instance().on_steel_ready() else: print 'will wait for steel to be ready before loading.' self.qsteelwidget.onSteelReady.connect( weld.Weld.instance().on_steel_ready) Ui.instance().level_name = self.name
def on_item_dropped(self, url): """ triggered when an item is dropped in the qsteelwidget. """ print 'Weld.on_item_dropped:', url #make sure all struct are present if not (self.project and self.project.level): print >> sys.stderr, 'it\'s too early to drop stuff: '\ 'create a project and a level first !' return #retrieve data if it comes from weld if url in self.resMan: props = self.resMan.file_props(url) if props is None: print >> sys.stderr, curr_f(), ': url(\'%s\') in self.resMan '\ 'but can\'t retrieve props.' % (url) return props = self.project.level.resMan.add_resource( self.resMan.base_path, props) url = props['url'] if props == {} or url not in self.project.level.resMan: print >> sys.stderr, curr_f(), 'could not retrieve file and/or '\ 'dependencies for props:', pp(props) return #instanciate it if url in self.project.level.resMan: props = self.project.level.resMan.file_props(url) dtp = self.project.level.qsteelwidget.dropTargetPosition( Config.instance().drop_target_vec) props['position'] = dtp props[ 'rotation'] = self.project.level.qsteelwidget.dropTargetRotation( ) if props['resource_type'] == 'meshes': props['meshName'] = props['name'] self.project.level.instanciate(props) s = 'dropped agent \'%s\' with id %i' % (props['name'], props['agentId']) print s Ui.instance().show_status(s) else: Ui.instance().show_status('can only drop meshes so far')
def new_project(self, rootdir=None): """ Open a new project. If rootdir is given, directly open the project file (see Project.file_extension)") located in it. Otherwise, open a dialog for the user to choose a path. """ if rootdir is None: rootdir = Ui.instance().select_directory(user.home) if not os.path.exists(rootdir): os.makedirs(rootdir) print 'Weld.new_project in ', rootdir project = Project(rootdir) project.save() self.project = project self.current_project_path = rootdir Ui.instance().set_resources_draggable(True) Ui.instance().show_status('new project created')
def load(self): Savable.load(self) Ui.instance().project_name = self.name print debug.curr_f(), ': Config.on_project_opening_reopen_last_level = True' if Config.instance().on_project_opening_reopen_last_level: print 'trying to reopen level named \'%s\'' % (self.level_name) if self.level_name: self.open_level(self.level_name) else: print >> sys.stderr, 'could not reopen such level.' else: print 'level auto reload skipped.'
def on_item_dropped(self, url): """ triggered when an item is dropped in the qsteelwidget. """ print 'Weld.on_item_dropped:', url #make sure all struct are present if not(self.project and self.project.level): print >> sys.stderr, 'it\'s too early to drop stuff: '\ 'create a project and a level first !' return #retrieve data if it comes from weld if url in self.resMan: props = self.resMan.file_props(url) if props is None: print >> sys.stderr, curr_f(), ': url(\'%s\') in self.resMan '\ 'but can\'t retrieve props.' % (url) return props = self.project.level.resMan.add_resource(self.resMan.base_path, props) url = props['url'] if props == {} or url not in self.project.level.resMan: print >> sys.stderr, curr_f(), 'could not retrieve file and/or '\ 'dependencies for props:', pp(props) return #instanciate it if url in self.project.level.resMan: props = self.project.level.resMan.file_props(url) dtp = self.project.level.qsteelwidget.dropTargetPosition(Config.instance().drop_target_vec) props['position'] = dtp props['rotation'] = self.project.level.qsteelwidget.dropTargetRotation() if props['resource_type'] == 'meshes': props['meshName'] = props['name'] self.project.level.instanciate(props) s = 'dropped agent \'%s\' with id %i' % (props['name'], props['agentId']) print s Ui.instance().show_status(s) else: Ui.instance().show_status('can only drop meshes so far')
def load(self): Savable.load(self) Ui.instance().project_name = self.name print debug.curr_f( ), ': Config.on_project_opening_reopen_last_level = True' if Config.instance().on_project_opening_reopen_last_level: print 'trying to reopen level named \'%s\'' % (self.level_name) if self.level_name: self.open_level(self.level_name) else: print >> sys.stderr, 'could not reopen such level.' else: print 'level auto reload skipped.'
def __init__(self, parent=None): if Weld.__instance is None: Weld.__instance = self else: raise Exception( 'can\'t declare new Weld instance. Weld is a singleton.') Savable.__init__(self, savepath=os.path.join(user.home, '.weld', 'weld.cache')) self.savelist += ['current_project_path'] # this is used to reload last opened project self.current_project_path = None Ui(self) # setup internal resource handlers self.project = None # list of resources we can ask steel to load self.resMan = ResourceManager( os.path.join(Config.instance().weld_data_path, 'resources')) self.resMan.attach_to_Ui(Ui.instance().res_browser['library']) # entity responsible for behavior trees manipulations self.BTMan = BTManager() # ready Ui.instance().show_status('ready', 1000) self.load() if Config.instance().on_weld_start_reopen_last_project: if self.current_project_path is not None: print 'auto reopening project \'%s\'.' % self.current_project_path p, f = os.path.split(self.current_project_path) self.open_project(self.current_project_path, f) else: print 'no project to reopen.' else: print 'skipping project reopening.'
def __init__(self, rootdir): """ rootdir: path to the root folder of the project. The project holds the directory name. """ if not os.path.exists(rootdir): raise Exception('path %s does not exist.') self.rootdir = rootdir self.name = os.path.split(rootdir)[-1] Ui.instance()._project_name = self.name Savable.__init__(self, savepath=os.path.join(rootdir, self.name + Project.file_extension)) self.level = None self.level_name = None # list of commands (as custom strings to execute at next call to on_steel_ready) self.on_steel_ready_actions = [] # self.savelist += ['rootdir', 'name', 'level_name']
def __init__(self, rootdir): """ rootdir: path to the root folder of the project. The project holds the directory name. """ if not os.path.exists(rootdir): raise Exception('path %s does not exist.') self.rootdir = rootdir self.name = os.path.split(rootdir)[-1] Ui.instance()._project_name = self.name Savable.__init__(self, savepath=os.path.join( rootdir, self.name + Project.file_extension)) self.level = None self.level_name = None # list of commands (as custom strings to execute at next call to on_steel_ready) self.on_steel_ready_actions = [] # self.savelist += ['rootdir', 'name', 'level_name']
def open_project(self, rootdir=None, filename=None): """ root dir is the path to the project, filename is the project file. """ if None in [rootdir, filename]: if rootdir is None: rootdir = '~' filepath = Ui.instance().select_file(startdir=rootdir, extensions='Weld project files (*%s)' % Project.file_extension, label='Select a weld project') if filepath is None: Ui.instance().show_status('project opening is aborted') return rootdir, filename = os.path.split(filepath) else: if not os.path.exists(rootdir): self.current_project_path = None s = 'invalid project path:', rootdir print >> sys.stderr, s Ui.instance().show_status(s) return if not os.path.exists(os.path.join(rootdir, filename + Project.file_extension)): self.current_project_path = None s = 'can\'t locate project file \'%s\' inside \'%s\'' % (filename, rootdir) print >> sys.stderr, s Ui.instance().show_status(s) return print 'Weld.open_project \'%(filename)s in %(rootdir)s' % locals() project = Project(rootdir) project.load() self.project = project self.current_project_path = rootdir Ui.instance().set_resources_draggable(True) Ui.instance().show_status('project %s opened' % (filename))
def close_project(self): Ui.instance().set_resources_draggable(False) raise NotImplementedError()
def run(self): r = Ui.instance().show() self.on_quit() return r
def open_project(self, rootdir=None, filename=None): """ root dir is the path to the project, filename is the project file. """ if None in [rootdir, filename]: if rootdir is None: rootdir = '~' filepath = Ui.instance().select_file( startdir=rootdir, extensions='Weld project files (*%s)' % Project.file_extension, label='Select a weld project') if filepath is None: Ui.instance().show_status('project opening is aborted') return rootdir, filename = os.path.split(filepath) else: if not os.path.exists(rootdir): self.current_project_path = None s = 'invalid project path:', rootdir print >> sys.stderr, s Ui.instance().show_status(s) return if not os.path.exists( os.path.join(rootdir, filename + Project.file_extension)): self.current_project_path = None s = 'can\'t locate project file \'%s\' inside \'%s\'' % ( filename, rootdir) print >> sys.stderr, s Ui.instance().show_status(s) return print 'Weld.open_project \'%(filename)s in %(rootdir)s' % locals() project = Project(rootdir) project.load() self.project = project self.current_project_path = rootdir Ui.instance().set_resources_draggable(True) Ui.instance().show_status('project %s opened' % (filename))