Tanoda
pb_ToolDialog.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
3using System.Collections;
4using System.Collections.Generic;
5using GILES;
6using System.IO;
7using System.Linq;
9
10namespace GILES.Interface
11{
12 public class pb_ToolDialog : MonoBehaviour
13 {
15 public GameObject scrollContent;
16
19
21 public InputField directoryCrumbsField;
22
24 public InputField fileInputField;
25
27 public string currentDirectory;
28
31
34
37 public Callback<GameObject> OnSave;
38
41
42 public GameObject selectedGo = null;
43 public GameObject selectedLine = null;
44
48 public void AddOnSaveListener(Callback<GameObject> listener)
49 {
50 if(OnSave != null)
51 OnSave += listener;
52 else
53 OnSave = listener;
54 }
55
59 public void AddOnCancelListener(Callback listener)
60 {
61 if(OnCancel != null)
62 OnCancel += listener;
63 else
64 OnCancel = listener;
65 }
66
67 private bool mInitialized = false;
68
69 void Awake()
70 {
71 if(!mInitialized)
72 Initialize();
73 }
74
75 void Initialize()
76 {
77 mInitialized = true;
78
79 cancelButton.onClick.RemoveAllListeners();
80 saveButton.onClick.RemoveAllListeners();
81
82 cancelButton.onClick.AddListener( Cancel );
83 saveButton.onClick.AddListener( Save );
84 }
85
89 public void UpdateDirectoryContents(string selected = "")
90 {
91 var children = FindObjectsOfType<ToolGO>();
92
93 // hide the contents while working with them, otherwise you get flashes and artifacts
94 scrollContent.SetActive(false);
95
96 ClearScrollRect();
97
98 int i = 0;
99
100 for (int n = 0; n < children.Length; n++)
101 {
102 pb_SaveDialogButton button = GameObject.Instantiate(rowButtonPrefab);
103 button.SetDelegateAndPath(SetFile, children[n].name);
104 pb_GUIStyleApplier style = button.GetComponent<pb_GUIStyleApplier>();
105 style.style = i++ % 2 == 0 ? evenRowStyle : oddRowStyle;
106 style.ApplyStyle();
107
108 button.transform.SetParent(scrollContent.transform);
109 if (selected == children[n].name)
110 {
111 selectedLine = button.gameObject;
112 }
113 }
114
115 scrollContent.SetActive(true);
116 }
117
118 private void ClearScrollRect()
119 {
120 foreach(Transform t in scrollContent.transform)
121 pb_ObjectUtility.Destroy(t.gameObject);
122 }
123
124 private bool ValidDir(string dir)
125 {
126 return !string.IsNullOrEmpty(dir) && Directory.Exists(dir);
127 }
128
129 public void SetFile(string path)
130 {
131 fileInputField.text = Path.GetFileName(path);
132
133 var children = FindObjectsOfType<ToolGO>();
134 foreach (var go in children)
135 {
136 if (go.name == fileInputField.text || Macro.StripPath(go.name) == fileInputField.text)
137 {
138 selectedGo = go.gameObject;
139 return;
140 }
141 }
142 }
143
148 public void Cancel()
149 {
150 if(OnCancel != null)
151 OnCancel();
152
154 }
155
160 public void Save()
161 {
162 if(OnSave != null)
164 else
165 Debug.LogWarning("File dialog was dismissed by user but no callback is registered to perform the action!");
166
168 }
169 }
170}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void SetDelegateAndPath(Callback< string > del, string path)
void UpdateDirectoryContents(string selected="")
void AddOnCancelListener(Callback listener)
pb_GUIStyle oddRowStyle
pb_GUIStyle to apply to odd and even rows.
string currentDirectory
The directory currently being inspected.
void AddOnSaveListener(Callback< GameObject > listener)
GameObject scrollContent
Where to put current directory folder buttons.
Button saveButton
Save and cancel buttons. onClick delegates will automatically be added by this script.
Callback< GameObject > OnSave
pb_SaveDialogButton rowButtonPrefab
The prefab to populate scrollview contents with.
Callback OnCancel
Called if the user cancels this action.
InputField directoryCrumbsField
The input field that shows the directory path.
InputField fileInputField
The input field that allows user to type in file or folder name.
Definition: Macro.cs:12
static string StripPath(string value)
Definition: Macro.cs:180
delegate void Callback()