This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
forked from Randactyl/InventoryGridView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.lua
484 lines (397 loc) · 17.3 KB
/
adapter.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
local IGV = InventoryGridView
IGV.adapter = {}
local util = IGV.util
local settings = IGV.settings
local adapter = IGV.adapter
local LEFT_PADDING = 25
--[[----------------------------------------------------------------------------
Ported ZOS code from esoui\libraries\zo_templates\scrolltemplates.lua
--]]----------------------------------------------------------------------------
local MAX_FADE_VALUE = 64
local ANIMATE_INSTANTLY = true
local function UpdateScrollFade(useFadeGradient, scroll, slider, sliderValue)
if(useFadeGradient) then
local sliderMin, sliderMax = slider:GetMinMax()
sliderValue = sliderValue or slider:GetValue()
if(sliderValue > sliderMin) then
scroll:SetFadeGradient(1, 0, 1, zo_min(sliderValue - sliderMin, MAX_FADE_VALUE))
else
scroll:SetFadeGradient(1, 0, 0, 0)
end
if(sliderValue < sliderMax) then
scroll:SetFadeGradient(2, 0, -1, zo_min(sliderMax - sliderValue, MAX_FADE_VALUE))
else
scroll:SetFadeGradient(2, 0, 0, 0);
end
else
scroll:SetFadeGradient(1, 0, 0, 0)
scroll:SetFadeGradient(2, 0, 0, 0)
end
end
local function AreSelectionsEnabled(self)
return self.selectionTemplate or self.selectionCallback
end
local function RemoveAnimationOnControl(control, animationFieldName, animateInstantly)
if control[animationFieldName] then
if animateInstantly then
control[animationFieldName]:PlayInstantlyToStart()
else
control[animationFieldName]:PlayBackward()
end
end
end
local function UnhighlightControl(self, control)
RemoveAnimationOnControl(control, "HighlightAnimation")
self.highlightedControl = nil
if(self.highlightCallback) then
self.highlightCallback(control, false)
end
end
local function UnselectControl(self, control, animateInstantly)
RemoveAnimationOnControl(control, "SelectionAnimation", animateInstantly)
self.selectedControl = nil
end
local function AreDataEqualSelections(self, data1, data2)
if(data1 == data2) then
return true
end
if(data1 == nil or data2 == nil) then
return false
end
local dataEntry1 = data1.dataEntry
local dataEntry2 = data2.dataEntry
if(dataEntry1.typeId == dataEntry2.typeId) then
local equalityFunction = self.dataTypes[dataEntry1.typeId].equalityFunction
if(equalityFunction) then
return equalityFunction(data1, data2)
end
end
return false
end
local function FreeActiveScrollListControl(self, i)
local currentControl = self.activeControls[i]
local currentDataEntry = currentControl.dataEntry
local dataType = self.dataTypes[currentDataEntry.typeId]
if(self.highlightTemplate and currentControl == self.highlightedControl) then
UnhighlightControl(self, currentControl)
if(self.highlightLocked) then
self.highlightLocked = false
end
end
if(currentControl == self.pendingHighlightControl) then
self.pendingHighlightControl = nil
end
if AreSelectionsEnabled(self) and currentControl == self.selectedControl then
UnselectControl(self, currentControl, ANIMATE_INSTANTLY)
end
if(dataType.hideCallback) then
dataType.hideCallback(currentControl, currentControl.dataEntry.data)
end
dataType.pool:ReleaseObject(currentControl.key)
currentControl.key = nil
currentControl.dataEntry = nil
self.activeControls[i] = self.activeControls[#self.activeControls]
self.activeControls[#self.activeControls] = nil
end
local HIDE_SCROLLBAR = true
local function ResizeScrollBar(self, scrollableDistance)
local scrollBarHeight = self.scrollbar:GetHeight()
local scrollListHeight = ZO_ScrollList_GetHeight(self)
if(scrollableDistance > 0) then
self.scrollbar:SetEnabled(true)
if self.ScrollBarHiddenCallback then
self.ScrollBarHiddenCallback(self, not HIDE_SCROLLBAR)
else
self.scrollbar:SetHidden(false)
end
self.scrollbar:SetThumbTextureHeight(scrollBarHeight * scrollListHeight /(scrollableDistance + scrollListHeight))
if(self.offset > scrollableDistance) then
self.offset = scrollableDistance
end
self.scrollbar:SetMinMax(0, scrollableDistance)
else
self.offset = 0
self.scrollbar:SetThumbTextureHeight(scrollBarHeight)
self.scrollbar:SetMinMax(0, 0)
self.scrollbar:SetEnabled(false)
if(self.hideScrollBarOnDisabled) then
if self.ScrollBarHiddenCallback then
self.ScrollBarHiddenCallback(self, HIDE_SCROLLBAR)
else
self.scrollbar:SetHidden(true)
end
end
end
end
local function CompareEntries(topEdge, compareData)
return topEdge - compareData.bottom
end
local SCROLL_LIST_UNIFORM = 1
local function FindStartPoint(self, topEdge)
if(self.mode == SCROLL_LIST_UNIFORM) then
return zo_floor(topEdge / self.controlHeight)+1
else
local found, insertPoint = zo_binarysearch(topEdge, self.data, CompareEntries)
return insertPoint
end
end
--[[----------------------------------------------------------------------------
Modified version of ZO_ScrollList_UpdateScroll(self) from
esoui\libraries\zo_templates\scrolltemplates.lua
--]]----------------------------------------------------------------------------
local consideredMap = {}
local function IGV_ScrollList_UpdateScroll_Grid(self)
local windowHeight = ZO_ScrollList_GetHeight(self)
--Added---------------------------------------------------------------------
local gridIconSize = settings.GetGridIconSize()
local contentsWidth = self.contents:GetWidth()
local contentsWidthMinusPadding = contentsWidth - LEFT_PADDING
local itemsPerRow = zo_floor(contentsWidthMinusPadding / gridIconSize)
local numControls = #self.data or 0
local numRows = zo_ceil(numControls / itemsPerRow)
local gridSpacing = .5
local totalControlHeight = gridIconSize * numRows
local totalSpacingHeight = gridSpacing * (numRows - 1)
local scrollableDistance = (totalControlHeight + totalSpacingHeight) - windowHeight
local function GetTargetTopAndLeftPositions(viewIndex)
local totalControlWidth = gridIconSize + gridSpacing
local controlTop = zo_floor((viewIndex - 1) / itemsPerRow) * totalControlWidth
local controlLeft = ((viewIndex - 1) % itemsPerRow) * totalControlWidth + LEFT_PADDING
return controlTop, controlLeft
end
self.controlHeight = gridIconSize
ResizeScrollBar(self, scrollableDistance)
----------------------------------------------------------------------------
local controlHeight = self.controlHeight
local activeControls = self.activeControls
local offset = self.offset
UpdateScrollFade(self.useFadeGradient, self.contents, self.scrollbar, offset)
--remove active controls that are now hidden
local i = 1
local numActive = #activeControls
while(i <= numActive) do
local currentDataEntry = activeControls[i].dataEntry
if(currentDataEntry.bottom < offset or currentDataEntry.top > offset + windowHeight) then
FreeActiveScrollListControl(self, i)
numActive = numActive - 1
else
i = i + 1
end
consideredMap[currentDataEntry] = true
end
--add revealed controls
local firstInViewIndex = FindStartPoint(self, offset)
local data = self.data
local dataTypes = self.dataTypes
local visibleData = self.visibleData
local mode = self.mode
local i = firstInViewIndex
local visibleDataIndex = visibleData[i]
local dataEntry = data[visibleDataIndex]
local bottomEdge = offset + windowHeight
--Modified------------------------------------------------------------------
local controlTop, controlLeft
if dataEntry then
--removed isUniform check because we're assuming always uniform
controlTop, controlLeft = GetTargetTopAndLeftPositions(i)
end
----------------------------------------------------------------------------
while(dataEntry and controlTop <= bottomEdge) do
if(not consideredMap[dataEntry]) then
local dataType = dataTypes[dataEntry.typeId]
local controlPool = dataType.pool
local control, key = controlPool:AcquireObject()
control:SetHidden(false)
control.dataEntry = dataEntry
control.key = key
control.index = visibleDataIndex
--Added-------------------------------------------------------------
control.isGrid = false
--------------------------------------------------------------------
if(dataType.setupCallback) then
dataType.setupCallback(control, dataEntry.data, self)
end
table.insert(activeControls, control)
consideredMap[dataEntry] = true
if(AreDataEqualSelections(self, dataEntry.data, self.selectedData)) then
SelectControl(self, control)
end
--even uniform active controls need to know their position to determine if they are still active
--Modified----------------------------------------------------------
--removed isUniform check because we're assuming always uniform
dataEntry.top = controlTop
dataEntry.bottom = controlTop + controlHeight
--------------------------------------------------------------------
--Added-------------------------------------------------------------
dataEntry.left = controlLeft
dataEntry.right = controlLeft + gridIconSize
--------------------------------------------------------------------
end
i = i + 1
visibleDataIndex = visibleData[i]
dataEntry = data[visibleDataIndex]
--Modified--------------------------------------------------------------
if(dataEntry) then
--removed isUniform check because we're assuming always uniform
controlTop, controlLeft = GetTargetTopAndLeftPositions(i)
end
------------------------------------------------------------------------
end
--update positions
local contents = self.contents
local numActive = #activeControls
for i = 1, numActive do
local currentControl = activeControls[i]
local currentData = currentControl.dataEntry
local controlOffset = currentData.top - offset
--Added-----------------------------------------------------------------
local controlOffsetX = currentData.left
------------------------------------------------------------------------
currentControl:ClearAnchors()
--Modified--------------------------------------------------------------
currentControl:SetAnchor(TOPLEFT, contents, TOPLEFT, controlOffsetX, controlOffset)
--removed other anchor because this will no longer stretch across the contents pane
------------------------------------------------------------------------
end
--reset considered
for k,v in pairs(consideredMap) do
consideredMap[k] = nil
end
end
--[[----------------------------------------------------------------------------
Modified version of ZO_ItemTooltip_AddMoney(...) from
esoui\publicallingames\tooltip\tooltip.lua
--]]----------------------------------------------------------------------------
--Added currencyType parameter
local privateKey = {}
function ZO_ItemTooltip_AddMoney(tooltipControl, amount, reason, notEnough, currencyType)
local moneyLine = GetControl(tooltipControl, "SellPrice")
local reasonLabel = GetControl(moneyLine, "Reason")
local currencyControl = GetControl(moneyLine, "Currency")
--Added---------------------------------------------------------------------
local currencyType = currencyType or CURT_MONEY
--these is also from tooltip.lua, outside the function
local SELL_REASON_COLOR = ZO_ColorDef:New(GetInterfaceColor(INTERFACE_COLOR_TYPE_ITEM_TOOLTIP, ITEM_TOOLTIP_COLOR_SELLS_FOR))
local REASON_CURRENCY_SPACING = 3
local MONEY_LINE_HEIGHT = 18
local ITEM_TOOLTIP_CURRENCY_OPTIONS = {showTooltips = false,}
----------------------------------------------------------------------------
moneyLine:SetHidden(false)
local width = 0
reasonLabel:ClearAnchors()
currencyControl:ClearAnchors()
-- right now reason is always a string index
--Added------------------------------------------------------------------
if reason == privateKey and amount > 0 then
reasonLabel:SetAnchor(TOPLEFT, nil, TOPLEFT, 0, 0)
currencyControl:SetAnchor(TOPLEFT, reasonLabel, TOPRIGHT, REASON_CURRENCY_SPACING)
reasonLabel:SetHidden(false)
reasonLabel:SetColor(SELL_REASON_COLOR:UnpackRGBA())
reasonLabel:SetText(GetString(SI_STORE_SORT_TYPE_PRICE)..":")
local reasonTextWidth, reasonTextHeight = reasonLabel:GetTextDimensions()
width = width + reasonTextWidth + REASON_CURRENCY_SPACING
----------------------------------------------------------------------------
--Modified------------------------------------------------------------------
elseif reason and reason ~= 0 and reason ~= privateKey then
----------------------------------------------------------------------------
reasonLabel:SetAnchor(TOPLEFT, nil, TOPLEFT, 0, 0)
currencyControl:SetAnchor(TOPLEFT, reasonLabel, TOPRIGHT, REASON_CURRENCY_SPACING, -2)
reasonLabel:SetHidden(false)
reasonLabel:SetColor(SELL_REASON_COLOR:UnpackRGBA())
reasonLabel:SetText(GetString(reason))
local reasonTextWidth, reasonTextHeight = reasonLabel:GetTextDimensions()
width = width + reasonTextWidth + REASON_CURRENCY_SPACING
else
reasonLabel:SetHidden(true)
currencyControl:SetAnchor(TOPLEFT, nil, TOPLEFT, 0, 0)
end
if amount > 0 then
currencyControl:SetHidden(false)
--Modified--------------------------------------------------------------
ZO_CurrencyControl_SetSimpleCurrency(currencyControl, currencyType,
amount, ITEM_TOOLTIP_CURRENCY_OPTIONS, CURRENCY_DONT_SHOW_ALL,
notEnough)
------------------------------------------------------------------------
width = width + currencyControl:GetWidth()
else
currencyControl:SetHidden(true)
end
tooltipControl:AddControl(moneyLine)
moneyLine:SetAnchor(CENTER)
moneyLine:SetDimensions(width, MONEY_LINE_HEIGHT)
end
--[[----------------------------------------------------------------------------
Our own code
--]]----------------------------------------------------------------------------
local function AddCurrency(rowControl)
if not rowControl.dataEntry then return end
local IGVId = IGV.currentIGVId
local slotIndex = rowControl.dataEntry.data.slotIndex
local _, stack, sellPrice, currencyType, notEnough
if IGVId == IGVID_STORE then
for _, v in pairs(rowControl:GetNamedChild("SellPrice").currencyArgs) do
if v.isUsed == true then
currencyType = v.type
notEnough = v.notEnough
end
end
if currencyType == CURT_MONEY then
sellPrice = rowControl.dataEntry.data.price
else
sellPrice = rowControl.dataEntry.data.currencyQuantity1
end
--bandaid catch all for sellPrice == nil
sellPrice = sellPrice or 0
stack = rowControl.dataEntry.data.stack
ZO_ItemTooltip_AddMoney(ItemTooltip, sellPrice * stack, privateKey, notEnough, currencyType)
else
local bagId = rowControl.dataEntry.data.bagId
if not bagId then return end
_, stack, sellPrice = GetItemInfo(bagId, slotIndex)
ZO_ItemTooltip_AddMoney(ItemTooltip, sellPrice * stack, privateKey)
end
end
function adapter.AddCurrencySoon(rowControl)
if not rowControl and not rowControl.isGrid then return end
if IGV.currentIGVId == IGVID_CRAFT_BAG or IGV.currentIGVId == IGVID_STORE then
local function wrapper()
AddCurrency(rowControl)
end
zo_callLater(wrapper, 50)
end
end
local function freeActiveScrollListControls(scrollList)
if not scrollList or not scrollList.activeControls then return end
while #scrollList.activeControls > 0 do
FreeActiveScrollListControl(scrollList, 1)
end
end
function adapter.ScrollController(self)
if self == IGV.currentScrollList and settings.IsGrid(IGV.currentIGVId) then
freeActiveScrollListControls(self)
IGV_ScrollList_UpdateScroll_Grid(self)
util.ReshapeSlots()
return true
else
return false
end
end
function adapter.ToggleGrid()
local IGVId = IGV.currentIGVId
local scrollList = IGV.currentScrollList
util.debug("Toggle grid for " .. (IGVId or "nil"))
if not scrollList then return end
settings.ToggleGrid(IGVId)
local isGrid = settings.IsGrid(IGVId)
ZO_ScrollList_ResetToTop(scrollList)
util.ReshapeSlots()
freeActiveScrollListControls(scrollList)
ZO_ScrollList_UpdateScroll(scrollList)
if isGrid then
util.ReshapeSlots()
else
ResizeScrollBar(scrollList, (#scrollList.data * scrollList.controlHeight) - ZO_ScrollList_GetHeight(scrollList))
end
ZO_ScrollList_RefreshVisible(scrollList)
util.ReshapeSlots()
end