private void ConvertCoords(ref int x, ref int y, bool addParent) { Adapter rootVisual = DynamicAdapterFactory.Instance.RootVisualAdapter; if (rootVisual == null || rootVisual.Parent == null) { // TODO: Logging return; } // Since our parent is unmanaged, we can't just cast. Atk.Component parent = ComponentAdapter.GetObject(rootVisual.Parent); if (parent == null) { // TODO: Logging return; } int parentX, parentY; parent.GetPosition(out parentX, out parentY, CoordType.Screen); x += parentX * (addParent ? 1 : -1); y += parentY * (addParent ? 1 : -1); }
/// <summary> /// Processes a single event listener phases /// </summary> /// <param name="adapter">Component descriptor referencing event handlers</param> /// <param name="component">Component dispatching events</param> /// <param name="mapping">Event mapping</param> /// <param name="enabled">Enabled state</param> public static void ProcessPhases(ComponentAdapter adapter, Components.Component component, EventMapping mapping, bool enabled) { if (null == component && null == adapter.Component) // not instantiated (edit mode) and no component supplied { return; } Component script = adapter.GetComponent(mapping.ScriptName); if (null == script) { Debug.LogWarning("Component " + mapping.ScriptName + " not found on " + adapter.gameObject); return; } Type type = script.GetType(); MethodInfo methodInfo = type.GetMethod(mapping.MethodName, AllInstanceMethodsBindingFlags, null, TypeOfEvent, Modifiers); //type.GetMethod(mapping.MethodName, TypeOfEvent); EventHandler handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), script, methodInfo); var cmp = component ?? adapter.Component; /** * 1. Remove all phases * */ cmp.RemoveEventListener(mapping.EventType, handler, EventPhase.Capture | EventPhase.Target | EventPhase.Bubbling); /** * 2. If enabled, subscribe again * */ if (enabled) { cmp.AddEventListener(mapping.EventType, handler, mapping.Phase); } }
// ReSharper restore UnassignedField.Global #endif internal static void Process(List <Node> removals) { if (0 == removals.Count) { return; } #if DEBUG if (DebugMode) { Debug.Log(string.Format("Processing {0} removals.", removals.Count)); } #endif //Debug.Log(string.Format("Processing {0} removals.", removals.Count)); // TODO: do removals bottom up! nesting level should be used here foreach (Node node in removals) { //if (null == node.Transform) // continue; // ROOT node.RemoveFromHierarchy(); //Debug.Log("node.ParentTransform: " + node.ParentTransform); if (null == node.ParentTransform) //|| /*!*/(adapter is StageAdapter)) { continue; // not a stage, return (if stage, should process) } // consolidate parent transform ComponentAdapter adapter = node.Adapter; GroupAdapter parentGroupAdapter = GuiLookup.GetAdapter(node.ParentTransform) as GroupAdapter; if (null != parentGroupAdapter) { /** * Stop monitoring * */ PersistenceManager.Instance.Unwatch(node.AdapterId); //PersistenceManager.Instance.RemoveAdapter(node.AdapterId); /** * Note: if object removed from the hierarchy, the adapter is destroyed * In that case following command doesn nothing (doesn not remove the slot from the parent) * Thus we have the consolidation below (removing null) * */ parentGroupAdapter.RemoveChild(adapter); // TODO: consolidate only for top level removals (add parameter) /*parentContainerAdapter.RemoveChild(adapter);*/ //parentContainerAdapter.RemoveAllChildren(); ChildGroupPack pack = ChildGroupPack.Read(parentGroupAdapter); pack.Consolidate(); // there is a null slot here. We have to re-render children //Debug.Log("*pack: " + pack); parentGroupAdapter.InstantiateChildren(true); } } }
public AdapterAnalysis(object target) { if (null != target) { ComponentAdapter = target as ComponentAdapter; SkinnableComponentAdapter = target as SkinnableComponentAdapter; SkinnableContainerAdapter = target as SkinnableContainerAdapter; GroupAdapter = target as GroupAdapter; if (null != GroupAdapter) { HasAbsoluteLayout = CheckForAbsoluteLayout(GroupAdapter); } if (null != ComponentAdapter && null != ComponentAdapter.transform && null != ComponentAdapter.transform.parent) { ParentGroupAdapter = ComponentAdapter.transform.parent.GetComponent <GroupAdapter>(); HasParent = null != ParentGroupAdapter; if (null != ParentGroupAdapter) { ParentIsStage = ParentGroupAdapter is StageAdapter; //ParentUsesLayoutDescriptor = ParentContainerAdapter.UseLayoutDescriptor; //ParentLayoutDescriptor = ParentContainerAdapter.LayoutDescriptor; ParentLayout = ParentGroupAdapter.Layout; ParentHasAbsoluteLayout = CheckForAbsoluteLayout(ParentGroupAdapter); } } } }
private static ComponentAdapter FindAdapterRecursive(GameObject go, string id) { ComponentAdapter adapter = null; ComponentAdapter a = go.GetComponent <ComponentAdapter>(); if (null != a && a.Id == id) { return(a); } Transform transform = go.transform; int count = transform.childCount; for (int i = 0; i < count; i++) { Transform childTransform = transform.GetChild(i); // Note: recursion a = FindAdapterRecursive(childTransform.gameObject, id); if (null != a && a.Id == id) { adapter = a; break; } } return(adapter); }
/// <summary> /// Gets a component from the adapter specified by ID /// </summary> /// <returns></returns> public static Component GetComponent(GameObject parentGameObject, string id) { ComponentAdapter adapter = GetAdapter(parentGameObject); if (null == adapter) { Debug.LogWarning("Cannot find component adapter on game object: " + parentGameObject); return(null); } if (null == adapter.Component) { Debug.LogWarning("Component not instantiated. Adapter: " + adapter); return(null); } Component parentComponent = adapter.Component; // TODO: create method ComponentManager.Instance.GetMultiple(id); and use it here! InteractiveComponent component = ComponentManager.Instance.Get(id); if (parentComponent.HasChild(component)) { return((Component)component); } return(null); }
public Node(Transform transform) { _transform = transform; if (null != _transform) { _parentTransform = _transform.parent; } if (null == transform) { _name = "ROOT"; } else { _name = transform.name; _transformId = transform.GetInstanceID(); _adapter = transform.GetComponent <ComponentAdapter>(); if (null != _adapter) { _adapterId = _adapter.GetInstanceID(); } } _description = string.Format("[{0}]", GuiLookup.PathToString(transform, "->")); }
public static Component AddHandlerScript(ComponentAdapter adapter, AddEventHandlerPersistedData data) { //Debug.Log(string.Format(@"AddHandlerScript [adapter: {0}, data: {1}]", adapter, data)); var component = adapter.gameObject.GetComponent(data.ClassName); Component addedComponent = null; /** * 1. Check if the component is already attached * */ if (null != component) { string text = string.Format(@"Script ""{0}"" is already attached to the selected game object.", data.ClassName); Debug.LogWarning(text); EditorUtility.DisplayDialog("Duplicated script", text, "OK"); } else { /** * 2. Add component * */ addedComponent = adapter.gameObject.AddComponent(data.ClassName); } return(addedComponent); }
public static Dictionary <string, EventAttribute> GetBubblingEvents(ComponentAdapter adapter) { Dictionary <string, EventAttribute> dict = new Dictionary <string, EventAttribute>(); GetEventsRecursive(adapter, adapter, ref dict, true, null); return(dict); }
/// <summary> /// Returns the component adapter atached to the specified game object /// </summary> /// <param name="gameObject">GameObject to look for adapter</param> /// <returns></returns> public static ComponentAdapter GetAdapter(GameObject gameObject) { ComponentAdapter componentAdapter = gameObject.GetComponent <ComponentAdapter>(); return(componentAdapter); }
/// <summary> /// Finds a gui element in children. If such an element exists, returns it; if not, returns null /// </summary> /// <typeparam name="T">Type of the adapter to find</typeparam> /// <param name="go">Game object on which to do the search</param> /// <param name="id">Adapter ID</param> /// <returns></returns> public static T FindComponent <T>(GameObject go, string id) where T : Component { ComponentAdapter adapter = go.GetComponent <ComponentAdapter>(); Component component = adapter.Component; if (adapter.Id == id && adapter.ComponentType == typeof(T)) { return(component as T); } GroupAdapter groupAdapter = adapter as GroupAdapter; if (null != groupAdapter) { int count = groupAdapter.transform.childCount; for (int i = 0; i < count; i++) { Transform childTransform = groupAdapter.transform.GetChild(i); adapter = childTransform.GetComponent <ComponentAdapter>(); if (adapter.ComponentType == typeof(T) && null != adapter.Component && adapter.Component.Id == id) { break; } } } return(adapter.Component as T); }
/// <summary> /// Since all of the tree displays use the selection change, I decided to implement it in superclass /// </summary> internal void ProcessSelectionChange() { //Debug.Log("ProcessSelectionChange: " + Selection.activeObject); if (null == Selection.activeGameObject) { return; } GameObject go = Selection.activeGameObject; /*if (null == go) * throw new Exception("Couldn't get the selection");*/ Adapter = go.GetComponent(typeof(ComponentAdapter)) as ComponentAdapter; if (null == Adapter) { /** * Not a GUI component * S hould do cleanup and handle selection (deselect basically) * */ GroupAdapter = null; //HandleSelectionChange(); return; } Target = Adapter; GroupAdapter = Target as GroupAdapter; HandleSelectionChange(); }
public static void Process(List <Node> nodes) { if (nodes.Count == 0) { return; } //Debug.Log(string.Format("Processing {0} top level additions in edit mode.", nodes.Count)); foreach (Node node in nodes) { /** * 1. Process transforms * */ Transform transform = node.Transform; if (null == transform) { continue; // ROOT } ComponentAdapter adapter = GuiLookup.GetAdapter(transform); bool isStage = adapter is StageAdapter; Transform parentTransform = node.ParentTransform; /* This happens when a stage added to root */ if (null == parentTransform && !isStage) { continue; // not a stage, return (if stage, should process) } GroupAdapter parentAdapter = null; if (null != parentTransform) { parentAdapter = GuiLookup.GetAdapter(node.ParentTransform) as GroupAdapter; } /** * 2. Process adapters * */ if (null != adapter) { // this is eDriven.Gui component. process it properly if (null == parentAdapter) { if (!isStage) { const string txt = "eDriven.Gui components could be added to containers only"; throw new Exception(txt); } } else { parentAdapter.AddChild(adapter, true); //if (!PrefabUtil.IsCreatedFromPrefab(parentAdapter)) ParentChildLinker.Instance.Update(parentAdapter); } } } }
/// <summary> /// Finds a gui element in children. If such an element exists, returns it; if not, returns null /// </summary> /// <param name="go">Game object on which to do the search</param> /// <param name="id">Adapter ID</param> /// <returns></returns> public static Component FindComponent(GameObject go, string id) { ComponentAdapter adapter = go.GetComponent <ComponentAdapter>(); Component component = adapter.Component; if (adapter.Id == id) { return(component); } GroupAdapter groupAdapter = adapter as GroupAdapter; if (null != groupAdapter) { int count = groupAdapter.transform.childCount; for (int i = 0; i < count; i++) { Transform childTransform = groupAdapter.transform.GetChild(i); adapter = childTransform.GetComponent <ComponentAdapter>(); if (null != adapter.Component && adapter.Component.Id == id) { break; } } } return(adapter.Component); }
protected bool CheckSelection(bool mustBeContainer, bool renderMessage = true) { if (null == Selection.activeTransform) { if (renderMessage) { GUILayout.Label(GuiContentCache.Instance.NoSelectionContent, StyleCache.Instance.CenteredLabel, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); } return(false); } Adapter = GuiLookup.GetAdapter(Selection.activeTransform); GroupAdapter = Adapter as GroupAdapter; if (null == Adapter) { if (renderMessage) { GUILayout.Label(GuiContentCache.Instance.NotEDrivenComponentContent, StyleCache.Instance.CenteredLabel, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); } return(false); } if (mustBeContainer && null == GroupAdapter) { if (renderMessage) { GUILayout.Label(GuiContentCache.Instance.NotAContainerContent, StyleCache.Instance.CenteredLabel, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); } return(false); } return(true); }
private static void SetupDefaults(bool alt, ComponentAdapter adapter) { if (alt || EditorSettings.ExpandWidthUponCreation) { adapter.UseWidth = true; adapter.UsePercentWidth = true; adapter.Width = 100; } if (alt || EditorSettings.ExpandHeightUponCreation) { adapter.UseHeight = true; adapter.UsePercentHeight = true; adapter.Height = 100; } if (EditorSettings.FactoryModeUponCreation) { adapter.FactoryMode = true; } var skinnableComponentAdapter = adapter as SkinnableComponentAdapter; if (skinnableComponentAdapter != null) { skinnableComponentAdapter.ApplySkin(EditorSettings.ApplyLastUsedSkinUponCreation); } }
/// <summary> /// /// </summary> /// <param name="nodes"></param> /// <param name="adapter"></param> /// <returns></returns> // ReSharper disable SuggestBaseTypeForParameter private static Node GetNode(List <Node> nodes, ComponentAdapter adapter) // ReSharper restore SuggestBaseTypeForParameter { return(nodes.Find(delegate(Node node) { return node.Adapter == adapter; })); }
public void Remove(ComponentAdapter adapter) { if (!_adapters.Contains(adapter)) { throw new Exception("Child group doesn't contains adapter"); } _adapters.Remove(adapter); }
public void Insert(ComponentAdapter adapter, int index) { if (_adapters.Contains(adapter)) { throw new Exception("Child group already contains adapter"); } _adapters.Insert(index, adapter); }
//public void Add(ComponentAdapter adapter) //{ // if (_adapters.Contains(adapter)) // throw new Exception("Child group already contains adapter"); // _adapters.Add(adapter); //} public void Add(ComponentAdapter adapter) { if (_adapters.Contains(adapter)) { throw new Exception("Child group already contains adapter"); } _adapters.Add(adapter); }
public static bool IsCreatedFromPrefab(ComponentAdapter adapter) { // if this is a prefab, or a child of a prefab, do not reorder its children var prefabRoot = PrefabUtility.FindPrefabRoot(adapter.gameObject); var prefabParent = PrefabUtility.GetPrefabParent(adapter.gameObject); var prefabObject = PrefabUtility.GetPrefabObject(adapter.gameObject); return(null != prefabParent || null != prefabObject || null != prefabRoot); }
public static string GetDefaultEventName(ComponentAdapter adapter) { var list = Core.Reflection.CoreReflector.GetClassAttributes <DefaultEvent>(adapter.ComponentType); if (list.Count == 0) { return(null); } return(list[0].Name); }
///<summary> ///</summary> ///<param name="containerAdapter"></param> ///<returns></returns> public List <ComponentAdapter> GetChildAdaptersCollection(ComponentAdapter containerAdapter) { if (null == CollectionMemberInfo || null == containerAdapter) { return(null); } List <ComponentAdapter> collection = Core.Reflection.CoreReflector.GetMemberValue(CollectionMemberInfo, containerAdapter) as List <ComponentAdapter>; return(collection); }
/// <summary> /// Gets a component from the adapter specified by ID /// </summary> /// <param name="id">Adapter ID</param> /// <returns></returns> public static Component GetComponent(string id) { ComponentAdapter adapter = FindAdapter(id); if (null == adapter) { return(null); } return(adapter.Component); }
/*public static List<EventAttribute> GetEvents(Type componentType) * { * object[] list = componentType.GetCustomAttributes(typeof(EventAttribute), true); * var events = new List<EventAttribute>(); * foreach (var e in list) * { * events.Add((EventAttribute) e); * } * return events; * }*/ private static void GetEventsRecursive(ComponentAdapter clickedAdapter, ComponentAdapter currentAdapter, ref Dictionary <string, EventAttribute> dict, bool bubbling, ICollection <ComponentAdapter> adaptersToExclude) { Type componentType = currentAdapter.ComponentType; if (null == adaptersToExclude || !adaptersToExclude.Contains(currentAdapter)) { //object[] list = componentType.GetCustomAttributes(typeof (EventAttribute), true); var eventAttributes = CoreReflector.GetClassAttributes <EventAttribute>(componentType); foreach (EventAttribute attribute in eventAttributes) { if (clickedAdapter == currentAdapter) { /** * 1. If this is a clicked adapter, get all events * */ dict[attribute.Name] = attribute; } else if (bubbling && attribute.Bubbles) // if (bubbling) { /** * 2. Else get only events that may bubble from children * */ dict[attribute.Name] = attribute; } /*if (!bubbling || attribute.Bubbles) // if (bubbling) * { * // bubbling events only * if (attribute.Bubbles) * dict[attribute.Name] = attribute; * } * else * { * // target events only * dict[attribute.Name] = attribute; * }*/ //Debug.Log(" --> " + attribute.Name); } } if (bubbling) { Transform transform = currentAdapter.transform; var childCount = transform.childCount; for (int i = 0; i < childCount; i++) { var childTransform = transform.GetChild(i); ComponentAdapter childAdapter = GuiLookup.GetAdapter(childTransform); GetEventsRecursive(clickedAdapter, childAdapter, ref dict, true, adaptersToExclude); } } }
//public void Add(List<ComponentAdapter> adapters) //{ // _groups.Add(new ChildGroup(adapters)); // Beware: the bug was here - internally the changes were immediatelly applied to collections // //_groups.Add(new ChildGroup(ListUtil<ComponentAdapter>.Clone(adapters))); //TODO?? //} public int GetGroupHostingAdapterIndex(ComponentAdapter adapter) { for (int i = 0; i < Groups.Count; i++) { bool contains = Groups[i].Adapters.Contains(adapter); if (contains) { return(i); } } return(-1); }
// public void Flush() // { // //Debug.Log("_groupDescriptors.Count: " + _groupDescriptors.Count); // //Debug.Log("groupIndex: " + groupIndex); // foreach (ChildGroupDescriptor groupDescriptor in _groupDescriptors) // { // if (null == _containerAdapter.Component) // not instantiated // continue; // //Debug.Log("Getting a target container for " + _containerAdapter); // var targetContainer = groupDescriptor.GetTargetContainer((Container)_containerAdapter.Component); // if (null == targetContainer) // throw new Exception("targetContainer is null"); // targetContainer.RemoveAllContentChildren(); // var adapters = groupDescriptor.GetChildAdaptersCollection(_containerAdapter); //#if DEBUG // if (DebugMode) // { // Debug.Log(string.Format(@"groupDescriptor: {0} //{1}", groupDescriptor.Attribute.Label, ComponentAdapterUtil.DescribeAdapterList(adapters))); // } //#endif // foreach (ComponentAdapter adapter in adapters) // { // if (null == adapter) // continue; // throw new Exception("adapter is null"); // var comp = adapter.Component; // if (null == comp) // //throw new Exception("adapter.Component is null"); // continue; // targetContainer.AddContentChild(comp); // } // } // } ///<summary> ///</summary> ///<param name="childAdapter"></param> ///<returns></returns> public List <ComponentAdapter> GetGroupContainingAdapter(ComponentAdapter childAdapter) { foreach (ChildGroupDescriptor groupDescriptor in _groupDescriptors) { List <ComponentAdapter> collection = groupDescriptor.GetChildAdaptersCollection(_groupAdapter); if (collection.Contains(childAdapter)) { return(collection); } } return(null); }
/// <summary> /// Processes multiple event listeners /// </summary> /// <param name="adapter">Component descriptor referencing event handlers</param> /// <param name="component">Component dispatching events</param> /// <param name="enabled">Enabled state</param> public static void ProcessListeners(ComponentAdapter adapter, Components.Component component, bool enabled) { if (null == adapter) { return; } foreach (EventMapping mapping in adapter.EventMap.Items) { ProcessListener(adapter, component, mapping, enabled && mapping.Enabled); // use (enabled && mapping.Enabled) here! } }
/// <summary> /// Monitors the object for property changes<br/> /// Monitoring an object means putting it on the list which will be checked for changes after the play mode is stopped<br/> /// When put on the list, all the original properties of the object are being saved (cloned)<br/> /// When play mode stopped, properties are being read from all the monitored objects<br/> /// For each property, the original and current value are being compared for change<br/> /// and the changed value list is being made<br/> /// Changed values are being applied to an object "resurrected" by Unity, after the play mode is stopped<br/> /// Here we are monitoring eDriven.Gui COMPONENTS, not transforms or game objects<br/> /// (however, they could also be monitored in some other scenario)<br/> /// </summary> /// <param name="target">Target (component) to monitor</param> public void Watch(Object target) { /** * 1. Get the instance ID because it is the key in the dictionary holding the monitored objects * */ int instanceId = target.GetInstanceID(); //Debug.Log("* Monitoring: " + instanceId); /** * 2. We need to check if the object is already being monitored * This is important because we must not overwrite the original values each time the component is being clicked * because this might lead to the loss of data (only changes from the last component click would be then be saved as the original values) * For instance, take a look at this scenario: * - this component click. Changing the text to "foo". * - other component click. * - this component click. Changing the color to green. * In play mode, all the changes would be accumulated, and there would seem to be no problems. * But after the play mode is stopped, and started again, we would discover that only the component color is being changed to green, * and no text has been changed - due to second component click rewriting the "original" values, thus deleting the change to "foo" * */ if (_monitoredObjects.ContainsKey(instanceId)) { return; } #if DEBUG if (DebugMode) { ComponentAdapter componentAdapter = target as ComponentAdapter; if (null != componentAdapter) { Debug.Log("Monitoring: " + GuiLookup.PathToString(componentAdapter.transform, " -> "), componentAdapter.transform); } } #endif /** * 3. This is the first time we are monitoring this object * Create a new PersistedComponent instance and add it to dictionary * */ _monitoredObjects[instanceId] = new PersistedComponent(target); //_currentInstanceId = target.GetInstanceID(); #if DEBUG if (DebugMode) { Debug.Log(string.Format(" Added [{0}] to monitored objects list. Total: {1}", target.name, _monitoredObjects.Count), target); } #endif //MonitoredObjectAddedSignal.Emit(target); }
/// <summary> /// Note: this method actually changes the collections /// </summary> /// <param name="adapter"></param> /// <returns></returns> public ChildGroupPack Remove(ComponentAdapter adapter) { _pack = ChildGroupPack.Read(_groupAdapter); //Debug.Log(string.Format("Reorder: grp:{0}, item:{1}", groupIndex, itemIndex)); // get old group index var oldGroupIndex = _pack.GetGroupHostingAdapterIndex(adapter); // remove adapter from old group _pack.Groups[oldGroupIndex].Remove(adapter); return(_pack); }