// ===================================================================== // Copyright 2013-2017 Fluffy Underware // All rights reserved // // http://www.fluffyunderware.com // ===================================================================== using UnityEngine; using UnityEditor; using System.Collections.Generic; using System; using FluffyUnderware.DevTools; namespace FluffyUnderware.DevToolsEditor { public abstract class DTProject : IComparable { public string Identifier { get; private set; } public string Version { get; private set; } public DTResource Resource { get; protected set; } [Obsolete("Assumed to be always true")] public bool ShowToolbarInAllSceneViews = false; public DTToolbarOrientation ToolbarOrientation { get { return mTBOrientation; } set { if (mTBOrientation != value) { mTBOrientation = value; if (!string.IsNullOrEmpty(Identifier)) { SetEditorPrefs("ToolbarOrientation", ToolbarOrientation); } else DTLog.LogError("[DevTools] Project " + this.GetType().Name + "missing identifier!"); DTToolbar.RecalcItemSize = true; } } } public DTToolbarMode ToolbarMode { get { return mTBMode; } set { if (mTBMode != value) { mTBMode = value; if (!string.IsNullOrEmpty(Identifier)) { SetEditorPrefs("ToolbarMode", ToolbarMode); } else DTLog.LogError("[DevTools] Project " + this.GetType().Name + "missing identifier!"); DTToolbar.RecalcItemSize = true; } } } Dictionary AdditionalKeyBindings = new Dictionary(); internal List ToolbarItems = new List(); static DTToolbarOrientation mTBOrientation = DTToolbarOrientation.Left; static DTToolbarMode mTBMode = DTToolbarMode.Full; protected DTProject(string identifier, string version) { Identifier = identifier; Version = version; LoadPreferences(); } internal void Clear() { ToolbarItems.Clear(); AdditionalKeyBindings.Clear(); } public virtual void ResetPreferences() { } public virtual void LoadPreferences() { if (!string.IsNullOrEmpty(Identifier)) { // Upgrade? string ver=GetEditorPrefs("Version", Version); if (string.Compare(ver, Version)==-1)// (ver != Version) { UpgradePreferences(ver); SetEditorPrefs("Version", Version); } mTBMode=GetEditorPrefs("ToolbarMode", ToolbarMode); mTBOrientation=GetEditorPrefs("ToolbarOrientation", ToolbarOrientation); } else DTLog.LogError("[DevTools] Project "+this.GetType().Name+"missing identifier!"); } public virtual void SavePreferences() { if (!string.IsNullOrEmpty(Identifier)) { SetEditorPrefs("Version",Version); SetEditorPrefs("ToolbarMode", ToolbarMode); SetEditorPrefs("ToolbarOrientation", ToolbarOrientation); } else DTLog.LogError("[DevTools] Project " + this.GetType().Name + "missing identifier!"); } public List GetProjectBindings() { List res = new List(); foreach (DTToolbarItem item in ToolbarItems) foreach (EditorKeyBinding binding in item.KeyBindings) if (binding.ShowInPrefs) res.Add(binding); res.AddRange(AdditionalKeyBindings.Values); res.Sort(); return res; } public EditorKeyBinding RegisterKeyBinding(EditorKeyBinding binding) { EditorKeyBinding b; if (AdditionalKeyBindings.TryGetValue(binding.Name,out b)) return b; else { setKeyBindingFromPrefs(binding,false); AdditionalKeyBindings.Add(binding.Name, binding); return binding; } } /// /// For all EditorKeyBindings registered to ToolbarItems of this project, check if Name is unique /// /// internal bool CheckKeyBindingNamesAreUnique() { Dictionary dict = new Dictionary(); object exist; foreach (DTToolbarItem item in ToolbarItems) foreach (EditorKeyBinding binding in item.KeyBindings) { if (dict.TryGetValue(binding.Name, out exist)){ DTLog.LogError(string.Format("[DevTools] KeyBindings need unique names! ({0} and {1}",item.GetType().Name,exist.GetType().Name)); return false; } else dict.Add(binding.Name,item); } return true; } void setKeyBindingFromPrefs(EditorKeyBinding binding, bool removePrefsIfEqual=true) { string newBindString = GetEditorPrefs(binding.Name, ""); if (!string.IsNullOrEmpty(newBindString) ){ // Same binding? Then remove prefs setting if (removePrefsIfEqual && newBindString == binding.ToPrefsString()) DeleteEditorPrefs(binding.Name); else // load and set new binding binding.Set(newBindString); } } /// /// For all EditorKeyBindings registered to ToolbarItems of this project, manage remappings stored in EditorPrefs /// internal void LoadKeyBindingRemappings() { // Toolbars foreach (DTToolbarItem item in ToolbarItems) for (int i=0;i(string key, T value) { DT.SetEditorPrefs(Identifier + "." + key, value); } public T GetEditorPrefs(string key, T defaultValue) { return DT.GetEditorPrefs(Identifier + "." + key, defaultValue); } public string[] GetEditorPrefs(string key) { return DT.GetEditorPrefs(Identifier + "." + key); } public bool HasEditorPrefs(string key) { return EditorPrefs.HasKey(Identifier + "." + key); } public void DeleteEditorPrefs(string key) { EditorPrefs.DeleteKey(Identifier + "." + key); } public T FindItem() where T:DTToolbarItem { Type t = typeof(T); for (int i = 0; i < ToolbarItems.Count; i++) if (ToolbarItems[i].GetType() == t) return (T)ToolbarItems[i]; return null; } internal void SetRadioGroupState(DTToolbarRadioButton active) { for (int i = 0; i < ToolbarItems.Count; i++) { DTToolbarRadioButton rad = ToolbarItems[i] as DTToolbarRadioButton; if (rad && rad.RadioGroup.Equals(active.RadioGroup, StringComparison.CurrentCultureIgnoreCase)) rad.mOn = (rad == active); } } public int CompareTo(object obj) { DTProject other = (DTProject)obj; return Identifier.CompareTo(other.Identifier); } } }