/// <summary> /// Removes a registry from the list to be serialized. /// </summary> /// <param name="name">The ButtonName of the button.</param> /// <param name="action">The ButtonActions of the button.</param> /// <param name="overridesInteraction">True if this button overrides interaction.</param> /// <param name="isRightController">True if this button stays on the right controller.</param> private void RemoveButtonChoice(Button.ButtonName name, Button.ButtonActions action, bool overridesInteraction, bool isRightController) { ButtonRegistry registry = ScriptableObject.CreateInstance <ButtonRegistry>(); registry.Name = name; registry.Action = action; registry.OverrideInteraction = overridesInteraction; registry.IsRightControllerButton = isRightController; if (action == Button.ButtonActions.PressUp) { registry.methodName = nameof(Teleport); } else if (action == Button.ButtonActions.HoldDown) { registry.methodName = nameof(WhileHoldingButtonDown); } else { Debug.LogError("Could not find a method to register for this action, is there something off?"); } if (TeleportSettings.ButtonsSelected.Contains(registry)) { TeleportSettings.ButtonsSelected.Remove(registry); } }
/// <summary> /// Creates the registry of a button (ButtonRegistry) on the serializable data class. /// </summary> /// <param name="name">The ButtonName of the button.</param> /// <param name="action">The ButtonActions of the button.</param> /// <param name="overridesInteraction">True if this button overrides interaction.</param> /// <param name="isRightController">True if this button stays on the right controller.</param> private void SaveButtonChoice(Button.ButtonName name, Button.ButtonActions action, bool overridesInteraction, bool isRightController) { ButtonRegistry registry = ScriptableObject.CreateInstance <ButtonRegistry>(); registry.Name = name; registry.Action = action; registry.OverrideInteraction = overridesInteraction; registry.IsRightControllerButton = isRightController; if (action == Button.ButtonActions.PressUp) { registry.methodName = nameof(Teleport); } else if (action == Button.ButtonActions.HoldDown) { registry.methodName = nameof(WhileHoldingButtonDown); } else { Debug.LogError("Could not find a method to register for this action, is there something off?"); return; } // Append it to the teleport Settings object. registry.AddToAsset(TeleportSettings.GetPathToData()); // This is a costy operation.. if (!TeleportSettings.ButtonsSelected.Contains(registry)) { TeleportSettings.ButtonsSelected.Add(registry); } TeleportSettings.Save();// This is a costy operation.. }
/// <summary> /// Clear selected buttons locally. /// </summary> public void ClearButtonRegistries() { if (TeleportSettings.ButtonsSelected.Count == 0) { // This happens during initialization // No button recorded on the feature. return; } // Clear the list of buttons we have. for (int i = 0; i < TeleportSettings.ButtonsSelected.Count; i++) { ButtonRegistry.Delete(TeleportSettings.ButtonsSelected[i], TeleportSettings); } TeleportSettings.ButtonsSelected.Clear(); }
//// called first //void OnEnable() //{ // Debug.Log("OnEnable called"); // SceneManager.sceneLoaded += OnSceneLoaded; //} /// <summary> /// Register all the buttons on the control set. /// </summary> /// <param name="userSelectedButtons">The ButtonNames</param> /// <param name="userSelectedActions">The ButtonActions</param> public void RegisterButtons(Button.ButtonName[] userSelectedButtons, Button.ButtonActions[] userSelectedActions, bool[] positions) { if (ButtonsSelected == null) { ButtonsSelected = new List <ButtonRegistry>(); } // Foreach userSelectedButton we add the button. for (int i = 0; i < userSelectedButtons.Length; i++) { ButtonRegistry reg = ScriptableObject.CreateInstance <ButtonRegistry>(); reg.Name = userSelectedButtons[i]; reg.Action = userSelectedActions[i]; reg.IsRightControllerButton = positions[i]; reg.OverrideInteraction = true; ButtonsSelected.Add(reg); } }
/// <summary> /// Load the selected buttons. /// </summary> protected virtual void LoadSelectedButtons() { InteractableObject interactableObject = (InteractableObject)this.target; if (interactableObject.ButtonsSelected == null) { interactableObject.ButtonsSelected = new List <ButtonRegistry>(); } userSelectedButtons = new Button.ButtonName[interactableObject.AmountOfButtons]; userSelectedActions = new Button.ButtonActions[interactableObject.AmountOfButtons]; // Clone the list. List <ButtonRegistry> tempList = new List <ButtonRegistry>(); tempList.AddRange(interactableObject.ButtonsSelected); // We need templist because the amount of buttons on buttonSelected // is bigger than the amount of buttons we will show on the UI. // Foreach button for (int i = 0; i < interactableObject.ButtonsSelected.Count; i++) { // Pick a button and see if it already was registered. ButtonRegistry buttonToRegister = tempList.Find(button => button == interactableObject.ButtonsSelected[i]); if (buttonToRegister == null) { continue; // If so continue to the next. } // Case not register it, so it shows in the component inspector. userSelectedButtons[i] = buttonToRegister.Name; userSelectedActions[i] = buttonToRegister.Action; // Now remove him and all the same others from the list. tempList.RemoveAll(b => b.Name == buttonToRegister.Name && b.Action == buttonToRegister.Action); } }
/// <summary> /// Returns the right method from a feature given a ButtonRegistry. /// </summary> /// <param name="buttonRegistry">A button registry.</param> /// <returns>A method</returns> public Action GetButtonMethod(ButtonRegistry buttonRegistry) { if (TeleportSettings.ButtonsSelected == null) { Debug.LogError("ButtonsSelected on TeleportSettings not initialized!"); return(null); } // Retrieving the button. List <ButtonRegistry> matchButtons = TeleportSettings.ButtonsSelected.FindAll(b => b.Action == buttonRegistry.Action && b.Name == buttonRegistry.Name && b.IsRightControllerButton == buttonRegistry.IsRightControllerButton && b.OverrideInteraction == buttonRegistry.OverrideInteraction); if (matchButtons.Count == 0) { Debug.LogError("No buttons were found for this ButtonRegistry."); return(null); } else if (matchButtons.Count > 1) { Debug.Log("Multiple buttons with the same registry found. Something could be wrong with the serialization."); } // Finding the proper method. if (matchButtons[0].methodName == nameof(Teleport)) { return(Teleport); } else if (matchButtons[0].methodName == nameof(WhileHoldingButtonDown)) { return(WhileHoldingButtonDown); } return(null); }
/// <summary> /// Register the selected buttons on the interaction. /// </summary> private void RegisterSelectedButtons() { InteractableObject interactableObject = (InteractableObject)this.target; // We must add for all known positions! The ControllerManager always calls the correct position Method. Button.ButtonName[] finalButtonNames = new Button.ButtonName[interactableObject.AmountOfButtons * 2]; Button.ButtonActions[] finalButtonActions = new Button.ButtonActions[interactableObject.AmountOfButtons * 2]; bool[] finalPositions = new bool[interactableObject.AmountOfButtons * 2]; // Creating positions and setting final buttons to be passed. for (int i = 0; i < interactableObject.AmountOfButtons; i++) { // Copy buttons finalButtonNames[i] = userSelectedButtons[i]; finalButtonNames[i + 1] = userSelectedButtons[i]; // Copy Actions finalButtonActions[i] = finalButtonActions[i]; finalButtonActions[i + 1] = finalButtonActions[i]; // Create Positions finalPositions[i] = true; finalPositions[i + 1] = false; } // Calling unregister and register methods. IActiveInteraction interaction = interactableObject as IActiveInteraction; // Unregister all buttons.. interactableObject.ButtonsSelected.Clear(); // Register all buttons.. Debug.Log("Registering..."); if (interactableObject.ButtonsSelected == null) { interactableObject.ButtonsSelected = new List <ButtonRegistry>(); } // Foreach userSelectedButton we add the button. for (int i = 0; i < finalButtonNames.Length; i++) { ButtonRegistry reg = ScriptableObject.CreateInstance <ButtonRegistry>(); reg.Name = finalButtonNames[i]; reg.Action = finalButtonActions[i]; reg.IsRightControllerButton = finalPositions[i]; reg.OverrideInteraction = true; interactableObject.ButtonsSelected.Add(reg); } // This saves the button selection. SerializedProperty buttons = serializedObject.FindProperty("ButtonsSelected"); buttons.arraySize = interactableObject.ButtonsSelected.Count; //int j = 0; buttons.Next(true); // Go further on this property (Ends up on Array) buttons.Next(true); // Go further on again.. (Ends up probaly on the array size. buttons.Next(true); // Go further on more time.. (Ends up on first element) foreach (ButtonRegistry b in interactableObject.ButtonsSelected) { buttons.objectReferenceValue = b; buttons.Next(false); // move to the next element. (The argument here is false because the next element is adjacent to this // if it was true we would go to a property inside the current element). } serializedObject.ApplyModifiedProperties(); }