Skip to content

Instantly share code, notes, and snippets.

@microloop
Created December 14, 2025 14:37
Show Gist options
  • Select an option

  • Save microloop/70e5eb8093bb4fe9647ded9001f49216 to your computer and use it in GitHub Desktop.

Select an option

Save microloop/70e5eb8093bb4fe9647ded9001f49216 to your computer and use it in GitHub Desktop.
Toolbar Component for PICO-8
Toolbar = {}
function Toolbar:new(o)
o = o or {}
o.posx = o.posx or 0
o.sizex = o.sizex or 16
o.posy = o.posy or 16
o.sizey = o.sizey or 96
o.size = o.size or 3
o.round = o.round or 0
o.icons = o.icons or {}
setmetatable(o, {__index = self})
return o
end
function Toolbar:cls()
for _, icon in pairs(self.icons) do
icon.active = false
icon.sprite = icon.ORIGINAL_SPRITE
end
end
function Toolbar:hover()
if mouse.posx >= self.posx and mouse.posx < (self.posx + self.sizex) and mouse.posy >= self.posy and mouse.posy <= self.posy + (self.sizey) then
return true
else
return false
end
end
function Toolbar:update()
if cam then
if cam.mode == “static” then
if btn(1) then
toolbar.posx += cam.move_x
end
if btn(0) then
toolbar.posx -= cam.move_x
end
end
end
-- If we are outside the Toolbar area, switch to selected mode, otherwise switch to pointer
if mouse then
if mouse.current_mode != mouse.modes.pointer and self:hover() then
mouse:switch_to_pointer()
elseif mouse.previous_mode != mouse.current_mode and not self:hover() then
mouse:switch_previous_mode()
end
for k,icon in pairs(self.icons) do
icon:update()
end
end
end
function Toolbar:draw()
-- Inner window
rrectfill(self.posx + 1, self.posy + 1, self.sizex - 2, self.sizey, self.round, 14)
-- Shade bottom
line(self.posx + 2, self.posy + self.sizey, self.posx + self.sizex -3, self.posy + self.sizey, 2)
-- Draw icons in a horizontal grid, responsive to component position
local icon_size = 8
local padding = 4
local cols = flr((self.sizex - padding) / (icon_size + padding))
for i, icon in ipairs(self.icons) do
local col = (i - 1) % cols
local row = flr((i - 1) / cols)
local x = self.posx + padding + col * (icon_size + padding)
local y = self.posy + padding + row * (icon_size + padding)
icon.posx = x
icon.posy = y
icon:draw()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment