Edit
To clarifiy, I’m converting offset to scale because the itemframe won’t work properly on different sized screens, such as in this example.
Of course, any other solution other than converting from offset to scale is also welcome.
As the image buttons for the items are created through a script when a player picks up an item, I have to find a way to properly convert the image buttons’ position and size from offset to scale.
function InventoryUI:OffsetToScale(Offset, absoluteSize)
return UDim2.new(Offset.X/absoluteSize.X, 0, Offset.Y/absoluteSize.Y, 0)
end
function InventoryUI:PositionToSpace(ItemFrame, FirstGrid)
-- FirstGrid is the top left starting grid
local pos = FirstGrid.AbsolutePosition - ItemFrame.Parent.AbsolutePosition
return self:OffsetToScale(pos, ItemFrame.Parent.AbsolutePosition)
--return UDim2.fromOffset(pos.X, pos.Y)
end
function InventoryUI:CreateItemFrame(FirstGrid)
local ItemFrame = Instance.new("ImageButton", Backpack.ScrollingFrame)
ItemFrame.BorderSizePixel = 0
ItemFrame.Size = UDim2.new(0, 1 * GridSize.X, 0, 1 * GridSize.Y) -- Otherwise, ItemFrame.AbsoluteSize is 0,0
ItemFrame.Size = self:OffsetToScale(ItemFrame.AbsoluteSize, ItemFrame.Parent.AbsoluteSize)
end)
However, clearly, something isn’t working as the resulting ItemFrames scaled position and size is this.
It should be fitting perfectly along the first column of the inventory grid.
This is the current hierarchy in PlayerGui, where the image button is the ItemFrame, and the Grid frame contains all the grids in the Inventory.