/// <summary>
    /// This attempts to restore the last set data group on the target.
    /// </summary>
    /// <param name="target">The target to restore.</param>
    /// <returns>Whether or not the target was restored.</returns>
    public static bool RestoreVisDataGroupTarget(IVisDataGroupTarget target)
    {
        //make sure the data group is set, and if not, check if there
        //is a name set and try and find that object as the data group
        if (target.DataGroup == null && target.LastDataGroupName != null && target.LastDataGroupName.Length > 0)
        {
            //try to get the manager for this target
            VisManager manager = null;
            if (target is IVisManagerTarget)
            {
                manager = (target as IVisManagerTarget).Manager;
            }

            //make sure a manager was found
            if (manager != null)
            {
                //loop through all data groups and make sure it was found
                for (int i = 0; i < manager.DataGroups.Count; i++)
                {
                    if (manager.DataGroups[i].dataGroupName == target.LastDataGroupName)
                    {
                        target.DataGroup = manager.DataGroups[i];
                        return(true);
                    }
                }
            }
        }

        return(false);
    }
Esempio n. 2
0
    /// <summary>
    /// This displays the drop down for selecting a data group from the inspector.
    /// </summary>
    /// <param name="dataGroupTarget">The data group target to set the data group for.</param>
    /// <returns>Whether or not a data group is currently set.</returns>
	public bool DisplayIVisDataGroupTargetGUI(IVisDataGroupTarget dataGroupTarget)
	{
		EnsureAllDataGroupsRegistered();
		
		if (dataGroupTarget != null && dataGroupTarget is IVisManagerTarget)
        {
            //make sure and try to restore it first
            VisDataGroup.RestoreVisDataGroupTarget(dataGroupTarget);

			VisManager manager = (dataGroupTarget as IVisManagerTarget).Manager;
			if (manager != null)
			{	
				ReadOnlyCollection<VisDataGroup> dataGroups = manager.DataGroups;
				if (dataGroups.Count > 0)
				{					
					//create list of vis data group names and a dictionary to map IDs, and sort it
					List<string> sortedNames = new List<string>(dataGroups.Count);
					Dictionary<string, int> nameToIndexMap = new Dictionary<string, int>(dataGroups.Count);
					for (int i = 0; i < dataGroups.Count; i++)
					{
						sortedNames.Add((dataGroups[i] as VisDataGroup).dataGroupName);
						nameToIndexMap.Add((dataGroups[i] as VisDataGroup).dataGroupName, i);
					}
					sortedNames.Sort();
					
					//create array of names and set current index
					int currentIndex = 0;
					string[] displayedNames = new string[dataGroups.Count + 1];
					displayedNames[0] = "None";
					for (int i = 0; i < sortedNames.Count; i++)
					{
						displayedNames[i + 1] = sortedNames[i];
						if (dataGroupTarget.DataGroup == dataGroups[nameToIndexMap[sortedNames[i]]])
							currentIndex = i + 1;
					}
					
					//display popup
					int newIndex = EditorGUILayout.Popup("   Data Group", currentIndex, displayedNames);
					
					//set new vis data group if the index has changed
					if (newIndex != currentIndex)
					{
						if (newIndex == 0)
							dataGroupTarget.DataGroup = null;
						else
						{
							string newName = sortedNames[newIndex - 1];
							int remappedIndex = nameToIndexMap[newName];
							dataGroupTarget.DataGroup = dataGroups[remappedIndex] as VisDataGroup;
						}
						EditorUtility.SetDirty(target);
					}
					return dataGroupTarget.DataGroup != null;
				}
				else
				{
                    if (dataGroupTarget.LastDataGroupName != null && dataGroupTarget.LastDataGroupName.Length > 0)
                        EditorGUILayout.LabelField("   Data Group", dataGroupTarget.LastDataGroupName + " (not found, try selecting the Object with this Data Group)");
                    else
    					EditorGUILayout.LabelField("   Data Group", "NO DATA GROUPS FOUND!");
					return false;
				}
			}
		}
		return false;
	}
Esempio n. 3
0
    /// <summary>
    /// This displays the drop down for selecting a data group from the inspector.
    /// </summary>
    /// <param name="dataGroupTarget">The data group target to set the data group for.</param>
    /// <returns>Whether or not a data group is currently set.</returns>
    public bool DisplayIVisDataGroupTargetGUI(IVisDataGroupTarget dataGroupTarget)
    {
        EnsureAllDataGroupsRegistered();

        if (dataGroupTarget != null && dataGroupTarget is IVisManagerTarget)
        {
            //make sure and try to restore it first
            VisDataGroup.RestoreVisDataGroupTarget(dataGroupTarget);

            VisManager manager = (dataGroupTarget as IVisManagerTarget).Manager;
            if (manager != null)
            {
                ReadOnlyCollection <VisDataGroup> dataGroups = manager.DataGroups;
                if (dataGroups.Count > 0)
                {
                    //create list of vis data group names and a dictionary to map IDs, and sort it
                    List <string>            sortedNames    = new List <string>(dataGroups.Count);
                    Dictionary <string, int> nameToIndexMap = new Dictionary <string, int>(dataGroups.Count);
                    for (int i = 0; i < dataGroups.Count; i++)
                    {
                        sortedNames.Add((dataGroups[i] as VisDataGroup).dataGroupName);
                        nameToIndexMap.Add((dataGroups[i] as VisDataGroup).dataGroupName, i);
                    }
                    sortedNames.Sort();

                    //create array of names and set current index
                    int      currentIndex   = 0;
                    string[] displayedNames = new string[dataGroups.Count + 1];
                    displayedNames[0] = "None";
                    for (int i = 0; i < sortedNames.Count; i++)
                    {
                        displayedNames[i + 1] = sortedNames[i];
                        if (dataGroupTarget.DataGroup == dataGroups[nameToIndexMap[sortedNames[i]]])
                        {
                            currentIndex = i + 1;
                        }
                    }

                    //display popup
                    int newIndex = EditorGUILayout.Popup("   Data Group", currentIndex, displayedNames);

                    //set new vis data group if the index has changed
                    if (newIndex != currentIndex)
                    {
                        if (newIndex == 0)
                        {
                            dataGroupTarget.DataGroup = null;
                        }
                        else
                        {
                            string newName       = sortedNames[newIndex - 1];
                            int    remappedIndex = nameToIndexMap[newName];
                            dataGroupTarget.DataGroup = dataGroups[remappedIndex] as VisDataGroup;
                        }
                        EditorUtility.SetDirty(target);
                    }
                    return(dataGroupTarget.DataGroup != null);
                }
                else
                {
                    if (dataGroupTarget.LastDataGroupName != null && dataGroupTarget.LastDataGroupName.Length > 0)
                    {
                        EditorGUILayout.LabelField("   Data Group", dataGroupTarget.LastDataGroupName + " (not found, try selecting the Object with this Data Group)");
                    }
                    else
                    {
                        EditorGUILayout.LabelField("   Data Group", "NO DATA GROUPS FOUND!");
                    }
                    return(false);
                }
            }
        }
        return(false);
    }