1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ストワで始めるLua第5回のコード

1
Posted at

動画はこちら

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
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?