Small fixes & comments for some code, mainly the animation stuff of Greenshot.

This commit is contained in:
Robin 2018-06-08 08:30:09 +02:00
commit b65badf359
9 changed files with 202 additions and 84 deletions

View file

@ -32,12 +32,24 @@ namespace Greenshot.Addons.Animation
/// </summary> /// </summary>
internal class AnimationLeg<T> internal class AnimationLeg<T>
{ {
/// <summary>
/// The destination for an animation
/// </summary>
public T Destination { get; set; } public T Destination { get; set; }
/// <summary>
/// Easing mode to use for this animation
/// </summary>
public EasingModes EasingMode { get; set; } public EasingModes EasingMode { get; set; }
/// <summary>
/// Easing type to use for the animation leg
/// </summary>
public EasingTypes EasingType { get; set; } public EasingTypes EasingType { get; set; }
/// <summary>
/// Number of frames in the leg
/// </summary>
public int Frames { get; set; } public int Frames { get; set; }
} }
} }

View file

@ -29,11 +29,11 @@ using System.Collections.Generic;
namespace Greenshot.Addons.Animation namespace Greenshot.Addons.Animation
{ {
/// <summary> /// <summary>
/// Base class for the animation logic, this only implements Properties and a constructor /// Base class for the animation logic, this only implements Properties and a constructor
/// </summary> /// </summary>
/// <typeparam name="T">Type for the animation, like NativePoint/NativeRect/Size</typeparam> /// <typeparam name="T">Type for the animation, like NativePoint/NativeRectNative/NativeSize</typeparam>
public abstract class AnimatorBase<T> : IAnimator public abstract class AnimatorBase<T> : IAnimator
{ {
private readonly Queue<AnimationLeg<T>> _queue = new Queue<AnimationLeg<T>>(); private readonly Queue<AnimationLeg<T>> _queue = new Queue<AnimationLeg<T>>();

View file

@ -0,0 +1,59 @@
#region Greenshot GNU General Public License
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2018 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/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 <http://www.gnu.org/licenses/>.
#endregion
#region using
using System;
#endregion
namespace Greenshot.Addons.Animation
{
/// <summary>
/// A supporting class to do Pow calculations for easing
/// </summary>
public static class EasePower
{
public static double EaseIn(double s, int power)
{
return Math.Pow(s, power);
}
public static double EaseInOut(double s, int power)
{
s *= 2;
if (s < 1)
{
return EaseIn(s, power) / 2;
}
var sign = power % 2 == 0 ? -1 : 1;
return sign / 2.0 * (Math.Pow(s - 2, power) + sign * 2);
}
public static double EaseOut(double s, int power)
{
var sign = power % 2 == 0 ? -1 : 1;
return sign * (Math.Pow(s - 1, power) + sign);
}
}
}

View file

@ -0,0 +1,52 @@
#region Greenshot GNU General Public License
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2018 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/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 <http://www.gnu.org/licenses/>.
#endregion
#region using
using System;
#endregion
namespace Greenshot.Addons.Animation
{
/// <summary>
/// This is a supporting class to do Sin calculations for the easing
/// </summary>
public static class EaseSine
{
public static double EaseIn(double s)
{
return Math.Sin(s * (Math.PI / 2) - Math.PI / 2) + 1;
}
public static double EaseInOut(double s)
{
return Math.Sin(s * Math.PI - Math.PI / 2 + 1) / 2;
}
public static double EaseOut(double s)
{
return Math.Sin(s * (Math.PI / 2));
}
}
}

View file

@ -29,12 +29,19 @@ using System;
namespace Greenshot.Addons.Animation namespace Greenshot.Addons.Animation
{ {
/// <summary> /// <summary>
/// Easing logic, to make the animations more "fluent" /// Easing logic, to make the animations more "fluent"
/// Adapted from <a href="http://www.robertpenner.com/easing/penner_chapter7_tweening.pdf">here</a> /// Adapted from <a href="http://www.robertpenner.com/easing/penner_chapter7_tweening.pdf">here</a>
/// </summary> /// </summary>
public static class Easing public static class Easing
{ {
/// <summary>
/// Apply "ease" to ubfirnatuin
/// </summary>
/// <param name="linearStep">double</param>
/// <param name="acceleration">double</param>
/// <param name="type">double</param>
/// <returns>double</returns>
public static double Ease(double linearStep, double acceleration, EasingTypes type) public static double Ease(double linearStep, double acceleration, EasingTypes type)
{ {
var easedStep = acceleration > 0 ? EaseIn(linearStep, type) : acceleration < 0 ? EaseOut(linearStep, type) : linearStep; var easedStep = acceleration > 0 ? EaseIn(linearStep, type) : acceleration < 0 ? EaseOut(linearStep, type) : linearStep;
@ -42,6 +49,12 @@ namespace Greenshot.Addons.Animation
return (easedStep - linearStep) * Math.Abs(acceleration) + linearStep; return (easedStep - linearStep) * Math.Abs(acceleration) + linearStep;
} }
/// <summary>
/// Apply ease in
/// </summary>
/// <param name="linearStep">double</param>
/// <param name="type">EasingTypes</param>
/// <returns>double</returns>
public static double EaseIn(double linearStep, EasingTypes type) public static double EaseIn(double linearStep, EasingTypes type)
{ {
switch (type) switch (type)
@ -51,24 +64,37 @@ namespace Greenshot.Addons.Animation
case EasingTypes.Linear: case EasingTypes.Linear:
return linearStep; return linearStep;
case EasingTypes.Sine: case EasingTypes.Sine:
return Sine.EaseIn(linearStep); return EaseSine.EaseIn(linearStep);
case EasingTypes.Quadratic: case EasingTypes.Quadratic:
return Power.EaseIn(linearStep, 2); return EasePower.EaseIn(linearStep, 2);
case EasingTypes.Cubic: case EasingTypes.Cubic:
return Power.EaseIn(linearStep, 3); return EasePower.EaseIn(linearStep, 3);
case EasingTypes.Quartic: case EasingTypes.Quartic:
return Power.EaseIn(linearStep, 4); return EasePower.EaseIn(linearStep, 4);
case EasingTypes.Quintic: case EasingTypes.Quintic:
return Power.EaseIn(linearStep, 5); return EasePower.EaseIn(linearStep, 5);
} }
throw new NotImplementedException(); throw new NotImplementedException();
} }
/// <summary>
/// Apply ease in-out
/// </summary>
/// <param name="linearStep">double</param>
/// <param name="easeInType">EasingTypes</param>
/// <param name="easeOutType">EasingTypes</param>
/// <returns>double</returns>
public static double EaseInOut(double linearStep, EasingTypes easeInType, EasingTypes easeOutType) public static double EaseInOut(double linearStep, EasingTypes easeInType, EasingTypes easeOutType)
{ {
return linearStep < 0.5 ? EaseInOut(linearStep, easeInType) : EaseInOut(linearStep, easeOutType); return linearStep < 0.5 ? EaseInOut(linearStep, easeInType) : EaseInOut(linearStep, easeOutType);
} }
/// <summary>
/// Apply easy in out
/// </summary>
/// <param name="linearStep">double</param>
/// <param name="type">EasingTypes</param>
/// <returns>double</returns>
public static double EaseInOut(double linearStep, EasingTypes type) public static double EaseInOut(double linearStep, EasingTypes type)
{ {
switch (type) switch (type)
@ -78,20 +104,26 @@ namespace Greenshot.Addons.Animation
case EasingTypes.Linear: case EasingTypes.Linear:
return linearStep; return linearStep;
case EasingTypes.Sine: case EasingTypes.Sine:
return Sine.EaseInOut(linearStep); return EaseSine.EaseInOut(linearStep);
case EasingTypes.Quadratic: case EasingTypes.Quadratic:
return Power.EaseInOut(linearStep, 2); return EasePower.EaseInOut(linearStep, 2);
case EasingTypes.Cubic: case EasingTypes.Cubic:
return Power.EaseInOut(linearStep, 3); return EasePower.EaseInOut(linearStep, 3);
case EasingTypes.Quartic: case EasingTypes.Quartic:
return Power.EaseInOut(linearStep, 4); return EasePower.EaseInOut(linearStep, 4);
case EasingTypes.Quintic: case EasingTypes.Quintic:
return Power.EaseInOut(linearStep, 5); return EasePower.EaseInOut(linearStep, 5);
} }
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static double EaseOut(double linearStep, EasingTypes type) /// <summary>
/// Apply easy out
/// </summary>
/// <param name="linearStep">double</param>
/// <param name="type">EasingTypes</param>
/// <returns>double</returns>
public static double EaseOut(double linearStep, EasingTypes type)
{ {
switch (type) switch (type)
{ {
@ -100,60 +132,17 @@ namespace Greenshot.Addons.Animation
case EasingTypes.Linear: case EasingTypes.Linear:
return linearStep; return linearStep;
case EasingTypes.Sine: case EasingTypes.Sine:
return Sine.EaseOut(linearStep); return EaseSine.EaseOut(linearStep);
case EasingTypes.Quadratic: case EasingTypes.Quadratic:
return Power.EaseOut(linearStep, 2); return EasePower.EaseOut(linearStep, 2);
case EasingTypes.Cubic: case EasingTypes.Cubic:
return Power.EaseOut(linearStep, 3); return EasePower.EaseOut(linearStep, 3);
case EasingTypes.Quartic: case EasingTypes.Quartic:
return Power.EaseOut(linearStep, 4); return EasePower.EaseOut(linearStep, 4);
case EasingTypes.Quintic: case EasingTypes.Quintic:
return Power.EaseOut(linearStep, 5); return EasePower.EaseOut(linearStep, 5);
} }
throw new NotImplementedException(); throw new NotImplementedException();
} }
private static class Sine
{
public static double EaseIn(double s)
{
return Math.Sin(s * (Math.PI / 2) - Math.PI / 2) + 1;
}
public static double EaseInOut(double s)
{
return Math.Sin(s * Math.PI - Math.PI / 2 + 1) / 2;
}
public static double EaseOut(double s)
{
return Math.Sin(s * (Math.PI / 2));
}
}
private static class Power
{
public static double EaseIn(double s, int power)
{
return Math.Pow(s, power);
}
public static double EaseInOut(double s, int power)
{
s *= 2;
if (s < 1)
{
return EaseIn(s, power) / 2;
}
var sign = power % 2 == 0 ? -1 : 1;
return sign / 2.0 * (Math.Pow(s - 2, power) + sign * 2);
}
public static double EaseOut(double s, int power)
{
var sign = power % 2 == 0 ? -1 : 1;
return sign * (Math.Pow(s - 1, power) + sign);
}
}
} }
} }

View file

@ -23,6 +23,9 @@
namespace Greenshot.Addons.Animation namespace Greenshot.Addons.Animation
{ {
/// <summary>
/// Define the possible easing modes
/// </summary>
public enum EasingModes public enum EasingModes
{ {
EaseIn, EaseIn,

View file

@ -23,7 +23,7 @@
#region using #region using
using System.Drawing; using Dapplo.Windows.Common.Structs;
#endregion #endregion
@ -32,18 +32,18 @@ namespace Greenshot.Addons.Animation
/// <summary> /// <summary>
/// Implementation of the SizeAnimator /// Implementation of the SizeAnimator
/// </summary> /// </summary>
public class SizeAnimator : AnimatorBase<Size> public class SizeAnimator : AnimatorBase<NativeSize>
{ {
public SizeAnimator(Size first, Size last, int frames, EasingTypes easingType = EasingTypes.Linear, EasingModes easingMode = EasingModes.EaseIn) public SizeAnimator(NativeSize first, NativeSize last, int frames, EasingTypes easingType = EasingTypes.Linear, EasingModes easingMode = EasingModes.EaseIn)
: base(first, last, frames, easingType, easingMode) : base(first, last, frames, easingType, easingMode)
{ {
} }
/// <summary> /// <summary>
/// Calculate the next frame values /// Calculate the next frame values
/// </summary> /// </summary>
/// <returns>Size</returns> /// <returns>NativeSize</returns>
public override Size Next() public override NativeSize Next()
{ {
if (!NextFrame) if (!NextFrame)
{ {
@ -54,7 +54,7 @@ namespace Greenshot.Addons.Animation
double dh = Last.Height - First.Height; double dh = Last.Height - First.Height;
var width = First.Width + (int) (easingValue * dw); var width = First.Width + (int) (easingValue * dw);
var height = First.Height + (int) (easingValue * dh); var height = First.Height + (int) (easingValue * dh);
Current = new Size(width, height); Current = new NativeSize(width, height);
return Current; return Current;
} }
} }

View file

@ -155,6 +155,8 @@
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Animation\EasePower.cs" />
<Compile Include="Animation\EaseSine.cs" />
<Compile Include="Components\DestinationAttribute.cs" /> <Compile Include="Components\DestinationAttribute.cs" />
<Compile Include="Animation\ColorAnimator.cs" /> <Compile Include="Animation\ColorAnimator.cs" />
<Compile Include="Animation\Easing.cs" /> <Compile Include="Animation\Easing.cs" />

View file

@ -74,7 +74,7 @@ namespace Greenshot.Forms
private int _mX; private int _mX;
private int _mY; private int _mY;
private Point _previousMousePos = Point.Empty; private NativePoint _previousMousePos = NativePoint.Empty;
// the window which is selected // the window which is selected
private bool _showDebugInfo; private bool _showDebugInfo;
private RectangleAnimator _windowAnimator; private RectangleAnimator _windowAnimator;
@ -423,7 +423,7 @@ namespace Greenshot.Forms
/// </summary> /// </summary>
/// <param name="currentMouse"></param> /// <param name="currentMouse"></param>
/// <returns></returns> /// <returns></returns>
private Point FixMouseCoordinates(Point currentMouse) private Point FixMouseCoordinates(NativePoint currentMouse)
{ {
switch (_fixMode) switch (_fixMode)
{ {
@ -455,9 +455,10 @@ namespace Greenshot.Forms
/// <param name="e"></param> /// <param name="e"></param>
private void OnMouseMove(object sender, MouseEventArgs e) private void OnMouseMove(object sender, MouseEventArgs e)
{ {
// Make sure the mouse coordinates are fixed, when pressing shift var cursorLocation = User32Api.GetCursorLocation();
_mouseMovePos = FixMouseCoordinates(User32Api.GetCursorLocation()); var relativeCursorPosition = WindowCapture.GetLocationRelativeToScreenBounds(cursorLocation);
_mouseMovePos = WindowCapture.GetLocationRelativeToScreenBounds(_mouseMovePos); // Make sure the mouse coordinates are fixed, e.g. when pressing shift
_mouseMovePos = FixMouseCoordinates(relativeCursorPosition);
} }
/// <summary> /// <summary>
@ -709,7 +710,7 @@ namespace Greenshot.Forms
/// <param name="paintEventArgs">PaintEventArgs</param> /// <param name="paintEventArgs">PaintEventArgs</param>
protected override void OnPaintBackground(PaintEventArgs paintEventArgs) protected override void OnPaintBackground(PaintEventArgs paintEventArgs)
{ {
// Ignore the event, to reduct painting // Ignore the event, to reduce painting
} }
/// <summary> /// <summary>