menuInputButtons.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. /// \param %gamepadOnly (bool) If true, will only show the button when working in the gamepad input mode
  54. function MenuInputButton::set(%this, %gamepadButton, %keyboardButton, %text, %command, %gamepadOnly)
  55. {
  56. %set = (! ((%text $= "") && (%command $= "")));
  57. %this.setText(%text);
  58. %this.setActive(%set);
  59. %this.setVisible(%set);
  60. %this.gamepadButton = %gamepadButton;
  61. %this.keyboardButton = %keyboardButton;
  62. if(%gamepadOnly $= "")
  63. %gamepadOnly = false;
  64. %this.gamepadOnly = %gamepadOnly;
  65. %this.Command = %command;
  66. }
  67. /// Refreshes the specific button, updating it's visbility status and the displayed input image
  68. function MenuInputButton::refresh(%this)
  69. {
  70. %set = (! ((%this.text $= "") && (%this.command $= "")));
  71. //Special-case of where we're in keyboard+mouse mode, but the menubutton is gamepad only mode, so we early out
  72. if(%this.gamepadOnly && $activeControllerType !$= "gamepad")
  73. %set = false;
  74. %this.setActive(%set);
  75. %this.setVisible(%set);
  76. if(!%this.isActive())
  77. return;
  78. if($activeControllerType $= "gamepad")
  79. {
  80. if(%this.gamepadButton !$= "")
  81. {
  82. %path = "";
  83. if($activeControllerName $= "PS4 Controller")
  84. {
  85. %path = "data/ui/images/inputs/PS4/PS4_";
  86. if(%this.gamepadButton $= "btn_a")
  87. %path = %path @ "Cross";
  88. else if(%this.gamepadButton $= "btn_b")
  89. %path = %path @ "Circle";
  90. else if(%this.gamepadButton $= "btn_x")
  91. %path = %path @ "Square";
  92. else if(%this.gamepadButton $= "btn_y")
  93. %path = %path @ "Triangle";
  94. else if(%this.gamepadButton $= "btn_l")
  95. %path = %path @ "L1";
  96. else if(%this.gamepadButton $= "zaxis")
  97. %path = %path @ "L2";
  98. else if(%this.gamepadButton $= "btn_r")
  99. %path = %path @ "R1";
  100. else if(%this.gamepadButton $= "rzaxis")
  101. %path = %path @ "R2";
  102. else if(%this.gamepadButton $= "btn_start")
  103. %path = %path @ "Options";
  104. else if(%this.gamepadButton $= "btn_back")
  105. %path = %path @ "Share";
  106. }
  107. else if($activeControllerName $= "Nintendo Switch Pro Controller")
  108. {
  109. %path = "data/ui/images/inputs/Switch/Switch_";
  110. if(%this.gamepadButton $= "btn_a")
  111. %path = %path @ "B";
  112. else if(%this.gamepadButton $= "btn_b")
  113. %path = %path @ "A";
  114. else if(%this.gamepadButton $= "btn_x")
  115. %path = %path @ "Y";
  116. else if(%this.gamepadButton $= "btn_y")
  117. %path = %path @ "X";
  118. else if(%this.gamepadButton $= "btn_l")
  119. %path = %path @ "LB";
  120. else if(%this.gamepadButton $= "zaxis")
  121. %path = %path @ "LT";
  122. else if(%this.gamepadButton $= "btn_r")
  123. %path = %path @ "RB";
  124. else if(%this.gamepadButton $= "rzaxis")
  125. %path = %path @ "RT";
  126. else if(%this.gamepadButton $= "btn_start")
  127. %path = %path @ "Plus";
  128. else if(%this.gamepadButton $= "btn_back")
  129. %path = %path @ "Minus";
  130. }
  131. else if($activeControllerName !$= "")
  132. {
  133. %path = "data/ui/images/inputs/Xbox/Xbox_";
  134. if(%this.gamepadButton $= "btn_a")
  135. %path = %path @ "A";
  136. else if(%this.gamepadButton $= "btn_b")
  137. %path = %path @ "B";
  138. else if(%this.gamepadButton $= "btn_x")
  139. %path = %path @ "X";
  140. else if(%this.gamepadButton $= "btn_y")
  141. %path = %path @ "Y";
  142. else if(%this.gamepadButton $= "btn_l")
  143. %path = %path @ "LB";
  144. else if(%this.gamepadButton $= "zaxis")
  145. %path = %path @ "LT";
  146. else if(%this.gamepadButton $= "btn_r")
  147. %path = %path @ "RB";
  148. else if(%this.gamepadButton $= "rzaxis")
  149. %path = %path @ "RT";
  150. else if(%this.gamepadButton $= "btn_start")
  151. %path = %path @ "Menu";
  152. else if(%this.gamepadButton $= "btn_back")
  153. %path = %path @ "Windows";
  154. }
  155. }
  156. }
  157. else
  158. {
  159. if(%this.keyboardButton !$= "")
  160. {
  161. %path = "data/ui/images/Inputs/Keyboard & Mouse/Keyboard_Black_" @ %this.keyboardButton;
  162. }
  163. }
  164. %this.setBitmap(%path);
  165. return true;
  166. }
  167. /// Refreshes a menu input container, updating the buttons inside it
  168. function MenuInputButtonContainer::refresh(%this)
  169. {
  170. %count = %this.getCount();
  171. for(%i=0; %i < %count; %i++)
  172. {
  173. %btn = %this.getObject(%i);
  174. %btn.refresh();
  175. }
  176. }
  177. /// Sets the given MenuInputButtonContainer as the active one. This directs input events
  178. /// to it's buttons, ensures it's visible, and auto-hides the old active container if it was set
  179. function MenuInputButtonContainer::setActive(%this)
  180. {
  181. if(isObject($activeMenuButtonContainer))
  182. $activeMenuButtonContainer.hidden = true;
  183. $activeMenuButtonContainer = %this;
  184. $activeMenuButtonContainer.hidden = false;
  185. $activeMenuButtonContainer.refresh();
  186. }
  187. /// Checks the input manager for if we have a gamepad active and gets it's name
  188. /// If we have one, also sets the active input type to gamepad
  189. function MenuInputButtonContainer::checkGamepad(%this)
  190. {
  191. %controllerName = SDLInputManager::JoystickNameForIndex(0);
  192. $activeControllerName = %controllerName;
  193. if($activeControllerName $= "")
  194. $activeControllerType = "K&M";
  195. else
  196. $activeControllerType = "gamepad";
  197. }
  198. /// This is called by the earlier inputs callback that comes from the menu list
  199. /// this allows us to first check what the input type is, and if the device is different
  200. /// (such as going from keyboard and mouse to gamepad) we can refresh the buttons to update
  201. /// the display
  202. /// Then we process the input to see if it matches to any of the button maps for our
  203. /// MenuInputButtons. If we have a match, we execute it's command.
  204. function MenuInputButtonContainer::processInputs(%this, %device, %action)
  205. {
  206. //check to see if our status has changed
  207. %changed = false;
  208. %oldDevice = $activeControllerName;
  209. %deviceName = stripTrailingNumber(%device);
  210. if(%deviceName $= "keyboard" || %deviceName $= "mouse")
  211. {
  212. if($activeControllerName !$= "K&M")
  213. %changed = true;
  214. $activeControllerName = "K&M";
  215. $activeControllerType = "K&M";
  216. Canvas.showCursor();
  217. }
  218. else
  219. {
  220. if(%this.checkGamepad())
  221. {
  222. Canvas.hideCursor();
  223. }
  224. if($activeControllerType !$= %oldDevice)
  225. %changed = true;
  226. }
  227. if(%changed)
  228. %this.refresh();
  229. //Now process the input for the button accelerator, if applicable
  230. //Set up our basic buttons
  231. for(%i=0; %i < %this.getCount(); %i++)
  232. {
  233. %btn = %this.getObject(%i);
  234. if(!%btn.isActive())
  235. continue;
  236. if($activeControllerType !$= "K&M")
  237. {
  238. if(%btn.gamepadButton $= %action)
  239. {
  240. eval(%btn.command);
  241. }
  242. }
  243. else
  244. {
  245. if(%btn.keyboardButton $= %action)
  246. {
  247. eval(%btn.command);
  248. }
  249. }
  250. }
  251. }
  252. /// This is called by the earlier inputs callback that comes from the menu list
  253. /// this allows us to first check what the input type is, and if the device is different
  254. /// (such as going from keyboard and mouse to gamepad) we can refresh the buttons to update
  255. /// the display
  256. function MenuInputButtonContainer::processAxisEvent(%this, %device, %action, %axisVal)
  257. {
  258. //check to see if our status has changed
  259. %changed = false;
  260. %oldDevice = $activeControllerName;
  261. %deviceName = stripTrailingNumber(%device);
  262. if(%deviceName $= "mouse")
  263. {
  264. if($activeControllerName !$= "K&M")
  265. %changed = true;
  266. $activeControllerName = "K&M";
  267. $activeControllerType = "K&M";
  268. Canvas.showCursor();
  269. }
  270. else
  271. {
  272. if(%this.checkGamepad())
  273. {
  274. Canvas.hideCursor();
  275. }
  276. if($activeControllerType !$= %oldDevice)
  277. %changed = true;
  278. }
  279. if(%changed)
  280. %this.refresh();
  281. }
  282. //
  283. //
  284. function onSDLDeviceConnected(%sdlIndex, %deviceName, %deviceType)
  285. {
  286. /*if(GamepadButtonsGui.checkGamepad())
  287. {
  288. GamepadButtonsGui.hidden = false;
  289. }*/
  290. }
  291. function onSDLDeviceDisconnected(%sdlIndex)
  292. {
  293. /*if(!GamepadButtonsGui.checkGamepad())
  294. {
  295. GamepadButtonsGui.hidden = true;
  296. }*/
  297. }