Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Chronicles.Data.Mapping;
- using System;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- [CustomEditor(typeof(InteractableObject))]
- public class DynamicInteractableObjectEditor : Editor
- {
- // Base data
- private InteractableObject baseInteractable;
- // Foldouts
- private bool _showItemInteractions = false;
- private bool _showEntityStatModifications = false;
- private bool _showStorytelling = false;
- private bool _showSpecialInteractableBehaviour = false;
- // Reference to Interaction Sheet (Property)
- private SerializedProperty _referencedInteractionSheetProp;
- private InteractionSheet _referencedInteractionSheet;
- // Fetched DB data
- private List<MappedItem> _dbItems;
- private List<JournalEntry> _journalEntries;
- // Special Interactable Behaviour
- private bool _resetInteractionCount = false;
- // Add Item
- private bool _itemAddEnabled = false;
- private int _selectedAddItemIndex;
- private int _itemAddAmount = 0;
- // Remove Item
- private bool _itemRemoveEnabled = false;
- private int _selectedRemoveItemIndex;
- private int _itemRemoveAmount = 0;
- // Entity Stats
- private bool _restoreEntityHealthEnabled = false;
- public float _restoreEntityHealthAmount = 0;
- private float _drainEntityHealthAmount = 0;
- private bool _drainEntityHealthEnabled = false;
- private float _restoreEntityStaminaAmount = 0;
- private bool _restoreEntityStaminaEnabled = false;
- private float _drainEntityStaminaAmount = 0;
- private bool _drainEntityStaminaEnabled = false;
- private float _restoreEntitySatiationAmount = 0;
- private bool _restoreEntitySatiationEnabled = false;
- private float _drainEntitySatiationAmount = 0;
- private bool _drainEntitySatiationEnabled = false;
- // Storytelling
- private int _selectedJournalEntry = 0;
- private bool _journalEntryEnabled = false;
- private void OnEnable()
- {
- // Fetch serialized property of Interaction Sheet
- if (serializedObject != null && (this._referencedInteractionSheetProp = serializedObject.FindProperty("interactionSheet")) != null)
- {
- this._referencedInteractionSheet = this._referencedInteractionSheetProp.objectReferenceValue as InteractionSheet;
- }
- // Initial refreshing of data
- if (ChroniclesDatabaseWindow.Instance.IsConnected || ChroniclesDatabaseWindow.Instance.AutoConnect())
- {
- RefreshData();
- }
- // Get base interactable
- this.baseInteractable = target as InteractableObject;
- }
- public override void OnInspectorGUI()
- {
- DrawDefaultInspector();
- // Main Header
- EditorUtils.Header("Dynamic Interaction");
- // Interaction Sheet
- this.DrawInteractionSheetReferenceField();
- // Will disable all controls when no Interaction Sheet is loaded
- EditorGUI.BeginDisabledGroup(!this._referencedInteractionSheetProp.objectReferenceValue);
- {
- // Special Interactable Behaviour
- EditorGUILayout.Space();
- this.DrawSpecialBehavior();
- // Item Selection
- EditorGUILayout.Space();
- this.DrawItemSelectionEditor();
- // Entity Stats
- EditorGUILayout.Space();
- this.DrawEntityStatsEditor();
- // Storytelling
- EditorGUILayout.Space();
- this.DrawStorytellingEditor();
- // Save and Refresh Buttons
- EditorGUILayout.Space();
- this.DrawControlButtons();
- // Will disable all controls when no Interaction Sheet is loaded
- EditorGUI.EndDisabledGroup();
- }
- }
- private void DrawSpecialBehavior()
- {
- // Storytelling foldout
- this._showSpecialInteractableBehaviour = EditorGUILayout.Foldout(EditorPrefs.GetBool("DIOE_Foldout_SpecialInteractableBehaviour", true), "Special Interactable Behaviour", true, EditorStyles.foldout);
- if (this._showSpecialInteractableBehaviour != EditorPrefs.GetBool("DIOE_Foldout_SpecialInteractableBehaviour", true))
- {
- EditorPrefs.SetBool("DIOE_Foldout_SpecialInteractableBehaviour", this._showSpecialInteractableBehaviour);
- }
- // If foldout is opened, display Storytelling controls
- EditorGUILayout.BeginHorizontal();
- EditorGUI.BeginDisabledGroup(!ChroniclesDatabaseWindow.Instance.IsConnected);
- if (this._showSpecialInteractableBehaviour)
- {
- // Auto-Reset of interaction within a specific or random (between two constants) amount of time
- EditorGUILayout.BeginHorizontal();
- EditorGUI.BeginDisabledGroup(this.baseInteractable.OneTimeInteraction);
- this._resetInteractionCount = EditorGUILayout.Toggle("Reset Interaction", this._resetInteractionCount); // TODO <- Continue here
- EditorGUI.EndDisabledGroup();
- EditorGUILayout.EndHorizontal();
- }
- EditorGUI.EndDisabledGroup();
- EditorGUILayout.EndHorizontal();
- }
- private void DrawInteractionSheetReferenceField()
- {
- SerializedProperty oldReferencedInteractionSheetProp = this._referencedInteractionSheetProp;
- EditorGUILayout.BeginHorizontal();
- // Object Field (-> Interaction Sheet)
- EditorGUILayout.ObjectField(this._referencedInteractionSheetProp, typeof(InteractionSheet));
- // Create Button
- EditorGUI.BeginDisabledGroup(this._referencedInteractionSheetProp.objectReferenceValue);
- if (GUILayout.Button("Create", GUILayout.Width(60.0f)))
- {
- this._referencedInteractionSheetProp.objectReferenceValue = InteractionSheetCreator.CreateSheet();
- }
- EditorGUI.EndDisabledGroup();
- // Delete Button
- EditorGUI.BeginDisabledGroup(!this._referencedInteractionSheetProp.objectReferenceValue);
- if (GUILayout.Button("Delete", GUILayout.Width(60.0f)))
- {
- string assetPath = InteractionSheetCreator.TARGET_PATH + "/" + this._referencedInteractionSheetProp.objectReferenceValue.name + ".asset";
- this._referencedInteractionSheetProp.objectReferenceValue = null;
- DestroyImmediate(AssetDatabase.LoadAssetAtPath<InteractionSheet>(assetPath), true);
- }
- EditorGUI.EndDisabledGroup();
- // Save Interaction Sheet Reference if changes were made
- if (oldReferencedInteractionSheetProp != this._referencedInteractionSheetProp)
- {
- if (serializedObject.ApplyModifiedProperties())
- {
- Debug.Log("Interaction Sheet was updated on " + this.baseInteractable.gameObject.name);
- } else
- {
- Debug.LogError("Failed to update Interaction Sheet on " + this.baseInteractable.gameObject.name);
- }
- }
- EditorGUILayout.EndHorizontal();
- }
- private void DrawItemSelectionEditor()
- {
- // Items foldout
- this._showItemInteractions = EditorGUILayout.Foldout(EditorPrefs.GetBool("DIOE_Foldout_Items", true), "Adding / Removing Items from Inventory", true, EditorStyles.foldout);
- if (this._showItemInteractions != EditorPrefs.GetBool("DIOE_Foldout_Items", true))
- {
- EditorPrefs.SetBool("DIOE_Foldout_Items", this._showItemInteractions);
- }
- // If foldout is opened, display add / remove item controls
- if (this._showItemInteractions)
- {
- // Items Add / Remove is only available if a database connection is established
- EditorGUI.BeginDisabledGroup(!ChroniclesDatabaseWindow.Instance.IsConnected);
- GUILayout.BeginHorizontal();
- // Add Item Dropdown
- GUIContent addItemLabel = new GUIContent("Add Item");
- if (ChroniclesDatabaseWindow.Instance.IsConnected)
- {
- this._selectedAddItemIndex = EditorGUILayout.Popup(addItemLabel, this._selectedAddItemIndex, this._dbItems.ToArray());
- }
- else
- {
- EditorGUILayout.Popup(addItemLabel, 0, new string[] { "<Not connected>" });
- }
- // Add Item Amount
- this._itemAddAmount = EditorGUILayout.IntField(this._itemAddAmount, GUILayout.Width(60.0f));
- this._itemAddEnabled = EditorGUILayout.Toggle(this._itemAddEnabled, GUILayout.Width(20.0f));
- GUILayout.EndHorizontal();
- // Remove Item Dropdown
- GUILayout.BeginHorizontal();
- GUIContent removeItemLabel = new GUIContent("Remove Item");
- if (ChroniclesDatabaseWindow.Instance.IsConnected)
- {
- this._selectedRemoveItemIndex = EditorGUILayout.Popup(removeItemLabel, this._selectedRemoveItemIndex, this._dbItems.ToArray());
- }
- else
- {
- EditorGUILayout.Popup(removeItemLabel, 0, new string[] { "<Not connected>" });
- }
- // Add Item Amount
- this._itemRemoveAmount = EditorGUILayout.IntField(this._itemRemoveAmount, GUILayout.Width(60.0f));
- this._itemRemoveEnabled = EditorGUILayout.Toggle(this._itemRemoveEnabled, GUILayout.Width(20.0f));
- GUILayout.EndHorizontal();
- EditorGUI.EndDisabledGroup();
- }
- // Item amount input validation
- if (this._itemAddAmount < 0)
- {
- this._itemAddAmount = 0;
- }
- if (this._itemRemoveAmount < 0)
- {
- this._itemRemoveAmount = 0;
- }
- }
- private void DrawEntityStatsEditor()
- {
- // Entity Stat foldout
- this._showEntityStatModifications = EditorGUILayout.Foldout(EditorPrefs.GetBool("DIOE_Foldout_EntityStats", true), "Entity Stat Modification", true, EditorStyles.foldout);
- if (this._showEntityStatModifications != EditorPrefs.GetBool("DIOE_Foldout_EntityStats", true))
- {
- EditorPrefs.SetBool("DIOE_Foldout_EntityStats", this._showEntityStatModifications);
- }
- // If foldout is opened, display Entity Stat modification controls
- if (this._showEntityStatModifications)
- {
- // Restore Health
- EditorGUILayout.BeginHorizontal();
- this._restoreEntityHealthAmount = EditorGUILayout.FloatField(new GUIContent("Restore Health"), this._restoreEntityHealthAmount);
- this._restoreEntityHealthEnabled = EditorGUILayout.Toggle(this._restoreEntityHealthEnabled, GUILayout.Width(20.0f));
- EditorGUILayout.EndHorizontal();
- // Drain Health
- EditorGUILayout.BeginHorizontal();
- this._drainEntityHealthAmount = EditorGUILayout.FloatField(new GUIContent("Drain Health"), this._drainEntityHealthAmount);
- this._drainEntityHealthEnabled = EditorGUILayout.Toggle(this._drainEntityHealthEnabled, GUILayout.Width(20.0f));
- EditorGUILayout.EndHorizontal();
- // Restore Stamina
- EditorGUILayout.BeginHorizontal();
- this._restoreEntityStaminaAmount = EditorGUILayout.FloatField(new GUIContent("Restore Stamina"), this._restoreEntityStaminaAmount);
- this._restoreEntityStaminaEnabled = EditorGUILayout.Toggle(this._restoreEntityStaminaEnabled, GUILayout.Width(20.0f));
- EditorGUILayout.EndHorizontal();
- // Drain Stamina
- EditorGUILayout.BeginHorizontal();
- this._drainEntityStaminaAmount = EditorGUILayout.FloatField(new GUIContent("Drain Stamina"), this._drainEntityStaminaAmount);
- this._drainEntityStaminaEnabled = EditorGUILayout.Toggle(this._drainEntityStaminaEnabled, GUILayout.Width(20.0f));
- EditorGUILayout.EndHorizontal();
- // Restore Satiation
- EditorGUILayout.BeginHorizontal();
- this._restoreEntitySatiationAmount = EditorGUILayout.FloatField(new GUIContent("Restore Satiation"), this._restoreEntitySatiationAmount);
- this._restoreEntitySatiationEnabled = EditorGUILayout.Toggle(this._restoreEntitySatiationEnabled, GUILayout.Width(20.0f));
- EditorGUILayout.EndHorizontal();
- // Drain Satiation
- EditorGUILayout.BeginHorizontal();
- this._drainEntitySatiationAmount = EditorGUILayout.FloatField(new GUIContent("Drain Satiation"), this._drainEntitySatiationAmount);
- this._drainEntitySatiationEnabled = EditorGUILayout.Toggle(this._drainEntitySatiationEnabled, GUILayout.Width(20.0f));
- EditorGUILayout.EndHorizontal();
- // Input validation
- if (this._restoreEntityHealthAmount < 0)
- {
- this._restoreEntityHealthAmount = 0;
- }
- if (this._drainEntityHealthAmount < 0)
- {
- this._drainEntityHealthAmount = 0;
- }
- if (this._restoreEntityStaminaAmount < 0)
- {
- this._restoreEntityStaminaAmount = 0;
- }
- if (this._drainEntityStaminaAmount < 0)
- {
- this._drainEntityStaminaAmount = 0;
- }
- if (this._restoreEntitySatiationAmount < 0)
- {
- this._restoreEntitySatiationAmount = 0;
- }
- if (this._drainEntitySatiationAmount < 0)
- {
- this._drainEntitySatiationAmount = 0;
- }
- }
- }
- private void DrawStorytellingEditor()
- {
- // Storytelling foldout
- this._showStorytelling = EditorGUILayout.Foldout(EditorPrefs.GetBool("DIOE_Foldout_Storytelling", true), "Storytelling", true, EditorStyles.foldout);
- if (this._showStorytelling != EditorPrefs.GetBool("DIOE_Foldout_Storytelling", true))
- {
- EditorPrefs.SetBool("DIOE_Foldout_Storytelling", this._showStorytelling);
- }
- // If foldout is opened, display Storytelling controls
- EditorGUILayout.BeginHorizontal();
- EditorGUI.BeginDisabledGroup(!ChroniclesDatabaseWindow.Instance.IsConnected);
- if (this._showStorytelling)
- {
- this._selectedJournalEntry = this.DrawJournalEntryPopup("Trigger Journal Entry", this._selectedJournalEntry);
- this._journalEntryEnabled = EditorGUILayout.Toggle(this._journalEntryEnabled, GUILayout.Width(20.0f));
- }
- EditorGUI.EndDisabledGroup();
- EditorGUILayout.EndHorizontal();
- }
- private void DrawControlButtons()
- {
- // Save Changes Button
- EditorGUILayout.BeginHorizontal();
- if (GUILayout.Button("Save Changes"))
- {
- this.SaveProperties();
- }
- // Refresh from DB Button
- if (GUILayout.Button("Refresh from DB"))
- {
- this.RefreshFromDB();
- }
- EditorGUILayout.EndHorizontal();
- }
- private int DrawJournalEntryPopup(string label, int selectedIndex, params GUILayoutOption[] options)
- {
- if (ChroniclesDatabaseWindow.Instance.IsConnected)
- {
- selectedIndex = EditorGUILayout.Popup(label, selectedIndex, this._journalEntries.ConvertAll(j => j.Title).ToArray(), options);
- } else
- {
- EditorGUI.BeginDisabledGroup(true);
- EditorGUILayout.Popup(0, new string[] { "<Not connected>" });
- EditorGUI.EndDisabledGroup();
- }
- return selectedIndex;
- }
- private void SaveProperties()
- {
- if (_referencedInteractionSheet)
- {
- if (ChroniclesDatabaseWindow.Instance.IsConnected && this._dbItems != null && this._dbItems.Count > 0)
- {
- this._referencedInteractionSheet.enableItemAdding = this._itemAddEnabled;
- this._referencedInteractionSheet.itemToBeAddedAmount = this._itemAddAmount;
- if (this._selectedAddItemIndex < 0 || this._selectedAddItemIndex >= this._dbItems.Count)
- {
- this._selectedAddItemIndex = 0;
- this._itemAddEnabled = false;
- }
- this._referencedInteractionSheet.itemToBeAdded = this._dbItems[this._selectedAddItemIndex].item.ID;
- this._referencedInteractionSheet.enableItemRemoval = this._itemRemoveEnabled;
- this._referencedInteractionSheet.itemToBeRemovedAmount = this._itemRemoveAmount;
- if (this._selectedRemoveItemIndex < 0 || this._selectedRemoveItemIndex >= this._dbItems.Count)
- {
- this._selectedRemoveItemIndex = 0;
- this._itemRemoveEnabled = false;
- }
- this._referencedInteractionSheet.itemToBeRemoved = this._dbItems[this._selectedRemoveItemIndex].item.ID;
- }
- // Storytelling - Journal Entry
- if (this._journalEntries != null && this._journalEntries.Count > 0)
- {
- if (this._selectedJournalEntry >= this._journalEntries.Count || this._selectedJournalEntry < 0)
- {
- this._selectedJournalEntry = 0;
- this._journalEntryEnabled = false;
- }
- int journalEntryID = this._journalEntries[this._selectedJournalEntry].ID;
- this._referencedInteractionSheet.addJournalEntryID = journalEntryID;
- this._referencedInteractionSheet.enableJournalEntry = this._journalEntryEnabled;
- }
- this._referencedInteractionSheet.enableHealthRestore = this._restoreEntityHealthEnabled;
- this._referencedInteractionSheet.enableHealthDrain = this._drainEntityHealthEnabled;
- this._referencedInteractionSheet.enableStaminaRestore = this._restoreEntityStaminaEnabled;
- this._referencedInteractionSheet.enableStaminaDrain = this._drainEntityStaminaEnabled;
- this._referencedInteractionSheet.enableSatiationRestore = this._restoreEntitySatiationEnabled;
- this._referencedInteractionSheet.enableSatiationDrain = this._drainEntitySatiationEnabled;
- this._referencedInteractionSheet.healthToRestore = this._restoreEntityHealthAmount;
- this._referencedInteractionSheet.staminaToRestore = this._restoreEntityStaminaAmount;
- this._referencedInteractionSheet.satiationToRestore = this._restoreEntitySatiationAmount;
- this._referencedInteractionSheet.healthToDrain = this._drainEntityHealthAmount;
- this._referencedInteractionSheet.staminaToDrain = this._drainEntityStaminaAmount;
- this._referencedInteractionSheet.satiationToDrain = this._drainEntitySatiationAmount;
- // Save Data!
- if (serializedObject.ApplyModifiedProperties())
- {
- Debug.Log("Interaction Sheet was saved for GameObject " + this.baseInteractable.gameObject.name);
- } else
- {
- Debug.LogError("Failed to save Interaction Sheet of GameObject " + this.baseInteractable.gameObject.name);
- }
- }
- }
- public void RefreshFromDB()
- {
- this._journalEntries = ChroniclesDatabaseWindow.Instance.Database.JournalDatabase.GetAllJournalEntries();
- this._dbItems = ChroniclesDatabaseWindow.Instance.Database.ItemDatabase.GetAllItems().ConvertAll(i => new MappedItem()
- {
- item = i,
- text = i.Name
- });
- }
- public void RefreshData()
- {
- if (!ChroniclesDatabaseWindow.Instance || !ChroniclesDatabaseWindow.Instance.IsConnected)
- {
- return;
- }
- RefreshFromDB();
- if (this._referencedInteractionSheet)
- {
- // Retrieve from DB
- this._selectedAddItemIndex = this._dbItems.FindIndex(i => i.item.ID == this._referencedInteractionSheet.itemToBeAdded);
- this._selectedRemoveItemIndex = this._dbItems.FindIndex(i => i.item.ID == this._referencedInteractionSheet.itemToBeRemoved);
- this._selectedJournalEntry = this._journalEntries.FindIndex(j => j.ID == this._referencedInteractionSheet.addJournalEntryID);
- // Fix dropdown index
- if (this._selectedAddItemIndex < 0)
- {
- this._selectedAddItemIndex = 0;
- }
- if (this._selectedRemoveItemIndex < 0)
- {
- this._selectedRemoveItemIndex = 0;
- }
- if (this._selectedJournalEntry < 0)
- {
- this._selectedJournalEntry = 0;
- }
- // Get initial values (DB independent)
- this._itemAddEnabled = this._referencedInteractionSheet.enableItemAdding;
- this._itemRemoveEnabled = this._referencedInteractionSheet.enableItemRemoval;
- this._itemAddAmount = this._referencedInteractionSheet.itemToBeAddedAmount;
- this._itemRemoveAmount = this._referencedInteractionSheet.itemToBeRemovedAmount;
- this._restoreEntityHealthEnabled = this._referencedInteractionSheet.enableHealthRestore;
- this._restoreEntityStaminaEnabled = this._referencedInteractionSheet.enableStaminaRestore;
- this._restoreEntitySatiationEnabled = this._referencedInteractionSheet.enableSatiationRestore;
- this._drainEntityHealthEnabled = this._referencedInteractionSheet.enableHealthDrain;
- this._drainEntityStaminaEnabled = this._referencedInteractionSheet.enableStaminaDrain;
- this._drainEntitySatiationEnabled = this._referencedInteractionSheet.enableSatiationDrain;
- this._restoreEntityHealthAmount = this._referencedInteractionSheet.healthToRestore;
- this._restoreEntityStaminaAmount = this._referencedInteractionSheet.staminaToRestore;
- this._restoreEntitySatiationAmount = this._referencedInteractionSheet.satiationToRestore;
- this._drainEntityHealthAmount = this._referencedInteractionSheet.healthToDrain;
- this._drainEntityStaminaAmount = this._referencedInteractionSheet.staminaToDrain;
- this._drainEntitySatiationAmount = this._referencedInteractionSheet.satiationToDrain;
- this._journalEntryEnabled = this._referencedInteractionSheet.enableJournalEntry;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement