controlsMenu.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // =============================================================================
  2. // KEYBINDS MENU
  3. // =============================================================================
  4. $RemapCount = 0;
  5. $RemapName[$RemapCount] = "Forward";
  6. $RemapCmd[$RemapCount] = "moveforward";
  7. $RemapGroup[$RemapCount] = "Movement";
  8. $RemapCount++;
  9. $RemapName[$RemapCount] = "Backward";
  10. $RemapCmd[$RemapCount] = "movebackward";
  11. $RemapGroup[$RemapCount] = "Movement";
  12. $RemapCount++;
  13. $RemapName[$RemapCount] = "Strafe Left";
  14. $RemapCmd[$RemapCount] = "moveleft";
  15. $RemapGroup[$RemapCount] = "Movement";
  16. $RemapCount++;
  17. $RemapName[$RemapCount] = "Strafe Right";
  18. $RemapCmd[$RemapCount] = "moveright";
  19. $RemapGroup[$RemapCount] = "Movement";
  20. $RemapCount++;
  21. $RemapName[$RemapCount] = "Jump";
  22. $RemapCmd[$RemapCount] = "jump";
  23. $RemapGroup[$RemapCount] = "Movement";
  24. $RemapCount++;
  25. $RemapName[$RemapCount] = "Fire Weapon";
  26. $RemapCmd[$RemapCount] = "mouseFire";
  27. $RemapGroup[$RemapCount] = "Combat";
  28. $RemapCount++;
  29. $RemapName[$RemapCount] = "Adjust Zoom";
  30. $RemapCmd[$RemapCount] = "setZoomFov";
  31. $RemapGroup[$RemapCount] = "Combat";
  32. $RemapCount++;
  33. $RemapName[$RemapCount] = "Toggle Zoom";
  34. $RemapCmd[$RemapCount] = "toggleZoom";
  35. $RemapGroup[$RemapCount] = "Combat";
  36. $RemapCount++;
  37. $RemapName[$RemapCount] = "Free Look";
  38. $RemapCmd[$RemapCount] = "toggleFreeLook";
  39. $RemapGroup[$RemapCount] = "Miscellaneous";
  40. $RemapCount++;
  41. $RemapName[$RemapCount] = "Switch 1st/3rd";
  42. $RemapCmd[$RemapCount] = "toggleFirstPerson";
  43. $RemapGroup[$RemapCount] = "Miscellaneous";
  44. $RemapCount++;
  45. $RemapName[$RemapCount] = "Toggle Camera";
  46. $RemapCmd[$RemapCount] = "toggleCamera";
  47. $RemapGroup[$RemapCount] = "Miscellaneous";
  48. $RemapCount++;
  49. function ControlsMenu::onWake(%this)
  50. {
  51. ControlSetList.clear();
  52. ControlSetList.add( "Movement", "Movement" );
  53. ControlSetList.add( "Combat", "Combat" );
  54. ControlSetList.add( "Miscellaneous", "Miscellaneous" );
  55. ControlSetList.setSelected( "Movement", false );
  56. ControlsMenuOptionsArray.clear();
  57. ControlsMenu.loadGroupKeybinds("Movement");
  58. ControlsMenuOptionsArray.refresh();
  59. }
  60. function ControlSetList::onSelect( %this, %id, %text )
  61. {
  62. ControlsMenuOptionsArray.clear();
  63. if(%text $= "Movement")
  64. ControlsMenu.loadGroupKeybinds("Movement");
  65. else if(%text $= "Combat")
  66. ControlsMenu.loadGroupKeybinds("Combat");
  67. else if(%text $= "Miscellaneous")
  68. ControlsMenu.loadGroupKeybinds("Miscellaneous");
  69. ControlsMenuOptionsArray.refresh();
  70. }
  71. function ControlsMenuOKButton::onClick(%this)
  72. {
  73. // write out the control config into the keybinds.cs file
  74. %prefPath = getPrefpath();
  75. moveMap.save( %prefPath @ "/keybinds.cs" );
  76. OptionsMenu.backOut();
  77. }
  78. function ControlsMenuDefaultsButton::onClick(%this)
  79. {
  80. //For this to work with module-style, we have to figure that somewhere, we'll set where our default keybind script is at.
  81. //This can be hardcoded in your actual project.
  82. exec($KeybindPath);
  83. ControlsMenu.reload();
  84. }
  85. function ControlsMenu::loadGroupKeybinds(%this, %keybindGroup)
  86. {
  87. %optionIndex = 0;
  88. for(%i=0; %i < $RemapCount; %i++)
  89. {
  90. //find and add all the keybinds for the particular group we're looking at
  91. if($RemapGroup[%i] $= %keybindGroup)
  92. {
  93. %temp = %this.getKeybindString(%i);
  94. %option = %this.addKeybindOption();
  95. %option-->nameText.setText($RemapName[%i]);
  96. %option-->rebindButton.setText(%temp);
  97. %option-->rebindButton.keybindIndex = %i;
  98. %option-->rebindButton.optionIndex = %optionIndex;
  99. %optionIndex++;
  100. }
  101. }
  102. }
  103. function ControlsMenu::addKeybindOption(%this)
  104. {
  105. %tamlReader = new Taml();
  106. %graphicsOption = %tamlReader.read("data/ui/scripts/guis/controlsMenuSetting.taml");
  107. ControlsMenuOptionsArray.add(%graphicsOption);
  108. return %graphicsOption;
  109. }
  110. function ControlsMenu::getKeybindString(%this, %index )
  111. {
  112. %name = $RemapName[%index];
  113. %cmd = $RemapCmd[%index];
  114. %temp = moveMap.getBinding( %cmd );
  115. if ( %temp $= "" )
  116. return %name TAB "";
  117. %mapString = "";
  118. %count = getFieldCount( %temp );
  119. for ( %i = 0; %i < %count; %i += 2 )
  120. {
  121. %device = getField( %temp, %i + 0 );
  122. %object = getField( %temp, %i + 1 );
  123. %displayName = %this.getMapDisplayName( %device, %object );
  124. if(%displayName !$= "")
  125. {
  126. %tmpMapString = %this.getMapDisplayName( %device, %object );
  127. if(%mapString $= "")
  128. {
  129. %mapString = %tmpMapString;
  130. }
  131. else
  132. {
  133. if ( %tmpMapString !$= "")
  134. {
  135. %mapString = %mapString @ ", " @ %tmpMapString;
  136. }
  137. }
  138. }
  139. }
  140. return %mapString;
  141. }
  142. function ControlsMenu::redoMapping( %device, %action, %cmd, %oldIndex, %newIndex )
  143. {
  144. //%actionMap.bind( %device, %action, $RemapCmd[%newIndex] );
  145. moveMap.bind( %device, %action, %cmd );
  146. %remapList = %this-->OptRemapList;
  147. %remapList.setRowById( %oldIndex, buildFullMapString( %oldIndex ) );
  148. %remapList.setRowById( %newIndex, buildFullMapString( %newIndex ) );
  149. }
  150. function ControlsMenu::getMapDisplayName( %this, %device, %action )
  151. {
  152. if ( %device $= "keyboard" )
  153. return( %action );
  154. else if ( strstr( %device, "mouse" ) != -1 )
  155. {
  156. // Substitute "mouse" for "button" in the action string:
  157. %pos = strstr( %action, "button" );
  158. if ( %pos != -1 )
  159. {
  160. %mods = getSubStr( %action, 0, %pos );
  161. %object = getSubStr( %action, %pos, 1000 );
  162. %instance = getSubStr( %object, strlen( "button" ), 1000 );
  163. return( %mods @ "mouse" @ ( %instance + 1 ) );
  164. }
  165. else
  166. error( "Mouse input object other than button passed to getDisplayMapName!" );
  167. }
  168. else if ( strstr( %device, "joystick" ) != -1 )
  169. {
  170. // Substitute "joystick" for "button" in the action string:
  171. %pos = strstr( %action, "button" );
  172. if ( %pos != -1 )
  173. {
  174. %mods = getSubStr( %action, 0, %pos );
  175. %object = getSubStr( %action, %pos, 1000 );
  176. %instance = getSubStr( %object, strlen( "button" ), 1000 );
  177. return( %mods @ "joystick" @ ( %instance + 1 ) );
  178. }
  179. else
  180. {
  181. %pos = strstr( %action, "pov" );
  182. if ( %pos != -1 )
  183. {
  184. %wordCount = getWordCount( %action );
  185. %mods = %wordCount > 1 ? getWords( %action, 0, %wordCount - 2 ) @ " " : "";
  186. %object = getWord( %action, %wordCount - 1 );
  187. switch$ ( %object )
  188. {
  189. case "upov": %object = "POV1 up";
  190. case "dpov": %object = "POV1 down";
  191. case "lpov": %object = "POV1 left";
  192. case "rpov": %object = "POV1 right";
  193. case "upov2": %object = "POV2 up";
  194. case "dpov2": %object = "POV2 down";
  195. case "lpov2": %object = "POV2 left";
  196. case "rpov2": %object = "POV2 right";
  197. default: %object = "";
  198. }
  199. return( %mods @ %object );
  200. }
  201. else
  202. error( "Unsupported Joystick input object passed to getDisplayMapName!" );
  203. }
  204. }
  205. return( "" );
  206. }
  207. function ControlsMenu::buildFullMapString( %this, %index )
  208. {
  209. %name = $RemapName[%index];
  210. %cmd = $RemapCmd[%index];
  211. %temp = moveMap.getBinding( %cmd );
  212. if ( %temp $= "" )
  213. return %name TAB "";
  214. %mapString = "";
  215. %count = getFieldCount( %temp );
  216. for ( %i = 0; %i < %count; %i += 2 )
  217. {
  218. if ( %mapString !$= "" )
  219. %mapString = %mapString @ ", ";
  220. %device = getField( %temp, %i + 0 );
  221. %object = getField( %temp, %i + 1 );
  222. %mapString = %mapString @ %this.getMapDisplayName( %device, %object );
  223. }
  224. return %name TAB %mapString;
  225. }
  226. function ControlsMenu::fillRemapList( %this )
  227. {
  228. %remapList = %this-->OptRemapList;
  229. %remapList.clear();
  230. for ( %i = 0; %i < $RemapCount; %i++ )
  231. %remapList.addRow( %i, %this.buildFullMapString( %i ) );
  232. }
  233. function ControlsMenu::doRemap( %this )
  234. {
  235. %remapList = %this-->OptRemapList;
  236. %selId = %remapList.getSelectedId();
  237. %name = $RemapName[%selId];
  238. RemapDlg-->OptRemapText.setValue( "Re-bind \"" @ %name @ "\" to..." );
  239. OptRemapInputCtrl.index = %selId;
  240. Canvas.pushDialog( RemapDlg );
  241. }
  242. function ControlsMenuRebindButton::onClick(%this)
  243. {
  244. %name = $RemapName[%this.keybindIndex];
  245. RemapDlg-->OptRemapText.setValue( "Re-bind \"" @ %name @ "\" to..." );
  246. OptRemapInputCtrl.index = %this.keybindIndex;
  247. OptRemapInputCtrl.optionIndex = %this.optionIndex;
  248. Canvas.pushDialog( RemapDlg );
  249. }
  250. function OptRemapInputCtrl::onInputEvent( %this, %device, %action )
  251. {
  252. //error( "** onInputEvent called - device = " @ %device @ ", action = " @ %action @ " **" );
  253. Canvas.popDialog( RemapDlg );
  254. // Test for the reserved keystrokes:
  255. if ( %device $= "keyboard" )
  256. {
  257. // Cancel...
  258. if ( %action $= "escape" )
  259. {
  260. // Do nothing...
  261. return;
  262. }
  263. }
  264. %cmd = $RemapCmd[%this.index];
  265. %name = $RemapName[%this.index];
  266. // Grab the friendly display name for this action
  267. // which we'll use when prompting the user below.
  268. %mapName = ControlsMenu.getMapDisplayName( %device, %action );
  269. // Get the current command this action is mapped to.
  270. %prevMap = moveMap.getCommand( %device, %action );
  271. // If nothing was mapped to the previous command
  272. // mapping then it's easy... just bind it.
  273. if ( %prevMap $= "" )
  274. {
  275. ControlsMenu.unbindExtraActions( %cmd, 1 );
  276. moveMap.bind( %device, %action, %cmd );
  277. //ControlsMenu.reload();
  278. %newCommands = getField(ControlsMenu.buildFullMapString( %this.index ), 1);
  279. ControlsMenuOptionsArray.getObject(%this.optionIndex)-->rebindButton.setText(%newCommands);
  280. return;
  281. }
  282. // If the previous command is the same as the
  283. // current then they hit the same input as what
  284. // was already assigned.
  285. if ( %prevMap $= %cmd )
  286. {
  287. ControlsMenu.unbindExtraActions( %cmd, 0 );
  288. moveMap.bind( %device, %action, %cmd );
  289. //ControlsMenu.reload();
  290. %newCommands = getField(ControlsMenu.buildFullMapString( %this.index ), 1);
  291. ControlsMenuOptionsArray.getObject(%this.optionIndex)-->rebindButton.setText(%newCommands);
  292. return;
  293. }
  294. // Look for the index of the previous mapping.
  295. %prevMapIndex = ControlsMenu.findRemapCmdIndex( %prevMap );
  296. // If we get a negative index then the previous
  297. // mapping was to an item that isn't included in
  298. // the mapping list... so we cannot unmap it.
  299. if ( %prevMapIndex == -1 )
  300. {
  301. MessageBoxOK( "Remap Failed", "\"" @ %mapName @ "\" is already bound to a non-remappable command!" );
  302. return;
  303. }
  304. // Setup the forced remapping callback command.
  305. %callback = "redoMapping(" @ %device @ ", \"" @ %action @ "\", \"" @
  306. %cmd @ "\", " @ %prevMapIndex @ ", " @ %this.index @ ");";
  307. // Warn that we're about to remove the old mapping and
  308. // replace it with another.
  309. %prevCmdName = $RemapName[%prevMapIndex];
  310. Canvas.pushDialog( RemapConfirmDlg );
  311. RemapConfirmationText.setText("\"" @ %mapName @ "\" is already bound to \""
  312. @ %prevCmdName @ "\"! Do you wish to replace this mapping?");
  313. RemapConfirmationYesButton.command = "ControlsMenu.redoMapping(" @ %device @ ", \"" @ %action @ "\", \"" @
  314. %cmd @ "\", " @ %prevMapIndex @ ", " @ %this.index @ "); Canvas.popDialog();";
  315. RemapConfirmationNoButton.command = "Canvas.popDialog();";
  316. /*MessageBoxYesNo( "Warning",
  317. "\"" @ %mapName @ "\" is already bound to \""
  318. @ %prevCmdName @ "\"!\nDo you wish to replace this mapping?",
  319. %callback, "" );*/
  320. }
  321. function ControlsMenu::findRemapCmdIndex( %this, %command )
  322. {
  323. for ( %i = 0; %i < $RemapCount; %i++ )
  324. {
  325. if ( %command $= $RemapCmd[%i] )
  326. return( %i );
  327. }
  328. return( -1 );
  329. }
  330. /// This unbinds actions beyond %count associated to the
  331. /// particular moveMap %commmand.
  332. function ControlsMenu::unbindExtraActions( %this, %command, %count )
  333. {
  334. %temp = moveMap.getBinding( %command );
  335. if ( %temp $= "" )
  336. return;
  337. %count = getFieldCount( %temp ) - ( %count * 2 );
  338. for ( %i = 0; %i < %count; %i += 2 )
  339. {
  340. %device = getField( %temp, %i + 0 );
  341. %action = getField( %temp, %i + 1 );
  342. moveMap.unbind( %device, %action );
  343. }
  344. }
  345. function ControlsMenu::redoMapping( %this, %device, %action, %cmd, %oldIndex, %newIndex )
  346. {
  347. //%actionMap.bind( %device, %action, $RemapCmd[%newIndex] );
  348. moveMap.bind( %device, %action, %cmd );
  349. %remapList = %this-->OptRemapList;
  350. %remapList.setRowById( %oldIndex, %this.buildFullMapString( %oldIndex ) );
  351. %remapList.setRowById( %newIndex, %this.buildFullMapString( %newIndex ) );
  352. %this.changeSettingsPage();
  353. }