/TEditXna/Terraria/World.Settings.cs

https://github.com/Surfpup/Terraria-Map-Editor · C# · 363 lines · 312 code · 43 blank · 8 comment · 44 complexity · 6729c587e17ecd4d461b479f2c019045 MD5 · raw file

  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Xml.Linq;
  10. using BCCL.Geometry.Primitives;
  11. using XNA = Microsoft.Xna.Framework;
  12. using TEditXNA.Terraria.Objects;
  13. namespace TEditXNA.Terraria
  14. {
  15. public partial class World
  16. {
  17. public static uint CompatibleVersion = 39;
  18. private static readonly Dictionary<string, XNA.Color> _globalColors = new Dictionary<string, XNA.Color>();
  19. private static readonly Dictionary<string, int> _npcIds = new Dictionary<string, int>();
  20. private static readonly Dictionary<int, int> _npcFrames = new Dictionary<int, int>();
  21. private static readonly Dictionary<byte, string> _prefix = new Dictionary<byte, string>();
  22. public static readonly ObservableCollection<ItemProperty> _itemProperties = new ObservableCollection<ItemProperty>();
  23. private static Collection<TileProperty> _tileProperties = new Collection<TileProperty>();
  24. private static readonly ObservableCollection<TileProperty> _tileBricks = new ObservableCollection<TileProperty>();
  25. public static Collection<WallProperty> _wallProperties = new Collection<WallProperty>();
  26. private static readonly ObservableCollection<Sprite> _sprites = new ObservableCollection<Sprite>();
  27. private static readonly Dictionary<Key, string> _shortcuts = new Dictionary<Key, string>();
  28. private static readonly Dictionary<int, ItemProperty> _itemLookup = new Dictionary<int, ItemProperty>();
  29. private static Vector2 _appSize;
  30. internal static string AltC;
  31. static World()
  32. {
  33. var settingspath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "settings.xml");
  34. LoadObjectDbXml(settingspath);
  35. Sprites.Add(new Sprite());
  36. }
  37. private static IEnumerable<TOut> StringToList<TOut>(string xmlcsv)
  38. {
  39. if (!string.IsNullOrWhiteSpace(xmlcsv))
  40. {
  41. string[] split = xmlcsv.Split(',');
  42. foreach (var s in split)
  43. {
  44. yield return (TOut)Convert.ChangeType(s, typeof(TOut));
  45. }
  46. }
  47. }
  48. private static T InLineEnumTryParse<T>(string str) where T : struct
  49. {
  50. T result;
  51. Enum.TryParse(str, true, out result);
  52. return result;
  53. }
  54. private static Vector2Short StringToVector2Short(string v, short defaultX = 0, short defaultY = 0)
  55. {
  56. if (!string.IsNullOrWhiteSpace(v))
  57. {
  58. short x = 0;
  59. short y = 0;
  60. var split = v.Split(',');
  61. if (split.Length == 2)
  62. {
  63. if (short.TryParse(split[0], out x) && short.TryParse(split[1], out y))
  64. return new Vector2Short(x, y);
  65. }
  66. }
  67. return new Vector2Short(defaultX, defaultY);
  68. }
  69. private static Color ColorFromString(string colorstring)
  70. {
  71. if (!string.IsNullOrWhiteSpace(colorstring))
  72. {
  73. var colorFromString = ColorConverter.ConvertFromString(colorstring);
  74. if (colorFromString != null)
  75. {
  76. return (Color)colorFromString;
  77. }
  78. }
  79. return Colors.Magenta;
  80. }
  81. private static XNA.Color XnaColorFromString(string colorstring)
  82. {
  83. if (!string.IsNullOrWhiteSpace(colorstring))
  84. {
  85. var colorFromString = ColorConverter.ConvertFromString(colorstring);
  86. if (colorFromString != null)
  87. {
  88. var c = (Color)colorFromString;
  89. return XNA.Color.FromNonPremultiplied(c.R, c.G, c.B, c.A);
  90. }
  91. }
  92. return XNA.Color.Magenta;
  93. }
  94. private static void LoadObjectDbXml(string file)
  95. {
  96. var xmlSettings = XElement.Load(file);
  97. // Load Colors
  98. foreach (var xElement in xmlSettings.Elements("GlobalColors").Elements("GlobalColor"))
  99. {
  100. string name = (string)xElement.Attribute("Name");
  101. XNA.Color color = XnaColorFromString((string)xElement.Attribute("Color"));
  102. GlobalColors.Add(name, color);
  103. }
  104. foreach (var xElement in xmlSettings.Elements("Tiles").Elements("Tile"))
  105. {
  106. var curTile = new TileProperty();
  107. // Read XML attributes
  108. curTile.Color = ColorFromString((string)xElement.Attribute("Color"));
  109. curTile.Name = (string)xElement.Attribute("Name");
  110. curTile.Id = (int?)xElement.Attribute("Id") ?? 0;
  111. curTile.IsFramed = (bool?)xElement.Attribute("Framed") ?? false;
  112. curTile.IsSolid = (bool?)xElement.Attribute("Solid") ?? false;
  113. curTile.IsSolidTop = (bool?)xElement.Attribute("SolidTop") ?? false;
  114. curTile.IsLight = (bool?)xElement.Attribute("Light") ?? false;
  115. curTile.FrameSize = StringToVector2Short((string)xElement.Attribute("Size"), 1, 1);
  116. curTile.Placement = InLineEnumTryParse<FramePlacement>((string)xElement.Attribute("Placement"));
  117. curTile.TextureGrid = StringToVector2Short((string)xElement.Attribute("TextureGrid"), 16, 16);
  118. curTile.IsGrass = "Grass".Equals((string)xElement.Attribute("Special")); /* Heathtech */
  119. curTile.IsPlatform = "Platform".Equals((string)xElement.Attribute("Special")); /* Heathtech */
  120. curTile.IsCactus = "Cactus".Equals((string)xElement.Attribute("Special")); /* Heathtech */
  121. curTile.IsStone = (bool?)xElement.Attribute("Stone") ?? false; /* Heathtech */
  122. curTile.CanBlend = (bool?)xElement.Attribute("Blends") ?? false; /* Heathtech */
  123. curTile.MergeWith = (int?)xElement.Attribute("MergeWith") ?? null; /* Heathtech */
  124. foreach (var elementFrame in xElement.Elements("Frames").Elements("Frame"))
  125. {
  126. var curFrame = new FrameProperty();
  127. // Read XML attributes
  128. curFrame.Name = (string)elementFrame.Attribute("Name");
  129. curFrame.Variety = (string)elementFrame.Attribute("Variety");
  130. curFrame.UV = StringToVector2Short((string)elementFrame.Attribute("UV"), 0, 0);
  131. curFrame.Anchor = InLineEnumTryParse<FrameAnchor>((string)elementFrame.Attribute("Anchor"));
  132. // Assign a default name if none existed
  133. if (string.IsNullOrWhiteSpace(curFrame.Name))
  134. curFrame.Name = curTile.Name;
  135. curTile.Frames.Add(curFrame);
  136. Sprites.Add(new Sprite
  137. {
  138. Anchor = curFrame.Anchor,
  139. IsPreviewTexture = false,
  140. Name = curFrame.Name + ", " + curFrame.Variety,
  141. Origin = curFrame.UV,
  142. Size = curTile.FrameSize,
  143. Tile = (byte)curTile.Id,
  144. TileName = curTile.Name
  145. });
  146. if (curTile.FrameSize.X == 0 && curTile.FrameSize.Y == 0)
  147. {
  148. int z = 0;
  149. }
  150. }
  151. if (curTile.Frames.Count == 0 && curTile.IsFramed)
  152. {
  153. var curFrame = new FrameProperty();
  154. // Read XML attributes
  155. curFrame.Name = curTile.Name;
  156. curFrame.Variety = string.Empty;
  157. curFrame.UV = new Vector2Short(0, 0);
  158. //curFrame.Anchor = InLineEnumTryParse<FrameAnchor>((string)xElement.Attribute("Anchor"));
  159. // Assign a default name if none existed
  160. if (string.IsNullOrWhiteSpace(curFrame.Name))
  161. curFrame.Name = curTile.Name;
  162. curTile.Frames.Add(curFrame);
  163. Sprites.Add(new Sprite
  164. {
  165. Anchor = curFrame.Anchor,
  166. IsPreviewTexture = false,
  167. Name = curFrame.Name + ", " + curFrame.Variety,
  168. Origin = curFrame.UV,
  169. Size = curTile.FrameSize,
  170. Tile = (byte)curTile.Id,
  171. TileName = curTile.Name
  172. });
  173. }
  174. TileProperties.Add(curTile);
  175. if (!curTile.IsFramed)
  176. TileBricks.Add(curTile);
  177. }
  178. for (int i = TileProperties.Count; i < 255; i++)
  179. {
  180. TileProperties.Add(new TileProperty(i, "UNKNOWN", Color.FromArgb(255, 255, 0, 255), true));
  181. }
  182. foreach (var xElement in xmlSettings.Elements("Walls").Elements("Wall"))
  183. {
  184. var curWall = new WallProperty();
  185. curWall.Color = ColorFromString((string)xElement.Attribute("Color"));
  186. curWall.Name = (string)xElement.Attribute("Name");
  187. curWall.Id = (int?)xElement.Attribute("Id") ?? -1;
  188. curWall.IsHouse = (bool?)xElement.Attribute("IsHouse") ?? false;
  189. WallProperties.Add(curWall);
  190. }
  191. for (int i = WallProperties.Count; i < 255; i++)
  192. {
  193. WallProperties.Add(new WallProperty(i, "UNKNOWN", Color.FromArgb(255, 255, 0, 255)));
  194. }
  195. foreach (var xElement in xmlSettings.Elements("Items").Elements("Item"))
  196. {
  197. var curItem = new ItemProperty();
  198. curItem.Id = (int?)xElement.Attribute("Id") ?? -1;
  199. curItem.Name = (string)xElement.Attribute("Name");
  200. ItemProperties.Add(curItem);
  201. _itemLookup.Add(curItem.Id, curItem);
  202. }
  203. foreach (var xElement in xmlSettings.Elements("Npcs").Elements("Npc"))
  204. {
  205. int id = (int?)xElement.Attribute("Id") ?? -1;
  206. string name = (string)xElement.Attribute("Name");
  207. NpcIds.Add(name, id);
  208. int frames = (int?)xElement.Attribute("Frames") ?? 16;
  209. NpcFrames.Add(id, frames);
  210. }
  211. foreach (var xElement in xmlSettings.Elements("ItemPrefix").Elements("Prefix"))
  212. {
  213. int id = (int?)xElement.Attribute("Id") ?? -1;
  214. string name = (string)xElement.Attribute("Name");
  215. ItemPrefix.Add((byte)id, name);
  216. }
  217. foreach (var xElement in xmlSettings.Elements("ShortCutKeys").Elements("Shortcut"))
  218. {
  219. var key = InLineEnumTryParse<Key>((string)xElement.Attribute("Key"));
  220. var tool = (string)xElement.Attribute("Tool");
  221. ShortcutKeys.Add(key, tool);
  222. }
  223. XElement appSettings = xmlSettings.Element("App");
  224. int appWidth = (int?)appSettings.Attribute("Width") ?? 800;
  225. int appHeight = (int?)appSettings.Attribute("Height") ?? 600;
  226. _appSize = new Vector2(appWidth, appHeight);
  227. ToolDefaultData.LoadSettings(xmlSettings.Elements("Tools"));
  228. AltC = (string)xmlSettings.Element("AltC");
  229. }
  230. public static TileProperty GetBrickFromColor(byte a, byte r, byte g, byte b)
  231. {
  232. for (int i = 0; i < TileBricks.Count; i++)
  233. {
  234. var curBrick = TileBricks[i];
  235. var aB = curBrick.Color.A;
  236. var rB = curBrick.Color.R;
  237. var gB = curBrick.Color.G;
  238. var bB = curBrick.Color.B;
  239. if (r == rB && g == gB && b == bB)
  240. return curBrick;
  241. }
  242. return null;
  243. }
  244. public static WallProperty GetWallFromColor(byte a, byte r, byte g, byte b)
  245. {
  246. // if it is a global color, skip
  247. foreach (var global in GlobalColors)
  248. {
  249. var aB = global.Value.A;
  250. var rB = global.Value.R;
  251. var gB = global.Value.G;
  252. var bB = global.Value.B;
  253. if (r == rB && g == gB && b == bB)
  254. return null;
  255. }
  256. for (int i = 0; i < WallProperties.Count; i++)
  257. {
  258. var curBrick = WallProperties[i];
  259. var aB = curBrick.Color.A;
  260. var rB = curBrick.Color.R;
  261. var gB = curBrick.Color.G;
  262. var bB = curBrick.Color.B;
  263. if (r == rB && g == gB && b == bB)
  264. return curBrick;
  265. }
  266. return null;
  267. }
  268. public static Dictionary<string, XNA.Color> GlobalColors
  269. {
  270. get { return _globalColors; }
  271. }
  272. public static Dictionary<string, int> NpcIds
  273. {
  274. get { return _npcIds; }
  275. }
  276. public static Dictionary<int, int> NpcFrames
  277. {
  278. get { return _npcFrames; }
  279. }
  280. public static Dictionary<byte, string> ItemPrefix
  281. {
  282. get { return _prefix; }
  283. }
  284. public static Dictionary<Key, string> ShortcutKeys
  285. {
  286. get { return _shortcuts; }
  287. }
  288. public static Collection<TileProperty> TileProperties
  289. {
  290. get { return _tileProperties; }
  291. }
  292. public static ObservableCollection<TileProperty> TileBricks
  293. {
  294. get
  295. {
  296. return _tileBricks;
  297. }
  298. }
  299. public static Collection<WallProperty> WallProperties
  300. {
  301. get { return _wallProperties; }
  302. }
  303. public static ObservableCollection<ItemProperty> ItemProperties
  304. {
  305. get { return _itemProperties; }
  306. }
  307. public static Dictionary<int, ItemProperty> ItemLookupTable
  308. {
  309. get { return _itemLookup; }
  310. }
  311. public static ObservableCollection<Sprite> Sprites
  312. {
  313. get { return _sprites; }
  314. }
  315. internal static Vector2 AppSize
  316. {
  317. get { return _appSize; }
  318. }
  319. }
  320. }