Пример #1
0
        /// <summary>
        /// Attempts to load a mapand bring up a map form for
        /// it. Function will fail if the map is corrupt and
        /// display an error message. Provide a null string to
        /// show openfileDialog. Returns a refernce to the created mapform
        /// if needed
        /// </summary>
        /// <param name="mapFileName">The map File Name.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public MapForm TryLoadMapForm(string mapFileName)
        {
            // Only allow for 10 map forms to be open at any one time, for
            // memory reasons
            if (MapCount >= 10)
            {
                MessageBox.Show("Too many maps open, close a map.");
                return null;
            }

            // show a dialog if null filename
            if (mapFileName == null)
            {
                openmapdialog.InitialDirectory = Prefs.pathMapsFolder;
                // if the user cancels out the dialog, no map to open
                if (openmapdialog.ShowDialog() == DialogResult.Cancel)
                {
                    return null;
                }
                Prefs.pathMapsFolder = openmapdialog.InitialDirectory;

                // set the file name to user chosen file
                mapFileName = openmapdialog.FileName;
            }

            // Check map isn't already loaded into a map form
            foreach (MapForm mapForm in this.MdiChildren)
            {
                if (mapForm.map.filePath == mapFileName)
                {
                    MessageBox.Show("This map is already open in this editor!..");
                    return mapForm;
                }
            }

            // Show a wait cursor while map is loading
            this.Cursor = Cursors.WaitCursor;

            // Attempt to load the map
            Map newMap = Map.LoadFromFile(mapFileName);
            // Restore our arrow cursor after map is loaded
            this.Cursor = Cursors.Arrow;
            // map failed to load for some reason
            if (newMap == null)
            {
                MessageBox.Show("Map failed to load...");
                return null;
            }

            // Show a wait cursor while map is loading
            this.Cursor = Cursors.WaitCursor;
            // create a map form for the map
            MapForm newMapForm = new MapForm(newMap);
            // Restore our arrow cursor after map is loaded
            this.Cursor = Cursors.Arrow;

            // set it to a child of this main form
            newMapForm.MdiParent = this;

            // keep track of open maps
            MapCount++;
            if (MapCount > 0)
            {
                closemapmenu.Enabled = true;
            }

            // store map in recent list
            UpdateRecentFiles(mapFileName);

            // give the form a close event
            newMapForm.FormClosed += newMapForm_FormClosed;
            // show form
            newMapForm.Show();

            return newMapForm;
        }