diff --git a/docs/api-reference.md b/docs/api-reference.md index ebdc9dd..be0fa36 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -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) @@ -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 ``` diff --git a/src/TopbarIcon.client.luau b/src/TopbarIcon.client.luau index 79e1ec2..730112c 100644 --- a/src/TopbarIcon.client.luau +++ b/src/TopbarIcon.client.luau @@ -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)