PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Effects/PolarInversionEffect.cs

https://bitbucket.org/tuldok89/openpdn
C# | 136 lines | 86 code | 19 blank | 31 comment | 0 complexity | f47d14a74ad4fd34cff85e2e99715083 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. // Copyright (c) 2006-2008 Ed Harvey
  10. //
  11. // MIT License: http://www.opensource.org/licenses/mit-license.php
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining a copy
  14. // of this software and associated documentation files (the "Software"), to deal
  15. // in the Software without restriction, including without limitation the rights
  16. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. // copies of the Software, and to permit persons to whom the Software is
  18. // furnished to do so, subject to the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be included in
  21. // all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. // THE SOFTWARE.
  30. //
  31. using System.Collections.Generic;
  32. using System.Drawing;
  33. using PaintDotNet.Base;
  34. using PaintDotNet.Base.PropertySystem;
  35. using PaintDotNet.IndirectUI;
  36. namespace PaintDotNet.Effects
  37. {
  38. public sealed class PolarInversionEffect
  39. : WarpEffectBase
  40. {
  41. public PolarInversionEffect()
  42. : base(PdnResources.GetString("PolarInversion.Name"),
  43. PdnResources.GetImageResource("Icons.PolarInversionEffect.png").Reference,
  44. SubmenuNames.Distort,
  45. EffectFlags.Configurable)
  46. {
  47. }
  48. public enum PropertyNames
  49. {
  50. Amount = 0,
  51. Offset = 1,
  52. EdgeBehavior = 2,
  53. Quality = 3
  54. }
  55. private double _amount;
  56. protected override PropertyCollection OnCreatePropertyCollection()
  57. {
  58. var properties = new List<Property>
  59. {
  60. new DoubleProperty(PropertyNames.Amount, 1, -4, 4),
  61. new DoubleVectorProperty(PropertyNames.Offset, Pair.Create<double, double>(0, 0),
  62. Pair.Create<double, double>(-2, -2),
  63. Pair.Create<double, double>(2, 2)),
  64. new StaticListChoiceProperty(PropertyNames.EdgeBehavior,
  65. new object[]
  66. {
  67. WarpEdgeBehavior.Clamp, WarpEdgeBehavior.Reflect,
  68. WarpEdgeBehavior.Wrap
  69. }, 2),
  70. new Int32Property(PropertyNames.Quality, 2, 1, 5)
  71. };
  72. return new PropertyCollection(properties);
  73. }
  74. protected override ControlInfo OnCreateConfigUI(PropertyCollection props)
  75. {
  76. ControlInfo configUi = base.OnCreateConfigUI(props);
  77. configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.DisplayName, PdnResources.GetString("PolarInversion.ConfigUI.Amount.DisplayName"));
  78. configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.UseExponentialScale, true);
  79. configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.SliderLargeChange, 0.25);
  80. configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.SliderSmallChange, 0.05);
  81. configUi.SetPropertyControlValue(PropertyNames.Amount, ControlInfoPropertyNames.UpDownIncrement, 0.01);
  82. configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.DisplayName, PdnResources.GetString("PolarInversion.ConfigUI.Offset.DisplayName"));
  83. configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.SliderSmallChangeX, 0.05);
  84. configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.SliderLargeChangeX, 0.25);
  85. configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.UpDownIncrementX, 0.01);
  86. configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.SliderSmallChangeY, 0.05);
  87. configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.SliderLargeChangeY, 0.25);
  88. configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.UpDownIncrementY, 0.01);
  89. Rectangle selection = EnvironmentParameters.GetSelection(EnvironmentParameters.SourceSurface.Bounds).GetBoundsInt();
  90. ImageResource propertyValue = ImageResource.FromImage(EnvironmentParameters.SourceSurface.CreateAliasedBitmap(selection));
  91. configUi.SetPropertyControlValue(PropertyNames.Offset, ControlInfoPropertyNames.StaticImageUnderlay, propertyValue);
  92. configUi.SetPropertyControlValue(PropertyNames.EdgeBehavior, ControlInfoPropertyNames.DisplayName, PdnResources.GetString("PolarInversion.ConfigUI.EdgeBehavior.DisplayName"));
  93. PropertyControlInfo edgeBehaviorPCI = configUi.FindControlForPropertyName(PropertyNames.EdgeBehavior);
  94. edgeBehaviorPCI.SetValueDisplayName(WarpEdgeBehavior.Clamp, PdnResources.GetString("PolarInversion.ConfigUI.EdgeBehavior.Clamp.DisplayName"));
  95. edgeBehaviorPCI.SetValueDisplayName(WarpEdgeBehavior.Reflect, PdnResources.GetString("PolarInversion.ConfigUI.EdgeBehavior.Reflect.DisplayName"));
  96. edgeBehaviorPCI.SetValueDisplayName(WarpEdgeBehavior.Wrap, PdnResources.GetString("PolarInversion.ConfigUI.EdgeBehavior.Wrap.DisplayName"));
  97. configUi.SetPropertyControlValue(PropertyNames.Quality, ControlInfoPropertyNames.DisplayName, PdnResources.GetString("PolarInversion.ConfigUI.Quality.DisplayName"));
  98. return configUi;
  99. }
  100. protected override void OnSetRenderInfo2(PropertyBasedEffectConfigToken newToken, RenderArgs dstArgs, RenderArgs srcArgs)
  101. {
  102. _amount = newToken.GetProperty<DoubleProperty>(PropertyNames.Amount).Value;
  103. Offset = newToken.GetProperty<DoubleVectorProperty>(PropertyNames.Offset).Value;
  104. EdgeBehavior = (WarpEdgeBehavior)newToken.GetProperty<StaticListChoiceProperty>(PropertyNames.EdgeBehavior).Value;
  105. Quality = newToken.GetProperty<Int32Property>(PropertyNames.Quality).Value;
  106. }
  107. protected override void InverseTransform(ref TransformData data)
  108. {
  109. double x = data.X;
  110. double y = data.Y;
  111. // NOTE: when x and y are zero, this will divide by zero and return NaN
  112. double invertDistance = Utility.Lerp(1d, DefaultRadius2 / ((x * x) + (y * y)), _amount);
  113. data.X = x * invertDistance;
  114. data.Y = y * invertDistance;
  115. }
  116. }
  117. }