Skip to content

Instantly share code, notes, and snippets.

@josefnpat
Created September 17, 2013 06:45
Show Gist options
  • Save josefnpat/6590807 to your computer and use it in GitHub Desktop.
Save josefnpat/6590807 to your computer and use it in GitHub Desktop.
Dip Switch for Love
local dipswitch = {}
dipswitch.theme = {}
dipswitch.theme.default = {}
dipswitch.theme.default.dip = {}
dipswitch.theme.default.dip.padding = 8
dipswitch.theme.default.dip.color = {2,105,223}
dipswitch.theme.default.switch = {}
dipswitch.theme.default.switch.x = 16
dipswitch.theme.default.switch.y = 32
dipswitch.theme.default.switch.gap = 4
dipswitch.theme.default.switch.bgcolor = {156,172,197}
dipswitch.theme.default.switch.color = {255,255,255}
dipswitch.theme.default.switch.padding = 2
function dipswitch.new(size,x,y,theme)
local dip = {}
-- init vector
dip.data = {}
for i = 1,size do
dip.data[i] = false
end
-- init draw coords
dip.x,dip.y = x,y
-- allow theme override
dip.theme = theme or dipswitch.theme.default
-- add the love events
dip.draw = dipswitch.draw
dip.mousepressed = dipswitch.mousepressed
-- add the sub draw functions
dip.draw_dip = dipswitch.draw_dip
dip.draw_switchwell = dipswitch.draw_switchwell
dip.draw_switch = dipswitch.draw_switch
dip.dip_getdim = dipswitch.dip_getdim
dip.switchwell_getdim = dipswitch.switchwell_getdim
dip.switch_getdim = dipswitch.switch_getdim
-- add the lower level interactions
dip.bitset = dipswitch.bitset
dip.bitget = dipswitch.bitget
dip.decset = dipswitch.decset
dip.decget = dipswitch.decget
return dip
end
function dipswitch.bitset(self,i,val) -- flip on no val
if val == false or val == true then
self.data[i] = val
else
self.data[i] = not self.data[i]
end
end
function dipswitch.bitget(self,i)
return self.data[i]
end
function dipswitch.decset(self,val)
local d = {}
for i = 1,#self.data do
local rem = val%2
val = math.floor(val/2)
d[i] = rem == 1 and true or false
end
self.data = d
end
function dipswitch.decget(self)
local t = 0
for i,v in pairs(self.data) do
if v then
t = t + 2^(i-1)
end
end
return t
end
function dipswitch.dip_getdim(self)
local dx = (#self.data-1)*self.theme.switch.gap+(#self.data)*self.theme.switch.x+self.theme.dip.padding*2
local dy = self.theme.switch.y + self.theme.dip.padding*2
return self.x,self.y,dx,dy
end
function dipswitch.switchwell_getdim(self,i)
local sx = self.x+self.theme.dip.padding+(i-1)*(self.theme.switch.gap+self.theme.switch.x)
local sy = self.y+self.theme.dip.padding
return sx,sy,self.theme.switch.x,self.theme.switch.y
end
function dipswitch.switch_getdim(self,i)
local swx,swy,sww,swh = self:switchwell_getdim(i)
local sw = self.theme.switch.x-self.theme.switch.padding*2
local sh = (self.theme.switch.y-self.theme.switch.padding*2)/2
if not self:bitget(i) then
swy = swy + sh
end
return swx+self.theme.switch.padding,swy+self.theme.switch.padding,sw,sh
end
function dipswitch.draw(self)
local orig_color = {love.graphics.getColor()}
local dx,dy,dw,dh = self:dip_getdim()
self:draw_dip(dx,dy,dw,dh)
for i,sdat in pairs(self.data) do
local swx,swy,sww,swh = self:switchwell_getdim(i)
self:draw_switchwell(swx,swy,sww,swh)
local sx,sy,sw,sh = self:switch_getdim(i)
self:draw_switch(sx,sy,sw,sh)
end
love.graphics.setColor(orig_color)
end
function dipswitch.draw_dip(self,x,y,w,h)
love.graphics.setColor(self.theme.dip.color)
love.graphics.rectangle("fill",x,y,w,h)
end
function dipswitch.draw_switchwell(self,x,y,w,h)
love.graphics.setColor(self.theme.switch.bgcolor)
love.graphics.rectangle("fill",x,y,w,h)
end
function dipswitch.draw_switch(self,x,y,w,h)
love.graphics.setColor(self.theme.switch.color)
love.graphics.rectangle("fill",x,y,w,h)
end
function dipswitch.mousepressed(self,x,y)
for i,switch in pairs(self.data) do
local sx,sy,sw,sh = self:switchwell_getdim(i)
if x >= sx and x <= sx + sw and
y >= sy and y <= sy + sh then
self:bitset(i)
end
end
end
return dipswitch
love.graphics.setCaption("DIP SWITCH PLAYGROUND")
dipswitchlib = require "dipswitch"
function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
alttheme = deepcopy(dipswitchlib.theme.default)
alttheme.dip.color = {232,51,58}
dips = {}
table.insert(dips,dipswitchlib.new(8 ,16,64*0+16,alttheme))
table.insert(dips,dipswitchlib.new(4 ,16,64*1+16))
table.insert(dips,dipswitchlib.new(1 ,16,64*2+16))
table.insert(dips,dipswitchlib.new(10,16,64*3+16))
function love.draw()
for _,dip in ipairs(dips) do
dip:draw()
end
end
function love.keypressed()
dips[1]:decset(85)
end
function love.mousepressed(x,y,button)
for i,dip in ipairs(dips) do
dip:mousepressed(x,y)
print(i,dip:decget())
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment