Entry.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.IO;
  4. namespace OpenVIII
  5. {
  6. public class Entry
  7. {
  8. #region Fields
  9. public Vector2 End;
  10. public Vector2 Location;
  11. public Vector2 Offset;
  12. public Vector2 Size;
  13. public byte[] Unk;
  14. private Loc _loc;
  15. #endregion Fields
  16. #region Constructors
  17. public Entry()
  18. {
  19. Unk = new byte[2];
  20. File = 0;
  21. Part = 1;
  22. }
  23. #endregion Constructors
  24. #region Properties
  25. public ushort CurrentPos { get; set; }
  26. public sbyte CustomPalette { get; set; } = -1;
  27. public byte File { get; set; }
  28. public Vector2 Fill { get; set; }
  29. /// <summary>
  30. /// Animation Frame
  31. /// </summary>
  32. public int Frame { get; set; } = -1;
  33. //public Loc GetLoc => loc;
  34. public Rectangle GetRectangle => new Rectangle(Location.ToPoint(), Size.ToPoint());
  35. public float Height { get => Size.Y; set => Size.Y = value; }
  36. /// <summary>
  37. /// If set this has ID, default behavior is to leave unset.
  38. /// </summary>
  39. public Enum ID { get; set; } = null;
  40. /// <summary>
  41. /// Number Value, set if this is a number.
  42. /// </summary>
  43. public int? NumberValue { get; set; } = null;
  44. public byte Part { get; set; } = 1;
  45. /// <summary>
  46. /// point where you want to stop drawing from bottom side so -8 would stop 8*scale pixels
  47. /// from edge
  48. /// </summary>
  49. //public sbyte Offset_Y2 { get; set; }
  50. public bool Snap_Bottom { get; set; } = false;
  51. /// <summary>
  52. /// point where you want to stop drawing from right side so -8 would stop 8*scale pixels from edge
  53. /// </summary>
  54. //public sbyte Offset_X2 { get; set; }
  55. public bool Snap_Right { get; set; } = false;
  56. /// <summary> Vector2.Zero = no tile, Vector2.One = tile x & y, Vector.UnitX = tile x,
  57. /// Vector.UnitY = tile y </summary>
  58. public Vector2 Tile { get; set; } = Vector2.Zero;
  59. /// <summary>
  60. /// How long to spend on each frame.
  61. /// </summary>
  62. public TimeSpan TimePerFrame { get; set; } = TimeSpan.Zero;
  63. public string ToStringHeader { get; } = "{File},{Part},{CustomPalette},{Location.X},{Location.Y},{Size.X},{Size.Y},{Offset.X},{Offset.Y},{End.X},{End.Y},{Tile.X},{Tile.Y},{Fill.X},{Fill.Y},{Snap_Right},{Snap_Bottom}\n";
  64. public bool Trimmed { get; private set; }
  65. public float Width { get => Size.X; set => Size.X = value; }
  66. public float X { get => Location.X; set => Location.X = value; }
  67. public float Y { get => Location.Y; set => Location.Y = value; }
  68. #endregion Properties
  69. #region Methods
  70. /// <summary>
  71. /// Shadowcopy
  72. /// </summary>
  73. /// <returns>Copy of class</returns>
  74. public Entry Clone()
  75. => (Entry)MemberwiseClone();
  76. public void LoadfromStreamSP1(BinaryReader br)
  77. {
  78. CurrentPos = (ushort)br.BaseStream.Position;
  79. Location.X = br.ReadByte();
  80. Location.Y = br.ReadByte();
  81. Unk = br.ReadBytes(2);
  82. Size.X = br.ReadByte();
  83. Offset.X = br.ReadSByte();
  84. Size.Y = br.ReadByte();
  85. Offset.Y = br.ReadSByte();
  86. }
  87. public void LoadfromStreamSP2(BinaryReader br, ushort loc, Entry prev, ref byte fid)
  88. {
  89. if (loc > 0)
  90. br.BaseStream.Seek(loc + 4, SeekOrigin.Begin);
  91. CurrentPos = loc;
  92. Location.X = br.ReadByte();
  93. Location.Y = br.ReadByte();
  94. Unk = br.ReadBytes(2);
  95. Size.X = br.ReadByte();
  96. Offset.X = br.ReadSByte();
  97. Size.Y = br.ReadByte();
  98. Offset.Y = br.ReadSByte();
  99. if (prev != null && Location.Y < prev.Y)
  100. fid++;
  101. File = fid;
  102. }
  103. public void SetLoc(Loc value) => _loc = value;
  104. public void SetTrim_1stPass(Rectangle value, bool skipoffset = false)
  105. {
  106. Trimmed = true;
  107. var newLoc = value.Location.ToVector2();
  108. //Offset may need to change.
  109. //I had to preserve a good offset for the D-pad to work.
  110. //So i was getting the difference in the two locations and adding to the offset.
  111. //skipoffset can be used if the entry is solo so you don't need to alter the offset.
  112. if (!skipoffset)
  113. Offset += (newLoc - Location).Abs();
  114. Location = newLoc;
  115. Size = value.Size.ToVector2();
  116. }
  117. /// <summary>
  118. /// After determining the trimmed dim. There will be code in entrygroup that finds the actual
  119. /// top left and then passes to this function This will take that offset and subtract it from
  120. /// the current Offset value.
  121. /// </summary>
  122. /// <param name="offset"></param>
  123. public void SetTrim_2ndPass(Vector2 offset) => Offset -= offset;
  124. public void SetTrimNonGroup(TextureHandler tex)
  125. {
  126. if (!Trimmed)
  127. SetTrim_1stPass(tex.Trim(GetRectangle));
  128. }
  129. public override string ToString()
  130. { //public override string ToString() => $"{File},{Part},{CustomPalette},{Location.X},{Location.Y},{Size.X},{Size.Y},{Offset.X},{Offset.Y},{End.X},{End.Y},{Tile.X},{Tile.Y},{Fill.X},{Fill.Y},{Snap_Right},{Snap_Bottom}\n";
  131. var prefix = "";
  132. var suffix = "";
  133. if (ID != null)
  134. prefix = ID.ToString();
  135. if (NumberValue.HasValue)
  136. suffix = NumberValue.Value.ToString();
  137. if (!string.IsNullOrWhiteSpace(prefix) && !string.IsNullOrWhiteSpace(suffix))
  138. return $"{prefix}.{suffix}";
  139. else if (!string.IsNullOrWhiteSpace(prefix))
  140. return prefix;
  141. else if (!string.IsNullOrWhiteSpace(suffix))
  142. return suffix;
  143. return base.ToString();
  144. }
  145. #endregion Methods
  146. }
  147. }