GameMenu.tscript 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. function GameMenu::onAdd(%this)
  2. {
  3. %this.gameMenusArray = new ArrayObject(){};
  4. }
  5. function GameMenu::onWake(%this)
  6. {
  7. if($Server::ServerType $= "SinglePlayer")
  8. {
  9. $timescale = 0;
  10. sfxSetChannelVolume( $SimAudioType, $pref::SFX::channelVolume[ 0 ] );
  11. }
  12. callOnModules("registerGameMenus", "", %this.gameMenusArray);
  13. //Remove duplicates as needed
  14. %this.gameMenusArray.uniqueKey();
  15. GameMenuButtonList.clear();
  16. %stackWidth = 0;
  17. //process the entries and give 'em buttons on the button array
  18. for(%i=0; %i < %this.gameMenusArray.count(); %i++)
  19. {
  20. %buttonText = %this.gameMenusArray.getKey(%i);
  21. %textWidth = GuiMenuButtonProfile.getStringWidth(%buttonText);
  22. %btn = new GuiButtonCtrl() {
  23. extent = %textWidth + 80 SPC 40;
  24. profile = GuiMenuButtonProfile;
  25. text = %buttonText;
  26. class = "GameMenuButton";
  27. command = "GameMenu.openGameMenu(\"" @ %buttonText @ "\");";
  28. };
  29. %stackWidth += %textWidth + 40;
  30. GameMenuButtonList.add(%btn);
  31. }
  32. GameMenuButtonList.resize(GameMenuTitlePanel.extent.x/2 - %stackWidth/2, 0, %stackWidth, GameMenuTitlePanel.extent.y);
  33. %this.openGameMenu("System");
  34. //give a slight delay for the canvas to be fully refreshed before we sync things
  35. %this.schedule(500, "syncGUI");
  36. }
  37. function GameMenu::onSleep(%this)
  38. {
  39. if($Server::ServerType $= "SinglePlayer")
  40. {
  41. $timescale = 1;
  42. sfxSetChannelVolume( $SimAudioType, $pref::SFX::channelVolume[ $SimAudioType ] );
  43. }
  44. if(isObject(%this.currentMenu))
  45. {
  46. Canvas.popDialog(%this.currentMenu);
  47. }
  48. }
  49. if(!isObject( GameMenuActionMap ) )
  50. {
  51. new ActionMap(GameMenuActionMap){};
  52. //We'll just use the existing BaseUI nav functions because it'd be the same logic anyways
  53. GameMenuActionMap.bind( keyboard, w, BaseUINavigatePrev );
  54. GameMenuActionMap.bind( keyboard, s, BaseUINavigateNext );
  55. GameMenuActionMap.bind( gamepad, yaxis, "D", "-0.23 0.23", BaseUIStickNavigate );
  56. GameMenuActionMap.bind( gamepad, upov, BaseUINavigatePrev );
  57. GameMenuActionMap.bind( gamepad, dpov, BaseUINavigateNext );
  58. GameMenuActionMap.bind( keyboard, Enter, BaseUIActivateSelected );
  59. GameMenuActionMap.bind( gamepad, btn_a, BaseUIActivateSelected );
  60. GameMenuActionMap.bindCmd( keyboard, Escape, "Canvas.popDialog(GameMenu);", "" );
  61. GameMenuActionMap.bindCmd( gamepad, btn_b, "Canvas.popDialog(GameMenu);", "" );
  62. GameMenuActionMap.bindCmd( gamepad, btn_start, "Canvas.popDialog(GameMenu);", "" );
  63. GameMenuActionMap.bind( keyboard, q, GameMenuPrevMenu );
  64. GameMenuActionMap.bind( gamepad, btn_l, GameMenuPrevMenu );
  65. GameMenuActionMap.bind( keyboard, e, GameMenuNextMenu );
  66. GameMenuActionMap.bind( gamepad, btn_r, GameMenuNextMenu );
  67. }
  68. function GameMenu::openGameMenu(%this, %menuName)
  69. {
  70. %menuIdx = %this.gameMenusArray.getIndexFromKey(%menuName);
  71. if(%menuIdx != -1)
  72. {
  73. %newMenu = %this.gameMenusArray.getValue(%menuIdx);
  74. if(isObject(%this.currentMenu))
  75. Canvas.popDialog(%this.currentMenu);
  76. Canvas.pushDialog(%newMenu);
  77. %this.currentMenu = %newMenu;
  78. %this.currentMenuIdx = %menuIdx;
  79. }
  80. %this.syncGui();
  81. }
  82. function openPauseMenuOptions()
  83. {
  84. GameMenu.pushPage(OptionsMenu);
  85. }
  86. function pauseMenuExitToMenu()
  87. {
  88. MessageBoxOKCancel("Exit?", "Do you wish to exit to the Main Menu?", "escapeFromGame();", "");
  89. }
  90. function pauseMenuExitToDesktop()
  91. {
  92. MessageBoxOKCancel("Exit?", "Do you wish to exit to the desktop?", "quit();", "");
  93. }
  94. function GameMenuPrevMenu(%val)
  95. {
  96. if(%val)
  97. {
  98. %currentIdx = GameMenu.currentMenuIdx;
  99. GameMenu.currentMenuIdx -= 1;
  100. if(GameMenu.currentMenuIdx < 0)
  101. GameMenu.currentMenuIdx = 0;
  102. if(%currentIdx == GameMenu.currentMenuIdx)
  103. return;
  104. %menuName = GameMenu.gameMenusArray.getKey(GameMenu.currentMenuIdx);
  105. GameMenu.openGameMenu(%menuName);
  106. }
  107. }
  108. function GameMenuNextMenu(%val)
  109. {
  110. if(%val)
  111. {
  112. %currentIdx = GameMenu.currentMenuIdx;
  113. GameMenu.currentMenuIdx += 1;
  114. if(GameMenu.currentMenuIdx >= GameMenu.gameMenusArray.count())
  115. GameMenu.currentMenuIdx = GameMenu.gameMenusArray.count()-1;
  116. if(%currentIdx == GameMenu.currentMenuIdx)
  117. return;
  118. %menuName = GameMenu.gameMenusArray.getKey(GameMenu.currentMenuIdx);
  119. GameMenu.openGameMenu(%menuName);
  120. }
  121. }
  122. function GameMenu::syncGui(%this)
  123. {
  124. GameMenuButtonList.callOnChildren("setHighlighted", false);
  125. %btn = GameMenuButtonList.getObject(%this.currentMenuIdx);
  126. %btn.setHighlighted(true);
  127. %buttonPosX = %btn.position.x + GameMenuButtonList.position.x;
  128. GameMenuPrevNavIcon.position.x = %buttonPosX;
  129. GameMenuNextNavIcon.position.x = %buttonPosX + %btn.extent.x - 40;
  130. //Update the button imagery to comply to the last input device we'd used
  131. %device = Canvas.getLastInputDevice();
  132. if(%device $= "mouse")
  133. %device = "keyboard";
  134. GameMenuBackBtn.setBitmap(BaseUIActionMap.getCommandButtonBitmap(%device, "BaseUIBackOut"));
  135. GameMenuPrevNavIcon.setBitmap(GameMenuActionMap.getCommandButtonBitmap(%device, "GameMenuPrevMenu"));
  136. GameMenuNextNavIcon.setBitmap(GameMenuActionMap.getCommandButtonBitmap(%device, "GameMenuNextMenu"));
  137. %this.schedule(500, "syncGUI");
  138. }
  139. /*
  140. */