-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDiplomacyRibbon.lua
429 lines (373 loc) · 16.1 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
-- ===========================================================================
-- Leader container list on top of the HUD
-- ===========================================================================
include("DiplomacyRibbonSupport");
include("InstanceManager");
include("TeamSupport");
include("LeaderIcon");
-- ===========================================================================
-- CONSTANTS
-- ===========================================================================
local SCROLL_SPEED :number = 3;
local SIZE_LEADER :number = 52;
local PADDING_LEADER :number = 3;
local BG_PADDING_EDGE :number = 20;
local OFF_LEFT_ARROW :number = 20;
local OFF_LEFT_SCREEN :number = 360;
local RIGHT_HOOKS_INITIAL :number = 163;
local MIN_LEFT_HOOKS :number = 260;
local MINIMUM_BG_SIZE :number = 100;
local WORLD_TRACKER_OFFSET :number = 40;
local BAR_PADDING :number = 50;
local TEAM_RIBBON_SIZE :number = 53;
local TEAM_RIBBON_PREFIX :string = "ICON_TEAM_RIBBON_";
-- ===========================================================================
-- VARIABLES
-- ===========================================================================
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_maxNumLeaders :number = 0; -- Number of leaders that can fit in the ribbon
local m_isScrolling :boolean = false;
local m_uiLeadersByID :table = {};
local m_uiChatIconsVisible :table = {};
local m_kLeaderIM :table = InstanceManager:new("LeaderInstance", "LeaderContainer", Controls.LeaderStack);
local m_PartialScreenHookBar: table; -- = ContextPtr:LookUpControl( "/InGame/PartialScreenHooks/LaunchBacking" );
local m_LaunchBar : table; -- = ContextPtr:LookUpControl( "/InGame/LaunchBar/LaunchBacking" );
-- ===========================================================================
-- 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);
end
-- ===========================================================================
-- Clears leaders and re-adds them to the stack
-- ===========================================================================
function UpdateLeaders()
-- Clear previous list items
ResetLeaders();
-- Add entries for everyone we know (Majors only)
local aPlayers:table = PlayerManager.GetAliveMajors();
local localPlayerID:number = Game.GetLocalPlayer();
if (localPlayerID ~= -1) then -- It is possible to not have a local player!
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);
--Then, let's do a check to see if any of these players are duplicate leaders and track it.
-- Must go through entire list to detect duplicates (would be lovely if we had an IsUnique from PlayerConfigurations)
local metPlayers:table = {};
local isUniqueLeader:table = {};
for _, pPlayer in ipairs(aPlayers) do
local playerID:number = pPlayer:GetID();
if(playerID ~= localPlayerID) then
local playerMet:boolean = localDiplomacy:HasMet(playerID);
if (playerMet) then
local leaderName:string = PlayerConfigurations[playerID]:GetLeaderTypeName();
if (isUniqueLeader[leaderName] == nil) then
isUniqueLeader[leaderName] = true;
else
isUniqueLeader[leaderName] = false;
end
end
metPlayers[playerID] = playerMet;
end
end
--Then, add the leader icons.
for _, pPlayer in ipairs(aPlayers) do
local playerID:number = pPlayer:GetID();
if(playerID ~= localPlayerID) then
local playerMet:boolean = metPlayers[playerID];
local pPlayerConfig:table = PlayerConfigurations[playerID];
if (playerMet or (GameConfiguration.IsAnyMultiplayer() and pPlayerConfig:IsHuman())) then
if playerMet then
local leaderName:string = pPlayerConfig:GetLeaderTypeName();
AddLeader("ICON_"..leaderName, playerID, isUniqueLeader[leaderName]);
else
AddLeader("ICON_LEADER_DEFAULT", playerID);
end
end
end
end
end
Controls.LeaderStack:CalculateSize();
RealizeSize();
end
-- ===========================================================================
-- Updates size and location of BG and Scroll controls
-- ===========================================================================
-- Optional size argument being passed in through an event.
local BG_TILE_PADDING: number = 0;
function RealizeSize( barWidth:number )
local launchBarWidth = MIN_LEFT_HOOKS;
local partialScreenBarWidth = RIGHT_HOOKS_INITIAL;
-- TODO this should somehow be done relative to the other controls
m_PartialScreenHookBar = ContextPtr:LookUpControl( "/InGame/PartialScreenHooks/ButtonStack" );
m_LaunchBar = ContextPtr:LookUpControl( "/InGame/LaunchBar/ButtonStack" );
if (m_LaunchBar ~= nil) then
launchBarWidth = math.max(m_LaunchBar:GetSizeX() + WORLD_TRACKER_OFFSET + BG_TILE_PADDING, MIN_LEFT_HOOKS);
end
if (m_PartialScreenHookBar~=nil) then
partialScreenBarWidth = m_PartialScreenHookBar:GetSizeX() + BG_TILE_PADDING;
end
local screenWidth:number, screenHeight:number = UIManager:GetScreenSizeVal(); -- Cache screen dimensions
local maxSize:number = screenWidth - launchBarWidth - partialScreenBarWidth;
m_maxNumLeaders = math.floor(maxSize / (SIZE_LEADER + PADDING_LEADER));
local size:number = maxSize;
if(m_leadersMet == 0) then
Controls.LeaderBG:SetHide(true);
else
Controls.LeaderBG:SetHide(false);
size = m_maxNumLeaders * (SIZE_LEADER + PADDING_LEADER) - 8;
local bgSize;
if (m_leadersMet > m_maxNumLeaders) then
bgSize = m_maxNumLeaders * (SIZE_LEADER + PADDING_LEADER)+ BG_PADDING_EDGE;
else
bgSize = m_leadersMet * (SIZE_LEADER + PADDING_LEADER)+ BG_PADDING_EDGE;
end
Controls.LeaderBG:SetSizeX(math.max(bgSize, MINIMUM_BG_SIZE));
Controls.LeaderBGClip:SetSizeX(math.max(bgSize, MINIMUM_BG_SIZE));
Controls.RibbonContainer:SetSizeX(math.max(bgSize, MINIMUM_BG_SIZE));
end
Controls.LeaderScroll:SetSizeX(size);
Controls.RibbonContainer:ReprocessAnchoring();
Controls.RibbonContainer:SetOffsetX(partialScreenBarWidth);
Controls.LeaderBGClip:CalculateSize();
Controls.LeaderBGClip:ReprocessAnchoring();
Controls.LeaderScroll:CalculateSize();
Controls.LeaderScroll:ReprocessAnchoring();
RealizeScroll();
end
-- ===========================================================================
-- Updates visibility of previous and next buttons
-- ===========================================================================
function RealizeScroll()
Controls.NextButtonContainer:SetHide(not CanScroll(-1));
Controls.PreviousButtonContainer:SetHide(not CanScroll(1));
end
-- ===========================================================================
-- Determines visibility of previous and next buttons
-- ===========================================================================
function CanScroll(direction : number)
if(direction < 0) then
return m_scrollIndex > 0;
else
return m_leadersMet - m_scrollIndex > m_maxNumLeaders;
end
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 - m_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
-- ===========================================================================
-- 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
-- ===========================================================================
-- LocalPlayerTurnBegin / RemotePlayerTurnBegin Callback
-- ===========================================================================
function OnTurnBegin(playerID:number)
local leader:table = m_uiLeadersByID[playerID];
if(leader ~= nil) then
leader.LeaderContainer:SetToBeginning();
leader.LeaderContainer:Play();
end
end
function OnTurnEnd(playerID:number)
if(playerID ~= -1) then
local leader = m_uiLeadersByID[playerID];
if(leader ~= nil) then
leader.LeaderContainer:Reverse();
end
end
end
-- ===========================================================================
-- UI Callback
-- ===========================================================================
function OnScrollLeft()
if CanScroll(-1) then Scroll(-1); end
end
-- ===========================================================================
-- UI Callback
-- ===========================================================================
function OnScrollRight()
if CanScroll(1) then Scroll(1); end
end
-- ===========================================================================
-- Debug Helper
-- ===========================================================================
function DebugWorstCase()
-- Clear previous list items
ResetLeaders();
for i=1, 50 do
AddLeader("ICON_LEADER_DEFAULT", i);
end
Controls.LeaderStack:CalculateSize();
RealizeSize();
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
-- ===========================================================================
-- INIT
-- ===========================================================================
function Initialize()
--DebugWorstCase();
UpdateLeaders();
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(RealizeSize);
LuaEvents.PartialScreenHooks_Resize.Add(RealizeSize);
Controls.NextButton:RegisterCallback( Mouse.eLClick, OnScrollLeft );
Controls.PreviousButton:RegisterCallback( Mouse.eLClick, OnScrollRight );
end
Initialize();