menuInputHandling.tscript 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. //==============================================================================
  2. // Menu Input Buttons
  3. // This file manages the Menu Input Buttons stuff
  4. // Any time you have a GUI button that should be clickable AND map to a key input
  5. // such as a gamepad button, or enter, etc, this stuff can be used
  6. //==============================================================================
  7. /*
  8. Gamepad input reference for 360 controller
  9. btn_a = A
  10. btn_b = B
  11. btn_x = X
  12. btn_y = Y
  13. btn_r = Right Bumper
  14. btn_l = Right Bumper
  15. upov = Dpad Up
  16. dpov = Dpad Down
  17. lpov = Dpad Left
  18. rpov = Dpad Right
  19. xaxis = Left Stick | + values = up, - values = down
  20. yaxis = Left Stick | + values = up, - values = down
  21. rxaxis = Right Stick | + values = up, - values = down
  22. ryaxis = Right Stick | + values = up, - values = down
  23. zaxis = Left Trigger
  24. rzaxis = Right Trigger
  25. btn_start = Start
  26. btn_back = Back/Select
  27. */
  28. /// This is used with the main UI menu lists, when a non-axis input event is called
  29. /// such as pressing a button
  30. /// It is called from the engine
  31. function UIMenuButtonList::onInputEvent(%this, %device, %action, %state)
  32. {
  33. if(%state)
  34. $activeMenuButtonContainer.processInputs(%device, %action);
  35. }
  36. /// This is used with the main UI menu lists, when an axis input event is called
  37. /// such as moving a joystick
  38. /// It is called from the engine
  39. function UIMenuButtonList::onAxisEvent(%this, %device, %action, %axisVal)
  40. {
  41. //Skip out of the value is too low as it could just be noise or miscalibrated defaults
  42. if(%axisVal < 0.02)
  43. return;
  44. $activeMenuButtonContainer.processAxisEvent(%device, %action);
  45. }
  46. /// Sets the command and text for the specified button. If %text and %command
  47. /// are left empty, the button will be disabled and hidden.
  48. ///
  49. /// \param %gamepadButton (string) The button to set for when using gamepad input. See the input map reference comment at the top of the file
  50. /// \param %keyboardButton (string) The button to set for when using keyboard/mouse input.
  51. /// \param %text (string) The text to display next to the A button graphic.
  52. /// \param %command (string) The command executed when the A button is pressed.
  53. function MenuInputButton::set(%this, %gamepadButton, %keyboardButton, %text, %command)
  54. {
  55. %this.setHidden(false);
  56. %set = (! ((%text $= "") && (%command $= "")));
  57. %this.gamepadButton = %gamepadButton;
  58. %this.keyboardButton = %keyboardButton;
  59. if(%gamepadButton $= "")
  60. %this.gamepadValid = false;
  61. else
  62. %this.gamepadValid = true;
  63. if(%keyboardButton $= "")
  64. %this.kbmValid = false;
  65. else
  66. %this.kbmValid = true;
  67. if((!%this.kbmValid && $activeControllerType !$= "gamepad") ||
  68. (!%this.gamepadValid && $activeControllerType $= "gamepad"))
  69. %set = false;
  70. %this.setText(%text);
  71. %this.Command = %command;
  72. %this.refresh();
  73. }
  74. function MenuInputButton::disable(%this)
  75. {
  76. %this.setText("");
  77. %this.Command = "";
  78. %this.setActive(false);
  79. %this.setVisible(false);
  80. }
  81. /// Refreshes the specific button, updating it's visbility status and the displayed input image
  82. function MenuInputButton::refresh(%this)
  83. {
  84. %set = (! ((%this.text $= "") && (%this.command $= "")));
  85. //Do a check so if a MenuInput is selectively bound and we're not using the
  86. //matched input type, then we skip
  87. if((!%this.kbmValid && $activeControllerType !$= "gamepad") ||
  88. (!%this.gamepadValid && $activeControllerType $= "gamepad"))
  89. %set = false;
  90. %this.setActive(%set);
  91. %this.setVisible(%set);
  92. if(!%this.isActive())
  93. return;
  94. if($activeControllerType $= "gamepad")
  95. {
  96. if(%this.gamepadButton !$= "")
  97. {
  98. %assetId = "";
  99. if($activeControllerName $= "PS4 Controller")
  100. {
  101. %assetId = "UI:PS4_";
  102. if(%this.gamepadButton $= "btn_a")
  103. %assetId = %assetId @ "Cross";
  104. else if(%this.gamepadButton $= "btn_b")
  105. %assetId = %assetId @ "Circle";
  106. else if(%this.gamepadButton $= "btn_x")
  107. %assetId = %assetId @ "Square";
  108. else if(%this.gamepadButton $= "btn_y")
  109. %assetId = %assetId @ "Triangle";
  110. else if(%this.gamepadButton $= "btn_l")
  111. %assetId = %assetId @ "L1";
  112. else if(%this.gamepadButton $= "zaxis")
  113. %assetId = %assetId @ "L2";
  114. else if(%this.gamepadButton $= "btn_r")
  115. %assetId = %assetId @ "R1";
  116. else if(%this.gamepadButton $= "rzaxis")
  117. %assetId = %assetId @ "R2";
  118. else if(%this.gamepadButton $= "btn_start")
  119. %assetId = %assetId @ "Options";
  120. else if(%this.gamepadButton $= "btn_back")
  121. %assetId = %assetId @ "Share";
  122. }
  123. else if($activeControllerName $= "Nintendo Switch Pro Controller")
  124. {
  125. %assetId = "UI:Switch_";
  126. if(%this.gamepadButton $= "btn_a")
  127. %assetId = %assetId @ "B";
  128. else if(%this.gamepadButton $= "btn_b")
  129. %assetId = %assetId @ "A";
  130. else if(%this.gamepadButton $= "btn_x")
  131. %assetId = %assetId @ "Y";
  132. else if(%this.gamepadButton $= "btn_y")
  133. %assetId = %assetId @ "X";
  134. else if(%this.gamepadButton $= "btn_l")
  135. %assetId = %assetId @ "LB";
  136. else if(%this.gamepadButton $= "zaxis")
  137. %assetId = %assetId @ "LT";
  138. else if(%this.gamepadButton $= "btn_r")
  139. %assetId = %assetId @ "RB";
  140. else if(%this.gamepadButton $= "rzaxis")
  141. %assetId = %assetId @ "RT";
  142. else if(%this.gamepadButton $= "btn_start")
  143. %assetId = %assetId @ "Plus";
  144. else if(%this.gamepadButton $= "btn_back")
  145. %assetId = %assetId @ "Minus";
  146. }
  147. else if($activeControllerName !$= "")
  148. {
  149. %assetId = "UI:Xbox_";
  150. if(%this.gamepadButton $= "btn_a")
  151. %assetId = %assetId @ "A";
  152. else if(%this.gamepadButton $= "btn_b")
  153. %assetId = %assetId @ "B";
  154. else if(%this.gamepadButton $= "btn_x")
  155. %assetId = %assetId @ "X";
  156. else if(%this.gamepadButton $= "btn_y")
  157. %assetId = %assetId @ "Y";
  158. else if(%this.gamepadButton $= "btn_l")
  159. %assetId = %assetId @ "LB";
  160. else if(%this.gamepadButton $= "zaxis")
  161. %assetId = %assetId @ "LT";
  162. else if(%this.gamepadButton $= "btn_r")
  163. %assetId = %assetId @ "RB";
  164. else if(%this.gamepadButton $= "rzaxis")
  165. %assetId = %assetId @ "RT";
  166. else if(%this.gamepadButton $= "btn_start")
  167. %assetId = %assetId @ "Menu";
  168. else if(%this.gamepadButton $= "btn_back")
  169. %assetId = %assetId @ "Windows";
  170. }
  171. }
  172. }
  173. else
  174. {
  175. if(%this.keyboardButton !$= "")
  176. {
  177. %assetId = "UI:Keyboard_Black_" @ %this.keyboardButton;
  178. }
  179. }
  180. %this.setBitmap(%assetId @ "_image");
  181. return true;
  182. }
  183. /// Refreshes a menu input container, updating the buttons inside it
  184. function MenuInputButtonContainer::refresh(%this)
  185. {
  186. %count = %this.getCount();
  187. for(%i=0; %i < %count; %i++)
  188. {
  189. %btn = %this.getObject(%i);
  190. %btn.refresh();
  191. }
  192. }
  193. /// Sets the given MenuInputButtonContainer as the active one. This directs input events
  194. /// to it's buttons, ensures it's visible, and auto-hides the old active container if it was set
  195. function MenuInputButtonContainer::setActive(%this)
  196. {
  197. if(isObject($activeMenuButtonContainer))
  198. $activeMenuButtonContainer.hidden = true;
  199. $activeMenuButtonContainer = %this;
  200. $activeMenuButtonContainer.hidden = false;
  201. $activeMenuButtonContainer.refresh();
  202. }
  203. /// Checks the input manager for if we have a gamepad active and gets it's name
  204. /// If we have one, also sets the active input type to gamepad
  205. function MenuInputButtonContainer::checkGamepad(%this)
  206. {
  207. %controllerName = SDLInputManager::JoystickNameForIndex(0);
  208. $activeControllerName = %controllerName;
  209. if($activeControllerName $= "")
  210. $activeControllerType = "K&M";
  211. else
  212. $activeControllerType = "gamepad";
  213. }
  214. /// This is called by the earlier inputs callback that comes from the menu list
  215. /// this allows us to first check what the input type is, and if the device is different
  216. /// (such as going from keyboard and mouse to gamepad) we can refresh the buttons to update
  217. /// the display
  218. /// Then we process the input to see if it matches to any of the button maps for our
  219. /// MenuInputButtons. If we have a match, we execute it's command.
  220. function MenuInputButtonContainer::processInputs(%this, %device, %action)
  221. {
  222. //check to see if our status has changed
  223. %changed = false;
  224. %oldDevice = $activeControllerName;
  225. %deviceName = stripTrailingNumber(%device);
  226. if(%deviceName $= "keyboard" || %deviceName $= "mouse")
  227. {
  228. if($activeControllerName !$= "K&M")
  229. %changed = true;
  230. $activeControllerName = "K&M";
  231. $activeControllerType = "K&M";
  232. Canvas.showCursor();
  233. }
  234. else
  235. {
  236. if(%this.checkGamepad())
  237. {
  238. Canvas.hideCursor();
  239. }
  240. if($activeControllerType !$= %oldDevice)
  241. %changed = true;
  242. }
  243. if(%changed)
  244. %this.refresh();
  245. //Now process the input for the button accelerator, if applicable
  246. //Set up our basic buttons
  247. for(%i=0; %i < %this.getCount(); %i++)
  248. {
  249. %btn = %this.getObject(%i);
  250. if(!%btn.isActive())
  251. continue;
  252. if($activeControllerType !$= "K&M")
  253. {
  254. if(%btn.gamepadButton $= %action)
  255. {
  256. eval(%btn.command);
  257. }
  258. }
  259. else
  260. {
  261. if(%btn.keyboardButton $= %action)
  262. {
  263. eval(%btn.command);
  264. }
  265. }
  266. }
  267. }
  268. /// This is called by the earlier inputs callback that comes from the menu list
  269. /// this allows us to first check what the input type is, and if the device is different
  270. /// (such as going from keyboard and mouse to gamepad) we can refresh the buttons to update
  271. /// the display
  272. function MenuInputButtonContainer::processAxisEvent(%this, %device, %action, %axisVal)
  273. {
  274. //check to see if our status has changed
  275. %changed = false;
  276. %oldDevice = $activeControllerName;
  277. %deviceName = stripTrailingNumber(%device);
  278. if(%deviceName $= "mouse")
  279. {
  280. if($activeControllerName !$= "K&M")
  281. %changed = true;
  282. $activeControllerName = "K&M";
  283. $activeControllerType = "K&M";
  284. Canvas.showCursor();
  285. }
  286. else
  287. {
  288. if(%this.checkGamepad())
  289. {
  290. Canvas.hideCursor();
  291. }
  292. if($activeControllerType !$= %oldDevice)
  293. %changed = true;
  294. }
  295. if(%changed)
  296. %this.refresh();
  297. }
  298. //
  299. //
  300. function onSDLDeviceConnected(%sdlIndex, %deviceName, %deviceType)
  301. {
  302. /*if(GamepadButtonsGui.checkGamepad())
  303. {
  304. GamepadButtonsGui.hidden = false;
  305. }*/
  306. }
  307. function onSDLDeviceDisconnected(%sdlIndex)
  308. {
  309. /*if(!GamepadButtonsGui.checkGamepad())
  310. {
  311. GamepadButtonsGui.hidden = true;
  312. }*/
  313. }
  314. //==============================================================================
  315. // Menu Input processing
  316. // These functions manage the Menu input processing in general
  317. // Whenever a MenuInputHandler consumes an input event, it'll process them here
  318. // This'll let the active menu list be navigated, as well as buttons be processed
  319. // and ultimately handled by the Input Buttons above
  320. //==============================================================================
  321. function MenuInputHandler::onAxisEvent(%this, %device, %action, %value)
  322. {
  323. //this is to force a refresh of the menu
  324. if(%value == 1 || %value == -1)
  325. $activeMenuButtonContainer.processInputs(%device, %action);
  326. if(startsWith(%device, "mouse"))
  327. return;
  328. if((%action $= "upov" && %value > 0) || (%action $= "yaxis" && %value == -1))
  329. {
  330. $activeMenuList.navigateUp();
  331. }
  332. if((%action $= "dpov" && %value > 0) || (%action $= "yaxis" && %value == 1))
  333. {
  334. $activeMenuList.navigateDown();
  335. }
  336. //How we deal with the left and right navigation is dependant on the mode of the
  337. //menu list
  338. if($activeMenuListMode $= "Settings")
  339. {
  340. if((%action $= "lpov" && %value > 0) || (%action $= "xaxis" && %value == -1))
  341. {
  342. echo("Options menu nudged left!");
  343. //$activeMenuList.navigateLeft();
  344. }
  345. if((%action $= "rpov" && %value > 0) || (%action $= "xaxis" && %value == -1))
  346. {
  347. echo("Options menu nudged right!");
  348. //$activeMenuList.navigateRight();
  349. }
  350. }
  351. else
  352. {
  353. if((%action $= "lpov" && %value > 0) || (%action $= "xaxis" && %value == -1))
  354. {
  355. $activeMenuList.navigateLeft();
  356. }
  357. if((%action $= "rpov" && %value > 0) || (%action $= "xaxis" && %value == -1))
  358. {
  359. $activeMenuList.navigateRight();
  360. }
  361. }
  362. }
  363. function MenuInputHandler::onInputEvent(%this, %device, %action, %state)
  364. {
  365. if(%action $= "upov" || %action $= "dpov" || %action $= "lpov" || %action $= "rpov")
  366. {
  367. %this.onAxisEvent(%device, %action, %state);
  368. return;
  369. }
  370. if(%state)
  371. $activeMenuButtonContainer.processInputs(%device, %action);
  372. }
  373. //==============================================================================
  374. // Menu List processing
  375. // These functions manage the navigation and activation of the Menu Lists
  376. //==============================================================================
  377. function MenuList::isActiveMenuList(%this)
  378. {
  379. if($activeMenuList == %this)
  380. return true;
  381. return false;
  382. }
  383. function MenuList::setAsActiveMenuList(%this, %startPosition, %menuMode)
  384. {
  385. if(%startPosition $= "")
  386. %startPosition = "0 0";
  387. if(%menuMode $= "")
  388. %menuMode = "Menu";
  389. $activeMenuList = %this;
  390. $activeMenuList.hidden = false;
  391. $activeMenuList.ListPosition = %startPosition;
  392. $activeMenuListMode = %menuMode;
  393. %this.refresh();
  394. }
  395. function MenuList::activate(%this)
  396. {
  397. //check for a highlighted element
  398. if($activeMenuList.ListPosition.y > -1 && $activeMenuList.ListPosition < $activeMenuList.getCount())
  399. {
  400. %btn = $activeMenuList.getObject($activeMenuList.ListPosition.y);
  401. %btn.performClick();
  402. }
  403. }
  404. function MenuList::refresh(%this)
  405. {
  406. %selectedObject = -1;
  407. for(%i=0; %i < $activeMenuList.getCount(); %i++)
  408. {
  409. %btn = $activeMenuList.getObject(%i);
  410. %isSelected = %i == $activeMenuList.ListPosition.y;
  411. %btn.setHighlighted(%isSelected);
  412. if(%isSelected)
  413. %selectedObject = %i;
  414. }
  415. if(isObject(%this.buttonPointerCtrl))
  416. {
  417. if(%selectedObject != -1)
  418. {
  419. %this.buttonPointerCtrl.setHidden(false);
  420. %buttonCenter = $activeMenuList.getObject(%selectedObject).getGlobalCenter();
  421. if(%this.centerButtonPointerCtrl)
  422. {
  423. %this.buttonPointerCtrl.setCenter(%buttonCenter.x, %buttonCenter.y);
  424. }
  425. else
  426. {
  427. //if we're not centering, then left-justify
  428. %this.buttonPointerCtrl.setCenter(%buttonCenter.x - $activeMenuList.getObject(%selectedObject).extent.x / 2, %buttonCenter.y);
  429. }
  430. }
  431. else
  432. {
  433. %this.buttonPointerCtrl.setHidden(true);
  434. }
  435. }
  436. if($activeMenuList.isMethod("onNavigate"))
  437. $activeMenuList.onNavigate($activeMenuList.ListPosition.y);
  438. %parent = $activeMenuList.getParent();
  439. if(%parent.getClassName() $= "GuiScrollCtrl")
  440. {
  441. %parent.scrollToObject(%selectedObject);
  442. }
  443. }
  444. function MenuList::navigateUp(%this)
  445. {
  446. $activeMenuList.ListPosition.y -= 1;
  447. if($activeMenuList.ListPosition.y < 0)
  448. $activeMenuList.ListPosition.y = 0;
  449. %this.refresh();
  450. }
  451. function MenuList::navigateDown(%this)
  452. {
  453. $activeMenuList.ListPosition.y += 1;
  454. if($activeMenuList.ListPosition.y >= $activeMenuList.getCount())
  455. $activeMenuList.ListPosition.y = $activeMenuList.getCount()-1;
  456. %this.refresh();
  457. }
  458. function MenuList::navigateLeft()
  459. {
  460. //Atm, we're only handling specific control types, namely options entries, but
  461. //this could readily be expanded upon to handle grids like for inventory screens
  462. //or the like
  463. %btn = $activeMenuList.getObject($activeMenuList.ListPosition.y);
  464. if(%btn.getClassName() $= "GuiGameSettingsCtrl" && %btn.isEnabled())
  465. {
  466. %mode = %btn.getMode();
  467. if(%mode == 0) //options list
  468. {
  469. %optionId = %btn.getCurrentOptionIndex() - 1;
  470. %btn.selectOptionByIndex(%optionId);
  471. %btn.onChange();
  472. }
  473. else if(%mode == 1) //slider
  474. {
  475. %value = %btn.getValue();
  476. %adjustedValue = %value - %btn.getIncrement();
  477. %minValue = %btn.getRange().x;
  478. if(%adjustedValue < %minValue)
  479. %adjustedValue = %minValue;
  480. %btn.setValue(%adjustedValue);
  481. %btn.onChange();
  482. }
  483. }
  484. }
  485. function MenuList::navigateRight()
  486. {
  487. %btn = $activeMenuList.getObject($activeMenuList.ListPosition.y);
  488. if(%btn.getClassName() $= "GuiGameSettingsCtrl" && %btn.isEnabled())
  489. {
  490. %mode = %btn.getMode();
  491. if(%mode == 0) //options list
  492. {
  493. %optionId = %btn.getCurrentOptionIndex() + 1;
  494. %btn.selectOptionByIndex(%optionId);
  495. %btn.onChange();
  496. }
  497. else if(%mode == 1) //slider
  498. {
  499. %value = %btn.getValue();
  500. %adjustedValue = %value + %btn.getIncrement();
  501. %maxValue = %btn.getRange().y;
  502. if(%adjustedValue > %maxValue)
  503. %adjustedValue = %maxValue;
  504. %btn.setValue(%adjustedValue);
  505. %btn.onChange();
  506. }
  507. }
  508. }
  509. function MenuList::getActiveRow(%this)
  510. {
  511. return $activeMenuList.ListPosition.y;
  512. }
  513. function MenuListButton::onHighlighted(%this, %state)
  514. {
  515. %parentContainer = %this.getParent();
  516. if(%parentContainer.class $= "MenuList" || %parentContainer.superClass $= "MenuList")
  517. {
  518. if(isObject(%parentContainer.buttonPointerCtrl))
  519. {
  520. if(%state)
  521. {
  522. %parentContainer.buttonPointerCtrl.setHidden(false);
  523. %buttonCenter = %this.getGlobalCenter();
  524. if(%parentContainer.centerButtonPointerCtrl)
  525. {
  526. %parentContainer.buttonPointerCtrl.setGlobalCenter(%buttonCenter.x, %buttonCenter.y);
  527. }
  528. else
  529. {
  530. //if we're not centering, then left-justify
  531. %parentContainer.buttonPointerCtrl.setGlobalCenter(%buttonCenter.x - %this.extent.x / 2, %buttonCenter.y);
  532. }
  533. }
  534. }
  535. }
  536. }