An advanced sprite packing tool. Currently a work in progress.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sprite.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Runtime.Remoting.Messaging;
  5. namespace SpritePacker
  6. {
  7. public class Sprite
  8. {
  9. private Rectangle area;
  10. private string filename;
  11. private Image image;
  12. public bool Placed { get; set; } = false;
  13. public Rectangle Area
  14. {
  15. get { return area; }
  16. set { area = value; }
  17. }
  18. public string Filename
  19. {
  20. get { return filename; }
  21. set { filename = value; }
  22. }
  23. public Image Image { get { return image; } }
  24. public Point Location { get { return new Point(X, Y); } }
  25. public int X
  26. {
  27. get { return area.X; }
  28. set { area.X = value; }
  29. }
  30. public int Y
  31. {
  32. get { return area.Y; }
  33. set { area.Y = value; }
  34. }
  35. public int Width
  36. {
  37. get { return area.Width; }
  38. set { area.Width = value; }
  39. }
  40. public int Height
  41. {
  42. get { return area.Height; }
  43. set { area.Height = value; }
  44. }
  45. public int Top { get { return area.Top; } }
  46. public int Bottom { get { return area.Bottom; } }
  47. public int Left { get { return area.Left; } }
  48. public int Right { get { return area.Right; } }
  49. public int AreaSize { get { return area.Width * area.Height; } }
  50. public int MaxSideLength { get { return Math.Max(area.Width, area.Height); } }
  51. public Sprite(string inFilename)
  52. {
  53. Filename = inFilename;
  54. image = Image.FromFile(filename);
  55. Width = image.Width;
  56. Height = image.Height;
  57. }
  58. public static int GetLargestSize(List<Sprite> sprList)
  59. {
  60. int largestSoFar = 0;
  61. foreach(Sprite spr in sprList)
  62. {
  63. if (spr.Area.Width > largestSoFar)
  64. largestSoFar = spr.Area.Width;
  65. }
  66. return largestSoFar;
  67. }
  68. public List<Sprite> GetIntersectors(List<Sprite> spriteList)
  69. {
  70. List<Sprite> result = new List<Sprite>();
  71. foreach(Sprite spr in spriteList)
  72. {
  73. if (spr == this) // Don't attempt to check for collisions with ourselves
  74. continue;
  75. if (spr.IntersectsWith(this))
  76. result.Add(spr);
  77. }
  78. return result;
  79. }
  80. public List<Sprite> GetIntersectorsX(List<Sprite> spriteList)
  81. {
  82. List<Sprite> result = new List<Sprite>();
  83. foreach(Sprite spr in spriteList)
  84. {
  85. if (spr.IntersectsWithX(this))
  86. result.Add(spr);
  87. }
  88. return result;
  89. }
  90. public List<Sprite> GetIntersectorsY(List<Sprite> spriteList)
  91. {
  92. List<Sprite> result = new List<Sprite>();
  93. foreach(Sprite spr in spriteList)
  94. {
  95. if (spr.IntersectsWithY(this))
  96. result.Add(spr);
  97. }
  98. return result;
  99. }
  100. public bool IntersectsWith(List<Sprite> otherSprites)
  101. {
  102. foreach (Sprite spr in otherSprites)
  103. {
  104. if (IntersectsWith(spr))
  105. return true;
  106. }
  107. return false;
  108. }
  109. public bool IntersectsWith(Sprite otherSprite)
  110. {
  111. return otherSprite.Area.IntersectsWith(Area);
  112. }
  113. public bool IntersectsWithX(List<Sprite> otherSprites)
  114. {
  115. foreach (Sprite spr in otherSprites)
  116. {
  117. if (IntersectsWithX(spr))
  118. return true;
  119. }
  120. return false;
  121. }
  122. public bool IntersectsWithY(List<Sprite> otherSprites)
  123. {
  124. foreach (Sprite spr in otherSprites)
  125. {
  126. if (IntersectsWithY(spr))
  127. return true;
  128. }
  129. return false;
  130. }
  131. public bool IntersectsWithX(Sprite otherSprite)
  132. {
  133. if(Area.Right > otherSprite.Area.X &&
  134. Area.X < otherSprite.Area.Right)
  135. return true;
  136. else
  137. return false;
  138. }
  139. public bool IntersectsWithY(Sprite otherSprite)
  140. {
  141. if(Area.Bottom > otherSprite.Area.Y &&
  142. Area.Y < otherSprite.Area.Bottom)
  143. return true;
  144. else
  145. return false;
  146. }
  147. public override string ToString()
  148. {
  149. return string.Format("Sprite {0} at {1}", Filename, Area);
  150. }
  151. }
  152. }