PageRenderTime 50ms CodeModel.GetById 13ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 1ms

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