SpriteData.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SuperTux Editor
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.Collections.Generic;
  18. using Drawing;
  19. using Lisp;
  20. using DataStructures;
  21. namespace Sprites
  22. {
  23. /// <summary>
  24. /// Stores the "static" data (images, size and so on) for sprites.
  25. /// </summary>
  26. internal class SpriteData {
  27. /// <summary>
  28. /// An action, and it's images and other data.
  29. /// </summary>
  30. public class Action {
  31. public string Name;
  32. /// <summary>
  33. /// A list of <see cref="Surface"/> objects for the
  34. /// images in the action.
  35. /// </summary>
  36. public List<Surface> Frames = new List<Surface>();
  37. public float Speed = 1.0f;
  38. /// <summary>
  39. /// Offset for coordinates that should be used in level file compared
  40. /// to coordinates for image. Calculated from <see cref="Hitbox"/>
  41. /// </summary>
  42. public Vector Offset = new Vector();
  43. /// <summary>
  44. /// The width of the widest of the images that this action consists of.
  45. /// </summary>
  46. public float Width;
  47. /// <summary>
  48. /// The height of the highest of the images that this action consists of.
  49. /// </summary>
  50. public float Height;
  51. /// <summary>
  52. /// The hitbox (if any) for this action.
  53. /// </summary>
  54. /// <remarks>
  55. /// The game uses float for this so we do as well, no sprite
  56. /// currently use anything but integers for this though.
  57. /// </remarks>
  58. public RectangleF Hitbox;
  59. public Action(string Name, Surface Surface) {
  60. this.Name = Name;
  61. Frames.Add(Surface);
  62. Width = Surface.Width;
  63. Height = Surface.Height;
  64. }
  65. public Action(List Data, string BaseDir, SpriteData spriteData) {
  66. Properties Props = new Properties(Data);
  67. if(!Props.Get("name", ref Name))
  68. throw new Exception("Action without name specified");
  69. Props.Get("fps", ref Speed);
  70. if(Props.Exists("hitbox")) {
  71. List<float> hitbox = new List<float>();
  72. Props.GetFloatList("hitbox", hitbox);
  73. if (hitbox.Count != 4)
  74. throw new Exception("hitbox must specify exactly 4 coordinates");
  75. Hitbox = new RectangleF(hitbox[0], hitbox[1], hitbox[2], hitbox[3]);
  76. Offset.X = Hitbox.Left;
  77. Offset.Y = Hitbox.Top;
  78. }
  79. List<string> ImageFileNames = new List<string>();
  80. Props.GetStringList("images", ImageFileNames);
  81. Props.PrintUnusedWarnings();
  82. foreach(string ImageFile in ImageFileNames) {
  83. Surface surface = new Surface(BaseDir + "/" + ImageFile);
  84. Width = Math.Max(Width, surface.Width);
  85. Height = Math.Max(Height, surface.Height);
  86. Frames.Add(surface);
  87. }
  88. string MirrorActionName = null;
  89. Props.Get("mirror-action", ref MirrorActionName);
  90. if(MirrorActionName != null) {
  91. Action MirrorAction = spriteData.Actions[MirrorActionName];
  92. foreach(Surface surface in MirrorAction.Frames) {
  93. Surface flippedSurface = new Surface(surface);
  94. flippedSurface.Left = surface.Right;
  95. flippedSurface.Right = surface.Left;
  96. Width = Math.Max(Width, surface.Width);
  97. Height = Math.Max(Height, surface.Height);
  98. Frames.Add(flippedSurface);
  99. }
  100. }
  101. }
  102. }
  103. public Dictionary<string, Action> Actions = new Dictionary<string, Action>();
  104. public SpriteData(List Data, string BaseDir) {
  105. LispIterator iter = new LispIterator(Data);
  106. while(iter.MoveNext()) {
  107. if(iter.Key == "action") {
  108. Action Action = new Action(iter.List, BaseDir, this);
  109. Actions.Add(Action.Name, Action);
  110. } else {
  111. Console.WriteLine("Unknown tag in sprite: " + iter.Key);
  112. }
  113. }
  114. }
  115. public SpriteData(Surface Surface, Vector offset) {
  116. Action Action = new Action("default", Surface);
  117. Action.Offset = offset;
  118. Actions.Add(Action.Name, Action);
  119. }
  120. }
  121. }
  122. /* EOF */