hotbar: wrap

This commit is contained in:
2026-01-09 18:10:16 +02:00
parent 4785795640
commit bc62f8bd70
4 changed files with 81 additions and 51 deletions

View File

@@ -53,7 +53,7 @@ local function isTextInputFocused(): boolean
end
local function resolveSelectedSlot(slots, desired)
if desired and desired >= 1 and desired <= HOTBAR_SIZE and slots[desired] ~= "" then
if desired and desired >= 1 and desired <= HOTBAR_SIZE then
return desired
end
for i = 1, HOTBAR_SIZE do
@@ -146,30 +146,31 @@ function Hotbar:init()
names = nextNames,
selected = nextSelected,
})
local id = nextSlots[nextSelected] or ""
local rawId = nextSlots[nextSelected] or ""
local effectiveId = rawId ~= "" and rawId or "hand"
local name = ""
if id ~= "" then
name = nextNames[id] or id
if rawId ~= "" then
name = nextNames[rawId] or rawId
end
PlacementState:SetSelected(id, name)
PlacementState:SetSelected(effectiveId, name)
end
self._setSelected = function(slot: number)
if slot < 1 or slot > HOTBAR_SIZE then
return
end
local info = ClientState:GetSlotInfo(slot)
if not info then
return
end
ClientState:SetSelectedSlot(slot)
self:setState({
selected = slot,
})
local id = tostring(info.id)
local name = info.name or id
Util.StudioLog("[PLACE][CLIENT][SELECT]", "slot", slot, "id", id, "name", name)
PlacementState:SetSelected(id, name)
local rawId = self.state.slots[slot] or ""
local effectiveId = rawId ~= "" and rawId or "hand"
local name = ""
if rawId ~= "" then
name = self.state.names[rawId] or rawId
end
Util.StudioLog("[PLACE][CLIENT][SELECT]", "slot", slot, "id", effectiveId, "name", name)
PlacementState:SetSelected(effectiveId, name)
end
self._handleInput = function(input: InputObject, gameProcessedEvent: boolean)
@@ -207,7 +208,7 @@ function Hotbar:init()
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
Util.StudioLog("[INPUT][CLIENT]", "MouseButton2", "processed", gameProcessedEvent)
-- Allow click even if gameProcessedEvent (UI can set this), but only if we're actually pointing at a block
local mouseBlock = PM:DebugGetPlacementOrWarn()
local mouseBlock = PM:DebugGetPlacementOrWarn(true) -- skip selection outline on right click
if not mouseBlock then
return
end
@@ -249,7 +250,7 @@ function Hotbar:init()
return
end
local delta = direction > 0 and -1 or 1
local nextSlot = math.clamp(self.state.selected + delta, 1, HOTBAR_SIZE)
local nextSlot = ((self.state.selected - 1 + delta) % HOTBAR_SIZE) + 1
if nextSlot ~= self.state.selected then
self._setSelected(nextSlot)
end
@@ -268,12 +269,13 @@ function Hotbar:didMount()
self._syncFromClientState()
self:_refreshViewports()
-- initialize selection broadcast
local id = self.state.slots and self.state.slots[self.state.selected] or ""
local rawId = self.state.slots and self.state.slots[self.state.selected] or ""
local effectiveId = rawId ~= "" and rawId or "hand"
local name = ""
if id ~= "" and self.state.names then
name = self.state.names[id] or id
if rawId ~= "" and self.state.names then
name = self.state.names[rawId] or rawId
end
PlacementState:SetSelected(id, name)
PlacementState:SetSelected(effectiveId, name)
end
function Hotbar:willUnmount()
@@ -345,7 +347,7 @@ function Hotbar:render()
}),
IndexLabel = Roact.createElement("TextLabel", {
BackgroundTransparency = 1,
Position = UDim2.fromOffset(4, 2),
Position = UDim2.fromOffset(8, 4),
Size = UDim2.fromOffset(18, 14),
Font = Enum.Font.Gotham,
Text = i == 10 and "0" or tostring(i),
@@ -412,6 +414,7 @@ function Hotbar:render()
BorderSizePixel = 0,
Position = UDim2.new(0.5, 0, 1, -80-10),
Size = UDim2.fromOffset(0, 25),
Visible = selectedName ~= "",
}, {
Corner = Roact.createElement("UICorner", {
CornerRadius = UDim.new(0, 8),