Do optimization in Piano applet (#1047)

This commit is contained in:
C6H12O6 + NaCl + H2O
2023-11-16 23:10:21 +07:00
committed by GitHub
parent a2931ea290
commit 486eaeae52
2 changed files with 62 additions and 109 deletions

View File

@@ -14,15 +14,16 @@ local inst
local offset
local tempoffset=0
local lastPlayBGM
local generateVKey
local showingKey
local pianoVK={} -- All piano key can be appear on the screen, want to see? Check the end of the code
local touches={}
local keyCount=0 -- Get key count (up to 626, can pass), used to check if we need to launch Lua's garbage collector or not
local keyCount=0 -- Get key count (up to 262, can be larger), used to check if we need to launch Lua's garbage collector or not
local textObj={} -- We will keep all text objects of note here, only used for virutal keys
local lastKeyTime -- Last time any key pressed
local lastPlayBGM
local scene={}
-- Rename all virtual key's text
@@ -91,6 +92,7 @@ function scene.enter()
keyCount=0
lastKeyTime=nil
generateVKey()
_notHoldCS()
_showVirtualKey(MOBILE)
end
@@ -144,7 +146,7 @@ function scene.keyUp()
if (
not kbIsDown('lctrl','rctrl','lshift','rshift') -- If we are not holding Ctrl or Shift keys
) and not moIsDown(1) -- and the left mouse button is not being held
-- The implementationo is really wild but I hope it will good enough to keep the virtual keys from bugs
-- The implementation is really wild but I hope it will good enough to keep the virtual keys from bugs
then _notHoldCS() end
end
@@ -158,9 +160,7 @@ function scene.draw()
-- Drawing virtual keys
if showingKey then
for _,key in pairs(pianoVK) do
key:draw()
end
for _,key in pairs(pianoVK) do key:draw() end
gc.setLineWidth(1)
gc.setColor(COLOR.Z)
gc.line(685.5,297,685.5,642)
@@ -168,11 +168,9 @@ function scene.draw()
end
function scene.update(dt)
for _,key in pairs(pianoVK) do
key:update(nil,dt)
end
for _,key in pairs(pianoVK) do key:update(nil,dt) end
if lastKeyTime and keyCount>626 and TIME()-lastKeyTime>10 then
if lastKeyTime and keyCount>262 and TIME()-lastKeyTime>10 then
collectgarbage()
lastKeyTime=nil
keyCount=0
@@ -186,101 +184,57 @@ scene.widgetList={
WIDGET.newKey {name='offset+' ,x=475 ,y=60,w=60 ,h=60,fText=CHAR.key.right,code=pressKey"ralt",hideF=function() return not showingKey end},
}
-- Set virtual keys (seperate from ZFramework)
-- Generate virtual keys (seperate from ZFramework, to use only in this scene)
-- Using hashtable to reduce usage time
pianoVK={
-- Number row: 01234567890-= 13
['1' ]=WIDGET.newKey{x= 75,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('1' ) end},
['2' ]=WIDGET.newKey{x= 165,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('2' ) end},
['3' ]=WIDGET.newKey{x= 255,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('3' ) end},
['4' ]=WIDGET.newKey{x= 345,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('4' ) end},
['5' ]=WIDGET.newKey{x= 435,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('5' ) end},
['6' ]=WIDGET.newKey{x= 525,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('6' ) end},
['7' ]=WIDGET.newKey{x= 615,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('7' ) end},
['8' ]=WIDGET.newKey{x= 755,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('8' ) end},
['9' ]=WIDGET.newKey{x= 845,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('9' ) end},
['0' ]=WIDGET.newKey{x= 935,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('0' ) end},
['-' ]=WIDGET.newKey{x=1025,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('-' ) end},
['=' ]=WIDGET.newKey{x=1115,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('=' ) end},
['backspace']=WIDGET.newKey{x=1205,y=335,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('backspace') end},
generateVKey=function()
local allRow={
{'1','2','3','4','5','6','7','8','9','0' ,'-','=','backspace'},
{'q','w','e','r','t','y','u','i','o','p','[' ,']','\\'},
{'a','s','d','f','g','h','j','k','l',';','\'','return'},
{'z','x','c','v','b','n','m',',','.','/',},
}
local keyColorInHomeRow={'R','W','P','N','Z','Z','O','L','G','C','Z','Z'}
-- Top row: QWERTYUIOP[]\ 13
['q' ]=WIDGET.newKey{x= 75,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('q' ) end},
['w' ]=WIDGET.newKey{x= 165,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('w' ) end},
['e' ]=WIDGET.newKey{x= 255,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('e' ) end},
['r' ]=WIDGET.newKey{x= 345,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('r' ) end},
['t' ]=WIDGET.newKey{x= 435,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('t' ) end},
['y' ]=WIDGET.newKey{x= 525,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('y' ) end},
['u' ]=WIDGET.newKey{x= 615,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('u' ) end},
['i' ]=WIDGET.newKey{x= 755,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('i' ) end},
['o' ]=WIDGET.newKey{x= 845,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('o' ) end},
['p' ]=WIDGET.newKey{x= 935,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('p' ) end},
['[' ]=WIDGET.newKey{x=1025,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('[' ) end},
[']' ]=WIDGET.newKey{x=1115,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown(']' ) end},
['\\']=WIDGET.newKey{x=1205,y=425,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('\\') end},
for row,keysInRow in pairs(allRow) do
for keyIndex,keyChar in pairs(keysInRow) do
-- Create the base first
local K=WIDGET.newKey{
x=75+90*(keyIndex-1)+50*(keyIndex>7 and 1 or 0),
y=335+90*(row-1),
w=75,h=75,
-- Home row ASDFGHJKL;''<ENTER> 12
['a' ]=WIDGET.newKey{x= 75,y=515,w=75,h=75,sound=false,font=35,fText='',color='R',code=function() scene.keyDown('a' ) end},
['s' ]=WIDGET.newKey{x= 165,y=515,w=75,h=75,sound=false,font=35,fText='',color='W',code=function() scene.keyDown('s' ) end},
['d' ]=WIDGET.newKey{x= 255,y=515,w=75,h=75,sound=false,font=35,fText='',color='P',code=function() scene.keyDown('d' ) end},
['f' ]=WIDGET.newKey{x= 345,y=515,w=75,h=75,sound=false,font=35,fText='',color='N',code=function() scene.keyDown('f' ) end},
['g' ]=WIDGET.newKey{x= 435,y=515,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('g' ) end},
['h' ]=WIDGET.newKey{x= 525,y=515,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('h' ) end},
['j' ]=WIDGET.newKey{x= 615,y=515,w=75,h=75,sound=false,font=35,fText='',color='O',code=function() scene.keyDown('j' ) end},
['k' ]=WIDGET.newKey{x= 755,y=515,w=75,h=75,sound=false,font=35,fText='',color='L',code=function() scene.keyDown('k' ) end},
['l' ]=WIDGET.newKey{x= 845,y=515,w=75,h=75,sound=false,font=35,fText='',color='G',code=function() scene.keyDown('l' ) end},
[';' ]=WIDGET.newKey{x= 935,y=515,w=75,h=75,sound=false,font=35,fText='',color='C',code=function() scene.keyDown(';' ) end},
['\'' ]=WIDGET.newKey{x=1025,y=515,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('\'' ) end},
['return']=WIDGET.newKey{x=1115,y=515,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('return') end},
font=35,fText='',sound=false,
color=row==3 and keyColorInHomeRow[keyIndex] or 'Z',
code=function() scene.keyDown(keyChar) end
}
-- Bottom row ZXCVBNM,./ 10
['z']=WIDGET.newKey{x= 75,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('z') end},
['x']=WIDGET.newKey{x=165,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('x') end},
['c']=WIDGET.newKey{x=255,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('c') end},
['v']=WIDGET.newKey{x=345,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('v') end},
['b']=WIDGET.newKey{x=435,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('b') end},
['n']=WIDGET.newKey{x=525,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('n') end},
['m']=WIDGET.newKey{x=615,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('m') end},
[',']=WIDGET.newKey{x=755,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown(',') end},
['.']=WIDGET.newKey{x=845,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('.') end},
['/']=WIDGET.newKey{x=935,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() scene.keyDown('/') end},
-- Ctrl and Shift 2
['ctrl' ]=WIDGET.newKey{x=1115,y=605,w=75 ,h=75,sound=false,font=35,fText='',color='Z',code=function() if not tempoffset==-1 then _holdingCtrl() else _notHoldCS() end end},
['shift']=WIDGET.newKey{x=1205,y=605,w=75 ,h=75,sound=false,font=35,fText='',color='Z',code=function() if not tempoffset== 1 then _holdingShift() else _notHoldCS() end end},
}
-- Set objects text
pianoVK.ctrl :setObject(CHAR.key.ctrl )
pianoVK.shift:setObject(CHAR.key.shift)
-- Overwrite some functions
for _,K in pairs(pianoVK) do
-- Overwrite the update function
function K:update(activateState,dt)
-- activateState
-- 0 - Off
-- 1 - On then off
-- 2 - On
local ATV=self.ATV
local maxTime=6.2
if activateState~=nil then self.activateState=activateState
elseif (self.activateState==1 and ATV==maxTime) or not self.activateState then self.activateState=0 end
-- LIKELY NOT POSSIBLE TO DO
-- Holding key: self.activateState=activateState and activateState or not ATV>maxTime and self.activateState or 0 end
if dt then
if self.activateState>0 then
self.ATV=min(ATV+dt*60,maxTime)
elseif ATV>0 then
self.ATV=max(ATV-dt*30,0)
-- Then modify the base to get the key we expected
function K:update(activateState,dt)
-- activateState: 0=off, 1=on then off, 2=on
local activationTime=self.activationTime or 0
local maxTime=6.2
if activateState~=nil then self.activateState=activateState
elseif (self.activateState==1 and activationTime==maxTime) or not self.activateState then self.activateState=0 end
-- LIKELY NOT POSSIBLE TO DO
-- Holding key: self.activateState=activateState and activateState or not activationTime>maxTime and self.activateState or 0 end
if dt then
if self.activateState>0 then self.activationTime=min(activationTime+dt*60,maxTime)
elseif activationTime>0 then self.activationTime=max(activationTime-dt*30,0)
end
end
end
K.getCenter,K.drag,K.release=nil
pianoVK[keyChar]=K
K=nil
end
end
-- Remove unnecessary function (reduce memory usage)
function K:getCenter() end
function K:drag() end
function K:release() end
-- Special case
pianoVK.ctrl =WIDGET.newKey{x=1115,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() if not tempoffset==-1 then _holdingCtrl() else _notHoldCS() end end}
pianoVK.ctrl :setObject(CHAR.key.ctrl )
pianoVK.shift=WIDGET.newKey{x=1205,y=605,w=75,h=75,sound=false,font=35,fText='',color='Z',code=function() if not tempoffset== 1 then _holdingShift() else _notHoldCS() end end}
pianoVK.shift:setObject(CHAR.key.shift)
end
return scene

View File

@@ -55,8 +55,8 @@ local function _scanDict(D)
end
local function _getList() return result[1] and result or dict end
local textBox=WIDGET.newTextBox{name='infoBox',x=320,y=180,w=862,h=526,font=25,fix=true}
local inputBox=WIDGET.newInputBox{name='input',x=20,y=110,w=762,h=60,font=40,limit=32}
local contentBox=WIDGET.newTextBox{name='contentBox',x=320,y=180,w=862,h=526,font=25,fix=true}
local inputBox=WIDGET.newInputBox{name='inputBox',x=20,y=110,w=762,h=60,font=40,limit=32}
local listBox=WIDGET.newListBox{name='listBox',x=20,y=180,w=280,h=526,font=30,lineH=35,drawF=function(item,id,ifSel)
-- Background
if ifSel then
@@ -75,7 +75,7 @@ local function _updateContentBox()
_t,t=pcall(function() return _getList()[listBox.selected].content end)
if not _t then t={"???"} end
local _w,c=FONT.get(currentFontSize):getWrap(t,840)
textBox:setTexts(c)
contentBox:setTexts(c)
end
-- Clear the result
local function _clearResult()
@@ -130,9 +130,8 @@ end
-- Changing font size, z=0 --> reset
local function _setZoom(z)
currentFontSize=MATH.clamp(z~=0 and currentFontSize+z or 25,15,40)
textBox.font=currentFontSize
textBox.lineH=currentFontSize*7/5 -- Recalculate the line's height
textBox.capacity=math.ceil((textBox.h-10)/textBox.lineH)
contentBox.font=currentFontSize
contentBox:reset()
_updateContentBox()
MES.new("check",z~=0 and text.dict.sizeChanged:repD(currentFontSize) or text.dict.sizeReset,1.26)
end
@@ -166,7 +165,7 @@ function scene.wheelMoved(_,y)
if WIDGET.sel==listBox then
listBox:scroll(-y)
else
textBox:scroll(-y)
contentBox:scroll(-y)
end
end
function scene.keyDown(key)
@@ -174,7 +173,7 @@ function scene.keyDown(key)
-- Switching selected items
if key=='up' or key=='down' then
textBox:scroll(key=='up' and -1 or 1)
contentBox:scroll(key=='up' and -1 or 1)
elseif (key=='left' or key=='pageup' or key=='right' or key=='pagedown') then
_jumpover(key,love.keyboard.isDown('lctrl','rctrl','lalt','ralt','lshift','rshift') and 12)
elseif key=='cC' or key=='c' and love.keyboard.isDown('lctrl','rctrl') then
@@ -251,7 +250,7 @@ function scene.gamepadDown(key)
if Joystick:isGamepadDown('a') then
_setZoom(key=='dpup' and 5 or -5)
else
textBox:scroll(key=='dpup' and -3 or 3)
contentBox:scroll(key=='dpup' and -3 or 3)
end
elseif key=='dpleft' or key=='dpright' then
_jumpover(key:gsub('dp',''),Joystick:isGamepadDown('a') and 12)
@@ -308,7 +307,7 @@ scene.widgetList={
WIDGET.newText{name='title',x=100,y=15,font=70,align='L'},
listBox,
inputBox,
textBox,
contentBox,
WIDGET.newKey{name='link',x=1234,y=520,w=60,font=45,fText=CHAR.icon.globe,code=pressKey'application',hideF=function() return not (listBox.selected>0 and _getList()[listBox.selected].url) end},
WIDGET.newKey{name='copy',x=1234,y=590,w=60,font=40,fText=CHAR.icon.copy,code=pressKey'cC',hideF=function() return not (listBox.selected>0) end},