Skip to content

Commit

Permalink
- Added context menu that can be opened in 2 ways: a) right clicking …
Browse files Browse the repository at this point in the history
…a file on standalone platforms or b) clicking the More Options button located at the top-right corner. Context menu has the following buttons:

  - Select All (only when multi selection is enabled and More Options button is clicked)
  - Deselect All (only when more than 1 items are selected and More Options button is clicked)
  - New Folder (closed #25)
  - Delete (only when 1 or more items are selected)
  - Rename (only when 1 item is selected)
  • Loading branch information
yasirkula committed Oct 27, 2020
1 parent 13798a1 commit a234c6b
Show file tree
Hide file tree
Showing 15 changed files with 7,601 additions and 2,491 deletions.
9,244 changes: 6,816 additions & 2,428 deletions Plugins/SimpleFileBrowser/Resources/SimpleFileBrowserCanvas.prefab

Large diffs are not rendered by default.

387 changes: 326 additions & 61 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowserContextMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using UnityEngine;
using UnityEngine.UI;

namespace SimpleFileBrowser
{
public class FileBrowserContextMenu : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField]
private FileBrowser fileBrowser;

[SerializeField]
private RectTransform rectTransform;

[SerializeField]
private Button selectAllButton;
[SerializeField]
private Button deselectAllButton;
[SerializeField]
private Button deleteButton;
[SerializeField]
private Button renameButton;

[SerializeField]
private GameObject selectAllButtonSeparator;

[SerializeField]
private float minDistanceToEdges = 10f;
#pragma warning restore 0649

internal void Show( bool selectAllButtonVisible, bool deselectAllButtonVisible, bool deleteButtonVisible, bool renameButtonVisible, Vector2 position, bool isMoreOptionsMenu )
{
selectAllButton.gameObject.SetActive( selectAllButtonVisible );
deselectAllButton.gameObject.SetActive( deselectAllButtonVisible );
deleteButton.gameObject.SetActive( deleteButtonVisible );
renameButton.gameObject.SetActive( renameButtonVisible );
selectAllButtonSeparator.SetActive( !deselectAllButtonVisible );

rectTransform.anchoredPosition = position;
gameObject.SetActive( true );

if( isMoreOptionsMenu )
rectTransform.pivot = Vector2.one;
else
{
// Find the optimal pivot value
LayoutRebuilder.ForceRebuildLayoutImmediate( rectTransform );

Vector2 size = rectTransform.sizeDelta;
Vector2 canvasSize = fileBrowser.rectTransform.sizeDelta;

// Take canvas' Pivot into consideration
Vector2 positionOffset = canvasSize;
positionOffset.Scale( fileBrowser.rectTransform.pivot );
position += positionOffset;

// Try bottom-right corner first
Vector2 cornerPos = position + new Vector2( size.x + minDistanceToEdges, -size.y - minDistanceToEdges );
if( cornerPos.x <= canvasSize.x && cornerPos.y >= 0f )
rectTransform.pivot = new Vector2( 0f, 1f );
else
{
// Try bottom-left corner
cornerPos = position - new Vector2( size.x + minDistanceToEdges, size.y + minDistanceToEdges );
if( cornerPos.x >= 0f && cornerPos.y >= 0f )
rectTransform.pivot = Vector2.one;
else
{
// Try top-right corner
cornerPos = position + new Vector2( size.x + minDistanceToEdges, size.y + minDistanceToEdges );
if( cornerPos.x <= canvasSize.x && cornerPos.y <= canvasSize.y )
rectTransform.pivot = Vector2.zero;
else
{
// Use top-left corner
rectTransform.pivot = new Vector2( 1f, 0f );
}
}
}
}
}

public void Hide()
{
gameObject.SetActive( false );
}

public void OnSelectAllButtonClicked()
{
Hide();
fileBrowser.SelectAllFiles();
}

public void OnDeselectAllButtonClicked()
{
Hide();
fileBrowser.DeselectAllFiles();
}

public void OnCreateFolderButtonClicked()
{
Hide();
fileBrowser.CreateNewFolder();
}

public void OnDeleteButtonClicked()
{
Hide();
fileBrowser.DeleteSelectedFiles();
}

public void OnRenameButtonClicked()
{
Hide();
fileBrowser.RenameSelectedFile();
}
}
}
12 changes: 12 additions & 0 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowserContextMenu.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using UnityEngine;
using UnityEngine.UI;

