using UnityEngine; using System.Collections.Generic; namespace UltraControls { [ExecuteInEditMode] public class Handler : MonoBehaviour { public static uint InvalidHandle = 0; public static bool is_singleton_lived { get { return s_instance != null; } } public static Handler singleton { get { if (s_instance != null) { return s_instance; } s_instance = GameObject.FindObjectOfType(); if (s_instance != null) { return s_instance; } GameObject obj = new GameObject("UltraControls::Handler"); obj.hideFlags = HideFlags.DontSave; s_instance = obj.AddComponent(); return s_instance; } } void Awake() { //Debug.Log("UltraControls.Handler.Awake()"); if (s_instance != null && s_instance != this) { #if UNITY_EDITOR DestroyImmediate(this.gameObject); #else Destroy(this.gameObject); #endif return; } s_instance = this; if (Application.isPlaying) { DontDestroyOnLoad(this); } } void OnEnable() { //Debug.Log("UltraControls.Handler.OnEnable()"); if (m_is_enable) { return; } NativeLib.Init(); m_map_info = new Dictionary(); m_is_enable = true; } void Start() { //Debug.Log("UltraControls.Handler.Start()"); } void Update() { } void OnDisable() { //Debug.Log("UltraControls.Handler.OnDisable()"); m_is_enable = false; s_instance = null; NativeLib.Term(); } public bool IsExisted(uint handle) { if (!m_is_enable) return false; return m_map_info.ContainsKey(handle); } public uint Create(string name, uint max_ctrls) { if (!m_is_enable) return InvalidHandle; uint handle; int ret = NativeLib.CreateHandle(name, max_ctrls, out handle); if (ret < 0) { return InvalidHandle; } UltraInfo info; if (ret == 1 && m_map_info.TryGetValue(handle, out info)) { ++info.ref_count; return handle; } info = new UltraInfo(); info.raw_ctrls = Utils.AllocAryRawCtrl(max_ctrls); info.ref_count = 1; m_map_info.Add(handle, info); return handle; } public void Destroy(uint handle) { if (!m_is_enable) return; if (handle == InvalidHandle) { return; } UltraInfo info; if (m_map_info.TryGetValue(handle, out info)) { if (--info.ref_count > 0) { return; } if (info.controls != null) { foreach (Control ctrl in info.controls) { ctrl.Die(); } } m_map_info.Remove(handle); NativeLib.DestroyHandle(handle); } } public Control[] FindGetter(uint handle) { if (!m_is_enable) return null; UltraInfo info; if (!m_map_info.TryGetValue(handle, out info)) { return null; } if (info.controls != null) { return info.controls; } uint count = 0; int ret = NativeLib.FindGetter(handle, info.raw_ctrls, (uint)info.raw_ctrls.Length, out count); if (ret != 1) { return null; } info.controls = new Control[count]; for (uint i = 0; i < count; ++i) { info.controls[i] = new Control(ref info.raw_ctrls[i]); } return info.controls; } public void SetControls(uint handle) { if (!m_is_enable) return; UltraInfo info; if (!m_map_info.TryGetValue(handle, out info)) { return; } if (info.controls == null) { return; } for (uint i = 0; i < info.controls.Length; ++i) { Control ctrl = info.controls[i]; if (ctrl.isDirty) { info.is_dirty = true; info.raw_ctrls[i].data = ctrl.data; } } if (!info.is_dirty) { return; } info.is_internal_dirty = true; int ret = NativeLib.SetControls(handle, info.raw_ctrls, (uint)info.raw_ctrls.Length); if (ret == 1) { info.is_dirty = false; } } public Control[] SetupGetter(uint handle, List list) { if (!m_is_enable) return null; UltraInfo info; if (!m_map_info.TryGetValue(handle, out info)) { return null; } int max_count = System.Math.Min(list.Count, info.raw_ctrls.Length); uint ok_count = 0; for (int i = 0; i < max_count; ++i) { bool yes = mf_SetupRawCtrl(list[i], ref info.raw_ctrls[ok_count]); if (!yes) { continue; } ++ok_count; } int ret = NativeLib.CreateGetter(handle, info.raw_ctrls, ok_count); if (ret < 0) { return null; } info.controls = new Control[ok_count]; for (uint i = 0; i < ok_count; ++i) { info.controls[i] = new Control(ref info.raw_ctrls[i]); } return info.controls; } public bool CheckControls(uint handle) { if (!m_is_enable) return false; UltraInfo info; if (!m_map_info.TryGetValue(handle, out info)) { return false; } if (info.controls == null) { return false; } if (info.is_internal_dirty) { info.is_internal_dirty = false; return true; } uint count = 0; int ret = NativeLib.CheckControls(handle, info.raw_ctrls, (uint)info.raw_ctrls.Length, out count); if (ret <= 0) { return false; } count = System.Math.Min(count, (uint)info.controls.Length); for (uint i = 0; i < count; ++i) { info.controls[i].data = info.raw_ctrls[i].data; } return true; } private class UltraInfo { public RawCtrl[] raw_ctrls; public Control[] controls; public uint ref_count; public bool is_dirty; public bool is_internal_dirty; } //[SerializeField] private static Handler s_instance; private Dictionary m_map_info; private bool m_is_enable; private bool mf_SetupRawCtrl(ControlInfo info, ref RawCtrl raw_ctrl) { CtrlType type = CtrlType.Unknown; if (info.m_type == typeof(string)) { type = CtrlType.Text; System.Array.Clear(raw_ctrl.data, 0, raw_ctrl.data.Length); byte[] bytes = System.Text.Encoding.ASCII.GetBytes((string)info.m_value_default); System.Buffer.BlockCopy(bytes, 0, raw_ctrl.data, 0, System.Math.Min(bytes.Length, raw_ctrl.data.Length)); raw_ctrl.data[raw_ctrl.data.Length - 1] = 0; } else if (info.m_type == typeof(Color32)) { type = CtrlType.Color; Utils.SetColor32((Color32)info.m_value_default, raw_ctrl.data);; } else { CtrlValues values = Utils.AllocCtrlVaules(); if (info.m_type == typeof(bool)) { type = CtrlType.Bool; values.val[0] = ((bool)info.m_value_default)? 1.0f: 0.0f; } else if (info.m_type == typeof(float)) { type = CtrlType.Float; values.val[0] = (float)info.m_value_default; if (info.m_has_range) { values.min[0] = (float)info.m_value_min; values.max[0] = (float)info.m_value_max; } } else if (info.m_type == typeof(int)) { type = CtrlType.Int; values.val[0] = (int)info.m_value_default; if (info.m_has_range) { values.min[0] = (int)info.m_value_min; values.max[0] = (int)info.m_value_max; } } else if (info.m_type == typeof(Vector2)) { type = CtrlType.Vec2F; Utils.SetVec2((Vector2)info.m_value_default, values.val); if (info.m_has_range) { Utils.SetVec2((Vector2)info.m_value_min, values.min); Utils.SetVec2((Vector2)info.m_value_max, values.max); } } else if (info.m_type == typeof(Vector3)) { type = CtrlType.Vec3F; Utils.SetVec3((Vector3)info.m_value_default, values.val); if (info.m_has_range) { Utils.SetVec3((Vector3)info.m_value_min, values.min); Utils.SetVec3((Vector3)info.m_value_max, values.max); } } else if (info.m_type == typeof(Vector4)) { type = CtrlType.Vec4F; Utils.SetVec4((Vector4)info.m_value_default, values.val); if (info.m_has_range) { Utils.SetVec4((Vector4)info.m_value_min, values.min); Utils.SetVec4((Vector4)info.m_value_max, values.max); } } else if (info.m_type == typeof(Color)) { type = CtrlType.ColorF; Utils.SetColor((Color)info.m_value_default, values.val); } else { return false; } Utils.StructureToBytes(values, raw_ctrl.data); } raw_ctrl.name = info.m_name; raw_ctrl.type = (uint)type; return true; } }//Handler }//Namespace UltraControls