// Keybind list functions // ================================================== /// <summary> /// Populates the list of keybinds, will destroy old ones if they already exist /// </summary> public void PopulateKeybindScrollView() { Logger.Log("Populating keybind scroll view", Category.Keybindings); if (KeybindItemList.Count > 0) { Logger.Log("Removing old keybind objects", Category.Keybindings); // Destroy all items in list if it already exists foreach (GameObject item in KeybindItemList) { Destroy(item.gameObject); } KeybindItemList.Clear(); } // Reverse loop direction so items are in intended order under the headings for (int i = KeybindCount; i > 0; i--) { // Convert i to a KeyAction enum and get the corresponding keybind KeyAction action = (KeyAction)i; // Check if there is an entry for the action, not all of them will have entries if (!tempKeybinds.ContainsKey(action)) { continue; } DualKeyCombo actionKeybind = tempKeybinds[action]; KeybindMetadata actionMetadata = keybindManager.keyActionMetadata[action]; // Only add the action if it can be rebound if (!actionMetadata.Rebindable) { continue; } // Create the item and give it the same parent as the template (the scroll view content) GameObject newKeybindItem = Instantiate(KeybindItemTemplate, KeybindItemTemplate.transform.parent, false); // Add item to the list so we can destroy it again later KeybindItemList.Add(newKeybindItem); // Set the correct labels and onClick functions newKeybindItem.GetComponent <KeybindItemTemplate>().SetupKeybindItem(action, actionKeybind, actionMetadata); // Grab the index of the appropriate heading and put the keybind under it int headingIndex = KeybindHeadingDict[actionMetadata.Type].transform.GetSiblingIndex(); newKeybindItem.transform.SetSiblingIndex(headingIndex + 1); newKeybindItem.SetActive(true); } }
public void SetupKeybindItem(KeyAction action, DualKeyCombo keybind, KeybindMetadata metadata) { // Setup the action text and store it ActionText.text = metadata.Name; ItemAction = action; // Only activate the remove button if a KeyCombo is present PrimaryRemoveButton.gameObject.SetActive(keybind.PrimaryCombo != KeyCombo.None); PrimaryButton.GetComponentInChildren <Text>().text = keybind.PrimaryCombo.ToString(); PrimaryButton.onClick.AddListener(primary_onClick); PrimaryRemoveButton.onClick.AddListener(primaryRemove_onClick); // Setup the secondary buttons too SecondaryRemoveButton.gameObject.SetActive(keybind.SecondaryCombo != KeyCombo.None); SecondaryButton.GetComponentInChildren <Text>().text = keybind.SecondaryCombo.ToString(); SecondaryButton.onClick.AddListener(secondary_onClick); SecondaryRemoveButton.onClick.AddListener(secondaryRemove_onClick); }
/// <summary> /// Changes the keybind for an action. Must be run as a coroutine! /// </summary> /// <param name="selectedAction">The action to change the keybinding of</param> /// <param name="isPrimary">Is this a primary or a secondary keybinding?</param> public IEnumerator ChangeKeybind(KeyAction selectedAction, bool isPrimary) { KeyValuePair <KeyAction, DualKeyCombo> conflictingKVP; KeyCombo capturedKeyCombo = KeyCombo.None; bool isConflictPrimary = true; // Wait for the keycombo to be captured KeyCapturePanel.SetActive(true); UIManager.IsInputFocus = true; while (capturedKeyCombo == KeyCombo.None) { capturedKeyCombo = keybindManager.CaptureKeyCombo(); yield return(null); } KeyCapturePanel.SetActive(false); // If null stop the function (null is returned if Escape was captured) if (capturedKeyCombo == null) { Logger.Log("Captured Escape key, cancelling change", Category.Keybindings); UIManager.IsInputFocus = false; yield break; } Logger.Log("Captured key combo: " + capturedKeyCombo.ToString(), Category.Keybindings); conflictingKVP = tempKeybinds.CheckConflict(capturedKeyCombo, ref isConflictPrimary); KeyAction conflictingAction = conflictingKVP.Key; if (conflictingAction == KeyAction.None) { // No conflicts found so set the new keybind and refresh the view tempKeybinds.Set(selectedAction, capturedKeyCombo, isPrimary); keybindManager.SaveKeybinds(tempKeybinds); // Make sure the player can move around again UIManager.IsInputFocus = false; PopulateKeybindScrollView(); } // Check that the conflict isn't with itself, if it is just ignore it else if (conflictingAction != selectedAction) { // Get the metadata for the keybind KeybindMetadata conflictingKeybindMetadata = keybindManager.keyActionMetadata[conflictingAction]; // Check if the user wants to change the keybind modalPanelManager.Confirm( "Warning!\n\nThis combination is already being used by:\n" + conflictingKeybindMetadata.Name + "\nAre you sure you want to override it?", () => { // User confirms they want to change the keybind tempKeybinds.Set(selectedAction, capturedKeyCombo, isPrimary); tempKeybinds.Remove(conflictingAction, isConflictPrimary); keybindManager.SaveKeybinds(tempKeybinds); UIManager.IsInputFocus = false; PopulateKeybindScrollView(); }, "Yes", () => { UIManager.IsInputFocus = false; } ); } }