namespace SimpleFileBrowser
{
public class FileBrowserDeleteConfirmationPanel : MonoBehaviour
{
public delegate void OnDeletionConfirmed();

#pragma warning disable 0649
[SerializeField]
private GameObject[] deletedItems;

[SerializeField]
private Image[] deletedItemIcons;

[SerializeField]
private Text[] deletedItemNames;

[SerializeField]
private GameObject deletedItemsRest;

[SerializeField]
private Text deletedItemsRestLabel;

[SerializeField]
private RectTransform yesButtonTransform;

[SerializeField]
private RectTransform noButtonTransform;

[SerializeField]
private float narrowScreenWidth = 380f;
#pragma warning restore 0649

private OnDeletionConfirmed onDeletionConfirmed;

internal void Show( Sprite[] icons, string[] filenames, OnDeletionConfirmed onDeletionConfirmed )
{
this.onDeletionConfirmed = onDeletionConfirmed;

for( int i = 0; i < deletedItems.Length; i++ )
deletedItems[i].SetActive( i < icons.Length );

for( int i = 0; i < deletedItems.Length && i < icons.Length; i++ )
{
deletedItemIcons[i].sprite = icons[i];
deletedItemNames[i].text = filenames[i];
}

if( icons.Length > deletedItems.Length )
{
deletedItemsRestLabel.text = string.Concat( "...and ", ( icons.Length - deletedItems.Length ).ToString(), " other" );
deletedItemsRest.SetActive( true );
}
else
deletedItemsRest.SetActive( false );

gameObject.SetActive( true );
}

// Handles responsive user interface
internal void OnCanvasDimensionsChanged( Vector2 size )
{
if( size.x >= narrowScreenWidth )
{
yesButtonTransform.anchorMin = new Vector2( 0.5f, 0f );
yesButtonTransform.anchorMax = new Vector2( 0.75f, 1f );
noButtonTransform.anchorMin = new Vector2( 0.75f, 0f );
}
else
{
yesButtonTransform.anchorMin = Vector2.zero;
yesButtonTransform.anchorMax = new Vector2( 0.5f, 1f );
noButtonTransform.anchorMin = new Vector2( 0.5f, 0f );
}
}

#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_WSA_10_0
private void LateUpdate()
{
// Handle keyboard shortcuts
if( Input.GetKeyDown( KeyCode.Return ) || Input.GetKeyDown( KeyCode.KeypadEnter ) )
YesButtonClicked();

if( Input.GetKeyDown( KeyCode.Escape ) )
NoButtonClicked();
}
#endif

public void YesButtonClicked()
{
gameObject.SetActive( false );

if( onDeletionConfirmed != null )
onDeletionConfirmed();
}

public void NoButtonClicked()
{
gameObject.SetActive( false );
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowserItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ private void Update()
#region Pointer Events
public void OnPointerClick( PointerEventData eventData )
{
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_WSA_10_0
if( eventData.button == PointerEventData.InputButton.Middle )
return;
else if( eventData.button == PointerEventData.InputButton.Right )
{
// First, select the item
if( !isSelected )
{
prevClickTime = 0f;
fileBrowser.OnItemSelected( this, false );
}

// Then, show the context menu
fileBrowser.OnContextMenuTriggered();
return;
}
#endif

if( Time.realtimeSinceStartup - prevClickTime < DOUBLE_CLICK_TIME )
{
prevClickTime = 0f;
Expand All @@ -102,11 +120,21 @@ public void OnPointerClick( PointerEventData eventData )

public void OnPointerDown( PointerEventData eventData )
{
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_WSA_10_0
if( eventData.button != PointerEventData.InputButton.Left )
return;
#endif

pressTime = Time.realtimeSinceStartup;
}

public void OnPointerUp( PointerEventData eventData )
{
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_WSA_10_0
if( eventData.button != PointerEventData.InputButton.Left )
return;
#endif

if( pressTime == Mathf.Infinity )
{
// We have activated MultiSelectionToggleSelectionMode with this press, processing the click would result in
Expand Down
59 changes: 59 additions & 0 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowserRenamedItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace SimpleFileBrowser
{
public class FileBrowserRenamedItem : MonoBehaviour
{
public delegate void OnRenameCompleted( string filename );

#pragma warning disable 0649
[SerializeField]
private Image background;

[SerializeField]
private Image icon;

[SerializeField]
private InputField nameInputField;
#pragma warning restore 0649

private OnRenameCompleted onRenameCompleted;

public void Show( string initialFilename, Color backgroundColor, Sprite icon, OnRenameCompleted onRenameCompleted )
{
background.color = backgroundColor;
this.icon.sprite = icon;
this.onRenameCompleted = onRenameCompleted;

transform.SetAsLastSibling();
gameObject.SetActive( true );

nameInputField.text = initialFilename;
nameInputField.ActivateInputField();
}

#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_WSA_10_0
private void LateUpdate()
{
// Don't allow scrolling with mouse wheel while renaming a file or creating a folder
if( Input.mouseScrollDelta.y != 0f )
nameInputField.DeactivateInputField();
}
#endif

public void OnInputFieldEndEdit( string filename )
{
gameObject.SetActive( false );

// If we don't deselect the InputField manually, FileBrowser's keyboard shortcuts
// no longer work until user clicks on a UI element and thus, deselects the InputField
if( !EventSystem.current.alreadySelecting && EventSystem.current.currentSelectedGameObject == nameInputField.gameObject )
EventSystem.current.SetSelectedGameObject( null );

if( onRenameCompleted != null )
onRenameCompleted( filename );
}
}
}
12 changes: 12 additions & 0 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowserRenamedItem.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Plugins/SimpleFileBrowser/Scripts/NonDrawingGraphic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using UnityEngine;
using UnityEngine.UI;

namespace SimpleFileBrowser
{
// Credit: http://answers.unity.com/answers/1157876/view.html
[RequireComponent( typeof( CanvasRenderer ) )]
public class NonDrawingGraphic : Graphic
{
public override void SetMaterialDirty() { return; }
public override void SetVerticesDirty() { return; }

protected override void OnPopulateMesh( VertexHelper vh )
{
vh.Clear();
return;
}
}
}
Loading

0 comments on commit a234c6b

Please sign in to comment.