Пример #1
0
        /// <summary>
        /// If this inputbox is type AddTileset, the accept click must check to
        /// see if a descriptor has been created already with the CreateMap
        /// button first.
        ///
        /// If this inputbox is type EditTileset, the accept click will create a
        /// descriptor if the tileset-label changed and delete the old
        /// descriptor, and add the new one to the current tilegroup/category.
        /// If the tileset-label didn't change, nothing more need be done since
        /// any terrains that were changed have already been changed by changes
        /// to the Allocated/Available listboxes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnAcceptClick(object sender, EventArgs e)
        {
            //LogFile.WriteLine("");
            //LogFile.WriteLine("OnAcceptClick");
            //LogFile.WriteLine(". Tileset= " + Tileset);

            switch (InputBoxType)
            {
            case BoxType.EditTileset:
                //LogFile.WriteLine(". . MODE:EditTileset");

                if (String.IsNullOrEmpty(Tileset))
                {
                    //LogFile.WriteLine(". The Map label cannot be blank.");
                    ShowErrorDialog("The Map label cannot be blank.");

                    tbTileset.Select();
                }
                else if (lbTerrainsAllocated.Items.Count == 0)
                {
                    //LogFile.WriteLine(". The Map must have at least one terrain allocated.");
                    ShowErrorDialog("The Map must have at least one terrain allocated.");
                }
                else
                {
                    //foreach (string t in TerrainsOriginal)
                    //	LogFile.WriteLine(". terrain original= " + t);
                    //foreach (string t in Descriptor.Terrains)
                    //	LogFile.WriteLine(". terrain= " + t);

                    if (Tileset == TilesetOriginal)                             // label didn't change; check if terrains changed ->
                    {
                        //LogFile.WriteLine(". . label did *not* change");

                        if (Descriptor.Terrains.SequenceEqual(TerrainsOriginal))
                        {
                            //LogFile.WriteLine(". . . No changes were made.");
                            ShowInfoDialog("No changes were made.");
                        }
                        else
                        {
                            //LogFile.WriteLine(". . . DialogResult OK");
                            DialogResult = DialogResult.OK;
                        }

                        // NOTE: a Save of Map-file is *not* required here.
                    }
                    else                             // label changed; rewrite the descriptor ->
                    {
                        //LogFile.WriteLine(". . change label");

                        // NOTE: user cannot edit a Map-label to be another already existing file.
                        // There are other ways to do that: either let the user delete the target-
                        // Map-file from his/her disk, or better click Edit on *that* tileset.
                        // NOTE: however, if while editing a tileset the user browses to another
                        // tileset and edits that tileset's terrains, the changes are effective.

                        if (MapFileExists(Tileset))
                        {
                            //LogFile.WriteLine(". The Map file already exists on disk.");
                            ShowErrorDialog("The Map file already exists on disk. The Tileset Editor is"
                                            + " not sophisticated enough to deal with this eventuality."
                                            + " Either edit that Map directly if it's already in the Maptree,"
                                            + " or use Add Tileset to make it editable, or as a last"
                                            + " resort delete it from your disk."
                                            + Environment.NewLine + Environment.NewLine
                                            + GetFullPathMap(Tileset));
                            // TODO: Ask user if he/she wants to overwrite the Map-file.
                        }
                        else
                        {
                            if (IsTilesetInGroups(Tileset))
                            {
                                //LogFile.WriteLine(". The tileset already exists in the Maptree.");
                                ShowErrorDialog("The tileset already exists in the Maptree."
                                                + Environment.NewLine + Environment.NewLine
                                                + Tileset
                                                + Environment.NewLine + Environment.NewLine
                                                + "Options are to edit that one, delete that one,"
                                                + " or choose a different label for this one.");
                            }
                            else
                            {
                                //LogFile.WriteLine(". . . tileset Created");

                                string pfeMap    = GetFullPathMap(Tileset);
                                string pfeMapPre = GetFullPathMap(TilesetOriginal);

                                //LogFile.WriteLine(". . . . fileMapPre= " + pfeMapPre);
                                //LogFile.WriteLine(". . . . fileMap= " + pfeMap);

                                File.Move(pfeMapPre, pfeMap);                                           // NOTE: This has to happen now because once the MapTree node
                                // is selected it will try to load the .MAP file etc.

                                if (File.Exists(pfeMap))                                                        // NOTE: do *not* alter the descriptor if File.Move() went bork.
                                {                                                                               // This is likely redundant: File.Move() should throw.
                                    string pfeRoutes    = GetFullPathRoutes(Tileset);
                                    string pfeRoutesPre = GetFullPathRoutes(TilesetOriginal);

                                    //LogFile.WriteLine(". . . . fileRoutesPre= " + pfeRoutesPre);
                                    //LogFile.WriteLine(". . . . fileRoutes= " + pfeRoutes);

                                    File.Move(pfeRoutesPre, pfeRoutes);

                                    var category = TileGroup.Categories[Category];
                                    Descriptor = new Descriptor(
                                        Tileset,
                                        category[TilesetOriginal].Terrains,
                                        BasePath,
                                        TileGroup.Pal);
                                    TileGroup.AddTileset(Descriptor, Category);                                                         // NOTE: This could be done on return to XCMainWindow.OnEditTilesetClick()
                                    // but then 'Descriptor' would have to be internal.
                                    TileGroup.DeleteTileset(TilesetOriginal, Category);                                                 // NOTE: This could be done on return to XCMainWindow.OnEditTilesetClick()
                                    // but then 'TilesetOriginal' would have to be internal.

                                    DialogResult = DialogResult.OK;
                                }
                            }
                        }
                    }
                }
                break;

            case BoxType.AddTileset:
                //LogFile.WriteLine(". . MODE:AddTileset");

                if (String.IsNullOrEmpty(Tileset))                                                                      // NOTE: The tileset-label should already have been
                {                                                                                                       // checked for validity by here before the Create button.
                    //LogFile.WriteLine(". The Map label cannot be blank.");	// But these handle the case when user immediately clicks the Ok button.
                    ShowErrorDialog("The Map label cannot be blank.");                                                  // ... TODO: so disable the Ok button, unless a descriptor is valid

                    tbTileset.Select();
                }
                else if (lbTerrainsAllocated.Items.Count == 0)
                {
                    //LogFile.WriteLine(". The Map must have at least one terrain allocated.");
                    ShowErrorDialog("The Map must have at least one terrain allocated.");
                }
                else
                {
                    switch (FileAddType)
                    {
                    case AddType.MapExists:
                        //LogFile.WriteLine(". . Map file EXISTS");

                        TileGroup.AddTileset(Descriptor, Category);
                        DialogResult = DialogResult.OK;
                        break;

                    case AddType.MapCreate:
                        //LogFile.WriteLine(". . Map file does NOT exist - Create new Map file");

                        string pfeMap = GetFullPathMap(Tileset);
                        //LogFile.WriteLine(". . . fileMap= " + pfeMap);

                        string pfeRoutes = GetFullPathRoutes(Tileset);
                        //LogFile.WriteLine(". . . fileRoutes= " + pfeRoutes);

                        Directory.CreateDirectory(Path.GetDirectoryName(pfeRoutes));
                        using (var fs = File.Create(pfeRoutes))                                         // create a blank Route-file and release its handle.
                        {}

                        Directory.CreateDirectory(Path.GetDirectoryName(pfeMap));
                        using (var fs = File.Create(pfeMap))                                            // create the Map-file and release its handle.
                        {                                                                               // NOTE: This has to happen now because once the MapTree node
                            MapFileChild.CreateMap(                                                     // is selected it will try to load the .MAP file etc.
                                fs,
                                10, 10, 1);                                                             // <- default new Map size
                        }

                        if (File.Exists(pfeMap) && File.Exists(pfeRoutes))                                         // NOTE: The descriptor has already been created with the Create descriptor button.
                        {
                            //LogFile.WriteLine(". tileset Created");

                            TileGroup.AddTileset(Descriptor, Category);
                            DialogResult = DialogResult.OK;
                        }
                        break;
                    }
                }
                break;
            }
        }