SteamVR_Input_EditorWindow.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. EditorApplication.delayCall += SaveFile;
  246. return initializeSuccess;
  247. }
  248. else
  249. {
  250. SteamVR_Input.CreateEmptyActionsFile();
  251. System.Threading.Thread.Sleep(1000);
  252. bool initializeSuccess = SteamVR_Input.InitializeFile();
  253. return initializeSuccess;
  254. }
  255. //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);
  256. }
  257. private void CheckFileInitialized()
  258. {
  259. if (initialized == false)
  260. {
  261. Initialize();
  262. }
  263. if (SteamVR_Input.actionFile == null)
  264. {
  265. bool initializeSuccess = SteamVR_Input.InitializeFile(false, false);
  266. if (initializeSuccess == false)
  267. {
  268. bool copySuccess = CopyOrClose();
  269. if (copySuccess == false)
  270. {
  271. Close(); // close the window since they didn't initialize the file
  272. }
  273. }
  274. }
  275. }
  276. private void UpdateProgressBar()
  277. {
  278. UpdateProgressBarTextFromPrefs();
  279. if (string.IsNullOrEmpty(progressBarText) == false)
  280. {
  281. EditorUtility.DisplayProgressBar(progressBarTitle, progressBarText, progressBarAmount);
  282. }
  283. }
  284. private void CheckInitialized()
  285. {
  286. if (localizationList == null)
  287. InitializeLocalizationArray();
  288. if (selectedLocalizationIndex == -1 && localizationList.count > 0)
  289. selectedLocalizationIndex = 0;
  290. if (selectedActionSet == -1 && SteamVR_Input.actionFile.action_sets.Count() > 0)
  291. {
  292. selectedActionSet = 0;
  293. InitializeLists();
  294. }
  295. if (selectedActionSet != -1 && inList == null)
  296. InitializeLists();
  297. }
  298. public static bool IsOpen()
  299. {
  300. SteamVR_Input_EditorWindow[] windows = Resources.FindObjectsOfTypeAll<SteamVR_Input_EditorWindow>();
  301. if (windows != null && windows.Length > 0)
  302. {
  303. return true;
  304. }
  305. return false;
  306. }
  307. public static SteamVR_Input_EditorWindow GetOpenWindow()
  308. {
  309. SteamVR_Input_EditorWindow[] windows = Resources.FindObjectsOfTypeAll<SteamVR_Input_EditorWindow>();
  310. if (windows != null && windows.Length > 0)
  311. {
  312. return windows[0];
  313. }
  314. return null;
  315. }
  316. private GUIStyle headerLabelStyle = null;
  317. private void OnGUI()
  318. {
  319. if (headerLabelStyle == null)
  320. headerLabelStyle = new GUIStyle(EditorStyles.boldLabel);
  321. CheckFileInitialized();
  322. UpdateProgressBar();
  323. if (Application.isPlaying == false && (SteamVR_Input_Generator.IsGenerating() == true || string.IsNullOrEmpty(progressBarText) == false))
  324. {
  325. EditorGUI.LabelField(new Rect(0, 0, 200, 20), "Generating SteamVR Input...");
  326. bool cancel = GUI.Button(new Rect(50, 20, 100, 20), "Cancel");
  327. if (cancel)
  328. {
  329. SteamVR_Input_Generator.CancelGeneration();
  330. ClearProgressBar();
  331. }
  332. return;
  333. }
  334. #if UNITY_2017_1_OR_NEWER
  335. if (EditorApplication.isCompiling)
  336. {
  337. EditorGUI.LabelField(new Rect(0, 0, 100, 20), "Compiling...");
  338. return; //ongui gets more fussy after 2017
  339. }
  340. #endif
  341. CheckInitialized();
  342. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  343. EditorGUILayout.Space();
  344. DrawTop();
  345. EditorGUILayout.Space();
  346. DrawSets();
  347. EditorGUILayout.Space();
  348. EditorGUILayout.Space();
  349. EditorGUILayout.Space();
  350. EditorGUILayout.Space();
  351. if (selectedActionSet == -1)
  352. {
  353. DrawNoSetSelected();
  354. }
  355. else
  356. {
  357. EditorGUILayout.BeginHorizontal();
  358. EditorGUILayout.BeginVertical();
  359. DrawActions();
  360. EditorGUILayout.EndVertical();
  361. EditorGUILayout.Space();
  362. EditorGUILayout.Space();
  363. EditorGUILayout.BeginVertical();
  364. if (selectedActionIndex == -1)
  365. {
  366. DrawNoActionSelected();
  367. }
  368. else
  369. {
  370. DrawDetails();
  371. }
  372. EditorGUILayout.EndVertical();
  373. EditorGUILayout.EndHorizontal();
  374. EditorGUILayout.Space();
  375. EditorGUILayout.Space();
  376. EditorGUILayout.Space();
  377. DrawSave();
  378. }
  379. EditorGUILayout.EndScrollView();
  380. }
  381. private void DrawTop()
  382. {
  383. EditorGUILayout.BeginHorizontal();
  384. GUILayout.FlexibleSpace();
  385. DrawSettingsButton();
  386. EditorGUILayout.EndHorizontal();
  387. }
  388. private void DrawSettingsButton()
  389. {
  390. bool openSettings = GUILayout.Button("Advanced Settings");
  391. if (openSettings)
  392. {
  393. SteamVR_Input_SettingsEditor.ShowWindow();
  394. }
  395. }
  396. private void DrawNoSetSelected()
  397. {
  398. //EditorGUILayout.LabelField("No action set selected.");
  399. EditorGUILayout.LabelField("");
  400. }
  401. private void DrawNoActionSelected()
  402. {
  403. //EditorGUILayout.LabelField("No action selected.");
  404. EditorGUILayout.LabelField("");
  405. }
  406. private void DrawSave()
  407. {
  408. EditorGUILayout.BeginHorizontal();
  409. GUILayout.FlexibleSpace();
  410. bool save = GUILayout.Button("Save and generate");
  411. GUILayout.FlexibleSpace();
  412. bool open = GUILayout.Button("Open binding UI");
  413. GUILayout.FlexibleSpace();
  414. EditorGUILayout.EndHorizontal();
  415. if (save)
  416. {
  417. EditorApplication.delayCall += SaveFile;
  418. }
  419. if (open)
  420. {
  421. OpenControllerBindings();
  422. }
  423. }
  424. private void OpenControllerBindings()
  425. {
  426. if (HasBeenModified())
  427. {
  428. 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");
  429. if (saveFirst)
  430. {
  431. SaveFile();
  432. }
  433. }
  434. SteamVR.ShowBindingsForEditor();
  435. }
  436. private bool HasBeenModified()
  437. {
  438. SteamVR_Input.actionFile.SaveHelperLists();
  439. return SteamVR_Input.HasFileInMemoryBeenModified();
  440. }
  441. private void OnDestroy()
  442. {
  443. if (forcingClose == false && HasBeenModified())
  444. {
  445. 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");
  446. if (saveFirst)
  447. {
  448. SaveFile();
  449. }
  450. }
  451. }
  452. private void DrawActions()
  453. {
  454. EditorGUILayout.BeginFadeGroup(1);
  455. EditorGUILayout.LabelField("Actions", headerLabelStyle);
  456. EditorGUILayout.Space();
  457. if (inList != null)
  458. inList.DoLayoutList();
  459. EditorGUILayout.Space();
  460. if (inList != null)
  461. outList.DoLayoutList();
  462. EditorGUILayout.EndFadeGroup();
  463. }
  464. private void DrawDetails()
  465. {
  466. EditorGUILayout.BeginFadeGroup(1);
  467. EditorGUILayout.LabelField("Action Details", headerLabelStyle);
  468. EditorGUILayout.Space();
  469. EditorGUILayout.LabelField("Full Action Path:");
  470. if (selectedActionIndex != -1)
  471. {
  472. EditorGUILayout.LabelField(selectedAction.name);
  473. }
  474. EditorGUILayout.Space();
  475. EditorGUILayout.LabelField("Name:");
  476. if (selectedActionIndex != -1)
  477. {
  478. string newName = EditorGUILayout.TextField(selectedAction.shortName);
  479. if (newName != selectedAction.shortName)
  480. {
  481. selectedAction.name = selectedAction.path + newName;
  482. }
  483. }
  484. EditorGUILayout.Space();
  485. EditorGUILayout.LabelField("Type:");
  486. if (selectedActionIndex != -1)
  487. {
  488. bool directionIn = selectedAction.path.IndexOf("/in/", StringComparison.CurrentCultureIgnoreCase) != -1;
  489. string[] list;
  490. if (directionIn)
  491. list = SteamVR_Input_ActionFile_ActionTypes.listIn;
  492. else
  493. list = SteamVR_Input_ActionFile_ActionTypes.listOut;
  494. int selectedType = Array.IndexOf(list, selectedAction.type);
  495. int newSelectedType = EditorGUILayout.Popup(selectedType, list);
  496. if (selectedType == -1 && newSelectedType == -1)
  497. newSelectedType = 0;
  498. if (selectedType != newSelectedType && newSelectedType != -1)
  499. {
  500. selectedAction.type = list[newSelectedType];
  501. }
  502. if (selectedAction.type == SteamVR_Input_ActionFile_ActionTypes.skeleton)
  503. {
  504. string currentSkeletonPath = selectedAction.skeleton;
  505. if (string.IsNullOrEmpty(currentSkeletonPath) == false)
  506. currentSkeletonPath = currentSkeletonPath.Replace("/", "\\");
  507. int selectedSkeletonType = Array.IndexOf(SteamVR_Input_ActionFile_ActionTypes.listSkeletons, currentSkeletonPath);
  508. int newSelectedSkeletonType = EditorGUILayout.Popup(selectedSkeletonType, SteamVR_Input_ActionFile_ActionTypes.listSkeletons);
  509. if (selectedSkeletonType == -1)
  510. selectedSkeletonType = 0;
  511. if (selectedSkeletonType != newSelectedSkeletonType && newSelectedSkeletonType != -1)
  512. {
  513. selectedAction.skeleton = SteamVR_Input_ActionFile_ActionTypes.listSkeletons[newSelectedSkeletonType].Replace("\\", "/");
  514. }
  515. }
  516. }
  517. EditorGUILayout.Space();
  518. EditorGUILayout.LabelField("Required:");
  519. if (selectedActionIndex != -1)
  520. {
  521. int oldRequirement = (int)selectedAction.requirementEnum;
  522. int newRequirement = GUILayout.SelectionGrid(oldRequirement, SteamVR_Input_ActionFile_Action.requirementValues, 1, EditorStyles.radioButton);
  523. if (oldRequirement != newRequirement)
  524. {
  525. selectedAction.requirementEnum = (SteamVR_Input_ActionFile_Action_Requirements)newRequirement;
  526. }
  527. }
  528. EditorGUILayout.Space();
  529. EditorGUILayout.LabelField("Localization:");
  530. localizationList.DoLayoutList();
  531. EditorGUILayout.Space();
  532. EditorGUILayout.LabelField("Localized String:");
  533. if (selectedLocalizationIndex != -1)
  534. {
  535. Dictionary<string, string> localizationItems = SteamVR_Input.actionFile.localizationHelperList[selectedLocalizationIndex].items;
  536. string oldValue = "";
  537. if (localizationItems.ContainsKey(selectedAction.name))
  538. oldValue = localizationItems[selectedAction.name];
  539. string newValue = EditorGUILayout.TextField(oldValue);
  540. if (string.IsNullOrEmpty(newValue))
  541. {
  542. localizationItems.Remove(selectedAction.name);
  543. }
  544. else if (oldValue != newValue)
  545. {
  546. if (localizationItems.ContainsKey(selectedAction.name) == false)
  547. localizationItems.Add(selectedAction.name, newValue);
  548. else
  549. localizationItems[selectedAction.name] = newValue;
  550. }
  551. }
  552. EditorGUILayout.EndFadeGroup();
  553. }
  554. private Vector2 setScrollPosition;
  555. private void DrawSets()
  556. {
  557. EditorGUILayout.LabelField("Action Sets", headerLabelStyle);
  558. setScrollPosition = EditorGUILayout.BeginScrollView(setScrollPosition);
  559. EditorGUILayout.BeginHorizontal();
  560. GUILayout.FlexibleSpace();
  561. for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionFile.action_sets.Count; actionSetIndex++)
  562. {
  563. if (selectedActionSet == actionSetIndex)
  564. {
  565. EditorGUILayout.BeginVertical();
  566. string newName = GUILayout.TextField(SteamVR_Input.actionFile.action_sets[actionSetIndex].shortName);
  567. if (newName != SteamVR_Input.actionFile.action_sets[actionSetIndex].shortName)
  568. {
  569. string oldName = SteamVR_Input.actionFile.action_sets[actionSetIndex].name;
  570. foreach (var action in SteamVR_Input.actionFile.actions)
  571. {
  572. if (action.actionSet == oldName)
  573. action.SetNewActionSet(newName);
  574. }
  575. SteamVR_Input.actionFile.action_sets[actionSetIndex].SetNewShortName(newName);
  576. }
  577. EditorGUILayout.BeginHorizontal();
  578. int selectedUsage = -1;
  579. for (int valueIndex = 0; valueIndex < SteamVR_Input_ActionFile_ActionSet_Usages.listValues.Length; valueIndex++)
  580. {
  581. if (SteamVR_Input_ActionFile_ActionSet_Usages.listValues[valueIndex] == SteamVR_Input.actionFile.action_sets[actionSetIndex].usage)
  582. {
  583. selectedUsage = valueIndex;
  584. break;
  585. }
  586. }
  587. int wasUsage = selectedUsage;
  588. if (selectedUsage == -1)
  589. selectedUsage = 1;
  590. selectedUsage = EditorGUILayout.Popup(selectedUsage, SteamVR_Input_ActionFile_ActionSet_Usages.listDescriptions);
  591. if (wasUsage != selectedUsage)
  592. {
  593. SteamVR_Input.actionFile.action_sets[actionSetIndex].usage = SteamVR_Input_ActionFile_ActionSet_Usages.listValues[selectedUsage];
  594. }
  595. EditorGUILayout.Space();
  596. bool removeSet = GUILayout.Button("-");
  597. if (removeSet)
  598. {
  599. bool confirm = EditorUtility.DisplayDialog("Confirmation", "Are you sure you want to delete this action set and all of its actions?.", "Delete", "Cancel");
  600. if (confirm)
  601. {
  602. //todo: this doesn't work
  603. SteamVR_Input.actionFile.action_sets.RemoveAt(selectedActionSet);
  604. selectedActionSet = -1;
  605. selectedAction = null;
  606. InitializeLists();
  607. break;
  608. }
  609. }
  610. EditorGUILayout.Space();
  611. EditorGUILayout.EndHorizontal();
  612. EditorGUILayout.EndVertical();
  613. }
  614. else
  615. {
  616. bool pressedSet = GUILayout.Button(SteamVR_Input.actionFile.action_sets[actionSetIndex].shortName);
  617. if (pressedSet)
  618. {
  619. selectedActionSet = actionSetIndex;
  620. selectedActionIndex = -1;
  621. selectedAction = null;
  622. InitializeLists();
  623. }
  624. }
  625. if (actionSetIndex < SteamVR_Input.actionFile.action_sets.Count - 1)
  626. GUILayout.FlexibleSpace();
  627. }
  628. EditorGUILayout.Space();
  629. bool addSet = GUILayout.Button("+");
  630. if (addSet)
  631. {
  632. SteamVR_Input_ActionFile_ActionSet newActionSet = new SteamVR_Input_ActionFile_ActionSet();
  633. newActionSet.name = SteamVR_Input_ActionFile_ActionSet.CreateNewName();
  634. SteamVR_Input.actionFile.action_sets.Add(newActionSet);
  635. selectedActionSet = SteamVR_Input.actionFile.action_sets.Count - 1;
  636. selectedActionIndex = -1;
  637. selectedAction = null;
  638. InitializeLists();
  639. }
  640. GUILayout.FlexibleSpace();
  641. EditorGUILayout.EndHorizontal();
  642. EditorGUILayout.EndScrollView();
  643. }
  644. private static MemberInfo GetMemberInfo<TModel, TItem>(TModel model, Expression<Func<TModel, TItem>> expr)
  645. {
  646. return ((MemberExpression)expr.Body).Member;
  647. }
  648. private void SaveFile()
  649. {
  650. SteamVR_Input.actionFile.SaveHelperLists();
  651. SteamVR_Input.actionFile.Save(SteamVR_Input.GetActionsFilePath());
  652. SteamVR_Input_ActionManifest_Manager.CleanBindings(true);
  653. Debug.Log("<b>[SteamVR Input]</b> Saved actions manifest successfully.");
  654. SteamVR_Input_Generator.BeginGeneration();
  655. }
  656. private void SanitizeActionFile()
  657. {
  658. foreach (var action in SteamVR_Input.actionFile.actions)
  659. {
  660. if (action.type != SteamVR_Input_ActionFile_ActionTypes.skeleton)
  661. {
  662. if (string.IsNullOrEmpty(action.skeleton) == false)
  663. {
  664. action.skeleton = null; //todo: shouldn't have skeleton data for non skeleton types I think
  665. }
  666. }
  667. }
  668. }
  669. }
  670. public class SteamVR_Input_SettingsEditor : EditorWindow
  671. {
  672. public static void ShowWindow()
  673. {
  674. GetWindow<SteamVR_Input_SettingsEditor>(true, "SteamVR Input Settings", true);
  675. }
  676. private GUIStyle headerLabelStyle = null;
  677. private GUIStyle multiLineStyle = null;
  678. private GUIStyle smallMultiLineStyle = null;
  679. private void OnGUI()
  680. {
  681. if (headerLabelStyle == null)
  682. headerLabelStyle = new GUIStyle(EditorStyles.boldLabel);
  683. if (multiLineStyle == null)
  684. multiLineStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  685. if (smallMultiLineStyle == null)
  686. {
  687. smallMultiLineStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  688. smallMultiLineStyle.fontSize = EditorStyles.miniLabel.fontSize;
  689. smallMultiLineStyle.fontStyle = FontStyle.Italic;
  690. }
  691. SteamVR_Input.InitializeFile();
  692. EditorGUILayout.Space();
  693. EditorGUILayout.LabelField("Actions manifest", headerLabelStyle);
  694. EditorGUILayout.LabelField(string.Format("Path: {0}", SteamVR_Input.GetActionsFilePath(true)));
  695. DrawRefreshButton();
  696. EditorGUILayout.Space();
  697. EditorGUILayout.Space();
  698. EditorGUILayout.Space();
  699. EditorGUILayout.Space();
  700. DrawPartial();
  701. EditorGUILayout.Space();
  702. EditorGUILayout.Space();
  703. EditorGUILayout.Space();
  704. EditorGUILayout.Space();
  705. DrawOverwriteOption();
  706. DrawDeleteUnusedActions();
  707. EditorGUILayout.Space();
  708. EditorGUILayout.Space();
  709. EditorGUILayout.BeginHorizontal();
  710. GUILayout.FlexibleSpace();
  711. bool delete = GUILayout.Button("Delete generated input folder");
  712. GUILayout.FlexibleSpace();
  713. bool showSettings = GUILayout.Button("SteamVR Settings");
  714. GUILayout.FlexibleSpace();
  715. EditorGUILayout.EndHorizontal();
  716. if (delete)
  717. {
  718. 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");
  719. if (confirm)
  720. SteamVR_Input_Generator.DeleteGeneratedFolder();
  721. }
  722. if (showSettings)
  723. {
  724. Selection.activeObject = SteamVR_Settings.instance;
  725. }
  726. }
  727. private void DrawRefreshButton()
  728. {
  729. EditorGUILayout.BeginHorizontal();
  730. bool refresh = GUILayout.Button("Refresh");
  731. GUILayout.FlexibleSpace();
  732. EditorGUILayout.EndHorizontal();
  733. if (refresh)
  734. {
  735. SteamVR_Input_EditorWindow.ReopenWindow();
  736. }
  737. }
  738. private void DrawPartial()
  739. {
  740. EditorGUILayout.LabelField("Partial bindings", headerLabelStyle);
  741. 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);
  742. EditorGUILayout.LabelField("note: Please create action sets specific to your asset to avoid collisions.", smallMultiLineStyle);
  743. bool create = GUILayout.Button("Create");
  744. if (create)
  745. {
  746. SteamVR_Input_CreatePartial.ShowWindow();
  747. }
  748. }
  749. private void DrawOverwriteOption()
  750. {
  751. EditorGUILayout.BeginHorizontal();
  752. EditorGUILayout.LabelField("Overwrite actions and bindings json files during build");
  753. bool overwrite = EditorPrefs.GetBool(SteamVR_Input_Generator.steamVRInputOverwriteBuildKey);
  754. bool newOverwrite = EditorGUILayout.Toggle(overwrite);
  755. if (overwrite != newOverwrite)
  756. {
  757. EditorPrefs.SetBool(SteamVR_Input_Generator.steamVRInputOverwriteBuildKey, newOverwrite);
  758. }
  759. EditorGUILayout.EndHorizontal();
  760. }
  761. private void DrawDeleteUnusedActions()
  762. {
  763. EditorGUILayout.BeginHorizontal();
  764. EditorGUILayout.LabelField("Delete actions that are no longer in the action list during generation");
  765. bool deleteUnused = EditorPrefs.GetBool(SteamVR_Input_Generator.steamVRInputDeleteUnusedKey);
  766. bool newDeleteUnused = EditorGUILayout.Toggle(deleteUnused);
  767. if (deleteUnused != newDeleteUnused)
  768. {
  769. EditorPrefs.SetBool(SteamVR_Input_Generator.steamVRInputDeleteUnusedKey, newDeleteUnused);
  770. }
  771. EditorGUILayout.EndHorizontal();
  772. }
  773. }
  774. public class SteamVR_Input_CreatePartial : EditorWindow
  775. {
  776. public static void ShowWindow()
  777. {
  778. GetWindow<SteamVR_Input_CreatePartial>(true, "SteamVR Input Partial Bindings Creator", true);
  779. }
  780. private GUIStyle headerLabelStyle = null;
  781. private GUIStyle multiLineStyle = null;
  782. private GUIStyle smallMultiLineStyle = null;
  783. private string folderName = null;
  784. private int version = 1;
  785. private bool overwriteOldActions = true;
  786. private bool removeOldVersionActions = true;
  787. private void OnGUI()
  788. {
  789. if (headerLabelStyle == null)
  790. headerLabelStyle = new GUIStyle(EditorStyles.boldLabel);
  791. if (multiLineStyle == null)
  792. multiLineStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  793. if (smallMultiLineStyle == null)
  794. {
  795. smallMultiLineStyle = new GUIStyle(EditorStyles.wordWrappedLabel);
  796. smallMultiLineStyle.fontSize = EditorStyles.miniLabel.fontSize;
  797. smallMultiLineStyle.fontStyle = FontStyle.Italic;
  798. }
  799. if (folderName == null)
  800. folderName = "SteamVR_" + SteamVR.GenerateCleanProductName();
  801. SteamVR_Input.InitializeFile();
  802. EditorGUILayout.Space();
  803. EditorGUILayout.LabelField("Partial input bindings", headerLabelStyle);
  804. 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);
  805. EditorGUILayout.LabelField("note: You can rename the folder but do not rename any files inside.", smallMultiLineStyle);
  806. EditorGUILayout.Space();
  807. EditorGUILayout.Space();
  808. folderName = EditorGUILayout.TextField("Name", folderName);
  809. EditorGUILayout.Space();
  810. version = EditorGUILayout.IntField("Version", version);
  811. EditorGUILayout.LabelField("note: only whole numbers", smallMultiLineStyle);
  812. EditorGUILayout.Space();
  813. overwriteOldActions = EditorGUILayout.Toggle("Overwrite old actions", overwriteOldActions);
  814. 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);
  815. EditorGUILayout.Space();
  816. removeOldVersionActions = EditorGUILayout.Toggle("Remove unused actions from old versions", overwriteOldActions);
  817. 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);
  818. EditorGUILayout.Space();
  819. EditorGUILayout.Space();
  820. bool create = GUILayout.Button("Create");
  821. if (create)
  822. {
  823. SteamVR_Input_ActionManifest_Manager.CreatePartial(folderName, version, overwriteOldActions, removeOldVersionActions);
  824. }
  825. }
  826. }
  827. }