-
Notifications
You must be signed in to change notification settings - Fork 0
/
modmain.lua
433 lines (396 loc) · 13.8 KB
/
modmain.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
AddPlayerPostInit(function(player)
end)
-- function RemoveInspectable(inst)
-- print("removing inspectable")
-- inst:RemoveTag("inspectable")
-- end
-- for k,prefab in pairs(GLOBAL.PREFABFILES) do
-- AddPrefabPostInit(prefab, RemoveInspectable)
-- end
local function GetInventory()
-- GLOBAL.ThePlayer.components.playercontroller.DoInspectButton = function() return end
-- print(tostring(GLOBAL.ThePlayer.CanExamine ~= nil))
-- print(tostring(GLOBAL.ThePlayer.components.CanExamine ~= nil))
-- print(tostring(GLOBAL.ThePlayer.components.playercontroller.CanExamine ~= nil))
GLOBAL.ThePlayer.CanExamine = function(inst) return false end
return GLOBAL.ThePlayer.replica.inventory
end
local function PrintTable(table)
for key,value in pairs(table) do print(key,value) end
end
local function GetIfItemEquipped(item)
local handItem = GetInventory():GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS)
if (handItem ~= nil and handItem == item) then return true end
local headItem = GetInventory():GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)
if (headItem ~= nil and headItem == item) then return true end
local bodyItem = GetInventory():GetEquippedItem(GLOBAL.EQUIPSLOTS.BODY)
if (bodyItem ~= nil and bodyItem == item) then return true end
return false
end
local function ActuallyGetItems()
local items = {}
local inventory = GetInventory()
local numItems = inventory:GetNumSlots()
for i = 1, numItems do
local item = inventory:GetItemInSlot(i)
if (item ~= nil) then
table.insert(items, item)
end
end
local overFlowContainer = inventory:GetOverflowContainer()
if overFlowContainer == nil then return items end
local backpack = overFlowContainer
if backpack == nil then return items end
local numBackPackSlots = backpack:GetNumSlots()
for i = 1, numBackPackSlots do
local item = backpack:GetItemInSlot(i)
if (item ~= nil) then
--print(item.prefab)
table.insert(items, item)
end
end
return items
end
KEYBOARDTOGGLEKEY = GetModConfigData("KEYBOARDTOGGLEKEY") or "G"
if type(KEYBOARDTOGGLEKEY) == "string" then
KEYBOARDTOGGLEKEY = KEYBOARDTOGGLEKEY:lower():byte()
end
local SCALEFACTOR = GetModConfigData("SCALEFACTOR") or 1
local CENTERWHEEL = GetModConfigData("CENTERWHEEL")
--Gross way of handling the default behavior, but I don't see a better option
if CENTERWHEEL == nil then CENTERWHEEL = true end
local RESTORECURSOROPTIONS = GetModConfigData("RESTORECURSOR") or 3
if not CENTERWHEEL and RESTORECURSOROPTIONS == 3 then
--if the wheel isn't centered, then restoring basically just puts it where it was already
-- so turn that off to prevent jitter
RESTORECURSOROPTIONS = 0
end
--0 means don't center or restore, even if the wheel is centered
local CENTERCURSOR = CENTERWHEEL and (RESTORECURSOROPTIONS >= 1)
local RESTORECURSOR = RESTORECURSOROPTIONS >= 2
local ADJUSTCURSOR = RESTORECURSOROPTIONS >= 3
local IMAGETEXT = GetModConfigData("IMAGETEXT") or 2
local SHOWIMAGE = IMAGETEXT > 1
local SHOWTEXT = IMAGETEXT%2 == 1
local RIGHTSTICK = GetModConfigData("RIGHTSTICK")
--Backward-compatibility if they had changed the option
if GetModConfigData("LEFTSTICK") == false then RIGHTSTICK = true end
-- ONLYEIGHT isn't compatible with multiple rings; it will disable Party and Old emotes
local ONLYEIGHT = GetModConfigData("ONLYEIGHT")
local EIGHTS = {}
for i=1,8 do
EIGHTS[i] = GetModConfigData("EIGHT"..i)
end
local function ActuallyBuildItemSets()
-- get all items
local allitems = ActuallyGetItems()
-- filter to equippable items
local equippableItems = {}
-- local handItem = GetInventory():GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS)
-- local headItem = GetInventory():GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)
-- local bodyItem = GetInventory():GetEquippedItem(GLOBAL.EQUIPSLOTS.BODY)
for i, item in ipairs(allitems) do
local equippable = item.replica.equippable ~= nil
-- local isEquipped = (handItem ~= nil and item == handItem)
-- or (headItem ~= nil and item == headItem)
-- or (bodyItem ~= nil and item == bodyItem)
if (
equippable
--and (not isEquipped)
) then
item.myIndex = i
table.insert(equippableItems, item)
end
end
-- add to item set
local defaultitemset = {}
for i, item in ipairs(equippableItems) do
table.insert(defaultitemset, item)
end
-- for i, item in ipairs(defaultitemset) do
-- print(item.prefab)
-- print(item.myIndex)
-- end
local actual_item_sets = {}
table.insert(
actual_item_sets,
{
name = "default",
emotes = defaultitemset,
radius = ONLYEIGHT and 250 or 325,
color = GLOBAL.BROWN,
}
)
return actual_item_sets
end
--All code below is for handling the wheel
local ItemWheel = GLOBAL.require("widgets/itemwheel")
--Variables to control the display of the wheel
local cursorx = 0
local cursory = 0
local centerx = 0
local centery = 0
local controls = nil
local itemwheel = nil
local keydown = false
local using_gesture_wheel = false
local NORMSCALE = nil
local STARTSCALE = nil
local function CanUseItemWheel()
local screen = GLOBAL.TheFrontEnd:GetActiveScreen()
screen = (screen and type(screen.name) == "string") and screen.name or ""
if screen:find("HUD") == nil or not GLOBAL.ThePlayer then
return false
end
local full_enabled, soft_enabled = GLOBAL.ThePlayer.components.playercontroller:IsEnabled()
return full_enabled or soft_enabled or using_gesture_wheel
end
local function ResetTransform()
local screenwidth, screenheight = GLOBAL.TheSim:GetScreenSize()
centerx = math.floor(screenwidth/2 + 0.5)
centery = math.floor(screenheight/2 + 0.5)
local screenscalefactor = math.min(screenwidth/1920, screenheight/1080) --normalize by my testing setup, 1080p
itemwheel.screenscalefactor = SCALEFACTOR*screenscalefactor
NORMSCALE = SCALEFACTOR*screenscalefactor
STARTSCALE = 0
itemwheel:SetPosition(centerx, centery, 0)
itemwheel.inst.UITransform:SetScale(STARTSCALE, STARTSCALE, 1)
end
local function ShowItemWheel(controller_mode)
if keydown then
return
end
if type(GLOBAL.ThePlayer) ~= "table" or type(GLOBAL.ThePlayer.HUD) ~= "table" then
return
end
if not CanUseItemWheel() then
return
end
keydown = true
--SetModHUDFocus("ItemWheel", true)
GLOBAL.ThePlayer.HUD.controls:HideCraftingAndInventory()
using_gesture_wheel = true
ResetTransform()
if RESTORECURSOR then
cursorx, cursory = GLOBAL.TheInputProxy:GetOSCursorPos()
end
if CENTERCURSOR then
GLOBAL.TheInputProxy:SetOSCursorPos(centerx, centery)
end
if CENTERWHEEL then
itemwheel:SetPosition(centerx, centery, 0)
else
itemwheel:SetPosition(GLOBAL.TheInput:GetScreenPosition():Get())
end
itemwheel:SetControllerMode(controller_mode)
local actualItemSets = ActuallyBuildItemSets()
itemwheel:UpdateItems(actualItemSets, SHOWIMAGE, SHOWTEXT)
itemwheel:Show()
itemwheel:ScaleTo(STARTSCALE, NORMSCALE, .25)
end
local function HideItemWheel(delay_focus_loss)
if type(GLOBAL.ThePlayer) ~= "table" or type(GLOBAL.ThePlayer.HUD) ~= "table" then return end
keydown = false
itemwheel:Hide()
GLOBAL.ThePlayer.HUD.controls:ShowCraftingAndInventory()
itemwheel.inst.UITransform:SetScale(STARTSCALE, STARTSCALE, 1)
local can_use_wheel = CanUseItemWheel()
using_gesture_wheel = false
if not can_use_wheel then return end
if RESTORECURSOR then
if ADJUSTCURSOR then
local x,y = GLOBAL.TheInputProxy:GetOSCursorPos()
local gx, gy = itemwheel:GetPosition():Get()
local dx, dy = x-gx, y-gy
cursorx = cursorx + dx
cursory = cursory + dy
end
GLOBAL.TheInputProxy:SetOSCursorPos(cursorx, cursory)
end
if itemwheel.activeitem then -- actually an active item
local itemIndex = itemwheel.activeitem -- NOT SAFE, WILL PROBABLY FUCK UP WHEN MULTIPLE WHEELS
--local item = GetInventory():GetItemInSlot(itemIndex)
local item = itemwheel.actualItems[itemIndex]
if item == nil then
print("item is nil somehow, index was:")
print(tostring(itemIndex))
return
end
GetInventory():UseItemFromInvTile(item)
end
SetModHUDFocus("ItemWheel", false)
end
local handlers_applied = false
local originalOpenControllerInventory = nil
local function AddItemWheel(self)
controls = self -- this just makes controls available in the rest of the modmain's functions
if itemwheel then
itemwheel:Kill()
end
itemwheel = controls:AddChild(ItemWheel(SHOWIMAGE, SHOWTEXT, RIGHTSTICK))
controls.itemwheel = itemwheel
ResetTransform()
itemwheel:Hide()
if not handlers_applied then
-- APPLY HANDLERS TO OPEN / CLOSE WHEEL
-- Keyboard controls
GLOBAL.TheInput:AddKeyDownHandler(KEYBOARDTOGGLEKEY, ShowItemWheel)
GLOBAL.TheInput:AddKeyUpHandler(KEYBOARDTOGGLEKEY, HideItemWheel)
-- fuck the opening of inventory thingo
originalOpenControllerInventory = GLOBAL.ThePlayer.HUD.OpenControllerInventory
GLOBAL.ThePlayer.HUD.OpenControllerInventory = function()
ShowItemWheel(true)
end
-- R2
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_OPEN_INVENTORY, function(down)
if down then
return -- this case doesn't get hit anyway
else
HideItemWheel(true)
end
end)
-- L3
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_MENU_MISC_3, function(down)
if (down and using_gesture_wheel) then
SetModHUDFocus("ItemWheel", true)
else
return
end
end)
-- R3
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_MENU_MISC_4, function(down)
GLOBAL.ThePlayer.CanExamine = function(inst) return true end
end)
-- d-pad up
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_INVENTORY_EXAMINE, function(down)
if not down then return end
if itemwheel.activeitem == nil then return end
local item = itemwheel.actualItems[itemwheel.activeitem]
if item == nil then return end
itemwheel.item1prefab = item.prefab
itemwheel:UpdateItems(ActuallyBuildItemSets(), SHOWIMAGE, SHOWTEXT)
end)
-- d-pad right
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_INVENTORY_USEONSELF, function(down)
if not down then return end
if itemwheel.activeitem == nil then return end
local item = itemwheel.actualItems[itemwheel.activeitem]
if item == nil then return end
itemwheel.item2prefab = item.prefab
itemwheel:UpdateItems(ActuallyBuildItemSets(), SHOWIMAGE, SHOWTEXT)
end)
-- d-pad down
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_INVENTORY_DROP, function(down)
if not down then return end
if itemwheel.activeitem == nil then return end
local item = itemwheel.actualItems[itemwheel.activeitem]
if item == nil then return end
itemwheel.item3prefab = item.prefab
itemwheel:UpdateItems(ActuallyBuildItemSets(), SHOWIMAGE, SHOWTEXT)
end)
-- GLOBAL.ACTIONS.LOOKAT.fn = function(act) return end -- disables inspection completely
GLOBAL.ThePlayer.HUD.InspectSelf = function() return end -- disables self inspection popup
GLOBAL.ThePlayer.HUD.ShowPlayerStatusScreen = function(a, b) return end -- disables other player inspection popup
local timeLastTriangleDown = 0
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_MENU_MISC_2, function(down)
if ((down) and using_gesture_wheel) then
-- show default inventory
HideItemWheel(true)
originalOpenControllerInventory(GLOBAL.ThePlayer.HUD)
elseif (GLOBAL.ThePlayer.HUD:IsControllerInventoryOpen()) then
-- don't do anything if inventory open
return
elseif (down and (not using_gesture_wheel)) then
timeLastTriangleDown = GLOBAL.GetTime()
elseif ((not down) and (not using_gesture_wheel)) then
function GetItemFromPrefabName(prefab)
if (prefab == nil) then return nil end
local items = ActuallyGetItems()
for i,item in ipairs(items) do
if (item.prefab == prefab) then
return item
end
end
return nil
end
function GetItemToHotSwitchTo()
if (itemwheel.item3prefab ~=nil and (GLOBAL.GetTime() - timeLastTriangleDown > 0.2)) then
local item = GetItemFromPrefabName(itemwheel.item3prefab)
if (item ~= nil and (not GetIfItemEquipped(item))) then
return item
end
end
if (itemwheel.item1prefab ~=nil) then
local item = GetItemFromPrefabName(itemwheel.item1prefab)
if (item ~= nil and (not GetIfItemEquipped(item))) then
return item
end
end
if (itemwheel.item2prefab ~=nil) then
local item = GetItemFromPrefabName(itemwheel.item2prefab)
if (item ~= nil and (not GetIfItemEquipped(item))) then
return item
end
end
return nil
end
local itemToSwitchTo = GetItemToHotSwitchTo()
if (itemToSwitchTo ~= nil) then
GetInventory():UseItemFromInvTile(itemToSwitchTo)
end
end
end)
-- this is just a lock system to make it only register one shift at a time
local rotate_left_free = true
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_ROTATE_LEFT, function(down)
if down then
if keydown and rotate_left_free then
itemwheel:SwitchWheel(-1)
rotate_left_free = false
end
else
rotate_left_free = true
end
end)
local rotate_right_free = true
GLOBAL.TheInput:AddControlHandler(GLOBAL.CONTROL_ROTATE_RIGHT, function(down)
if down then
if keydown and rotate_right_free then
itemwheel:SwitchWheel(1)
rotate_right_free = false
end
else
rotate_right_free = true
end
end)
handlers_applied = true
end
end
AddClassPostConstruct( "widgets/controls", AddItemWheel)
--Patch the class definition directly instead of each new instance
local Controls = GLOBAL.require("widgets/controls")
local OldOnUpdate = Controls.OnUpdate
local function OnUpdate(self, ...)
OldOnUpdate(self, ...)
if keydown then
self.itemwheel:OnUpdate()
end
end
Controls.OnUpdate = OnUpdate
--In order to update the emote set when a skin is received, hook into the giftitempopup
AddClassPostConstruct("screens/giftitempopup", function(self)
local function ScheduleRebuild()
--give it a little time to update the skin inventory
controls.owner:DoTaskInTime(5, function() AddItemWheel(controls) end)
end
local OldOnClose = self.OnClose
function self:OnClose(...)
OldOnClose(self, ...)
ScheduleRebuild()
end
local OldApplySkin = self.ApplySkin
function self:ApplySkin(...)
OldApplySkin(self, ...)
ScheduleRebuild()
end
end)