Skip to content

Commit ef605b0

Browse files
committed
Merge branch 'react-luau' of https://github.com/ryanlua/satchel into react-luau
2 parents 493bf8c + 28216f5 commit ef605b0

14 files changed

Lines changed: 161 additions & 138 deletions

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.rbxmx linguist-language=XML

src/Client.client.luau

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
local GuiService = game:GetService("GuiService")
44
local Players = game:GetService("Players")
55
local StarterGui = game:GetService("StarterGui")
6+
local UserInputService = game:GetService("UserInputService")
67

78
local React = require(script.Parent.Parent.react)
89
local ReactRoblox = require(script.Parent.Parent["react-roblox"])
10+
local closeInventory = require(script.Parent.Api.closeInventory)
911

1012
local App = require(script.Parent.Components.App)
1113

@@ -36,34 +38,56 @@ local function Satchel()
3638
local inventoryClosedSignal = bindableEvents.InventoryClosed.Event:Connect(function()
3739
setOpened(false)
3840
end)
41+
local menuOpenedSignal = GuiService.MenuOpened:Connect(function()
42+
closeInventory()
43+
end)
3944

4045
return function()
4146
inventoryOpenedSignal:Disconnect()
4247
inventoryClosedSignal:Disconnect()
48+
menuOpenedSignal:Disconnect()
4349
end
4450
end, {})
4551

52+
-- Close backpack when clicking outside of it
53+
UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
54+
local inputType = input.UserInputType
55+
if not gameProcessedEvent then
56+
if inputType == Enum.UserInputType.MouseButton1 or inputType == Enum.UserInputType.Touch then
57+
closeInventory()
58+
end
59+
end
60+
end)
61+
4662
-- Change slots based on viewport display size
4763
local slots, setSlots = React.useState(10)
4864
local rows, setRows = React.useState(4)
4965

5066
React.useEffect(function()
51-
local function viewportDisplaySizeChanged()
67+
local function updateBackpackSize()
68+
local screenOrientation = playerGui.CurrentScreenOrientation
5269
local viewportSize = GuiService.ViewportDisplaySize
53-
if viewportSize == Enum.DisplaySize.Small then
54-
setSlots(5)
70+
71+
if screenOrientation == Enum.ScreenOrientation.Portrait then
72+
setSlots(3)
5573
setRows(3)
74+
elseif viewportSize == Enum.DisplaySize.Small then
75+
setSlots(5)
76+
setRows(2)
5677
else
5778
setSlots(10)
5879
setRows(4)
5980
end
6081
end
6182

62-
viewportDisplaySizeChanged()
63-
local signal = GuiService:GetPropertyChangedSignal("ViewportDisplaySize"):Connect(viewportDisplaySizeChanged)
83+
updateBackpackSize()
84+
local viewportSignal = GuiService:GetPropertyChangedSignal("ViewportDisplaySize"):Connect(updateBackpackSize)
85+
local orientationSignal =
86+
playerGui:GetPropertyChangedSignal("CurrentScreenOrientation"):Connect(updateBackpackSize)
6487

6588
return function()
66-
signal:Disconnect()
89+
viewportSignal:Disconnect()
90+
orientationSignal:Disconnect()
6791
end
6892
end, {})
6993

src/Components/App.luau

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export type Props = {
1919
forceGamepadHintVisible: boolean?,
2020
forceKeyboardHintVisible: boolean?,
2121
slots: number?,
22-
rows: number?, -- TODO: Add rows for inventory height
23-
tools: { Tool? }?,
22+
rows: number?,
23+
items: { Tool | HopperBin }?,
2424
opened: boolean?,
2525
}
2626

@@ -69,7 +69,7 @@ local function App(props: Props)
6969
forceKeyboardHintVisible = props.forceKeyboardHintVisible,
7070
slots = props.slots,
7171
rows = props.rows,
72-
tools = props.tools,
72+
items = props.items,
7373
opened = props.opened,
7474
onAbsoluteSizeChanged = setBackpackSize,
7575
}),

src/Components/Backpack.luau

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type Props = {
99
forceKeyboardHintVisible: boolean?,
1010
slots: number?,
1111
rows: number?, -- TODO: Add rows for inventory height
12-
tools: { Tool? }?,
12+
items: { Tool | HopperBin }?,
1313
opened: boolean?,
1414
onAbsoluteSizeChanged: ((Vector2) -> ())?,
1515
}
@@ -18,15 +18,15 @@ local function Backpack(props: Props)
1818
local opened = props.opened or false
1919
local slots = props.slots or 10
2020

21-
-- Tools at indices 1..slots are hotbar; tools above slots are inventory.
22-
local hotbarTools: { [number]: Tool? } = {}
23-
local inventoryTools: { [number]: Tool? } = {}
24-
if props.tools then
25-
for index, tool in props.tools do
26-
if tool and index > 0 and index <= slots then
27-
hotbarTools[index] = tool
28-
elseif tool and index > slots then
29-
inventoryTools[index - slots] = tool
21+
-- Items at indices 1..slots are hotbar; items above slots are inventory.
22+
local hotbarItems: { [number]: Tool | HopperBin } = {}
23+
local inventoryItems: { [number]: Tool | HopperBin } = {}
24+
if props.items then
25+
for index, item in props.items do
26+
if item and index > 0 and index <= slots then
27+
hotbarItems[index] = item
28+
elseif item and index > slots then
29+
inventoryItems[index - slots] = item
3030
end
3131
end
3232
end
@@ -42,13 +42,13 @@ local function Backpack(props: Props)
4242
Hotbar = React.createElement(Hotbar, {
4343
forceKeyboardHintVisible = props.forceKeyboardHintVisible,
4444
slots = slots,
45-
tools = hotbarTools,
45+
items = hotbarItems,
4646
opened = opened,
4747
}),
4848
Inventory = React.createElement(Inventory, {
4949
slots = slots,
5050
rows = props.rows,
51-
tools = inventoryTools,
51+
items = inventoryItems,
5252
opened = opened,
5353
}),
5454
})

