整理框架代码
整理手柄的摇杆/扳机支持代码
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
NONE={}function NULL()end
|
NONE={}function NULL()end
|
||||||
EDITING=""
|
EDITING=""
|
||||||
LOADED=false
|
LOADED=false
|
||||||
ERRDATA={}
|
|
||||||
|
|
||||||
--Pure lua modules (basic)
|
--Pure lua modules (basic)
|
||||||
MATH= require'Zframework.mathExtend'
|
MATH= require'Zframework.mathExtend'
|
||||||
@@ -74,16 +73,8 @@ local xOy=SCR.xOy
|
|||||||
local ITP=xOy.inverseTransformPoint
|
local ITP=xOy.inverseTransformPoint
|
||||||
|
|
||||||
local mx,my,mouseShow=-20,-20,false
|
local mx,my,mouseShow=-20,-20,false
|
||||||
joysticks={}
|
local jsState={}--map, joystickID->axisStates: {axisName->axisVal}
|
||||||
local joystick_last_known_axis_value={}
|
local errData={}--list, each error create {mes={errMes strings},scene=sceneNameStr}
|
||||||
local joystick_key_events_name={
|
|
||||||
leftx={'leftstick_left', 'leftstick_right'},
|
|
||||||
lefty={'leftstick_up', 'leftstick_down'},
|
|
||||||
rightx={'rightstick_left', 'rightstick_right'},
|
|
||||||
righty={'rightstick_up', 'rightstick_down'},
|
|
||||||
triggerleft='triggerleft',
|
|
||||||
triggerright='triggerright'
|
|
||||||
}
|
|
||||||
|
|
||||||
local devMode
|
local devMode
|
||||||
|
|
||||||
@@ -297,121 +288,75 @@ function love.textinput(texts)
|
|||||||
WIDGET.textinput(texts)
|
WIDGET.textinput(texts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--analog sticks: -1, 0, 1 for neg, neutral, pos
|
||||||
|
--triggers: 0 for released, 1 for pressed
|
||||||
|
local jsAxisEventName={
|
||||||
|
leftx={'leftstick_left','leftstick_right'},
|
||||||
|
lefty={'leftstick_up','leftstick_down'},
|
||||||
|
rightx={'rightstick_left','rightstick_right'},
|
||||||
|
righty={'rightstick_up','rightstick_down'},
|
||||||
|
triggerleft='triggerleft',
|
||||||
|
triggerright='triggerright'
|
||||||
|
}
|
||||||
function love.joystickadded(JS)
|
function love.joystickadded(JS)
|
||||||
local id, instanceid = JS:getID()
|
jsState[JS:getID()]={
|
||||||
-- analog sticks: -1, 0, 1 for neg, neutral, pos
|
_loveJSObj=JS,
|
||||||
-- triggers: 0 for released, 1 for pressed
|
leftx=0,lefty=0,
|
||||||
joystick_last_known_axis_value[id]={
|
rightx=0,righty=0,
|
||||||
leftx=0,
|
triggerleft=0,triggerright=0
|
||||||
lefty=0,
|
|
||||||
rightx=0,
|
|
||||||
righty=0,
|
|
||||||
triggerleft=0,
|
|
||||||
triggerright=0
|
|
||||||
}
|
}
|
||||||
table.insert(joysticks,JS)
|
|
||||||
MES.new('info',"Joystick added")
|
MES.new('info',"Joystick added")
|
||||||
end
|
end
|
||||||
function love.joystickremoved(JS)
|
function love.joystickremoved(JS)
|
||||||
local i=TABLE.find(joysticks,JS)
|
local js=jsState[JS:getID()]
|
||||||
if i then
|
if js then
|
||||||
local id, instanceid = JS:getID()
|
love.gamepadaxis(JS,'leftx',0)
|
||||||
-- send key events for neutral positions, so that it doesn't
|
love.gamepadaxis(JS,'lefty',0)
|
||||||
-- leave phantom held keys
|
love.gamepadaxis(JS,'rightx',0)
|
||||||
love.gamepadaxis(JS, 'leftx', 0)
|
love.gamepadaxis(JS,'righty',0)
|
||||||
love.gamepadaxis(JS, 'lefty', 0)
|
love.gamepadaxis(JS,'triggerleft',-1)
|
||||||
love.gamepadaxis(JS, 'rightx', 0)
|
love.gamepadaxis(JS,'triggerright',-1)
|
||||||
love.gamepadaxis(JS, 'righty', 0)
|
jsState[JS:getID()]=nil
|
||||||
love.gamepadaxis(JS, 'triggerleft', -1)
|
|
||||||
love.gamepadaxis(JS, 'triggerright', -1)
|
|
||||||
-- remove data for last known axis values
|
|
||||||
joystick_last_known_axis_value[id]=nil
|
|
||||||
table.remove(joysticks,i)
|
|
||||||
MES.new('info',"Joystick removed")
|
MES.new('info',"Joystick removed")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.gamepadaxis(joystick, axis, value)
|
function love.gamepadaxis(JS,axis,val)
|
||||||
-- This function serves as a proxy that sends analog axises events to
|
local js=jsState[JS:getID()]
|
||||||
-- the regular gamepad keypress function.
|
if js then
|
||||||
-- This proxy is sensitive to individual joysticks, however, you may
|
if axis=='leftx'or axis=='lefty'or axis=='rightx'or axis=='righty'then
|
||||||
-- encounter issues when performing inputs on two different controllers
|
local newVal=--range: [0,1]
|
||||||
-- at the same time.
|
val>.5 and 1 or
|
||||||
-- This is because the gamepadpressed and gamepadreleased functions
|
val<-.5 and -1 or
|
||||||
-- don't distinguish different controllers, and is not issue of this
|
0
|
||||||
-- function.
|
if newVal~=js[axis]then
|
||||||
|
if js[axis]==-1 then
|
||||||
-- This function cannot prevent diagonals because it takes axises one
|
love.gamepadreleased(JS,jsAxisEventName[axis][1])
|
||||||
-- at a time. If such feature is to be added, the implementation would
|
elseif js[axis]~=0 then
|
||||||
-- have to be overhauled to consider both axises on one analog stick
|
love.gamepadreleased(JS,jsAxisEventName[axis][2])
|
||||||
-- at the same time.
|
end
|
||||||
|
if newVal==-1 then
|
||||||
-- The names of the keypresses fired are defined in the `joystick_
|
love.gamepadpressed(JS,jsAxisEventName[axis][1])
|
||||||
-- key_events_name` dictionary.
|
elseif newVal==1 then
|
||||||
|
love.gamepadpressed(JS,jsAxisEventName[axis][2])
|
||||||
-- In production, please hook these to an appropriate settings value
|
end
|
||||||
local stickSensitivity=0.5
|
js[axis]=newVal
|
||||||
local triggerSensitivity=0.5
|
end
|
||||||
-- conversion is made because trigger values are -1 to +1
|
elseif axis=='triggerleft'or axis=='triggerright'then
|
||||||
local triggerThreshold = triggerSensitivity*2-1
|
local newVal=val>0 and 1 or 0--range: [-1,1]
|
||||||
local id, instanceid = joystick:getID()
|
if newVal~=js[axis]then
|
||||||
local newAxisValue
|
if newVal==1 then
|
||||||
if axis=='leftx'
|
love.gamepadpressed(JS,jsAxisEventName[axis])
|
||||||
or axis=='lefty'
|
|
||||||
or axis=='rightx'
|
|
||||||
or axis=='righty' then
|
|
||||||
-- doing this because lack of ?: in lua and I can't remember how
|
|
||||||
-- the and-or hack works. Feel free to change that for me
|
|
||||||
if value > stickSensitivity then
|
|
||||||
newAxisValue=1
|
|
||||||
elseif value < -stickSensitivity then
|
|
||||||
newAxisValue=-1
|
|
||||||
else
|
else
|
||||||
newAxisValue=0
|
love.gamepadreleased(JS,jsAxisEventName[axis])
|
||||||
end
|
end
|
||||||
if newAxisValue == joystick_last_known_axis_value[id][axis] then
|
js[axis]=newVal
|
||||||
-- nothing changed, no event needs to be fired
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if joystick_last_known_axis_value[id][axis] ~=0 then
|
|
||||||
-- If the axis goes from negative to positive (or reverse) within
|
|
||||||
-- one single fire of this function, the key release is fired
|
|
||||||
-- first, then the key press of the opposite direction.
|
|
||||||
-- This is to prevent issues with having opposite keys pressed
|
|
||||||
-- at the same time.
|
|
||||||
if joystick_last_known_axis_value[id][axis] == -1 then
|
|
||||||
love.gamepadreleased(joystick, joystick_key_events_name[axis][1])
|
|
||||||
else
|
|
||||||
love.gamepadreleased(joystick, joystick_key_events_name[axis][2])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if newAxisValue == -1 then
|
|
||||||
love.gamepadpressed(joystick, joystick_key_events_name[axis][1])
|
|
||||||
elseif newAxisValue == 1 then
|
|
||||||
love.gamepadpressed(joystick, joystick_key_events_name[axis][2])
|
|
||||||
end
|
|
||||||
joystick_last_known_axis_value[id][axis] = newAxisValue
|
|
||||||
elseif axis=='triggerleft'
|
|
||||||
or axis=='triggerright' then
|
|
||||||
if value > triggerThreshold then
|
|
||||||
newAxisValue=1
|
|
||||||
else
|
|
||||||
newAxisValue=0
|
|
||||||
end
|
|
||||||
if newAxisValue == joystick_last_known_axis_value[id][axis] then
|
|
||||||
-- nothing changed, no event needs to be fired
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if newAxisValue == 1 then
|
|
||||||
love.gamepadpressed(joystick, joystick_key_events_name[axis])
|
|
||||||
else
|
|
||||||
love.gamepadreleased(joystick, joystick_key_events_name[axis])
|
|
||||||
end
|
|
||||||
joystick_last_known_axis_value[id][axis] = newAxisValue
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local keyMirror={
|
local dPadToKey={
|
||||||
dpup='up',
|
dpup='up',
|
||||||
dpdown='down',
|
dpdown='down',
|
||||||
dpleft='left',
|
dpleft='left',
|
||||||
@@ -423,39 +368,16 @@ function love.gamepadpressed(_,i)
|
|||||||
mouseShow=false
|
mouseShow=false
|
||||||
if SCN.swapping then return end
|
if SCN.swapping then return end
|
||||||
if SCN.gamepadDown then SCN.gamepadDown(i)
|
if SCN.gamepadDown then SCN.gamepadDown(i)
|
||||||
elseif SCN.keyDown then SCN.keyDown(keyMirror[i]or i)
|
elseif SCN.keyDown then SCN.keyDown(dPadToKey[i]or i)
|
||||||
elseif i=="back"then SCN.back()
|
elseif i=="back"then SCN.back()
|
||||||
else WIDGET.gamepadPressed(keyMirror[i]or i)
|
else WIDGET.gamepadPressed(dPadToKey[i]or i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function love.gamepadreleased(_,i)
|
function love.gamepadreleased(_,i)
|
||||||
if SCN.swapping then return end
|
if SCN.swapping then return end
|
||||||
if SCN.gamepadUp then SCN.gamepadUp(i)end
|
if SCN.gamepadUp then SCN.gamepadUp(i)end
|
||||||
end
|
end
|
||||||
--[[
|
|
||||||
function love.joystickpressed(JS,k)
|
|
||||||
mouseShow=false
|
|
||||||
if SCN.swapping then return end
|
|
||||||
if SCN.gamepadDown then SCN.gamepadDown(i)
|
|
||||||
elseif SCN.keyDown then SCN.keyDown(keyMirror[i]or i)
|
|
||||||
elseif i=="back"then SCN.back()
|
|
||||||
else WIDGET.gamepadPressed(i)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function love.joystickreleased(JS,k)
|
|
||||||
if SCN.swapping then return end
|
|
||||||
if SCN.gamepadUp then SCN.gamepadUp(i)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function love.joystickaxis(JS,axis,val)
|
|
||||||
|
|
||||||
end
|
|
||||||
function love.joystickhat(JS,hat,dir)
|
|
||||||
|
|
||||||
end
|
|
||||||
function love.sendData(data)end
|
|
||||||
function love.receiveData(id,data)end
|
|
||||||
]]
|
|
||||||
function love.filedropped(file)
|
function love.filedropped(file)
|
||||||
if SCN.fileDropped then SCN.fileDropped(file)end
|
if SCN.fileDropped then SCN.fileDropped(file)end
|
||||||
end
|
end
|
||||||
@@ -515,20 +437,20 @@ function love.errorhandler(msg)
|
|||||||
love.audio.stop()
|
love.audio.stop()
|
||||||
gc.reset()
|
gc.reset()
|
||||||
|
|
||||||
if LOADED and #ERRDATA<3 then
|
if LOADED and #errData<3 then
|
||||||
BG.set('none')
|
BG.set('none')
|
||||||
local scn=SCN and SCN.cur or"NULL"
|
local scn=SCN and SCN.cur or"NULL"
|
||||||
table.insert(ERRDATA,{mes=err,scene=scn})
|
table.insert(errData,{mes=err,scene=scn})
|
||||||
|
|
||||||
--Write messages to log file
|
--Write messages to log file
|
||||||
love.filesystem.append('conf/error.log',
|
love.filesystem.append('conf/error.log',
|
||||||
os.date("%Y/%m/%d %A %H:%M:%S\n")..
|
os.date("%Y/%m/%d %A %H:%M:%S\n")..
|
||||||
#ERRDATA.." crash(es) "..love.system.getOS().."-"..VERSION.string.." scene: "..scn.."\n"..
|
#errData.." crash(es) "..love.system.getOS().."-"..VERSION.string.." scene: "..scn.."\n"..
|
||||||
table.concat(err,"\n",1,c-2).."\n\n"
|
table.concat(err,"\n",1,c-2).."\n\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
--Get screencapture
|
--Get screencapture
|
||||||
gc.captureScreenshot(function(_)ERRDATA[#ERRDATA].shot=gc.newImage(_)end)
|
gc.captureScreenshot(function(_)errData[#errData].shot=gc.newImage(_)end)
|
||||||
gc.present()
|
gc.present()
|
||||||
|
|
||||||
--Create a new mainLoop thread to keep game alive
|
--Create a new mainLoop thread to keep game alive
|
||||||
@@ -651,7 +573,7 @@ function love.run()
|
|||||||
--Scene Launch
|
--Scene Launch
|
||||||
while #SCN.stack>0 do SCN.pop()end
|
while #SCN.stack>0 do SCN.pop()end
|
||||||
SCN.push('quit','slowFade')
|
SCN.push('quit','slowFade')
|
||||||
SCN.init(#ERRDATA==0 and'load'or'error')
|
SCN.init(#errData==0 and'load'or'error')
|
||||||
|
|
||||||
return function()
|
return function()
|
||||||
local _
|
local _
|
||||||
@@ -815,6 +737,9 @@ end
|
|||||||
|
|
||||||
local Z={}
|
local Z={}
|
||||||
|
|
||||||
|
Z.js=jsState
|
||||||
|
Z.errData=errData
|
||||||
|
|
||||||
function Z.setIfPowerInfo(func)showPowerInfo=func end
|
function Z.setIfPowerInfo(func)showPowerInfo=func end
|
||||||
|
|
||||||
--[Warning] Color and line width is uncertain value, set it in the function.
|
--[Warning] Color and line width is uncertain value, set it in the function.
|
||||||
|
|||||||
4
main.lua
4
main.lua
@@ -629,9 +629,9 @@ if TABLE.find(arg,'--test')then
|
|||||||
TASK.new(function()
|
TASK.new(function()
|
||||||
while true do
|
while true do
|
||||||
YIELD()
|
YIELD()
|
||||||
if ERRDATA[1]then break end
|
if Z.errData[1]then break end
|
||||||
end
|
end
|
||||||
LOG("\27[91m\27[1mAutomatic Test Failed :(\27[0m\nThe error message is:\n"..table.concat(ERRDATA[1].mes,"\n").."\27[91m\nAborting\27[0m")
|
LOG("\27[91m\27[1mAutomatic Test Failed :(\27[0m\nThe error message is:\n"..table.concat(Z.errData[1].mes,"\n").."\27[91m\nAborting\27[0m")
|
||||||
TEST.yieldN(60)
|
TEST.yieldN(60)
|
||||||
love.event.quit(1)
|
love.event.quit(1)
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ local scene={}
|
|||||||
|
|
||||||
function scene.sceneInit()
|
function scene.sceneInit()
|
||||||
BGcolor=rnd()>.026 and{.3,.5,.9}or{.62,.3,.926}
|
BGcolor=rnd()>.026 and{.3,.5,.9}or{.62,.3,.926}
|
||||||
stateInfo=SYSTEM.."-"..VERSION.string.." scene:"..ERRDATA[#ERRDATA].scene
|
stateInfo=SYSTEM.."-"..VERSION.string.." scene:"..Z.errData[#Z.errData].scene
|
||||||
errorText=LOADED and text.errorMsg or"An error has occurred during loading.\nError info has been created, and you can send it to the author."
|
errorText=LOADED and text.errorMsg or"An error has occurred during loading.\nError info has been created, and you can send it to the author."
|
||||||
errorShot,errorInfo=ERRDATA[#ERRDATA].shot,ERRDATA[#ERRDATA].mes
|
errorShot,errorInfo=Z.errData[#Z.errData].shot,Z.errData[#Z.errData].mes
|
||||||
NET.wsclose_app()
|
NET.wsclose_app()
|
||||||
NET.wsclose_user()
|
NET.wsclose_user()
|
||||||
NET.wsclose_play()
|
NET.wsclose_play()
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ function scene.update()
|
|||||||
if kb.isDown("down","s")then dy=dy-10 F=true end
|
if kb.isDown("down","s")then dy=dy-10 F=true end
|
||||||
if kb.isDown("left","a")then dx=dx+10 F=true end
|
if kb.isDown("left","a")then dx=dx+10 F=true end
|
||||||
if kb.isDown("right","d")then dx=dx-10 F=true end
|
if kb.isDown("right","d")then dx=dx-10 F=true end
|
||||||
local js1=joysticks[1]
|
local js1=Z.js[1]
|
||||||
if js1 then
|
if js1 then
|
||||||
local dir=js1:getAxis(1)
|
local dir=js1:getAxis(1)
|
||||||
if dir~="c"then
|
if dir~="c"then
|
||||||
|
|||||||
Reference in New Issue
Block a user