forked from civfanatics/Civ6-UIFiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductionManager.lua
453 lines (379 loc) · 16.1 KB
/
ProductionManager.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
-- ===========================================================================--
-- Production Manager
-- Customize and review production queues
--
-- Original Authors: Keaton VanAuken
-- ===========================================================================
include("InstanceManager");
include("Civ6Common");
include("ProductionHelper");
-- ===========================================================================
-- CONSTANTS
-- ===========================================================================
local RELOAD_CACHE_ID:string = "ProductionManager"; -- Must be unique (usually the same as the file name)
local FIELD_CITY_ID:string = "cityID";
local FIELD_INSTANCE_MANAGER:string = "queueInstanceManager";
local FIELD_QUEUE_INSTANCES:string = "queueInstances";
-- ===========================================================================
-- VARIABLES
-- ===========================================================================
local m_CityInstanceIM:table = InstanceManager:new("CityInstance", "CityButton", Controls.CityStack);
local m_SelectedCityInstance:table = nil;
local m_kSelectedQueueItem:table = {Parent = nil, Button = nil, Index = -1};
-- ===========================================================================
function Refresh(sortFunc, sortName)
local pLocalPlayer:table = Players[Game.GetLocalPlayer()];
m_CityInstanceIM:ResetInstances();
-- Grab players cities so we can sort easily
local sortedCities:table = {}
local pPlayerCities:table = pLocalPlayer:GetCities();
for i, pCity in pPlayerCities:Members() do
table.insert(sortedCities, pCity);
end
-- Sort
if sortFunc ~= nil then
table.sort(sortedCities, sortFunc);
end
if sortName ~= nil then
Controls.FilterPulldown:GetButton():SetText(Locale.Lookup("LOC_PRODUCTION_MANAGER_SORT_BY", Locale.Lookup(sortName)));
else
Controls.FilterPulldown:GetButton():SetText(Locale.Lookup("LOC_PRODUCTION_MANAGER_SORT_BY", Locale.Lookup("LOC_PRODUCTION_MANAGER_FILTER_FOUNDING")));
end
-- Populate using sorted list
for _, pCity in ipairs(sortedCities) do
local cityInstance:table = CreateCityInstance(pCity);
RefreshCityInstance(cityInstance, pCity);
end
-- Set button of the currently selected city as selected
local pSelectedCity = UI.GetHeadSelectedCity();
if pSelectedCity then
SetSelectedCity(pSelectedCity:GetID());
end
end
-- ===========================================================================
function CreateCityInstance( city:table )
local cityInstance:table = m_CityInstanceIM:GetInstance();
cityInstance[FIELD_CITY_ID] = city:GetID();
-- Create an instance manager for the queue if we need one
if cityInstance[FIELD_INSTANCE_MANAGER] == nil then
cityInstance[FIELD_INSTANCE_MANAGER] = InstanceManager:new("ProductionQueueItem", "Top");
end
return cityInstance;
end
-- ===========================================================================
function FindCityInstance( cityID:number )
for i=1, m_CityInstanceIM.m_iCount, 1 do
local instance:table = m_CityInstanceIM:GetAllocatedInstance(i);
if instance and instance[FIELD_CITY_ID] == cityID then
return instance;
end
end
return nil;
end
-- ===========================================================================
function RefreshCityInstance( instance:table, city:table )
local cityID:number = city:GetID();
local ownerID:number = city:GetOwner();
-- Update name and capital icon
instance.CapitalIcon:SetHide(not city:IsCapital());
instance.CityName:SetText(Locale.Lookup(city:GetName()));
-- Update current production
local hasAnyProduction:boolean = RefreshCurrentProduction(instance, ownerID, cityID);
if hasAnyProduction then
instance.CurrentProductionGrid:SetHide(false);
instance.NoProductionContainer:SetHide(true);
else
instance.CurrentProductionGrid:SetHide(true);
instance.NoProductionContainer:SetHide(false);
end
-- Update production queue
instance[FIELD_QUEUE_INSTANCES] = RefreshProductionQueue(instance[FIELD_INSTANCE_MANAGER], instance.QueueStack, ownerID, cityID);
RefreshQueueButtonStates(instance);
-- Setup callbacks
instance.CityButton:RegisterCallback( Mouse.eLClick, function() OnCityInstanceClicked(ownerID, cityID); end );
instance.TrashButton:RegisterCallback( Mouse.eLClick, OnTrashButtonClicked );
instance.DarkenButton:RegisterCallback( Mouse.eLClick, DeselectItem );
instance.DarkenButton:RegisterCallback( Mouse.eRClick, DeselectItem );
end
-- ===========================================================================
function RefreshQueueButtonStates( kInstance:table )
local pSelectedCity = UI.GetHeadSelectedCity();
local bIsSelectedCity:boolean = pSelectedCity ~= nil and pSelectedCity:GetID() == kInstance[FIELD_CITY_ID];
kInstance.CurrentProductionGrid:SetDisabled(false);
kInstance.CurrentProductionGrid:RegisterCallback( Mouse.eLClick, function() OnQueueItemClicked(kInstance, kInstance.CurrentProductionGrid); end);
kInstance.CurrentProductionGrid:RegisterCallback( Mouse.eRClick, function() OnQueueItemRightClicked(kInstance, kInstance.CurrentProductionGrid); end);
for i,kQueueInstance in ipairs(kInstance[FIELD_QUEUE_INSTANCES]) do
if kQueueInstance[FIELD_QUEUE_INDEX] ~= -1 then
kQueueInstance.Top:SetDisabled(false);
kQueueInstance.Top:RegisterCallback( Mouse.eLClick, function() OnQueueItemClicked(kQueueInstance, kQueueInstance.Top); end);
kQueueInstance.Top:RegisterCallback( Mouse.eRClick, function() OnQueueItemRightClicked(kQueueInstance, kQueueInstance.Top); end);
end
end
end
-- ===========================================================================
function DeselectItem()
if m_kSelectedQueueItem.Parent ~= nil then
local kParentControl:table = m_kSelectedQueueItem.Parent;
if kParentControl.ProductionIcon ~= nil then
kParentControl.ProductionIcon:SetAlpha(1.0);
end
end
m_kSelectedQueueItem.Parent = nil;
m_kSelectedQueueItem.Button = nil;
m_kSelectedQueueItem.Index = -1;
LuaEvents.ProductionManager_SelectedIndexChanged(m_kSelectedQueueItem.Index);
DisableMouseIcon();
HighlightButtons(false);
ClearDarkenCities();
end
-- ===========================================================================
function SelectItem( kParentControl:table, kButtonControl:table )
if kParentControl ~= nil and kParentControl[FIELD_QUEUE_INDEX] then
if kParentControl.ProductionIcon ~= nil then
kParentControl.ProductionIcon:SetAlpha(SELECTED_ICON_ALPHA);
end
m_kSelectedQueueItem.Parent = kParentControl;
m_kSelectedQueueItem.Button = kButtonControl;
m_kSelectedQueueItem.Index = kParentControl[FIELD_QUEUE_INDEX];
-- We selected an item from another city so select that city
if m_SelectedCityInstance ~= kParentControl then
m_SelectedCityInstance = FindCityInstance(kParentControl[FIELD_CITY_ID]);
local pCity:table = CityManager.GetCity(Game.GetLocalPlayer(), kParentControl[FIELD_CITY_ID]);
UI.SelectCity( pCity );
end
LuaEvents.ProductionManager_SelectedIndexChanged(m_kSelectedQueueItem.Index);
EnableMouseIcon(kParentControl[FIELD_ICON_TEXTURE]);
HighlightButtons(true);
DarkenOtherCities();
end
end
-- ===========================================================================
function ScrollToSelectedCity()
for i=1, m_CityInstanceIM.m_iCount, 1 do
local instance:table = m_CityInstanceIM:GetAllocatedInstance(i);
if instance and instance == m_SelectedCityInstance then
local iScrollToPixel:number = 0;
if i > 1 then
iScrollToPixel = (i * instance.CityButton:GetSizeY()) + ((i - 1) * 2);
end
local iDesiredScroll:number = iScrollToPixel / Controls.CityStack:GetSizeY();
Controls.CityStackScroll:SetScrollValue(iDesiredScroll);
return;
end
end
end
-- ===========================================================================
function OnTrashButtonClicked()
if m_kSelectedQueueItem.Index ~= -1 then
RemoveQueueItem(m_kSelectedQueueItem.Index);
end
end
-- ===========================================================================
function DarkenOtherCities()
for i=1, m_CityInstanceIM.m_iCount, 1 do
local instance:table = m_CityInstanceIM:GetAllocatedInstance(i);
if instance and instance ~= m_SelectedCityInstance then
instance.DarkenBox:SetHide(false);
end
end
end
-- ===========================================================================
function ClearDarkenCities()
for i=1, m_CityInstanceIM.m_iCount, 1 do
local instance:table = m_CityInstanceIM:GetAllocatedInstance(i);
if instance then
instance.DarkenBox:SetHide(true);
end
end
end
-- ===========================================================================
function HighlightButtons( bShouldHighlight:boolean )
if m_SelectedCityInstance ~= nil then
m_SelectedCityInstance.CurrentProductionGrid:SetSelected(bShouldHighlight);
local kQueueInstance_IM = m_SelectedCityInstance[FIELD_INSTANCE_MANAGER];
for i=1, kQueueInstance_IM.m_iCount, 1 do
local instance:table = kQueueInstance_IM:GetAllocatedInstance(i);
if instance then
if instance[FIELD_QUEUE_INDEX] ~= -1 then
instance.Top:SetSelected(bShouldHighlight);
end
end
end
m_SelectedCityInstance.TrashButton:SetSelected(bShouldHighlight);
m_SelectedCityInstance.TrashButton:SetDisabled(not bShouldHighlight);
end
end
-- ===========================================================================
function OnQueueItemClicked( kParentControl:table, kButtonControl:table )
-- Prevent interaction with completed items
if kParentControl.CompletedArea and not kParentControl.CompletedArea:IsHidden() then
return;
end
if kParentControl[FIELD_QUEUE_INDEX] == m_kSelectedQueueItem.Index then
-- This item is already selected so just unselect it
DeselectItem();
return;
elseif m_kSelectedQueueItem.Index == -1 then
-- Nothing else is selected so select this item
SelectItem(kParentControl, kButtonControl);
return;
else
-- Swap currently selected item with this item
if kParentControl ~= nil and kParentControl[FIELD_QUEUE_INDEX] then
SwapQueueItem(kParentControl[FIELD_QUEUE_INDEX], m_kSelectedQueueItem.Index);
return;
end
end
-- Unselect current selection if action isn't consumed
DeselectItem();
end
-- ===========================================================================
function OnQueueItemRightClicked( kParentControl:table, kButtonControl:table )
if kParentControl[FIELD_QUEUE_INDEX] ~= -1 then
-- We selected an item from another city so select that city
if m_SelectedCityInstance ~= kParentControl then
m_SelectedCityInstance = FindCityInstance(kParentControl[FIELD_CITY_ID]);
local pCity:table = CityManager.GetCity(Game.GetLocalPlayer(), kParentControl[FIELD_CITY_ID]);
UI.SelectCity( pCity );
end
RemoveQueueItem(kParentControl[FIELD_QUEUE_INDEX]);
end
end
-- ===========================================================================
function SetupFilters()
AddFilter("LOC_PRODUCTION_MANAGER_FILTER_FOUNDING"); -- Founding requires no sort function since the default order is by founding
AddFilter("LOC_PRODUCTION_MANAGER_FILTER_NAME", function(a, b) return Locale.Lookup(a:GetName()) < Locale.Lookup(b:GetName()); end);
AddFilter("LOC_PRODUCTION_MANAGER_FILTER_POPULATION", function(a, b) return a:GetPopulation() > b:GetPopulation(); end);
Controls.FilterPulldown:CalculateInternals();
end
-- ===========================================================================
function AddFilter( name:string, sortFunc )
local controlTable:table = {};
Controls.FilterPulldown:BuildEntry( "FilterItemInstance", controlTable );
controlTable.DescriptionText:SetText(Locale.Lookup(name));
controlTable.Button:RegisterCallback(Mouse.eLClick, function() Refresh(sortFunc, name); end);
end
-- ===========================================================================
function SetSelectedCity( cityID:number )
for i=1, m_CityInstanceIM.m_iCount, 1 do
local instance:table = m_CityInstanceIM:GetAllocatedInstance(i);
if instance and instance[FIELD_CITY_ID] == cityID then
instance.CityButton:SetSelected(true);
m_SelectedCityInstance = instance;
else
instance.CityButton:SetSelected(false);
end
RefreshQueueButtonStates(instance)
end
end
-- ===========================================================================
function OnCityInstanceClicked( playerID:number, cityID:number )
local pCity:table = CityManager.GetCity(playerID, cityID);
UI.SelectCity( pCity );
end
-- ===========================================================================
function Close()
ContextPtr:SetHide(true);
end
-- ===========================================================================
function Open()
Refresh();
ContextPtr:SetHide(false);
end
-- ===========================================================================
function OnOpen()
Open();
ScrollToSelectedCity();
end
-- ===========================================================================
function OnClose()
Close();
end
-- ===========================================================================
function OnToggle()
if ContextPtr:IsHidden() then
Open();
else
Close();
end
end
-- ===========================================================================
function OnInit(isReload:boolean)
if isReload then
LuaEvents.GameDebug_GetValues( RELOAD_CACHE_ID );
end
SetupFilters();
end
-- ===========================================================================
function OnShutdown()
LuaEvents.GameDebug_AddValue(RELOAD_CACHE_ID, "isHidden", ContextPtr:IsHidden());
end
-- ===========================================================================
function OnGameDebugReturn(context:string, contextTable:table)
if context == RELOAD_CACHE_ID then
if contextTable["isHidden"] ~= nil then
if contextTable["isHidden"] == true then
Close();
else
Open();
end
end
end
end
-- ===========================================================================
function OnCityProductionQueueChanged(playerID, cityID, changeType, queueIndex)
if (ContextPtr ~= nil and ContextPtr:IsHidden()) or playerID ~= Game.GetLocalPlayer() then
return;
end
local pCityInstance:table = FindCityInstance(cityID);
if pCityInstance ~= nil then
local pCity:table = CityManager.GetCity(playerID, cityID);
if pCity ~= nil then
RefreshCityInstance(pCityInstance, pCity);
end
end
DeselectItem();
end
-- ===========================================================================
function OnCitySelectionChanged( owner:number, cityID:number, i, j, k, isSelected:boolean, isEditable:boolean)
if (ContextPtr ~= nil and ContextPtr:IsHidden()) or owner ~= Game.GetLocalPlayer() then
return;
end
SetSelectedCity(cityID);
end
-- ===========================================================================
function OnUnitSelectionChanged( playerID : number, unitID : number, hexI : number, hexJ : number, hexK : number, bSelected : boolean, bEditable : boolean )
local localPlayerID:number = Game.GetLocalPlayer();
if playerID == localPlayerID then
-- If a unit is selected and this is showing; hide it.
local pSelectedUnit:table = UI.GetHeadSelectedUnit();
if pSelectedUnit ~= nil and not ContextPtr:IsHidden() then
Close();
end
end
end
-- ===========================================================================
function OnProductionClicked()
if m_kSelectedQueueItem.Index ~= -1 then
DeselectItem();
end
end
-- ===========================================================================
function OnCancelManagerSelection()
DeselectItem();
end
-- ===========================================================================
function Initialize()
ContextPtr:SetInitHandler( OnInit );
ContextPtr:SetShutdown( OnShutdown );
LuaEvents.GameDebug_Return.Add( OnGameDebugReturn );
Events.CityProductionQueueChanged.Add( OnCityProductionQueueChanged );
Events.CitySelectionChanged.Add( OnCitySelectionChanged );
Events.UnitSelectionChanged.Add( OnUnitSelectionChanged );
LuaEvents.ProductionPanel_ToggleManager.Add( OnToggle );
LuaEvents.ProductionPanel_OpenManager.Add( OnOpen );
LuaEvents.ProductionPanel_CloseManager.Add( OnClose );
LuaEvents.ProductionPanel_ProductionClicked.Add( OnProductionClicked );
LuaEvents.ProductionPanel_CancelManagerSelection.Add( OnCancelManagerSelection );
end
Initialize();