/// <summary>Function to provide initialization for the plugin.</summary> /// <remarks>This method is only called when the plugin is loaded at startup.</remarks> protected override void OnInitialize() { ViewFactory.Register <ISpriteContent>(() => new SpriteEditorView()); // Get built-in codec list. _defaultCodec = new GorgonV3SpriteBinaryCodec(GraphicsContext.Renderer2D); SpriteEditorSettings settings = ContentPlugInService.ReadContentSettings <SpriteEditorSettings>(typeof(SpriteEditorPlugIn).FullName, new JsonSharpDxRectConverter()); if (settings != null) { _settings = settings; } _ddsCodec = new GorgonCodecDds(); using (var noImageStream = new MemoryStream(Resources.NoImage_256x256)) { _noImage = _ddsCodec.LoadFromStream(noImageStream); } _bgPattern = GorgonTexture2DView.CreateTexture(GraphicsContext.Graphics, new GorgonTexture2DInfo($"Sprite_Editor_Bg_Preview_{Guid.NewGuid():N}") { Width = EditorCommonResources.CheckerBoardPatternImage.Width, Height = EditorCommonResources.CheckerBoardPatternImage.Height }, EditorCommonResources.CheckerBoardPatternImage); _rtv = GorgonRenderTarget2DView.CreateRenderTarget(GraphicsContext.Graphics, new GorgonTexture2DInfo($"SpriteEditor_Rtv_Preview_{Guid.NewGuid():N}") { Width = 256, Height = 256, Format = BufferFormat.R8G8B8A8_UNorm, Binding = TextureBinding.ShaderResource }); }
/// <summary>Releases the unmanaged resources used by the <see cref="T:System.Windows.Forms.ApplicationContext" /> and optionally releases the managed resources.</summary> /// <param name="disposing"> /// <see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources. </param> protected override void Dispose(bool disposing) { AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; ToolPlugInService toolPlugIns = Interlocked.Exchange(ref _toolPlugIns, null); ContentPlugInService contentPlugIns = Interlocked.Exchange(ref _contentPlugIns, null); GraphicsContext context = Interlocked.Exchange(ref _graphicsContext, null); GorgonMefPlugInCache pluginCache = Interlocked.Exchange(ref _pluginCache, null); FormMain mainForm = Interlocked.Exchange(ref _mainForm, null); FormSplash splash = Interlocked.Exchange(ref _splash, null); toolPlugIns?.Dispose(); contentPlugIns?.Dispose(); context?.Dispose(); pluginCache?.Dispose(); mainForm?.Dispose(); splash?.Dispose(); base.Dispose(disposing); }
/// <summary> /// Initializes a new instance of the <see cref="ViewModelFactory"/> class. /// </summary> /// <param name="settings">The settings for the application.</param> /// <param name="providers">The providers used to open/save files.</param> /// <param name="contentPlugIns">The plugins used for content.</param> /// <param name="toolPlugIns">The plugins used for application tools.</param> /// <param name="projectManager">The application project manager.</param> /// <param name="viewModelInjection">The common services for the application.</param> /// <param name="clipboardService">The application clipboard service.</param> /// <param name="dirLocatorService">The directory locator service.</param> /// <param name="fileScanService">The file scanning service used to scan content files for content plugin associations.</param> /// <param name="folderBrowser">The file system folder browser service.</param> /// <exception cref="ArgumentNullException">Thrown when any parameter is <b>null</b>.</exception> public ViewModelFactory(EditorSettings settings, FileSystemProviders providers, ContentPlugInService contentPlugIns, ToolPlugInService toolPlugIns, ProjectManager projectManager, IViewModelInjection viewModelInjection, ClipboardService clipboardService, DirectoryLocateService dirLocatorService, FileScanService fileScanService, IEditorFileSystemFolderBrowseService folderBrowser) { Settings = settings ?? throw new ArgumentNullException(nameof(settings)); FileSystemProviders = providers ?? throw new ArgumentNullException(nameof(providers)); ContentPlugIns = contentPlugIns ?? throw new ArgumentNullException(nameof(contentPlugIns)); ToolPlugIns = toolPlugIns ?? throw new ArgumentNullException(nameof(toolPlugIns)); _projectManager = projectManager ?? throw new ArgumentNullException(nameof(projectManager)); _viewModelInjection = viewModelInjection ?? throw new ArgumentNullException(nameof(viewModelInjection)); _clipboard = clipboardService ?? throw new ArgumentNullException(nameof(clipboardService)); _dirLocator = dirLocatorService ?? throw new ArgumentNullException(nameof(dirLocatorService)); _fileScanService = fileScanService ?? throw new ArgumentNullException(nameof(fileScanService)); _folderBrowser = folderBrowser ?? throw new ArgumentNullException(nameof(folderBrowser)); }
/// <summary> /// Function to load any content plugins used to create/edit content. /// </summary> /// <returns>The content plugin manager service used to manipulate the loaded content plugins.</returns> private ContentPlugInService LoadContentPlugIns() { var contentPlugInsDir = new DirectoryInfo(Path.Combine(_plugInLocation.FullName, "Content")); var contentPlugInSettingsDir = new DirectoryInfo(Path.Combine(Program.ApplicationUserDirectory.FullName, "ContentPlugIns")); if (!contentPlugInSettingsDir.Exists) { contentPlugInSettingsDir.Create(); contentPlugInSettingsDir.Refresh(); } var contentPlugIns = new ContentPlugInService(contentPlugInSettingsDir, _graphicsContext, _folderBrowser, _commonServices); if (!contentPlugInsDir.Exists) { return(contentPlugIns); } try { _splash.InfoText = Resources.GOREDIT_TEXT_LOADING_CONTENT_PLUGINS; if (!contentPlugInsDir.Exists) { contentPlugInsDir.Create(); return(contentPlugIns); } contentPlugIns.LoadContentPlugIns(_pluginCache, contentPlugInsDir); } catch (Exception ex) { Program.Log.LogException(ex); GorgonDialogs.ErrorBox(_splash, Resources.GOREDIT_ERR_LOADING_PLUGINS, Resources.GOREDIT_ERR_ERROR, ex); } return(contentPlugIns); }
/// <summary>Function to provide clean up for the plugin.</summary> protected override void OnShutdown() { try { if (_settings != null) { // Persist any settings. ContentPlugInService.WriteContentSettings(typeof(SpriteEditorPlugIn).FullName, _settings, new JsonSharpDxRectConverter()); } } catch (Exception ex) { // We don't care if it crashes. The worst thing that'll happen is your settings won't persist. CommonServices.Log.LogException(ex); } _rtv?.Dispose(); _bgPattern?.Dispose(); _noImage?.Dispose(); ViewFactory.Unregister <ISpriteContent>(); base.OnShutdown(); }
/// <summary> /// Function to perform the boot strapping operation. /// </summary> /// <returns>The main application window.</returns> public async void BootStrap() { AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; try { // Get our initial context. SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext()); Program.Log.Print("Booting application...", LoggingLevel.All); Cursor.Current = Cursors.WaitCursor; await ShowSplashAsync(); // Initalize the common resources. EditorCommonResources.LoadResources(); _folderBrowser = new FileSystemFolderBrowseService(); _commonServices = new ViewModelInjection(Program.Log, new WaitCursorBusyState(), new MessageBoxService()); _pluginCache = new GorgonMefPlugInCache(Program.Log); _graphicsContext = GraphicsContext.Create(Program.Log); _plugInLocation = new DirectoryInfo(Path.Combine(GorgonApplication.StartupPath.FullName, "PlugIns")); if (!_plugInLocation.Exists) { Program.Log.Print($"[ERROR] Plug in path '{_plugInLocation.FullName}' was not found. No plug ins will be loaded.", LoggingLevel.Simple); GorgonDialogs.ErrorBox(null, Resources.GOREDIT_ERR_LOADING_PLUGINS); } // Get any application settings we might have. _settings = LoadSettings(); // Load our file system import/export plugins. FileSystemProviders fileSystemProviders = LoadFileSystemPlugIns(); // Load our tool plug ins. _toolPlugIns = LoadToolPlugIns(); // Load our content service plugins. _contentPlugIns = LoadContentPlugIns(); // Create the project manager for the application _projectManager = new ProjectManager(fileSystemProviders); _mainForm = new FormMain { Location = new Point(_settings.WindowBounds.Value.X, _settings.WindowBounds.Value.Y), Size = new Size(_settings.WindowBounds.Value.Width, _settings.WindowBounds.Value.Height), WindowState = FormWindowState.Normal }; await HideSplashAsync(); MainForm = _mainForm; var factory = new ViewModelFactory(_settings, fileSystemProviders, _contentPlugIns, _toolPlugIns, _projectManager, _commonServices, new ClipboardService(), new DirectoryLocateService(), new FileScanService(_contentPlugIns), _folderBrowser); FormWindowState windowState; // Ensure the window state values fall into an acceptable range. if (!Enum.IsDefined(typeof(FormWindowState), _settings.WindowState)) { windowState = FormWindowState.Maximized; } else { windowState = (FormWindowState)_settings.WindowState; } _mainForm.GraphicsContext = _graphicsContext; _mainForm.SetDataContext(factory.CreateMainViewModel(_graphicsContext.Graphics.VideoAdapter.Name)); _mainForm.Show(); _mainForm.WindowState = windowState; } finally { Cursor.Current = Cursors.Default; } }