private void ForceSelect(FileListing listing, bool addToSelectedList = false, bool setAsSelected = true) { // Used mainly in the script, while OnClick_SelectFile is used as the OnClick event for the listings... if (setAsSelected) // We don't always want the last one to be selected, such as in shift-click selection { curSelectedFileListing = listing; curSelectedFileListing.GetComponent <Image>().color = Color.green; } if (addToSelectedList) // You could probably do without this as in all method calls the 2nd { // bool parameter is set to true selectionList.Add(listing); listing.GetComponent <Image>().color = Color.green; } else { ClearSelectionList(); if (curSelectedFileListing != null) { curSelectedFileListing.GetComponent <Image>().color = Color.white; } if (curSelectedFileListing) { listing.GetComponent <Image>().color = Color.green; } } }
private void CreateFileListing(string fullPath) // Creates a file listing and places it in the content, it now exists in existingListings { FileListing fileListingClone = Instantiate(settings.fileListingPrefab, settings.fileScrollContent) as FileListing; Button cloneButton = fileListingClone.GetComponent <Button>(); Text cloneText = cloneButton.GetComponentInChildren <Text>(); fileListingClone.FullPath = fullPath; fileListingClone.FileName = Path.GetFileName(fullPath); existingListings.Add(fileListingClone); if (showFullPath) { cloneText.text = fileListingClone.FullPath; } else { cloneText.text = fileListingClone.FileName; } cloneButton.onClick.AddListener(() => OnClick_SelectFile(fileListingClone)); // You could probably do the above in the FileListing script as well, this just felt easier for me }
public void OnClick_SelectFile(FileListing listing) { if (Input.GetKey(KeyCode.LeftControl)) // Clicking on a file while holding ctrl, adds it to selection unless it already is in { // otherwise deselects it if (selectionList.Contains(listing)) { if (curSelectedFileListing) { curSelectedFileListing.GetComponent <Image>().color = Color.white; if (curSelectedFileListing.GetInstanceID() == listing.GetInstanceID()) { curSelectedFileListing = null; } } selectionList.Remove(listing); } else { curSelectedFileListing = listing; selectionList.Add(listing); curSelectedFileListing.GetComponent <Image>().color = Color.green; } } else if (Input.GetKey(KeyCode.LeftShift)) // // Clicking on a file while holding lshift, selects it from A to B { ClearSelectionList(); int start; int end; if (curSelectedFileListing == null) { start = 0; end = existingListings.IndexOf(listing); } else { int ind1 = existingListings.IndexOf(curSelectedFileListing); int ind2 = existingListings.IndexOf(listing); start = ind1 > ind2 ? ind2 : ind1; end = ind1 > ind2 ? ind1 : ind2; } for (int i = start; i <= end; i++) { if (existingListings[i] == null) { #if UNITY_EDITOR Debug.LogWarning("isnull"); #endif continue; } Debug.Log(existingListings[i]); ForceSelect(existingListings[i], true, false); // No matter where you shift-click, it always starts from the currently selected // listing, or 0 if none selected } } else { ClearSelectionList(); // Just clicking clears the list and selects the listing by itself if (curSelectedFileListing != null) { curSelectedFileListing.GetComponent <Image>().color = Color.white; } curSelectedFileListing = listing; selectionList.Add(listing); curSelectedFileListing.GetComponent <Image>().color = Color.green; } }