Advertisement
Guest User

MainMenu.lua

a guest
Oct 25th, 2016
3,623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.41 KB | None | 0 0
  1. include("InstanceManager");
  2. include("LobbyTypes"); --MPLobbyMode
  3. include("PopupDialogSupport");
  4.  
  5. -- ===========================================================================
  6. --  Members
  7. -- ===========================================================================
  8. local m_mainOptionIM :  table = InstanceManager:new( "MenuOption", "Top", Controls.MainMenuOptionStack );
  9. local m_subOptionIM :   table = InstanceManager:new( "MenuOption", "Top", Controls.SubMenuOptionStack );
  10. local m_preSaveMainMenuOptions: table = {};
  11. local m_defaultMainMenuOptions: table = {};
  12. local m_singlePlayerListOptions:table = {};
  13. local m_hasSaves:boolean = false;
  14. local m_kPopupDialog;
  15. local m_currentOptions:table = {};      --Track which main menu options are being displayed and selected. Indices follow the format of {optionControl:table, isSelected:boolean}
  16. local m_initialPause = 1.5;             --How long to wait before building the main menu options when the game first loads
  17. local m_internetButton:table = nil;     --Cache internet button so it can be updated when online status events fire
  18. local mm_resumeButton:table = nil;      --Cache resume button so it can be updated when FileListQueryResults event fires
  19.  
  20. -- ===========================================================================
  21. --  Constants
  22. -- ===========================================================================
  23. local PAUSE_INCREMENT = .18;            --How long to wait (in seconds) between main menu flyouts - length of the menu cascade
  24. local TRACK_PADDING = 40;               --The amount of Y pixels to add to the track on top of the list heigh
  25.  
  26. -- ===========================================================================
  27. --  Globals
  28. -- ===========================================================================
  29.  
  30. g_LastFileQueryRequestID = nil;         -- The file list ID used to determine whether the call-back is for us or not.
  31. g_MostRecentSave = nil;                 -- The most recent single player save a user has (locally)
  32.  
  33. -- ===========================================================================
  34. -- Button Handlers
  35. -- ===========================================================================
  36. function OnResumeGame()
  37.     if(g_MostRecentSave) then
  38.         local serverType : number = ServerType.SERVER_TYPE_NONE;
  39.         Network.LeaveGame();
  40.         Network.LoadGame(g_MostRecentSave, serverType);
  41.     end
  42. end
  43.  
  44. function UpdateResumeGame(resumeButton)
  45.     if (resumeButton ~= nil) then
  46.         m_resumeButton = resumeButton;
  47.     end
  48.     if(m_resumeButton ~= nil) then
  49.         if(g_MostRecentSave ~= nil) then
  50.  
  51.             local mods = g_MostRecentSave.RequiredMods or {};
  52.    
  53.             -- Test for errors.
  54.             -- Will return a combination array/map of any errors regarding this combination of mods.
  55.             -- Array messages are generalized error codes regarding the set.
  56.             -- Map messages are error codes specific to the mod Id.
  57.             local errors = Modding.CheckRequirements(mods, SaveTypes.SINGLE_PLAYER);
  58.             local success = (errors == nil or errors.Success);
  59.  
  60.             m_resumeButton.Top:SetHide(not success);
  61.         else
  62.             m_resumeButton.Top:SetHide(true);
  63.         end
  64.     end
  65. end
  66.  
  67.  
  68. function OnPlayCiv6()
  69.     local save = Options.GetAppOption("Debug", "PlayNowSave");
  70.     if(save ~= nil) then
  71.         Network.LeaveGame();
  72.  
  73.         local serverType : number = ServerType.SERVER_TYPE_NONE;
  74.         Network.LoadGame(save, serverType);
  75.     else
  76.         GameConfiguration.SetToDefaults();
  77.         Network.HostGame(ServerType.SERVER_TYPE_NONE);
  78.     end
  79. end
  80.  
  81. -- ===========================================================================
  82. function OnAdvancedSetup()
  83.     GameConfiguration.SetToDefaults();
  84.     UIManager:QueuePopup(Controls.AdvancedSetup, PopupPriority.Current);
  85. end
  86.  
  87. -- ===========================================================================
  88. function OnLoadSinglePlayer()
  89.     GameConfiguration.SetToDefaults();
  90.     LuaEvents.MainMenu_SetLoadGameServerType(ServerType.SERVER_TYPE_NONE);
  91.     UIManager:QueuePopup(Controls.LoadGameMenu, PopupPriority.Current);    
  92.     Close();
  93. end
  94.  
  95. -- ===========================================================================
  96. function OnOptions()
  97.     UIManager:QueuePopup(Controls.Options, PopupPriority.Current);
  98.     Close();
  99. end
  100.  
  101. -- ===========================================================================
  102. function OnMods()
  103.     GameConfiguration.SetToDefaults();
  104.     UIManager:QueuePopup(Controls.ModsContext, PopupPriority.Current);
  105.     Close();
  106. end
  107.  
  108. -- ===========================================================================
  109. function OnPlayMultiplayer()
  110.     UIManager:QueuePopup(Controls.MultiplayerSelect, PopupPriority.Current);
  111.     Close();
  112. end
  113.  
  114. -- ===========================================================================
  115. function OnMy2KLogin()
  116.     Events.Begin2KLoginProcess();
  117.     Close();
  118. end
  119.  
  120. -- ===========================================================================
  121. function OnExitToDesktop()
  122.     if ( not m_kPopupDialog:IsOpen()) then
  123.         m_kPopupDialog:AddText(Locale.Lookup("LOC_CONFIRM_EXIT_TXT"));
  124.         m_kPopupDialog:AddTitle(Locale.ToUpper(Locale.Lookup("LOC_MAIN_MENU_EXIT_TO_DESKTOP")), Controls.PopupTitle);
  125.         m_kPopupDialog:AddButton(Locale.Lookup("LOC_CANCEL_BUTTON"), ExitCancel);
  126.         m_kPopupDialog:AddButton(Locale.Lookup("LOC_OK_BUTTON"), ExitOK, nil, nil, "PopupButtonAltInstance");
  127.         m_kPopupDialog:Open();
  128.     end
  129. end
  130.  
  131. -- ===========================================================================
  132. function ExitOK()
  133.     m_kPopupDialog:Close();
  134.     Steam.ClearRichPresence();
  135.     Events.UserConfirmedClose();
  136. end    
  137.  
  138. -- ===========================================================================
  139. function ExitCancel()
  140.     m_kPopupDialog:Close();
  141. end
  142.  
  143.     -- ===========================================================================
  144. function OnBenchmark()
  145.     Benchmark.Run("Benchmark.Civ6Save");
  146. end
  147.  
  148. -- ===========================================================================
  149. function OnCredits()
  150.     UIManager:QueuePopup( Controls.CreditsScreen, PopupPriority.Current );
  151.     Close();
  152. end
  153.  
  154. -- ===========================================================================
  155. function OnCivilopedia()
  156.     LuaEvents.OpenCivilopedia();
  157.     Close();
  158. end
  159.  
  160. -- ===========================================================================
  161. -- Multiplayer Select Screen
  162. -- ===========================================================================
  163. local InternetButtonOnlineStr : string = Locale.Lookup("LOC_MULTIPLAYER_INTERNET_GAME_TT");
  164. local InternetButtonOfflineStr : string = Locale.Lookup("LOC_MULTIPLAYER_INTERNET_GAME_OFFLINE_TT");
  165.  
  166. -- ===========================================================================
  167. function OnInternet()
  168.     LuaEvents.ChangeMPLobbyMode(MPLobbyTypes.STANDARD_INTERNET);
  169.     UIManager:QueuePopup( Controls.Lobby, PopupPriority.Current );
  170.     Close();   
  171. end
  172.  
  173. -- ===========================================================================
  174. --  WB: This callback is complicated by these events which can happen at any time.
  175. --  Because NO other buttons in the shell function in this way, using a special
  176. --  variable to save this control (instead of a more general solution).
  177. -- ===========================================================================
  178. function UpdateInternetButton(buttonControl: table)
  179.     if (buttonControl ~=nil) then
  180.         m_internetButton = buttonControl;
  181.     end
  182.     -- Internet available?
  183.     if(m_internetButton ~= nil) then
  184.         if (Network.IsInternetLobbyServiceAvailable()) then
  185.             m_internetButton.OptionButton:SetDisabled(false);
  186.             m_internetButton.Top:SetToolTipString(InternetButtonOnlineStr);
  187.             m_internetButton.ButtonLabel:SetText(Locale.Lookup("LOC_MULTIPLAYER_INTERNET_GAME"));
  188.             m_internetButton.ButtonLabel:SetColorByName( "ButtonCS" );
  189.         else
  190.             m_internetButton.OptionButton:SetDisabled(true);
  191.             m_internetButton.Top:SetToolTipString(InternetButtonOfflineStr);
  192.             m_internetButton.ButtonLabel:SetText(Locale.Lookup("LOC_MULTIPLAYER_INTERNET_GAME_OFFLINE"));
  193.             m_internetButton.ButtonLabel:SetColorByName( "ButtonDisabledCS" );
  194.         end
  195.     end
  196. end
  197.  
  198. -- ===========================================================================
  199. function OnLANGame()
  200.     LuaEvents.ChangeMPLobbyMode(MPLobbyTypes.STANDARD_LAN);
  201.     UIManager:QueuePopup( Controls.Lobby, PopupPriority.Current );
  202.     Close();
  203. end
  204.  
  205. -- ===========================================================================
  206. function OnHotSeat()
  207.     LuaEvents.ChangeMPLobbyMode(MPLobbyTypes.HOTSEAT);
  208.     LuaEvents.MainMenu_RaiseHostGame();
  209.     Close();
  210. end
  211.  
  212. -- ===========================================================================
  213. function OnCloud()
  214.     UIManager:QueuePopup( Controls.CloudGameScreen, PopupPriority.Current );
  215. end
  216.  
  217. -- ===========================================================================
  218. function OnGameLaunched()  
  219. end
  220.  
  221. -- ===========================================================================
  222. -- ESC handler
  223. -- ===========================================================================
  224. function InputHandler( uiMsg, wParam, lParam )
  225.     if uiMsg == KeyEvents.KeyUp then
  226.         if wParam == Keys.VK_ESCAPE then
  227.             if(m_kPopupDialog ~= nil) then
  228.                 if(m_kPopupDialog:IsOpen()) then
  229.                     m_kPopupDialog:Close();
  230.                 end
  231.             end
  232.         end
  233.         return true;
  234.     end
  235. end
  236.  
  237. -- ===========================================================================
  238. function Close()
  239.     -- Set pause to 0 so it loads in right away when returning from any screen.
  240.     m_initialPause = 0;
  241. end
  242.  
  243.  
  244. --[[
  245. --UINETTODO - Do we need this so that multiplayer game invites skip straight into the invited game?
  246. -------------------------------------------------
  247. -------------------------------------------------
  248. -- The UI has requested that we go to the multiplayer select.  Show ourself
  249. function OnUpdateUI( type, tag, iData1, iData2, strData1 )
  250.     if (type == SystemUpdateUI.RestoreUI and tag == "MultiplayerSelect") then
  251.         if (ContextPtr:IsHidden()) then
  252.             UIManager:QueuePopup(ContextPtr, PopupPriority.Current );    
  253.         end
  254.     end
  255. end
  256. Events.SystemUpdateUI.Add( OnUpdateUI );
  257. --]]
  258.  
  259. -- ===========================================================================
  260. --  ToggleOption - called from button handlers
  261. -- ===========================================================================
  262. --  Toggles the specified index within the main menu
  263. --  ARG0: optionIndex - the index of the button control to deselect
  264. --  ARG1: submenu - if the specified index has a submenu, then build that menu
  265. -- ===========================================================================
  266. function ToggleOption(optionIndex, submenu)
  267.     if (not Controls.SubMenuSlide:IsStopped()) then
  268.         return;
  269.     end
  270.     local optionControl = m_currentOptions[optionIndex].control;
  271.     if(m_currentOptions[optionIndex].isSelected) then
  272.         -- If the thing I selected was already selected, then toggle it off
  273.         UI.PlaySound("Main_Main_Panel_Collapse");
  274.         --Controls.SubMenuContainer:SetHide(true);
  275.         Controls.SubMenuAlpha:Reverse();
  276.         Controls.SubMenuSlide:Reverse();
  277.         DeselectOption(optionIndex);
  278.     else
  279.         -- OTHERWISE - I am selecting a new thing
  280.         -- Was anything else OTHER than the optionIndex selected?  If so, we should hide its selection fanciness and turn it off
  281.         -- Let's also check to see if the submenu was already open
  282.         local subMenuClosed = true;
  283.         for i=1, table.count(m_currentOptions) do
  284.             if (i ~= optionIndex) then
  285.                 if(m_currentOptions[i].isSelected) then
  286.                     subMenuClosed = false;
  287.                     DeselectOption(i);
  288.                 end
  289.             end
  290.         end
  291.        
  292.         if(subMenuClosed) then
  293.             --If the submenu wasn't opened yet, then let's slide it out
  294.             Controls.SubMenuAlpha:SetToBeginning();
  295.             Controls.SubMenuAlpha:Play();
  296.             Controls.SubMenuSlide:SetToBeginning();
  297.             Controls.SubMenuSlide:Play();
  298.         end
  299.         -- Now show the selector around the new thing
  300.         optionControl.SelectionAnimAlpha:SetToBeginning();
  301.         optionControl.SelectionAnimSlide:SetToBeginning();
  302.         optionControl.SelectionAnimAlpha:Play();
  303.         optionControl.SelectionAnimSlide:Play();
  304.         optionControl.LabelAlphaAnim:SetPauseTime(0);
  305.         optionControl.LabelAlphaAnim:SetSpeed(6);
  306.         optionControl.LabelAlphaAnim:Reverse();
  307.         if (submenu ~= nil) then
  308.             BuildSubMenu(submenu);
  309.         end
  310.         m_currentOptions[optionIndex].isSelected = true;
  311.     end
  312. end
  313.  
  314. -- ===========================================================================
  315. --  Called from ToggleOption
  316. --  Visually deselects the specified index and tracks within m_currentOptions
  317. --  ARG0:   index - the index of the button control to deselect
  318. -- ===========================================================================
  319. function DeselectOption(index:number)
  320.     local control:table = m_currentOptions[index].control;
  321.     control.LabelAlphaAnim:SetSpeed(1);
  322.     control.LabelAlphaAnim:SetPauseTime(.4);
  323.     control.SelectionAnimAlpha:Reverse();
  324.     control.SelectionAnimSlide:Reverse();
  325.     control.LabelAlphaAnim:SetToBeginning();
  326.     control.LabelAlphaAnim:Play();
  327.     m_currentOptions[index].isSelected = false;
  328. end
  329.  
  330. -- ===========================================================================
  331. function OnTutorial()
  332.     GameConfiguration.SetToDefaults();
  333.     UIManager:QueuePopup(Controls.TutorialSetup, PopupPriority.Current);
  334. end
  335.  
  336.  
  337. -- ===========================================================================
  338. --  Callbacks for the main menu options which have submenus
  339. --  ARG0:   optionIndex - which index of the current options to toggle
  340. --  ARG1:   submenu - the submenu table to draw in
  341. -- ===========================================================================
  342. function OnSinglePlayer( optionIndex:number, submenu:table )   
  343.     ToggleOption(optionIndex, submenu);
  344. end
  345.  
  346. function OnMultiPlayer( optionIndex:number, submenu:table )
  347.     ToggleOption(optionIndex, submenu);
  348. end
  349.  
  350.  
  351.  
  352. -- *******************************************************************************
  353. --  MENUS need to be defined here as the callbacks reference functions which
  354. --  are defined above.
  355. -- *******************************************************************************
  356.  
  357.  
  358. -- ===============================================================================
  359. -- Sub Menu Option Tables
  360. --  --------------------------------------------------------------------------
  361. --  label - the text string for the button (un-localized)
  362. --  callback - the function to call from this button
  363. --  tooltip - the tooltip for this button
  364. --  buttonState - a function to call which will update the buttonstate and tooltip
  365. -- ===============================================================================
  366. local m_SinglePlayerSubMenu :table = {
  367.                                 {label = "LOC_MAIN_MENU_RESUME_GAME",       callback = OnResumeGame,    tooltip = "LOC_MAINMENU_RESUME_GAME_TT", buttonState = UpdateResumeGame},
  368.                                 {label = "LOC_LOAD_GAME",                   callback = OnLoadSinglePlayer,  tooltip = "LOC_MAINMENU_LOAD_GAME_TT",},
  369.                                 {label = "LOC_PLAY_CIVILIZATION_6",         callback = OnPlayCiv6,  tooltip = "LOC_MAINMENU_PLAY_NOW_TT"},
  370.                                 {label = "LOC_SETUP_CREATE_GAME",           callback = OnAdvancedSetup, tooltip = "LOC_MAINMENU_CREATE_GAME_TT"},
  371.                             };
  372.  
  373. local m_MultiPlayerSubMenu :table = {
  374.                                 {label = "LOC_MULTIPLAYER_INTERNET_GAME",   callback = OnInternet,  tooltip = "LOC_MULTIPLAYER_INTERNET_GAME_TT", buttonState = UpdateInternetButton},
  375.                                 {label = "LOC_MULTIPLAYER_LAN_GAME",        callback = OnLANGame,   tooltip = "LOC_MULTIPLAYER_LAN_GAME_TT"},
  376.                                 {label = "LOC_MULTIPLAYER_HOTSEAT_GAME",    callback = OnHotSeat,   tooltip = "LOC_MULTIPLAYER_HOTSEAT_GAME_TT"},
  377.                                 --{label = "LOC_MULTIPLAYER_CLOUD_GAME",        callback = OnCloud,     tooltip = "LOC_MULTIPLAYER_CLOUD_GAME_TT"},
  378.                             };
  379.  
  380. -- ===========================================================================
  381. --  Main Menu Option Tables
  382. --  --------------------------------------------------------------------------
  383. --  label - the text string for the button (un-localized)
  384. --  callback - the function to call from this button
  385. --  submenu - the submenu table to open for this button (defined above)
  386. -- ===========================================================================
  387. local m_preSaveMainMenuOptions :table = {   {label = "LOC_PLAY_CIVILIZATION_6",         callback = OnPlayCiv6}};
  388. local m_defaultMainMenuOptions :table = {  
  389.                                 {label = "LOC_SINGLE_PLAYER",               callback = OnSinglePlayer,  tooltip = "LOC_MAINMENU_SINGLE_PLAYER_TT",  submenu = m_SinglePlayerSubMenu},
  390.                                 {label = "LOC_PLAY_MULTIPLAYER",            callback = OnMultiPlayer,   tooltip = "LOC_MAINMENU_MULTIPLAYER_TT",    submenu = m_MultiPlayerSubMenu},
  391.                                 {label = "LOC_MAIN_MENU_OPTIONS",           callback = OnOptions,   tooltip = "LOC_MAINMENU_GAME_OPTIONS_TT"},
  392.                                 {label = "LOC_MAIN_MENU_ADDITIONAL_CONTENT",                callback = OnMods,  tooltip = "LOC_MAIN_MENU_ADDITIONAL_CONTENT_TT"},
  393.                                 {label = "LOC_MAIN_MENU_TUTORIAL",          callback = OnTutorial,  tooltip = "LOC_MAINMENU_TUTORIAL_TT"},
  394.                                 {label = "LOC_MAIN_MENU_BENCH",             callback = OnBenchmark, tooltip = "LOC_MAINMENU_BENCHMARK_TT"},
  395.                                 {label = "LOC_MAIN_MENU_CREDITS",           callback = OnCredits,   tooltip = "LOC_MAINMENU_CREDITS_TT"},
  396.                                 {label = "LOC_MAIN_MENU_CIVILOPEDIA",       callback = OnCivilopedia,   tooltip = "LOC_MAINMENU_CIVILOPEDIA_TT"},
  397.                                 {label = "LOC_MAIN_MENU_EXIT_TO_DESKTOP",   callback = OnExitToDesktop, tooltip = "LOC_MAINMENU_EXIT_GAME_TT"}
  398.                             };
  399.  
  400.  
  401. -- ===========================================================================
  402. --  Animation callback for top-menu option controls.
  403. -- ===========================================================================
  404. function TopMenuOptionAnimationCallback(control, progress)
  405.     local progress :number = control:GetProgress();
  406.                                                    
  407.     -- Only if the animation has just begun, play its sound
  408.     if(not control:IsReversing() and progress <.1) then
  409.         UI.PlaySound("Main_Menu_Expand_Notch");            
  410.     elseif(not control:IsReversing() and progress >.65) then
  411.         control:SetSpeed(.9);   -- As the flag is nearing the top of its bounce, slow it down
  412.     end                                                
  413.                                                    
  414.     -- After the flag animation has bounced, stop it at the correct position                                                   
  415.     if(control:IsReversing() and progress > .2) then
  416.         control:SetProgress( 0.2 );
  417.         control:Stop();                                                                                                
  418.     elseif(control:IsReversing() and progress < .03) then
  419.         control:SetSpeed(.4);   -- Right after the flag animation has bounced, slow it down dramatically
  420.     end
  421. end
  422.  
  423. -- ===========================================================================
  424. --  Animation callback for sub-menu option controls.
  425. -- ===========================================================================
  426. function SubMenuOptionAnimationCallback(control, progress)
  427.     if(not control:IsReversing() and progress <.1) then
  428.         UI.PlaySound("Main_Menu_Panel_Expand_Short");
  429.     elseif(not control:IsReversing() and progress >.65) then
  430.         control:SetSpeed(2);
  431.     end
  432.     if(control:IsReversing() and progress > .2) then
  433.         control:SetProgress( 0.2 );
  434.         control:Stop();                                                    
  435.     elseif(control:IsReversing() and progress < .03) then
  436.         control:SetSpeed(1);
  437.     end
  438. end
  439.  
  440.  
  441. function MenuOptionMouseEnterCallback()
  442.     UI.PlaySound("Main_Menu_Mouse_Over");
  443. end
  444.  
  445. -- ===========================================================================
  446. --  Animates the main menu options in
  447. --  ARG0:   menuOptions - Expects the table of options that is to appear on
  448. --          the topmost level - either [m_preSave/m_default]MainMenuOptions
  449. -- ===========================================================================
  450. function BuildMenu(menuOptions:table)
  451.     m_mainOptionIM:ResetInstances();
  452.     UI.PlaySound("Main_Menu_Panel_Expand_Top_Level");  
  453.     local pauseAccumulator = m_initialPause + PAUSE_INCREMENT;
  454.     for i, menuOption in ipairs(menuOptions) do
  455.  
  456.         -- Add the instances to the table and play the animations and add the sounds
  457.         local option = m_mainOptionIM:GetInstance();
  458.         option.ButtonLabel:LocalizeAndSetText(menuOption.label);
  459.         option.SelectedLabel:LocalizeAndSetText(menuOption.label);
  460.         option.LabelAlphaAnim:SetToBeginning();
  461.         option.LabelAlphaAnim:Play();
  462.         -- The label begin its alpha animation slightly after the flag begins to fly out
  463.         option.LabelAlphaAnim:SetPauseTime(pauseAccumulator + .2);
  464.         option.OptionButton:RegisterCallback( Mouse.eLClick, function()
  465.                                                                 --If a submenu exists, specify the index and pass the submenu along to the callback
  466.                                                                 if (menuOption.submenu ~= nil) then
  467.                                                                     menuOption.callback(i, menuOption.submenu);
  468.                                                                 else  
  469.                                                                     menuOption.callback();
  470.                                                                 end
  471.                                                             end);
  472.         option.OptionButton:RegisterCallback( Mouse.eMouseEnter, MenuOptionMouseEnterCallback);
  473.  
  474.         -- Define a custom animation curve and sounds for the button flag - this function is called for every frame
  475.         option.FlagAnim:RegisterAnimCallback(TopMenuOptionAnimationCallback);
  476.         -- Will not be called due to "Bounce" cycle being used: option.FlagAnim:RegisterEndCallback( function() print("done!"); end );
  477.         option.FlagAnim:SetPauseTime(pauseAccumulator);
  478.         option.FlagAnim:SetSpeed(4);
  479.         option.FlagAnim:SetToBeginning();
  480.         option.FlagAnim:Play();
  481.  
  482.        
  483.         option.Top:LocalizeAndSetToolTip(menuOption.tooltip);
  484.        
  485.         -- Accumulate a pause so that the flags appear one at a time
  486.         pauseAccumulator = pauseAccumulator + PAUSE_INCREMENT;
  487.         -- Track which options are being displayed and preserve the selection state so that we can rebuild a submenu
  488.         m_currentOptions[i] = {control = option, isSelected = false};
  489.     end
  490.     Controls.MainMenuOptionStack:CalculateSize();
  491.     Controls.MainMenuOptionStack:ReprocessAnchoring();
  492.  
  493.  
  494.     local trackHeight = Controls.MainMenuOptionStack:GetSizeY() + TRACK_PADDING;
  495.     -- Make sure the vertical div line is correctly sized for the number of options and draw it in
  496.     Controls.MainButtonTrack:SetSizeY(trackHeight);
  497.     Controls.MainButtonTrackAnim:SetBeginVal(0,-trackHeight);
  498.     Controls.MainButtonTrackAnim:Play();
  499.     Controls.MainMenuClip:SetSizeY(trackHeight);
  500. end
  501.  
  502. -- ===========================================================================
  503. --  Builds the table of submenu options
  504. --  ARG0:   menuOptions - Expects the table specified in the 'submenu' field
  505. --          of the m_defaultMainMenuOptions table  
  506. --
  507. --  WB: While this function shares a fair amount of code with BuildMenu,
  508. --  I have decided to keep them separate as I continue differentiate behavior
  509. --  and tweak the animations.
  510. -- ===========================================================================
  511. function BuildSubMenu(menuOptions:table)
  512.     m_subOptionIM:ResetInstances();
  513.  
  514.     for i, menuOption in ipairs(menuOptions) do
  515.         -- Add the instances to the table and play the animations and add the sounds
  516.         -- * Submenu options animate in all at once, instead of one at at a time
  517.         local option = m_subOptionIM:GetInstance();
  518.         option.ButtonLabel:LocalizeAndSetText(menuOption.label);
  519.         option.SelectedLabel:LocalizeAndSetText(menuOption.label);
  520.         option.LabelAlphaAnim:SetToBeginning();
  521.         option.LabelAlphaAnim:Play();
  522.         option.LabelAlphaAnim:SetPauseTime(0);
  523.         option.OptionButton:RegisterCallback( Mouse.eLClick, menuOption.callback);
  524.         option.OptionButton:RegisterCallback( Mouse.eMouseEnter, MenuOptionMouseEnterCallback);
  525.  
  526.         -- * Submenu options have a slightly different animation curve as well as a different animation sound
  527.         option.FlagAnim:RegisterAnimCallback(SubMenuOptionAnimationCallback);
  528.  
  529.         -- Will not be called due to "Bounce" cycle being used: option.FlagAnim:RegisterEndCallback( function() print("done!"); end );
  530.         option.FlagAnim:SetSpeed(4);
  531.         option.FlagAnim:SetToBeginning();
  532.         option.FlagAnim:Play();
  533.  
  534.         option.Top:LocalizeAndSetToolTip(menuOption.tooltip);
  535.        
  536.         -- Set a special disabled state for buttons (right now, only the Internet button has this function)
  537.         if (menuOption.buttonState ~= nil) then
  538.             menuOption.buttonState(option);
  539.         else
  540.             --ATTN:TRON For some reason my instances are not being completely reset when I rebuild the my list here
  541.             -- So I have to reset my tooltip string and button state.
  542.             option.OptionButton:SetDisabled(false);
  543.             option.ButtonLabel:SetColorByName( "ButtonCS" );
  544.         end    
  545.     end
  546.  
  547.     Controls.SubMenuOptionStack:CalculateSize();
  548.     Controls.SubMenuOptionStack:ReprocessAnchoring();
  549.     local trackHeight = Controls.SubMenuOptionStack:GetSizeY() + TRACK_PADDING;
  550.     Controls.SubButtonTrack:SetSizeY(trackHeight);
  551.     Controls.SubButtonTrackAnim:SetBeginVal(0,-trackHeight);
  552.     -- * The track line for the submenu also draws in more quickly since all the options are feeding in at once
  553.     Controls.SubButtonTrackAnim:SetSpeed(5);
  554.     Controls.SubButtonTrackAnim:SetToBeginning();
  555.     Controls.SubButtonTrackAnim:Play();
  556.     Controls.SubMenuClip:SetSizeY(trackHeight);
  557.     Controls.SubMenuAlpha:SetSizeY(trackHeight);
  558.     Controls.SubMenuContainer:SetSizeY(Controls.MainMenuClip:GetSizeY());
  559. end
  560.  
  561.  
  562. -- =============================================================================
  563. --  Searches the menu table for a value which contains a matching [label]. If
  564. --  found, that index is removed
  565. --  ARG0:   menu - the parent menu table.  Expects options to have a name
  566. --          string in the [label] field to compare against
  567. --  ARG1:   option - the table containing both the [label] and [callback]
  568. --          for the submenu option
  569. -- =============================================================================
  570. function RemoveOptionFromMenu(menu:table, option:table)
  571.     for i=1, table.count(menu) do
  572.         if(menu[i] ~= nil) then
  573.             if(menu[i].label == option.label) then
  574.                 table.remove(menu,i);
  575.             end
  576.         end
  577.     end
  578. end
  579.  
  580. -- =============================================================================
  581. --  Searches the menu table for a value which contains a matching [label]. If
  582. --  that value is NOT found, the submenu option is inserted at the first index
  583. --  ARG0:   menu - the parent menu table.  Expects options to have a name
  584. --          string in the [label] field to compare against
  585. --  ARG1:   option - the table containing both the [label] and [callback]
  586. --          for the submenu option
  587. --  ARG2:   (OPTIONAL) index - the index of the submenu where the option should
  588. --          be inserted.
  589. -- =============================================================================
  590. function AddOptionToMenu(menu:table, option:table, index:number)
  591.     local hasOption = false;
  592.     if (index == nil) then
  593.         index = 1;
  594.     end
  595.     for i=1, table.count(menu) do
  596.         if(menu[i].label == option) then
  597.             hasOption = true;
  598.         end
  599.     end
  600.     if (not hasOption) then
  601.         table.insert(menu,submenu,1);
  602.     end
  603. end
  604.  
  605. -- =============================================================================
  606. --  Called from the ESC handler and also when we show the screen
  607. --  Rebuilds the menu taking into account any submenus that were already open
  608. -- =============================================================================
  609. function BuildAllMenus()
  610.  
  611.     -- Reset cached buttons to make sure we don't reference reused instances
  612.     m_resumeButton = nil;
  613.     m_internetButton = nil;
  614.  
  615.     -- WISHLIST: When we rebuild the menus, let's check to see if there are ANY saved games whatsoever.  
  616.     -- If none exist, then do not display the option in the submenu. (See: OnFileListQueryResults)
  617.     local selectedIndex = -1;
  618.     for i=1, table.count(m_currentOptions) do
  619.         if(m_currentOptions[i].isSelected) then
  620.             selectedIndex = i;
  621.         end
  622.     end
  623.     if(selectedIndex ~= -1) then
  624.         if(m_defaultMainMenuOptions[selectedIndex].submenu ~= nil) then
  625.             BuildSubMenu(m_defaultMainMenuOptions[selectedIndex].submenu);
  626.         else
  627.             BuildMenu(m_defaultMainMenuOptions);
  628.         end
  629.     else
  630.         BuildMenu(m_defaultMainMenuOptions);
  631.     end
  632. end
  633.  
  634. function Resize()
  635.     local screenX, screenY:number = UIManager:GetScreenSizeVal();
  636.     local adjustedWidth = screenY*1.9;
  637.     Controls.Logo:ReprocessAnchoring();
  638.     Controls.ShellMenuAndLogo:ReprocessAnchoring();
  639.         Controls.VersionLabel:ReprocessAnchoring();
  640.     Controls.ShellStack:ReprocessAnchoring();
  641.     Controls.My2KContents:ReprocessAnchoring();
  642. end
  643. -- ===========================================================================
  644. --  UI Callback
  645. --  Restart animation on show
  646. -- ===========================================================================
  647. function OnShow()
  648.     local save = Options.GetAppOption("Debug", "PlayNowSave");
  649.     if (save ~= nil) then
  650.         --If we have a save specified in AppOptions, then only display the play button
  651.         BuildMenu(m_preSaveMainMenuOptions);
  652.     else
  653.         BuildAllMenus();
  654.     end
  655.     GameConfiguration.SetToDefaults();
  656.     UI.SetSoundStateValue("Game_Views", "Main_Menu");
  657.     LuaEvents.UpdateFiraxisLiveState();
  658.  
  659.     Steam.SetRichPresence("location", "LOC_PRESENCE_IN_SHELL");
  660.  
  661.     local gameType = SaveTypes.SINGLE_PLAYER;
  662.     local saveLocation = SaveLocations.LOCAL_STORAGE;
  663.  
  664.     g_MostRecentSave = nil;
  665.     g_LastFileQueryRequestID = nil;
  666.     local options = SaveLocationOptions.NORMAL + SaveLocationOptions.AUTOSAVE + SaveLocationOptions.QUICKSAVE + SaveLocationOptions.MOST_RECENT_ONLY + SaveLocationOptions.LOAD_METADATA ;
  667.     g_LastFileQueryRequestID = UI.QuerySaveGameList( saveLocation, gameType, options );
  668. end
  669.  
  670. function OnHide()
  671.     -- Set the pause to 0 as soon as we hide the main menu, so it loads in right
  672.     -- away when we return from any screen.
  673.     m_initialPause = 0;
  674. end
  675.  
  676. function OnUpdateUI( type:number, tag:string, iData1:number, iData2:number, strData1:string )  
  677.   if type == SystemUpdateUI.ScreenResize then
  678.     Resize();
  679.   end
  680. end
  681.  
  682. -- Call-back for when the list of files have been updated.
  683. function OnFileListQueryResults( fileList, queryID )
  684.     if g_LastFileQueryRequestID ~= nil then
  685.         if (g_LastFileQueryRequestID == queryID) then
  686.             g_MostRecentSave = nil;
  687.             if (fileList ~= nil) then
  688.                 for i, v in ipairs(fileList) do
  689.                     g_MostRecentSave = v;       -- There really should only be one or
  690.                 end
  691.            
  692.                 UpdateResumeGame();
  693.             end
  694.  
  695.             UI.CloseFileListQuery(g_LastFileQueryRequestID);
  696.             g_LastFileQueryRequestID = nil;
  697.         end
  698.     end
  699.    
  700. end
  701.  
  702. -- ===========================================================================
  703. function Initialize()
  704.     m_kPopupDialog = PopupDialogLogic:new( "MainMenu", Controls.PopupDialog, Controls.StackContents );
  705.     m_kPopupDialog:SetInstanceNames( "PopupButtonInstance", "Button", "PopupTextInstance", "Text", "RowInstance", "Row");
  706.     m_kPopupDialog:SetOpenAnimationControls( Controls.PopupAlphaIn, Controls.PopupSlideIn );
  707.     m_kPopupDialog:SetSize(400,200);
  708.  
  709.     ContextPtr:SetShowHandler( OnShow );
  710.     ContextPtr:SetInputHandler( InputHandler );
  711.     Controls.VersionLabel:SetText( UI.GetAppVersion() );
  712.     Controls.My2KLogin:RegisterCallback( Mouse.eLClick, OnMy2KLogin );
  713.     Controls.My2KLogin:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
  714.  
  715.     -- Game Events
  716.     Events.SteamServersConnected.Add( UpdateInternetButton );
  717.     Events.SteamServersDisconnected.Add( UpdateInternetButton );
  718.     Events.MultiplayerGameLaunched.Add( OnGameLaunched );
  719.     Events.SystemUpdateUI.Add( OnUpdateUI );
  720.     Events.UserRequestClose.Add( OnExitToDesktop );
  721.  
  722.     -- LUA Events
  723.     LuaEvents.FileListQueryResults.Add( OnFileListQueryResults );
  724.  
  725.     BuildAllMenus();
  726.     Resize();
  727. end
  728. Initialize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement