Skip to content

Instantly share code, notes, and snippets.

@Achie72
Last active April 14, 2024 17:14
Show Gist options
  • Save Achie72/8b0ca61d5dd0525560c2db9ecaacd735 to your computer and use it in GitHub Desktop.
Save Achie72/8b0ca61d5dd0525560c2db9ecaacd735 to your computer and use it in GitHub Desktop.
Janky Selector "Lib"
pico-8 cartridge // http://www.pico-8.com
version 42
__lua__
-- selector library
-- by achiegamedev
-- the selector object you will be playing and
-- calling around with. You can create a new
-- one by calling selector:init({object})
-- where object has the same values as here
-- plus any other you want to add to
selector = {
value = 0, -- current numerical value it holds
name = nil, -- name that you can search and filter logic with in if-s
doWrap = false, -- wrap value around low-high end
low = 0, -- lowest possible value
high = 0, -- highest posible value
storage = {}, -- store previous selectors inside, so you can reuse them
-- reduce the selectors value, if wrapped, go around on low end
stepDown = function (self)
self.value -= 1
if self.value < self.low then
self.value = self.doWrap and self.high or self.low
end
end,
-- increase the selectors value, if wrapped go aroud on high end
stepUp = function (self)
self.value += 1
if self.value > self.high then
self.value = self.doWrap and self.low or self.high
end
end,
-- add the current values of the selector into the storage
-- you can either pop it back from last place, from index place
-- where you can use any number, or use the index given back by find
push = function(self)
local selectordown = {}
for k, v in pairs(self) do
selectordown[k] = v
end
add(self.storage, selectordown)
end,
-- grab the last element of the storage out, and assign it to selector
pop = function(self)
local selectorpop = deli(self.storage,#self.storage)
for k, v in pairs(selectorpop) do
self[k] = v
end
end,
-- grab the i-th elements of the storage and assign it to selector
popi = function(self, _i)
local selectorpop = deli(self.storage,_i)
for k, v in pairs(selectorpop) do
self[k] = v
end
end,
-- find stored selector value by name, return index, or -1 if not found
find = function(self, _name)
for i=1,#self.storage do
if self.storage[i].name == _name then
return i
end
end
return -1
end,
-- create a new selector by and object, if there is a current value
-- inside selector push it into storage
init = function (self, _obj)
if self.name != nil then
self:push()
end
for k, v in pairs(_obj) do
self[k] = v
end
end,
-- return the current value of the selector
val = function(self)
return self.value
end
}
function _init()
selectorIndex = 1
-- create an initial selector
local sname = "selector:"..selectorIndex
selector:init({name=sname, value=1, low=1, high=5, doWrap=false})
selectorIndex += 1
end
function _update()
-- easily move any selector up-down
if btnp(0) then selector:stepDown() end
if btnp(1) then selector:stepUp() end
if btnp(5) then
-- create a new one, simulating a submenu selector maybe
-- using selectorIndex to better help visualise in the gif,
-- you can call them anything in the end, just remember
local sname = "selector:"..selectorIndex
selector:init({name=sname, value=1, low=1, high=5, doWrap=true})
selectorIndex += 1
end
if btnp(4) then
-- simulate an exist from the current selector's level,
-- this could be going up by one menupoint. Pop tself doesn't
-- save, if you want use
-- selector:push() first to save the current selector's value
-- into storage for later use, if you want of course
selector:pop()
selectorIndex -= 1
end
if btnp(2) then
-- example to find a selector from storage
res = selector:find("selector:2")
end
end
function _draw()
cls()
-- demo purposes
spr(selectorIndex, 8, selector:val()*10)
print(selector.name.." val "..selector.value.." doWrap "..tostr(selector.doWrap), 0, 0, 7)
-- show the found selector if we have enough data, note that we are checking res !=-1
if res != nil and res !=-1 and #selector.storage >= res then
local selectorFound = selector.storage[res]
print("found: "..selectorFound.name.." "..selectorFound.value, 0, 64, 7)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment