controlsMenu.tscript 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // =============================================================================
  2. // KEYBINDS MENU
  3. // =============================================================================
  4. function ControlsMenuDefaultsButton::onClick(%this)
  5. {
  6. //For this to work with module-style, we have to figure that somewhere, we'll set where our default keybind script is at.
  7. //This can be hardcoded in your actual project.
  8. //exec($KeybindPath);
  9. //ControlsMenu.reload();
  10. }
  11. function getMapDisplayName( %device, %action )
  12. {
  13. if ( %device $= "keyboard" )
  14. return( %action );
  15. else if ( strstr( %device, "mouse" ) != -1 )
  16. {
  17. // Substitute "mouse" for "button" in the action string:
  18. %pos = strstr( %action, "button" );
  19. if ( %pos != -1 )
  20. {
  21. %mods = getSubStr( %action, 0, %pos );
  22. %object = getSubStr( %action, %pos, 1000 );
  23. %instance = getSubStr( %object, strlen( "button" ), 1000 );
  24. return( %mods @ "mouse" @ ( %instance + 1 ) );
  25. }
  26. else
  27. error( "Mouse input object other than button passed to getDisplayMapName!" );
  28. }
  29. else if ( strstr( %device, "joystick" ) != -1 )
  30. {
  31. // Substitute "joystick" for "button" in the action string:
  32. %pos = strstr( %action, "button" );
  33. if ( %pos != -1 )
  34. {
  35. %mods = getSubStr( %action, 0, %pos );
  36. %object = getSubStr( %action, %pos, 1000 );
  37. %instance = getSubStr( %object, strlen( "button" ), 1000 );
  38. return( %mods @ "joystick" @ ( %instance + 1 ) );
  39. }
  40. else
  41. {
  42. %pos = strstr( %action, "pov" );
  43. if ( %pos != -1 )
  44. {
  45. %wordCount = getWordCount( %action );
  46. %mods = %wordCount > 1 ? getWords( %action, 0, %wordCount - 2 ) @ " " : "";
  47. %object = getWord( %action, %wordCount - 1 );
  48. switch$ ( %object )
  49. {
  50. case "upov": %object = "POV1 up";
  51. case "dpov": %object = "POV1 down";
  52. case "lpov": %object = "POV1 left";
  53. case "rpov": %object = "POV1 right";
  54. case "upov2": %object = "POV2 up";
  55. case "dpov2": %object = "POV2 down";
  56. case "lpov2": %object = "POV2 left";
  57. case "rpov2": %object = "POV2 right";
  58. default: %object = "";
  59. }
  60. return( %mods @ %object );
  61. }
  62. else
  63. error( "Unsupported Joystick input object passed to getDisplayMapName!" );
  64. }
  65. }
  66. else if ( strstr( %device, "gamepad" ) != -1 )
  67. {
  68. return %action;
  69. %pos = strstr( %action, "button" );
  70. if ( %pos != -1 )
  71. {
  72. %mods = getSubStr( %action, 0, %pos );
  73. %object = getSubStr( %action, %pos, 1000 );
  74. %instance = getSubStr( %object, strlen( "button" ), 1000 );
  75. return( %mods @ "joystick" @ ( %instance + 1 ) );
  76. }
  77. else
  78. {
  79. %pos = strstr( %action, "thumb" );
  80. if ( %pos != -1 )
  81. {
  82. //%instance = getSubStr( %action, strlen( "thumb" ), 1000 );
  83. //return( "thumb" @ ( %instance + 1 ) );
  84. return %action;
  85. }
  86. else
  87. error( "Unsupported gamepad input object passed to getDisplayMapName!" );
  88. }
  89. }
  90. return( "" );
  91. }
  92. function buildFullMapString( %index, %actionMap, %deviceType )
  93. {
  94. %name = $RemapName[%index];
  95. %cmd = $RemapCmd[%index];
  96. %temp = %actionMap.getBinding( %cmd );
  97. if ( %temp $= "" )
  98. return %name TAB "";
  99. %mapString = "";
  100. %count = getFieldCount( %temp );
  101. for ( %i = 0; %i < %count; %i += 2 )
  102. {
  103. if ( %mapString !$= "" )
  104. continue;
  105. //%mapString = %mapString @ ", ";
  106. %device = getField( %temp, %i + 0 );
  107. %object = getField( %temp, %i + 1 );
  108. if (startsWith(%device,"mouse"))
  109. %deviceType = "mouse";
  110. if(%deviceType !$= "" && !startsWith(%device, %deviceType))
  111. continue;
  112. %mapString = %mapString @ getMapDisplayName( %device, %object );
  113. }
  114. return %name TAB %mapString;
  115. }
  116. function fillRemapList()
  117. {
  118. %device = $remapListDevice;
  119. OptionsMenuSettingsList.clear();
  120. //build out our list of action maps
  121. %actionMapCount = ActionMapGroup.getCount();
  122. %actionMapList = "";
  123. for(%i=0; %i < %actionMapCount; %i++)
  124. {
  125. %actionMap = ActionMapGroup.getObject(%i);
  126. if(%actionMap == GlobalActionMap.getId())
  127. continue;
  128. %actionMapName = %actionMap.humanReadableName $= "" ? %actionMap.getName() : %actionMap.humanReadableName;
  129. if(%actionMapList $= "")
  130. %actionMapList = %actionMapName;
  131. else
  132. %actionMapList = %actionMapList TAB %actionMapName;
  133. }
  134. //If we didn't find any valid actionMaps, then just exit out
  135. if(%actionMapList $= "")
  136. return;
  137. if($activeRemapControlSet $= "")
  138. $activeRemapControlSet = getField(%actionMapList, 0);
  139. OptionsMenuSettingsList.addOptionRow("Control Set", "$activeRemapControlSet", %actionMapList, false, "controlSetChanged", true, "Which keybind control set to edit", $activeRemapControlSet);
  140. for ( %i = 0; %i < $RemapCount; %i++ )
  141. {
  142. if(%device !$= "" && %device !$= $RemapDevice[%i])
  143. continue;
  144. %actionMapName = $RemapActionMap[%i].humanReadableName $= "" ? $RemapActionMap[%i].getName() : $RemapActionMap[%i].humanReadableName;
  145. if($activeRemapControlSet !$= %actionMapName)
  146. continue;
  147. %keyMap = buildFullMapString( %i, $RemapActionMap[%i], %device );
  148. %description = $RemapDescription[%i];
  149. %buttonImageAsset = getButtonBitmap(%device, getField(%keyMap, 1));
  150. OptionsMenuSettingsList.addKeybindRow(getField(%keyMap, 0), %buttonImageAsset, "doKeyRemap", true, %description);
  151. }
  152. //OptionsMenuSettingsList.refresh();
  153. //OptionsMenu.addRow( %i, %this.buildFullMapString( %i ) );
  154. }
  155. function controlSetChanged()
  156. {
  157. $activeRemapControlSet = OptionsMenuSettingsList.getCurrentOption();
  158. fillRemapList();
  159. }
  160. function doKeyRemap( %rowIndex )
  161. {
  162. %rowIndex--; //Offset the rowIndex to account for controlset option
  163. %name = $RemapName[%rowIndex];
  164. RemapDlg-->OptRemapText.setValue( "Re-bind \"" @ %name @ "\" to..." );
  165. OptRemapInputCtrl.index = %rowIndex;
  166. Canvas.pushDialog( RemapDlg );
  167. //Let the options menu know
  168. %actionMap = $RemapActionMap[%rowIndex];
  169. OptionsMenu.onKeybindChanged(%actionMap, %name);
  170. }
  171. function ControlsMenuRebindButton::onClick(%this)
  172. {
  173. %name = $RemapName[%this.keybindIndex];
  174. RemapDlg-->OptRemapText.setValue( "Re-bind \"" @ %name @ "\" to..." );
  175. OptRemapInputCtrl.index = %this.keybindIndex;
  176. OptRemapInputCtrl.optionIndex = %this.optionIndex;
  177. Canvas.pushDialog( RemapDlg );
  178. }
  179. function OptRemapInputCtrl::onInputEvent( %this, %device, %action )
  180. {
  181. //error( "** onInputEvent called - device = " @ %device @ ", action = " @ %action @ " **" );
  182. Canvas.popDialog( RemapDlg );
  183. // Test for the reserved keystrokes:
  184. if ( %device $= "keyboard" )
  185. {
  186. // Cancel...
  187. if ( %action $= "escape" )
  188. {
  189. // Do nothing...
  190. return;
  191. }
  192. }
  193. %cmd = $RemapCmd[%this.index];
  194. %name = $RemapName[%this.index];
  195. %actionMap = $RemapActionMap[%this.index];
  196. // Grab the friendly display name for this action
  197. // which we'll use when prompting the user below.
  198. %mapName = getMapDisplayName( %device, %action );
  199. // Get the current command this action is mapped to.
  200. %prevMap = %actionMap.getCommand( %device, %action );
  201. //TODO: clear all existant keybinds to a command and then bind it so we only have a single one at all times
  202. unbindExtraActions( %cmd, %actionMap, 0 );
  203. unbindExtraActions( %cmd, %actionMap, 1 );
  204. // If nothing was mapped to the previous command
  205. // mapping then it's easy... just bind it.
  206. if ( %prevMap $= "" )
  207. {
  208. //unbindExtraActions( %cmd, %actionMap, 1 );
  209. %actionMap.bind( %device, %action, %cmd );
  210. fillRemapList();
  211. return;
  212. }
  213. // If the previous command is the same as the
  214. // current then they hit the same input as what
  215. // was already assigned.
  216. if ( %prevMap $= %cmd )
  217. {
  218. //unbindExtraActions( %cmd, %actionMap, 0 );
  219. %actionMap.bind( %device, %action, %cmd );
  220. fillRemapList();
  221. return;
  222. }
  223. // Look for the index of the previous mapping.
  224. %prevMapIndex = findRemapCmdIndex( %prevMap );
  225. // If we get a negative index then the previous
  226. // mapping was to an item that isn't included in
  227. // the mapping list... so we cannot unmap it.
  228. if ( %prevMapIndex == -1 )
  229. {
  230. MessageBoxOK( "Remap Failed", "\"" @ %mapName @ "\" is already bound to a non-remappable command!" );
  231. return;
  232. }
  233. // Setup the forced remapping callback command.
  234. %callback = "redoMapping(" @ %device @ ", " @ %actionMap @ ", \"" @ %action @ "\", \"" @
  235. %cmd @ "\", " @ %prevMapIndex @ ", " @ %this.index @ ");";
  236. // Warn that we're about to remove the old mapping and
  237. // replace it with another.
  238. %prevCmdName = $RemapName[%prevMapIndex];
  239. Canvas.pushDialog( RemapConfirmDlg );
  240. RemapConfirmationText.setText("\"" @ %mapName @ "\" is already bound to \""
  241. @ %prevCmdName @ "\"! Do you wish to replace this mapping?");
  242. RemapConfirmationYesButton.command = "redoMapping(" @ %device @ ", " @ %actionMap @ ", \"" @ %action @ "\", \"" @
  243. %cmd @ "\", " @ %prevMapIndex @ ", " @ %this.index @ "); Canvas.popDialog();";
  244. RemapConfirmationNoButton.command = "Canvas.popDialog();";
  245. /*MessageBoxYesNo( "Warning",
  246. "\"" @ %mapName @ "\" is already bound to \""
  247. @ %prevCmdName @ "\"!\nDo you wish to replace this mapping?",
  248. %callback, "" );*/
  249. }
  250. function findRemapCmdIndex( %command )
  251. {
  252. for ( %i = 0; %i < $RemapCount; %i++ )
  253. {
  254. if ( %command $= $RemapCmd[%i] )
  255. return( %i );
  256. }
  257. return( -1 );
  258. }
  259. /// This unbinds actions beyond %count associated to the
  260. /// particular actionMap %commmand.
  261. function unbindExtraActions( %command, %actionMap, %count )
  262. {
  263. %temp = %actionMap.getBinding( %command );
  264. if ( %temp $= "" )
  265. return;
  266. %count = getFieldCount( %temp ) - ( %count * 2 );
  267. for ( %i = 0; %i < %count; %i += 2 )
  268. {
  269. %device = getField( %temp, %i + 0 );
  270. %action = getField( %temp, %i + 1 );
  271. %actionMap.unbind( %device, %action );
  272. }
  273. }
  274. function redoMapping( %device, %actionMap, %action, %cmd, %oldIndex, %newIndex )
  275. {
  276. //%actionMap.bind( %device, %action, $RemapCmd[%newIndex] );
  277. %actionMap.bind( %device, %action, %cmd );
  278. fillRemapList();
  279. }