/src/Effects/PolarInversionEffect.cs
C# | 136 lines | 86 code | 19 blank | 31 comment | 0 complexity | f47d14a74ad4fd34cff85e2e99715083 MD5 | raw file
- /////////////////////////////////////////////////////////////////////////////////
- // Paint.NET //
- // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
- // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
- // See src/Resources/Files/License.txt for full licensing and attribution //
- // details. //
- // . //
- /////////////////////////////////////////////////////////////////////////////////
-
- // Copyright (c) 2006-2008 Ed Harvey
- //
- // MIT License: http://www.opensource.org/licenses/mit-license.php
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
-
- using System.Collections.Generic;
- using System.Drawing;
- using PaintDotNet.Base;
- using PaintDotNet.Base.PropertySystem;
- using PaintDotNet.IndirectUI;
-
- namespace PaintDotNet.Effects
- {
- public sealed class PolarInversionEffect
- : WarpEffectBase
- {
- public PolarInversionEffect()
- : base(PdnResources.GetString("PolarInversion.Name"),
- PdnResources.GetImageResource("Icons.PolarInversionEffect.png").Reference,
- SubmenuNames.Distort,
- EffectFlags.Configurable)
- {
- }
-
- public enum PropertyNames
- {
- Amount = 0,
- Offset = 1,
- EdgeBehavior = 2,
- Quality = 3
- }
-
- private double _amount;
-
- protected override PropertyCollection OnCreatePropertyCollection()
- {
- var properties = new List<Property>
- {
- new DoubleProperty(PropertyNames.Amount, 1, -4, 4),
- new DoubleVectorProperty(PropertyNames.Offset, Pair.Create<double, double>(0, 0),
- Pair.Create<double, double>(-2, -2),
- Pair.Create<double, double>(2, 2)),
- new StaticListChoiceProperty(PropertyNames.EdgeBehavior,
- new object[]
- {
- WarpEdgeBehavior.Clamp, WarpEdgeBehavior.Reflect,
- WarpEdgeBehavior.Wrap
- }, 2),
- new Int32Property(PropertyNames.Quality, 2, 1, 5)
- };
-
- return new PropertyCollection(properties);
- }
-
- protected override ControlInfo OnCreateConfigUI(PropertyCollection props)
- {
- ControlInfo configUi = base.OnCreateConfigUI(props);
-
- configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.DisplayName, PdnResources.GetString("PolarInversion.ConfigUI.Amount.DisplayName"));
- configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.UseExponentialScale, true);
- configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.SliderLargeChange, 0.25);
- configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.SliderSmallChange, 0.05);
- configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.UpDownIncrement, 0.01);
-
- configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.DisplayName, PdnResources.GetString("PolarInversion.ConfigUI.Offset.DisplayName"));
- configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.SliderSmallChangeX, 0.05);
- configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.SliderLargeChangeX, 0.25);
- configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.UpDownIncrementX, 0.01);
- configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.SliderSmallChangeY, 0.05);
- configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.SliderLargeChangeY, 0.25);
- configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.UpDownIncrementY, 0.01);
-
- Rectangle selection = EnvironmentParameters.GetSelection(EnvironmentParameters.SourceSurface.Bounds).GetBoundsInt();
- ImageResource propertyValue = ImageResource.FromImage(EnvironmentParameters.SourceSurface.CreateAliasedBitmap(selection));
- configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.StaticImageUnderlay, propertyValue);
-
- configUi.SetPropertyControlValue(PropertyNames.EdgeBehavior, ControlInfoPropertyNames.DisplayName, PdnResources.GetString("PolarInversion.ConfigUI.EdgeBehavior.DisplayName"));
-
- PropertyControlInfo edgeBehaviorPCI = configUi.FindControlForPropertyName(PropertyNames.EdgeBehavior);
- edgeBehaviorPCI.SetValueDisplayName(WarpEdgeBehavior.Clamp, PdnResources.GetString("PolarInversion.ConfigUI.EdgeBehavior.Clamp.DisplayName"));
- edgeBehaviorPCI.SetValueDisplayName(WarpEdgeBehavior.Reflect, PdnResources.GetString("PolarInversion.ConfigUI.EdgeBehavior.Reflect.DisplayName"));
- edgeBehaviorPCI.SetValueDisplayName(WarpEdgeBehavior.Wrap, PdnResources.GetString("PolarInversion.ConfigUI.EdgeBehavior.Wrap.DisplayName"));
-
- configUi.SetPropertyControlValue(PropertyNames.Quality, ControlInfoPropertyNames.DisplayName, PdnResources.GetString("PolarInversion.ConfigUI.Quality.DisplayName"));
-
- return configUi;
- }
-
- protected override void OnSetRenderInfo2(PropertyBasedEffectConfigToken newToken, RenderArgs dstArgs, RenderArgs srcArgs)
- {
- _amount = newToken.GetProperty<DoubleProperty>(PropertyNames.Amount).Value;
- Offset = newToken.GetProperty<DoubleVectorProperty>(PropertyNames.Offset).Value;
- EdgeBehavior = (WarpEdgeBehavior)newToken.GetProperty<StaticListChoiceProperty>(PropertyNames.EdgeBehavior).Value;
- Quality = newToken.GetProperty<Int32Property>(PropertyNames.Quality).Value;
- }
-
- protected override void InverseTransform(ref TransformData data)
- {
- double x = data.X;
- double y = data.Y;
-
- // NOTE: when x and y are zero, this will divide by zero and return NaN
- double invertDistance = Utility.Lerp(1d, DefaultRadius2 / ((x * x) + (y * y)), _amount);
-
- data.X = x * invertDistance;
- data.Y = y * invertDistance;
- }
- }
- }