Advertisement
Guest User

Untitled

a guest
Jul 13th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Documents;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using ErrorManager;
  12. using GRF.FileFormats.ActFormat;
  13. using GRF.FileFormats.SprFormat;
  14. using GRF.FileFormats.PalFormat;
  15. using GRF.Image;
  16. using GRF.Image.Decoders;
  17. using GRF.Graphics;
  18. using GrfToWpfBridge;
  19. using TokeiLibrary;
  20. using TokeiLibrary.WPF;
  21. using Action = GRF.FileFormats.ActFormat.Action;
  22. using Frame = GRF.FileFormats.ActFormat.Frame;
  23. using Point = System.Windows.Point;
  24.  
  25. namespace Scripts {
  26. public class Script : IActScript {
  27. public object DisplayName {
  28. get { return "MyCustomScript"; }
  29. }
  30.  
  31. public string Group {
  32. get { return "Scripts"; }
  33. }
  34.  
  35. public string InputGesture {
  36. get { return null; }
  37. }
  38.  
  39. public string Image {
  40. get { return null; }
  41. }
  42.  
  43. public void Execute(Act act, int selectedActionIndex, int selectedFrameIndex, int[] selectedLayerIndexes) {
  44. if (act == null) return;
  45.  
  46. try {
  47. act.Commands.Begin();
  48. act.Commands.Backup(_ => {
  49. int count = act.GetAllFrames().Count + 1;
  50. int index = 0;
  51.  
  52. TaskManager.DisplayTaskC("Rendering frames...", "Please wait...", () => index, count, new Action<Func<bool>>(isCancelling => {
  53. try {
  54. foreach (var action in act) {
  55. foreach (var frame in action) {
  56. if (frame.Layers.Count <= 1) {
  57. index++;
  58. continue;
  59. }
  60. if (isCancelling()) return;
  61.  
  62. var image = frame.Render(act);
  63. var box = ActImaging.Imaging.GenerateFrameBoundingBox(act, frame);
  64. int relativeIndex = -1;
  65.  
  66. for (int i = 0; i < act.Sprite.Images.Count; i++) {
  67. if (image.Equals(act.Sprite.Images[i])) {
  68. if (isCancelling()) return;
  69. relativeIndex = act.Sprite.AbsoluteToRelative(i, act.Sprite.Images[i].GrfImageType == GrfImageType.Indexed8 ? 0 : 1);
  70. }
  71. }
  72.  
  73. if (relativeIndex < 0) {
  74. relativeIndex = act.Sprite.InsertAny(image);
  75. }
  76.  
  77. int offsetX = (int) ((int) ((box.Max.X - box.Min.X + 1) / 2) + box.Min.X);
  78. int offsetY = (int) ((int) ((box.Max.Y - box.Min.Y + 1) / 2) + box.Min.Y);
  79. var layer = new Layer(relativeIndex, image);
  80.  
  81. layer.OffsetX = offsetX;
  82. layer.OffsetY = offsetY;
  83.  
  84. frame.Layers.Clear();
  85. frame.Layers.Add(layer);
  86. index++;
  87. }
  88. }
  89.  
  90. // Removes unused sprites - old way, older versions have a bug
  91. for (int i = act.Sprite.Images.Count - 1; i >= 0 ; i--) {
  92. if (act.FindUsageOf(i).Count == 0) {
  93. var type = act.Sprite.Images[i].GrfImageType;
  94. var relativeIndex = act.Sprite.AbsoluteToRelative(i, type == GrfImageType.Indexed8 ? 0 : 1);
  95. act.Sprite.Remove(relativeIndex, type);
  96.  
  97. if (type == GrfImageType.Indexed8) {
  98. act.AllLayers(layer => {
  99. if ((layer.IsIndexed8() && type == GrfImageType.Indexed8) ||
  100. (layer.IsBgra32() && type == GrfImageType.Bgra32)) {
  101. if (layer.SpriteIndex == relativeIndex) {
  102. layer.SpriteIndex = -1;
  103. }
  104. }
  105. });
  106. }
  107.  
  108. act.Sprite.ShiftIndexesAbove(act, type, -1, relativeIndex);
  109. }
  110. }
  111. }
  112. finally {
  113. index = count;
  114. }
  115. }));
  116. }, "MyCustomScript", true);
  117. }
  118. catch (Exception err) {
  119. act.Commands.CancelEdit();
  120. ErrorHandler.HandleException(err, ErrorLevel.Warning);
  121. }
  122. finally {
  123. act.Commands.End();
  124. act.InvalidateVisual();
  125. act.InvalidateSpriteVisual();
  126. }
  127. }
  128.  
  129. public bool CanExecute(Act act, int selectedActionIndex, int selectedFrameIndex, int[] selectedLayerIndexes) {
  130. return act != null;
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement