From fb993c223831c9d5b76b28b8ac93fc5f6cab7aaa Mon Sep 17 00:00:00 2001 From: RKrom Date: Thu, 12 Jun 2014 12:06:52 +0200 Subject: [PATCH] Fixed serialization issues with the StepLabelContainer and the SpeechbubbleContainer, they should now be in the correct shape/location when loading from the .greenshot file. --- Greenshot/Drawing/SpeechbubbleContainer.cs | 30 +- Greenshot/Drawing/StepLabelContainer.cs | 323 +++++++++++---------- Greenshot/Drawing/Surface.cs | 15 +- GreenshotPlugin/Interfaces/Generic.cs | 4 - 4 files changed, 213 insertions(+), 159 deletions(-) diff --git a/Greenshot/Drawing/SpeechbubbleContainer.cs b/Greenshot/Drawing/SpeechbubbleContainer.cs index d21eae804..cf4715d6e 100644 --- a/Greenshot/Drawing/SpeechbubbleContainer.cs +++ b/Greenshot/Drawing/SpeechbubbleContainer.cs @@ -27,6 +27,7 @@ using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; +using System.Runtime.Serialization; using System.Windows.Forms; namespace Greenshot.Drawing { @@ -35,7 +36,34 @@ namespace Greenshot.Drawing { /// [Serializable] public class SpeechbubbleContainer : TextContainer { - public SpeechbubbleContainer(Surface parent) : base(parent) { + + #region TargetGripper serializing code + // Only used for serializing the TargetGripper location + private Point _storedTargetGripperLocation; + + /// + /// Store the current location of the target gripper + /// + /// + [OnSerializing] + private void SetValuesOnSerializing(StreamingContext context) { + if (TargetGripper != null) { + _storedTargetGripperLocation = TargetGripper.Location; + } + } + + /// + /// Restore the target gripper + /// + /// + [OnDeserialized] + private void SetValuesOnDeserialized(StreamingContext context) { + InitTargetGripper(Color.Green, _storedTargetGripperLocation); + } + #endregion + + public SpeechbubbleContainer(Surface parent) + : base(parent) { } /// diff --git a/Greenshot/Drawing/StepLabelContainer.cs b/Greenshot/Drawing/StepLabelContainer.cs index 429d747c9..a33d6589c 100644 --- a/Greenshot/Drawing/StepLabelContainer.cs +++ b/Greenshot/Drawing/StepLabelContainer.cs @@ -1,160 +1,187 @@ -/* - * Greenshot - a free and open source screenshot tool - * Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom - * - * For more information see: http://getgreenshot.org/ - * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 1 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -using Greenshot.Drawing.Fields; -using Greenshot.Helpers; -using Greenshot.Plugin; -using Greenshot.Plugin.Drawing; -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Drawing.Text; -using System.Runtime.Serialization; - -namespace Greenshot.Drawing { - /// - /// Description of StepLabelContainer. - /// This is an enumerated label, every single StepLabelContainer shows the number of the order it was created. - /// To make sure that deleting recalculates, we check the location before every draw. - /// - [Serializable] - public class StepLabelContainer : DrawableContainer { - [NonSerialized] - private StringFormat _stringFormat = new StringFormat(); - - private readonly bool _drawAsRectangle = false; - - public StepLabelContainer(Surface parent) : base(parent) { - parent.AddStepLabel(this); - InitContent(); - } - - [OnDeserialized] - private void OnDeserialized(StreamingContext context) { - _stringFormat = new StringFormat(); - _stringFormat.Alignment = StringAlignment.Center; - _stringFormat.LineAlignment = StringAlignment.Center; +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +using Greenshot.Drawing.Fields; +using Greenshot.Helpers; +using Greenshot.Plugin; +using Greenshot.Plugin.Drawing; +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Text; +using System.Runtime.Serialization; + +namespace Greenshot.Drawing { + /// + /// Description of StepLabelContainer. + /// This is an enumerated label, every single StepLabelContainer shows the number of the order it was created. + /// To make sure that deleting recalculates, we check the location before every draw. + /// + [Serializable] + public class StepLabelContainer : DrawableContainer { + [NonSerialized] + private StringFormat _stringFormat = new StringFormat(); + + private readonly bool _drawAsRectangle = false; + + public StepLabelContainer(Surface parent) : base(parent) { + parent.AddStepLabel(this); + InitContent(); + } + + #region Number serializing + // Used to store the number of this label, so when deserializing it can be placed back to the StepLabels list in the right location + private int _number; + public int Number { + get { + return _number; + } + set { + _number = value; + } } /// - /// Make sure the StepLabel is addded to the parent after deserializing - /// by removing it from the current parent and added it to the new + /// Retrieve the counter before serializing + /// + /// + [OnSerializing] + private void SetValuesOnSerializing(StreamingContext context) { + if (Parent != null) { + Number = ((Surface)Parent).CountStepLabels(this); + } + } + #endregion + + /// + /// Restore values that don't serialize + /// + /// + [OnDeserialized] + private void SetValuesOnDeserialized(StreamingContext context) { + _stringFormat = new StringFormat(); + _stringFormat.Alignment = StringAlignment.Center; + _stringFormat.LineAlignment = StringAlignment.Center; + } + + /// + /// Add the StepLabel to the parent /// /// protected override void SwitchParent(Surface newParent) { if (Parent != null) { - Parent.RemoveStepLabel(this); + ((Surface)Parent).RemoveStepLabel(this); } base.SwitchParent(newParent); - if (Parent != null) { - Parent.AddStepLabel(this); + if (newParent != null) { + ((Surface)Parent).AddStepLabel(this); } } - public override Size DefaultSize { - get { - return new Size(30, 30); - } - } - - public override bool InitContent() { - _defaultEditMode = EditStatus.IDLE; - _stringFormat.Alignment = StringAlignment.Center; - _stringFormat.LineAlignment = StringAlignment.Center; - - // Set defaults - Width = DefaultSize.Width; - Height = DefaultSize.Height; - - return true; - } - - /// - /// This makes it possible for the label to be placed exactly in the middle of the pointer. - /// - public override bool HandleMouseDown(int mouseX, int mouseY) { - return base.HandleMouseDown(mouseX - (Width / 2), mouseY - (Height / 2)); - } - - /// - /// We set our own field values - /// - protected override void InitializeFields() { - AddField(GetType(), FieldType.FILL_COLOR, Color.DarkRed); - AddField(GetType(), FieldType.LINE_COLOR, Color.White); - } - - /// - /// Make sure this element is no longer referenced from the surface - /// - public override void Dispose() { - Parent.RemoveStepLabel(this); - base.Dispose(); - } - - public override bool HandleMouseMove(int x, int y) { - Invalidate(); - Left = x - (Width / 2); - Top = y - (Height / 2); - Invalidate(); - return true; - } - - /// - /// Override the parent, calculate the label number, than draw - /// - /// - /// - public override void Draw(Graphics graphics, RenderMode rm) { - graphics.SmoothingMode = SmoothingMode.HighQuality; - graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; - graphics.CompositingQuality = CompositingQuality.HighQuality; - graphics.PixelOffsetMode = PixelOffsetMode.None; - graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; - string text = Parent.CountStepLabels(this).ToString(); - Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); - Color fillColor = GetFieldValueAsColor(FieldType.FILL_COLOR); - Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR); - if (_drawAsRectangle) { - RectangleContainer.DrawRectangle(rect, graphics, rm, 0, Color.Transparent, fillColor, false); - } else { - EllipseContainer.DrawEllipse(rect, graphics, rm, 0, Color.Transparent, fillColor, false); - } - using (FontFamily fam = new FontFamily(FontFamily.GenericSansSerif.Name)) { - float factor = (((float)rect.Width / DefaultSize.Width) + ((float)rect.Height / DefaultSize.Height)) / 2; - using (Font _font = new Font(fam, 16 * factor, FontStyle.Bold, GraphicsUnit.Pixel)) { - TextContainer.DrawText(graphics, rect, 0, lineColor, false, _stringFormat, text, _font); - } - } - } - - public override bool ClickableAt(int x, int y) { - Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); - Color fillColor = GetFieldValueAsColor(FieldType.FILL_COLOR); - if (_drawAsRectangle) { - return RectangleContainer.RectangleClickableAt(rect, 0, fillColor, x, y); - } else { - return EllipseContainer.EllipseClickableAt(rect, 0, fillColor, x, y); - } - } - } -} + public override Size DefaultSize { + get { + return new Size(30, 30); + } + } + + public override bool InitContent() { + _defaultEditMode = EditStatus.IDLE; + _stringFormat.Alignment = StringAlignment.Center; + _stringFormat.LineAlignment = StringAlignment.Center; + + // Set defaults + Width = DefaultSize.Width; + Height = DefaultSize.Height; + + return true; + } + + /// + /// This makes it possible for the label to be placed exactly in the middle of the pointer. + /// + public override bool HandleMouseDown(int mouseX, int mouseY) { + return base.HandleMouseDown(mouseX - (Width / 2), mouseY - (Height / 2)); + } + + /// + /// We set our own field values + /// + protected override void InitializeFields() { + AddField(GetType(), FieldType.FILL_COLOR, Color.DarkRed); + AddField(GetType(), FieldType.LINE_COLOR, Color.White); + } + + /// + /// Make sure this element is no longer referenced from the surface + /// + public override void Dispose() { + ((Surface)Parent).RemoveStepLabel(this); + base.Dispose(); + } + + public override bool HandleMouseMove(int x, int y) { + Invalidate(); + Left = x - (Width / 2); + Top = y - (Height / 2); + Invalidate(); + return true; + } + + /// + /// Override the parent, calculate the label number, than draw + /// + /// + /// + public override void Draw(Graphics graphics, RenderMode rm) { + graphics.SmoothingMode = SmoothingMode.HighQuality; + graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; + graphics.CompositingQuality = CompositingQuality.HighQuality; + graphics.PixelOffsetMode = PixelOffsetMode.None; + graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; + string text = ((Surface)Parent).CountStepLabels(this).ToString(); + Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); + Color fillColor = GetFieldValueAsColor(FieldType.FILL_COLOR); + Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR); + if (_drawAsRectangle) { + RectangleContainer.DrawRectangle(rect, graphics, rm, 0, Color.Transparent, fillColor, false); + } else { + EllipseContainer.DrawEllipse(rect, graphics, rm, 0, Color.Transparent, fillColor, false); + } + using (FontFamily fam = new FontFamily(FontFamily.GenericSansSerif.Name)) { + float factor = (((float)rect.Width / DefaultSize.Width) + ((float)rect.Height / DefaultSize.Height)) / 2; + using (Font _font = new Font(fam, 16 * factor, FontStyle.Bold, GraphicsUnit.Pixel)) { + TextContainer.DrawText(graphics, rect, 0, lineColor, false, _stringFormat, text, _font); + } + } + } + + public override bool ClickableAt(int x, int y) { + Rectangle rect = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); + Color fillColor = GetFieldValueAsColor(FieldType.FILL_COLOR); + if (_drawAsRectangle) { + return RectangleContainer.RectangleClickableAt(rect, 0, fillColor, x, y); + } else { + return EllipseContainer.EllipseClickableAt(rect, 0, fillColor, x, y); + } + } + } +} diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index 74cc5933d..fe8dd1cce 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -208,24 +208,23 @@ namespace Greenshot.Drawing { /// /// all stepLabels for the surface, needed with serialization /// - private List _stepLabels = new List(); + private List _stepLabels = new List(); - public void AddStepLabel(IDrawableContainer stepLabel) { + public void AddStepLabel(StepLabelContainer stepLabel) { _stepLabels.Add(stepLabel); } - public void RemoveStepLabel(IDrawableContainer stepLabel) { + public void RemoveStepLabel(StepLabelContainer stepLabel) { _stepLabels.Remove(stepLabel); } /// - /// Count all the steplabels in the surface, up to the supplied one + /// Count all the VISIBLE steplabels in the surface, up to the supplied one /// /// can be null, if not the counting stops here /// number of steplabels before the supplied container public int CountStepLabels(IDrawableContainer stopAtContainer) { int number = 1; - foreach (var drawableContainer in _stepLabels) { - var possibleThis = (StepLabelContainer) drawableContainer; + foreach (var possibleThis in _stepLabels) { if (possibleThis == stopAtContainer) { break; } @@ -631,6 +630,10 @@ namespace Greenshot.Drawing { BinaryFormatter binaryRead = new BinaryFormatter(); DrawableContainerList loadedElements = (DrawableContainerList) binaryRead.Deserialize(streamRead); loadedElements.Parent = this; + // Make sure the steplabels are sorted accoring to their number + _stepLabels.Sort(delegate(StepLabelContainer p1, StepLabelContainer p2) { + return p1.Number.CompareTo(p2.Number); + }); DeselectAllElements(); AddElements(loadedElements); SelectElements(loadedElements); diff --git a/GreenshotPlugin/Interfaces/Generic.cs b/GreenshotPlugin/Interfaces/Generic.cs index fa5110b1b..db147c696 100644 --- a/GreenshotPlugin/Interfaces/Generic.cs +++ b/GreenshotPlugin/Interfaces/Generic.cs @@ -198,9 +198,5 @@ namespace Greenshot.Plugin { get; set; } - - void AddStepLabel(IDrawableContainer stepLabel); - void RemoveStepLabel(IDrawableContainer stepLabel); - int CountStepLabels(IDrawableContainer stopCountingOnStepLabel); } } \ No newline at end of file