動画はこちら
button
--initialize
selectFlg=false
isTouching=false
--class
button={}
button.new=function(x,y,w,h,str)
local obj={}
obj.x=x
obj.y=y
obj.w=w
obj.h=h
obj.isBool=false
obj.str=str
obj.getBool=function (this)
return this.isBool
end
obj.display=function(this)
screen.setColor(255,255,255)
screen.drawRectF(this.x,this.y,this.w,this.h)
if this.isBool then
screen.setColor(0,255,0)
else
screen.setColor(255,0,0)
end
screen.drawText(this.x,this.y,this.str)
end
obj.isPointIn=function (this, pointX, pointY)
return pointX > this.x and pointY > this.y and pointX < this.x+this.w and pointY < this.y+this.h
end
return obj
end
--toggle button
toggleButton={}
toggleButton.new=function(x,y,w,h,str)
local obj=button.new(x,y,w,h,str)
obj.toggle=function(this)
this.isBool=not(this.isBool)
end
return obj
end
--push button
pushButton={}
pushButton.new=function(x,y,w,h,str)
local obj=button.new(x,y,w,h,str)
obj.push=function(this)
this.isBool=true
end
obj.release=function(this)
this.isBool=false
end
return obj
end
--instance
toggleButtonTbl={toggleButton.new(0,0,4,5,"A"),toggleButton.new(0,6,4,5,"B"),toggleButton.new(0,12,4,5,"C")}
pushButtonTbl={pushButton.new(0,20,4,5,"1"),pushButton.new(0,26,4,5,"2")}
function onTick()
local w,h,inputX,inputY
--input
isTouch=input.getBool(1)
w=input.getNumber(1)
h=input.getNumber(2)
inputX=input.getNumber(3)
inputY=input.getNumber(4)
--Touch display
if isTouch==true and isTouching==false then
isTouching=true
--toggle
for k, value in pairs(toggleButtonTbl) do
if value:isPointIn(inputX,inputY) then
value:toggle()
end
end
--push
for k, value in pairs(pushButtonTbl) do
if value:isPointIn(inputX,inputY) then
value:push()
end
end
end
--no touch
if isTouch==false then
isTouching=false
for k, value in pairs(pushButtonTbl) do
value:release()
end
end
end
function onDraw()
--display
local w,h
w=screen.getWidth()
h=screen.getHeight()
for k, value in pairs(toggleButtonTbl) do
value:display()
end
for k, value in pairs(pushButtonTbl) do
value:display()
end
end