src/Components/Hotbar.luau

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ local Slot = require(script.Parent.Slot)
77
export type Props = {
88
forceKeyboardHintVisible: boolean?,
99
slots: number?,
10-
tools: { Tool? }?,
10+
items: { Tool | HopperBin }?,
1111
opened: boolean?,
1212
}
1313

1414
local function Hotbar(props: Props)
1515
local slotCount = props.slots or 10
1616

17-
local children = {}
17+
local children: any = {}
1818

19-
for slotNumber = 1, slotCount do
20-
local tool = props.tools and props.tools[slotNumber]
21-
local slotUnlocked = props.opened and tool ~= nil
19+
-- Create hotbar slots
20+
for order = 1, slotCount do
21+
local item = props.items and props.items[order]
22+
local slotUnlocked = props.opened and item ~= nil
2223

2324
-- Only show slots if the hotbar is opened or if there is a tool in the slot
24-
if props.opened or tool then
25-
children[slotNumber] = React.createElement(Slot, {
26-
order = slotNumber,
27-
tool = tool,
25+
if props.opened or item then
26+
children[tostring(order)] = React.createElement(Slot, {
27+
order = order,
28+
item = item,
2829
hint = true,
2930
forceHintVisible = props.forceKeyboardHintVisible,
3031
unlocked = slotUnlocked,

src/Components/Inventory.luau

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local Slot = require(script.Parent.Slot)
88
export type Props = {
99
width: number?,
1010
rows: number?,
11-
tools: { Tool? }?,
11+
items: { Tool | HopperBin }?,
1212
opened: boolean?,
1313
}
1414

@@ -17,15 +17,16 @@ local function Inventory(props: Props)
1717
local rows = props.rows or 4
1818
local height = rows * 65 - 5
1919

20-
local slotChildren = {}
20+
local children: any = {}
2121

22-
if props.tools then
23-
for order, tool in props.tools do
24-
if tool then
25-
slotChildren[order] = React.createElement(Slot, {
22+
-- Create inventory slots
23+
if props.items then
24+
for order, item in props.items do
25+
if item then
26+
children[tostring(order)] = React.createElement(Slot, {
2627
unlocked = true,
2728
order = order,
28-
tool = tool,
29+
item = item,
2930
})
3031
end
3132
end
@@ -39,7 +40,7 @@ local function Inventory(props: Props)
3940
SearchBox = React.createElement(Searchbar),
4041
SlotFrame = React.createElement("ScrollingFrame", {
4142
Size = UDim2.new(1, 0, 0, height),
42-
}, slotChildren),
43+
}, children),
4344
})
4445
end
4546

src/Components/Slot.luau

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local React = require(script.Parent.Parent.Parent.react)
77
local Tooltip = require(script.Parent.Tooltip)
88

99
export type Props = {
10-
tool: Tool?,
10+
item: (Tool | HopperBin)?,
1111
equipped: boolean?,
1212
unlocked: boolean?,
1313
forceHintVisible: boolean?,
@@ -16,10 +16,10 @@ export type Props = {
1616
}
1717

1818
local function Slot(props: Props)
19-
local tool = props.tool
20-
local toolName = tool and tool.Name
21-
local toolImage = tool and tool.TextureId
22-
local tooltipText = tool and tool.ToolTip
19+
local item = props.item
20+
local itemName = item and item.Name
21+
local itemImage = item and item.TextureId
22+
local tooltipText = item and (item:IsA("Tool") and item.ToolTip or "")
2323

2424
local equipped, setEquipped = React.useState(props.equipped or false)
2525

@@ -67,8 +67,8 @@ local function Slot(props: Props)
6767
end
6868

6969
-- Hide name if there is an image
70-
local slotText = toolName
71-
if toolImage ~= "" then
70+
local slotText = itemName
71+
if itemImage ~= "" then
7272
slotText = ""
7373
end
7474

@@ -77,7 +77,7 @@ local function Slot(props: Props)
7777
LayoutOrder = props.order,
7878
[React.Tag] = tags,
7979
[React.Event.Activated] = function()
80-
if props.tool then
80+
if props.item then
8181
setEquipped(not equipped)
8282
end
8383
end :: any,
@@ -88,7 +88,7 @@ local function Slot(props: Props)
8888
[React.Tag] = "SlotNumber",
8989
}),
9090
TextureIcon = React.createElement("ImageLabel", {
91-
Image = toolImage or "",
91+
Image = itemImage or "",
9292
}),
9393
ToolTip = React.createElement(Tooltip, {
9494
text = tooltipText,

0 commit comments

Comments
 (0)