Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local manager = {}
- local OS = love.system.getOS()
- manager.joysticks = {}
- manager.buttons = {}
- manager.sliders = {}
- manager.touchList = {}
- manager.swapZones = {}
- manager.isPhone = OS == "Android"
- or OS == "iOS"
- local function err(lvl, str, ...)
- error(str:format(...), lvl)
- end
- local function isNumber(v, str)
- return type(v) == 'number' and true or err(3, str, v)
- end
- local function isString(v, str)
- return type(v) == 'string' and true or err(3, str, v)
- end
- local function dist(x1, y1, x2, y2)
- return x1 and y1 and x2 and y2 and ((x2 - x1)^2 + (y2 - y1)^2)^.5
- or x1 and y1 and (x1^2 + y1^2)^.5
- end
- function manager:getTouches()
- local t = {}
- for k, v in pairs(self.touchList) do
- table.insert({id = k, x = v.x, y = v.y})
- end
- return t
- end
- function manager:updateTouches()
- self.touchList = not self.isPhone and love.mouse.isDown(1)
- and {mouse = {x = love.mouse.getX(), y = love.mouse.getY()}}
- or {}
- for i, id in pairs(love.touch.getTouches()) do
- self.touchList[id] = {}
- self.touchList[id].x, self.touchList[id].y = love.touch.getPosition(id)
- end
- end
- function manager:addJoy(name, x, y, rad, blind, sensivity)
- local o = {}
- o.x = x or 100
- o.y = y or 100
- o.rad, o.blind = rad or 50, blind or .2
- o.tp = 0.1
- o.axes = {x = 0, y = 0}
- o.touchID = false
- o.sensivity = not sensivity
- o.touchEnable = love.touch and true or false
- self.joysticks[name or ("%4d"):format(math.random(9999))] = o
- end
- local touch -- minimize memory consumption per time (locals)
- local x, y, d -- some x, y and distance in update methods
- function manager:updateJoy()
- for k, v in pairs(self.joysticks) do
- if v.touchID then
- if self.touchList[v.touchID] then
- touch = self.touchList[v.touchID] -- using local variable
- x, y = (touch.x - v.x)/v.rad, (touch.y - v.y)/v.rad
- d = dist(x, y)
- d = d < v.blind and math.huge -- gain distance to joy in %
- or v.sensivity and d < 1 and 1
- or d
- v.axes.x, v.axes.y = x / d, y / d
- else -- if touch is freed, drop axes and touch
- v.axes.x, v.axes.y = 0, 0
- v.touchID = false
- end
- else
- for id, t in pairs(self.touchList) do
- d = dist(v.x, v.y, t.x, t.y)
- if d < v.rad then v.touchID = id end
- end
- end
- end
- end
- function manager:getJoyAxes(name)
- if self.joysticks[name] then
- return self.joysticks[name].axes.x, self.joysticks[name].axes.y
- or err(2, 'No joy has \'%s\' name', name)
- end
- end
- function manager:getJoyPosition(name)
- if self.joysticks[name] then
- return self.joysticks[name].x, self.joysticks[name].y
- or err(2, 'No joy has \'%s\' name', name)
- end
- end
- function manager:setJoyPosition(name, x, y)
- if self.joysticks[name] then
- isNumber(x, 'Joy:setPosition arg#1: number expected got %s')
- isNumber(x, 'Joy:setPosition arg#2: number expected got %s')
- self.joysticks[name].x, self.joysticks[name].y = x, y
- or err(2, 'No joy has \'%s\' name', name)
- end
- end
- function manager:update()
- self:updateTouches()
- self:updateJoy()
- end
- local graphics = love.graphics
- function manager:draw()
- for k, v in pairs(self.joysticks) do
- graphics.push('all')
- v.tp = v.touchID and 0.8 or v.tp + (0.1 - v.tp)*0.03
- graphics.setColor(255, 255, 255, v.tp*255)
- x, y = v.axes.x, v.axes.y
- graphics.circle('line', v.x, v.y, v.rad)
- graphics.circle('line', v.x, v.y, v.blind*v.rad)
- if v.touchID then
- graphics.print(tostring(v.touchID), v.x, v.y)
- end
- graphics.line(v.x, v.y, v.x + x*v.rad, v.y + y*v.rad)
- graphics.print(('%1.2f:%1.2f'):format(x, y), v.x + x*v.rad, v.y + y*v.rad)
- graphics.pop()
- end
- end
- return manager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement