-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPartialScreenHooks.lua
334 lines (299 loc) · 13.3 KB
/
PartialScreenHooks.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
-- ===========================================================================
-- HUD Partial Screen Hooks
-- ===========================================================================
include("GameCapabilities");
-- ===========================================================================
-- Action Hotkeys
-- ===========================================================================
local m_ToggleCSId :number = Input.GetActionId("ToggleCityStates");
local m_ToggleEspId :number = Input.GetActionId("ToggleEspionage");
local m_ToggleRankingsId:number = Input.GetActionId("ToggleRankings");
local m_ToggleTradeId :number = Input.GetActionId("ToggleTradeRoutes");
-- ===========================================================================
-- CONSTANTS
-- ===========================================================================
local MIN_BG_SIZE : number = 226; -- Minimum size for the non-tiling component of the launch container
local BG_PADDING : number = 116; -- Additional pad the background PAST the size of the hooks
local BG_TILE_PADDING : number = 20; -- Inner padding to give the tile of the hook bar so that it does not show behind the hook bar itself
local BG_TOTAL_OFFSCREEN_OFFSET : number = -126; -- Amount of negative offset to totally remove the partial hook bar from the screen. Used so that we can add back the size of the hook stack to the bar when we have < 2 hooks visible
local m_kPartialScreens:table = { -- Screens that are considered "partial" and mutually exclusive in showing.
"CityStatesPopup",
"EspionageOverview",
"WorldRankings",
"TradeOverview"
}
-- ===========================================================================
-- VARIABLES
-- ===========================================================================
local m_isEspionageUnlocked :boolean = false;
local m_isTradeRoutesUnlocked :boolean = false;
local m_isCityStatesUnlocked :boolean = false;
local m_isDebug :boolean = false;
-- ===========================================================================
-- Checks all the screens or if a screen name is checked in, will check that
-- specific one.
-- RETURNS: true if any partial screen is open; false otherwise.
-- ===========================================================================
function IsPartialScreenOpen( optionalScreenName:string )
local kScreensToCheck:table = m_kPartialScreens;
if optionalScreenName then
kScreensToCheck = { optionalScreenName }; -- Override with specific screne
end
for _,contextName:string in ipairs( kScreensToCheck ) do
local pContextControl :table = ContextPtr:LookUpControl("/InGame/" .. contextName );
if pContextControl == nil then
UI.DataError("Cannot determine if partial screen \"/InGame/"..contextName.."\" is visible because it wasn't found at that path.");
elseif not pContextControl:IsHidden() then
return true;
end
end
return false;
end
-- ===========================================================================
-- UI Control Callback
-- ===========================================================================
function OnToggleEspionage()
if IsPartialScreenOpen("EspionageOverview") then
LuaEvents.PartialScreenHooks_CloseEspionage();
else
if IsPartialScreenOpen() then -- Only play open sound if no partial screen is open.
LuaEvents.PartialScreenHooks_CloseAllExcept("EspionageOverview");
end
LuaEvents.PartialScreenHooks_OpenEspionage();
end
end
-- ===========================================================================
-- UI Control Callback
-- ===========================================================================
function OnToggleCityStates()
if IsPartialScreenOpen("CityStatesPopup") then
LuaEvents.PartialScreenHooks_CloseCityStates();
else
if IsPartialScreenOpen() then -- Only play open sound if no partial screen is open.
LuaEvents.PartialScreenHooks_CloseAllExcept("CityStatesPopup");
end
LuaEvents.PartialScreenHooks_OpenCityStates();
end
end
-- ===========================================================================
-- UI Control Callback
-- ===========================================================================
function OnToggleTradeOverview()
if IsPartialScreenOpen("TradeOverview") then
LuaEvents.PartialScreenHooks_CloseTradeOverview();
else
if IsPartialScreenOpen() then -- Only play open sound if no partial screen is open.
LuaEvents.PartialScreenHooks_CloseAllExcept("TradeOverview");
end
LuaEvents.PartialScreenHooks_OpenTradeOverview();
end
end
-- ===========================================================================
-- UI Control Callback
-- ===========================================================================
function OnToggleWorldRankings()
if IsPartialScreenOpen("WorldRankings") then
LuaEvents.PartialScreenHooks_CloseWorldRankings();
else
if IsPartialScreenOpen() then -- Only play open sound if no partial screen is open.
LuaEvents.PartialScreenHooks_CloseAllExcept("WorldRankings");
end
LuaEvents.PartialScreenHooks_OpenWorldRankings();
end
end
-- ===========================================================================
-- UI Control Callback
-- ===========================================================================
function OnOpenDiplomacy()
LuaEvents.TopPanel_OpenDiplomacyActionView();
end
-- ===========================================================================
-- ===========================================================================
function Resize()
-- The Launch Bar width should accomodate how many hooks are currently in the stack.
if (m_isTradeRoutesUnlocked and HasCapability("CAPABILITY_TRADE_VIEW")) then
Controls.TradeRoutesButton:SetHide(false);
Controls.TradeRoutesBolt:SetHide(false);
else
if(not m_isDebug) then
Controls.TradeRoutesButton:SetHide(true);
Controls.TradeRoutesBolt:SetHide(true);
end
end
if (m_isEspionageUnlocked and HasCapability("CAPABILITY_ESPIONAGE_VIEW")) then
Controls.EspionageButton:SetHide(false);
Controls.EspionageBolt:SetHide(false);
else
if(not m_isDebug) then
Controls.EspionageButton:SetHide(true);
Controls.EspionageBolt:SetHide(true);
end
end
if (m_isCityStatesUnlocked) then
Controls.CityStatesButton:SetHide(false);
Controls.CityStatesBolt:SetHide(false);
else
if(not m_isDebug) then
Controls.CityStatesButton:SetHide(true);
Controls.CityStatesBolt:SetHide(true);
end
end
Controls.ButtonStack:CalculateSize();
Controls.ButtonStack:ReprocessAnchoring();
if( MIN_BG_SIZE > Controls.ButtonStack:GetSizeX() + BG_PADDING) then
Controls.LaunchBacking:SetSizeX(MIN_BG_SIZE);
Controls.LaunchBacking:SetOffsetX(BG_TOTAL_OFFSCREEN_OFFSET + (Controls.ButtonStack:GetSizeX() + BG_TILE_PADDING));
else
Controls.LaunchBacking:SetOffsetX(2);
Controls.LaunchBacking:SetSizeX(Controls.ButtonStack:GetSizeX() + BG_PADDING);
end
Controls.LaunchBackingDropShadow:SetSizeX(Controls.ButtonStack:GetSizeX() + BG_TILE_PADDING);
Controls.LaunchBackingDropShadow:ReprocessAnchoring();
Controls.LaunchBackingTile:SetSizeX(Controls.ButtonStack:GetSizeX() - BG_TILE_PADDING);
LuaEvents.PartialScreenHooks_Resize();
end
-- ===========================================================================
-- Refresh Data and View
-- ===========================================================================
-- Capture the meet civ event to see if we have encountered a city-state
function OnDiplomacyMeet(player1ID:number, player2ID:number)
if(not m_isCityStatesUnlocked) then
local localPlayerID:number = Game.GetLocalPlayer();
-- Have a local player?
if(localPlayerID ~= -1) then
-- Was the local player involved, and was it a minor civ that was met?
local metPlayer = Players[player2ID];
if (player1ID == localPlayerID and not metPlayer:IsMajor() and not metPlayer:IsBarbarian()) then
m_isCityStatesUnlocked = true;
Resize();
end
end
end
end
function CheckTradeCapacity(localPlayer)
if (not m_isTradeRoutesUnlocked) then
local playerTrade :table = localPlayer:GetTrade();
local routesCapacity:number = playerTrade:GetOutgoingRouteCapacity();
if (routesCapacity > 0) then
m_isTradeRoutesUnlocked = true;
Resize();
end
end
end
function CheckSpyCapacity(localPlayer)
if (not m_isEspionageUnlocked) then
local playerDiplo :table = localPlayer:GetDiplomacy();
local spyCapacity:number = playerDiplo:GetSpyCapacity();
if (spyCapacity > 0) then
m_isEspionageUnlocked = true;
Resize();
end
end
end
-- Capture spies or traders becoming unlocked
function OnCivicCompleted( player:number, civic:number, isCanceled:boolean)
if player == Game.GetLocalPlayer() and (not m_isEspionageUnlocked or not m_isTradeRoutesUnlocked) then
local localPlayer = Players[Game.GetLocalPlayer()];
if (localPlayer == nil) then
return;
end
CheckTradeCapacity(localPlayer);
CheckSpyCapacity(localPlayer);
end
end
-- Check trade, spy, and partial screen hooks OnTurnBegin
function OnTurnBegin()
local ePlayer:number = Game.GetLocalPlayer();
if ePlayer == -1 then
return;
end
localPlayer = Players[ePlayer];
CheckTradeCapacity(localPlayer);
CheckSpyCapacity(localPlayer);
-- Check to see if the player has met any city-states
-- Note: I'm using not IsMajor and not IsBarbarian to determine the city-state check at this point - though elsewhere we have used CanReceiveInfluencePoints.
-- For future expansions we should probably create an IsMinor check for the Player object.
if (not m_isCityStatesUnlocked) then
local localDiplomacy:table = localPlayer:GetDiplomacy();
local aPlayers = PlayerManager.GetAliveMinors();
for _, pPlayer in ipairs(aPlayers) do
if (not pPlayer:IsMajor() and not pPlayer:IsBarbarian() and localDiplomacy:HasMet(pPlayer:GetID())) then
m_isCityStatesUnlocked = true;
end
end
end
Resize();
end
-- ===========================================================================
-- Game Engine Event
-- ===========================================================================
function OnInterfaceModeChanged(eOldMode:number, eNewMode:number)
if eNewMode == InterfaceModeTypes.VIEW_MODAL_LENS then
ContextPtr:SetHide(true);
end
if eOldMode == InterfaceModeTypes.VIEW_MODAL_LENS then
ContextPtr:SetHide(false);
end
end
-- ===========================================================================
-- Input Hotkey Event
-- ===========================================================================
function OnInputActionTriggered( actionId )
if actionId == m_ToggleCSId then
OnToggleCityStates();
UI.PlaySound("Play_UI_Click");
end
if actionId == m_ToggleEspId and HasCapability("CAPABILITY_ESPIONAGE_VIEW") then
if UI.QueryGlobalParameterInt("DISABLE_ESPIONAGE_HOTKEY") ~= 1 then
OnToggleEspionage();
UI.PlaySound("Play_UI_Click");
end
end
if actionId == m_ToggleRankingsId and HasCapability("CAPABILITY_WORLD_RANKINGS") then
OnToggleWorldRankings();
UI.PlaySound("Play_UI_Click");
end
if actionId == m_ToggleTradeId and HasCapability("CAPABILITY_TRADE_VIEW") then
OnToggleTradeOverview();
UI.PlaySound("Play_UI_Click");
end
end
-- ===========================================================================
-- Reset the hooks that are visible for hotseat
-- ===========================================================================
function OnLocalPlayerChanged()
m_isEspionageUnlocked = false;
m_isTradeRoutesUnlocked = false;
m_isCityStatesUnlocked = false;
OnTurnBegin();
end
-- ===========================================================================
-- Lua Event
-- Tutorial system is requesting any partial screens open, to be closed.
-- ===========================================================================
function OnTutorialCloseAll()
if IsPartialScreenOpen("EspionageOverview") then LuaEvents.PartialScreenHooks_CloseEspionage(); end
if IsPartialScreenOpen("CityStatesPopup") then LuaEvents.PartialScreenHooks_CloseCityStates(); end
if IsPartialScreenOpen("TradeOverview") then LuaEvents.PartialScreenHooks_CloseTradeOverview(); end
if IsPartialScreenOpen("WorldRankings") then LuaEvents.PartialScreenHooks_CloseWorldRankings(); end
end
-- ===========================================================================
function Initialize()
Controls.CityStatesButton:RegisterCallback( Mouse.eLClick, OnToggleCityStates );
Controls.CityStatesButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.EspionageButton:RegisterCallback( Mouse.eLClick, OnToggleEspionage );
Controls.EspionageButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.WorldRankingsButton:RegisterCallback( Mouse.eLClick, OnToggleWorldRankings );
Controls.WorldRankingsButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.TradeRoutesButton:RegisterCallback( Mouse.eLClick, OnToggleTradeOverview );
Controls.TradeRoutesButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Events.CivicCompleted.Add(OnCivicCompleted);
Events.DiplomacyMeet.Add( OnDiplomacyMeet );
Events.InputActionTriggered.Add( OnInputActionTriggered );
Events.InterfaceModeChanged.Add( OnInterfaceModeChanged );
Events.LocalPlayerChanged.Add( OnLocalPlayerChanged );
Events.TurnBegin.Add( OnTurnBegin );
LuaEvents.Tutorial_CloseAllPartialScreens.Add( OnTutorialCloseAll );
OnTurnBegin();
end
Initialize();