public void ShowAddLevelUi() { AddNewLevelView view = new AddNewLevelView(); var viewModel = new AddNewLevelViewModel(); viewModel.MakeNameUnique(GlueState.Self.CurrentScreenSave.ReferencedFiles); view.DataContext = viewModel; AddTmxWithSharedTsxTo(viewModel); var dialogResult = view.ShowDialog(); if (dialogResult.HasValue && dialogResult.Value) { string whyIsntValid = GetWhyCantAddLevel(viewModel.Name); if (string.IsNullOrEmpty(whyIsntValid)) { #if DEBUG if (viewModel.CreateShareTilesetWith && viewModel.SelectedSharedFile == null) { throw new Exception("The viewModel indicates that the user selected a shared file, but it'the file is not specified"); } #endif AddLevel(viewModel); } else { MessageBox.Show(whyIsntValid); } } }
private static void AddTmxWithSharedTsxTo(AddNewLevelViewModel viewModel) { List <string> filesAlreadyVisited = new List <string>(); var tmxFiles = GlueState.Self.CurrentGlueProject.GetAllReferencedFiles().Where(item => TileGraphicsPlugin.Managers.FileReferenceManager.Self.IsTmx(item)); foreach (var file in tmxFiles) { string relativeToContent = file.Name; bool isBuiltTmx = !string.IsNullOrEmpty(file.SourceFile) && FileManager.GetExtension(file.SourceFile) == "tmx"; if (isBuiltTmx) { relativeToContent = file.SourceFile; } string absoluteFileName = GlueCommands.Self.GetAbsoluteFileName(relativeToContent, true); string absoluteStandardized = FileManager.Standardize(absoluteFileName).ToLowerInvariant(); if (!filesAlreadyVisited.Contains(absoluteStandardized) && System.IO.File.Exists(absoluteFileName)) { filesAlreadyVisited.Add(absoluteStandardized); TiledMapSave tsx = null; try { tsx = TiledMapSave.FromFile(absoluteFileName); } catch (Exception exception) { FlatRedBall.Glue.Plugins.PluginManager.ReceiveError("Error loading TMX when trying to find TSX references: " + exception.Message); } if (tsx != null) { var hasShared = tsx.Tilesets.Any(item => !string.IsNullOrEmpty(item.Source)); if (hasShared) { viewModel.AvailableSharedFiles.Add(relativeToContent); } } } } }
public void AddLevel(AddNewLevelViewModel levelInfo) { bool shouldContinue = true; shouldContinue = ShareOnSourceTsxIfNecessary(levelInfo); if (shouldContinue) { TryAddLevelsFolders(); CreateFilesOnDisk(levelInfo); CreateReferencedFileSaves(levelInfo.Name); TryAddSolidCollisions(); TryAddUsesTmxLevelFilesVariable(); SaveEverything(); GenerateCode(); } }
private void CreateFilesOnDisk(AddNewLevelViewModel levelInfo) { string levelName = levelInfo.Name; var screen = GlueState.Self.CurrentScreenSave; var treeNode = GlueState.Self.Find.ScreenTreeNode(screen); var filesTreeNode = treeNode.FilesTreeNode; var levelsFolder = filesTreeNode.Nodes.FirstOrDefault(item => item.Text == "Levels"); var tilesetsFolder = filesTreeNode.Nodes.FirstOrDefault(item => item.Text == "Tilesets"); var thisAssembly = this.GetType().Assembly; if (levelInfo.CreateSamplePlatformer) { CreateSamplePlatformerLevelAndAssociatedFilesOnDisk(levelName, levelsFolder, tilesetsFolder, thisAssembly); } else if (levelInfo.CreateEmptyLevel) { CreateEmptyLevelFilesOnDisk(levelName, levelsFolder, levelInfo.IndividualTileWidth, levelInfo.IndividualTileHeight, thisAssembly); } else if (levelInfo.CreateShareTilesetWith) { var savedFile = CreateEmptyLevelFilesOnDisk(levelName, levelsFolder, levelInfo.IndividualTileWidth, levelInfo.IndividualTileHeight, thisAssembly); string destinationFolder = FileManager.GetDirectory(savedFile); TiledMapSave newTms = TiledMapSave.FromFile(savedFile); if (string.IsNullOrEmpty(levelInfo.SelectedSharedFile)) { throw new NullReferenceException("levelInfo.SelectedSharedFile is null and it shouldn't be"); } string toPullFromAbsoluteFileName = GlueCommands.Self.GetAbsoluteFileName(levelInfo.SelectedSharedFile, true); string toPullFromFolder = FileManager.GetDirectory(toPullFromAbsoluteFileName); TiledMapSave toPullFrom = TiledMapSave.FromFile(toPullFromAbsoluteFileName); bool oldValue = Tileset.ShouldLoadValuesFromSource; Tileset.ShouldLoadValuesFromSource = false; foreach (var tileset in toPullFrom.Tilesets) { var clonedTileset = FileManager.CloneObject(tileset); // The tsx we're pulling from may not be in the same // folder as the tsx we're creating, so we need to convert // to absolute then back to relative: string absoluteSource = toPullFromFolder + clonedTileset.Source; string relativeToNewFolder = FileManager.MakeRelative(absoluteSource, destinationFolder); clonedTileset.Source = relativeToNewFolder; newTms.Tilesets.Add(clonedTileset); } Tileset.ShouldLoadValuesFromSource = oldValue; newTms.Save(newTms.FileName); } else { throw new NotImplementedException(); } }
private bool ShareOnSourceTsxIfNecessary(AddNewLevelViewModel levelInfo) { bool shouldContinue = true; if (levelInfo.CreateShareTilesetWith) { TiledMapSave tiledMapSave = null; string whyIsntValid = null; string absoluteFile = GlueCommands.Self.GetAbsoluteFileName(levelInfo.SelectedSharedFile, true); if (string.IsNullOrEmpty(levelInfo.SelectedSharedFile)) { whyIsntValid = "No TMX file selected to pull tilesets from"; } else if (System.IO.File.Exists(absoluteFile) == false) { whyIsntValid = "Could not find the file" + levelInfo.SelectedSharedFile; } if (string.IsNullOrEmpty(whyIsntValid)) { try { tiledMapSave = TiledMapSave.FromFile(absoluteFile); } catch (Exception e) { whyIsntValid = "Error loading the TMX: " + e.Message; } } if (!string.IsNullOrEmpty(whyIsntValid)) { MessageBox.Show(whyIsntValid); shouldContinue = false; } else { string destinationDirectory = GetLevelsFolderAbsoluteForScreen(GlueState.Self.CurrentScreenSave); var nonSharedTilesets = tiledMapSave.Tilesets.Where(item => item.IsShared == false); if (nonSharedTilesets.Count() > 0) { var shouldShareResult = MessageBox.Show( "The file " + levelInfo.SelectedSharedFile + " includes layers which are not shared. Would you like to share them?"); if (shouldShareResult != DialogResult.OK) { shouldContinue = false; } } if (shouldContinue) { bool shouldSave = false; foreach (var tileset in tiledMapSave.Tilesets.Where(item => item.IsShared == false)) { SharedTilesetManager.ConvertToSharedTileset( tileset, tiledMapSave, destinationDirectory); shouldSave = true; } if (shouldSave) { tiledMapSave.Save(tiledMapSave.FileName); } } } } return(shouldContinue); }