public void OnEnable() { activeGeneratorSetting = 0; activeComponentPool = -1; currentAddGeneratorSettingIndex = 1; scrollPosition = Vector2.zero; dmgScrollPosition = Vector2.zero; if (target != null) { readCurrentMission(); } Undo.undoRedoPerformed += onUndoRedoPerformed; dmgString = DMGMissionLoader.GetDmgString(serializedObject.targetObject as KMMission); errorMessage = null; }
private bool LoadFromDMGString() { // Reset error message errorMessage = null; // Parse mission KMMission mission; try { mission = DMGMissionLoader.CreateMissionFromDmgString(dmgString); } catch (DMGMissionLoader.ParseException e) { errorMessage = e.Message; return(false); } // Load mission properties serializedObject.FindProperty("PacingEventsEnabled").boolValue = mission.PacingEventsEnabled; serializedObject.FindProperty("DisplayName").stringValue = mission.DisplayName; serializedObject.FindProperty("Description").stringValue = mission.Description; serializedObject.FindProperty("GeneratorSetting.TimeLimit").floatValue = mission.GeneratorSetting.TimeLimit; serializedObject.FindProperty("GeneratorSetting.NumStrikes").intValue = mission.GeneratorSetting.NumStrikes; serializedObject.FindProperty("GeneratorSetting.TimeBeforeNeedyActivation").intValue = mission.GeneratorSetting.TimeBeforeNeedyActivation; serializedObject.FindProperty("GeneratorSetting.OptionalWidgetCount").intValue = mission.GeneratorSetting.OptionalWidgetCount; serializedObject.FindProperty("GeneratorSetting.FrontFaceOnly").boolValue = mission.GeneratorSetting.FrontFaceOnly; // Delete current pools var componentPools = serializedObject.FindProperty("GeneratorSetting.ComponentPools"); if (componentPools.arraySize > 0) { for (int i = componentPools.arraySize - 1; i >= 0; i--) { componentPools.DeleteArrayElementAtIndex(i); } } // Save pools var pools = mission.GeneratorSetting.ComponentPools; for (int i = 0; i < pools.Count; i++) { componentPools.InsertArrayElementAtIndex(i); var element = componentPools.GetArrayElementAtIndex(i); var pool = pools[i]; element.FindPropertyRelative("Count").intValue = pool.Count; element.FindPropertyRelative("SpecialComponentType").intValue = (int)pool.SpecialComponentType; element.FindPropertyRelative("ComponentTypes").arraySize = pool.ComponentTypes == null ? 0 : pool.ComponentTypes.Count; if (pool.ComponentTypes != null) { for (int j = 0; j < pool.ComponentTypes.Count; j++) { element.FindPropertyRelative("ComponentTypes").GetArrayElementAtIndex(j).intValue = (int)pool.ComponentTypes[j]; } } element.FindPropertyRelative("ModTypes").arraySize = pool.ModTypes == null ? 0 : pool.ModTypes.Count; if (pool.ModTypes != null) { for (int j = 0; j < pool.ModTypes.Count; j++) { element.FindPropertyRelative("ModTypes").GetArrayElementAtIndex(j).stringValue = pool.ModTypes[j]; } } } return(true); }
public override void OnInspectorGUI() { bool validModification = true; if (target != null) { serializedObject.Update(); EditorGUI.BeginChangeCheck(); //Basic mission meta-data EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("ID"); EditorGUILayout.SelectableLabel(serializedObject.targetObject.name); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("ID In-Game"); EditorGUILayout.SelectableLabel(string.Format("mod_{0}_{1}", ModConfig.ID, serializedObject.targetObject.name)); EditorGUILayout.EndHorizontal(); SerializedProperty displayNameProperty = serializedObject.FindProperty("DisplayName"); EditorGUILayout.PropertyField(displayNameProperty); displayNameProperty.stringValue = displayNameProperty.stringValue.Trim(); EditorGUILayout.PropertyField(serializedObject.FindProperty("Description")); EditorGUILayout.PropertyField(serializedObject.FindProperty("PacingEventsEnabled")); SerializedProperty componentPools = serializedObject.FindProperty("GeneratorSetting.ComponentPools"); if (factoryMode <= FactoryMode.FiniteSequenceGlobalTimeStrikes) { int newTotalBombCount = EditorGUILayout.IntField("Bomb Count", totalBombCount); setTotalBombCount(newTotalBombCount); } else { bool wasEnabled = GUI.enabled; GUI.enabled = false; EditorGUILayout.TextField("Bomb Count", "Infinite"); GUI.enabled = wasEnabled; } FactoryMode newFactoryMode = (FactoryMode)EditorGUILayout.Popup("Factory Mode", (int)factoryMode, FactoryModeFriendlyNames); if (newFactoryMode != factoryMode) { if (newFactoryMode == FactoryMode.Static) { if (factoryModeComponentPool != -1) { componentPools.DeleteArrayElementAtIndex(factoryModeComponentPool); readCurrentMission(); } } else if (factoryModeComponentPool == -1) { int index = addComponentPool(serializedObject.FindProperty("GeneratorSetting")); SerializedProperty componentPool = componentPools.GetArrayElementAtIndex(index); componentPool.FindPropertyRelative("Count").intValue = (int)newFactoryMode; componentPool.FindPropertyRelative("ModTypes").arraySize = 1; componentPool.FindPropertyRelative("ModTypes").GetArrayElementAtIndex(0).stringValue = FACTORY_MODE_COMPONENT_POOL_ID; factoryModeComponentPool = index; } else { componentPools.GetArrayElementAtIndex(factoryModeComponentPool).FindPropertyRelative("Count").intValue = (int)newFactoryMode; } if (newFactoryMode >= FactoryMode.InfiniteSequence) { setTotalBombCount(1); } factoryMode = newFactoryMode; } //Generator Settings EditorGUILayout.Separator(); EditorGUILayout.LabelField("Generator Settings"); List <string> unusedGeneratorSettings = new List <string>(); List <KeyValuePair <int, string> > tabMap = new List <KeyValuePair <int, string> >(); tabMap.Add(new KeyValuePair <int, string>(0, "Bomb 0")); foreach (KeyValuePair <int, KeyValuePair <KMGeneratorSetting, int> > kv in multipleBombsGeneratorSettings) { if (kv.Key < totalBombCount || factoryMode >= FactoryMode.InfiniteSequence) { tabMap.Add(new KeyValuePair <int, string>(kv.Key, "Bomb " + kv.Key)); } else { unusedGeneratorSettings.Add(kv.Key.ToString()); } } tabMap.Sort((x, y) => x.Key.CompareTo(y.Key)); if (unusedGeneratorSettings.Count > 0) { string unusedGeneratorSettingsWarningMessage = "The mission contains unused generator settings (for " + (unusedGeneratorSettings.Count > 1 ? "bombs " + string.Join(", ", unusedGeneratorSettings.ToArray()) : "bomb " + unusedGeneratorSettings[0]) + ")."; EditorGUILayout.HelpBox(unusedGeneratorSettingsWarningMessage, MessageType.Warning); } EditorGUILayout.BeginVertical("box"); int currentTab = activeGeneratorSetting != -1 ? tabMap.FindIndex((x) => x.Key == activeGeneratorSetting) : tabMap.Count; if (currentTab == -1) { activeGeneratorSetting = 0; currentTab = 0; } List <string> tabs = new List <string>(); tabs.Add(tabMap[0].Value); float minWidth = new GUIStyle("ButtonLeft").CalcSize(new GUIContent(tabMap[0].Value)).x; for (int i = 1; i < tabMap.Count; i++) { tabs.Add(tabMap[i].Value); float width = new GUIStyle("Button").CalcSize(new GUIContent(tabMap[i].Value)).x; if (width > minWidth) { minWidth = width; } } tabs.Add("+"); bool fits = Screen.width / tabs.Count > minWidth; //Screen.width is not an accurate measure of the available width but having the bar space always visible was too ugly if (!fits) { scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(40)); } int newTab = GUILayout.Toolbar(currentTab, tabs.ToArray()); if (!fits) { EditorGUILayout.EndScrollView(); } if (newTab != currentTab) { if (newTab == tabs.Count - 1) { activeGeneratorSetting = -1; } else { activeGeneratorSetting = tabMap[newTab].Key; activeComponentPool = -1; GUIUtility.keyboardControl = 0; } } if (activeGeneratorSetting == -1) { if (factoryMode <= FactoryMode.FiniteSequenceGlobalTimeStrikes) { List <int> vaildBombs = new List <int>(); for (int i = 1; i < totalBombCount; i++) { if (!multipleBombsGeneratorSettings.ContainsKey(i)) { vaildBombs.Add(i); } } if (vaildBombs.Count == 0) { EditorGUILayout.HelpBox("All of the bombs have a Generator Setting.", MessageType.None); } else { if (!vaildBombs.Contains(currentAddGeneratorSettingIndex)) { currentAddGeneratorSettingIndex = vaildBombs[0]; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Add Generator Setting for bomb"); currentAddGeneratorSettingIndex = vaildBombs[EditorGUILayout.Popup(vaildBombs.IndexOf(currentAddGeneratorSettingIndex), vaildBombs.Select((x) => x.ToString()).ToArray(), GUILayout.Width(60))]; EditorGUILayout.EndHorizontal(); } bool wasEnabled = GUI.enabled; GUI.enabled = vaildBombs.Count != 0; if (GUILayout.Button("Add Generator Setting")) { addGeneratorSetting(currentAddGeneratorSettingIndex); } GUI.enabled = wasEnabled; } else { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Add Generator Setting for bomb"); currentAddGeneratorSettingIndex = EditorGUILayout.IntField(currentAddGeneratorSettingIndex, GUILayout.Width(60)); EditorGUILayout.EndHorizontal(); bool isVaildBomb = currentAddGeneratorSettingIndex != 0 && !multipleBombsGeneratorSettings.ContainsKey(currentAddGeneratorSettingIndex); if (!isVaildBomb) { EditorGUILayout.HelpBox("Bomb " + currentAddGeneratorSettingIndex + " already has a Generator Setting.", MessageType.None); } bool wasEnabled = GUI.enabled; GUI.enabled = isVaildBomb; if (GUILayout.Button("Add Generator Setting")) { addGeneratorSetting(currentAddGeneratorSettingIndex); } GUI.enabled = wasEnabled; } } else if (activeGeneratorSetting == 0) { bool wasEnabled = GUI.enabled; GUI.enabled = false; EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.Button("Delete"); EditorGUILayout.EndHorizontal(); GUI.enabled = wasEnabled; drawGeneratorSetting(serializedObject.FindProperty("GeneratorSetting"), true); } else { EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); bool delete = GUILayout.Button("Delete"); EditorGUILayout.EndHorizontal(); if (delete) { removeGeneratorSetting(activeGeneratorSetting); } else { KeyValuePair <KMGeneratorSetting, int> activeKV = multipleBombsGeneratorSettings[activeGeneratorSetting]; KMMission dummyMission = CreateInstance <KMMission>(); dummyMission.GeneratorSetting = activeKV.Key; SerializedObject dummyMissionObject = new SerializedObject(dummyMission); drawGeneratorSetting(dummyMissionObject.FindProperty("GeneratorSetting"), false); if (dummyMissionObject.ApplyModifiedProperties()) { serializedObject.FindProperty("GeneratorSetting").FindPropertyRelative("ComponentPools").GetArrayElementAtIndex(activeKV.Value).FindPropertyRelative("ModTypes").GetArrayElementAtIndex(0).stringValue = "Multiple Bombs:" + activeGeneratorSetting + ":" + JsonConvert.SerializeObject(activeKV.Key); } } } EditorGUILayout.EndVertical(); //DMG String EditorGUILayout.PrefixLabel("DMG Mission String"); if (EditorGUI.EndChangeCheck()) { dmgString = DMGMissionLoader.GetDmgString((KMMission)serializedObject.targetObject); } dmgScrollPosition = EditorGUILayout.BeginScrollView(dmgScrollPosition, GUILayout.Height(15 * EditorGUIUtility.singleLineHeight)); dmgString = EditorGUILayout.TextArea(dmgString, GUILayout.ExpandHeight(true)); EditorGUILayout.EndScrollView(); if (GUILayout.Button("Refresh DMG Mission String")) { activeComponentPool = -1; dmgString = DMGMissionLoader.GetDmgString((KMMission)serializedObject.targetObject); errorMessage = null; } if (GUILayout.Button("Load from DMG Mission String")) { activeComponentPool = -1; validModification = LoadFromDMGString(); } if (GUILayout.Button("Open DMG Documentation")) { Application.OpenURL("https://github.com/red031000/ktane-DynamicMissionGenerator/blob/master/README.md"); } if (errorMessage != null) { GUIStyle s = new GUIStyle(EditorStyles.boldLabel); s.normal.textColor = Color.red; EditorGUILayout.LabelField("Error: " + errorMessage, s); } } if (validModification) { serializedObject.ApplyModifiedProperties(); } }