Skip to content

Commit

Permalink
piano小程序添加键盘状态可视化
Browse files Browse the repository at this point in the history
框架跟进
  • Loading branch information
MrZ626 committed Nov 20, 2024
1 parent 9bd9600 commit 21dd8f0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Zenitha
Submodule Zenitha updated 4 files
+59 −31 escape.lua
+182 −150 init.lua
+19 −13 log.lua
+10 −11 message.lua
1 change: 1 addition & 0 deletions assets/gamefunc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ end
getTouches=love.touch.getTouches
isMouseDown=love.mouse.isDown
isKeyDown=love.keyboard.isDown
isSCDown=love.keyboard.isScancodeDown
local isKeyDown=isKeyDown
function isCtrlPressed() return isKeyDown('lctrl','rctrl') end
function isShiftPressed() return isKeyDown('lshift','rshift') end
Expand Down
87 changes: 69 additions & 18 deletions assets/scene_app/piano.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
local gc=love.graphics

local instList={'organ_wave','square_wave','saw_wave','complex_wave','stairs_wave','spectral_wave'}
local keys={
-- ['1']=60,['2']=62,['3']=64,['4']=65,['5']=67,['6']=69,['7']=71,['8']=72,['9']=74,['0']=76,['-']=77,['=']=79,['backspace']=81,
['2']=37,['3']=39, ['5']=42,['6']=44,['7']=46, ['9']=49,['0']=51, ['=']=54,['backspace']=56,
['q']=36,['w']=38,['e']=40,['r']=41,['t']=43,['y']=45,['u']=47,['i']=48,['o']=50,['p']=52,['[']=53,[']']=55,['\\']=57,
-- ['a']=36,['s']=38,['d']=40,['f']=41,['g']=43,['h']=45,['j']=47,['k']=48,['l']=50,[';']=52,["'"]=50,['return']=55,
['s']=25,['d']=27, ['g']=30,['h']=32,['j']=34, ['l']=37,[';']=39,
['z']=24,['x']=26,['c']=28,['v']=29,['b']=31,['n']=33,['m']=35,[',']=36,['.']=38,['/']=40,
local layoutList={
{
['2']=37,['3']=39, ['5']=42,['6']=44,['7']=46, ['9']=49,['0']=51, ['=']=54,['backspace']=56,
['q']=36,['w']=38,['e']=40,['r']=41,['t']=43,['y']=45,['u']=47,['i']=48,['o']=50,['p']=52,['[']=53,[']']=55,['\\']=57,
['s']=25,['d']=27, ['g']=30,['h']=32,['j']=34, ['l']=37,[';']=39,
['z']=24,['x']=26,['c']=28,['v']=29,['b']=31,['n']=33,['m']=35,[',']=36,['.']=38,['/']=40,

pos={scale=10,x=-65,y=-20},
{x=-5,y=-5,w=140,h=50}, -- Outline, just for look

{x=005,y=00,w=8,h=8,key='2'},
{x=015,y=00,w=8,h=8,key='3'},
{x=035,y=00,w=8,h=8,key='5'},
{x=045,y=00,w=8,h=8,key='6'},
{x=055,y=00,w=8,h=8,key='7'},
{x=075,y=00,w=8,h=8,key='9'},
{x=085,y=00,w=8,h=8,key='0'},
{x=105,y=00,w=8,h=8,key='='},
{x=115,y=00,w=13,h=8,key='backspace'},

{x=000,y=10,w=8,h=8,key='q'},
{x=010,y=10,w=8,h=8,key='w'},
{x=020,y=10,w=8,h=8,key='e'},
{x=030,y=10,w=8,h=8,key='r'},
{x=040,y=10,w=8,h=8,key='t'},
{x=050,y=10,w=8,h=8,key='y'},
{x=060,y=10,w=8,h=8,key='u'},
{x=070,y=10,w=8,h=8,key='i'},
{x=080,y=10,w=8,h=8,key='o'},
{x=090,y=10,w=8,h=8,key='p'},
{x=100,y=10,w=8,h=8,key='['},
{x=110,y=10,w=8,h=8,key=']'},
{x=120,y=10,w=8,h=8,key='\\'},

{x=015,y=20,w=8,h=8,key='s'},
{x=025,y=20,w=8,h=8,key='d'},
{x=045,y=20,w=8,h=8,key='g'},
{x=055,y=20,w=8,h=8,key='h'},
{x=065,y=20,w=8,h=8,key='j'},
{x=085,y=20,w=8,h=8,key='l'},
{x=095,y=20,w=8,h=8,key=';'},

{x=010,y=30,w=8,h=8,key='z'},
{x=020,y=30,w=8,h=8,key='x'},
{x=030,y=30,w=8,h=8,key='c'},
{x=040,y=30,w=8,h=8,key='v'},
{x=050,y=30,w=8,h=8,key='b'},
{x=060,y=30,w=8,h=8,key='n'},
{x=070,y=30,w=8,h=8,key='m'},
{x=080,y=30,w=8,h=8,key=','},
{x=090,y=30,w=8,h=8,key='.'},
{x=100,y=30,w=8,h=8,key='/'},
}
}
local layout=layoutList[1]
local activeEventMap={}
local inst
local offset
Expand All @@ -35,22 +82,14 @@ local _param={
param={'release',1},
}
function scene.keyDown(key,isRep,keyCode)
if not isRep and keys[keyCode] then
local note=keys[keyCode]+offset
if not isRep and layout[keyCode] then
local note=layout[keyCode]+offset
if isShiftPressed() then note=note+1 end
if isCtrlPressed() then note=note-1 end
_param.tune=note-26
_param.volume=1
_param.param[2]=release*1.0594630943592953^(note-26)
activeEventMap[keyCode]=FMOD.effect(inst,_param)
TEXT:add{
text=SFX.getNoteName(note),
x=800+(keys[keyCode]-40)*26+62*math.random(),
y=math.random(140,500),
fontSize=60,
style='score',
duration=.8,
}
elseif key=='tab' then
inst=TABLE.next(instList,inst) or instList[1]
elseif key=='lalt' then
Expand All @@ -77,21 +116,33 @@ function scene.keyDown(key,isRep,keyCode)
release=math.min(release+100,2600)
elseif key=='escape' then
if sureCheck('back') then SCN.back() end
elseif key=='`' then
layout=TABLE.next(layoutList,layout) or layoutList[1]
end
return true
end
function scene.keyUp(_,keyCode)
if keys[keyCode] and activeEventMap[keyCode] then
if layout[keyCode] and activeEventMap[keyCode] then
activeEventMap[keyCode]:stop(FMOD.FMOD_STUDIO_STOP_ALLOWFADEOUT)
activeEventMap[keyCode]=false
end
end

local rect=gc.rectangle
local isSCDown=isSCDown
function scene.draw()
FONT.set(30)
gc.print(inst,40,60)
gc.print(offset,40,100)
gc.print(release,40,140)
gc.replaceTransform(SCR.xOy_m)
gc.scale(layout.pos.scale)
gc.translate(layout.pos.x,layout.pos.y)
gc.setLineWidth(5/layout.pos.scale)
for i=1,#layout do
local key=layout[i]
rect(key.key and isSCDown(key.key) and 'fill' or 'line',key.x,key.y,key.w,key.h)
end
end

scene.widgetList={
Expand Down

0 comments on commit 21dd8f0

Please sign in to comment.