Advertisement
Guest User

MinimapPanel.lua

a guest
Oct 23rd, 2016
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 30.95 KB | None | 0 0
  1. -- ===========================================================================
  2. --  MINIMAP PANEL
  3. -- ===========================================================================
  4. include( "InstanceManager" );
  5.  
  6.  
  7. -- ===========================================================================
  8. --  CONSTANTS
  9. -- ===========================================================================
  10. local MINIMAP_COLLAPSED_OFFSETY :number = -180;
  11.  
  12.  
  13. -- ===========================================================================
  14. --  MEMBERS
  15. -- ===========================================================================
  16. --local m_OptionsButtonManager= InstanceManager:new( "MiniMapOptionButtonInstance", "Top",      Controls.OptionsStack );
  17. local m_OptionButtons           :table = {};    -- option buttons indexed by buttonName.
  18. local iZoomIncrement            :number = 2;
  19. local m_isCollapsed             :boolean= false;
  20. local bGridOn                   :boolean= true;
  21. local m_ContinentsCreated       :boolean=false;
  22. local m_MiniMap_xmloffsety      :number = 0;
  23. local m_ContinentsCache         :table = {};
  24. local m_kFlyoutControlIds       :table = { "MapOptions", "Lens", "MapPinList"}; -- Name of controls that are the backing for "flyout" menus.
  25.  
  26. local m_shouldCloseLensMenu           :boolean = true;    -- Controls when the Lens menu should be closed.
  27.  
  28. local m_LensLayers              :table = {  LensLayers.HEX_COLORING_RELIGION,
  29.                                             LensLayers.HEX_COLORING_CONTINENT,
  30.                                             LensLayers.HEX_COLORING_APPEAL_LEVEL,
  31.                                             LensLayers.HEX_COLORING_GOVERNMENT,
  32.                                             LensLayers.HEX_COLORING_OWING_CIV,
  33.                                             LensLayers.HEX_COLORING_WATER_AVAILABLITY   };
  34.  
  35. local m_ToggleReligionLensId    = Input.GetActionId("LensReligion");
  36. local m_ToggleContinentLensId   = Input.GetActionId("LensContinent");
  37. local m_ToggleAppealLensId      = Input.GetActionId("LensAppeal");
  38. local m_ToggleSettlerLensId     = Input.GetActionId("LensSettler");
  39. local m_ToggleGovernmentLensId  = Input.GetActionId("LensGovernment");
  40. local m_TogglePoliticalLensId   = Input.GetActionId("LensPolitical");
  41.  
  42.  
  43. local m_isMouseDragEnabled      :boolean = true; -- Can the camera be moved by dragging on the minimap?
  44. local m_isMouseDragging         :boolean = false; -- Was LMB clicked inside the minimap, and has not been released yet?
  45. local m_hasMouseDragged         :boolean = false; -- Has there been any movements since m_isMouseDragging became true?
  46. local m_wasMouseInMinimap       :boolean = false; -- Was the mouse over the minimap the last time we checked?
  47.  
  48. -- ===========================================================================
  49. --  FUNCTIONS
  50. -- ===========================================================================
  51.  
  52. -- ===========================================================================
  53. function GetContinentsCache()
  54.     if m_ContinentsCache == nil then
  55.         m_ContinentsCache = Map.GetContinentsInUse();
  56.     end
  57. end
  58.  
  59. -- ===========================================================================
  60. function OnZoomIn()
  61.     UI.ZoomMap( iZoomIncrement );
  62. end
  63.  
  64. -- ===========================================================================
  65. function OnZoomOut()
  66.     UI.ZoomMap( -iZoomIncrement );
  67. end
  68.  
  69. -- ===========================================================================
  70. function CloseAllFlyouts()
  71.     for _,id in ipairs(m_kFlyoutControlIds) do
  72.         local panelId = id.."Panel";        -- e.g LenPanel, MapOptionPanel, etc...
  73.         local buttonId = id.."Button";
  74.         if Controls[panelId] ~= nil then
  75.             Controls[panelId]:SetHide( true ); 
  76.         else
  77.             UI.DataError("Minimap's CloseAllFlyouts() attempted to close '"..panelId.."' but the control doesn't exist in the XML.");
  78.         end    
  79.         if Controls[buttonId] ~= nil then
  80.             Controls[buttonId]:SetSelected( false );   
  81.         else
  82.             UI.DataError("Minimap's CloseAllFlyouts() attempted to unselect'"..buttonId.."' but the control doesn't exist in the XML.");
  83.         end    
  84.  
  85.  
  86.     end
  87. end
  88.  
  89. -- ===========================================================================
  90. --  Only show one "flyout" control at a time.
  91. -- ===========================================================================
  92. function RealizeFlyouts( pControl:table )  
  93.     if pControl:IsHidden() then
  94.         return;     -- If target control is hidden, ignore the rest.
  95.     end
  96.     for _,id in ipairs(m_kFlyoutControlIds) do
  97.         local panelId = id.."Panel";        -- e.g LenPanel, MapOptionPanel, etc...
  98.         local buttonId = id.."Button";
  99.         if Controls[panelId] ~= nil then
  100.             if Controls[panelId] ~= pControl and Controls[panelId]:IsHidden()==false then
  101.                 Controls[panelId]:SetHide( true );
  102.             end
  103.             if Controls[panelId] ~= pControl then
  104.                 if Controls[buttonId]:IsSelected() then
  105.                     Controls[buttonId]:SetSelected( false );
  106.                 end
  107.             else
  108.                 if not Controls[buttonId]:IsSelected() then
  109.                     Controls[buttonId]:SetSelected( true );
  110.                 end
  111.             end
  112.         else
  113.             UI.DataError("Minimap's RealizeFlyouts() attempted to close '"..panelId.."' but the control doesn't exist in the XML.");
  114.         end    
  115.     end
  116. end
  117.  
  118. -- ===========================================================================
  119. function ToggleMapOptionsList()
  120.     Controls.MapOptionsPanel:SetHide( not Controls.MapOptionsPanel:IsHidden() );
  121.     RealizeFlyouts(Controls.MapOptionsPanel);
  122.     Controls.MapOptionsButton:SetSelected( not Controls.MapOptionsPanel:IsHidden() );
  123. end
  124.  
  125. -- ===========================================================================
  126. function OnToggleLensList()
  127.     Controls.LensPanel:SetHide( not Controls.LensPanel:IsHidden() );
  128.     RealizeFlyouts(Controls.LensPanel);
  129.     Controls.LensButton:SetSelected( not Controls.LensPanel:IsHidden() );
  130.     if Controls.LensPanel:IsHidden() then
  131.         m_shouldCloseLensMenu = true;
  132.         Controls.ReligionLensButton:SetCheck(false);
  133.         Controls.ContinentLensButton:SetCheck(false);  
  134.         Controls.AppealLensButton:SetCheck(false);
  135.         Controls.GovernmentLensButton:SetCheck(false);
  136.         Controls.WaterLensButton:SetCheck(false);
  137.         Controls.OwnerLensButton:SetCheck(false);
  138.         Controls.TourismLensButton:SetCheck(false);
  139.         if UI.GetInterfaceMode() == InterfaceModeTypes.VIEW_MODAL_LENS then
  140.             UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
  141.         end
  142.     end
  143. end
  144.  
  145. ------------------------------------------------------------------------------
  146. function ToggleMapPinMode()
  147.     Controls.MapPinListPanel:SetHide( not Controls.MapPinListPanel:IsHidden() );
  148.     RealizeFlyouts(Controls.MapPinListPanel);
  149.     Controls.MapPinListButton:SetSelected( not Controls.MapPinListPanel:IsHidden() );
  150. end
  151.  
  152. -- ===========================================================================
  153. function ToggleResourceIcons()
  154.     local showMapResources:boolean = not UserConfiguration.ShowMapResources();
  155.     UserConfiguration.ShowMapResources( showMapResources );
  156.     if showMapResources then
  157.         LuaEvents.MinimapPanel_ShowYieldIcons();
  158.     else
  159.         LuaEvents.MinimapPanel_HideYieldIcons();
  160.     end
  161. end
  162.  
  163. -- ===========================================================================
  164. function ToggleYieldIcons()
  165.     local showMapYield:boolean = not UserConfiguration.ShowMapYield();
  166.     UserConfiguration.ShowMapYield( showMapYield );
  167.     if showMapYield then
  168.         LuaEvents.MinimapPanel_ShowYieldIcons();
  169.     else
  170.         LuaEvents.MinimapPanel_HideYieldIcons();
  171.     end
  172. end
  173.  
  174. -- ===========================================================================
  175. function ToggleReligionLens()
  176.     if Controls.ReligionLensButton:IsChecked() then
  177.         UILens.SetActive("Religion");
  178.         RefreshInterfaceMode();
  179.     else
  180.         m_shouldCloseLensMenu = false; --When toggling the lens off, shouldn't close the menu.
  181.         if UI.GetInterfaceMode() == InterfaceModeTypes.VIEW_MODAL_LENS then
  182.             UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
  183.         end
  184.     end
  185. end
  186.  
  187. -- ===========================================================================
  188. function ToggleContinentLens()
  189.     if Controls.ContinentLensButton:IsChecked() then
  190.         UILens.SetActive("Continent");
  191.         RefreshInterfaceMode();
  192.     else
  193.         m_shouldCloseLensMenu = false;
  194.         if UI.GetInterfaceMode() == InterfaceModeTypes.VIEW_MODAL_LENS then
  195.             UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
  196.         end
  197.     end    
  198. end
  199.  
  200. -- ===========================================================================
  201. function ToggleAppealLens()
  202.     if Controls.AppealLensButton:IsChecked() then
  203.         UILens.SetActive("Appeal");
  204.         RefreshInterfaceMode();
  205.     else
  206.         m_shouldCloseLensMenu = false;
  207.         if UI.GetInterfaceMode() == InterfaceModeTypes.VIEW_MODAL_LENS then
  208.             UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
  209.         end
  210.     end
  211. end
  212.  
  213. -- ===========================================================================
  214. function ToggleWaterLens()
  215.     if Controls.WaterLensButton:IsChecked() then
  216.         UILens.SetActive("WaterAvailability");
  217.         RefreshInterfaceMode();
  218.     else
  219.         m_shouldCloseLensMenu = false;
  220.         if UI.GetInterfaceMode() == InterfaceModeTypes.VIEW_MODAL_LENS then
  221.             UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
  222.         end
  223.     end
  224. end
  225.  
  226. -- ===========================================================================
  227. function ToggleGovernmentLens()
  228.     if Controls.GovernmentLensButton:IsChecked() then
  229.         UILens.SetActive("Government");
  230.         RefreshInterfaceMode();
  231.     else
  232.         m_shouldCloseLensMenu = false;
  233.         if UI.GetInterfaceMode() == InterfaceModeTypes.VIEW_MODAL_LENS then
  234.             UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
  235.         end
  236.     end
  237. end
  238.  
  239. -- ===========================================================================
  240. function ToggleOwnerLens()
  241.     if Controls.OwnerLensButton:IsChecked() then
  242.         UILens.SetActive("OwningCiv");
  243.         RefreshInterfaceMode();
  244.     else
  245.         m_shouldCloseLensMenu = false;
  246.         if UI.GetInterfaceMode() == InterfaceModeTypes.VIEW_MODAL_LENS then
  247.             UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
  248.         end
  249.     end
  250. end
  251.  
  252. -- ===========================================================================
  253. function ToggleTourismLens()
  254.     if Controls.TourismLensButton:IsChecked() then
  255.         UILens.SetActive("Tourism");
  256.         RefreshInterfaceMode();
  257.     else
  258.         m_shouldCloseLensMenu = false;
  259.         if UI.GetInterfaceMode() == InterfaceModeTypes.VIEW_MODAL_LENS then
  260.             UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
  261.         end
  262.     end
  263. end
  264.  
  265. -- ===========================================================================
  266. function ToggleGrid()
  267.     bGridOn = not bGridOn;
  268.     UI.ToggleGrid( bGridOn );
  269. end
  270.  
  271. -- ===========================================================================
  272. function Toggle2DView()
  273.     if (UserConfiguration.GetValue("RenderViewIsLocked") ~= true) then
  274.         if (UI.GetWorldRenderView() == WorldRenderView.VIEW_2D) then
  275.             UI.SetWorldRenderView( WorldRenderView.VIEW_3D );
  276.             Controls.SwitcherImage:SetTextureOffsetVal(0,0);
  277.             UI.PlaySound("Set_View_3D");
  278.         else
  279.             UI.SetWorldRenderView( WorldRenderView.VIEW_2D );
  280.             Controls.SwitcherImage:SetTextureOffsetVal(0,24);
  281.             UI.PlaySound("Set_View_2D");
  282.         end
  283.         UI.PlaySound("Stop_Unit_Movement_Master");
  284.     end
  285.    
  286. end
  287.  
  288. -- ===========================================================================
  289. function OnPauseEnd()
  290.     Controls.ExpandAnim:SetToBeginning();  
  291. end
  292.  
  293. -- ===========================================================================
  294. function OnCollapseToggle()
  295.     if ( m_isCollapsed ) then
  296.         UI.PlaySound("Minimap_Open");
  297.         Controls.ExpandButton:SetHide( true );
  298.         Controls.ExpandAnim:SetEndVal(0, -Controls.MinimapImage:GetOffsetY() - Controls.MinimapImage:GetSizeY());
  299.         Controls.ExpandAnim:SetToBeginning();
  300.         Controls.ExpandAnim:Play();
  301.         Controls.CompassArm:SetPercent(.25);
  302.     else
  303.         UI.PlaySound("Minimap_Closed");
  304.         Controls.ExpandButton:SetHide( false );
  305.         Controls.Pause:Play();
  306.         Controls.CollapseAnim:SetEndVal(0, Controls.MinimapImage:GetOffsetY() + Controls.MinimapImage:GetSizeY());
  307.         Controls.CollapseAnim:SetToBeginning();
  308.         Controls.CollapseAnim:Play();
  309.         Controls.CompassArm:SetPercent(.5);
  310.     end
  311.     m_isCollapsed = not m_isCollapsed;
  312. end
  313.  
  314. -- ===========================================================================
  315. function RefreshInterfaceMode()
  316.     if UI.GetInterfaceMode() ~= InterfaceModeTypes.VIEW_MODAL_LENS then
  317.         UI.SetInterfaceMode(InterfaceModeTypes.VIEW_MODAL_LENS);
  318.     end
  319. end
  320.  
  321. -- ===========================================================================
  322. function OnLensLayerOn( layerNum:number )      
  323.     if layerNum == LensLayers.HEX_COLORING_RELIGION then
  324.         UI.PlaySound("UI_Lens_Overlay_On");
  325.     elseif layerNum == LensLayers.HEX_COLORING_APPEAL_LEVEL then
  326.         SetAppealHexes();
  327.         UI.PlaySound("UI_Lens_Overlay_On");
  328.     elseif layerNum == LensLayers.HEX_COLORING_GOVERNMENT then
  329.         SetGovernmentHexes();
  330.         UI.PlaySound("UI_Lens_Overlay_On");
  331.     elseif layerNum == LensLayers.HEX_COLORING_OWING_CIV then
  332.         SetOwingCivHexes();
  333.         UI.PlaySound("UI_Lens_Overlay_On");
  334.     elseif layerNum == LensLayers.HEX_COLORING_CONTINENT then
  335.         SetContinentHexes();
  336.         UI.PlaySound("UI_Lens_Overlay_On");
  337.     elseif layerNum == LensLayers.HEX_COLORING_WATER_AVAILABLITY then
  338.         SetWaterHexes();
  339.         UI.PlaySound("UI_Lens_Overlay_On");
  340.     elseif layerNum == LensLayers.TOURIST_TOKENS then
  341.         UI.PlaySound("UI_Lens_Overlay_On");
  342.     end
  343. end
  344.  
  345. -- ===========================================================================
  346. function OnLensLayerOff( layerNum:number )
  347.     if (layerNum == LensLayers.HEX_COLORING_RELIGION        or
  348.             layerNum == LensLayers.HEX_COLORING_CONTINENT       or
  349.             layerNum == LensLayers.HEX_COLORING_APPEAL_LEVEL    or
  350.             layerNum == LensLayers.HEX_COLORING_GOVERNMENT      or
  351.             layerNum == LensLayers.HEX_COLORING_OWING_CIV)      then
  352.         UI.PlaySound("UI_Lens_Overlay_Off");
  353.     elseif layerNum == LensLayers.HEX_COLORING_WATER_AVAILABLITY then
  354.         -- Only clear the water lens if we're turning off lenses altogether, but not if switching to another modal lens (Turning on another modal lens clears it already).
  355.         if UI.GetInterfaceMode() ~= InterfaceModeTypes.VIEW_MODAL_LENS or (UI.GetHeadSelectedUnit() == nil) then
  356.             UILens.ClearLayerHexes(LensLayers.HEX_COLORING_WATER_AVAILABLITY);
  357.         end
  358.         UI.PlaySound("UI_Lens_Overlay_Off");
  359.     end
  360. end
  361.  
  362. -- ===========================================================================
  363. function OnToggleContinentLensExternal()
  364.     if Controls.LensPanel:IsHidden() then
  365.         Controls.LensPanel:SetHide(false);
  366.         RealizeFlyouts(Controls.LensPanel);
  367.         Controls.LensButton:SetSelected(true); 
  368.     end
  369.     if not Controls.ContinentLensButton:IsChecked() then
  370.         Controls.ContinentLensButton:SetCheck(true);
  371.         UILens.SetActive("Continent");
  372.         RefreshInterfaceMode();
  373.     end
  374. end
  375.  
  376. -- ===========================================================================
  377. --  Engine EVENT
  378. --  Local player changed; likely a hotseat game
  379. -- ===========================================================================
  380. function OnLocalPlayerChanged( eLocalPlayer:number , ePrevLocalPlayer:number )
  381.     if eLocalPlayer == -1 then
  382.         return;
  383.     end
  384.     CloseAllFlyouts();
  385. end
  386.  
  387. -- ===========================================================================
  388. function SetOwingCivHexes()
  389.     local localPlayer : number = Game.GetLocalPlayer();
  390.     local localPlayerVis:table = PlayersVisibility[localPlayer];
  391.     if (localPlayerVis ~= nil) then
  392.         local players = Game.GetPlayers();
  393.         for i, player in ipairs(players) do
  394.             local cities = players[i]:GetCities();
  395.             local primaryColor, secondaryColor = UI.GetPlayerColors( player:GetID() );
  396.        
  397.             for _, pCity in cities:Members() do
  398.                 local visibleCityPlots  :table = Map.GetCityPlots():GetVisiblePurchasedPlots(pCity);
  399.  
  400.                 if(table.count(visibleCityPlots) > 0) then
  401.                     UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_OWING_CIV, localPlayer, visibleCityPlots, primaryColor );
  402.                 end
  403.             end
  404.         end
  405.     end
  406. end
  407.  
  408. -- ===========================================================================
  409. function SetWaterHexes()
  410.     local FullWaterPlots:table = {};
  411.     local CoastalWaterPlots:table = {};
  412.     local NoWaterPlots:table = {};
  413.     local NoSettlePlots:table = {};
  414.  
  415.     UILens.ClearLayerHexes(LensLayers.HEX_COLORING_WATER_AVAILABLITY);
  416.     FullWaterPlots, CoastalWaterPlots, NoWaterPlots, NoSettlePlots = Map.GetContinentPlotsWaterAvailability();
  417.  
  418.     local BreathtakingColor :number = UI.GetColorValue("COLOR_BREATHTAKING_APPEAL");
  419.     local CharmingColor     :number = UI.GetColorValue("COLOR_CHARMING_APPEAL");
  420.     local AverageColor      :number = UI.GetColorValue("COLOR_AVERAGE_APPEAL");
  421.     local DisgustingColor   :number = UI.GetColorValue("COLOR_DISGUSTING_APPEAL");
  422.     local localPlayer       :number = Game.GetLocalPlayer();
  423.  
  424.     if(table.count(FullWaterPlots) > 0) then
  425.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_WATER_AVAILABLITY, localPlayer, FullWaterPlots, BreathtakingColor );
  426.     end
  427.     if(table.count(CoastalWaterPlots) > 0) then
  428.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_WATER_AVAILABLITY, localPlayer, CoastalWaterPlots, CharmingColor );
  429.     end
  430.     if(table.count(NoWaterPlots) > 0) then
  431.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_WATER_AVAILABLITY, localPlayer, NoWaterPlots, AverageColor );
  432.     end
  433.     if(table.count(NoSettlePlots) > 0) then
  434.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_WATER_AVAILABLITY, localPlayer, NoSettlePlots, DisgustingColor );
  435.     end
  436. end
  437.  
  438. -- ===========================================================================
  439. function SetGovernmentHexes()
  440.     local localPlayer : number = Game.GetLocalPlayer();
  441.     local localPlayerVis:table = PlayersVisibility[localPlayer];
  442.     if (localPlayerVis ~= nil) then
  443.         local players = Game.GetPlayers();
  444.         for i, player in ipairs(players) do
  445.             local cities = players[i]:GetCities();
  446.             local culture = player:GetCulture();
  447.             local governmentId :number = culture:GetCurrentGovernment();
  448.             local GovernmentColor;
  449.             if(governmentId < 0) then
  450.                 GovernmentColor = UI.GetColorValue("COLOR_GOVERNMENT_CITYSTATE");
  451.             else
  452.                 GovernmentColor = UI.GetColorValue("COLOR_" ..  GameInfo.Governments[governmentId].GovernmentType);
  453.             end
  454.            
  455.  
  456.             for _, pCity in cities:Members() do
  457.                 local visibleCityPlots:table = Map.GetCityPlots():GetVisiblePurchasedPlots(pCity);
  458.            
  459.                 if(table.count(visibleCityPlots) > 0) then
  460.                     UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_GOVERNMENT, localPlayer, visibleCityPlots, GovernmentColor );
  461.                 end
  462.             end
  463.         end
  464.     end
  465. end
  466.  
  467. -- ===========================================================================
  468. function SetAppealHexes()
  469.     local BreathtakingPlots:table = {};
  470.     local CharmingPlots:table = {};
  471.     local AveragePlots:table = {};
  472.     local UninvitingPlots:table = {};
  473.     local DisgustingPlots:table = {};
  474.  
  475.     BreathtakingPlots, CharmingPlots, AveragePlots, UninvitingPlots, DisgustingPlots = Map.GetContinentPlotsAppeal();
  476.  
  477.     local BreathtakingColor :number = UI.GetColorValue("COLOR_BREATHTAKING_APPEAL");
  478.     local CharmingColor     :number = UI.GetColorValue("COLOR_CHARMING_APPEAL");
  479.     local AverageColor      :number = UI.GetColorValue("COLOR_AVERAGE_APPEAL");
  480.     local UninvitingColor   :number = UI.GetColorValue("COLOR_UNINVITING_APPEAL");
  481.     local DisgustingColor   :number = UI.GetColorValue("COLOR_DISGUSTING_APPEAL");
  482.     local localPlayer       :number = Game.GetLocalPlayer();
  483.  
  484.     if(table.count(BreathtakingPlots) > 0) then
  485.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_APPEAL_LEVEL, localPlayer, BreathtakingPlots, BreathtakingColor );
  486.     end
  487.     if(table.count(CharmingPlots) > 0) then
  488.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_APPEAL_LEVEL, localPlayer, CharmingPlots, CharmingColor );
  489.     end
  490.     if(table.count(AveragePlots) > 0) then
  491.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_APPEAL_LEVEL, localPlayer, AveragePlots, AverageColor );
  492.     end
  493.     if(table.count(UninvitingPlots) > 0) then
  494.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_APPEAL_LEVEL, localPlayer, UninvitingPlots, UninvitingColor );
  495.     end
  496.     if(table.count(DisgustingPlots) > 0) then
  497.         UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_APPEAL_LEVEL, localPlayer, DisgustingPlots, DisgustingColor );
  498.     end
  499.  
  500. end
  501.  
  502. -- ===========================================================================
  503. function SetContinentHexes()
  504.     local ContinentColor:number = 0x02000000;
  505.     GetContinentsCache();
  506.     local localPlayerVis:table = PlayersVisibility[Game.GetLocalPlayer()];
  507.     if (localPlayerVis ~= nil) then
  508.        
  509.         local kContinentColors:table = {};
  510.         for loopNum, ContinentID in ipairs(m_ContinentsCache) do
  511.             local visibleContinentPlots:table = Map.GetVisibleContinentPlots(ContinentID);
  512.             ContinentColor = UI.GetColorValue("COLOR_" .. GameInfo.Continents[ loopNum-1 ].ContinentType);
  513.             if(table.count(visibleContinentPlots) > 0) then
  514.                 UILens.SetLayerHexesColoredArea( LensLayers.HEX_COLORING_CONTINENT, loopNum-1, visibleContinentPlots, ContinentColor );    
  515.                 kContinentColors[ContinentID] = ContinentColor;
  516.             end
  517.         end
  518.         LuaEvents.MinimapPanel_AddContinentColorPair( kContinentColors );
  519.     end
  520. end
  521.  
  522. -- ===========================================================================
  523. --  Support function for Hotkey Event
  524. -- ===========================================================================
  525. function LensPanelHotkeyControl( pControl:table )
  526.     if Controls.LensPanel:IsHidden() then
  527.         Controls.LensPanel:SetHide(false);
  528.         RealizeFlyouts(Controls.LensPanel);
  529.         Controls.LensButton:SetSelected(true); 
  530.     elseif (not Controls.LensPanel:IsHidden()) and pControl:IsChecked() then
  531.         Controls.LensPanel:SetHide(true);
  532.         Controls.LensButton:SetSelected(false);
  533.     end
  534.     pControl:SetCheck( not pControl:IsChecked() );
  535. end
  536.  
  537. -- ===========================================================================
  538. --  Input Hotkey Event
  539. -- ===========================================================================
  540. function OnInputActionTriggered( actionId )
  541.     if m_ToggleReligionLensId ~= nil and (actionId == m_ToggleReligionLensId) then
  542.         LensPanelHotkeyControl( Controls.ReligionLensButton );
  543.         ToggleReligionLens();
  544.         UI.PlaySound("Play_UI_Click");
  545.     end
  546.     if m_ToggleContinentLensId ~= nil and (actionId == m_ToggleContinentLensId) then
  547.         LensPanelHotkeyControl( Controls.ContinentLensButton );
  548.         ToggleContinentLens();
  549.         UI.PlaySound("Play_UI_Click");
  550.     end
  551.     if m_ToggleAppealLensId ~= nil and (actionId == m_ToggleAppealLensId) then
  552.         LensPanelHotkeyControl( Controls.AppealLensButton );
  553.         ToggleAppealLens();
  554.         UI.PlaySound("Play_UI_Click");
  555.     end
  556.     if m_ToggleSettlerLensId ~= nil and (actionId == m_ToggleSettlerLensId) then
  557.         LensPanelHotkeyControl( Controls.WaterLensButton );
  558.         ToggleWaterLens();
  559.         UI.PlaySound("Play_UI_Click");
  560.     end
  561.     if m_ToggleGovernmentLensId ~= nil and (actionId == m_ToggleGovernmentLensId) then
  562.         LensPanelHotkeyControl( Controls.GovernmentLensButton );
  563.         ToggleGovernmentLens();
  564.         UI.PlaySound("Play_UI_Click");
  565.     end
  566.     if m_TogglePoliticalLensId ~= nil and (actionId == m_TogglePoliticalLensId) then
  567.         LensPanelHotkeyControl( Controls.OwnerLensButton );
  568.         ToggleOwnerLens();
  569.         UI.PlaySound("Play_UI_Click");
  570.     end
  571. end
  572.  
  573. -- ===========================================================================
  574. --  Game Engine Event
  575. -- ===========================================================================
  576. function OnInterfaceModeChanged(eOldMode:number, eNewMode:number)
  577.     --and eNewMode ~= InterfaceModeTypes.VIEW_MODAL_LENS
  578.     if eOldMode == InterfaceModeTypes.VIEW_MODAL_LENS then
  579.         if not Controls.LensPanel:IsHidden() then
  580.             if m_shouldCloseLensMenu then --If player turns off the lens from the menu, do not close the menu
  581.                 Controls.LensPanel:SetHide( true );
  582.                 RealizeFlyouts(Controls.LensPanel);
  583.                 Controls.LensButton:SetSelected( false );
  584.             end
  585.             m_shouldCloseLensMenu = true; --Reset variable so the menu can be closed by selecting a unit/city  
  586.             Controls.ReligionLensButton:SetCheck(false);
  587.             Controls.ContinentLensButton:SetCheck(false);  
  588.             Controls.AppealLensButton:SetCheck(false);
  589.             Controls.GovernmentLensButton:SetCheck(false);
  590.             Controls.WaterLensButton:SetCheck(false);
  591.             Controls.OwnerLensButton:SetCheck(false);
  592.             Controls.TourismLensButton:SetCheck(false);
  593.         end
  594.     end
  595.    
  596. end
  597.  
  598. function GetMinimapMouseCoords( mousex:number, mousey:number )
  599.     local topLeftX, topLeftY = Controls.MinimapImage:GetScreenOffset();
  600.    
  601.     -- normalized 0-1, relative to map
  602.     local minix = mousex - topLeftX;
  603.     local miniy = mousey - topLeftY;
  604.     minix = minix / Controls.MinimapImage:GetSizeX();
  605.     miniy = miniy / Controls.MinimapImage:GetSizeY();
  606.  
  607.     return minix, miniy;
  608. end
  609. function IsMouseInMinimap( minix:number, miniy:number )
  610.     return minix >= 0 and minix <= 1 and miniy >= 0 and miniy <= 1;
  611. end
  612. function TranslateMinimapToWorld( minix:number, miniy:number )
  613.     local mapMinX, mapMinY, mapMaxX, mapMaxY = UI.GetMinimapWorldRect();
  614.  
  615.     -- Clamp coords to minimap.
  616.     minix = math.min( 1, math.max( 0, minix ) );
  617.     miniy = math.min( 1, math.max( 0, miniy ) );
  618.  
  619.     --TODO: max-min probably wont work for rects that cross world wrap! -KS
  620.     local wx = mapMinX + (mapMaxX-mapMinX) * minix;
  621.     local wy = mapMinY + (mapMaxY-mapMinY) * (1 - miniy);
  622.  
  623.     return wx, wy;
  624. end
  625.  
  626. function OnInputHandler( pInputStruct:table )
  627.     -- Skip all handling when dragging is disabled or the minimap is collapsed
  628.     if m_isMouseDragEnabled and not m_isCollapsed then
  629.         local msg = pInputStruct:GetMessageType( );
  630.  
  631.         -- Enable drag on LMB down
  632.         if msg == MouseEvents.LButtonDown then
  633.             local minix, miniy = GetMinimapMouseCoords( pInputStruct:GetX(), pInputStruct:GetY() );
  634.             if IsMouseInMinimap( minix, miniy ) then
  635.                 m_isMouseDragging = true; -- Potential drag is in process
  636.                 m_hasMouseDragged = false; -- There has been no actual dragging yet
  637.                 LuaEvents.WorldInput_DragMapBegin(); -- Alert luathings that a drag is about to go down
  638.                 return true; -- Consume event
  639.             end
  640.  
  641.         -- Disable drag on LMB up (but only if mouse was previously dragging)
  642.         elseif msg == MouseEvents.LButtonUp and m_isMouseDragging then
  643.             m_isMouseDragging = false;
  644.             -- In case of no actual drag occurring, perform camera jump.
  645.             if not m_hasMouseDragged then
  646.                 local minix, miniy = GetMinimapMouseCoords( pInputStruct:GetX(), pInputStruct:GetY() );
  647.                 local wx, wy = TranslateMinimapToWorld( minix, miniy );
  648.                 UI.LookAtPosition( wx, wy );
  649.             end
  650.    
  651.             LuaEvents.WorldInput_DragMapEnd(); -- Alert luathings that the drag has stopped
  652.             return true;
  653.  
  654.         -- Move camera if dragging, mouse moves, and mouse is over minimap.
  655.         elseif msg == MouseEvents.MouseMove and m_isMouseDragging then
  656.             local minix, miniy = GetMinimapMouseCoords( pInputStruct:GetX(), pInputStruct:GetY() );
  657.             local isMouseInMinimap = IsMouseInMinimap( minix, miniy );
  658.  
  659.             -- Catches entering, exiting, and moving within the minimap.
  660.             -- Clamping in TranslateMinimapToWorld guarantees OOB input is treated correctly.
  661.             if m_wasMouseInMinimap or isMouseInMinimap then
  662.                 m_hasMouseDragged = true;
  663.                 local wx, wy = TranslateMinimapToWorld( minix, miniy );
  664.                 UI.FocusMap( wx, wy );
  665.             end
  666.             m_wasMouseInMinimap = isMouseInMinimap
  667.             return isMouseInMinimap; -- Only consume event if it's inside the minimap.
  668.  
  669.         end
  670.     end
  671.     return false;
  672. end
  673.  
  674.  
  675. function OnTutorial_DisableMapDrag( isDisabled:boolean )
  676.     m_isMouseDragEnabled = not isDisabled;
  677.     if isDisabled then
  678.         m_isMouseDragging = false;
  679.         m_hasMouseDragged = false;
  680.         m_wasMouseInMinimap = false;
  681.     end
  682. end
  683.  
  684. function OnTutorial_SwitchToWorldView()
  685.     Controls.SwitcherImage:SetTextureOffsetVal(0,0);
  686. end
  687.  
  688. function OnShutdown()
  689.     LuaEvents.Tutorial_SwitchToWorldView.Remove( OnTutorial_SwitchToWorldView );
  690.     LuaEvents.Tutorial_DisableMapDrag.Remove( OnTutorial_DisableMapDrag );
  691.     LuaEvents.NotificationPanel_ShowContinentLens.Remove(OnToggleContinentLensExternal);
  692. end
  693.  
  694. -- ===========================================================================
  695. -- INITIALIZATION
  696. -- ===========================================================================
  697. function Initialize()
  698.     m_MiniMap_xmloffsety = Controls.MiniMap:GetOffsetY();
  699.     m_ContinentsCache = Map.GetContinentsInUse();
  700.     UI.SetMinimapImageControl(Controls.MinimapImage);
  701.    
  702.     ContextPtr:SetInputHandler( OnInputHandler, true );
  703.     ContextPtr:SetShutdown( OnShutdown );
  704.  
  705.     Controls.LensPanel:ChangeParent(Controls.LensButton);
  706.     Controls.MapOptionsPanel:ChangeParent(Controls.MapOptionsButton);
  707.     Controls.ToggleResourcesButton:SetCheck( UserConfiguration.ShowMapResources() );
  708.     Controls.ToggleYieldsButton:SetCheck( UserConfiguration.ShowMapYield() );
  709.  
  710.     Controls.AppealLensButton:RegisterCallback( Mouse.eLClick, ToggleAppealLens );
  711.     Controls.ContinentLensButton:RegisterCallback( Mouse.eLClick, ToggleContinentLens );
  712.     Controls.CollapseButton:RegisterCallback( Mouse.eLClick, OnCollapseToggle );
  713.     Controls.CollapseButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
  714.     Controls.ExpandButton:RegisterCallback( Mouse.eLClick, OnCollapseToggle );
  715.     Controls.ExpandButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
  716.     Controls.GovernmentLensButton:RegisterCallback( Mouse.eLClick, ToggleGovernmentLens );
  717.     Controls.LensButton:RegisterCallback( Mouse.eLClick, OnToggleLensList );
  718.     Controls.LensButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
  719.     Controls.MapOptionsButton:RegisterCallback( Mouse.eLClick, ToggleMapOptionsList );
  720.     Controls.MapOptionsButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
  721.     Controls.MapPinListButton:RegisterCallback( Mouse.eLClick, ToggleMapPinMode );
  722.     Controls.MapPinListButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
  723.     Controls.OwnerLensButton:RegisterCallback( Mouse.eLClick, ToggleOwnerLens );
  724.     Controls.TourismLensButton:RegisterCallback( Mouse.eLClick, ToggleTourismLens );
  725.     Controls.Pause:RegisterEndCallback( OnPauseEnd );
  726.     Controls.ReligionLensButton:RegisterCallback( Mouse.eLClick, ToggleReligionLens );
  727.     Controls.StrategicSwitcherButton:RegisterCallback( Mouse.eLClick, Toggle2DView );
  728.     Controls.StrategicSwitcherButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
  729.     Controls.ToggleGridButton:RegisterCallback( Mouse.eLClick, ToggleGrid );
  730.     Controls.ToggleResourcesButton:RegisterCallback( Mouse.eLClick, ToggleResourceIcons );
  731.     Controls.ToggleYieldsButton:RegisterCallback( Mouse.eLClick, ToggleYieldIcons );
  732.     Controls.WaterLensButton:RegisterCallback( Mouse.eLClick, ToggleWaterLens );
  733.    
  734.     -- Make sure the StrategicSwitcherButton has the correct image when the game starts in StrategicView
  735.     if UI.GetWorldRenderView() == WorldRenderView.VIEW_2D then
  736.         Controls.SwitcherImage:SetTextureOffsetVal(0,24);
  737.     end
  738.  
  739.     Events.InputActionTriggered.Add( OnInputActionTriggered );
  740.     Events.InterfaceModeChanged.Add( OnInterfaceModeChanged );
  741.     Events.LensLayerOn.Add( OnLensLayerOn );
  742.     Events.LensLayerOff.Add( OnLensLayerOff ); 
  743.     Events.LocalPlayerChanged.Add( OnLocalPlayerChanged );
  744.  
  745.     LuaEvents.NotificationPanel_ShowContinentLens.Add(OnToggleContinentLensExternal);
  746.     LuaEvents.Tutorial_DisableMapDrag.Add( OnTutorial_DisableMapDrag );
  747.     LuaEvents.Tutorial_SwitchToWorldView.Add( OnTutorial_SwitchToWorldView );
  748.  
  749.     LuaEvents.MinimapPanel_ToggleResourceIcons.Add( ToggleResourceIcons );
  750.     LuaEvents.MinimapPanel_ToggleStrategicMap.Add( Toggle2DView );
  751. end
  752. Initialize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement