def on_duplicate(self, event): """Ask for a name and clone the selected object it if it's a non existing name""" source_name = self.list.GetStringSelection() if source_name != "": dlg = wx.TextEntryDialog( None, "Enter the " + self.caption + "'s name:", "Duplicate " + self.caption, source_name ) if dlg.ShowModal() == wx.ID_OK: # Clone the object only if there isn't another object with the same name yet target_name = dlg.GetValue() target_path = format.append_to_path( self.lib_path, target_name ) try: # Lookup will fail if it's a new name (name not found) pynebula.lookup( target_path ) msg = "Another " + self.caption + " named '" + dlg.GetValue() + "' already exists." cjr.show_error_message(msg) except: # Clone object, save it to disk and add it to list source_path = format.append_to_path( self.lib_path, source_name ) source_obj = pynebula.lookup( source_path ) target_obj = source_obj.clone( str(target_path) ) self.save_object( target_obj ) self.list.Append( target_name ) dlg.Destroy()
def unassign_preset(level_name): # Build level configuration file path level_path = format.append_to_path(get_presets_path(), "levels/%s.txt" % level_name) # Delete the level configuration file, if exists if os.path.exists(level_path): os.remove(level_path)
def on_ok(self, event): try: wx.BeginBusyCursor() emitter_name = str( self.get_new_class_name() ) if self.is_use_default_texture_selected(): emitter = particle2.add_emitter( self.particle_system, emitter_name ) else: texture = format.append_to_path( "wc:export/textures", self.get_custom_texture_name() ) emitter = particle2.add_emitter( self.particle_system, emitter_name, str(texture) ) if emitter is None: msg = "Unable to create '%s' particle emitter" % emitter_name cjr.show_error_message(msg) else: self.EndModal(wx.ID_OK) finally: wx.EndBusyCursor()
def __copy(source_dir, target_dir): """Copy all objects from a directory to another directory (not recursive)""" obj = pynebula.lookup( source_dir ).gethead() while obj != None: path = format.append_to_path( target_dir, obj.getname() ) obj.clone( path ) obj = obj.getsucc()
def on_save_preset(self, event): """Save the current preset with the name chosen by the user""" # Ask for the preset's name if self.__prepresets_process(): path = format.append_to_path( get_presets_path(), "lib" ) dlg = dirdlg.DirDialog( self.get_frame(), dirdlg.SAVE, 'preset', 'Preset', path ) if dlg.ShowModal() == wx.ID_OK: if dlg.dir_exists(): msg = "Overwrite the '%s' preset?" % dlg.get_dirname() result = cjr.confirm_yes_no( self.get_frame(), msg ) if result == wx.ID_YES: save_preset( dlg.get_path() ) else: save_preset( dlg.get_path() ) dlg.Destroy()
def __can_delete_selector(self, class_name): usage_log = usagelog.find_fsm_selector( class_name ) if len( usage_log[1] ) > 0: dlg = usagelog.DeleteErrorDialog( app.get_top_window(), "FSM selector '" + class_name + "'", usage_log ) dlg.Show() return False # Save FSMs to avoid inconsistences between memory and persisted states # (and make the previous check valid) delete = self.__save_fsms('FSM selector') # Delete the singleton selector instance so the class can be deleted if delete: selector_path = format.append_to_path( fsm.get_fsm_selectors_lib(), class_name ) try: pynebula.delete( str(selector_path) ) except: pass return delete
def __get_template_path(self, template_name, unmangled=False): path = format.append_to_path( TemplatesPath, template_name + ".lua" ) if unmangled: return path return format.mangle_path( path )
def __get_cmd_path(self, class_name, cmd_name, unmangled=False): path = format.append_to_path( self.__get_class_path(class_name), cmd_name + ".lua" ) if unmangled: return path return format.mangle_path( path )
def on_save_default_preset(self, event): """Save the current preset as the default one""" # Ask for the preset's name if self.__prepresets_process(): path = format.append_to_path( get_presets_path(), "lib/default" ) save_preset( path )
def save_preset(preset_path): # Create preset path if it doesn't exist if not os.path.exists(preset_path): os.makedirs(preset_path) # Save stdlight entity_id = app.get_level().findentity('stdlight') if entity_id == 0: cjr.show_error_message("'stdlight' not found") else: entity = servers.get_entity_object_server().getentityobject(entity_id) entity.savestateas( str(format.append_to_path(preset_path, "stdlight.n2"))) # Save viewport ui viewport_ui = app.get_viewports_dir() viewport_ui.savestateas( str(format.append_to_path(preset_path, "viewportui.n2"))) # Save viewports for index in range(4): viewport_name = "viewport%s.n2" % index viewport_path = format.append_to_path(preset_path, viewport_name) viewport = pynebula.lookup( format.append_to_path(app.get_viewports_dir().getfullname(), viewport_name[:-3])) viewport.savestateas(str(viewport_path)) # Save special viewport rnsview viewport_path = format.append_to_path(preset_path, "rnsview.n2") viewport = pynebula.lookup('/usr/rnsview') viewport.savestateas(str(viewport_path)) # Save camera bookmarks bookmark_path = format.append_to_path(preset_path, "bookmarks.n2") servers.get_conjurer().savebookmarks(bookmark_path) # Save mouse settings (sensitivity, invert y-axis, etc) mouse_setting_path = format.append_to_path(preset_path, "mousesettings.n2") game_state = app.get_state("game") game_state.savemousesettings(str(mouse_setting_path))
def unassign_preset(level_name): # Build level configuration file path level_path = format.append_to_path( get_presets_path(), "levels/%s.txt" % level_name ) # Delete the level configuration file, if exists if os.path.exists( level_path ): os.remove( level_path )
def remove_class_dir(path, report=None, class_name=None): # Remove all files from the directory children = os.listdir( path ) for child in children: os.remove( format.append_to_path( path, child ) ) if report is not None: report.append( (class_name, child[:-4]) ) # Remove the directory os.rmdir( path )
def apply_layer_settings(self, layer_ctrl): u_value = float( self.settings.choice_u.GetValue() ) v_value = float( self.settings.choice_v.GetValue() ) layer_ctrl.set_layer_name( str( self.settings.text_name.GetValue() ) ) if self.settings.radio_local_texture.GetValue(): layer_ctrl.set_texture_filename( str( format.append_to_path( self.settings.get_textures_path(), self.settings.combo_local_texture.GetStringSelection() ) ) ) else: layer_ctrl.set_texture_filename( str( format.append_to_path( self.settings.get_shared_path(), self.settings.button_shared_texture.GetLabel() ) ) ) layer_ctrl.set_uv_scale(( self.settings.uvsize2scale(u_value), self.settings.uvsize2scale(v_value) )) layer_ctrl.set_projection( self.settings.choice_projection.GetSelection() ) layer_ctrl.set_color_mask( self.settings.color_sel.get_value() ) layer_ctrl.set_game_material( self.settings.choice_game_material.GetStringSelection() ) # update material info try: app.get_outdoor().updatematerialall() except: pass
def __assign_preset(self, preset_name): # Build level configuration file path level_path = format.append_to_path( get_presets_path(), "levels/%s.txt" % app.get_level().getname() ) # Write the preset name to the level configuration file level_file = open( level_path, 'w' ) level_file.write( preset_name ) level_file.close()
def get_class_commands(class_name, inherited_cmds = False): """Return the prototype of all the scripted commands for a class""" path = format.append_to_path( ClassesPath, class_name ) path = format.mangle_path( path ) cmds_list = filedlg.get_file_list( path, ['lua'] ) if inherited_cmds: class_name = servers.get_kernel_server().getsuperclass( class_name ) if class_name != ':null:': cmds_list.extend( get_class_commands(class_name, True) ) cmds_list.sort() return cmds_list
def copy_file(source_file, target_directory): """ Copy a file into a directory. If there's already another file with the same name in the target directory the user is asked to enter a new file name, overwrite the existing one or just cancel the operation. Return true if the file has been copied, false otherwise. """ src = servers.get_file_server().manglepath( str(source_file) ) dir_name = servers.get_file_server().manglepath( str(target_directory) ) target = format.append_to_path( dir_name, format.get_name(src) ) while os.path.exists( target ): result = cjr.confirm_yes_no( None, "A file called '%s' already exists.\n\n" \ "Do you want to rename the new file?\n" \ "(answering 'no' will overwrite the old one)"\ % format.get_name(src) ) if result == wx.ID_YES: dlg = wx.TextEntryDialog( None, "New file name:", "Conjurer", format.get_name(target) ) if dlg.ShowModal() == wx.ID_OK and dlg.GetValue() != "": target = format.append_to_path( dir, dlg.GetValue() ) elif result == wx.ID_NO: break else: return False shutil.copy(src, target) return True
def validate_fsm_name(name): path = format.append_to_path( fsm.get_fsms_lib(), name ) try: # Lookup will fail if it's a new name (name not found) pynebula.lookup( path ) msg = "Another finite state machine called '%s' already exists." % name cjr.show_error_message(msg) return False except: msg = "Renaming this FSM will invalidate any references to it that " \ "any agent has.\nAre you sure that you want to rename it?" result = cjr.confirm_yes_no(None, msg) return result == wx.ID_YES
def on_ok(self, event): if self.button_texture.GetLabel() == "<default>": entity_class = particle.CreateClass( str(self.text_name.GetValue()), self.target_library ) else: texture = format.append_to_path( "wc:export/textures", self.button_texture.GetLabel() ) entity_class = particle.CreateClass( str(self.text_name.GetValue()), self.target_library, str(texture) ) if entity_class is None: wx.MessageBox( "Couldn't create the particle system class named '" \ + self.text_name.GetValue() + "'", "Conjurer", style=wx.ICON_ERROR ) else: objdlg.create_window( app.get_top_window(), entity_class.getfullname() ) self.EndModal(wx.ID_OK)
def __delete_preset(self, preset_path): # Remove stdlight state stdlight_path = format.append_to_path( preset_path, "stdlight.n2" ) if os.path.exists( stdlight_path ): os.remove( stdlight_path ) # Remove viewport ui state viewport_ui_path = format.append_to_path( preset_path, "viewportui.n2" ) if os.path.exists( viewport_ui_path ): os.remove( viewport_ui_path ) # Remove viewports for index in range(4): viewport_name = "viewport%s.n2" % index viewport_path = format.append_to_path( preset_path, viewport_name ) if os.path.exists( viewport_path ): os.remove( viewport_path ) # Remove preset directory try: os.rmdir( preset_path ) except: cjr.show_error_message( "Unable to delete the preset directory '%s'.\n" \ "It may not be empty or another application may be using it." \ "\n\nPlease remove it manually to delete " \ "the preset." % preset_path ) # Remove bookmarks, if present bookmark_path = format.append_to_path( preset_path, "bookmarks.n2" ) if os.path.exists( bookmark_path ): os.remove( bookmark_path ) # Remove mouse settings, if present mouse_setting_path = format.append_to_path( preset_path, "mousesettings.n2" ) if os.path.exists( mouse_setting_path ): os.remove( mouse_setting_path )
def on_select_fsm(self, event): type = self.__get_selected_list_item( self.ID_AgentType ) if type != "": sm_name = self.choice_fsm.GetStringSelection() if sm_name == "<none>": sm = None else: sm_path = format.append_to_path( fsm.get_fsms_lib(), sm_name ) sm = pynebula.lookup( sm_path ) entity_class = servers.get_entity_class_server().getentityclass( str(type) ) entity_class.setparentfsm( sm ) servers.get_entity_class_server().setentityclassdirty( entity_class, True ) index = self.__get_selected_list_index() self.list.SetStringItem( index, self.ID_FSM, sm_name )
def get_level_preset_name(): # Get the preset name from the current level's configuration file level_name = app.get_level().getname() level_path = format.append_to_path(get_presets_path(), "levels/%s.txt" % level_name) if os.path.exists(level_path): # The level has a configuration file: get the preset name from it level_file = open(level_path, 'r') preset_name = level_file.read() level_file.close() else: # The level doesn't have a configuration file: use the default preset preset_name = "default" return preset_name
def __prepresets_process(self): # Ask for presets directory creation if it doesn't exist yet path = get_presets_path() if not os.path.exists( path ): msg = "The presets directory '%s' doesn't exist.\n" \ "A presets directory is needed to continue.\n\n" \ "Do you want to create it?" % path result = cjr.confirm_yes_no( self.get_frame(), msg ) if result == wx.ID_YES: os.makedirs( path ) # Create subpaths if doesn't exist yet lib_path = format.append_to_path( path, "lib" ) levels_path = format.append_to_path( path, "levels" ) if os.path.exists( path ): # Create subpaths only if root path has been created if not os.path.exists( lib_path ): os.mkdir( lib_path ) if not os.path.exists( levels_path ): os.mkdir( levels_path ) # Return true only if all the presets subpaths finally exists return os.path.exists( lib_path ) and os.path.exists( levels_path )
def get_level_preset_name(): # Get the preset name from the current level's configuration file level_name = app.get_level().getname() level_path = format.append_to_path( get_presets_path(), "levels/%s.txt" % level_name ) if os.path.exists(level_path): # The level has a configuration file: get the preset name from it level_file = open(level_path, 'r') preset_name = level_file.read() level_file.close() else: # The level doesn't have a configuration file: use the default preset preset_name = "default" return preset_name
def on_new(self, event): """Ask for a name and create a new object it if it's a non existing name""" dlg = wx.TextEntryDialog( None, "Enter the " + self.caption + "'s name:", "New " + self.caption ) if dlg.ShowModal() == wx.ID_OK: # Add the object only if there isn't another object with the same name yet obj_path = format.append_to_path( self.lib_path, dlg.GetValue() ) try: # Lookup will fail if it's a new name (name not found) pynebula.lookup( obj_path ) msg = "Another " + self.caption + " named '" + dlg.GetValue() + "' already exists." cjr.show_error_message(msg) except: # Create object, save it to disk and add it to list obj = pynebula.new( str(self.class_name), str(obj_path) ) self.save_object( obj ) self.list.Append( dlg.GetValue() ) dlg.Destroy()
def get_file_list(directory, extensions, autoextension=True, recursive=False, excluded_files=[]): """ Return a sequence of the files of the given directory. The file extension is removed if autoextension is set to True and only one file extension is given. Only the relative path to the given directory is returned for each file, not its full path. """ gui_names = [] mangled_dir = servers.get_file_server().manglepath( str(directory) ) try: filenames = os.listdir( mangled_dir ) except: return [] for filename in filenames: if filename.startswith("."): # skip special directories/files continue file_path = format.append_to_path( mangled_dir, filename ) if os.path.isdir( file_path ): if recursive: # recurse directories subnames = get_file_list( file_path, extensions, autoextension, recursive ) for name in subnames: gui_names.append( filename + "/" + name ) else: # add the file if its extension matches one of the desired options for extension in extensions: try: excluded_files.index( filename ) except: if filename.endswith( "." + extension ): if autoextension: gui_names.append( filename[:-len("."+extension)] ) else: gui_names.append( filename ) return gui_names
def on_apply_preset(self, event): """Apply the choosen preset onto the current level""" # Ask for the preset to apply if self.__prepresets_process(): path = format.append_to_path( get_presets_path(), "lib" ) dlg = dirdlg.DirDialog( self.get_frame(), dirdlg.OPEN, 'preset', 'Preset', path ) if dlg.ShowModal() == wx.ID_OK: apply_preset( dlg.get_dirname(), dlg.get_path() ) dlg.Destroy()
def get_directory_list(directory): """ Return a sequence of the directories within the given directory. Only the relative path to the given directory is returned for each file, not its full path. """ gui_names = [] mangled_dir = servers.get_file_server().manglepath( str(directory) ) filenames = os.listdir( mangled_dir ) for filename in filenames: if filename.startswith("."): # skip special directories/files continue file_path = format.append_to_path( mangled_dir, filename ) if os.path.isdir( file_path ): gui_names.append( filename ) return gui_names
def on_delete_preset(self, event): """Delete a preset""" # Ask for the preset to delete if self.__prepresets_process(): path = format.append_to_path( get_presets_path(), "lib" ) dlg = dirdlg.DirDialog( self.get_frame(), dirdlg.DELETE, 'preset', 'Preset', path ) if dlg.ShowModal() == wx.ID_OK: self.__delete_preset( dlg.get_path() ) dlg.Destroy()
def save_preset(preset_path): # Create preset path if it doesn't exist if not os.path.exists( preset_path ): os.makedirs( preset_path ) # Save stdlight entity_id = app.get_level().findentity('stdlight') if entity_id == 0: cjr.show_error_message("'stdlight' not found") else: entity = servers.get_entity_object_server().getentityobject( entity_id ) entity.savestateas( str( format.append_to_path(preset_path, "stdlight.n2") ) ) # Save viewport ui viewport_ui = app.get_viewports_dir() viewport_ui.savestateas( str( format.append_to_path(preset_path, "viewportui.n2") ) ) # Save viewports for index in range(4): viewport_name = "viewport%s.n2" % index viewport_path = format.append_to_path( preset_path, viewport_name ) viewport = pynebula.lookup( format.append_to_path( app.get_viewports_dir().getfullname(), viewport_name[:-3] ) ) viewport.savestateas( str(viewport_path) ) # Save special viewport rnsview viewport_path = format.append_to_path( preset_path, "rnsview.n2" ) viewport = pynebula.lookup( '/usr/rnsview' ) viewport.savestateas( str(viewport_path) ) # Save camera bookmarks bookmark_path = format.append_to_path( preset_path, "bookmarks.n2" ) servers.get_conjurer().savebookmarks( bookmark_path ) # Save mouse settings (sensitivity, invert y-axis, etc) mouse_setting_path = format.append_to_path( preset_path, "mousesettings.n2" ) game_state = app.get_state( "game" ) game_state.savemousesettings( str(mouse_setting_path) )
def on_delete(self, event): """Ask to confirm the object deletion and delete it if so""" name = self.list.GetStringSelection() if name != "": msg = "Deleting a %s cannot be undone.\n\n" \ "Are you sure you want to delete " \ "the '%s' %s?" % ( (self.caption, name, self.caption) ) delete = cjr.warn_yes_no(self, msg) if delete == wx.ID_YES: obj_path = format.append_to_path( self.lib_path, name ) if self.erase_object( pynebula.lookup(obj_path) ): # Close the editor for this object if it's opened editor = self._get_editor( obj_path ) if editor != None: editor.Close() # Delete the object pynebula.delete( str(obj_path) ) self.list.Delete( self.list.GetSelection() ) self.__refresh_editors()
def on_clean_commands(self, event): """Delete the whole directories of the non existent classes""" # Remove the classes directories ks = servers.get_kernel_server() removed_cmds = [] classes_path = format.mangle_path( cmdmgrdlg.ClassesPath ) filenames = os.listdir( classes_path ) for filename in filenames: if filename.startswith("."): # skip special directories/files continue file_path = format.append_to_path( classes_path, filename ) if os.path.isdir( file_path ) and ks.findclass(str(filename)) == "": cmdmgrdlg.remove_class_dir( file_path, removed_cmds, filename ) # Show which have been the removed classes if len( removed_cmds ) == 0: cjr.show_information_message( "No trash command has been found." ) else: dlg = CleanReportDialog( self.get_frame(), removed_cmds ) dlg.ShowModal() dlg.Destroy()
def get_level_preset_path(): return format.append_to_path( get_presets_path(), "lib/%s" % get_level_preset_name() )
def apply_preset(preset_name, preset_path): """Load and apply the specified preset""" # Validate preset path if not os.path.exists(preset_path): if preset_name != "default": level_name = get_name_of_current_level() msg = "Unable to find the '%s' preset attached to " \ "the '%s' level" % (preset_name, level_name) cjr.show_error_message(msg) return # Load stdlight stdlight_path = format.append_to_path(preset_path, "stdlight.n2") if not os.path.exists(stdlight_path): cjr.show_error_message( "Unable to find the Stdlight state for the '%s' preset"\ % preset_name ) else: entity_id = app.get_level().findentity('stdlight') if entity_id == 0: cjr.show_error_message("'stdlight' not found") else: entity = servers.get_entity_object_server().getentityobject( entity_id ) entity.loadstate( str(stdlight_path) ) # Viewport UI viewport_ui_path = format.append_to_path( preset_path, "viewportui.n2" ) if not os.path.exists( viewport_ui_path ): cjr.show_error_message( "Unable to find the Viewport UI state for the '%s' preset"\ % preset_name ) else: viewport_ui = app.get_viewports_dir() viewport_ui.loadstate( str(viewport_ui_path) ) # Viewports for index in range(4): viewport_name = "viewport%s.n2" % index viewport_path = format.append_to_path( preset_path, viewport_name ) if not os.path.exists( viewport_path ): cjr.show_error_message( "Unable to find the Viewport %s state for the '%s' preset" \ % ( index, preset_name ) ) else: viewport = pynebula.lookup( format.append_to_path( app.get_viewports_dir().getfullname(), viewport_name[:-3] ) ) viewport.loadstate( str(viewport_path) ) # Special viewport rnsview viewport_path = format.append_to_path(preset_path, "rnsview.n2") if os.path.exists( viewport_path ): viewport = pynebula.lookup( '/usr/rnsview' ) viewport.loadstate( str(viewport_path) ) # Camera bookmarks bookmark_path = format.append_to_path(preset_path, "bookmarks.n2") if os.path.exists( bookmark_path ): servers.get_conjurer().loadbookmarks(bookmark_path) # Mouse settings (sensitivity, invert y-axis, etc) mouse_setting_path = format.append_to_path(preset_path, "mousesettings.n2") if os.path.exists( mouse_setting_path ): game_state = app.get_state("game") game_state.loadmousesettings( str(mouse_setting_path) )