Пример #1
0
        /// <summary>
        /// Displays a file-editing tab.
        /// </summary>
        /// <typeparam name="T">The tab type to display.</typeparam>
        /// <param name="type">The type of file the tab edits (e.g. Textures, Sounds, ...).</param>
        /// <param name="extension">The file extension of the file type with a dot, but without a asterisk (e.g. .xml, .png, ...).</param>
        /// <param name="getInstance">A callback for creating an instance of the tab.</param>
        protected void OpenFileTab <T>(string type, string extension, Func <string, bool, T> getInstance) where T : Tab
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(type))
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (string.IsNullOrEmpty(extension))
            {
                throw new ArgumentNullException(nameof(extension));
            }
            if (getInstance == null)
            {
                throw new ArgumentNullException(nameof(getInstance));
            }
            #endregion

            // Get the file path
            string path;
            bool   overwrite;
            if (!FileSelectorDialog.TryGetPath(type, extension, out path, out overwrite))
            {
                return;
            }

            // ReSharper disable PossibleMultipleEnumeration
            var previousInstances = Tabs.Keys.OfType <T>().Where(x => x.FilePath == path);
            if (previousInstances.Any())
            {
                ShowTab(previousInstances.First());
            }
            else
            {
                AddTab(getInstance(path, overwrite));
            }
            // ReSharper restore PossibleMultipleEnumeration
        }
Пример #2
0
        /// <summary>
        /// Gets the file path for a game-content file
        /// </summary>
        /// <param name="type">The type of file you want (e.g. Textures, Sounds, ...)</param>
        /// <param name="extension">The file extension of the file type with a dot, but without a asterisk (e.g. .xml, .png, ...)</param>
        /// <param name="path">The absolute path to the requested content file</param>
        /// <param name="overwrite">Returns whether the user wants an existing file to be overwritten</param>
        /// <param name="allowNew">Allow the user to create a new file instead of opening an existing one?</param>
        /// <returns><c>true</c> if a file was selected, <c>false</c> if none was selected</returns>
        /// <exception cref="InvalidOperationException">The user didn't select a file.</exception>
        private static bool TryGetPath(string type, string extension, out string path, out bool overwrite, bool allowNew)
        {
            var selector = new FileSelectorDialog {
                _type = type, _extension = extension, buttonNew = { Enabled = allowNew }
            };

            // Display a nice text for the filetype
            string filetype = Path.GetFileName(type);

            if (filetype == "Meshes")
            {
                filetype = "Mesh";                       // Handle unusual plural form of Mesh
            }
            else if (filetype.EndsWith("s", StringComparison.Ordinal))
            {
                filetype = filetype.Remove(filetype.Length - 1, 1);                                                        // Remove plural "s"
            }
            selector.selectLabel.Text = string.Format(Resources.Select, filetype);

            // Create containing directory
            string directory;

            try
            {
                directory = ContentManager.CreateDirPath(type);
            }
            #region Error handling
            catch (IOException ex)
            {
                Msg.Inform(selector, Resources.InvalidDirectoryPath + "\n" + ex.Message, MsgSeverity.Warn);
                path      = null;
                overwrite = false;
                return(false);
            }
            catch (UnauthorizedAccessException ex)
            {
                Msg.Inform(selector, Resources.InvalidDirectoryPath + "\n" + ex.Message, MsgSeverity.Warn);
                path      = null;
                overwrite = false;
                return(false);
            }
            #endregion

            // Setup file dialogs
            selector.openFileDialog.InitialDirectory = selector.saveFileDialog.InitialDirectory = directory;
            selector.openFileDialog.Filter           = selector.saveFileDialog.Filter = string.Format("{0} (*{1})|*{1}", type, extension);

            // Display form
            selector.UpdateFileList();
            selector.ShowDialog();

            if (string.IsNullOrEmpty(selector._path))
            { // User canceled
                path      = null;
                overwrite = false;
                return(false);
            }

            // User has selected
            path      = selector._path;
            overwrite = selector._overwrite;
            return(true);
        }