Repository navigation
Expand file tree
/
Copy pathBackpackScript.lua
More file actions
1075 lines (926 loc) · 32.1 KB
/
Copy pathBackpackScript.lua
File metadata and controls
1075 lines (926 loc) · 32.1 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Backpack Version 4.21
-- OnlyTwentyCharacters
-------------------
--| Exposed API |--
-------------------
local BackpackScript = {}
BackpackScript.OpenClose = nil -- Function to toggle open/close
BackpackScript.StateChanged = Instance.new('BindableEvent') -- Fires after any open/close, passes IsNowOpen
---------------------
--| Configurables |--
---------------------
local ICON_SIZE = 60
local ICON_BUFFER = 5
local BACKGROUND_FADE = 0.50
local BACKGROUND_COLOR = Color3.new(31/255, 31/255, 31/255)
local SLOT_DRAGGABLE_COLOR = Color3.new(49/255, 49/255, 49/255)
local SLOT_EQUIP_COLOR = Color3.new(90/255, 142/255, 233/255)
local SLOT_EQUIP_THICKNESS = 0.1 -- Relative
local SLOT_FADE_LOCKED = 0.50 -- Locked means undraggable
local SLOT_BORDER_COLOR = Color3.new(1, 1, 1) -- Appears when dragging
local TOOLTIP_BUFFER = 6
local TOOLTIP_HEIGHT = 16
local TOOLTIP_OFFSET = -25 -- From top
local ARROW_IMAGE_OPEN = 'rbxasset://textures/ui/Backpack_Open.png'
local ARROW_IMAGE_CLOSE = 'rbxasset://textures/ui/Backpack_Close.png'
local ARROW_SIZE = UDim2.new(0, 14, 0, 9)
local ARROW_HOTKEY = Enum.KeyCode.Backquote.Value --TODO: Hookup '~' too?
local ARROW_HOTKEY_STRING = '`'
local HOTBAR_SLOTS_FULL = 10
local HOTBAR_SLOTS_MINI = 3
local HOTBAR_SLOTS_WIDTH_CUTOFF = 1024 -- Anything smaller is MINI
local HOTBAR_OFFSET_FROMBOTTOM = -30 -- Offset to make room for the Health GUI
local INVENTORY_ROWS_FULL = 4
local INVENTORY_ROWS_MINI = 2
local INVENTORY_HEADER_SIZE = 40
--local TITLE_OFFSET = 20 -- From left side
--local TITLE_TEXT = "Backpack"
local SEARCH_BUFFER = 5
local SEARCH_WIDTH = 200
local SEARCH_TEXT = "Search"
local SEARCH_TEXT_OFFSET_FROMLEFT = 15
local SEARCH_BACKGROUND_COLOR = Color3.new(0.37, 0.37, 0.37)
local SEARCH_BACKGROUND_FADE = 0.15
local DOUBLE_CLICK_TIME = 0.5
-----------------
--| Variables |--
-----------------
local PlayersService = game:GetService('Players')
local UserInputService = game:GetService('UserInputService')
local StarterGui = game:GetService('StarterGui')
local GuiService = game:GetService('GuiService')
local CoreGui = game:GetService('CoreGui')
local RobloxGui = CoreGui:WaitForChild('RobloxGui')
local IS_PHONE = UserInputService.TouchEnabled and GuiService:GetScreenResolution().X < HOTBAR_SLOTS_WIDTH_CUTOFF
local HOTBAR_SLOTS = (IS_PHONE) and HOTBAR_SLOTS_MINI or HOTBAR_SLOTS_FULL
local HOTBAR_SIZE = UDim2.new(0, ICON_BUFFER + (HOTBAR_SLOTS * (ICON_SIZE + ICON_BUFFER)), 0, ICON_BUFFER + ICON_SIZE + ICON_BUFFER)
local ZERO_KEY_VALUE = Enum.KeyCode.Zero.Value
local DROP_HOTKEY_VALUE = Enum.KeyCode.Backspace.Value
local INVENTORY_ROWS = (IS_PHONE) and INVENTORY_ROWS_MINI or INVENTORY_ROWS_FULL
local Player = PlayersService.LocalPlayer
local MainFrame = nil
local HotbarFrame = nil
local InventoryFrame = nil
local ScrollingFrame = nil
local Character = nil
local Humanoid = nil
local Backpack = nil
local Slots = {} -- List of all Slots by index
local LowestEmptySlot = nil
local SlotsByTool = {} -- Map of Tools to their assigned Slots
local HotkeyFns = {} -- Map of KeyCode values to their assigned behaviors
local Dragging = {} -- Only used to check if anything is being dragged, to disable other input
local FullHotbarSlots = 0
local UpdateArrowFrame = nil -- Function defined in arrow init logic at bottom
local ActiveHopper = nil --NOTE: HopperBin
local StarterToolFound = false -- Special handling is required for the gear currently equipped on the site
local WholeThingEnabled = false
local TextBoxFocused = false -- ANY TextBox, not just the search box
local ResultsIndices = nil -- Results of a search, or nil
local HotkeyStrings = {} -- Used for eating/releasing hotkeys
local CharConns = {} -- Holds character connections to be cleared later
local TopBarEnabled = false
-----------------
--| Functions |--
-----------------
local function NewGui(className, objectName)
local newGui = Instance.new(className)
newGui.Name = objectName
newGui.BackgroundColor3 = Color3.new(0, 0, 0)
newGui.BackgroundTransparency = 1
newGui.BorderColor3 = Color3.new(0, 0, 0)
newGui.BorderSizePixel = 0
newGui.Size = UDim2.new(1, 0, 1, 0)
if className:match('Text') then
newGui.TextColor3 = Color3.new(1, 1, 1)
newGui.Text = ''
newGui.Font = Enum.Font.SourceSans
newGui.FontSize = Enum.FontSize.Size14
newGui.TextWrapped = true
if className == 'TextButton' then
newGui.Font = Enum.Font.SourceSansBold
newGui.BorderSizePixel = 1
end
end
return newGui
end
local function FindLowestEmpty()
for i = 1, HOTBAR_SLOTS do
local slot = Slots[i]
if not slot.Tool then
return slot
end
end
return nil
end
local function AdjustHotbarFrames()
local inventoryOpen = InventoryFrame.Visible -- (Show all)
local visualTotal = (inventoryOpen) and HOTBAR_SLOTS or FullHotbarSlots
local visualIndex = 0
for i = 1, HOTBAR_SLOTS do
local slot = Slots[i]
if slot.Tool or inventoryOpen then
visualIndex = visualIndex + 1
slot:Readjust(visualIndex, visualTotal)
slot.Frame.Visible = true
else
slot.Frame.Visible = false
end
end
end
local function CheckBounds(guiObject, x, y)
local pos = guiObject.AbsolutePosition
local size = guiObject.AbsoluteSize
return (x > pos.X and x <= pos.X + size.X and y > pos.Y and y <= pos.Y + size.Y)
end
local function GetOffset(guiObject, point)
local centerPoint = guiObject.AbsolutePosition + (guiObject.AbsoluteSize / 2)
return (centerPoint - point).magnitude
end
local function DisableActiveHopper() --NOTE: HopperBin
ActiveHopper:ToggleSelect()
SlotsByTool[ActiveHopper]:UpdateEquipView()
ActiveHopper = nil
end
local function UnequipAllTools() --NOTE: HopperBin
Humanoid:UnequipTools()
if ActiveHopper then
DisableActiveHopper()
end
end
local function EquipNewTool(tool) --NOTE: HopperBin
UnequipAllTools()
if tool:IsA('HopperBin') then
tool:ToggleSelect()
SlotsByTool[tool]:UpdateEquipView()
ActiveHopper = tool
else
--Humanoid:EquipTool(tool) --NOTE: This would also unequip current Tool
tool.Parent = Character --TODO: Switch back to above line after EquipTool is fixed!
end
end
local function IsEquipped(tool)
return ((tool:IsA('HopperBin') and tool.Active) or tool.Parent == Character) --NOTE: HopperBin
end
local function MakeSlot(parent, index)
index = index or (#Slots + 1)
-- Slot Definition --
local slot = {}
slot.Tool = nil
slot.Index = index
slot.Frame = nil
local SlotFrame = nil
local ToolIcon = nil
local ToolName = nil
local ToolChangeConn = nil
local HighlightFrame = nil
--NOTE: The following are only defined for Hotbar Slots
local ToolTip = nil
local SlotNumber = nil
local ClickArea = nil
-- Slot Functions --
local function UpdateSlotFading()
SlotFrame.BackgroundTransparency = (SlotFrame.Draggable) and 0 or SLOT_FADE_LOCKED
SlotFrame.BackgroundColor3 = (SlotFrame.Draggable) and SLOT_DRAGGABLE_COLOR or BACKGROUND_COLOR
end
function slot:Reposition()
-- Slots are positioned into rows
local index = (ResultsIndices and ResultsIndices[self]) or self.Index
local sizePlus = ICON_BUFFER + ICON_SIZE
local modSlots = ((index - 1) % HOTBAR_SLOTS) + 1
local row = (index > HOTBAR_SLOTS) and (math.floor((index - 1) / HOTBAR_SLOTS)) - 1 or 0
SlotFrame.Position = UDim2.new(0, ICON_BUFFER + ((modSlots - 1) * sizePlus), 0, ICON_BUFFER + (sizePlus * row))
end
function slot:Readjust(visualIndex, visualTotal) --NOTE: Only used for Hotbar slots
local centered = HOTBAR_SIZE.X.Offset / 2
local sizePlus = ICON_BUFFER + ICON_SIZE
local midpointish = (visualTotal / 2) + 0.5
local factor = visualIndex - midpointish
SlotFrame.Position = UDim2.new(0, centered - (ICON_SIZE / 2) + (sizePlus * factor), 0, ICON_BUFFER)
end
function slot:Fill(tool)
self.Tool = tool
local function assignToolData()
local icon = tool.TextureId
ToolIcon.Image = icon
ToolName.Text = (icon == '') and tool.Name or '' -- (Only show name if no icon)
if ToolTip and tool:IsA('Tool') then --NOTE: HopperBin
ToolTip.Text = tool.ToolTip
local width = ToolTip.TextBounds.X + TOOLTIP_BUFFER
ToolTip.Size = UDim2.new(0, width, 0, TOOLTIP_HEIGHT)
ToolTip.Position = UDim2.new(0.5, -width / 2, 0, TOOLTIP_OFFSET)
end
end
assignToolData()
ToolChangeConn = tool.Changed:connect(function(property)
if property == 'TextureId' or property == 'Name' or property == 'ToolTip' then
assignToolData()
end
end)
local hotbarSlot = (self.Index <= HOTBAR_SLOTS)
local inventoryOpen = InventoryFrame.Visible
if not hotbarSlot or inventoryOpen then
SlotFrame.Draggable = true
end
self:UpdateEquipView()
if hotbarSlot then
FullHotbarSlots = FullHotbarSlots + 1
end
SlotsByTool[tool] = self
LowestEmptySlot = FindLowestEmpty()
UpdateArrowFrame()
end
function slot:Clear()
ToolChangeConn:disconnect()
ToolChangeConn = nil
ToolIcon.Image = ''
ToolName.Text = ''
if ToolTip then
ToolTip.Text = ''
ToolTip.Visible = false
end
SlotFrame.Draggable = false
self:UpdateEquipView(true) -- Show as unequipped
if self.Index <= HOTBAR_SLOTS then
FullHotbarSlots = FullHotbarSlots - 1
end
SlotsByTool[self.Tool] = nil
self.Tool = nil
LowestEmptySlot = FindLowestEmpty()
UpdateArrowFrame()
end
function slot:UpdateEquipView(unequippedOverride)
if not unequippedOverride and IsEquipped(self.Tool) then -- Equipped
if not HighlightFrame then
HighlightFrame = NewGui('Frame', 'Equipped')
HighlightFrame.ZIndex = SlotFrame.ZIndex
local t = SLOT_EQUIP_THICKNESS
local dataTable = { -- Relative sizes and positions
{t, 1, 0, 0},
{1, t, 0, 0},
{t, 1, 1 - t, 0},
{1, t, 0, 1 - t},
}
for _, data in pairs(dataTable) do
local edgeFrame = NewGui('Frame', 'Edge')
edgeFrame.BackgroundTransparency = 0
edgeFrame.BackgroundColor3 = SLOT_EQUIP_COLOR
edgeFrame.Size = UDim2.new(data[1], 0, data[2], 0)
edgeFrame.Position = UDim2.new(data[3], 0, data[4], 0)
edgeFrame.ZIndex = HighlightFrame.ZIndex
edgeFrame.Parent = HighlightFrame
end
end
HighlightFrame.Parent = SlotFrame
else -- In the Backpack
if HighlightFrame then
HighlightFrame.Parent = nil
end
end
UpdateSlotFading()
end
function slot:Delete()
SlotFrame:Destroy() --NOTE: Also clears connections
table.remove(Slots, self.Index)
local newSize = #Slots
-- Now adjust the rest (both visually and representationally)
for i = self.Index, newSize do
Slots[i]:SlideBack()
end
if newSize % HOTBAR_SLOTS == 0 then -- We lost a row at the bottom! Adjust the CanvasSize
local lastSlot = Slots[newSize]
local lowestPoint = lastSlot.Frame.Position.Y.Offset + lastSlot.Frame.Size.Y.Offset
ScrollingFrame.CanvasSize = UDim2.new(0, 0, 0, lowestPoint + ICON_BUFFER)
end
end
function slot:Swap(targetSlot) --NOTE: This slot (self) must not be empty!
local myTool, otherTool = self.Tool, targetSlot.Tool
self:Clear()
if otherTool then -- (Target slot might be empty)
targetSlot:Clear()
self:Fill(otherTool)
end
targetSlot:Fill(myTool)
end
function slot:SlideBack() -- For inventory slot shifting
self.Index = self.Index - 1
SlotFrame.Name = self.Index
self:Reposition()
end
function slot:TurnNumber(on)
if SlotNumber then
SlotNumber.Visible = on
end
end
function slot:SetClickability(on) -- (Happens on open/close arrow)
ClickArea.Visible = on
if self.Tool then
SlotFrame.Draggable = not on
UpdateSlotFading()
end
end
function slot:CheckTerms(terms)
local hits = 0
local function checkEm(str, term)
local _, n = str:lower():gsub(term, '')
hits = hits + n
end
local tool = self.Tool
for term in pairs(terms) do
checkEm(tool.Name, term)
if tool:IsA('Tool') then --NOTE: HopperBin
checkEm(tool.ToolTip, term)
end
end
return hits
end
-- Slot Init Logic --
SlotFrame = NewGui('Frame', index)
SlotFrame.BackgroundColor3 = BACKGROUND_COLOR
SlotFrame.BorderColor3 = SLOT_BORDER_COLOR
SlotFrame.Size = UDim2.new(0, ICON_SIZE, 0, ICON_SIZE)
SlotFrame.Active = true
SlotFrame.Draggable = false
SlotFrame.BackgroundTransparency = SLOT_FADE_LOCKED
slot.Frame = SlotFrame
ToolIcon = NewGui('ImageLabel', 'Icon')
ToolIcon.Size = UDim2.new(0.8, 0, 0.8, 0)
ToolIcon.Position = UDim2.new(0.1, 0, 0.1, 0)
ToolIcon.Parent = SlotFrame
ToolName = NewGui('TextLabel', 'ToolName')
ToolName.Size = UDim2.new(1, -2, 1, -2)
ToolName.Position = UDim2.new(0, 1, 0, 1)
ToolName.Parent = SlotFrame
slot:Reposition()
if index <= HOTBAR_SLOTS then -- Hotbar-Specific Slot Stuff
-- ToolTip stuff
ToolTip = NewGui('TextLabel', 'ToolTip')
ToolTip.TextWrapped = false
ToolTip.TextYAlignment = Enum.TextYAlignment.Top
ToolTip.BackgroundColor3 = Color3.new(0.4, 0.4, 0.4)
ToolTip.BackgroundTransparency = 0
ToolTip.Visible = false
ToolTip.Parent = SlotFrame
SlotFrame.MouseEnter:connect(function()
if ToolTip.Text ~= '' then
ToolTip.Visible = true
end
end)
SlotFrame.MouseLeave:connect(function() ToolTip.Visible = false end)
-- Slot select logic, activated by clicking or pressing hotkey
local function selectSlot()
local tool = slot.Tool
if tool then
if IsEquipped(tool) then --NOTE: HopperBin
UnequipAllTools()
elseif tool.Parent == Backpack then
EquipNewTool(tool)
end
end
end
ClickArea = NewGui('TextButton', 'GimmieYerClicks')
ClickArea.MouseButton1Click:connect(selectSlot)
ClickArea.Parent = SlotFrame
-- Show label and assign hotkeys for 1-9 and 0 (zero is always last slot when > 10 total)
if index < 10 or index == HOTBAR_SLOTS then -- NOTE: Hardcoded on purpose!
local slotNum = (index < 10) and index or 0
SlotNumber = NewGui('TextLabel', 'Number')
SlotNumber.Text = slotNum
SlotNumber.Size = UDim2.new(0.15, 0, 0.15, 0)
SlotNumber.Visible = false
SlotNumber.Parent = SlotFrame
HotkeyFns[ZERO_KEY_VALUE + slotNum] = selectSlot
end
else -- Inventory-Specific Slot Stuff
if index % HOTBAR_SLOTS == 1 then -- We are the first slot of a new row! Adjust the CanvasSize
local lowestPoint = SlotFrame.Position.Y.Offset + SlotFrame.Size.Y.Offset
ScrollingFrame.CanvasSize = UDim2.new(0, 0, 0, lowestPoint + ICON_BUFFER)
end
-- Scroll to new inventory slot, if we're open and not viewing search results
if InventoryFrame.Visible and not ResultsIndices then
local offset = ScrollingFrame.CanvasSize.Y.Offset - ScrollingFrame.AbsoluteSize.Y
ScrollingFrame.CanvasPosition = Vector2.new(0, math.max(0, offset))
end
end
do -- Dragging Logic
local startPoint = SlotFrame.Position
local lastUpTime = 0
local startParent = nil
SlotFrame.DragBegin:connect(function(dragPoint)
Dragging[SlotFrame] = true
startPoint = dragPoint
SlotFrame.BorderSizePixel = 2
-- Raise above other slots
SlotFrame.ZIndex = 2
ToolIcon.ZIndex = 2
ToolName.ZIndex = 2
if SlotNumber then
SlotNumber.ZIndex = 2
end
if HighlightFrame then
HighlightFrame.ZIndex = 2
for _, child in pairs(HighlightFrame:GetChildren()) do
child.ZIndex = 2
end
end
-- Circumvent the ScrollingFrame's ClipsDescendants property
startParent = SlotFrame.Parent
if startParent == ScrollingFrame then
SlotFrame.Parent = InventoryFrame
local pos = ScrollingFrame.Position
local offset = ScrollingFrame.CanvasPosition - Vector2.new(pos.X.Offset, pos.Y.Offset)
SlotFrame.Position = SlotFrame.Position - UDim2.new(0, offset.X, 0, offset.Y)
end
end)
SlotFrame.DragStopped:connect(function(x, y)
local now = tick()
SlotFrame.Position = startPoint
SlotFrame.Parent = startParent
SlotFrame.BorderSizePixel = 0
-- Restore height
SlotFrame.ZIndex = 1
ToolIcon.ZIndex = 1
ToolName.ZIndex = 1
if SlotNumber then
SlotNumber.ZIndex = 1
end
if HighlightFrame then
HighlightFrame.ZIndex = 1
for _, child in pairs(HighlightFrame:GetChildren()) do
child.ZIndex = 1
end
end
Dragging[SlotFrame] = nil
-- Make sure the tool wasn't dropped
if not slot.Tool then
return
end
local function moveToInventory()
if slot.Index <= HOTBAR_SLOTS then -- From a Hotbar slot
local tool = slot.Tool
slot:Clear() --NOTE: Order matters here
local newSlot = MakeSlot(ScrollingFrame)
newSlot:Fill(tool)
if IsEquipped(tool) then -- Also unequip it --NOTE: HopperBin
UnequipAllTools()
end
-- Also hide the inventory slot if we're showing results right now
if ResultsIndices then
newSlot.Frame.Visible = false
end
end
end
-- Check where we were dropped
if CheckBounds(InventoryFrame, x, y) then
moveToInventory()
-- Check for double clicking on an inventory slot, to move into empty hotbar slot
if slot.Index > HOTBAR_SLOTS and now - lastUpTime < DOUBLE_CLICK_TIME then
if LowestEmptySlot then
local myTool = slot.Tool
slot:Clear()
LowestEmptySlot:Fill(myTool)
slot:Delete()
end
now = 0 -- Resets the timer
end
elseif CheckBounds(HotbarFrame, x, y) then
local closest = {math.huge, nil}
for i = 1, HOTBAR_SLOTS do
local otherSlot = Slots[i]
local offset = GetOffset(otherSlot.Frame, Vector2.new(x, y))
if offset < closest[1] then
closest = {offset, otherSlot}
end
end
local closestSlot = closest[2]
if closestSlot ~= slot then
slot:Swap(closestSlot)
if slot.Index > HOTBAR_SLOTS then
local tool = slot.Tool
if not tool then -- Clean up after ourselves if we're an inventory slot that's now empty
slot:Delete()
else -- Moved inventory slot to hotbar slot, and gained a tool that needs to be unequipped
if IsEquipped(tool) then --NOTE: HopperBin
UnequipAllTools()
end
-- Also hide the inventory slot if we're showing results right now
if ResultsIndices then
slot.Frame.Visible = false
end
end
end
end
else
-- local tool = slot.Tool
-- if tool.CanBeDropped then --TODO: HopperBins
-- tool.Parent = workspace
-- --TODO: Move away from character
-- end
moveToInventory() --NOTE: Temporary
end
lastUpTime = now
end)
end
-- All ready!
SlotFrame.Parent = parent
Slots[index] = slot
return slot
end
local function OnChildAdded(child) -- To Character or Backpack
if not child:IsA('Tool') and not child:IsA('HopperBin') then --NOTE: HopperBin
if child:IsA('Humanoid') and child.Parent == Character then
Humanoid = child
end
return
end
local tool = child
if ActiveHopper and tool.Parent == Character then --NOTE: HopperBin
DisableActiveHopper()
end
--TODO: Optimize / refactor / do something else
if not StarterToolFound and tool.Parent == Character and not SlotsByTool[tool] then
local starterGear = Player:FindFirstChild('StarterGear')
if starterGear then
if starterGear:FindFirstChild(tool.Name) then
StarterToolFound = true
local slot = LowestEmptySlot or MakeSlot(ScrollingFrame)
for i = slot.Index, 1, -1 do
local curr = Slots[i] -- An empty slot, because above
local pIndex = i - 1
if pIndex > 0 then
local prev = Slots[pIndex] -- Guaranteed to be full, because above
prev:Swap(curr)
else
curr:Fill(tool)
end
end
-- Have to manually unequip a possibly equipped tool
for _, child in pairs(Character:GetChildren()) do
if child:IsA('Tool') and child ~= tool then
child.Parent = Backpack
end
end
AdjustHotbarFrames()
return -- We're done here
end
end
end
-- The tool is either moving or new
local slot = SlotsByTool[tool]
if slot then
slot:UpdateEquipView()
else -- New! Put into lowest hotbar slot or new inventory slot
slot = LowestEmptySlot or MakeSlot(ScrollingFrame)
slot:Fill(tool)
if slot.Index <= HOTBAR_SLOTS and not InventoryFrame.Visible then
AdjustHotbarFrames()
end
if tool:IsA('HopperBin') then --NOTE: HopperBin
if tool.Active then
UnequipAllTools()
ActiveHopper = tool
end
end
end
end
local function OnChildRemoved(child) -- From Character or Backpack
if not child:IsA('Tool') and not child:IsA('HopperBin') then --NOTE: HopperBin
return
end
local tool = child
-- Ignore this event if we're just moving between the two
local newParent = tool.Parent
if newParent == Character or newParent == Backpack then
return
end
local slot = SlotsByTool[tool]
if slot then
slot:Clear()
if slot.Index > HOTBAR_SLOTS then -- Inventory slot
slot:Delete()
elseif not InventoryFrame.Visible then
AdjustHotbarFrames()
end
end
if tool == ActiveHopper then --NOTE: HopperBin
ActiveHopper = nil
end
end
local function OnCharacterAdded(character)
-- First, clean up any old slots
for i = #Slots, 1, -1 do
local slot = Slots[i]
if slot.Tool then
slot:Clear()
end
if i > HOTBAR_SLOTS then
slot:Delete()
end
end
ActiveHopper = nil --NOTE: HopperBin
-- And any old connections
for _, conn in pairs(CharConns) do
conn:disconnect()
end
CharConns = {}
-- Hook up the new character
Character = character
table.insert(CharConns, character.ChildRemoved:connect(OnChildRemoved))
table.insert(CharConns, character.ChildAdded:connect(OnChildAdded))
for _, child in pairs(character:GetChildren()) do
OnChildAdded(child)
end
--NOTE: Humanoid is set inside OnChildAdded
-- And the new backpack, when it gets here
Backpack = Player:WaitForChild('Backpack')
table.insert(CharConns, Backpack.ChildRemoved:connect(OnChildRemoved))
table.insert(CharConns, Backpack.ChildAdded:connect(OnChildAdded))
for _, child in pairs(Backpack:GetChildren()) do
OnChildAdded(child)
end
AdjustHotbarFrames()
end
local function OnInputBegan(input, isProcessed)
-- Pass through keyboard hotkeys when not typing into a TextBox and not disabled (except for the Drop key)
if input.UserInputType == Enum.UserInputType.Keyboard and not TextBoxFocused and (WholeThingEnabled or input.KeyCode.Value == DROP_HOTKEY_VALUE) then
local hotkeyBehavior = HotkeyFns[input.KeyCode.Value]
if hotkeyBehavior then
hotkeyBehavior(isProcessed)
end
end
end
local function OnUISChanged(property)
if property == 'KeyboardEnabled' then
local on = UserInputService.KeyboardEnabled
for i = 1, HOTBAR_SLOTS do
Slots[i]:TurnNumber(on)
end
end
end
local function OnCoreGuiChanged(coreGuiType, enabled)
-- Check for enabling/disabling the whole thing
if coreGuiType == Enum.CoreGuiType.Backpack or coreGuiType == Enum.CoreGuiType.All then
WholeThingEnabled = enabled
MainFrame.Visible = enabled
-- Eat/Release hotkeys (Doesn't affect UserInputService)
for _, keyString in pairs(HotkeyStrings) do
if enabled then
GuiService:AddKey(keyString)
else
GuiService:RemoveKey(keyString)
end
end
end
-- Also check if the Health GUI is showing, and shift everything down (or back up) accordingly
if not TopBarEnabled and (coreGuiType == Enum.CoreGuiType.Health or coreGuiType == Enum.CoreGuiType.All) then
MainFrame.Position = UDim2.new(0, 0, 0, enabled and HOTBAR_OFFSET_FROMBOTTOM or 0)
end
end
--------------------
--| Script Logic |--
--------------------
-- First check if the TopBar is enabled. This affects the ArrowFrame existence and MainFrame position
pcall(function() TopBarEnabled = settings():GetFFlag('UseInGameTopBar') end)
-- Make the main frame, which (mostly) covers the screen
MainFrame = NewGui('Frame', 'Backpack')
MainFrame.Visible = false
MainFrame.Parent = RobloxGui
-- Make the HotbarFrame, which holds only the Hotbar Slots
HotbarFrame = NewGui('Frame', 'Hotbar')
HotbarFrame.Size = HOTBAR_SIZE
HotbarFrame.Position = UDim2.new(0.5, -HotbarFrame.Size.X.Offset / 2, 1, -HotbarFrame.Size.Y.Offset)
HotbarFrame.Parent = MainFrame
-- Make all the Hotbar Slots
for i = 1, HOTBAR_SLOTS do
local slot = MakeSlot(HotbarFrame, i)
slot.Frame.Visible = false
if not LowestEmptySlot then
LowestEmptySlot = slot
end
end
-- Make the Inventory, which holds the ScrollingFrame, the header, and the search box
InventoryFrame = NewGui('Frame', 'Inventory')
InventoryFrame.BackgroundTransparency = BACKGROUND_FADE
InventoryFrame.BackgroundColor3 = BACKGROUND_COLOR
InventoryFrame.Active = true
InventoryFrame.Size = UDim2.new(0, HotbarFrame.Size.X.Offset, 0, (HotbarFrame.Size.Y.Offset * INVENTORY_ROWS) + INVENTORY_HEADER_SIZE)
InventoryFrame.Position = UDim2.new(0.5, -InventoryFrame.Size.X.Offset / 2, 1, HotbarFrame.Position.Y.Offset - InventoryFrame.Size.Y.Offset)
InventoryFrame.Visible = false
InventoryFrame.Parent = MainFrame
-- Make the ScrollingFrame, which holds the rest of the Slots (however many)
ScrollingFrame = NewGui('ScrollingFrame', 'ScrollingFrame')
ScrollingFrame.Size = UDim2.new(1, ScrollingFrame.ScrollBarThickness + 1, 1, -INVENTORY_HEADER_SIZE)
ScrollingFrame.Position = UDim2.new(0, 0, 0, INVENTORY_HEADER_SIZE)
ScrollingFrame.CanvasSize = UDim2.new(0, 0, 0, 0)
ScrollingFrame.Parent = InventoryFrame
-- Make the header title, in the Inventory
--local headerText = NewGui('TextLabel', 'Header')
--headerText.Text = TITLE_TEXT
--headerText.TextXAlignment = Enum.TextXAlignment.Left
--headerText.Font = Enum.Font.SourceSansBold
--headerText.FontSize = Enum.FontSize.Size48
--headerText.TextStrokeColor3 = SLOT_EQUIP_COLOR
--headerText.TextStrokeTransparency = BACKGROUND_FADE
--headerText.Size = UDim2.new(0, (InventoryFrame.Size.X.Offset / 2) - TITLE_OFFSET, 0, INVENTORY_HEADER_SIZE)
--headerText.Position = UDim2.new(0, TITLE_OFFSET, 0, 0)
--headerText.Parent = InventoryFrame
do -- Search stuff
local searchFrame = NewGui('Frame', 'Search')
searchFrame.BackgroundColor3 = SEARCH_BACKGROUND_COLOR
searchFrame.BackgroundTransparency = SEARCH_BACKGROUND_FADE
searchFrame.Size = UDim2.new(0, SEARCH_WIDTH - (SEARCH_BUFFER * 2), 0, INVENTORY_HEADER_SIZE - (SEARCH_BUFFER * 2))
searchFrame.Position = UDim2.new(1, -searchFrame.Size.X.Offset - SEARCH_BUFFER, 0, SEARCH_BUFFER)
searchFrame.Parent = InventoryFrame
local searchBox = NewGui('TextBox', 'TextBox')
searchBox.Text = SEARCH_TEXT
searchBox.ClearTextOnFocus = false
searchBox.FontSize = Enum.FontSize.Size24
searchBox.TextXAlignment = Enum.TextXAlignment.Left
searchBox.Size = searchFrame.Size - UDim2.new(0, SEARCH_TEXT_OFFSET_FROMLEFT, 0, 0)
searchBox.Position = UDim2.new(0, SEARCH_TEXT_OFFSET_FROMLEFT, 0, 0)
searchBox.Parent = searchFrame
local xButton = NewGui('TextButton', 'X')
xButton.Text = 'x'
xButton.TextColor3 = SLOT_EQUIP_COLOR
xButton.FontSize = Enum.FontSize.Size24
xButton.TextYAlignment = Enum.TextYAlignment.Bottom
xButton.BackgroundColor3 = SEARCH_BACKGROUND_COLOR
xButton.BackgroundTransparency = 0
xButton.Size = UDim2.new(0, searchFrame.Size.Y.Offset - (SEARCH_BUFFER * 2), 0, searchFrame.Size.Y.Offset - (SEARCH_BUFFER * 2))
xButton.Position = UDim2.new(1, -xButton.Size.X.Offset - (SEARCH_BUFFER * 2), 0.5, -xButton.Size.Y.Offset / 2)
xButton.ZIndex = 3
xButton.Visible = false
xButton.Parent = searchFrame
local clickArea = NewGui('TextButton', 'GimmieYerClicks')
clickArea.ZIndex = 2
clickArea.Parent = searchFrame
local function search()
local terms = {}
for word in searchBox.Text:gmatch('%S+') do
terms[word:lower()] = true
end
local hitTable = {}
for i = HOTBAR_SLOTS + 1, #Slots do -- Only search inventory slots
local slot = Slots[i]
local hits = slot:CheckTerms(terms)
table.insert(hitTable, {slot, hits})
slot.Frame.Visible = false
end
table.sort(hitTable, function(left, right)
return left[2] > right[2]
end)
ResultsIndices = {}
for i, data in ipairs(hitTable) do
local slot, hits = data[1], data[2]
if hits > 0 then
ResultsIndices[slot] = HOTBAR_SLOTS + i
slot:Reposition()
slot.Frame.Visible = true
end
end
ScrollingFrame.CanvasPosition = Vector2.new(0, 0)
xButton.Visible = true
end
local function clearResults()
if xButton.Visible then
ResultsIndices = nil
for i = HOTBAR_SLOTS + 1, #Slots do
local slot = Slots[i]
slot:Reposition()
slot.Frame.Visible = true
end
xButton.Visible = false
end
end
local function reset()
clearResults()
searchBox.Text = SEARCH_TEXT
end
local function onChanged(property)
if property == 'Text' then
local text = searchBox.Text
if text == '' then
clearResults()
elseif text ~= SEARCH_TEXT then
search()
end
end
end
local function gainFocus()
searchBox:CaptureFocus()
if searchBox.Text == SEARCH_TEXT then
searchBox.Text = ''
end
end
local function focusLost(enterPressed)
if enterPressed then
--TODO: Could optimize
search()
elseif searchBox.Text == '' then
searchBox.Text = SEARCH_TEXT
end
end
clickArea.MouseButton1Click:connect(gainFocus)
xButton.MouseButton1Click:connect(reset)
searchBox.Changed:connect(onChanged)
searchBox.FocusLost:connect(focusLost)
BackpackScript.StateChanged.Event:connect(function(isNowOpen)
clickArea.Modal = isNowOpen -- Allows free mouse movement even in first person
if not isNowOpen then
reset()
end
end)
HotkeyFns[Enum.KeyCode.Escape.Value] = function(isProcessed)
if isProcessed then -- Pressed from within a TextBox
reset()
elseif InventoryFrame.Visible then
BackpackScript.OpenClose()
end
end
end
do -- Make the Inventory expand/collapse arrow (unless TopBar)
local arrowFrame, arrowIcon, clickArea = nil, nil, nil
local collapsed, closed, opened = nil, nil, nil
local function openClose()
if not next(Dragging) then -- Only continue if nothing is being dragged
InventoryFrame.Visible = not InventoryFrame.Visible
local nowOpen = InventoryFrame.Visible
if arrowIcon and clickArea then
arrowIcon.Image = (nowOpen) and ARROW_IMAGE_CLOSE or ARROW_IMAGE_OPEN
end
AdjustHotbarFrames()
UpdateArrowFrame()
HotbarFrame.Active = not HotbarFrame.Active
for i = 1, HOTBAR_SLOTS do
Slots[i]:SetClickability(not nowOpen)
end
end
BackpackScript.StateChanged:Fire(InventoryFrame.Visible)
end
HotkeyFns[ARROW_HOTKEY] = openClose
BackpackScript.OpenClose = openClose -- Exposed
if not TopBarEnabled then
arrowFrame = NewGui('Frame', 'Arrow')
arrowFrame.BackgroundTransparency = BACKGROUND_FADE
arrowFrame.BackgroundColor3 = BACKGROUND_COLOR
arrowFrame.Size = UDim2.new(0, ICON_SIZE, 0, ICON_SIZE / 2)