From e3dac6b4380abfd689f388d796ad47af67cf357e Mon Sep 17 00:00:00 2001 From: RKrom Date: Wed, 26 Sep 2012 10:20:00 +0000 Subject: [PATCH] Refactoring of Surface.cs, mostly cosmetic! Removed public properties by creating get/set and made most fields NonSerializable so they won't be serialized when writing the surface. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2097 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Drawing/Surface.cs | 240 +++++++++++++++++++++++++++++------ 1 file changed, 203 insertions(+), 37 deletions(-) diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index f81f7a099..16806fb2c 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -50,36 +50,189 @@ namespace Greenshot.Drawing { public class Surface : PictureBox, ISurface { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(Surface)); private static CoreConfiguration conf = IniConfig.GetIniSection(); - public event SurfaceElementEventHandler MovingElementChanged; - public event SurfaceDrawingModeEventHandler DrawingModeChanged; - public event SurfaceSizeChangeEventHandler SurfaceSizeChanged; - public event SurfaceMessageEventHandler SurfaceMessage; + + /// + /// Event handlers (do not serialize!) + /// + [NonSerialized] + private SurfaceElementEventHandler movingElementChanged; + public event SurfaceElementEventHandler MovingElementChanged { + add { + movingElementChanged += value; + } + remove { + movingElementChanged -= value; + } + } + [NonSerialized] + private SurfaceDrawingModeEventHandler drawingModeChanged; + public event SurfaceDrawingModeEventHandler DrawingModeChanged { + add { + drawingModeChanged += value; + } + remove { + drawingModeChanged -= value; + } + } + [NonSerialized] + private SurfaceSizeChangeEventHandler surfaceSizeChanged; + public event SurfaceSizeChangeEventHandler SurfaceSizeChanged { + add { + surfaceSizeChanged += value; + } + remove { + surfaceSizeChanged -= value; + } + } + [NonSerialized] + private SurfaceMessageEventHandler surfaceMessage; + public event SurfaceMessageEventHandler SurfaceMessage { + add { + surfaceMessage += value; + } + remove { + surfaceMessage -= value; + } + } + + /// + /// inUndoRedo makes sure we don't undo/redo while in a undo/redo action + /// + [NonSerialized] private bool inUndoRedo = false; + + /// + /// Make only one surfacemove cycle undoable, see SurfaceMouseMove + /// + [NonSerialized] private bool isSurfaceMoveMadeUndoable = false; + + /// + /// Undo/Redo stacks, should not be serialized as the file would be way to big + /// + [NonSerialized] private Stack undoStack = new Stack(); + [NonSerialized] private Stack redoStack = new Stack(); + + /// + /// Last save location, do not serialize! + /// + [NonSerialized] private string lastSaveFullPath = null; - public FieldAggregator FieldAggregator = new FieldAggregator(); - - private ICaptureDetails captureDetails = null; - - private Bitmap buffer = null; + /// + /// current drawing mode, do not serialize! + /// + [NonSerialized] + private DrawingModes drawingMode = DrawingModes.None; - Point mouseStart = Point.Empty; + /// + /// the keyslocked flag helps with focus issues + /// + [NonSerialized] + private bool keysLocked = false; + + /// + /// Location of the mouse-down (it "starts" here), do not serialize + /// + [NonSerialized] + private Point mouseStart = Point.Empty; + + /// + /// are we in a mouse down, do not serialize + /// + [NonSerialized] private bool mouseDown = false; + + /// + /// are we dragging, do not serialize + /// + [NonSerialized] private bool draggingInProgress = false; + + /// + /// The selected element for the mouse down, do not serialize + /// + [NonSerialized] private DrawableContainer mouseDownElement = null; - private DrawableContainerList elements = new DrawableContainerList(); - + /// + /// all selected elements, do not serialize + /// + [NonSerialized] private DrawableContainerList selectedElements = new DrawableContainerList(); + + /// + /// the element we are drawing with, do not serialize + /// + [NonSerialized] private DrawableContainer drawingElement = null; + + /// + /// the element we want to draw with (not yet drawn), do not serialize + /// + [NonSerialized] private DrawableContainer undrawnElement = null; + + /// + /// the cropcontainer, when cropping this is set, do not serialize + /// + [NonSerialized] private DrawableContainer cropContainer = null; - private IDrawableContainer cursorContainer = null; + + /// + /// the brush which is used for transparent backgrounds, set by the editor, do not serialize + /// + [NonSerialized] private TextureBrush transparencyBackgroundBrush; + /// + /// The buffer is only for drawing on it, saving a lot of "create new bitmap" commands + /// Should not be serialized, as it's generated. + /// The actual bitmap is in the paintbox... + /// TODO: Check if this buffer is still needed! + /// + [NonSerialized] + private Bitmap buffer = null; + + /// + /// all elements on the surface, needed with serialization + /// + private DrawableContainerList elements = new DrawableContainerList(); + + /// + /// all elements on the surface, needed with serialization + /// + private FieldAggregator fieldAggregator = new FieldAggregator(); + + /// + /// the cursor container, needed with serialization as we need a direct acces to it. + /// + private IDrawableContainer cursorContainer = null; + + /// + /// the capture details, needed with serialization + /// + private ICaptureDetails captureDetails = null; + + /// + /// the modified flag specifies if the surface has had modifications after the last export. + /// Initial state is modified, as "it's not saved" + /// After serialization this should actually be "false" (the surface came from a stream) + /// For now we just serialize it... + /// + private bool modified = true; + + public FieldAggregator FieldAggregator { + get { + return fieldAggregator; + } + set { + fieldAggregator = value; + } + } + public IDrawableContainer CursorContainer { get { return cursorContainer; @@ -106,9 +259,15 @@ namespace Greenshot.Drawing { } } - public bool KeysLocked = false; - - private bool modified = true; // Initial state is modified, as it's not saved + public bool KeysLocked { + get { + return keysLocked; + } + set { + keysLocked = value; + } + } + public bool Modified { get { return modified; @@ -117,13 +276,14 @@ namespace Greenshot.Drawing { modified = value; } } - - private DrawingModes drawingMode = DrawingModes.None; + public DrawingModes DrawingMode { get {return drawingMode;} set { drawingMode = value; - DrawingModeChanged.Invoke(this, drawingMode); + if (drawingModeChanged != null) { + drawingModeChanged.Invoke(this, drawingMode); + } DeselectAllElements(); CreateUndrawnElement(); } @@ -571,7 +731,9 @@ namespace Greenshot.Drawing { MakeUndoable(new SurfaceBackgroundChangeMemento(this, new Point(left, top)), false); SetImage(newImage, false); Invalidate(); - SurfaceSizeChanged(this); + if (surfaceSizeChanged != null) { + surfaceSizeChanged(this); + } } /// @@ -591,7 +753,9 @@ namespace Greenshot.Drawing { MakeUndoable(new SurfaceBackgroundChangeMemento(this, offset), false); SetImage(newImage, false); Invalidate(); - SurfaceSizeChanged(this); + if (surfaceSizeChanged != null) { + surfaceSizeChanged(this); + } } /// @@ -660,8 +824,8 @@ namespace Greenshot.Drawing { MakeUndoable(new SurfaceBackgroundChangeMemento(this, offset), false); SetImage(newImage, false); Invalidate(); - if (SurfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, newImage.Size))) { - SurfaceSizeChanged(this); + if (surfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, newImage.Size))) { + surfaceSizeChanged(this); } } } finally { @@ -695,12 +859,12 @@ namespace Greenshot.Drawing { /// Type of message /// Message itself public void SendMessageEvent(object source, SurfaceMessageTyp messageType, string message) { - if (SurfaceMessage != null) { + if (surfaceMessage != null) { SurfaceMessageEventArgs eventArgs = new SurfaceMessageEventArgs(); eventArgs.Message = message; eventArgs.MessageType = messageType; eventArgs.Surface = this; - SurfaceMessage(source, eventArgs); + surfaceMessage(source, eventArgs); } } @@ -721,8 +885,8 @@ namespace Greenshot.Drawing { SetImage(tmpImage, false); elements.MoveBy(offset.X, offset.Y); - if (SurfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, tmpImage.Size))) { - SurfaceSizeChanged(this); + if (surfaceSizeChanged != null && !imageRectangle.Equals(new Rectangle(Point.Empty, tmpImage.Size))) { + surfaceSizeChanged(this); } Invalidate(); return true; @@ -733,8 +897,8 @@ namespace Greenshot.Drawing { public void UndoBackgroundChange(Image previous, Point offset) { SetImage(previous, false); elements.MoveBy(offset.X, offset.Y); - if (SurfaceSizeChanged != null) { - SurfaceSizeChanged(this); + if (surfaceSizeChanged != null) { + surfaceSizeChanged(this); } Invalidate(); } @@ -944,8 +1108,8 @@ namespace Greenshot.Drawing { } } if (buffer == null) { + buffer = ImageHelper.CreateEmpty(Image.Width, Image.Height, Image.PixelFormat, Color.Empty, Image.HorizontalResolution, Image.VerticalResolution); LOG.DebugFormat("Created buffer with size: {0}x{1}", Image.Width, Image.Height); - buffer = new Bitmap(Image.Width, Image.Height, Image.PixelFormat); } // Elements might need the bitmap, so we copy the part we need using (Graphics graphics = Graphics.FromImage(buffer)) { @@ -1045,7 +1209,9 @@ namespace Greenshot.Drawing { RemoveElement(element, true); } selectedElements.Clear(); - MovingElementChanged(this, selectedElements); + if (movingElementChanged != null) { + movingElementChanged(this, selectedElements); + } } } @@ -1135,8 +1301,8 @@ namespace Greenshot.Drawing { element.Selected = false; selectedElements.Remove(element); FieldAggregator.UnbindElement(element); - if (MovingElementChanged != null) { - MovingElementChanged(this, selectedElements); + if (movingElementChanged != null) { + movingElementChanged(this, selectedElements); } } @@ -1150,8 +1316,8 @@ namespace Greenshot.Drawing { selectedElements.Remove(element); FieldAggregator.UnbindElement(element); } - if (MovingElementChanged != null) { - MovingElementChanged(this, selectedElements); + if (movingElementChanged != null) { + movingElementChanged(this, selectedElements); } } } @@ -1163,8 +1329,8 @@ namespace Greenshot.Drawing { element.ShowGrippers(); element.Selected = true; FieldAggregator.BindElement(element); - if (MovingElementChanged != null) { - MovingElementChanged(this, selectedElements); + if (movingElementChanged != null) { + movingElementChanged(this, selectedElements); } element.Invalidate(); }