SteamVR_Input_EditorWindow.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.CodeDom;
  4. using Microsoft.CSharp;
  5. using System.IO;
  6. using System.CodeDom.Compiler;
  7. using System.Linq;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. using System.Linq.Expressions;
  11. using System;
  12. using UnityEditorInternal;
  13. using Valve.Newtonsoft.Json;
  14. namespace Valve.VR
  15. {
  16. #pragma warning disable 0219 // variable assigned but not used.
  17. public class SteamVR_Input_EditorWindow : EditorWindow
  18. {
  19. private static SteamVR_Input_EditorWindow instance;
  20. [MenuItem("Window/SteamVR Input")]
  21. public static void ShowWindow()
  22. {
  23. instance = GetWindow<SteamVR_Input_EditorWindow>(false, "SteamVR Input", true);
  24. }
  25. public static void ReopenWindow()
  26. {
  27. instance = GetOpenWindow();
  28. if (instance != null)
  29. {
  30. instance.ForceClose();
  31. System.Threading.Thread.Sleep(100);
  32. }
  33. ShowWindow();
  34. }
  35. public static void Refresh()
  36. {
  37. instance = GetOpenWindow();
  38. if (instance != null)
  39. {
  40. instance.selectedActionIndex = -1;
  41. instance.selectedAction = null;
  42. instance.selectedActionSet = -1;
  43. instance.selectedLocalizationIndex = -1;
  44. instance.scrollPosition = Vector2.zero;
  45. SteamVR_Input.InitializeFile(true);
  46. instance.InitializeLists();
  47. }
  48. }
  49. private bool forcingClose = false;
  50. public void ForceClose()
  51. {
  52. forcingClose = true;
  53. this.Close();
  54. }
  55. private const bool defaultOverwriteBuildOption = true;
  56. private const bool defaultDeleteUnusedOption = true;
  57. private static void InitializeEditorValues()
  58. {
  59. if (EditorPrefs.HasKey(SteamVR_Input_Generator.steamVRInputOverwriteBuildKey) == false)
  60. EditorPrefs.SetBool(SteamVR_Input_Generator.steamVRInputOverwriteBuildKey, defaultOverwriteBuildOption);
  61. if (EditorPrefs.HasKey(SteamVR_Input_Generator.steamVRInputDeleteUnusedKey) == false)
  62. EditorPrefs.SetBool(SteamVR_Input_Generator.steamVRInputDeleteUnusedKey, defaultDeleteUnusedOption);
  63. }
  64. private ReorderableList inList;
  65. private ReorderableList outList;
  66. private ReorderableList localizationList;
  67. private int selectedActionIndex = -1;
  68. private SteamVR_Input_ActionFile_Action selectedAction;
  69. private int selectedActionSet = -1;
  70. private int selectedLocalizationIndex = -1;
  71. private void InitializeLists()
  72. {
  73. if (selectedActionSet == -1)
  74. {
  75. inList = null;
  76. outList = null;
  77. return;
  78. }
  79. inList = new ReorderableList(SteamVR_Input.actionFile.action_sets[selectedActionSet].actionsInList, typeof(string), false, true, true, true);
  80. inList.onAddCallback += OnAddCallback;
  81. inList.onRemoveCallback += OnRemoveCallback;
  82. inList.onSelectCallback += OnSelectCallback;
  83. inList.drawHeaderCallback += DrawHeaderCallbackIn;
  84. outList = new ReorderableList(SteamVR_Input.actionFile.action_sets[selectedActionSet].actionsOutList, typeof(string), false, true, true, true);
  85. outList.onAddCallback += OnAddCallback;
  86. outList.onRemoveCallback += OnRemoveCallback;
  87. outList.onSelectCallback += OnSelectCallback;
  88. outList.drawHeaderCallback += DrawHeaderCallbackOut;
  89. }
  90. private void DrawHeaderCallbackIn(Rect rect)
  91. {
  92. DrawHeaderCallback(rect, "In");
  93. }
  94. private void DrawHeaderCallbackOut(Rect rect)
  95. {
  96. DrawHeaderCallback(rect, "Out");
  97. }
  98. private void DrawHeaderCallback(Rect rect, string name)
  99. {
  100. EditorGUI.LabelField(rect, name);
  101. }
  102. private void OnSelectCallback(ReorderableList list)
  103. {
  104. selectedActionIndex = list.index;
  105. if (selectedActionIndex != -1)
  106. {
  107. selectedAction = (SteamVR_Input_ActionFile_Action)list.list[selectedActionIndex];
  108. }
  109. if (inList == list)
  110. {
  111. outList.index = -1;
  112. }
  113. else if (outList == list)
  114. {
  115. inList.index = -1;
  116. }
  117. }
  118. private void OnRemoveCallback(ReorderableList list)
  119. {
  120. if (list.index == -1)
  121. return;
  122. list.list.RemoveAt(list.index);
  123. list.index = -1;
  124. OnSelectCallback(list);
  125. }
  126. private void OnAddCallback(ReorderableList list)
  127. {
  128. if (selectedActionSet == -1)
  129. {
  130. return;
  131. }
  132. SteamVR_Input_ActionFile_Action newAction = new SteamVR_Input_ActionFile_Action();
  133. list.list.Add(newAction);
  134. string direction = "";
  135. if (inList == list)
  136. {
  137. direction = "in";
  138. outList.index = -1;
  139. inList.index = inList.list.Count - 1;
  140. }
  141. else if (outList == list)
  142. {
  143. direction = "out";
  144. inList.index = -1;
  145. outList.index = outList.list.Count - 1;
  146. }
  147. newAction.name = SteamVR_Input_ActionFile_Action.CreateNewName(SteamVR_Input.actionFile.action_sets[selectedActionSet].shortName, direction);
  148. OnSelectCallback(list);
  149. }
  150. private void InitializeLocalizationArray()
  151. {
  152. localizationList = new ReorderableList(SteamVR_Input.actionFile.localizationHelperList, typeof(string), false, true, true, true);
  153. localizationList.onAddCallback += LocalizationOnAddCallback;
  154. localizationList.onRemoveCallback += LocalizationOnRemoveCallback;
  155. localizationList.onSelectCallback += LocalizationOnSelectCallback;
  156. localizationList.drawHeaderCallback += LocalizationDrawHeaderCallback;
  157. localizationList.drawElementCallback += LocalizationDrawElementCallback;
  158. }
  159. private void LocalizationDrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
  160. {
  161. SteamVR_Input_ActionFile_LocalizationItem item = ((List<SteamVR_Input_ActionFile_LocalizationItem>)localizationList.list)[index];
  162. if (localizationList.index == index)
  163. {
  164. item.language = EditorGUI.TextField(rect, item.language);
  165. }
  166. else
  167. {
  168. EditorGUI.LabelField(rect, item.language);
  169. }
  170. }
  171. private void LocalizationDrawHeaderCallback(Rect rect)
  172. {
  173. EditorGUI.LabelField(rect, "Languages");
  174. }
  175. private void LocalizationOnSelectCallback(ReorderableList list)
  176. {
  177. selectedLocalizationIndex = list.index;
  178. }
  179. private void LocalizationOnRemoveCallback(ReorderableList list)
  180. {
  181. List<SteamVR_Input_ActionFile_LocalizationItem> itemList = ((List<SteamVR_Input_ActionFile_LocalizationItem>)localizationList.list);
  182. itemList.RemoveAt(list.index);
  183. selectedLocalizationIndex = -1;
  184. }
  185. private void LocalizationOnAddCallback(ReorderableList list)
  186. {
  187. List<SteamVR_Input_ActionFile_LocalizationItem> itemList = ((List<SteamVR_Input_ActionFile_LocalizationItem>)localizationList.list);
  188. SteamVR_Input_ActionFile_LocalizationItem newLanguage = new SteamVR_Input_ActionFile_LocalizationItem("new-language");
  189. newLanguage.items.Add(selectedAction.name, selectedAction.name);
  190. itemList.Add(newLanguage);
  191. selectedLocalizationIndex = list.list.Count - 1;
  192. }
  193. private const string progressBarTitle = "SteamVR Input Generation";
  194. private const string progressBarTextKey = "SteamVR_Input_ProgressBarText";
  195. private const string progressBarAmountKey = "SteamVR_Input_ProgressBarAmount";
  196. private static string progressBarText = null;
  197. private static float progressBarAmount = 0;
  198. public static void SetProgressBarText(string newText, float newAmount)
  199. {
  200. EditorPrefs.SetString(progressBarTextKey, newText);
  201. EditorPrefs.SetFloat(progressBarAmountKey, newAmount);
  202. progressBarText = newText;
  203. progressBarAmount = newAmount;
  204. }
  205. public static void ClearProgressBar()
  206. {
  207. EditorPrefs.SetString(progressBarTextKey, "");
  208. EditorPrefs.SetFloat(progressBarAmountKey, 0);
  209. progressBarText = "";
  210. progressBarAmount = 0;
  211. EditorUtility.ClearProgressBar();
  212. }
  213. private static void UpdateProgressBarTextFromPrefs()
  214. {
  215. if (progressBarText == null)
  216. {
  217. if (EditorPrefs.HasKey(progressBarTextKey))
  218. {
  219. progressBarText = EditorPrefs.GetString(progressBarTextKey);
  220. progressBarAmount = EditorPrefs.GetFloat(progressBarAmountKey);
  221. }
  222. else
  223. {
  224. progressBarText = "";
  225. EditorPrefs.SetString(progressBarTextKey, progressBarText);
  226. }
  227. }
  228. }
  229. private Vector2 scrollPosition;
  230. private bool initialized = false;
  231. private void Initialize(bool force = false)
  232. {
  233. SteamVR_Input.InitializeFile(force, false);
  234. InitializeEditorValues();
  235. initialized = true;
  236. }
  237. private bool CopyOrClose()
  238. {
  239. bool copyExamples = UnityEditor.EditorUtility.DisplayDialog("Copy Examples", "It looks like your project is missing an actions.json. Would you like to use the example files?", "Yes", "No");
  240. if (copyExamples)
  241. {
  242. SteamVR_CopyExampleInputFiles.CopyFiles(true);
  243. System.Threading.Thread.Sleep(1000);
  244. bool initializeSuccess = SteamVR_Input.InitializeFile();
  245. return initializeSuccess;
  246. }
  247. else
  248. {
  249. SteamVR_Input.CreateEmptyActionsFile();
  250. System.Threading.Thread.Sleep(1000);
  251. bool initializeSuccess = SteamVR_Input.InitializeFile();
  252. return initializeSuccess;
  253. }
  254. //Debug.LogErrorFormat("<b>[SteamVR]</b> Actions file does not exist in project root: {0}. You'll need to create one for SteamVR to work.", SteamVR_Input.actionsFilePath);
  255. }
  256. private void CheckFileInitialized()
  257. {
  258. if (initialized == false)
  259. {
  260. Initialize();
  261. }
  262. if (SteamVR_Input.actionFile == null)
  263. {
  264. bool initializeSuccess = SteamVR_Input.InitializeFile(false, false);
  265. if (initializeSuccess == false)
  266. {
  267. bool copySuccess = CopyOrClose();
  268. if (copySuccess == false)
  269. {
  270. Close(); // close the window since they didn't initialize the file
  271. }
  272. }
  273. }
  274. }
  275. private void UpdateProgressBar()
  276. {
  277. UpdateProgressBarTextFromPrefs();
  278. if (string.IsNullOrEmpty(progressBarText) == false)
  279. {
  280. EditorUtility.DisplayProgressBar(progressBarTitle, progressBarText, progressBarAmount);
  281. }
  282. }
  283. private void CheckInitialized()
  284. {
  285. if (localizationList == null)
  286. InitializeLocalizationArray();
  287. if (selectedLocalizationIndex == -1 && localizationList.count > 0)
  288. selectedLocalizationIndex = 0;
  289. if (selectedActionSet == -1 && SteamVR_Input.actionFile.action_sets.Count() > 0)
  290. {
  291. selectedActionSet = 0;
  292. InitializeLists();
  293. }
  294. if (selectedActionSet != -1 && inList == null)
  295. InitializeLists();
  296. }
  297. public static bool IsOpen()
  298. {
  299. SteamVR_Input_EditorWindow[] windows = Resources.FindObjectsOfTypeAll<SteamVR_Input_EditorWindow>();
  300. if (windows != null && windows.Length > 0)
  301. {
  302. return true;
  303. }
  304. return false;
  305. }
  306. public static SteamVR_Input_EditorWindow GetOpenWindow()
  307. {
  308. SteamVR_Input_EditorWindow[] windows = Resources.FindObjectsOfTypeAll<SteamVR_Input_EditorWindow>();
  309. if (windows != null && windows.Length > 0)
  310. {
  311. return windows[0];
  312. }
  313. return null;
  314. }
  315. private GUIStyle headerLabelStyle = null;
  316. private void OnGUI()
  317. {
  318. if (headerLabelStyle == null)
  319. headerLabelStyle = new GUIStyle(EditorStyles.boldLabel);
  320. CheckFileInitialized();
  321. UpdateProgressBar();
  322. if (Application.isPlaying == false && (SteamVR_Input_Generator.IsGenerating() == true || string.IsNullOrEmpty(progressBarText) == false))
  323. {
  324. EditorGUI.LabelField(new Rect(0, 0, 200, 20), "Generating SteamVR Input...");
  325. bool cancel = GUI.Button(new Rect(50, 20, 100, 20), "Cancel");
  326. if (cancel)
  327. {
  328. SteamVR_Input_Generator.CancelGeneration();
  329. ClearProgressBar();
  330. }
  331. return;
  332. }
  333. #if UNITY_2017_1_OR_NEWER
  334. if (EditorApplication.isCompiling)
  335. {
  336. EditorGUI.LabelField(new Rect(0, 0, 100, 20), "Compiling...");
  337. return; //ongui gets more fussy after 2017
  338. }
  339. #endif
  340. CheckInitialized();
  341. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  342. EditorGUILayout.Space();
  343. DrawTop();
  344. EditorGUILayout.Space();
  345. DrawSets();
  346. EditorGUILayout.Space();
  347. EditorGUILayout.Space();
  348. EditorGUILayout.Space();
  349. EditorGUILayout.Space();
  350. if (selectedActionSet == -1)
  351. {
  352. DrawNoSetSelected();
  353. }
  354. else
  355. {
  356. EditorGUILayout.BeginHorizontal();
  357. EditorGUILayout.BeginVertical();
  358. DrawActions();
  359. EditorGUILayout.EndVertical();
  360. EditorGUILayout.Space();
  361. EditorGUILayout.Space();
  362. EditorGUILayout.BeginVertical();
  363. if (selectedActionIndex == -1)
  364. {
  365. DrawNoActionSelected();
  366. }
  367. else
  368. {
  369. DrawDetails();
  370. }
  371. EditorGUILayout.EndVertical();
  372. EditorGUILayout.EndHorizontal();
  373. EditorGUILayout.Space();
  374. EditorGUILayout.Space();
  375. EditorGUILayout.Space();
  376. DrawSave();
  377. }
  378. EditorGUILayout.EndScrollView();
  379. }
  380. private void DrawTop()
  381. {
  382. EditorGUILayout.BeginHorizontal();
  383. GUILayout.FlexibleSpace();
  384. DrawSettingsButton();
  385. EditorGUILayout.EndHorizontal();
  386. }
  387. private void DrawSettingsButton()
  388. {
  389. bool openSettings = GUILayout.Button("Advanced Settings");
  390. if (openSettings)
  391. {
  392. SteamVR_Input_SettingsEditor.ShowWindow();
  393. }
  394. }
  395. private void DrawNoSetSelected()
  396. {
  397. //EditorGUILayout.LabelField("No action set selected.");
  398. EditorGUILayout.LabelField("");
  399. }
  400. private void DrawNoActionSelected()
  401. {
  402. //EditorGUILayout.LabelField("No action selected.");
  403. EditorGUILayout.LabelField("");
  404. }
  405. private void DrawSave()
  406. {
  407. EditorGUILayout.BeginHorizontal();
  408. GUILayout.FlexibleSpace();
  409. bool save = GUILayout.Button("Save and generate");
  410. GUILayout.FlexibleSpace();
  411. bool open = GUILayout.Button("Open binding UI");
  412. GUILayout.FlexibleSpace();
  413. EditorGUILayout.EndHorizontal();
  414. if (save)
  415. {
  416. EditorApplication.delayCall += SaveFile;
  417. }
  418. if (open)
  419. {
  420. OpenControllerBindings();
  421. }
  422. }
  423. private void OpenControllerBindings()
  424. {
  425. if (HasBeenModified())
  426. {
  427. bool saveFirst = EditorUtility.DisplayDialog("Save?", "It looks like you've made changes without saving. Would you like to save before editing the bindings?.", "Save", "Open without saving");
  428. if (saveFirst)
  429. {
  430. SaveFile();
  431. }
  432. }
  433. SteamVR.ShowBindingsForEditor();
  434. }
  435. private bool HasBeenModified()
  436. {
  437. SteamVR_Input.actionFile.SaveHelperLists();
  438. return SteamVR_Input.HasFileInMemoryBeenModified();
  439. }
  440. private void OnDestroy()
  441. {
  442. if (forcingClose == false && HasBeenModified())
  443. {
  444. bool saveFirst = EditorUtility.DisplayDialog("Save?", "It looks like you've closed the input actions window without saving changes. Would you like to save first?", "Save", "Close");
  445. if (saveFirst)
  446. {
  447. SaveFile();
  448. }
  449. }
  450. }
  451. private void DrawActions()
  452. {
  453. EditorGUILayout.BeginFadeGroup(1);
  454. EditorGUILayout.LabelField("Actions", headerLabelStyle);
  455. EditorGUILayout.Space();
  456. if (inList != null)
  457. inList.DoLayoutList();
  458. EditorGUILayout.Space();
  459. if (inList != null)
  460. outList.DoLayoutList();
  461. EditorGUILayout.EndFadeGroup();
  462. }
  463. private void DrawDetails()
  464. {
  465. EditorGUILayout.BeginFadeGroup(1);
  466. EditorGUILayout.LabelField("Action Details", headerLabelStyle);
  467. EditorGUILayout.Space();
  468. EditorGUILayout.LabelField("Full Action Path:");
  469. if (selectedActionIndex != -1)
  470. {
  471. EditorGUILayout.LabelField(selectedAction.name);
  472. }
  473. EditorGUILayout.Space();
  474. EditorGUILayout.LabelField("Name:");
  475. if (selectedActionIndex != -1)
  476. {
  477. string newName = EditorGUILayout.TextField(selectedAction.shortName);
  478. if (newName != selectedAction.shortName)
  479. {
  480. selectedAction.name = selectedAction.path + newName;
  481. }
  482. }
  483. EditorGUILayout.Space();
  484. EditorGUILayout.LabelField("Type:");
  485. if (selectedActionIndex != -1)
  486. {
  487. bool directionIn = selectedAction.path.IndexOf("/in/", StringComparison.CurrentCultureIgnoreCase) != -1;
  488. string[] list;
  489. if (directionIn)
  490. list = SteamVR_Input_ActionFile_ActionTypes.listIn;
  491. else
  492. list = SteamVR_Input_ActionFile_ActionTypes.listOut;
  493. int selectedType = Array.IndexOf(list, selectedAction.type);
  494. int newSelectedType = EditorGUILayout.Popup(selectedType, list);
  495. if (selectedType == -1 && newSelectedType == -1)
  496. newSelectedType = 0;
  497. if (selectedType != newSelectedType && newSelectedType != -1)
  498. {
  499. selectedAction.type = list[newSelectedType];
  500. }
  501. if (selectedAction.type == SteamVR_Input_ActionFile_ActionTypes.skeleton)
  502. {
  503. string currentSkeletonPath = selectedAction.skeleton;
  504. if (string.IsNullOrEmpty(currentSkeletonPath) == false)
  505. currentSkeletonPath = currentSkeletonPath.Replace("/", "\\");
  506. int selectedSkeletonType = Array.IndexOf(SteamVR_Input_ActionFile_ActionTypes.listSkeletons, currentSkeletonPath);
  507. int newSelectedSkeletonType = EditorGUILayout.Popup(selectedSkeletonType, SteamVR_Input_ActionFile_ActionTypes.listSkeletons);
  508. if (selectedSkeletonType == -1)
  509. selectedSkeletonType = 0;
  510. if (selectedSkeletonType != newSelectedSkeletonType && newSelectedSkeletonType != -1)
  511. {
  512. selectedAction.skeleton = SteamVR_Input_ActionFile_ActionTypes.listSkeletons[newSelectedSkeletonType].Replace("\\", "/");
  513. }
  514. }
  515. }
  516. EditorGUILayout.Space();
  517. EditorGUILayout.LabelField("Required:");
  518. if (selectedActionIndex != -1)
  519. {
  520. int oldRequirement = (int)selectedAction.requirementEnum;
  521. int newRequirement = GUILayout.SelectionGrid(oldRequirement, SteamVR_Input_ActionFile_Action.requirementValues, 1, EditorStyles.radioButton);
  522. if (oldRequirement != newRequirement)
  523. {
  524. selectedAction.requirementEnum = (SteamVR_Input_ActionFile_Action_Requirements)newRequirement;
  525. }
  526. }
  527. EditorGUILayout.Space();
  528. EditorGUILayout.LabelField("Localization:");
  529. localizationList.DoLayoutList();
  530. EditorGUILayout.Space();
  531. EditorGUILayout.LabelField("Localized String:");
  532. if (selectedLocalizationIndex != -1)
  533. {
  534. Dictionary<string, string> localizationItems = SteamVR_Input.actionFile.localizationHelperList[selectedLocalizationIndex].items;
  535. string oldValue = "";
  536. if (localizationItems.ContainsKey(selectedAction.name))
  537. oldValue = localizationItems[selectedAction.name];
  538. string newValue = EditorGUILayout.TextField(oldValue);
  539. if (string.IsNullOrEmpty(newValue))
  540. {
  541. localizationItems.Remove(selectedAction.name);
  542. }
  543. else if (oldValue != newValue)
  544. {
  545. if (localizationItems.ContainsKey(selectedAction.name) == false)
  546. localizationItems.Add(selectedAction.name, newValue);
  547. else
  548. localizationItems[selectedAction.name] = newValue;
  549. }
  550. }
  551. EditorGUILayout.EndFadeGroup();
  552. }
  553. private void DrawSets()
  554. {
  555. EditorGUILayout.LabelField("Action Sets", headerLabelStyle);
  556. EditorGUILayout.BeginHorizontal();
  557. GUILayout.FlexibleSpace();
  558. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionFile.action_sets.Count; actionSetIndex++)
  559. {
  560. if (selectedActionSet == actionSetIndex)
  561. {
  562. EditorGUILayout.BeginVertical();
  563. string newName = GUILayout.TextField(SteamVR_Input.actionFile.action_sets[actionSetIndex].shortName);
  564. if (newName != SteamVR_Input.actionFile.action_sets[actionSetIndex].shortName)
  565. {
  566. string oldName = SteamVR_Input.actionFile.action_sets[actionSetIndex].name;
  567. foreach (var action in SteamVR_Input.actionFile.actions)
  568. {
  569. if (action.actionSet == oldName)
  570. action.SetNewActionSet(newName);
  571. }
  572. SteamVR_Input.actionFile.action_sets[actionSetIndex].SetNewShortName(newName);
  573. }
  574. EditorGUILayout.BeginHorizontal();
  575. int selectedUsage = -1;
  576. for (int valueIndex = 0; valueIndex < SteamVR_Input_ActionFile_ActionSet_Usages.listValues.Length; valueIndex++)
  577. {
  578. if (SteamVR_Input_ActionFile_ActionSet_Usages.listValues[valueIndex] == SteamVR_Input.actionFile.action_sets[actionSetIndex].usage)
  579. {
  580. selectedUsage = valueIndex;
  581. break;
  582. }
  583. }
  584. int wasUsage = selectedUsage;
  585. if (selectedUsage == -1)
  586. selectedUsage = 1;
  587. selectedUsage = EditorGUILayout.Popup(selectedUsage, SteamVR_Input_ActionFile_ActionSet_Usages.listDescriptions);
  588. if (wasUsage != selectedUsage)
  589. {
  590. SteamVR_Input.actionFile.action_sets[actionSetIndex].usage = SteamVR_Input_ActionFile_ActionSet_Usages.listValues[selectedUsage];
  591. }
  592. EditorGUILayout.Space();
  593. bool removeSet = GUILayout.Button("-");
  594. if (removeSet)
  595. {
  596. bool confirm = EditorUtility.DisplayDialog("Confirmation", "Are you sure you want to delete this action set and all of its actions?.", "Delete", "Cancel");
  597. if (confirm)
  598. {
  599. //todo: this doesn't work
  600. SteamVR_Input.actionFile.action_sets.RemoveAt(selectedActionSet);
  601. selectedActionSet = -1;
  602. selectedAction = null;
  603. InitializeLists();
  604. break;
  605. }
  606. }
  607. EditorGUILayout.Space();
  608. EditorGUILayout.EndHorizontal();
  609. EditorGUILayout.EndVertical();
  610. }
  611. else
  612. {
  613. bool pressedSet = GUILayout.Button(SteamVR_Input.actionFile.action_sets[actionSetIndex].shortName);
  614. if (pressedSet)
  615. {
  616. selectedActionSet = actionSetIndex;
  617. selectedActionIndex = -1;
  618. selectedAction = null;
  619. InitializeLists();
  620. }
  621. }
  622. if (actionSetIndex < SteamVR_Input.actionFile.action_sets.Count - 1)
  623. GUILayout.FlexibleSpace();
  624. }
  625. EditorGUILayout.Space();
  626. bool addSet = GUILayout.Button("+");
  627. if (addSet)
  628. {
  629. SteamVR_Input_ActionFile_ActionSet newActionSet = new SteamVR_Input_ActionFile_ActionSet();
  630. newActionSet.name = SteamVR_Input_ActionFile_ActionSet.CreateNewName();
  631. SteamVR_Input.actionFile.action_sets.Add(newActionSet);
  632. selectedActionSet = SteamVR_Input.actionFile.action_sets.Count - 1;
  633. selectedActionIndex = -1;
  634. selectedAction = null;
  635. InitializeLists();
  636. }
  637. GUILayout.FlexibleSpace();
  638. EditorGUILayout.EndHorizontal();
  639. }
  640. private static MemberInfo GetMemberInfo<TModel, TItem>(TModel model, Expression<Func<TModel, TItem>> expr)
  641. {
  642. return ((MemberExpression)expr.Body).Member;
  643. }
  644. private void SaveFile()
  645. {
  646. SteamVR_Input.actionFile.SaveHelperLists();
  647. SteamVR_Input.actionFile.Save(SteamVR_Input.GetActionsFilePath());
  648. SteamVR_Input_ActionManifest_Manager.CleanBindings(true);
  649. Debug.Log("<b>[SteamVR Input]</b> Saved actions manifest successfully.");
  650. SteamVR_Input_Generator.BeginGeneration();
  651. }
  652. private void SanitizeActionFile()
  653. {
  654. foreach (var action in SteamVR_Input.actionFile.actions)
  655. {
  656. if (action.type != SteamVR_Input_ActionFile_ActionTypes.skeleton)
  657. {
  658. if (string.IsNullOrEmpty(action.skeleton) == false)
  659. {
  660. action.skeleton = null; //todo: shouldn't have skeleton data for non skeleton types I think
  661. }
  662. }
  663. }
  664. }
  665. }
  666. public class SteamVR_Input_SettingsEditor : EditorWindow
  667. {
  668. public static void ShowWindow()
  669. {
  670. GetWindow<SteamVR_Input_SettingsEditor>(true, "SteamVR Input Settings", true);
  671. }
  672. private GUIStyle headerLabelStyle = null;
  673. private GUIStyle multiLineStyle = null;
  674. private GUIStyle smallMultiLineStyle = null;
  675. private void OnGUI()
  676. {
  677. if (headerLabelStyle == null)
  678. headerLabelStyle = new GUIStyle(EditorStyles.boldLabel);
  679. if (multiLineStyle == null)
  680. multiLineStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  681. if (smallMultiLineStyle == null)
  682. {
  683. smallMultiLineStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  684. smallMultiLineStyle.fontSize = EditorStyles.miniLabel.fontSize;
  685. smallMultiLineStyle.fontStyle = FontStyle.Italic;
  686. }
  687. SteamVR_Input.InitializeFile();
  688. EditorGUILayout.Space();
  689. EditorGUILayout.LabelField("Actions manifest", headerLabelStyle);
  690. EditorGUILayout.LabelField(string.Format("Path: {0}", SteamVR_Input.GetActionsFilePath(true)));
  691. DrawRefreshButton();
  692. EditorGUILayout.Space();
  693. EditorGUILayout.Space();
  694. EditorGUILayout.Space();
  695. EditorGUILayout.Space();
  696. DrawPartial();
  697. EditorGUILayout.Space();
  698. EditorGUILayout.Space();
  699. EditorGUILayout.Space();
  700. EditorGUILayout.Space();
  701. DrawOverwriteOption();
  702. DrawDeleteUnusedActions();
  703. EditorGUILayout.Space();
  704. EditorGUILayout.Space();
  705. EditorGUILayout.BeginHorizontal();
  706. GUILayout.FlexibleSpace();
  707. bool delete = GUILayout.Button("Delete generated input folder");
  708. GUILayout.FlexibleSpace();
  709. bool showSettings = GUILayout.Button("SteamVR Settings");
  710. GUILayout.FlexibleSpace();
  711. EditorGUILayout.EndHorizontal();
  712. if (delete)
  713. {
  714. bool confirm = EditorUtility.DisplayDialog("Confirmation", "Are you sure you want to delete the input code files? This may make your project unable to compile.", "Delete", "Cancel");
  715. if (confirm)
  716. SteamVR_Input_Generator.DeleteGeneratedFolder();
  717. }
  718. if (showSettings)
  719. {
  720. Selection.activeObject = SteamVR_Settings.instance;
  721. }
  722. }
  723. private void DrawRefreshButton()
  724. {
  725. EditorGUILayout.BeginHorizontal();
  726. bool refresh = GUILayout.Button("Refresh");
  727. GUILayout.FlexibleSpace();
  728. EditorGUILayout.EndHorizontal();
  729. if (refresh)
  730. {
  731. SteamVR_Input_EditorWindow.ReopenWindow();
  732. }
  733. }
  734. private void DrawPartial()
  735. {
  736. EditorGUILayout.LabelField("Partial bindings", headerLabelStyle);
  737. EditorGUILayout.LabelField("This is for SteamVR related asset packages. Here you can create a folder containing your current action manifest and associated bindings that will automatically query users to import into their project and add to their actions when loaded (as long as they have the SteamVR asset).", multiLineStyle);
  738. EditorGUILayout.LabelField("note: Please create action sets specific to your asset to avoid collisions.", smallMultiLineStyle);
  739. bool create = GUILayout.Button("Create");
  740. if (create)
  741. {
  742. SteamVR_Input_CreatePartial.ShowWindow();
  743. }
  744. }
  745. private void DrawOverwriteOption()
  746. {
  747. EditorGUILayout.BeginHorizontal();
  748. EditorGUILayout.LabelField("Overwrite actions and bindings json files during build");
  749. bool overwrite = EditorPrefs.GetBool(SteamVR_Input_Generator.steamVRInputOverwriteBuildKey);
  750. bool newOverwrite = EditorGUILayout.Toggle(overwrite);
  751. if (overwrite != newOverwrite)
  752. {
  753. EditorPrefs.SetBool(SteamVR_Input_Generator.steamVRInputOverwriteBuildKey, newOverwrite);
  754. }
  755. EditorGUILayout.EndHorizontal();
  756. }
  757. private void DrawDeleteUnusedActions()
  758. {
  759. EditorGUILayout.BeginHorizontal();
  760. EditorGUILayout.LabelField("Delete actions that are no longer in the action list during generation");
  761. bool deleteUnused = EditorPrefs.GetBool(SteamVR_Input_Generator.steamVRInputDeleteUnusedKey);
  762. bool newDeleteUnused = EditorGUILayout.Toggle(deleteUnused);
  763. if (deleteUnused != newDeleteUnused)
  764. {
  765. EditorPrefs.SetBool(SteamVR_Input_Generator.steamVRInputDeleteUnusedKey, newDeleteUnused);
  766. }
  767. EditorGUILayout.EndHorizontal();
  768. }
  769. }
  770. public class SteamVR_Input_CreatePartial : EditorWindow
  771. {
  772. public static void ShowWindow()
  773. {
  774. GetWindow<SteamVR_Input_CreatePartial>(true, "SteamVR Input Partial Bindings Creator", true);
  775. }
  776. private GUIStyle headerLabelStyle = null;
  777. private GUIStyle multiLineStyle = null;
  778. private GUIStyle smallMultiLineStyle = null;
  779. private string folderName = null;
  780. private int version = 1;
  781. private bool overwriteOldActions = true;
  782. private bool removeOldVersionActions = true;
  783. private void OnGUI()
  784. {
  785. if (headerLabelStyle == null)
  786. headerLabelStyle = new GUIStyle(EditorStyles.boldLabel);
  787. if (multiLineStyle == null)
  788. multiLineStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  789. if (smallMultiLineStyle == null)
  790. {
  791. smallMultiLineStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  792. smallMultiLineStyle.fontSize = EditorStyles.miniLabel.fontSize;
  793. smallMultiLineStyle.fontStyle = FontStyle.Italic;
  794. }
  795. if (folderName == null)
  796. folderName = "SteamVR_" + SteamVR.GenerateCleanProductName();
  797. SteamVR_Input.InitializeFile();
  798. EditorGUILayout.Space();
  799. EditorGUILayout.LabelField("Partial input bindings", headerLabelStyle);
  800. EditorGUILayout.LabelField("When you create a partial bindings folder you can include that anywhere inside your asset package folder and it will automatically be found and imported to the user's project on load.", multiLineStyle);
  801. EditorGUILayout.LabelField("note: You can rename the folder but do not rename any files inside.", smallMultiLineStyle);
  802. EditorGUILayout.Space();
  803. EditorGUILayout.Space();
  804. folderName = EditorGUILayout.TextField("Name", folderName);
  805. EditorGUILayout.Space();
  806. version = EditorGUILayout.IntField("Version", version);
  807. EditorGUILayout.LabelField("note: only whole numbers", smallMultiLineStyle);
  808. EditorGUILayout.Space();
  809. overwriteOldActions = EditorGUILayout.Toggle("Overwrite old actions", overwriteOldActions);
  810. EditorGUILayout.LabelField("If the person importing your asset package has a previous version of your package this determines whether or not to overwrite actions and bindings that have the same name.", smallMultiLineStyle);
  811. EditorGUILayout.Space();
  812. removeOldVersionActions = EditorGUILayout.Toggle("Remove unused actions from old versions", overwriteOldActions);
  813. EditorGUILayout.LabelField("If the person importing your asset package has a previous version of your package that has actions that are not used in the new version of your package this determines whether or not to delete those actions.", smallMultiLineStyle);
  814. EditorGUILayout.Space();
  815. EditorGUILayout.Space();
  816. bool create = GUILayout.Button("Create");
  817. if (create)
  818. {
  819. SteamVR_Input_ActionManifest_Manager.CreatePartial(folderName, version, overwriteOldActions, removeOldVersionActions);
  820. }
  821. }
  822. }
  823. }