Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Purse unlocks APIs for the CoreGui backpack which were previously locked to othe

## Methods

``` lua title="LocalScript - Inventory Toggle Button" linenums="1"
The following code sample, placed within a child `LocalScript` of a `GuiButton`, uses [`OpenClose()`](#openclose) to toggle the inventory on the button's `Activated` event.

``` lua title="Inventory Toggle Button" linenums="1"
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Purse = require(ReplicatedStorage.Purse)
Expand Down Expand Up @@ -86,6 +88,22 @@ Returns true.

## Events

The following code sample, placed within a child `LocalScript` of `StarterPlayerScripts`, uses [`StateChanged`](#statechanged) to detect when the inventory is toggled and prints its state to output.

``` lua title="Detect Inventory State" linenums="1"
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Purse = require(ReplicatedStorage.Purse)

Purse.StateChanged.Event:Connect(function(isNowOpen)
if isNowOpen then
print("Inventory opened")
else
print("Inventory closed")
end
end)
```

### StateChanged

```
Expand Down
27 changes: 13 additions & 14 deletions src/TopbarIcon.client.luau
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
--!strict

local UserInputService = game:GetService("UserInputService")

local BackpackScript = require(script.Parent)
local Icon = require(script.Parent.Parent.topbarplus)

local iconSelectedImage = "rbxasset://textures/ui/TopBar/inventoryOn.png"
local iconDeselectedImage = "rbxasset://textures/ui/TopBar/inventoryOff.png"
local ICON_SELECTED_IMAGE = "rbxasset://textures/ui/TopBar/inventoryOn.png"
local ICON_DESELECTED_IMAGE = "rbxasset://textures/ui/TopBar/inventoryOff.png"

local icon = Icon.new()
icon:setCaption("Inventory")
icon:setImage(iconSelectedImage, "Selected")
icon:setImage(iconDeselectedImage, "Deselected")
icon:setImage(ICON_SELECTED_IMAGE, "Selected")
icon:setImage(ICON_DESELECTED_IMAGE, "Deselected")
icon:setImageScale(1)
icon:autoDeselect(false)
icon:setOrder(-1)
icon:bindToggleKey(Enum.KeyCode.Backquote)

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean): ()
local inputType = input.UserInputType
if not gameProcessedEvent then
if inputType == Enum.UserInputType.MouseButton1 or inputType == Enum.UserInputType.Touch then
icon:deselect()
end
BackpackScript.StateChanged.Event:Connect(function(isNowOpen)
if isNowOpen then
icon:select()
else
icon:deselect()
end
end)

icon.toggled:Connect(function(): ()
BackpackScript.OpenClose()
icon.toggled:Connect(function(_isSelected, fromSource)
if fromSource == "User" then
BackpackScript.OpenClose()
end
end)