From 861f9b3caaab3a0b145b60a6023908e846faf4dc Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Sun, 28 Nov 2021 19:40:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E5=AE=8C=E5=96=84=E6=89=8B?= =?UTF-8?q?=E6=9F=84=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/init.lua | 64 +++++++++++++++++++++++++++++-------------- parts/scenes/mode.lua | 15 ++++++---- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Zframework/init.lua b/Zframework/init.lua index 73b2b210..7005703e 100644 --- a/Zframework/init.lua +++ b/Zframework/init.lua @@ -154,7 +154,7 @@ local function _triggerMouseDown(x,y,k) lastX,lastY=x,y if SETTING.clickFX then SYSFX.newTap(3,x,y)end end -local function _mouse_update(dt) +local function mouse_update(dt) if not KBisDown('lctrl','rctrl')and KBisDown('up','down','left','right')then local dx,dy=0,0 if KBisDown('up')then dy=dy-cursorSpd end @@ -173,6 +173,26 @@ local function _mouse_update(dt) cursorSpd=6 end end +local function gp_update(js,dt) + local sx,sy=js._jsObj:getGamepadAxis('leftx'),js._jsObj:getGamepadAxis('lefty') + if math.abs(sx)>.1 or math.abs(sy)>.1 then + local dx,dy=0,0 + if sy<-.1 then dy=dy+2*sy*cursorSpd end + if sy>.1 then dy=dy+2*sy*cursorSpd end + if sx<-.1 then dx=dx+2*sx*cursorSpd end + if sx>.1 then dx=dx+2*sx*cursorSpd end + mx=max(min(mx+dx,1280),0) + my=max(min(my+dy,720),0) + if my==0 or my==720 then + WIDGET.sel=false + WIDGET.drag(0,0,0,-dy) + end + _updateMousePos(mx,my,dx,dy) + cursorSpd=min(cursorSpd+dt*26,12.6) + else + cursorSpd=6 + end +end function love.mousepressed(x,y,k,touch) if touch then return end mouseShow=true @@ -347,35 +367,38 @@ local dPadToKey={ back='escape', } function love.joystickadded(JS) - jsState[JS:getID()]={ - _loveJSObj=JS, + table.insert(jsState,{ + _id=JS:getID(), + _jsObj=JS, leftx=0,lefty=0, rightx=0,righty=0, triggerleft=0,triggerright=0 - } + }) MES.new('info',"Joystick added") end function love.joystickremoved(JS) - local js=jsState[JS:getID()] - if js then - for i=1,#gamePadKeys do - if JS:isGamepadDown(gamePadKeys[i])then - love.gamepadreleased(JS,gamePadKeys[i]) + for i=1,#jsState do + if jsState[i]._jsObj==JS then + for j=1,#gamePadKeys do + if JS:isGamepadDown(gamePadKeys[j])then + love.gamepadreleased(JS,gamePadKeys[j]) + end end + love.gamepadaxis(JS,'leftx',0) + love.gamepadaxis(JS,'lefty',0) + love.gamepadaxis(JS,'rightx',0) + love.gamepadaxis(JS,'righty',0) + love.gamepadaxis(JS,'triggerleft',-1) + love.gamepadaxis(JS,'triggerright',-1) + MES.new('info',"Joystick removed") + table.remove(jsState,i) + break end - love.gamepadaxis(JS,'leftx',0) - love.gamepadaxis(JS,'lefty',0) - love.gamepadaxis(JS,'rightx',0) - love.gamepadaxis(JS,'righty',0) - love.gamepadaxis(JS,'triggerleft',-1) - love.gamepadaxis(JS,'triggerright',-1) - jsState[JS:getID()]=nil - MES.new('info',"Joystick removed") end end function love.gamepadaxis(JS,axis,val) - local js=jsState[JS:getID()] - if js then + if JS==jsState[1]._jsObj then + local js=jsState[1] if axis=='leftx'or axis=='lefty'or axis=='rightx'or axis=='righty'then local newVal=--range: [0,1] val>.4 and 1 or @@ -661,7 +684,8 @@ function love.run() --UPDATE STEP() - if mouseShow then _mouse_update(dt)end + if mouseShow then mouse_update(dt)end + if next(jsState)then gp_update(jsState[1],dt)end VOC_update() BG_update(dt) TEXT_update(dt) diff --git a/parts/scenes/mode.lua b/parts/scenes/mode.lua index 5c9ec451..b1c8a1c6 100644 --- a/parts/scenes/mode.lua +++ b/parts/scenes/mode.lua @@ -162,11 +162,16 @@ function scene.update() 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('right','d')then dx=dx-10 F=true end - local js1=Z.js[1] - if js1 and js1.getGamepadAxis then - dx=dx+12.6*js1:getGamepadAxis('leftx') - dy=dy+12.6*js1:getGamepadAxis('lefty') - F=true + local js=Z.js[1] + if js then + local sx,sy=js._jsObj:getGamepadAxis('leftx'),js._jsObj:getGamepadAxis('lefty') + if math.abs(sx)>.1 or math.abs(sy)>.1 then + if sy<-.1 then dy=dy-12.6*sy end + if sy>.1 then dy=dy-12.6*sy end + if sx<-.1 then dx=dx-12.6*sx end + if sx>.1 then dx=dx-12.6*sx end + F=true + end end end if F then