From febb3c2f720678e077b2d23316e1bfa7664861a3 Mon Sep 17 00:00:00 2001 From: Ian C Date: Tue, 7 Jul 2020 21:49:44 +0000 Subject: Added cut/copy/paste. Hopefully fixed up code to get current view controller. --- SpriteEd/AppDelegate.cs | 71 +++++++++++++++++++++++++++++++++----- SpriteEd/NSSpriteEdit.cs | 5 +++ SpriteEd/ViewController.cs | 86 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 152 insertions(+), 10 deletions(-) diff --git a/SpriteEd/AppDelegate.cs b/SpriteEd/AppDelegate.cs index 3756e70..c213a25 100644 --- a/SpriteEd/AppDelegate.cs +++ b/SpriteEd/AppDelegate.cs @@ -24,34 +24,46 @@ namespace SpriteEd [Register("AppDelegate")] public class AppDelegate : NSApplicationDelegate { + private Sprite m_copy; + + /// + /// Default constructor. + /// public AppDelegate() { } + /// + /// Initialise the application/ + /// + /// The notification. public override void DidFinishLaunching(NSNotification notification) { - // Insert code here to initialize your application } + /// + /// Tear-down the application. + /// + /// The notification. public override void WillTerminate(NSNotification notification) { - // Insert code here to tear down your application } + /// + /// Get the current view controller. + /// private ViewController ViewController { get { - NSWindowController controller = NSApplication.SharedApplication.KeyWindow.WindowController as NSWindowController; - return controller.ContentViewController as ViewController; + return NSApplication.SharedApplication.KeyWindow.ContentViewController as ViewController; } } - [Export("newDocument:")] - private void OnNewDocument(NSObject sender) - { - } - + /// + /// Handle Save Document As. + /// + /// The event sender. [Export("saveDocumentAs:")] private void OnSaveDocumentAs(NSObject sender) { @@ -80,6 +92,10 @@ namespace SpriteEd } } + /// + /// Handle Save Document. + /// + /// The event sender. [Export("saveDocument:")] private void OnSaveDocument(NSObject sender) { @@ -104,6 +120,10 @@ namespace SpriteEd } } + /// + /// Handle Open Document. + /// + /// The event sender. [Export("openDocument:")] private void OnOpenDocument(NSObject sender) { @@ -136,5 +156,38 @@ namespace SpriteEd } } } + + /// + /// Handle Cut. + /// + /// The event sender. + [Export("cut:")] + private void OnCut(NSObject sender) + { + m_copy = ViewController.CutOrCopy(true); + } + + /// + /// Handle Copy. + /// + /// The event sender. + [Export("copy:")] + private void OnCopy(NSObject sender) + { + m_copy = ViewController.CutOrCopy(false); + } + + /// + /// Handle Paste. + /// + /// The event sender. + [Export("paste:")] + private void OnPaste(NSObject sender) + { + if (m_copy != null) + { + ViewController.Paste(m_copy); + } + } } } diff --git a/SpriteEd/NSSpriteEdit.cs b/SpriteEd/NSSpriteEdit.cs index f5d5dc5..8d9a41f 100644 --- a/SpriteEd/NSSpriteEdit.cs +++ b/SpriteEd/NSSpriteEdit.cs @@ -99,6 +99,11 @@ namespace SpriteEd ClearUndo(); NeedsDisplay = true; } + + get + { + return m_sprite; + } } /// diff --git a/SpriteEd/ViewController.cs b/SpriteEd/ViewController.cs index 43fdc64..588f9d8 100644 --- a/SpriteEd/ViewController.cs +++ b/SpriteEd/ViewController.cs @@ -22,14 +22,22 @@ namespace SpriteEd { public partial class ViewController : NSViewController { + private static int m_Untitled = 1; + private SpriteSet m_SpriteSet; private NSUrl m_Url; - private static int m_Untitled = 1; + /// + /// Constructor. + /// + /// The handle. public ViewController(IntPtr handle) : base(handle) { } + /// + /// Called when view loaded. + /// public override void ViewDidLoad() { base.ViewDidLoad(); @@ -43,6 +51,9 @@ namespace SpriteEd SpriteSet = new SpriteSet(SpriteSet.SetType.C64CharacterSet, 8, 8, palette, false, false, CodePoints.C64); } + /// + /// Called when view appears on screen. I think. + /// public override void ViewDidAppear() { base.ViewDidAppear(); @@ -63,6 +74,9 @@ namespace SpriteEd } } + /// + /// Get/set the represented object. + /// public override NSObject RepresentedObject { get @@ -76,6 +90,9 @@ namespace SpriteEd } } + /// + /// Get/set the SpriteSet being edited. + /// public SpriteSet SpriteSet { get @@ -99,6 +116,9 @@ namespace SpriteEd } } + /// + /// Get/set the file URL associated with the sprite set. + /// public NSUrl Url { get @@ -114,8 +134,15 @@ namespace SpriteEd } } + /// + /// Whether the view is untitled. + /// public bool Untitled {get;set;} + /// + /// Called when the sprite stepper is pressed. + /// + /// The event sender. partial void OnSpriteStepper(NSObject sender) { NSStepper stepper = sender as NSStepper; @@ -136,6 +163,10 @@ namespace SpriteEd } } + /// + /// Called when the colour stepper is pressed. + /// + /// The event sender. partial void OnColourStepper(NSObject sender) { NSStepper stepper = sender as NSStepper; @@ -152,12 +183,20 @@ namespace SpriteEd } } + /// + /// Called when the colour well is interacted with. + /// + /// The event sender. partial void OnColourWell(NSObject sender) { m_SpriteSet.Palette[(uint)m_ColourStepper.IntValue] = Util.ColourNS(m_ColourWell.Color); m_SpriteEdit.NeedsDisplay = true; } + /// + /// Called when the drawing mode pop-up button is interacted with. + /// + /// The event sender. partial void OnDrawingMode(NSObject sender) { NSPopUpButton button = sender as NSPopUpButton; @@ -167,5 +206,50 @@ namespace SpriteEd m_SpriteEdit.Mode = (NSSpriteEdit.DrawingMode)(int)button.SelectedTag; } } + + /// + /// Called to perform a cut or copy operation on the sprite. + /// + /// True if it's a cut. + /// The sprite data. + public Sprite CutOrCopy(bool cut) + { + Sprite copy = new Sprite(m_SpriteEdit.Sprite); + + if (cut) + { + for(uint x = 0; x < m_SpriteEdit.Sprite.Width; x++) + { + for(uint y = 0; y < m_SpriteEdit.Sprite.Height; y++) + { + m_SpriteEdit.Sprite[x,y] = 0; + } + } + + m_SpriteEdit.NeedsDisplay = true; + } + + return copy; + } + + /// + /// Called to perform a paste operation on the sprite from the passed sprite. + /// + /// The sprite to paste from. + public void Paste(Sprite paste) + { + uint mw = Math.Min(paste.Width, m_SpriteEdit.Sprite.Width); + uint mh = Math.Min(paste.Height, m_SpriteEdit.Sprite.Height); + + for(uint x = 0; x < mw; x++) + { + for(uint y = 0; y < mh; y++) + { + m_SpriteEdit.Sprite[x,y] = paste[x,y]; + } + } + + m_SpriteEdit.NeedsDisplay = true; + } } } -- cgit v1.2.3