diff options
author | Ian C <ianc@noddybox.co.uk> | 2020-07-12 20:16:41 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2020-07-12 20:16:41 +0000 |
commit | 953e5de5bf59c1a453d059fc813cbaf50d8c0f77 (patch) | |
tree | 90224853f993ef11e45821d6450eb1d79adb191b | |
parent | febb3c2f720678e077b2d23316e1bfa7664861a3 (diff) |
Added closing window checks. Also moved Save code to ViewController.
-rw-r--r-- | SpriteEd/AppDelegate.cs | 60 | ||||
-rw-r--r-- | SpriteEd/NSSpriteEdit.cs | 20 | ||||
-rw-r--r-- | SpriteEd/NSSpriteEditWindowDelegate.cs | 87 | ||||
-rw-r--r-- | SpriteEd/ViewController.cs | 75 |
4 files changed, 176 insertions, 66 deletions
diff --git a/SpriteEd/AppDelegate.cs b/SpriteEd/AppDelegate.cs index c213a25..1969e7e 100644 --- a/SpriteEd/AppDelegate.cs +++ b/SpriteEd/AppDelegate.cs @@ -67,29 +67,7 @@ namespace SpriteEd [Export("saveDocumentAs:")] private void OnSaveDocumentAs(NSObject sender) { - ViewController view = ViewController; - - NSSavePanel fsel = new NSSavePanel(); - - fsel.Title = "Save Sprite Set"; - fsel.AllowedFileTypes = new string[] {"sprset"}; - fsel.ShowsTagField = false; - - if (fsel.RunModal() == 1) - { - view.Url = fsel.Url; - - try - { - FileStream stream = File.Create(view.Url.Path); - view.SpriteSet.Save(stream); - stream.Close(); - } - catch (Exception e) - { - Util.DisplayAlert("Error saving sprite set", e.Message); - } - } + ViewController.SaveAs(); } /// <summary> @@ -103,20 +81,11 @@ namespace SpriteEd if (view.Untitled) { - OnSaveDocumentAs(sender); + view.SaveAs(); } else { - try - { - FileStream stream = File.Create(view.Url.Path); - view.SpriteSet.Save(stream); - stream.Close(); - } - catch (Exception e) - { - Util.DisplayAlert("Error saving sprite set", e.Message); - } + view.Save(); } } @@ -158,6 +127,29 @@ namespace SpriteEd } /// <summary> + /// Handle Revert Document To Saved. + /// </summary> + /// <param name="sender">The event sender.</param> + [Export("revertDocumentToSaved:")] + private void OnRevertDocumentToSaved(NSObject sender) + { + ViewController view = ViewController; + + try + { + FileStream stream = File.OpenRead(view.Url.Path); + SpriteSet set = SpriteSet.Load(stream); + stream.Close(); + + view.SpriteSet = set; + } + catch (Exception e) + { + Util.DisplayAlert("Error reverting sprite set", e.Message); + } + } + + /// <summary> /// Handle Cut. /// </summary> /// <param name="sender">The event sender.</param> diff --git a/SpriteEd/NSSpriteEdit.cs b/SpriteEd/NSSpriteEdit.cs index 8d9a41f..53e9ab3 100644 --- a/SpriteEd/NSSpriteEdit.cs +++ b/SpriteEd/NSSpriteEdit.cs @@ -132,6 +132,24 @@ namespace SpriteEd public bool DoubleHeight {get; set;} /// <summary> + /// Event called when sprite edited. + /// </summary> + public event EventHandler SpriteEdited; + + /// <summary> + /// Calls SpriteEdited event. + /// </summary> + public void OnSpriteEdited(EventArgs e) + { + EventHandler handler = SpriteEdited; + + if (handler != null) + { + handler(this, e); + } + } + + /// <summary> /// Default constructor /// </summary> public NSSpriteEdit() @@ -253,6 +271,8 @@ namespace SpriteEd // but this seems to cause a crash deep in the runtime. // del.UndoManager.RegisterUndo(this, Undo); + + OnSpriteEdited(new EventArgs()); } } diff --git a/SpriteEd/NSSpriteEditWindowDelegate.cs b/SpriteEd/NSSpriteEditWindowDelegate.cs index d0cf4e0..eb1579b 100644 --- a/SpriteEd/NSSpriteEditWindowDelegate.cs +++ b/SpriteEd/NSSpriteEditWindowDelegate.cs @@ -22,42 +22,21 @@ namespace SpriteEd { public class NSSpriteEditWindowDelegate : NSWindowDelegate { - private NSUndoManager m_manager; - /// <summary> - /// Default constructor. + /// Get the undo manager /// </summary> - public NSSpriteEditWindowDelegate() : base() - { - Init(); - } + public NSUndoManager UndoManager {get; private set;} - /// <summary> - /// Construct given handle. - /// </summary> - /// <param name="handle">The handle.</param> - public NSSpriteEditWindowDelegate(IntPtr handle) : base(handle) - { - Init(); - } + private NSWindow Window {get; set;} /// <summary> - /// Initialise the delegate. + /// Construct given a window. /// </summary> - private void Init() + /// <param name="window">The window.</param> + public NSSpriteEditWindowDelegate(NSWindow window) { - m_manager = new NSUndoManager(); - } - - /// <summary> - /// Get the Undo Manager. - /// </summary> - public NSUndoManager UndoManager - { - get - { - return m_manager; - } + UndoManager = new NSUndoManager(); + Window = window; } /// <summary> @@ -78,9 +57,55 @@ namespace SpriteEd { get { - NSWindowController controller = NSApplication.SharedApplication.KeyWindow.WindowController as NSWindowController; - return controller.ContentViewController as ViewController; + return Window.ContentViewController as ViewController; + } + } + + [Export("windowShouldClose:")] + public override bool WindowShouldClose(NSObject sender) + { + bool close = false; + + if (Window.DocumentEdited) + { + NSAlert alert = new NSAlert () + { + AlertStyle = NSAlertStyle.Critical, + InformativeText = "Save changes to document before closing window?", + MessageText = "Save Document", + }; + + alert.AddButton ("Save"); + alert.AddButton ("Delete"); + alert.AddButton ("Cancel"); + + switch (alert.RunModal()) + { + case 1000: + if (ViewController.Untitled) + { + close = ViewController.SaveAs(); + } + else + { + close = ViewController.Save(); + } + break; + + case 1001: + close = true; + break; + + default: + break; + } } + else + { + close = true; + } + + return close; } } } diff --git a/SpriteEd/ViewController.cs b/SpriteEd/ViewController.cs index 588f9d8..d555dd1 100644 --- a/SpriteEd/ViewController.cs +++ b/SpriteEd/ViewController.cs @@ -15,6 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // using System; +using System.IO; using AppKit; using Foundation; @@ -49,6 +50,8 @@ namespace SpriteEd Untitled = true; SpriteSet = new SpriteSet(SpriteSet.SetType.C64CharacterSet, 8, 8, palette, false, false, CodePoints.C64); + + m_SpriteEdit.SpriteEdited += SpriteEdited; } /// <summary> @@ -58,7 +61,7 @@ namespace SpriteEd { base.ViewDidAppear(); - View.Window.Delegate = new NSSpriteEditWindowDelegate(); + View.Window.Delegate = new NSSpriteEditWindowDelegate(View.Window); if (Untitled) { @@ -251,5 +254,75 @@ namespace SpriteEd m_SpriteEdit.NeedsDisplay = true; } + + /// <summary> + /// Event to handle sprite edited. + /// </summary> + /// <param name="o">The object raising the event.</param> + /// <param name="e">The event arguments.</param> + private void SpriteEdited(object o, EventArgs e) + { + View.Window.DocumentEdited = true; + } + + /// <summary> + /// Select a file name and save the current sprite set. + /// </summary> + /// <returns>True if the sprite set was saved.</returns> + public bool SaveAs() + { + bool ok = false; + + NSSavePanel fsel = new NSSavePanel(); + + fsel.Title = "Save Sprite Set"; + fsel.AllowedFileTypes = new string[] {"sprset"}; + fsel.ShowsTagField = false; + + if (fsel.RunModal() == 1) + { + Url = fsel.Url; + + try + { + FileStream stream = File.Create(Url.Path); + SpriteSet.Save(stream); + stream.Close(); + ok = true; + } + catch (Exception e) + { + Util.DisplayAlert("Error saving sprite set", e.Message); + } + } + + return ok; + } + + /// <summary> + /// Save the current sprite set. + /// </summary> + /// <returns>True if the sprite set was saved.</returns> + public bool Save() + { + bool ok = false; + + try + { + if (Url != null) + { + FileStream stream = File.Create(Url.Path); + SpriteSet.Save(stream); + stream.Close(); + ok = true; + } + } + catch (Exception e) + { + Util.DisplayAlert("Error saving sprite set", e.Message); + } + + return ok; + } } } |