TemplateType.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using MobiusEditor.Interface;
  15. using MobiusEditor.Utility;
  16. using System;
  17. using System.Drawing;
  18. namespace MobiusEditor.Model
  19. {
  20. [Flags]
  21. public enum TemplateTypeFlag
  22. {
  23. None = 0,
  24. Clear = (1 << 1),
  25. Water = (1 << 2),
  26. OreMine = (1 << 3),
  27. }
  28. public class TemplateType : IBrowsableType
  29. {
  30. public ushort ID { get; private set; }
  31. public string Name { get; private set; }
  32. public string DisplayName => Name;
  33. public int IconWidth { get; private set; }
  34. public int IconHeight { get; private set; }
  35. public Size IconSize => new Size(IconWidth, IconHeight);
  36. public int NumIcons => IconWidth * IconHeight;
  37. public bool[,] IconMask { get; set; }
  38. public Image Thumbnail { get; set; }
  39. public TheaterType[] Theaters { get; private set; }
  40. public TemplateTypeFlag Flag { get; private set; }
  41. public TemplateType(ushort id, string name, int iconWidth, int iconHeight, TheaterType[] theaters, TemplateTypeFlag flag)
  42. {
  43. ID = id;
  44. Name = name;
  45. IconWidth = iconWidth;
  46. IconHeight = iconHeight;
  47. Theaters = theaters;
  48. Flag = flag;
  49. }
  50. public TemplateType(ushort id, string name, int iconWidth, int iconHeight, TheaterType[] theaters)
  51. : this(id, name, iconWidth, iconHeight, theaters, TemplateTypeFlag.None)
  52. {
  53. }
  54. public override bool Equals(object obj)
  55. {
  56. if (obj is TemplateType)
  57. {
  58. return this == obj;
  59. }
  60. else if (obj is byte)
  61. {
  62. return ID == (byte)obj;
  63. }
  64. else if (obj is ushort)
  65. {
  66. return ID == (ushort)obj;
  67. }
  68. else if (obj is string)
  69. {
  70. return string.Equals(Name, obj as string, StringComparison.OrdinalIgnoreCase);
  71. }
  72. return base.Equals(obj);
  73. }
  74. public override int GetHashCode()
  75. {
  76. return ID.GetHashCode();
  77. }
  78. public override string ToString()
  79. {
  80. return Name;
  81. }
  82. public void Init(TheaterType theater)
  83. {
  84. var size = new Size(Globals.OriginalTileWidth / 4, Globals.OriginalTileWidth / 4);
  85. var iconSize = Math.Max(IconWidth, IconHeight);
  86. var thumbnail = new Bitmap(iconSize * size.Width, iconSize * size.Height);
  87. var mask = new bool[IconWidth, IconHeight];
  88. Array.Clear(mask, 0, mask.Length);
  89. bool found = false;
  90. using (var g = Graphics.FromImage(thumbnail))
  91. {
  92. g.Clear(Color.Transparent);
  93. int icon = 0;
  94. for (var y = 0; y < IconHeight; ++y)
  95. {
  96. for (var x = 0; x < IconWidth; ++x, ++icon)
  97. {
  98. if (Globals.TheTilesetManager.GetTileData(theater.Tilesets, Name, icon, out Tile tile))
  99. {
  100. g.DrawImage(tile.Image, x * size.Width, y * size.Height, size.Width, size.Height);
  101. found = mask[x, y] = true;
  102. }
  103. }
  104. }
  105. }
  106. Thumbnail = found ? thumbnail : null;
  107. IconMask = mask;
  108. }
  109. }
  110. }