/// <summary> /// Allows the user to pick a project file. /// </summary> public static void OpenProject() { OpenFileDialog of = new OpenFileDialog(); of.SetFilter(new FileFilter("MK Project File", "mkproj")); string lastfolder = ""; if (GeneralSettings.RecentFiles.Count > 0) { string path = GeneralSettings.RecentFiles[0][1]; while (path.Contains("/")) { path = path.Replace("/", "\\"); } List <string> folders = path.Split('\\').ToList(); for (int i = 0; i < folders.Count - 1; i++) { lastfolder += folders[i]; if (i != folders.Count - 2) { lastfolder += "\\"; } } } of.SetInitialDirectory(lastfolder); of.SetTitle("Choose a project file..."); string result = of.Show() as string; if (!string.IsNullOrEmpty(result)) { if (!result.EndsWith(".mkproj")) { new MessageBox("Error", "Invalid project file.", ButtonType.OK, IconType.Error); } else { CloseProject(); Data.SetProjectPath(result); MainWindow.CreateEditor(); MakeRecentProject(); } } }
public TilesetEditor(IContainer Parent) : base(Parent) { Submodes = new SubmodeView(this); Submodes.SetHeaderHeight(31); Submodes.SetHeaderWidth(96); Submodes.SetHeaderSelHeight(1); Submodes.SetTextY(6); PassageContainer = Submodes.CreateTab("Passage"); VignetteFade PassageFade = new VignetteFade(PassageContainer); PassageContainer.OnSizeChanged += delegate(BaseEventArgs e) { PassageFade.SetSize(PassageContainer.Size); }; FourDirContainer = Submodes.CreateTab("4-Dir"); VignetteFade FourDirFade = new VignetteFade(FourDirContainer); FourDirContainer.OnSizeChanged += delegate(BaseEventArgs e) { FourDirFade.SetSize(FourDirContainer.Size); }; //PriorityContainer = Submodes.CreateTab("Priority"); //VignetteFade PriorityFade = new VignetteFade(PriorityContainer); //PriorityContainer.OnSizeChanged += delegate (BaseEventArgs e) { PriorityFade.SetSize(PriorityContainer.Size); }; //Submodes.CreateTab("Terrain Tag"); //Submodes.CreateTab("Bush Flag"); //Submodes.CreateTab("Counter Flag"); Container PassageSubContainer = new Container(PassageContainer); PassageList = new TilesetDisplay(PassageSubContainer); PassageList.OnTilesetLoaded += delegate(BaseEventArgs e) { PassageDrawAll(); }; PassageList.OnTileClicked += delegate(MouseEventArgs e) { PassageInput(e); }; Container FourDirSubContainer = new Container(FourDirContainer); FourDirList = new TilesetDisplay(FourDirSubContainer); FourDirList.OnTilesetLoaded += delegate(BaseEventArgs e) { FourDirDrawAll(); }; FourDirList.OnTileClicked += delegate(MouseEventArgs e) { FourDirInput(e); }; PassageContainer.SetBackgroundColor(28, 50, 73); FourDirContainer.SetBackgroundColor(28, 50, 73); //PriorityContainer.SetBackgroundColor(28, 50, 73); SharedContainer = new Container(this); SharedContainer.SetPosition(22, 41); SharedContainer.Sprites["bg"] = new Sprite(SharedContainer.Viewport); SimpleFade fade = new SimpleFade(SharedContainer); fade.SetPosition(4, 4); NameLabel = new Label(SharedContainer); NameLabel.SetText("Name"); NameLabel.SetFont(Font.Get("Fonts/Ubuntu-B", 14)); NameLabel.SetPosition(19, 16); NameBox = new TextBox(SharedContainer); NameBox.SetPosition(19, 40); NameBox.SetSize(156, 21); NameBox.SetSkin(1); // Updates tileset list NameBox.OnTextChanged += delegate(BaseEventArgs e) { if (this.Tileset == null) { return; } this.Tileset.Name = NameBox.Text; ListItem item = DBDataList.DataList.Items[TilesetID - 1]; item.Name = item.Name.Split(':')[0] + ": " + this.Tileset.Name; DBDataList.DataList.Redraw(); }; GraphicLabel = new Label(SharedContainer); GraphicLabel.SetFont(Font.Get("Fonts/Ubuntu-B", 14)); GraphicLabel.SetPosition(19, 79); GraphicLabel.SetText("Tileset Graphic"); GraphicBox = new BrowserBox(SharedContainer); GraphicBox.SetPosition(19, 103); GraphicBox.SetSize(156, 21); GraphicBox.OnDropDownClicked += delegate(BaseEventArgs e) { OpenFileDialog of = new OpenFileDialog(); of.SetFilter(new FileFilter("PNG Image", "png")); of.SetInitialDirectory(Game.Data.ProjectPath + "/gfx/tilesets"); of.SetTitle("Pick a tileset..."); object result = of.Show(); if (result != null) { // Converts path (C:/.../.../tileset_image.png) to filename (tileset_image) string path = result as string; while (path.Contains('\\')) { path = path.Replace('\\', '/'); } string[] folders = path.Split('/'); string file_ext = folders[folders.Length - 1]; string[] dots = file_ext.Split('.'); string file = ""; for (int i = 0; i < dots.Length - 1; i++) { file += dots[i]; if (i != dots.Length - 2) { file += '.'; } } string tilesetsfolder = Game.Data.ProjectPath + "/gfx/tilesets"; // Selected file not in the tilesets folder // Copies source to tilesets folder string chosenparent = System.IO.Directory.GetParent(path).FullName; while (chosenparent.Contains('\\')) { chosenparent = chosenparent.Replace('\\', '/'); } if (chosenparent != tilesetsfolder) { MessageBox box = new MessageBox("Error", "The selected file doesn't exist in the gfx/tilesets folder. Would you like to copy it in?", ButtonType.YesNo); box.OnButtonPressed += delegate(BaseEventArgs e) { if (box.Result == 0) // Yes { string newfilename = null; if (System.IO.File.Exists(tilesetsfolder + "/" + file_ext)) { int iterator = 1; while (string.IsNullOrEmpty(newfilename)) { if (!System.IO.File.Exists(tilesetsfolder + "/" + file + " (" + iterator.ToString() + ")." + dots[dots.Length - 1])) { newfilename = tilesetsfolder + "/" + file + " (" + iterator.ToString() + ")." + dots[dots.Length - 1]; file = file + " (" + iterator.ToString() + ")"; } iterator++; } } else { newfilename = tilesetsfolder + "/" + file_ext; } System.IO.File.Copy(path, newfilename); SetTilesetGraphic(file); } }; } // File is in tilesets folder else { SetTilesetGraphic(file); } } }; // Updates graphic if typed GraphicBox.TextArea.OnWidgetDeselected += delegate(BaseEventArgs e) { string file = GraphicBox.Text; if (!System.IO.File.Exists(Game.Data.ProjectPath + "/gfx/tilesets/" + file + ".png")) { new MessageBox("Error", "No tileset with the name '" + file + "' exists in gfx/tilesets.", IconType.Error); } else { SetTilesetGraphic(file); } }; ClearTilesetButton = new Button(SharedContainer); ClearTilesetButton.SetPosition(25, 150); ClearTilesetButton.SetSize(140, 44); ClearTilesetButton.SetFont(Font.Get("Fonts/Ubuntu-B", 16)); ClearTilesetButton.SetText("Clear Tileset"); ClearTilesetButton.OnClicked += delegate(BaseEventArgs e) { ConfirmClearTileset(); }; Submodes.SelectTab(1); }
public static void ImportMaps() { Setup(); OpenFileDialog of = new OpenFileDialog(); of.SetFilter(new FileFilter("RPG Maker XP Map", "rxdata")); of.SetTitle("Pick map(s)"); of.SetAllowMultiple(true); object ret = of.Show(); List <string> Files = new List <string>(); if (ret is string) { Files.Add(ret as string); } else if (ret is List <string> ) { Files = ret as List <string>; } else { return; // No files picked } for (int i = 0; i < Files.Count; i++) { while (Files[i].Contains('\\')) { Files[i] = Files[i].Replace('\\', '/'); } } string[] folders = Files[0].Split('/'); string parent = ""; string root = ""; for (int i = 0; i < folders.Length - 1; i++) { parent += folders[i]; if (i != folders.Length - 2) { root += folders[i]; } if (i != folders.Length - 2) { parent += '/'; } if (i != folders.Length - 3) { root += '/'; } } List <string> Names = new List <string>(); foreach (string f in Files) { string[] l = f.Split('/').Last().Split('.'); string n = ""; for (int i = 0; i < l.Length - 1; i++) { n += l[i]; if (i != l.Length - 2) { n += '.'; } } Names.Add(n); } // Load MapInfos.rxdata IntPtr infofile = Ruby.Funcall(Ruby.GetConst(Ruby.Object.Class, "File"), "open", Ruby.String.ToPtr(parent + "/MapInfos.rxdata"), Ruby.String.ToPtr("rb")); IntPtr infos = Ruby.Funcall(Ruby.GetConst(Ruby.Object.Class, "Marshal"), "load", infofile); Ruby.Pin(infos); IntPtr keys = Ruby.Funcall(infos, "keys"); Ruby.Pin(keys); Ruby.Funcall(infofile, "close"); // Load Tilesets.rxdata IntPtr tilesetfile = Ruby.Funcall(Ruby.GetConst(Ruby.Object.Class, "File"), "open", Ruby.String.ToPtr(parent + "/Tilesets.rxdata"), Ruby.String.ToPtr("rb")); IntPtr tilesets = Ruby.Funcall(Ruby.GetConst(Ruby.Object.Class, "Marshal"), "load", tilesetfile); Ruby.Pin(tilesets); Ruby.Funcall(tilesetfile, "close"); Action <int> ImportMap = null; ImportMap = delegate(int MapIndex) { // Convert rxdata (Ruby) to mkd (C#) string MapName = Names[MapIndex]; string file = Files[MapIndex]; while (file.Contains('\\')) { file = file.Replace('\\', '/'); } // Load Map.rxdata IntPtr f = Ruby.Funcall(Ruby.GetConst(Ruby.Object.Class, "File"), "open", Ruby.String.ToPtr(file), Ruby.String.ToPtr("rb")); IntPtr map = Ruby.Funcall(Ruby.GetConst(Ruby.Object.Class, "Marshal"), "load", f); Ruby.Pin(map); Ruby.Funcall(f, "close"); int id = Convert.ToInt32(file.Substring(file.Length - 10, 3)); // Link Map with its MapInfo IntPtr info = IntPtr.Zero; for (int i = 0; i < Ruby.Array.Length(keys); i++) { if (Ruby.Array.Get(keys, i) == Ruby.Integer.ToPtr(id)) { info = Ruby.Funcall(infos, "[]", Ruby.Array.Get(keys, i)); } } if (info == IntPtr.Zero) { throw new Exception($"No MapInfo could be found for map ({MapName})."); } Game.Map data = new Game.Map(); data.ID = Editor.GetFreeMapID(); data.DisplayName = MapInfo.Name(info); data.DevName = data.DisplayName; data.Width = Map.Width(map); data.Height = Map.Height(map); IntPtr tileset = Ruby.Array.Get(tilesets, Map.TilesetID(map)); Ruby.Pin(tileset); string tilesetname = Tileset.Name(tileset); Action cont = null; Game.Tileset existing = Data.Tilesets.Find(t => t != null && (t.Name == tilesetname || t.GraphicName == tilesetname)); bool exist = existing != null; string message = $"Importing Map ({MapName})...\n\n"; if (exist) { message += "The tileset of the imported map has the same name as an already-defined tileset in " + $"the database ({existing.Name}).\n" + "Would you like to use this tileset, choose a different one, or import it?"; } else { message += $"No tilesets similar to the one used in the imported map ({Tileset.Name(tileset)}) could be found.\n" + "Would you like to pick an existing tileset, or import it?"; } List <string> Options = new List <string>(); if (exist) { Options.Add("Use this"); } Options.Add("Pick other"); Options.Add("Import it"); MessageBox box = new MessageBox("Importing Map", message, Options); box.OnButtonPressed += delegate(BaseEventArgs e) { if (Options[box.Result] == "Use this") // Use the matched tileset { data.TilesetIDs = new List <int>() { existing.ID }; cont(); } else if (Options[box.Result] == "Pick other") // Pick other tileset { TilesetPicker picker = new TilesetPicker(null); picker.OnClosed += delegate(BaseEventArgs e) { if (picker.ChosenTilesetID > 0) // Chose tileset { data.TilesetIDs = new List <int>() { picker.ChosenTilesetID }; cont(); } else // Didn't pick tileset; cancel importing { data = null; Ruby.Unpin(tileset); Ruby.Unpin(map); MessageBox b = new MessageBox("Warning", $"Importing Map ({MapName})...\n\nAs no tileset was chosen, this map will not be imported.", IconType.Warning); b.OnButtonPressed += delegate(BaseEventArgs e) { if (MapIndex < Files.Count - 1) { ImportMap(MapIndex + 1); } }; } }; } else if (Options[box.Result] == "Import it") // Import the tileset { string filename = root + "/Graphics/Tilesets/" + Tileset.TilesetName(tileset) + ".png"; if (!File.Exists(filename)) // Graphic doesn't exist { MessageBox b = new MessageBox("Error", $"Importing Map ({MapName})...\n\nThe tileset graphic could not be found. The tileset cannot be imported, and thus this map will not be imported.", IconType.Error); b.OnButtonPressed += delegate(BaseEventArgs e) { if (MapIndex < Files.Count - 1) { ImportMap(MapIndex + 1); } }; } else // Graphic does exist { Bitmap bmp = new Bitmap(filename); int pw = bmp.Width / 32 * 33; int ph = bmp.Height / 32 * 33; if (pw > Graphics.MaxTextureSize.Width || ph > Graphics.MaxTextureSize.Height) { MessageBox b = new MessageBox("Error", $"Importing Map ({MapName})...\n\n" + $"The formatted tileset will exceed the maximum texture size ({Graphics.MaxTextureSize.Width},{Graphics.MaxTextureSize.Height}).\n" + "This map will not be imported." ); b.OnButtonPressed += delegate(BaseEventArgs e) { if (MapIndex < Files.Count - 1) { ImportMap(MapIndex + 1); } }; } else { string destination = Data.ProjectPath + "/gfx/tilesets/"; string name = Tileset.TilesetName(tileset); if (File.Exists(destination + Tileset.TilesetName(tileset) + ".png")) { int i = 0; do { i++; } while (File.Exists(destination + Tileset.TilesetName(tileset) + " (" + i.ToString() + ").png")); destination += Tileset.TilesetName(tileset) + " (" + i.ToString() + ").png"; name += " (" + i.ToString() + ")"; } else { destination += Tileset.TilesetName(tileset) + ".png"; } File.Copy(filename, destination); Game.Tileset set = new Game.Tileset(); set.Name = Tileset.Name(tileset); set.GraphicName = name; set.ID = Editor.GetFreeTilesetID(); int tilecount = 8 * bmp.Height / 32; set.Passabilities = new List <Passability>(); set.Priorities = new List <int?>(); set.Tags = new List <int?>(); for (int i = 0; i < tilecount; i++) { set.Passabilities.Add(Passability.All); set.Priorities.Add(0); set.Tags.Add(null); } Data.Tilesets[set.ID] = set; set.CreateBitmap(); if (Editor.MainWindow.DatabaseWidget != null) { Editor.MainWindow.DatabaseWidget.DBDataList.RefreshList(); } data.TilesetIDs = new List <int>() { set.ID }; cont(); } } } }; // Called whenever a choice has been made for tileset importing. cont = new Action(delegate() { if (data.TilesetIDs == null || data.TilesetIDs.Count == 0) // Should not be reachable { throw new Exception("Cannot continue without a tileset."); } data.Layers = new List <Layer>(); bool RemovedAutotiles = false; bool RemovedEvents = Ruby.Integer.FromPtr(Ruby.Funcall(Map.Events(map), "length")) > 0; IntPtr Tiles = Map.Data(map); int XSize = Table.XSize(Tiles); int YSize = Table.YSize(Tiles); int ZSize = Table.ZSize(Tiles); IntPtr tiledata = Table.Data(Tiles); for (int z = 0; z < ZSize; z++) { Layer layer = new Layer($"Layer {z + 1}"); for (int y = 0; y < YSize; y++) { for (int x = 0; x < XSize; x++) { int idx = x + y * XSize + z * XSize * YSize; int tileid = (int)Ruby.Integer.FromPtr(Ruby.Array.Get(tiledata, idx)); if (tileid < 384) { RemovedAutotiles = true; } if (tileid == 0) { layer.Tiles.Add(null); } else { layer.Tiles.Add(new TileData() { TileType = TileType.Tileset, Index = 0, ID = tileid - 384 }); } } } data.Layers.Add(layer); } Ruby.Unpin(map); Ruby.Unpin(tileset); Editor.AddMap(data); if (MapIndex < Files.Count - 1) { ImportMap(MapIndex + 1); } else { string Title = "Warning"; string Msg = ""; if (Files.Count > 1) { Msg = "The maps were imported successfully"; } else { Msg = "The map was imported successfully"; } if (RemovedEvents && RemovedAutotiles) { Msg += ", but all events and autotiles have been deleted as these have not yet been implemented in RPG Studio MK."; } else if (RemovedEvents) { Msg += ", but all events have been deleted as these have not yet been implemented in RPG Studio MK."; } else if (RemovedAutotiles) { Msg += ", but all autotiles have been deleted as these have not yet been implemented in RPG Studio MK."; } else { Title = "Success"; Msg += "."; } List <string> options = new List <string>(); if (Editor.ProjectSettings.LastMode != "MAPPING") { options.Add("Go to Map"); } options.Add("OK"); MessageBox box = new MessageBox(Title, Msg, options, IconType.Info); box.OnButtonPressed += delegate(BaseEventArgs e) { if (options[box.Result] == "Go to Map") // Go to map { Editor.SetMode("MAPPING"); Editor.MainWindow.MapWidget.MapSelectPanel.SetMap(data); } }; Ruby.Unpin(keys); Ruby.Unpin(infos); Ruby.Unpin(tileset); Cleanup(); } }); }; ImportMap(0); }