Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using Vexe.Runtime.Types;
- namespace IOU
- {
- [Serializable]
- public class DataContainer : SerializableDictionary<string, GameData>, IDisposable
- {
- new public Dictionary<string, GameData> AsDictionary
- {
- get { return new Dictionary<string, GameData>(this); }
- }
- public string name { get { return this["Name", "Empty"]; } set { this["Name"] = value; } }
- public override string ToString()
- {
- return name + " (" + Count + ")";
- }
- private static readonly Stack<DataContainer> pool = new Stack<DataContainer>();
- public static DataContainer get()
- {
- if (pool.Count == 0)
- return new DataContainer();
- return pool.Pop();
- }
- new public GameData this[string key]
- {
- set { base[key] = value; }
- get { return this[key, -1]; }
- }
- public bool has(string key)
- {
- return ContainsKey(key);
- }
- public void Dispose()
- {
- Clear();
- pool.Push(this);
- }
- public void clone_into<T>(T container) where T : DataContainer
- {
- var iter = GetEnumerator();
- while(iter.MoveNext())
- {
- var item = iter.Current;
- container[item.Key] = item.Value;
- }
- }
- public T clone<T>() where T : DataContainer, new()
- {
- T container = new T();
- clone_into(container);
- return container;
- }
- public DataContainer clone()
- {
- return clone<DataContainer>();
- }
- }
- public enum DATA_TYPE { NONE, INT, FLOAT, BOOL, STRING }
- [Serializable]
- public struct GameData : IEquatable<GameData>
- {
- [SerializeField] float _value;
- [SerializeField] string _text;
- [SerializeField] DATA_TYPE _type;
- public DATA_TYPE Type
- {
- get { return _type; }
- }
- GameData(string value)
- {
- _value = -1;
- _text = value;
- _type = DATA_TYPE.STRING;
- }
- GameData(int value)
- {
- _value = value;
- _type = DATA_TYPE.INT;
- _text = null;
- }
- GameData(bool value)
- {
- _value = value ? 1 : 0;
- _type = DATA_TYPE.BOOL;
- _text = null;
- }
- GameData(float value)
- {
- _value = value;
- _type = DATA_TYPE.FLOAT;
- _text = null;
- }
- public static implicit operator GameData(int value)
- {
- return new GameData(value);
- }
- public static implicit operator GameData(bool value)
- {
- return new GameData(value);
- }
- public static implicit operator GameData(float value)
- {
- return new GameData(value);
- }
- public static implicit operator GameData(string value)
- {
- return new GameData(value);
- }
- public static implicit operator float(GameData data)
- {
- return data._value;
- }
- public static implicit operator string(GameData data)
- {
- return data._text;
- }
- public static implicit operator int(GameData data)
- {
- return (int)data._value;
- }
- public static implicit operator bool(GameData data)
- {
- return data._value > 0 || !string.IsNullOrEmpty(data._text);
- }
- public static GameData operator +(GameData left, GameData right)
- {
- return new GameData(left._value + right._value);
- }
- public static GameData operator ++(GameData data)
- {
- return data + data;
- }
- public static GameData operator -(GameData left, GameData right)
- {
- return new GameData(left._value - right._value);
- }
- public static GameData operator --(GameData data)
- {
- return data - data;
- }
- public static bool operator ==(GameData left, int right)
- {
- return (int)left._value == right;
- }
- public static bool operator !=(GameData left, int right)
- {
- return !(left == right);
- }
- public static bool operator ==(GameData left, float right)
- {
- return Mathf.Approximately(left._value, right);
- }
- public static bool operator !=(GameData left, float right)
- {
- return !(left == right);
- }
- public static bool operator ==(GameData left, GameData right)
- {
- return left.Equals(right);
- }
- public static bool operator !=(GameData left, GameData right)
- {
- return !left.Equals(right);
- }
- public bool Equals(GameData other)
- {
- if (other.Type == DATA_TYPE.STRING)
- return other._text == _text;
- return Mathf.Approximately(_value, other._value);
- }
- public override bool Equals(object other)
- {
- if (other == null || other.GetType() != typeof(GameData))
- return false;
- return ((GameData)other).Equals(this);
- }
- public override int GetHashCode()
- {
- if (_type == DATA_TYPE.STRING)
- return _text.GetHashCode();
- return _value.GetHashCode();
- }
- public override string ToString()
- {
- switch(_type)
- {
- case DATA_TYPE.NONE: return "None";
- case DATA_TYPE.INT: return string.Format("{0}", (int)_value);
- case DATA_TYPE.FLOAT: return string.Format("{0}f", _value);
- case DATA_TYPE.BOOL: return string.Format("{0}", _value > 0 ? "true" : "false");
- case DATA_TYPE.STRING: return _text;
- }
- throw new Exception("Unsupported data type: " + _type);
- }
- }
- }
- // ----------------------------- EDITOR/GUI
- using System;
- using IOU;
- using UnityEditor;
- using Vexe.Editor;
- using Vexe.Editor.Drawers;
- using Vexe.Editor.Types;
- using Vexe.Runtime.Extensions;
- using Vexe.Runtime.Types;
- [InitializeOnLoad]
- public static class CustomDrawersMapper
- {
- static CustomDrawersMapper()
- {
- var mapper = MemberDrawersHandler.Mapper;
- mapper.Insert<GameData, GameDataDrawer>()
- .Insert<DataContainer, ContainerDrawer>(true);
- }
- }
- public class GameDataDrawer : ObjectDrawer<GameData>
- {
- public override void OnGUI()
- {
- using (gui.Horizontal())
- {
- gui.Space(2.5f);
- using (gui.LabelWidth(18f))
- {
- const string assign = "=";
- switch(memberValue.Type)
- {
- case DATA_TYPE.NONE:
- {
- var type = gui.EnumPopup(DATA_TYPE.NONE);
- switch(type)
- {
- case DATA_TYPE.INT: memberValue = 1; break;
- case DATA_TYPE.FLOAT: memberValue = 1f; break;
- case DATA_TYPE.BOOL: memberValue = true; break;
- case DATA_TYPE.STRING: memberValue = "New Value"; break;
- }
- }; break;
- case DATA_TYPE.INT: memberValue = gui.Int(assign, memberValue); break;
- case DATA_TYPE.FLOAT: memberValue = gui.Float(assign, memberValue); break;
- case DATA_TYPE.BOOL: memberValue = gui.Toggle(assign, memberValue); break;
- case DATA_TYPE.STRING: memberValue = gui.Text(assign, memberValue); break;
- }
- }
- }
- }
- }
- public class ContainerDrawer : ObjectDrawer<DataContainer>
- {
- IDictionaryDrawer<string, GameData> drawer;
- protected override void Initialize()
- {
- var display = new DisplayAttribute(Dict.HorizontalPairs | Dict.Filter | Dict.ManualAlloc);
- var memberDisplay = member.Attributes.GetAttribute<DisplayAttribute>();
- if (memberDisplay != null)
- EditorMember.CombineDisplays(src: memberDisplay, dest: display);
- drawer = new IDictionaryDrawer<string, GameData>();
- drawer.Initialize(member, new Attribute[] { display }, gui);
- drawer.UpdateCount = false;
- }
- public override void OnGUI()
- {
- if (memberValue == null)
- member.DisplayText = "null (0)";
- else
- member.DisplayText = memberValue.ToString();
- drawer.OnGUI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement