Advertisement
Guest User

DataContainer

a guest
Jul 4th, 2015
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using Vexe.Runtime.Types;
  6.  
  7. namespace IOU
  8. {
  9.     [Serializable]
  10.     public class DataContainer : SerializableDictionary<string, GameData>, IDisposable
  11.     {
  12.         new public Dictionary<string, GameData> AsDictionary
  13.         {
  14.             get { return new Dictionary<string, GameData>(this); }
  15.         }
  16.  
  17.         public string name { get { return this["Name", "Empty"]; } set { this["Name"] = value; } }
  18.  
  19.         public override string ToString()
  20.         {
  21.             return name + " (" + Count + ")";
  22.         }
  23.  
  24.         private static readonly Stack<DataContainer> pool = new Stack<DataContainer>();
  25.  
  26.         public static DataContainer get()
  27.         {
  28.             if (pool.Count == 0)
  29.                 return new DataContainer();
  30.             return pool.Pop();
  31.         }
  32.  
  33.         new public GameData this[string key]
  34.         {
  35.             set { base[key] = value; }
  36.             get { return this[key, -1]; }
  37.         }
  38.  
  39.         public bool has(string key)
  40.         {
  41.             return ContainsKey(key);
  42.         }
  43.  
  44.         public void Dispose()
  45.         {
  46.             Clear();
  47.             pool.Push(this);
  48.         }
  49.  
  50.         public void clone_into<T>(T container) where T : DataContainer
  51.         {
  52.             var iter = GetEnumerator();
  53.             while(iter.MoveNext())
  54.             {
  55.                 var item = iter.Current;
  56.                 container[item.Key] = item.Value;
  57.             }
  58.         }
  59.  
  60.         public T clone<T>() where T : DataContainer, new()
  61.         {
  62.             T container = new T();
  63.             clone_into(container);
  64.             return container;
  65.         }
  66.  
  67.         public DataContainer clone()
  68.         {
  69.             return clone<DataContainer>();
  70.         }
  71.     }
  72.  
  73.     public enum DATA_TYPE { NONE, INT, FLOAT, BOOL, STRING }
  74.  
  75.     [Serializable]
  76.     public struct GameData : IEquatable<GameData>
  77.     {
  78.         [SerializeField] float _value;
  79.         [SerializeField] string _text;
  80.         [SerializeField] DATA_TYPE _type;
  81.  
  82.         public DATA_TYPE Type
  83.         {
  84.             get { return _type; }
  85.         }
  86.  
  87.         GameData(string value)
  88.         {
  89.             _value = -1;
  90.             _text = value;
  91.             _type = DATA_TYPE.STRING;
  92.         }
  93.  
  94.         GameData(int value)
  95.         {
  96.             _value = value;
  97.             _type = DATA_TYPE.INT;
  98.             _text = null;
  99.         }
  100.  
  101.         GameData(bool value)
  102.         {
  103.             _value = value ? 1 : 0;
  104.             _type = DATA_TYPE.BOOL;
  105.             _text = null;
  106.         }
  107.  
  108.         GameData(float value)
  109.         {
  110.             _value = value;
  111.             _type = DATA_TYPE.FLOAT;
  112.             _text = null;
  113.         }
  114.  
  115.         public static implicit operator GameData(int value)
  116.         {
  117.             return new GameData(value);
  118.         }
  119.  
  120.         public static implicit operator GameData(bool value)
  121.         {
  122.             return new GameData(value);
  123.         }
  124.  
  125.         public static implicit operator GameData(float value)
  126.         {
  127.             return new GameData(value);
  128.         }
  129.  
  130.         public static implicit operator GameData(string value)
  131.         {
  132.             return new GameData(value);
  133.         }
  134.  
  135.         public static implicit operator float(GameData data)
  136.         {
  137.             return data._value;
  138.         }
  139.  
  140.         public static implicit operator string(GameData data)
  141.         {
  142.             return data._text;
  143.         }
  144.  
  145.         public static implicit operator int(GameData data)
  146.         {
  147.             return (int)data._value;
  148.         }
  149.  
  150.         public static implicit operator bool(GameData data)
  151.         {
  152.             return data._value > 0 || !string.IsNullOrEmpty(data._text);
  153.         }
  154.  
  155.         public static GameData operator +(GameData left, GameData right)
  156.         {
  157.             return new GameData(left._value + right._value);
  158.         }
  159.  
  160.         public static GameData operator ++(GameData data)
  161.         {
  162.             return data + data;
  163.         }
  164.  
  165.         public static GameData operator -(GameData left, GameData right)
  166.         {
  167.             return new GameData(left._value - right._value);
  168.         }
  169.  
  170.         public static GameData operator --(GameData data)
  171.         {
  172.             return data - data;
  173.         }
  174.  
  175.         public static bool operator ==(GameData left, int right)
  176.         {
  177.             return (int)left._value == right;
  178.         }
  179.  
  180.         public static bool operator !=(GameData left, int right)
  181.         {
  182.             return !(left == right);
  183.         }
  184.  
  185.         public static bool operator ==(GameData left, float right)
  186.         {
  187.             return Mathf.Approximately(left._value, right);
  188.         }
  189.  
  190.         public static bool operator !=(GameData left, float right)
  191.         {
  192.             return !(left == right);
  193.         }
  194.  
  195.         public static bool operator ==(GameData left, GameData right)
  196.         {
  197.             return left.Equals(right);
  198.         }
  199.  
  200.         public static bool operator !=(GameData left, GameData right)
  201.         {
  202.             return !left.Equals(right);
  203.         }
  204.  
  205.         public bool Equals(GameData other)
  206.         {
  207.             if (other.Type == DATA_TYPE.STRING)
  208.                 return other._text == _text;
  209.             return Mathf.Approximately(_value, other._value);
  210.         }
  211.  
  212.         public override bool Equals(object other)
  213.         {
  214.             if (other == null || other.GetType() != typeof(GameData))
  215.                 return false;
  216.  
  217.             return ((GameData)other).Equals(this);
  218.         }
  219.  
  220.         public override int GetHashCode()
  221.         {
  222.             if (_type == DATA_TYPE.STRING)
  223.                 return _text.GetHashCode();
  224.             return _value.GetHashCode();
  225.         }
  226.  
  227.         public override string ToString()
  228.         {
  229.             switch(_type)
  230.             {
  231.                 case DATA_TYPE.NONE: return "None";
  232.                 case DATA_TYPE.INT: return string.Format("{0}", (int)_value);
  233.                 case DATA_TYPE.FLOAT: return string.Format("{0}f", _value);
  234.                 case DATA_TYPE.BOOL: return string.Format("{0}", _value > 0 ? "true" : "false");
  235.                 case DATA_TYPE.STRING: return _text;
  236.             }
  237.  
  238.             throw new Exception("Unsupported data type: " + _type);
  239.         }
  240.     }
  241. }
  242.  
  243.  
  244.  
  245. // ----------------------------- EDITOR/GUI
  246.  
  247. using System;
  248. using IOU;
  249. using UnityEditor;
  250. using Vexe.Editor;
  251. using Vexe.Editor.Drawers;
  252. using Vexe.Editor.Types;
  253. using Vexe.Runtime.Extensions;
  254. using Vexe.Runtime.Types;
  255.  
  256. [InitializeOnLoad]
  257. public static class CustomDrawersMapper
  258. {
  259.     static CustomDrawersMapper()
  260.     {
  261.         var mapper = MemberDrawersHandler.Mapper;
  262.         mapper.Insert<GameData, GameDataDrawer>()
  263.               .Insert<DataContainer, ContainerDrawer>(true);
  264.     }
  265. }
  266.  
  267. public class GameDataDrawer : ObjectDrawer<GameData>
  268. {
  269.     public override void OnGUI()
  270.     {
  271.         using (gui.Horizontal())
  272.         {
  273.             gui.Space(2.5f);
  274.             using (gui.LabelWidth(18f))
  275.             {
  276.                 const string assign = "=";
  277.                 switch(memberValue.Type)
  278.                 {
  279.                     case DATA_TYPE.NONE:
  280.                     {
  281.                         var type = gui.EnumPopup(DATA_TYPE.NONE);
  282.                         switch(type)
  283.                         {
  284.                             case DATA_TYPE.INT: memberValue = 1; break;
  285.                             case DATA_TYPE.FLOAT: memberValue = 1f; break;
  286.                             case DATA_TYPE.BOOL: memberValue = true; break;
  287.                             case DATA_TYPE.STRING: memberValue = "New Value"; break;
  288.                         }
  289.                     }; break;
  290.                     case DATA_TYPE.INT: memberValue = gui.Int(assign, memberValue); break;
  291.                     case DATA_TYPE.FLOAT: memberValue = gui.Float(assign, memberValue); break;
  292.                     case DATA_TYPE.BOOL: memberValue = gui.Toggle(assign, memberValue); break;
  293.                     case DATA_TYPE.STRING: memberValue = gui.Text(assign, memberValue); break;
  294.                 }
  295.             }
  296.         }
  297.     }
  298. }
  299.  
  300. public class ContainerDrawer : ObjectDrawer<DataContainer>
  301. {
  302.     IDictionaryDrawer<string, GameData> drawer;
  303.  
  304.     protected override void Initialize()
  305.     {
  306.         var display = new DisplayAttribute(Dict.HorizontalPairs | Dict.Filter | Dict.ManualAlloc);
  307.         var memberDisplay = member.Attributes.GetAttribute<DisplayAttribute>();
  308.         if (memberDisplay != null)
  309.             EditorMember.CombineDisplays(src: memberDisplay, dest: display);
  310.  
  311.         drawer = new IDictionaryDrawer<string, GameData>();
  312.         drawer.Initialize(member, new Attribute[] { display }, gui);
  313.         drawer.UpdateCount = false;
  314.     }
  315.  
  316.     public override void OnGUI()
  317.     {
  318.         if (memberValue == null)
  319.             member.DisplayText = "null (0)";
  320.         else
  321.             member.DisplayText = memberValue.ToString();
  322.  
  323.         drawer.OnGUI();
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement