public static void FixupStateRefs(TimelineStateMachine timeLineStateMachine, object obj)
            {
                if (obj != null)
                {
                    object[] nodeFieldObjects = SerializedFieldInfo.GetSerializedFieldInstances(obj);

                    foreach (object nodeFieldObject in nodeFieldObjects)
                    {
                        TimelineStateRef stateRefProperty = nodeFieldObject as TimelineStateRef;

                        if (stateRefProperty != null)
                        {
                            stateRefProperty.FixUpRef(timeLineStateMachine);
                        }
                        else
                        {
#if UNITY_EDITOR
                            LocalisedStringRef localisedstring = nodeFieldObject as LocalisedStringRef;

                            if (localisedstring != null)
                            {
                                localisedstring.SetEditorStateMachine(timeLineStateMachine);
                            }
#endif
                        }

                        FixupStateRefs(timeLineStateMachine, nodeFieldObject);
                    }
                }
            }
Example #2
0
            private static object FixupStateRefs(object obj, object stateMachine)
            {
                if (obj.GetType() == typeof(StateRef))
                {
                    StateRef StateRef = (StateRef)obj;
                    StateRef.SetParentStatemachine((StateMachine)stateMachine);
                    return(StateRef);
                }
#if UNITY_EDITOR
                else if (obj.GetType() == typeof(LocalisedStringRef))
                {
                    LocalisedStringRef localisedStringRef = (LocalisedStringRef)obj;
                    localisedStringRef.SetAutoNameParentName(((StateMachine)stateMachine)._name);
                    return(localisedStringRef);
                }
#endif
                return(obj);
            }
Example #3
0
				public static object PropertyField(object obj, GUIContent label, ref bool dataChanged, GUIStyle style, params GUILayoutOption[] options)
				{
					LocalisedStringRef localisedString = (LocalisedStringRef)obj;
					
					bool editorCollapsed = !EditorGUILayout.Foldout(!localisedString._editorCollapsed, label);

					if (editorCollapsed != localisedString._editorCollapsed)
					{
						localisedString._editorCollapsed = editorCollapsed;
						dataChanged = true;
					}

					if (!editorCollapsed)
					{
						int origIndent = EditorGUI.indentLevel;
						EditorGUI.indentLevel++;
						
						//Draw list of possible keys
						int currentKeyIndex = 0;
						{
							string[] keys = Localisation.GetStringKeys();
							string currentKey = localisedString.GetLocalisationKey();

							for (int i = 0; i < keys.Length; i++)
							{
								if (keys[i] == currentKey)
								{
									currentKeyIndex = i;
									break;
								}
							}

							EditorGUI.BeginChangeCheck();
							currentKeyIndex = EditorGUILayout.Popup("Localisation Key", currentKeyIndex, keys);

							//If key has changed
							if (EditorGUI.EndChangeCheck())
							{
								if (currentKeyIndex == 0)
								{
									localisedString = new LocalisedStringRef();
								}
								else
								{
									localisedString = new LocalisedStringRef(keys[currentKeyIndex]);
								}

								dataChanged = true;
							}
						}

						//Draw buttons for adding new key
						if (currentKeyIndex == 0)
						{
							string[] folders = Localisation.GetStringFolders();
							int currentFolderIndex = 0;
							string keyWithoutFolder;
							Localisation.GetFolderIndex(localisedString.GetLocalisationKey(), out currentFolderIndex, out keyWithoutFolder);

							EditorGUILayout.BeginHorizontal();
							{
								EditorGUILayout.LabelField(new GUIContent("New Key"), GUILayout.Width(EditorUtils.GetLabelWidth()));

								string editorParentName = localisedString.GetAutoNameParentName();

								EditorGUI.BeginChangeCheck();
								int newFolderIndex = EditorGUILayout.Popup(currentFolderIndex, folders);
								string currentFolder = newFolderIndex == 0 ? "" : folders[newFolderIndex];
								if (EditorGUI.EndChangeCheck())
								{
									if (newFolderIndex != 0)
									{
										localisedString = new LocalisedStringRef(currentFolder + "/" + keyWithoutFolder);
										localisedString.SetAutoNameParentName(editorParentName);
										dataChanged = true;
									}
								}

								EditorGUILayout.LabelField(new GUIContent("/"), GUILayout.Width(44));

								EditorGUI.BeginChangeCheck();
								keyWithoutFolder = EditorGUILayout.TextField(keyWithoutFolder);
								if (EditorGUI.EndChangeCheck())
								{
									localisedString = new LocalisedStringRef(currentFolder + "/" + keyWithoutFolder);
									localisedString.SetAutoNameParentName(editorParentName);
									dataChanged = true;
								}							

								if (GUILayout.Button("Auto", GUILayout.Width(36)))
								{
									string newKey = localisedString.GetAutoKey();
									localisedString = new LocalisedStringRef(newKey);
									localisedString.SetAutoNameParentName(editorParentName);
									dataChanged = true;
								}

								if (GUILayout.Button("Add", GUILayout.Width(32)) && !string.IsNullOrEmpty(localisedString.GetLocalisationKey()))
								{
									if (!Localisation.IsKeyInTable(localisedString.GetLocalisationKey()))
									{
										Localisation.UpdateString(localisedString.GetLocalisationKey(), Localisation.GetCurrentLanguage(), string.Empty);
										LocalisationEditorWindow.EditString(localisedString.GetLocalisationKey());
									}
									dataChanged = true;
								}
							}
							EditorGUILayout.EndHorizontal();
						}

						//Draw actual localised text (can be edited to update localisation file)
						{
							string currentKey = localisedString.GetLocalisationKey();

							//Only display if have a valid key
							if (!string.IsNullOrEmpty(currentKey) && Localisation.IsKeyInTable(currentKey))
							{
								EditorGUI.BeginChangeCheck();
								string text;
								if (style != null)
									text = EditorGUILayout.TextArea(Localisation.GetRawString(currentKey), style);
								else
									text = EditorGUILayout.TextArea(Localisation.GetRawString(currentKey));
								if (EditorGUI.EndChangeCheck())
								{
									Localisation.UpdateString(currentKey, Localisation.GetCurrentLanguage(), text);
								}
							}
						}

						EditorGUI.indentLevel = origIndent;
					}

					return localisedString;
				}
                public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
                {
                    EditorGUI.BeginProperty(position, label, property);

                    SerializedProperty localisationkeyProperty = property.FindPropertyRelative("_localisationKey");

                    float yPos = position.y;

                    Rect foldoutPosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);

                    property.isExpanded = EditorGUI.Foldout(foldoutPosition, property.isExpanded, property.displayName + " (Localised String)");
                    yPos += EditorGUIUtility.singleLineHeight;

                    if (property.isExpanded)
                    {
                        int origIndent = EditorGUI.indentLevel;
                        EditorGUI.indentLevel++;

                        //Draw list of possible keys
                        int currentKey = 0;
                        {
                            string[] keys = Localisation.GetStringKeys();

                            for (int i = 0; i < keys.Length; i++)
                            {
                                if (keys[i] == localisationkeyProperty.stringValue)
                                {
                                    currentKey = i;
                                    break;
                                }
                            }

                            Rect typePosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);
                            yPos += EditorGUIUtility.singleLineHeight;

                            EditorGUI.BeginChangeCheck();
                            currentKey = EditorGUI.Popup(typePosition, "Localisation Key", currentKey, keys);

                            if (EditorGUI.EndChangeCheck())
                            {
                                if (currentKey == 0)
                                {
                                    localisationkeyProperty.stringValue = null;
                                }
                                else
                                {
                                    localisationkeyProperty.stringValue = keys[currentKey];
                                }
                            }
                        }

                        //Draw button for adding new key
                        //show drop downs for where to put the key - enum list of all current folders + empty.
                        //select one to preapend
                        if (currentKey == 0)
                        {
                            float folderNameWidth       = 160.0f;
                            float autoKeySlashFakeWidth = 12.0f;
                            float autoKeySlashWidth     = 40.0f;
                            float autoKeybuttonWidth    = 42.0f;
                            float addButtonWidth        = 38.0f;
                            float buttonSpace           = 2.0f;
                            float fudge        = 13.0f;
                            float keyTextWidth = position.width - EditorUtils.GetLabelWidth() - (folderNameWidth + buttonSpace + autoKeybuttonWidth + buttonSpace + addButtonWidth);
                            float buttonWidth  = autoKeySlashFakeWidth + keyTextWidth + buttonSpace + autoKeybuttonWidth + buttonSpace + addButtonWidth;


                            //Get list of current folder options
                            Rect folderText = new Rect(position.x, yPos, position.width - buttonWidth, EditorGUIUtility.singleLineHeight);

                            string[] folders            = Localisation.GetStringFolders();
                            int      currentFolderIndex = 0;
                            string   currentFolder;
                            for (int i = 0; i < folders.Length; i++)
                            {
                                //To do - if the first bit of our folder exists then thats the current folder eg Roles/NewRole/Name - Roles/
                                if (folders[i] == Localisation.GetFolderName(localisationkeyProperty.stringValue))
                                {
                                    currentFolderIndex = i;
                                    break;
                                }
                            }
                            EditorGUI.BeginChangeCheck();
                            int newFolderIndex = EditorGUI.Popup(folderText, "New Key", currentFolderIndex, folders);
                            currentFolder = newFolderIndex == 0 ? "" : folders[newFolderIndex];
                            if (EditorGUI.EndChangeCheck())
                            {
                                if (newFolderIndex != 0)
                                {
                                    localisationkeyProperty.stringValue = currentFolder + "/" + Localisation.GetKeyWithoutFoldder(localisationkeyProperty.stringValue);
                                }
                                else if (currentFolderIndex != 0)
                                {
                                    localisationkeyProperty.stringValue = Localisation.GetKeyWithoutFoldder(localisationkeyProperty.stringValue);
                                }
                            }

                            Rect addKeySlash = new Rect(position.x + (position.width - buttonWidth) - fudge, yPos, autoKeySlashWidth, EditorGUIUtility.singleLineHeight);
                            EditorGUI.LabelField(addKeySlash, new GUIContent("/"));

                            Rect addKeyText = new Rect(position.x + (position.width - buttonWidth) - fudge + autoKeySlashFakeWidth, yPos, keyTextWidth + fudge, EditorGUIUtility.singleLineHeight);
                            if (newFolderIndex != 0)
                            {
                                string newAddKey = EditorGUI.TextField(addKeyText, Localisation.GetKeyWithoutFoldder(localisationkeyProperty.stringValue));
                                localisationkeyProperty.stringValue = currentFolder + "/" + newAddKey;
                            }
                            else
                            {
                                string newAddKey = EditorGUI.TextField(addKeyText, localisationkeyProperty.stringValue);
                                localisationkeyProperty.stringValue = newAddKey;
                            }

                            Rect autoKeyButton = new Rect(position.x + (position.width - buttonWidth) + autoKeySlashFakeWidth + buttonSpace + keyTextWidth, yPos, autoKeybuttonWidth, EditorGUIUtility.singleLineHeight);
                            Rect addKeyButton  = new Rect(position.x + (position.width - buttonWidth) + autoKeySlashFakeWidth + buttonSpace + keyTextWidth + buttonSpace + autoKeybuttonWidth, yPos, addButtonWidth, EditorGUIUtility.singleLineHeight);

                            yPos += addKeyButton.height;

                            if (GUI.Button(autoKeyButton, "Auto"))
                            {
                                LocalisedStringRef localisedStringRef = (LocalisedStringRef)EditorUtils.GetTargetObjectOfProperty(property);

                                Component component = property.serializedObject.targetObject as Component;
                                if (component != null)
                                {
                                    string parentName = component.gameObject.name;

                                    if (component.gameObject.scene.IsValid())
                                    {
                                        parentName = component.gameObject.scene.name + "_" + parentName;
                                    }

                                    localisedStringRef.SetAutoNameParentName(parentName);
                                }

                                localisationkeyProperty.stringValue = localisedStringRef.GetAutoKey();
                            }

                            if (GUI.Button(addKeyButton, "Add") && !string.IsNullOrEmpty(localisationkeyProperty.stringValue))
                            {
                                if (!Localisation.IsKeyInTable(localisationkeyProperty.stringValue))
                                {
                                    Localisation.UpdateString(localisationkeyProperty.stringValue, Localisation.GetCurrentLanguage(), string.Empty);
                                }
                            }
                        }

                        //Draw displayed text (can be edited to update localization file)
                        {
                            //Only display if have a valid key
                            if (!string.IsNullOrEmpty(localisationkeyProperty.stringValue) && Localisation.IsKeyInTable(localisationkeyProperty.stringValue))
                            {
                                string text       = Localisation.GetString(localisationkeyProperty.stringValue);
                                int    numLines   = StringUtils.GetNumberOfLines(text);
                                float  height     = (EditorGUIUtility.singleLineHeight - 2.0f) * numLines + 4.0f;
                                float  labelWidth = EditorUtils.GetLabelWidth();

                                Rect textPosition = new Rect(position.x + labelWidth, yPos, position.width - labelWidth, height);
                                yPos += height;

                                EditorGUI.BeginChangeCheck();
                                text = EditorGUI.TextArea(textPosition, text);
                                if (EditorGUI.EndChangeCheck())
                                {
                                    Localisation.UpdateString(localisationkeyProperty.stringValue, Localisation.GetCurrentLanguage(), text);
                                }
                            }
                        }

                        EditorGUI.indentLevel = origIndent;
                    }

                    EditorGUI.EndProperty();
                }
                public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
                {
                    EditorGUI.BeginProperty(position, label, property);

                    SerializedProperty localisationkeyProperty = property.FindPropertyRelative("_localisationKey");
                    string             localisationkey         = localisationkeyProperty.stringValue;

                    float yPos = position.y;

                    Rect foldoutPosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);

                    property.isExpanded = !EditorGUI.Foldout(foldoutPosition, !property.isExpanded, property.displayName + " (Localised String)");
                    yPos += EditorGUIUtility.singleLineHeight;

                    if (!property.isExpanded)
                    {
                        int origIndent = EditorGUI.indentLevel;
                        EditorGUI.indentLevel++;

                        //Draw list of possible keys
                        int currentKey = 0;
                        {
                            string[] keys = Localisation.GetStringKeys();

                            for (int i = 0; i < keys.Length; i++)
                            {
                                if (keys[i] == localisationkey)
                                {
                                    currentKey = i;
                                    break;
                                }
                            }

                            Rect typePosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);
                            yPos += EditorGUIUtility.singleLineHeight;

                            EditorGUI.BeginChangeCheck();
                            currentKey = EditorGUI.Popup(typePosition, "Localisation Key", currentKey, keys);

                            if (EditorGUI.EndChangeCheck())
                            {
                                if (currentKey == 0)
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, null);
                                }
                                else
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, keys[currentKey]);
                                }
                            }
                        }

                        //Draw button for adding new key
                        if (currentKey == 0)
                        {
                            string[] folders            = Localisation.GetStringFolders();
                            int      currentFolderIndex = 0;
                            string   keyWithoutFolder;
                            Localisation.GetFolderIndex(localisationkey, out currentFolderIndex, out keyWithoutFolder);


                            float keyTextWidth = position.width - EditorUtils.GetLabelWidth() - (folderNameWidth + buttonSpace + autoKeybuttonWidth + buttonSpace + addButtonWidth);
                            float buttonWidth  = autoKeySlashFakeWidth + keyTextWidth + buttonSpace + autoKeybuttonWidth + buttonSpace + addButtonWidth;


                            //Get list of current folder options
                            Rect folderText = new Rect(position.x, yPos, position.width - buttonWidth, EditorGUIUtility.singleLineHeight);

                            EditorGUI.BeginChangeCheck();
                            int    newFolderIndex = EditorGUI.Popup(folderText, "New Key", currentFolderIndex, folders);
                            string currentFolder  = newFolderIndex == 0 ? "" : folders[newFolderIndex];
                            if (EditorGUI.EndChangeCheck())
                            {
                                if (newFolderIndex != 0)
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, currentFolder + "/" + keyWithoutFolder);
                                }
                            }

                            Rect addKeySlash = new Rect(position.x + (position.width - buttonWidth) - fudge, yPos, autoKeySlashWidth, EditorGUIUtility.singleLineHeight);
                            EditorGUI.LabelField(addKeySlash, new GUIContent("/"));

                            Rect addKeyText = new Rect(position.x + (position.width - buttonWidth) - fudge + autoKeySlashFakeWidth, yPos, keyTextWidth + fudge, EditorGUIUtility.singleLineHeight);
                            if (newFolderIndex != 0)
                            {
                                EditorGUI.BeginChangeCheck();
                                keyWithoutFolder = EditorGUI.TextField(addKeyText, keyWithoutFolder);

                                if (EditorGUI.EndChangeCheck())
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, currentFolder + "/" + keyWithoutFolder);
                                }
                            }
                            else
                            {
                                EditorGUI.BeginChangeCheck();
                                localisationkey = EditorGUI.TextField(addKeyText, localisationkey);

                                if (EditorGUI.EndChangeCheck())
                                {
                                    UpdateNewLocalisedStringRef(property, localisationkey);
                                }
                            }

                            Rect autoKeyButton = new Rect(position.x + (position.width - buttonWidth) + autoKeySlashFakeWidth + buttonSpace + keyTextWidth, yPos, autoKeybuttonWidth, EditorGUIUtility.singleLineHeight);
                            Rect addKeyButton  = new Rect(position.x + (position.width - buttonWidth) + autoKeySlashFakeWidth + buttonSpace + keyTextWidth + buttonSpace + autoKeybuttonWidth, yPos, addButtonWidth, EditorGUIUtility.singleLineHeight);

                            yPos += addKeyButton.height;

                            if (GUI.Button(autoKeyButton, "Auto"))
                            {
                                LocalisedStringRef localisedStringRef = SerializedPropertyUtils.GetSerializedPropertyValue <LocalisedStringRef>(property);

                                Component component = property.serializedObject.targetObject as Component;
                                if (component != null)
                                {
                                    string parentName = component.gameObject.name;

                                    if (component.gameObject.scene.IsValid())
                                    {
                                        parentName = component.gameObject.scene.name + "_" + parentName;
                                    }

                                    localisedStringRef.SetAutoNameParentName(parentName);
                                }

                                localisationkey = UpdateNewLocalisedStringRef(property, localisedStringRef.GetAutoKey());
                            }

                            if (GUI.Button(addKeyButton, "Add") && !string.IsNullOrEmpty(localisationkey))
                            {
                                if (!Localisation.Exists(localisationkey))
                                {
                                    Localisation.Set(localisationkey, Localisation.GetCurrentLanguage(), string.Empty);
                                    LocalisationEditorWindow.EditString(localisationkey);
                                }
                            }
                        }

                        //Draw displayed text (can be edited to update localization file)
                        {
                            //Only display if have a valid key
                            if (!string.IsNullOrEmpty(localisationkey) && Localisation.Exists(localisationkey))
                            {
                                string text       = StringUtils.GetFirstLine(Localisation.GetRawString(localisationkey));
                                float  height     = EditorGUIUtility.singleLineHeight;
                                float  labelWidth = EditorUtils.GetLabelWidth();

                                Rect textPosition = new Rect(position.x + labelWidth + 2.0f, yPos, position.width - labelWidth - 2.0f - editbuttonWidth - buttonSpace, height);
                                EditorGUI.LabelField(textPosition, text, EditorUtils.ReadonlyTextBoxStyle);
                                Rect editTextPosition = new Rect(textPosition.x + textPosition.width + buttonSpace, yPos, editbuttonWidth, EditorGUIUtility.singleLineHeight);

                                if (GUI.Button(editTextPosition, "Edit"))
                                {
                                    LocalisationEditorWindow.EditString(localisationkey);
                                }

                                yPos += height;
                            }
                        }

                        EditorGUI.indentLevel = origIndent;
                    }

                    EditorGUI.EndProperty();
                }