public override List <TypeDescriptor> GetComponentDescriptors() { if (null != _typeNameList) { return(_typeNameList); } _typeNameList = new List <TypeDescriptor>(); var types = EditorReflector.GetAllStyleableClasses(); foreach (var type in types) { var exists = _typeNameList.Exists(delegate(TypeDescriptor descriptor) { return(descriptor.Type == type); }); if (!exists) { _typeNameList.Add(new TypeDescriptor(type.FullName, type, GetComponentIcon(type))); } } return(_typeNameList); }
public static Type ConstructableTypePopup <T>(Rect position, Type currentType, GUIStyle style = null) { Type[] subclasses = EditorReflector.FindConstructableSubClassesWithNull(typeof(T)); string[] subclassNames = EditorReflector.FindConstructableSubClassNamesWithNull(typeof(T)); int currentIndex = Array.IndexOf(subclasses, currentType); return(subclasses[EditorGUI.Popup(position, currentIndex, subclassNames, style ?? EditorStyles.popup)]); }
/// <summary> /// Describes component skins /// </summary> /// <param name="componentType"></param> /// <returns></returns> public static string GetSkins(Type componentType) { if (!typeof(SkinnableComponent).IsAssignableFrom(componentType)) { return(string.Format(@"Skins: Not skinnable." + NewLine + NewLine)); } return(EditorReflector.GetSkins(componentType) + NewLine /* + NewLine*/); }
public bool Run() { var path = EditorUtility.OpenFilePanel( "Choose the existing script", "Assets", EditorSettings.ScriptExtension); if (string.IsNullOrEmpty(path)) // canceled { return(false); } //Debug.Log("path: " + path); /** * 1. Get class name * */ var className = Util.ClassNameFromPath(path); //Debug.Log("className: " + className); //Debug.Log(string.Format(@"AddHandlerScript [adapter: {0}, className: {1}]", Adapter, className)); var component = Adapter.gameObject.GetComponent(className); /** * 1. Check if the component is already attached, but only if not editing (adding new handler) - we'll handle that separately * */ if (null != component) { /*if (!CreatingNewHandler) { * string text = string.Format(@"Script ""{0}"" is already attached to the selected game object.", className); * Debug.LogWarning(text); * EditorUtility.DisplayDialog("Duplicated script", text, "OK"); * return false; * }*/ Data.ScriptAlreadyAttached = true; } Data.ScriptPath = path; Data.ClassName = className; Data.AttachedScriptType = EditorReflector.GetTypeByClassName(className); // Type.GetType(className); Data.Action = CreatingNewHandler ? AddHandlerAction.CreateNewHandlerInExistingScript : AddHandlerAction.AttachExistingScriptAndMapHandler; //Data.Snippet = AssetDatabase.LoadAssetAtPath(path, typeof(string)).ToString(); if (CreatingNewHandler) { // load the old script Data.Snippet = Util.LoadFile(path); //Debug.LogWarning("Data.Snippet: " + Data.Snippet); } //Debug.LogWarning("Data.AttachedScriptType: " + Data.AttachedScriptType); return(true); }
public void Initialize(ReflectedProperty source) { if (!initialized) { subclasses = EditorReflector.FindConstructableSubClasses(source.DeclaredType); List <string> names = subclasses.ToList().Map((s) => s.Name); names.Insert(0, "-- Null --"); subclassNames = names.ToArray(); initialized = true; } }
public static Type ConstructableTypePopup <T>(Rect position, Type currentType, Func <string, string> formatLabels, GUIStyle style = null) { Type[] subclasses = EditorReflector.FindConstructableSubClassesWithNull(typeof(T)); string[] subclassNames = EditorReflector.FindConstructableSubClassNamesWithNull(typeof(T)); for (int i = 0; i < subclassNames.Length; i++) { subclassNames[i] = formatLabels(subclassNames[i]); } int currentIndex = Array.IndexOf(subclasses, currentType); return(subclasses[EditorGUI.Popup(position, currentIndex, subclassNames, style ?? EditorStyles.popup)]); }
public override void OnGUI(Rect position, ReflectedProperty property, GUIContent label = null) { guiRect.SetRect(position); Initialize(property); int index = -1; int newIndex; if (property.Value != null) { index = Array.IndexOf(subclasses, property.Type); } if (index != -1) { Rect rect = guiRect.GetFieldRect(); float width = EditorGUIUtility.labelWidth; Rect r1 = new Rect(rect) { width = width }; Rect r2 = new Rect(rect) { x = rect.x + width, width = rect.width - width }; property.IsExpanded = EditorGUI.Foldout(r1, property.IsExpanded, property.Label); newIndex = EditorGUI.Popup(r2, index + 1, subclassNames) - 1; } else { newIndex = EditorGUI.Popup(guiRect.GetFieldRect(), property.Label, index + 1, subclassNames) - 1; } if (index != newIndex) { property.SetValueAndCopyCompatibleProperties( newIndex == -1 ? null : EditorReflector.MakeInstance(subclasses[newIndex]) ); property.IsExpanded = newIndex != -1; } if (property.IsExpanded && newIndex != -1) { EditorGUI.indentLevel += 2; EditorGUIX.DrawProperties(guiRect.GetRect(), property); EditorGUI.indentLevel -= 2; } }
/// <summary> /// Describes component styles /// </summary> /// <param name="componentType"></param> /// <returns></returns> public static string GetStyles(Type componentType) { var styles = EditorReflector.GetStyleAttributes(componentType); styles.Sort(StyleSort); StringBuilder sb = new StringBuilder(); foreach (var styleAttribute in styles) { sb.AppendLine(styleAttribute.ToString()); } return(string.Format(@"Styles ({0}): {1} {2}", styles.Count, Line, sb) + NewLine /* + NewLine*/); }
/// <summary> /// Initializes the Singleton instance /// </summary> public void Initialize() { _hasAttachedHandlers = EditorReflector.ContainsEventHandlers(AddEventHandlerDialog.Instance.Adapter.gameObject); if (EditorSettings.ScriptExtension == ScriptExtensions.JAVASCRIPT) { _selectedIndex = 0; } else if (EditorSettings.ScriptExtension == ScriptExtensions.CSHARP) { _selectedIndex = 1; } else if (EditorSettings.ScriptExtension == ScriptExtensions.BOO) { _selectedIndex = 2; } }
protected override void CreateMenu(object sender, AddMenuClickedEventArgs args) { Type[] subClasses = EditorReflector.FindSubClasses <Goal>(); GenericMenu menu = new GenericMenu(); for (int i = 0; i < subClasses.Length; i++) { Type type = subClasses[i]; if (!EditorReflector.IsDefaultConstructable(type)) { continue; } GUIContent content = new GUIContent($"Create {type.Name}"); menu.AddItem(content, false, CreateGoal, type); } menu.ShowAsContext(); }
/// <summary> /// Returns the collection of available styles for a given type /// </summary> /// <param name="type"></param> /// <returns></returns> public override Dictionary <string, MemberDescriptor> GetStyleDescriptors(Type type) { //return EditorReflector.GetStyleProperties(type, true); // restrict var attributes = EditorReflector.GetStyleAttributes(type); Dictionary <string, MemberDescriptor> dict = new Dictionary <string, MemberDescriptor>(); foreach (var attribute in attributes) { if (StyleProperty.NonSerializableStyleTypes.Contains(attribute.Type)) { continue; // skip } if (null == attribute.Type) { attribute.Type = typeof(Type); } dict[attribute.Name] = new MemberDescriptor(attribute.Name, attribute.Type, GetStyleIcon(attribute.Type)); // no value needed } return(dict); }
protected override void CreateMenu(object sender, AddMenuClickedEventArgs args) { Type[] subClasses = EditorReflector.FindSubClasses <AIAction>(); GenericMenu menu = new GenericMenu(); for (int i = 0; i < subClasses.Length; i++) { Type type = subClasses[i]; if (!EditorReflector.IsDefaultConstructable(type)) { continue; } GUIContent content = new GUIContent($"Create {StringUtil.NicifyName(type.Name, "Action")}"); menu.AddItem(content, false, () => { propertyAsList.AddElement(EditorReflector.MakeInstance(type)); }); } menu.ShowAsContext(); }
private void DrawTypeSelect(Rect selectRect, ReflectedProperty property) { Type newConsiderationType; if (property.Value != null) { considerationType = property.Value.GetType(); newConsiderationType = EditorGUIX.ConstructableTypePopup <Consideration>(selectRect, considerationType, FormatTypeName, style); } else { considerationType = null; newConsiderationType = EditorGUIX.ConstructableTypePopup <Consideration>(selectRect, considerationType, FormatTypeName, style); } if (newConsiderationType != considerationType) { considerationType = newConsiderationType; Consideration instance = EditorReflector.MakeInstance <Consideration>(considerationType); property.SetValueAndCopyCompatibleProperties(instance); } }
private void LoadAvailableSkins(ComponentAdapter adapter) { var skins = EditorReflector.GetSkins(adapter.ComponentType).ToList(); #if DEBUG if (true) { StringBuilder sb = new StringBuilder(); if (skins.Count == 0) { sb.AppendLine("No available skins."); } else { /*foreach (KeyValuePair<string, Type> pair in skins) * { * sb.AppendLine(string.Format(" {0} -> {1}", pair.Key, pair.Value)); * }*/ foreach (var skin in skins) { sb.AppendLine(string.Format(" -> {0}", skin)); } } /*Debug.Log(string.Format(@"====== Skins ====== * {0}", sb));*/ } #endif _availableSkinClasses = new List <string>(); //if (_couldNotLocateMapper) // list.Add("=== Not found ==="); //list.Add("= Default ="); foreach (Type skinClass in skins) { _availableSkinClasses.Add(skinClass.FullName); } }
public override void OnGUI(Rect position, ReflectedProperty property, GUIContent label = null) { if (property[EvaluatorField].Value == null) { // todo -- context should match the decisions context property[EvaluatorField].Value = new Evaluator <EntityContext>(); } GUIRect guiRect = new GUIRect(position); EditorGUIX.PropertyField(guiRect, property[NameField]); EditorGUIX.TypePopup <DecisionContext>(guiRect, EditorGUIX.TempLabel("Context Type"), property[ContextTypeField]); EditorGUIX.PropertyField(guiRect, property[ActionField]); EditorGUIX.PropertyField(guiRect, property[ContextCreatorField]); EditorGUIX.PropertyField(guiRect, property[EvaluatorField]); if (property[ContextTypeField].DidChange) { property.ApplyChanges(); Type newContextType = (Type)property[ContextTypeField].Value; if (!AssertCompatible(property[ActionField], newContextType)) { property[ActionField].Value = null; } if (!AssertCompatible(property[ContextCreatorField], newContextType)) { property[ContextCreatorField].Value = null; } if (!AssertCompatible(property[EvaluatorField], newContextType)) { property[EvaluatorField].SetValueAndCopyCompatibleProperties( EditorReflector.CreateGenericInstance(typeof(Evaluator <>), newContextType) ); // todo -- for each consideration, make sure its compatible with new context type } } }
private void ProcessEventHandlers(int instanceid) { /** * 1. If there is no component adapter available, this is not what we're looking for * */ if (!_componentAdapterIds.Contains(instanceid)) { return; } GameObject obj = (GameObject)EditorUtility.InstanceIDToObject(instanceid); bool contains = EditorReflector.ContainsEventHandlers(obj); /** * 2. Check for event handler scripts via reflection * */ if (contains && !_eventHandlerScriptIds.Contains(instanceid)) { #if DEBUG if (DebugMode) { Debug.Log(string.Format("Adding eDriven event handler: {0} [{1}]", obj, instanceid)); } #endif _eventHandlerScriptIds.Add(instanceid); } else if (!contains && _eventHandlerScriptIds.Contains(instanceid)) { #if DEBUG if (DebugMode) { Debug.Log(string.Format("Removing eDriven event handler: {0} [{1}]", obj, instanceid)); } #endif _eventHandlerScriptIds.Remove(instanceid); } }
public bool Run() { if (string.IsNullOrEmpty(DefaultClassName)) { throw new Exception("DefaultClassName not defined"); } /** * 1. Get fixed class name * */ var className = EditorReflector.CreateUniqueScriptName(DefaultClassName); /** * 2. Get path * */ var path = GetFilePath(className); if (string.IsNullOrEmpty(path)) // canceled { return(false); } /** * 3. Get chosen class name * */ className = Util.ClassNameFromPath(path); bool isUnique = EditorReflector.IsUniqueScriptName(className); if (!isUnique) { string text = string.Format(@"Script of type ""{0}"" already exists in a project. Please choose a different script name.", className); //Debug.LogWarning(text); EditorUtility.DisplayDialog("Duplicated script name", text, "OK"); Run(); return(false); } /** * 4. Check if the component is already attached * */ var component = Adapter.gameObject.GetComponent(className); if (null != component) { string text = string.Format(@"Script ""{0}"" is already attached to the selected game object.", className); //Debug.LogWarning(text); EditorUtility.DisplayDialog("Duplicated script", text, "OK"); Run(); return(false); } Data.ScriptPath = path; Data.ClassName = className; //Data.AttachedScriptType = ReflectionUtil.GetTypeByClassName(className); // Type.GetType(className); //Debug.LogWarning("Data.AttachedScriptType: " + Data.AttachedScriptType); Data.Action = AddHandlerAction.CreateNewScriptAndHandler; return(true); }
private void CreateGoal(object goalType) { propertyAsList.AddElement(EditorReflector.MakeInstance((Type)goalType)); }
internal void Process() { //Debug.Log("Process!"); _selectedIndex = -1; var adapter = AddEventHandlerDialog.Instance.Adapter; // allow th ebubbling button only for container _allowBubbling = adapter is GroupAdapter; //_allowBubbling = adapter.ComponentType.IsSubclassOf(typeof(Container)); // this way we enable all the containers (even the programmable ones) to be listened to //Debug.Log("_allowBubbling: " + _allowBubbling); if (!_allowBubbling) { _targetMode = true; _bubblingMode = false; } _actualEventDict.Clear(); if (_targetMode) { var dict = EditorReflector.GetEvents(adapter); foreach (KeyValuePair <string, EventAttribute> pair in dict) { if (!_actualEventDict.ContainsKey(pair.Key)) { _actualEventDict.Add(pair.Key, pair.Value); } } } if (_allowBubbling && _bubblingMode) { var dict = EditorReflector.GetEventsBubblingFromChildren(adapter); foreach (KeyValuePair <string, EventAttribute> pair in dict) { if (!_actualEventDict.ContainsKey(pair.Key)) { _actualEventDict.Add(pair.Key, pair.Value); } } } _defaultEvent = EditorReflector.GetDefaultEventName(adapter); _actualEventList = new List <string>(); foreach (string key in _actualEventDict.Keys) { _actualEventList.Add(key); } //Debug.Log("events: " + events.Count); List <string> events2 = new List <string>(); foreach (string s in _actualEventList) { if (!string.IsNullOrEmpty(_searchText) && !s.ToUpper().Contains(_searchText.ToUpper())) { continue; } events2.Add(string.Format("{0}", s)); } _actualEventList = events2; _actualEventList.Sort(); _strEvents = _actualEventList.ToArray(); List <int> ints = new List <int>(); for (int i = 0; i < _strEvents.Length; i++) { ints.Add(i); } _intEvents = ints.ToArray(); List <GUIContent> contentList = new List <GUIContent>(); foreach (string s in _strEvents) { contentList.Add(new GUIContent(" " + s, TextureCache.Instance.Event)); } _contents = contentList.ToArray(); // select a default event if (!string.IsNullOrEmpty(_defaultEvent)) { _selectedIndex = _actualEventList.IndexOf(_defaultEvent); if (-1 == _selectedIndex) { return; } _inputText = _defaultEvent; //Debug.Log("Setting out defaultEvent: " + _defaultEvent); //Debug.Log("_actualEventDict[_defaultEvent]: " + _actualEventDict[_defaultEvent]); //AddEventHandlerDialog.Instance.Data.EventAttribute = _actualEventDict[_defaultEvent]; //AddEventHandlerDialog.Instance.Data.EventName = _defaultEvent; EventAttribute attr = _actualEventDict[_actualEventList[_selectedIndex]]; if (null != attr) { AddEventHandlerDialog.Instance.Data.EventAttribute = _actualEventDict[attr.Name]; //AddEventHandlerDialog.Instance.Data.EventName = null; } //Debug.Log("EventAttribute: " + AddEventHandlerDialog.Instance.Data.EventAttribute); } }