controlsMenu.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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(%deviceType !$= "" && !startsWith(%device, %deviceType))
  109. continue;
  110. %mapString = %mapString @ getMapDisplayName( %device, %object );
  111. }
  112. return %name TAB %mapString;
  113. }
  114. function fillRemapList()
  115. {
  116. %device = $remapListDevice;
  117. OptionsMenuSettingsList.clearRows();
  118. //build out our list of action maps
  119. %actionMapCount = ActionMapGroup.getCount();
  120. %actionMapList = "";
  121. for(%i=0; %i < %actionMapCount; %i++)
  122. {
  123. %actionMap = ActionMapGroup.getObject(%i);
  124. if(%actionMap == GlobalActionMap.getId())
  125. continue;
  126. %actionMapName = %actionMap.humanReadableName $= "" ? %actionMap.getName() : %actionMap.humanReadableName;
  127. if(%actionMapList $= "")
  128. %actionMapList = %actionMapName;
  129. else
  130. %actionMapList = %actionMapList TAB %actionMapName;
  131. }
  132. //If we didn't find any valid actionMaps, then just exit out
  133. if(%actionMapList $= "")
  134. return;
  135. if($activeRemapControlSet $= "")
  136. $activeRemapControlSet = getField(%actionMapList, 0);
  137. OptionsMenuSettingsList.addOptionRow("Control Set", %actionMapList, false, "controlSetChanged", -1, -30, true, "Which keybind control set to edit", $activeRemapControlSet);
  138. for ( %i = 0; %i < $RemapCount; %i++ )
  139. {
  140. if(%device !$= "" && %device !$= $RemapDevice[%i])
  141. continue;
  142. %actionMapName = $RemapActionMap[%i].humanReadableName $= "" ? $RemapActionMap[%i].getName() : $RemapActionMap[%i].humanReadableName;
  143. if($activeRemapControlSet !$= %actionMapName)
  144. continue;
  145. %keyMap = buildFullMapString( %i, $RemapActionMap[%i], %device );
  146. %description = $RemapDescription[%i];
  147. OptionsMenuSettingsList.addKeybindRow(getField(%keyMap, 0), getButtonBitmap(%device, getField(%keyMap, 1)), "doKeyRemap", -1, -15, true, %description);
  148. }
  149. OptionsMenuSettingsList.refresh();
  150. //OptionsMenu.addRow( %i, %this.buildFullMapString( %i ) );
  151. }
  152. function controlSetChanged()
  153. {
  154. $activeRemapControlSet = OptionsMenuSettingsList.getCurrentOption(0);
  155. fillRemapList();
  156. }
  157. function doKeyRemap( %rowIndex )
  158. {
  159. %rowIndex--; //Offset the rowIndex to account for controlset option
  160. %name = $RemapName[%rowIndex];
  161. RemapDlg-->OptRemapText.setValue( "Re-bind \"" @ %name @ "\" to..." );
  162. OptRemapInputCtrl.index = %rowIndex;
  163. Canvas.pushDialog( RemapDlg );
  164. }
  165. function ControlsMenuRebindButton::onClick(%this)
  166. {
  167. %name = $RemapName[%this.keybindIndex];
  168. RemapDlg-->OptRemapText.setValue( "Re-bind \"" @ %name @ "\" to..." );
  169. OptRemapInputCtrl.index = %this.keybindIndex;
  170. OptRemapInputCtrl.optionIndex = %this.optionIndex;
  171. Canvas.pushDialog( RemapDlg );
  172. }
  173. function OptRemapInputCtrl::onInputEvent( %this, %device, %action )
  174. {
  175. //error( "** onInputEvent called - device = " @ %device @ ", action = " @ %action @ " **" );
  176. Canvas.popDialog( RemapDlg );
  177. // Test for the reserved keystrokes:
  178. if ( %device $= "keyboard" )
  179. {
  180. // Cancel...
  181. if ( %action $= "escape" )
  182. {
  183. // Do nothing...
  184. return;
  185. }
  186. }
  187. %cmd = $RemapCmd[%this.index];
  188. %name = $RemapName[%this.index];
  189. %actionMap = $RemapActionMap[%this.index];
  190. // Grab the friendly display name for this action
  191. // which we'll use when prompting the user below.
  192. %mapName = getMapDisplayName( %device, %action );
  193. // Get the current command this action is mapped to.
  194. %prevMap = %actionMap.getCommand( %device, %action );
  195. //TODO: clear all existant keybinds to a command and then bind it so we only have a single one at all times
  196. unbindExtraActions( %cmd, %actionMap, 0 );
  197. unbindExtraActions( %cmd, %actionMap, 1 );
  198. // If nothing was mapped to the previous command
  199. // mapping then it's easy... just bind it.
  200. if ( %prevMap $= "" )
  201. {
  202. //unbindExtraActions( %cmd, %actionMap, 1 );
  203. %actionMap.bind( %device, %action, %cmd );
  204. fillRemapList();
  205. return;
  206. }
  207. // If the previous command is the same as the
  208. // current then they hit the same input as what
  209. // was already assigned.
  210. if ( %prevMap $= %cmd )
  211. {
  212. //unbindExtraActions( %cmd, %actionMap, 0 );
  213. %actionMap.bind( %device, %action, %cmd );
  214. fillRemapList();
  215. return;
  216. }
  217. // Look for the index of the previous mapping.
  218. %prevMapIndex = findRemapCmdIndex( %prevMap );
  219. // If we get a negative index then the previous
  220. // mapping was to an item that isn't included in
  221. // the mapping list... so we cannot unmap it.
  222. if ( %prevMapIndex == -1 )
  223. {
  224. MessageBoxOK( "Remap Failed", "\"" @ %mapName @ "\" is already bound to a non-remappable command!" );
  225. return;
  226. }
  227. // Setup the forced remapping callback command.
  228. %callback = "redoMapping(" @ %device @ ", " @ %actionMap @ ", \"" @ %action @ "\", \"" @
  229. %cmd @ "\", " @ %prevMapIndex @ ", " @ %this.index @ ");";
  230. // Warn that we're about to remove the old mapping and
  231. // replace it with another.
  232. %prevCmdName = $RemapName[%prevMapIndex];
  233. Canvas.pushDialog( RemapConfirmDlg );
  234. RemapConfirmationText.setText("\"" @ %mapName @ "\" is already bound to \""
  235. @ %prevCmdName @ "\"! Do you wish to replace this mapping?");
  236. RemapConfirmationYesButton.command = "redoMapping(" @ %device @ ", " @ %actionMap @ ", \"" @ %action @ "\", \"" @
  237. %cmd @ "\", " @ %prevMapIndex @ ", " @ %this.index @ "); Canvas.popDialog();";
  238. RemapConfirmationNoButton.command = "Canvas.popDialog();";
  239. /*MessageBoxYesNo( "Warning",
  240. "\"" @ %mapName @ "\" is already bound to \""
  241. @ %prevCmdName @ "\"!\nDo you wish to replace this mapping?",
  242. %callback, "" );*/
  243. }
  244. function findRemapCmdIndex( %command )
  245. {
  246. for ( %i = 0; %i < $RemapCount; %i++ )
  247. {
  248. if ( %command $= $RemapCmd[%i] )
  249. return( %i );
  250. }
  251. return( -1 );
  252. }
  253. /// This unbinds actions beyond %count associated to the
  254. /// particular actionMap %commmand.
  255. function unbindExtraActions( %command, %actionMap, %count )
  256. {
  257. %temp = %actionMap.getBinding( %command );
  258. if ( %temp $= "" )
  259. return;
  260. %count = getFieldCount( %temp ) - ( %count * 2 );
  261. for ( %i = 0; %i < %count; %i += 2 )
  262. {
  263. %device = getField( %temp, %i + 0 );
  264. %action = getField( %temp, %i + 1 );
  265. %actionMap.unbind( %device, %action );
  266. }
  267. }
  268. function redoMapping( %device, %actionMap, %action, %cmd, %oldIndex, %newIndex )
  269. {
  270. //%actionMap.bind( %device, %action, $RemapCmd[%newIndex] );
  271. %actionMap.bind( %device, %action, %cmd );
  272. fillRemapList();
  273. }