forked from Azurency/Civ6-UIFiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiplomacyRibbon.lua
435 lines (366 loc) · 15.8 KB
/
DiplomacyRibbon.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
-- Copyright 2017-2018, Firaxis Games.
-- Leader container list on top of the HUD
include("InstanceManager");
include("LeaderIcon");
include("PlayerSupport");
-- ===========================================================================
-- CONSTANTS
-- ===========================================================================
local SCROLL_SPEED :number = 3;
-- ===========================================================================
-- GLOBALS
-- ===========================================================================
g_maxNumLeaders = 0; -- Number of leaders that can fit in the ribbon
-- ===========================================================================
-- MEMBERS
-- ===========================================================================
local m_leadersMet :number = 0; -- Number of leaders in the ribbon
local m_scrollIndex :number = 0; -- Index of leader that is supposed to be on the far right
local m_scrollPercent :number = 0; -- Necessary for scroll lerp
local m_isScrolling :boolean = false;
local m_uiLeadersByID :table = {};
local m_uiChatIconsVisible :table = {};
local m_kLeaderIM :table = InstanceManager:new("LeaderInstance", "LeaderContainer", Controls.LeaderStack);
-- ===========================================================================
-- Cleanup leaders
-- ===========================================================================
function ResetLeaders()
m_leadersMet = 0;
m_uiLeadersByID = {};
m_kLeaderIM:ResetInstances();
end
-- ===========================================================================
function OnLeaderClicked(playerID : number )
-- Send an event to open the leader in the diplomacy view (only if they met)
local localPlayerID:number = Game.GetLocalPlayer();
if playerID == localPlayerID or Players[localPlayerID]:GetDiplomacy():HasMet(playerID) then
LuaEvents.DiplomacyRibbon_OpenDiplomacyActionView( playerID );
end
end
-- ===========================================================================
-- Add a leader (from right to left)
-- ===========================================================================
function AddLeader(iconName : string, playerID : number, isUniqueLeader: boolean)
m_leadersMet = m_leadersMet + 1;
-- Create a new leader instance
local leaderIcon, instance = LeaderIcon:GetInstance(m_kLeaderIM);
m_uiLeadersByID[playerID] = instance;
leaderIcon:UpdateIcon(iconName, playerID, isUniqueLeader);
leaderIcon:RegisterCallback(Mouse.eLClick, function() OnLeaderClicked(playerID); end);
-- Returning these so mods can override them and modify the icons
return leaderIcon, instance;
end
-- ===========================================================================
-- Clears leaders and re-adds them to the stack
-- ===========================================================================
function UpdateLeaders()
local localPlayerID:number = Game.GetLocalPlayer();
if localPlayerID == -1 then
Controls.LeaderStack:CalculateSize();
RealizeSize();
return;
end;
ResetLeaders();
-- Add entries for everyone we know (Majors only)
local aPlayers:table = PlayerManager.GetAliveMajors();
local localPlayer:table = Players[localPlayerID];
local localDiplomacy:table = localPlayer:GetDiplomacy();
table.sort(aPlayers, function(a:table,b:table) return localDiplomacy:GetMetTurn(a:GetID()) < localDiplomacy:GetMetTurn(b:GetID()) end);
--First, add me!
AddLeader("ICON_"..PlayerConfigurations[localPlayerID]:GetLeaderTypeName(), localPlayerID);
local kMetPlayers, kUniqueLeaders = GetMetPlayersAndUniqueLeaders();
--Then, add the leader icons.
for _, pPlayer in ipairs(aPlayers) do
local playerID:number = pPlayer:GetID();
if(playerID ~= localPlayerID) then
local isMet :boolean = kMetPlayers[playerID];
local pPlayerConfig :table = PlayerConfigurations[playerID];
if (isMet or (GameConfiguration.IsAnyMultiplayer() and pPlayerConfig:IsHuman())) then
if isMet then
local leaderName:string = pPlayerConfig:GetLeaderTypeName();
AddLeader("ICON_"..leaderName, playerID, kUniqueLeaders[leaderName]);
else
AddLeader("ICON_LEADER_DEFAULT", playerID);
end
end
end
end
Controls.LeaderStack:CalculateSize();
RealizeSize();
end
-- ===========================================================================
-- Updates size and location of BG and Scroll controls
-- additionalElementsWidth, from MODS that add additional content.
-- ===========================================================================
function RealizeSize( additionalElementsWidth:number )
if additionalElementsWidth == nil then
additionalElementsWidth = 0;
end
local MIN_LEFT_HOOKS :number = 260;
local RIGHT_HOOKS_INITIAL :number = 163;
local WORLD_TRACKER_OFFSET :number = 40;
local launchBarWidth :number = MIN_LEFT_HOOKS;
local partialScreenBarWidth :number = RIGHT_HOOKS_INITIAL;
-- Obtain controls
local uiPartialScreenHookBar :table = ContextPtr:LookUpControl( "/InGame/PartialScreenHooks/ButtonStack" );
local uiLaunchBar :table = ContextPtr:LookUpControl( "/InGame/LaunchBar/ButtonStack" );
if (uiLaunchBar ~= nil) then
launchBarWidth = math.max(uiLaunchBar:GetSizeX() + WORLD_TRACKER_OFFSET, MIN_LEFT_HOOKS);
end
if (uiPartialScreenHookBar~=nil) then
partialScreenBarWidth = uiPartialScreenHookBar:GetSizeX();
end
local screenWidth:number, screenHeight:number = UIManager:GetScreenSizeVal(); -- Cache screen dimensions
local SIZE_LEADER :number = 51; -- Size of leader icon and border.
local PADDING_LEADER :number = 3; -- Padding used in stack control.
local maxSize :number = screenWidth - launchBarWidth - partialScreenBarWidth;
local size :number = maxSize;
g_maxNumLeaders = math.floor(maxSize / (SIZE_LEADER + PADDING_LEADER));
Controls.LeaderBG:SetHide( m_leadersMet==0 )
if m_leadersMet > 0 then
-- Compute size of the background shadow
local BG_PADDING_EDGE :number = 50; -- Account for the (tons of) alpha on edges of shadow graphic.
local MINIMUM_BG_SIZE :number = 100;
local bgSize :number = 0;
if (m_leadersMet > g_maxNumLeaders) then
bgSize = g_maxNumLeaders * (SIZE_LEADER + PADDING_LEADER) + additionalElementsWidth + BG_PADDING_EDGE;
else
bgSize = m_leadersMet * (SIZE_LEADER + PADDING_LEADER) + additionalElementsWidth + BG_PADDING_EDGE;
end
bgSize = math.max(bgSize, MINIMUM_BG_SIZE);
Controls.LeaderBG:SetSizeX( bgSize );
Controls.RibbonContainer:SetSizeX( bgSize );
-- Compute actual size of the container
local PADDING_EDGE :number = 8; -- Ensure scroll bar is wide enough
size = g_maxNumLeaders * (SIZE_LEADER + PADDING_LEADER) + PADDING_EDGE + additionalElementsWidth;
end
Controls.ScrollContainer:SetSizeX(size);
Controls.LeaderScroll:SetSizeX(size);
Controls.RibbonContainer:SetOffsetX(partialScreenBarWidth);
Controls.LeaderScroll:CalculateSize();
RealizeScroll();
end
-- ===========================================================================
-- Updates visibility of previous and next buttons
-- ===========================================================================
function RealizeScroll()
Controls.NextButtonContainer:SetHide( not CanScrollLeft() );
Controls.PreviousButtonContainer:SetHide( not CanScrollRight() );
end
-- ===========================================================================
function CanScrollLeft()
return m_scrollIndex > 0;
end
-- ===========================================================================
function CanScrollRight()
return m_leadersMet - m_scrollIndex > g_maxNumLeaders;
end
-- ===========================================================================
-- Initialize scroll animation in a particular direction
-- ===========================================================================
function Scroll(direction : number)
m_scrollPercent = 0;
m_scrollIndex = m_scrollIndex + direction;
if(m_scrollIndex < 0) then
m_scrollIndex = 0;
end
if(not m_isScrolling) then
ContextPtr:SetUpdate( UpdateScroll );
m_isScrolling = true;
end
RealizeScroll();
end
-- ===========================================================================
-- Update scroll animation (only called while animating)
-- ===========================================================================
function UpdateScroll(deltaTime : number)
local start:number = Controls.LeaderScroll:GetScrollValue();
local destination:number = 1.0 - (m_scrollIndex / (m_leadersMet - g_maxNumLeaders));
m_scrollPercent = m_scrollPercent + (SCROLL_SPEED * deltaTime);
if(m_scrollPercent >= 1) then
m_scrollPercent = 1
EndScroll();
end
Controls.LeaderScroll:SetScrollValue(start + (destination - start) * m_scrollPercent);
end
-- ===========================================================================
-- Cleans up scroll update callback when done scrollin
-- ===========================================================================
function EndScroll()
ContextPtr:ClearUpdate();
m_isScrolling = false;
RealizeScroll();
end
-- ===========================================================================
-- SystemUpdateUI Callback
-- ===========================================================================
function OnUpdateUI(type:number, tag:string, iData1:number, iData2:number, strData1:string)
if(type == SystemUpdateUI.ScreenResize) then
RealizeSize();
end
end
-- ===========================================================================
-- Diplomacy Callback
-- ===========================================================================
function OnDiplomacyMeet(player1ID:number, player2ID:number)
local localPlayerID:number = Game.GetLocalPlayer();
-- Have a local player?
if(localPlayerID ~= -1) then
-- Was the local player involved?
if (player1ID == localPlayerID or player2ID == localPlayerID) then
UpdateLeaders();
end
end
end
-- ===========================================================================
-- Diplomacy Callback
-- ===========================================================================
function OnDiplomacyWarStateChange(player1ID:number, player2ID:number)
local localPlayerID:number = Game.GetLocalPlayer();
-- Have a local player?
if(localPlayerID ~= -1) then
-- Was the local player involved?
if (player1ID == localPlayerID or player2ID == localPlayerID) then
UpdateLeaders();
end
end
end
-- ===========================================================================
-- Diplomacy Callback
-- ===========================================================================
function OnDiplomacySessionClosed(sessionID:number)
local localPlayerID:number = Game.GetLocalPlayer();
-- Have a local player?
if(localPlayerID ~= -1) then
-- Was the local player involved?
local diplomacyInfo:table = DiplomacyManager.GetSessionInfo(sessionID);
if(diplomacyInfo ~= nil and (diplomacyInfo.FromPlayer == localPlayerID or diplomacyInfo.ToPlayer == localPlayerID)) then
UpdateLeaders();
end
end
end
-- ===========================================================================
-- 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
-- ===========================================================================
-- EVENT
-- ===========================================================================
function OnTurnBegin(playerID:number)
local leader:table = m_uiLeadersByID[playerID];
if(leader ~= nil) then
leader.LeaderContainer:SetToBeginning();
leader.LeaderContainer:Play();
end
end
-- ===========================================================================
-- EVENT
-- ===========================================================================
function OnTurnEnd(playerID:number)
if(playerID ~= -1) then
local leader = m_uiLeadersByID[playerID];
if(leader ~= nil) then
leader.LeaderContainer:Reverse();
end
end
end
-- ===========================================================================
-- LUAEvent
-- ===========================================================================
function OnLaunchBarResized( width:number )
RealizeSize();
end
-- ===========================================================================
-- UI Callback
-- ===========================================================================
function OnScrollLeft()
if CanScrollLeft() then
Scroll(-1);
end
end
-- ===========================================================================
-- UI Callback
-- ===========================================================================
function OnScrollRight()
if CanScrollRight() then
Scroll(1);
end
end
-- ===========================================================================
function OnChatReceived(fromPlayer:number, stayOnScreen:boolean)
local instance:table= m_uiLeadersByID[fromPlayer];
if instance == nil then return; end
if stayOnScreen then
Controls.ChatIndicatorWaitTimer:Stop();
instance.ChatIndicatorFade:RegisterEndCallback(function() end);
table.insert(m_uiChatIconsVisible, instance.ChatIndicatorFade);
else
Controls.ChatIndicatorWaitTimer:Stop();
instance.ChatIndicatorFade:RegisterEndCallback(function()
Controls.ChatIndicatorWaitTimer:RegisterEndCallback(function()
instance.ChatIndicatorFade:RegisterEndCallback(function() instance.ChatIndicatorFade:SetToBeginning(); end);
instance.ChatIndicatorFade:Reverse();
end);
Controls.ChatIndicatorWaitTimer:SetToBeginning();
Controls.ChatIndicatorWaitTimer:Play();
end);
end
instance.ChatIndicatorFade:Play();
end
-- ===========================================================================
function OnChatPanelShown(fromPlayer:number, stayOnScreen:boolean)
for _, chatIndicatorFade in ipairs(m_uiChatIconsVisible) do
chatIndicatorFade:RegisterEndCallback(function() chatIndicatorFade:SetToBeginning(); end);
chatIndicatorFade:Reverse();
end
chatIndicatorFade = {};
end
-- ===========================================================================
function LateInitialize()
Controls.NextButton:RegisterCallback( Mouse.eLClick, OnScrollLeft );
Controls.PreviousButton:RegisterCallback( Mouse.eLClick, OnScrollRight );
Controls.LeaderScroll:SetScrollValue(1);
Events.SystemUpdateUI.Add( OnUpdateUI );
Events.DiplomacyMeet.Add( OnDiplomacyMeet );
Events.DiplomacySessionClosed.Add( OnDiplomacySessionClosed );
Events.DiplomacyDeclareWar.Add( OnDiplomacyWarStateChange );
Events.DiplomacyMakePeace.Add( OnDiplomacyWarStateChange );
Events.DiplomacyRelationshipChanged.Add( UpdateLeaders );
Events.InterfaceModeChanged.Add( OnInterfaceModeChanged );
Events.RemotePlayerTurnBegin.Add( OnTurnBegin );
Events.RemotePlayerTurnEnd.Add( OnTurnEnd );
Events.LocalPlayerTurnBegin.Add( function() OnTurnBegin(Game.GetLocalPlayer()); end );
Events.LocalPlayerTurnEnd.Add( function() OnTurnEnd(Game.GetLocalPlayer()); end );
Events.MultiplayerPlayerConnected.Add(UpdateLeaders);
Events.MultiplayerPostPlayerDisconnected.Add(UpdateLeaders);
Events.LocalPlayerChanged.Add(UpdateLeaders);
Events.PlayerInfoChanged.Add(UpdateLeaders);
Events.PlayerDefeat.Add(UpdateLeaders);
Events.PlayerRestored.Add(UpdateLeaders);
LuaEvents.ChatPanel_OnChatReceived.Add(OnChatReceived);
LuaEvents.WorldTracker_OnChatShown.Add(OnChatPanelShown);
LuaEvents.LaunchBar_Resize.Add( OnLaunchBarResized );
LuaEvents.PartialScreenHooks_Realize.Add(RealizeSize);
if not BASE_LateInitialize then -- Only update leaders if this is the last in the call chain.
UpdateLeaders();
end
end
-- ===========================================================================
function OnInit( isReload:boolean )
LateInitialize();
end
-- ===========================================================================
-- Main Initialize
-- ===========================================================================
function Initialize()
-- UI Events
ContextPtr:SetInitHandler( OnInit );
end
Initialize();