-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmappinlistpanel.lua
249 lines (214 loc) · 8.85 KB
/
mappinlistpanel.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
----------------------------------------------------------------
-- Map Pin List Panel
----------------------------------------------------------------
include( "MapTacks" );
local m_playerMapPins :table = {};
local m_MapPinListButtonToPinEntry :table = {}; -- map pin entries keyed to their MapPinListButton object string names. This is currently just used for sorting, please becareful if you use it for anything else as it is cleared after use.
local PlayerMapPinListTTStr :string = Locale.Lookup( "LOC_MAP_PIN_LIST_REMOTE_PIN_TOOLTIP" );
local RemoteMapPinListTTStr :string = Locale.Lookup( "LOC_MAP_PIN_LIST_REMOTE_PIN_TOOLTIP" );
-------------------------------------------------
-- Map Pin List Scripting
-------------------------------------------------
function GetMapPinConfig(iPlayerID :number, mapPinID :number)
local playerCfg :table = PlayerConfigurations[iPlayerID];
if(playerCfg ~= nil) then
local playerMapPins :table = playerCfg:GetMapPins();
if(playerMapPins ~= nil) then
return playerMapPins[mapPinID];
end
end
return nil;
end
-------------------------------------------------
-------------------------------------------------
function SetMapPinIcon(imageControl :table, mapPinIconName :string)
if(imageControl ~= nil and mapPinIconName ~= nil) then
local iconName = mapPinIconName;
if(not imageControl:SetIcon(iconName)) then
imageControl:SetIcon(MapTacks.UNKNOWN);
end
end
end
-------------------------------------------------
-------------------------------------------------
function SortMapPinEntryStack(a, b)
local controlStringA = tostring(a);
local controlStringB = tostring(b);
local pinEntryA = m_MapPinListButtonToPinEntry[controlStringA];
local pinEntryB = m_MapPinListButtonToPinEntry[controlStringB];
if (pinEntryA and pinEntryB) then
if pinEntryA.unnamedID ~= pinEntryB.unnamedID then
-- sort unnamed pins at the end, in ID order
return (pinEntryA.unnamedID or -1) < (pinEntryB.unnamedID or -1);
end
local pinNameA :string = Locale.ToLower(pinEntryA.MapPinName:GetText());
local pinNameB :string = Locale.ToLower(pinEntryB.MapPinName:GetText());
return pinNameA < pinNameB;
else
-- This is obviously bad, why are they not in the list? Use the control strings to test against something.
return controlStringA < controlStringB;
end
end
-------------------------------------------------
-------------------------------------------------
function UpdateMapPinListEntry(iPlayerID :number, mapPinID :number)
local mapPinEntry = GetMapPinListEntry(iPlayerID, mapPinID);
local mapPinCfg = GetMapPinConfig(iPlayerID, mapPinID);
if(mapPinCfg ~= nil and mapPinEntry ~= nil) then
-- Determine map pin display name.
local pinName :string = mapPinCfg:GetName();
if(pinName == nil) then
local id = mapPinCfg:GetID();
pinName = Locale.Lookup( "LOC_MAP_PIN_DEFAULT_NAME", id+1 );
mapPinEntry.unnamedID = id; -- for sorting
end
mapPinEntry.MapPinName:SetText(pinName);
-- Set pin colors based on owner's player colors.
--local primaryColor, secondaryColor = UI.GetPlayerColors(iPlayerID);
--mapPinEntry.PrimaryColorBox:SetColor(primaryColor);
--mapPinEntry.SecondaryColorBox:SetColor(secondaryColor);
-- Set pin icon
SetMapPinIcon(mapPinEntry.IconImage, mapPinCfg:GetIconName());
-- Is this map pin visible on this list?
local localPlayerID = Game.GetLocalPlayer();
local showMapPin = mapPinCfg:IsVisible(localPlayerID);
mapPinEntry.Root:SetHide(not showMapPin);
end
end
-------------------------------------------------
-------------------------------------------------
function GetMapPinListEntry(iPlayerID :number, mapPinID :number)
local playerMapPins = m_playerMapPins[iPlayerID];
if(playerMapPins == nil) then
playerMapPins = {};
m_playerMapPins[iPlayerID] = playerMapPins;
end
local mapPinEntry = playerMapPins[mapPinID];
if(mapPinEntry == nil) then
mapPinEntry = {};
ContextPtr:BuildInstanceForControl( "MapPinListEntry", mapPinEntry, Controls.MapPinEntryStack);
playerMapPins[mapPinID] = mapPinEntry;
m_MapPinListButtonToPinEntry[tostring(mapPinEntry.Root)] = mapPinEntry;
mapPinEntry.MapPinListButton:SetVoids(iPlayerID, mapPinID);
if( iPlayerID == Game.GetLocalPlayer()) then
mapPinEntry.EditMapPin:SetHide(false);
mapPinEntry.EditMapPin:SetVoids(iPlayerID, mapPinID);
mapPinEntry.EditMapPin:RegisterCallback(Mouse.eLClick, OnMapPinEntryEdit);
else
mapPinEntry.EditMapPin:SetHide(true);
end
mapPinEntry.MapPinListButton:RegisterCallback(Mouse.eLClick, OnMapPinEntryLeftClick);
if(iPlayerID == Game.GetLocalPlayer()) then
mapPinEntry.MapPinListButton:SetToolTipString(PlayerMapPinListTTStr);
else
mapPinEntry.MapPinListButton:SetToolTipString(RemoteMapPinListTTStr);
end
UpdateMapPinListEntry(iPlayerID, mapPinID);
Controls.MapPinEntryStack:CalculateSize();
end
return mapPinEntry;
end
-------------------------------------------------
-------------------------------------------------
function BuildMapPinList()
m_playerMapPins = {};
m_MapPinListButtonToPinEntry = {};
Controls.MapPinEntryStack:DestroyAllChildren();
-- Call GetMapPinListEntry on every map pin to initially create the entries.
for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS-1 do
local pPlayerConfig = PlayerConfigurations[iPlayer];
if(pPlayerConfig ~= nil) then
local pPlayerPins :table = pPlayerConfig:GetMapPins();
for ii, mapPinCfg in pairs(pPlayerPins) do
local pinID :number = mapPinCfg:GetID();
GetMapPinListEntry(iPlayer, pinID);
end
end
end
-- Sort by names when we're done.
Controls.MapPinEntryStack:SortChildren(SortMapPinEntryStack);
-- Don't need this anymore, get rid of the references so they can be properly released!
m_MapPinListButtonToPinEntry = {};
-- Recalc after sorting so the anchoring can account for hidden elements.
Controls.MapPinEntryStack:CalculateSize();
-- Dynamically resize scroll panel
local maxY = 15 * 25 - 1; -- show up to 15 pins without a scrollbar
local stackY = Controls.MapPinEntryStack:GetSizeY();
local panelY = math.min(maxY, stackY);
Controls.MapPinScrollPanel:SetSizeY(panelY);
Controls.MapPinStack:CalculateSize();
end
-------------------------------------------------
-- Button Event Handlers
-------------------------------------------------
function OnMapPinEntryLeftClick(iPlayerID :number, mapPinID :number)
local mapPinCfg = GetMapPinConfig(iPlayerID, mapPinID);
if(mapPinCfg ~= nil) then
local hexX :number = mapPinCfg:GetHexX();
local hexY :number = mapPinCfg:GetHexY();
UI.LookAtPlot(hexX, hexY);
-- Would love to find a fun effect to play on the map pin when you look at it so that you don't lose it.
-- UI.OnNaturalWonderRevealed(hexX, hexY);
end
end
function OnMapPinEntryEdit(iPlayerID :number, mapPinID :number)
if(iPlayerID == Game.GetLocalPlayer()) then
-- You're only allowed to edit your own pins.
local mapPinCfg = GetMapPinConfig(iPlayerID, mapPinID);
if(mapPinCfg ~= nil) then
local hexX :number = mapPinCfg:GetHexX();
local hexY :number = mapPinCfg:GetHexY();
UI.LookAtPlot(hexX, hexY);
LuaEvents.MapPinPopup_RequestMapPin(hexX, hexY);
end
end
end
-------------------------------------------------
-------------------------------------------------
function OnAddPinButton()
-- Toggles map pin interface mode
if (UI.GetInterfaceMode() == InterfaceModeTypes.PLACE_MAP_PIN) then
UI.SetInterfaceMode(InterfaceModeTypes.SELECTION);
else
UI.SetInterfaceMode(InterfaceModeTypes.PLACE_MAP_PIN);
end
end
-------------------------------------------------
-- External Event Handlers
-------------------------------------------------
function OnPlayerInfoChanged(playerID)
BuildMapPinList();
end
-------------------------------------------------
-------------------------------------------------
function OnInterfaceModeChanged( eNewMode:number )
if (UI.GetInterfaceMode() == InterfaceModeTypes.PLACE_MAP_PIN) then
Controls.AddPinButton:SetText(Locale.Lookup("LOC_GREAT_WORKS_PLACING"));
Controls.AddPinButton:SetSelected(true);
else
Controls.AddPinButton:SetText(Locale.Lookup("LOC_HUD_MAP_PLACE_MAP_PIN"));
Controls.AddPinButton:SetSelected(false);
end
end
function OnLocalPlayerChanged( eLocalPlayer:number , ePrevLocalPlayer:number )
BuildMapPinList();
end
-------------------------------------------------
-- ShowHideHandler
-------------------------------------------------
function ShowHideHandler( bIsHide, bIsInit )
if(not bIsHide) then
end
end
ContextPtr:SetShowHideHandler( ShowHideHandler );
-- ===========================================================================
function Initialize()
Controls.AddPinButton:RegisterCallback( Mouse.eLClick, OnAddPinButton );
Controls.AddPinButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Events.PlayerInfoChanged.Add(OnPlayerInfoChanged);
Events.InterfaceModeChanged.Add( OnInterfaceModeChanged );
Events.LocalPlayerChanged.Add( OnLocalPlayerChanged );
BuildMapPinList();
end
Initialize()
-- vim: sw=4 ts=4