forked from Azurency/Civ6-UIFiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
InGame.lua
386 lines (335 loc) · 15.7 KB
/
InGame.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
-- Copyright 2015-2018, Firaxis Games
-- Root context for ingame (aka: All-the-things)
-- MODs / Expansions cannot use partial replacement as this context is
-- directly added to the UI Control Tree via engine.
include( "LocalPlayerActionSupport" );
include( "InputSupport" );
-- ===========================================================================
-- CONSTANTS
-- ===========================================================================
local TIME_UNTIL_UPDATE:number = 0.1; -- time to wait before attempting to release delayshow popups.
-- ===========================================================================
-- VARIABLES
-- ===========================================================================
local DefaultMessageHandler = {};
local m_bulkHideTracker :number = 0;
local m_lastBulkHider:string = "first call";
g_uiAddins = {};
local m_PauseId :number = Input.GetActionId("PauseMenu");
local m_QuicksaveId :number = Input.GetActionId("QuickSave");
local m_HexColoringReligion : number = UILens.CreateLensLayerHash("Hex_Coloring_Religion");
local m_CulturalIdentityLens: number = UILens.CreateLensLayerHash("Cultural_Identity_Lens");
local m_TouristTokens : number = UILens.CreateLensLayerHash("Tourist_Tokens");
local m_activeLocalPlayer : number = -1;
local m_timeUntilPopupCheck : number = 0;
-- ===========================================================================
-- FUNCTIONS
-- ===========================================================================
-- ===========================================================================
-- Open up the TopOptionsMenu with the utmost priority.
-- ===========================================================================
function OpenInGameOptionsMenu()
LuaEvents.InGame_OpenInGameOptionsMenu();
end
-- ===========================================================================
-- LUA Event
-- ===========================================================================
function OnTutorialToggleInGameOptionsMenu()
if Controls.TopOptionsMenu:IsHidden() then
OpenInGameOptionsMenu();
else
LuaEvents.InGame_CloseInGameOptionsMenu();
end
end
-- ===========================================================================
DefaultMessageHandler[KeyEvents.KeyUp] =
function( pInputStruct:table )
local uiKey = pInputStruct:GetKey();
if( uiKey == Keys.VK_ESCAPE ) then
if( Controls.TopOptionsMenu:IsHidden() ) then
OpenInGameOptionsMenu();
return true;
end
return false; -- Already open, let it handle it.
elseif( uiKey == Keys.B and pInputStruct:IsShiftDown() and pInputStruct:IsAltDown() and (not UI.IsFinalRelease()) ) then
-- DEBUG: Force unhiding
local msg:string = "***PLAYER Force Bulk unhiding SHIFT+ALT+B ***";
UI.DataError(msg);
m_bulkHideTracker = 1;
BulkHide(false, msg);
elseif( uiKey == Keys.J and pInputStruct:IsShiftDown() and pInputStruct:IsAltDown() and (not UI.IsFinalRelease()) ) then
if m_bulkHideTracker < 1 then
BulkHide(true, "Forced" );
else
BulkHide(false, "Forced" );
end
end
return false;
end
----------------------------------------------------------------
-- LoadGameViewStateDone Event Handler
----------------------------------------------------------------
function OnLoadGameViewStateDone()
-- show HUD elements that relay on the gamecache being fully initialized.
if(GameConfiguration.IsNetworkMultiplayer()) then
Controls.MultiplayerTurnManager:SetHide(false);
end
end
----------------------------------------------------------------
-- Input handling
----------------------------------------------------------------
function OnInputHandler( pInputStruct )
local uiMsg = pInputStruct:GetMessageType();
if DefaultMessageHandler[uiMsg] ~= nil then
return DefaultMessageHandler[uiMsg]( pInputStruct );
end
return false;
end
----------------------------------------------------------------
function OnShow()
Controls.WorldViewControls:SetHide( false );
local pFriends = Network.GetFriends();
if (pFriends ~= nil) then
if (GameConfiguration.IsAnyMultiplayer()) then
if GameConfiguration.IsHotseat() then
pFriends:SetRichPresence("civPresence", "LOC_PRESENCE_IN_GAME_HOTSEAT");
elseif GameConfiguration.IsLANMultiplayer() then
pFriends:SetRichPresence("civPresence", "LOC_PRESENCE_IN_GAME_LAN");
elseif GameConfiguration.IsPlayByCloud() then
pFriends:SetRichPresence("civPresence", "LOC_PRESENCE_IN_GAME_PLAYBYCLOUD");
else
pFriends:SetRichPresence("civPresence", "LOC_PRESENCE_IN_GAME_ONLINE");
end
else
pFriends:SetRichPresence("civPresence", "LOC_PRESENCE_IN_GAME_SP");
end
end
RealizeTooltipBehavior();
end
-- ===========================================================================
function RealizeTooltipBehavior()
local toolTipBehavior:number = Options.GetAppOption("UI", "TooltipBehavior");
if toolTipBehavior == TooltipBehavior.AlwaysShowing then
TTManager:SetToolTipDelay( 0.0 );
elseif toolTipBehavior == TooltipBehavior.ShowAfterDelay then
TTManager:SetToolTipDelay( 2.0 ); -- seconds to delay before showing
elseif toolTipBehavior == TooltipBehavior.ShowOnButton then
TTManager:SetToolTipDelay( 0.0 ); -- no delay (but require button.)
end
end
-- ===========================================================================
-- Hide (or Show) all the contexts part of the BULK group.
-- ===========================================================================
function BulkHide( isHide:boolean, debugWho:string )
-- Tracking for debugging:
m_bulkHideTracker = m_bulkHideTracker + (isHide and 1 or -1);
print("Request to BulkHide( "..tostring(isHide)..", "..debugWho.." ), Show on 0 = "..tostring(m_bulkHideTracker));
if m_bulkHideTracker < 0 then
UI.DataError("Request to bulk show past limit by "..debugWho..". Last bulk shown by "..m_lastBulkHider);
m_bulkHideTracker = 0;
end
m_lastBulkHider = debugWho;
-- Do the bulk hiding/showing
local kGroups:table = {"WorldViewControls", "HUD", "PartialScreens", "Screens", "TopLevelHUD" };
for i,group in ipairs(kGroups) do
local pContext :table = ContextPtr:LookUpControl("/InGame/"..group);
if pContext == nil then
UI.DataError("InGame is unable to BulkHide("..isHide..") '/InGame/"..group.."' because the Context doesn't exist.");
else
if m_bulkHideTracker == 1 and isHide then
pContext:SetHide(true);
elseif m_bulkHideTracker == 0 and isHide==false then
pContext:SetHide(false);
RestartRefreshRequest();
else
-- Do nothing
end
end
end
end
-- ===========================================================================
-- Hotkey Event
-- ===========================================================================
function OnInputActionTriggered( actionId )
if actionId == m_PauseId then
if( Controls.TopOptionsMenu:IsHidden() ) then
OpenInGameOptionsMenu();
return true;
end
elseif actionId == m_QuicksaveId then
-- Quick save
if CanLocalPlayerSaveGame() then
local gameFile = {};
gameFile.Name = "quicksave";
gameFile.Location = SaveLocations.LOCAL_STORAGE;
gameFile.Type= Network.GetGameConfigurationSaveType();
gameFile.IsAutosave = false;
gameFile.IsQuicksave = true;
Network.SaveGame(gameFile);
UI.PlaySound("Confirm_Bed_Positive");
end
end
end
-- ===========================================================================
-- Gamecore Event
-- Called once per layer that is turned on when a new lens is activated,
-- or when a player explicitly turns off the layer from the "player" lens.
-- ===========================================================================
function OnLensLayerOn( layerHash:number )
if layerHash == m_HexColoringReligion or layerHash == m_CulturalIdentityLens or
layerHash == m_TouristTokens then
Controls.CityBannerManager:ChangeParent(Controls.BannerAndFlags);
end
end
-- ===========================================================================
-- Gamecore Event
-- Called once per layer that is turned on when a new lens is deactivated,
-- or when a player explicitly turns off the layer from the "player" lens.
-- ===========================================================================
function OnLensLayerOff( layerHash:number )
if layerHash == m_HexColoringReligion or layerHash == m_CulturalIdentityLens or
layerHash == m_TouristTokens then
Controls.UnitFlagManager:ChangeParent(Controls.BannerAndFlags);
end
end
-- ===========================================================================
-- EVENT
-- ===========================================================================
function OnTurnBegin()
m_activeLocalPlayer = Game.GetLocalPlayer();
end
-- ===========================================================================
-- EVENT
-- ===========================================================================
function OnTurnEnd()
m_activeLocalPlayer = -1;
end
-- ===========================================================================
function RestartRefreshRequest()
-- Increasing this adds a delay, but will make it less likely that lower
-- priority popups will be shown before all the popups are in added in
-- the queue.
m_timeUntilPopupCheck = TIME_UNTIL_UPDATE;
ContextPtr:SetRefreshHandler( OnRefreshAttemptPopupRelease );
ContextPtr:RequestRefresh();
end
-- ===========================================================================
-- EVENT
-- Gamecore is done processing events; this may fire multiple times as a
-- turn begins, as well as after player actions.
-- ===========================================================================
function OnGameCoreEventPlaybackComplete()
-- Gate using this based on whether or not it's firing for a local player
if m_activeLocalPlayer == -1 then return; end;
RestartRefreshRequest();
end
-- ===========================================================================
-- UI Manager Callback
-- ===========================================================================
function OnPopupQueueChange( isQueuing:boolean )
if m_timeUntilPopupCheck <= 0 then
RestartRefreshRequest();
end
end
-- ===========================================================================
-- Event
-- ===========================================================================
function OnUpdateUI( type, tag, iData1, iData2, strData1 )
if (type == SystemUpdateUI.TouchTipBehaviorChanged) then
RealizeTooltipBehavior();
end
end
-- ===========================================================================
-- Event
-- ===========================================================================
function OnUIIdle()
-- If a countdown to check hasn't started, kick one off.
if m_timeUntilPopupCheck <= 0 then
RestartRefreshRequest();
end
end
-- ===========================================================================
function IsAbleToShowDelayedPopups()
local isBulkHideOkay :boolean = (m_bulkHideTracker == 0);
local isQueueEnabled :boolean = (UIManager:IsPopupQueueDisabled() == false);
return isBulkHideOkay and isQueueEnabled;
end
-- ===========================================================================
-- UI Callback
-- ===========================================================================
function OnRefreshAttemptPopupRelease( delta:number )
m_timeUntilPopupCheck = m_timeUntilPopupCheck - delta;
if m_timeUntilPopupCheck <= 0 then
-- Only release delayed popups if a bulk hide operation isn't currently happening.
if IsAbleToShowDelayedPopups() then
UIManager:ShowDelayedPopups(); -- Show any popups that had been added to Forge waiting to be shown
end
ContextPtr:ClearRefreshHandler();
else
ContextPtr:RequestRefresh();
end
end
-- ===========================================================================
function OnDiplomacyHideIngameUI() BulkHide( true, "Diplomacy" ); Input.PushActiveContext(InputContext.Diplomacy); end
function OnDiplomacyShowIngameUI() BulkHide(false, "Diplomacy" ); Input.PopContext(); end
function OnEndGameMenuShown() BulkHide( true, "EndGame" ); Input.PushActiveContext(InputContext.EndGame); end
function OnEndGameMenuClosed() BulkHide(false, "EndGame" ); Input.PopContext(); end
function OnFullscreenMapShown() BulkHide( true, "FullscreenMap" ); Input.PushActiveContext(InputContext.FullscreenMap);end
function OnFullscreenMapClosed() BulkHide(false, "FullscreenMap" ); Input.PopContext(); end
function OnNaturalWonderPopupShown() BulkHide( true, "NaturalWonder" ); end
function OnNaturalWonderPopupClosed() BulkHide(false, "NaturalWonder" ); end
function OnProjectBuiltShown() BulkHide( true, "Project" ); end
function OnProjectBuiltClosed() BulkHide(false, "Project" ); end
function OnTutorialEndHide() BulkHide( true, "TutorialEnd" ); end
function OnWonderBuiltPopupShown() BulkHide( true, "Wonder" ); end
function OnWonderBuiltPopupClosed() BulkHide(false, "Wonder" ); end
-- ===========================================================================
function OnShutdown()
UIManager:ClearPopupChangeHandler();
end
-- ===========================================================================
-- Cannot use LateInitialize patterns as this context is attached via C++
-- ===========================================================================
function Initialize()
m_activeLocalPlayer = Game.GetLocalPlayer();
-- Support for Modded Add-in UI's
for i, addin in ipairs(Modding.GetUserInterfaces("InGame")) do
print("Loading InGame UI - " .. addin.ContextPath);
local id :string = addin.ContextPath:sub( -(string.find( string.reverse(addin.ContextPath), '/') - 1) ); -- grab id from end of path
local isHidden :boolean = true;
local newContext:table = ContextPtr:LoadNewContext(addin.ContextPath, Controls.AdditionalUserInterfaces, id, isHidden); -- Content, ID, hidden
table.insert(g_uiAddins, newContext);
end
ContextPtr:SetInputHandler( OnInputHandler, true );
ContextPtr:SetShowHandler( OnShow );
ContextPtr:SetRefreshHandler( OnRefreshAttemptPopupRelease );
ContextPtr:SetShutdown( OnShutdown );
UIManager:SetPopupChangeHandler( OnPopupQueueChange );
Events.GameCoreEventPlaybackComplete.Add( OnGameCoreEventPlaybackComplete );
Events.InputActionTriggered.Add( OnInputActionTriggered );
Events.LensLayerOff.Add( OnLensLayerOff );
Events.LensLayerOn.Add( OnLensLayerOn );
Events.LoadGameViewStateDone.Add( OnLoadGameViewStateDone );
Events.LocalPlayerTurnBegin.Add( OnTurnBegin );
Events.LocalPlayerTurnEnd.Add( OnTurnEnd );
Events.SystemUpdateUI.Add( OnUpdateUI );
Events.UIIdle.Add( OnUIIdle );
-- NOTE: Using UI open/closed pairs in the case of end game; where
-- the same player receives both a victory and defeat messages
-- across the wire.
LuaEvents.DiplomacyActionView_HideIngameUI.Add( OnDiplomacyHideIngameUI );
LuaEvents.DiplomacyActionView_ShowIngameUI.Add( OnDiplomacyShowIngameUI );
LuaEvents.EndGameMenu_Shown.Add( OnEndGameMenuShown );
LuaEvents.EndGameMenu_Closed.Add( OnEndGameMenuClosed );
LuaEvents.FullscreenMap_Shown.Add( OnFullscreenMapShown );
LuaEvents.FullscreenMap_Closed.Add( OnFullscreenMapClosed );
LuaEvents.NaturalWonderPopup_Shown.Add( OnNaturalWonderPopupShown );
LuaEvents.NaturalWonderPopup_Closed.Add( OnNaturalWonderPopupClosed );
LuaEvents.ProjectBuiltPopup_Shown.Add( OnProjectBuiltShown );
LuaEvents.ProjectBuiltPopup_Closed.Add( OnProjectBuiltClosed );
LuaEvents.Tutorial_ToggleInGameOptionsMenu.Add( OnTutorialToggleInGameOptionsMenu );
LuaEvents.Tutorial_TutorialEndHideBulkUI.Add( OnTutorialEndHide );
LuaEvents.WonderBuiltPopup_Shown.Add( OnWonderBuiltPopupShown );
LuaEvents.WonderBuiltPopup_Closed.Add( OnWonderBuiltPopupClosed );
end
Initialize();