-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathVikingItemPreview.lua
229 lines (183 loc) · 7.08 KB
/
VikingItemPreview.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
require "Window"
require "GameLib"
require "Item"
-----------------------------------------------------------------------------------------------
-- ItemPreview Module Definition
-----------------------------------------------------------------------------------------------
local VikingItemPreview = {}
-----------------------------------------------------------------------------------------------
-- Constants
-----------------------------------------------------------------------------------------------
local ktVisibleSlots =
{
2,
3,
0,
5,
1,
4,
16
}
local knSaveVersion
-----------------------------------------------------------------------------------------------
-- Initialization
-----------------------------------------------------------------------------------------------
function VikingItemPreview:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
-- initialize variables here
return o
end
function VikingItemPreview:Init()
Apollo.RegisterAddon(self)
end
function VikingItemPreview:OnSave(eType)
if eType ~= GameLib.CodeEnumAddonSaveLevel.Account then
return
end
local locWindowLocation = self.wndMain and self.wndMain:GetLocation() or self.locSavedWindowLoc
local tSaved =
{
tWindowLocation = locWindowLocation and locWindowLocation:ToTable() or nil,
nSaveVersion = knSaveVersion
}
return tSaved
end
function VikingItemPreview:OnRestore(eType, tSavedData)
if not tSavedData or tSavedData.nSaveVersion ~= knSaveVersion then
return
end
if tSavedData.tWindowLocation then
self.locSavedWindowLoc = WindowLocation.new(tSavedData.tWindowLocation)
end
end
-----------------------------------------------------------------------------------------------
-- ItemPreview OnLoad
-----------------------------------------------------------------------------------------------
function VikingItemPreview:OnLoad()
self.xmlDoc = XmlDoc.CreateFromFile("VikingItemPreview.xml")
self.xmlDoc:RegisterCallback("OnDocumentReady", self)
end
function VikingItemPreview:OnDocumentReady()
if self.xmlDoc == nil then
return
end
-- Register handlers for events, slash commands and timer, etc.
-- e.g. Apollo.RegisterEventHandler("KeyDown", "OnKeyDown", self)
Apollo.RegisterEventHandler("ShowItemInDressingRoom", "OnShowItemInDressingRoom", self)
self.bSheathed = false
end
-----------------------------------------------------------------------------------------------
-- ItemPreview Functions
-----------------------------------------------------------------------------------------------
-- Define general functions here
function VikingItemPreview:OnShowItemInDressingRoom(item)
if self.wndMain ~= nil then
self:OnWindowClosed()
end
if item == nil or not self:HelperValidateSlot(item) then
return
end
self.wndMain = Apollo.LoadForm(self.xmlDoc, "VikingItemPreviewForm", "TooltipStratum", self)
local nWndLeft, nWndTop, nWndRight, nWndBottom = self.wndMain:GetRect()
local nWndWidth = nWndRight - nWndLeft
local nWndHeight = nWndBottom - nWndTop
self.wndMain:SetSizingMinimum(nWndWidth - 10, nWndHeight - 10)
self.wndMain:FindChild("PreviewWindow"):SetCostume(GameLib.GetPlayerUnit())
self.wndMain:FindChild("PreviewWindow"):SetItem(item)
if self.locSavedWindowLoc then
self.wndMain:MoveToLocation(self.locSavedWindowLoc)
end
-- set item name;
--local strLabel = string.format("<T Font=\"CRB_InterfaceMedium\" TextColor=\"UI_TextHoloBody\" Align=\"Center\">%s</T>", Apollo.GetString("Inventory_ItemPreviewLabel"))
local strItem = string.format("<T Font=\"CRB_InterfaceSmall\" TextColor=\"UI_TextHoloTitle\" Align=\"Center\">%s</T>", item:GetName())
self.wndMain:FindChild("ItemLabel"):SetAML("<P Align=\"Center\">"..String_GetWeaselString(strItem).."</P>")
-- set sheathed or not
local eItemType = item:GetItemType()
self.bSheathed = not self:HelperCheckForWeapon(eItemType)
self.wndMain:FindChild("PreviewWindow"):SetSheathed(self.bSheathed)
self:HelperFormatSheathButton(self.bSheathed)
self.wndMain:Show(true)
end
function VikingItemPreview:HelperCheckForWeapon(eItemType)
local bIsWeapon = false
if eItemType >= Item.CodeEnumItemType.WeaponMHPistols and eItemType <= Item.CodeEnumItemType.WeaponMHSword then
bIsWeapon = true
end
return bIsWeapon
end
function VikingItemPreview:HelperFormatSheathButton(bSheathed)
if bSheathed == true then
self.wndMain:FindChild("SheathButton"):SetText(Apollo.GetString("Inventory_DrawWeapons"))
else
self.wndMain:FindChild("SheathButton"):SetText(Apollo.GetString("Inventory_Sheathe"))
end
end
function VikingItemPreview:HelperValidateSlot(item)
local bVisibleSlot = false
local bRightClassOrProf = false
local tProficiency = item:GetProficiencyInfo()
local arReqClass = item:GetRequiredClass()
local unitPlayer = GameLib.GetPlayerUnit()
local bCanEquip = item:CanEquip()
if #arReqClass > 0 then
for idx,tClass in ipairs(arReqClass) do
if tClass.idClassReq == unitPlayer:GetClassId() then
bRightClassOrProf = true
end
end
elseif tProficiency then
bRightClassOrProf = tProficiency.bHasProficiency
elseif bCanEquip then
bRightClassOrProf = true
end
for idx, nSlot in pairs(ktVisibleSlots) do
if item:GetSlot() and item:GetSlot() == nSlot then
bVisibleSlot = bRightClassOrProf
break
end
end
return bVisibleSlot
end
-----------------------------------------------------------------------------------------------
-- ItemPreviewForm Functions
-----------------------------------------------------------------------------------------------
function VikingItemPreview:OnWindowClosed( wndHandler, wndControl )
if self.wndMain ~= nil then
--self.wndMain:Close()
self.locSavedWindowLoc = self.wndMain:GetLocation()
self.wndMain:Destroy()
self.wndMain = nil
end
end
function VikingItemPreview:OnToggleSheathButton( wndHandler, wndControl, eMouseButton )
local bWeapon = wndControl:IsChecked()
self.wndMain:FindChild("PreviewWindow"):SetSheathed(bWeapon)
end
function VikingItemPreview:OnCloseBtn( wndHandler, wndControl, eMouseButton )
self:OnWindowClosed()
end
function VikingItemPreview:OnToggleSheathed( wndHandler, wndControl, eMouseButton )
local bSheathed = not self.bSheathed
self.wndMain:FindChild("PreviewWindow"):SetSheathed(bSheathed)
self:HelperFormatSheathButton(bSheathed)
self.bSheathed = bSheathed
end
function VikingItemPreview:OnRotateRight()
self.wndMain:FindChild("PreviewWindow"):ToggleLeftSpin(true)
end
function VikingItemPreview:OnRotateRightCancel()
self.wndMain:FindChild("PreviewWindow"):ToggleLeftSpin(false)
end
function VikingItemPreview:OnRotateLeft()
self.wndMain:FindChild("PreviewWindow"):ToggleRightSpin(true)
end
function VikingItemPreview:OnRotateLeftCancel()
self.wndMain:FindChild("PreviewWindow"):ToggleRightSpin(false)
end
-----------------------------------------------------------------------------------------------
-- VikingItemPreview Instance
-----------------------------------------------------------------------------------------------
local ItemPreviewInst = VikingItemPreview:new()
ItemPreviewInst:Init()