----------------------------------------------------------------------------------------------------
Joybutton = class()
function Joybutton:init(x, y, width, height)
self.x = x -- 按钮坐标X
self.y = y -- 按钮坐标Y
self.width = width -- 按钮宽度
self.height = height -- 按钮高度
self.caption = "" -- 按钮文本
self.drawMode = CENTER -- 绘制方式
self.enabled = true -- 是否激活
self.visible = true -- 是否可见
self.font = "ArialMT" -- 文本字体
self.fontsize = 18 -- 文本字体大小
self.textcolor = color(0, 0) -- 文本颜色
self.backcolor = color(0, 0) -- 背景颜色
self.strokecolor = color(0, 0) -- 边框颜色
self.strokewidth = 2 -- 边框宽度
self.touchedStrokewidth = 5 -- 触摸边框宽度
self.touchedStrokecolor = color(0, 0) -- 触摸边框颜色
self.touchedTextcolor = color(0, 0) -- 触摸文本颜色
self.touchedBackcolor = color(0, 0) -- 触摸背景颜色
self.touchedSound = "" -- 触摸声音
self.backImg = "" -- 背景图像
self.foreImg = {"", 0, 0, 0, 0} -- 前景图像
-- 格式
-- {sprite, X, Y, width, height}
-- x,y为相对按钮的坐标。
-- 若要使用图片原始大小,width,height请填0
-- 下同
self.touchedBackimg = "" -- 触摸背景图像
self.touchedForeimg = {"", 0 ,0 ,0 ,0} -- 触摸前景图像
self.touch_event = {function() end} -- 触摸事件
-- 使用方法:
-- 实例化一个新的Button,比如:newButton = Button()
-- 在你的程序中自定义一个新的函数作为事件函数
-- 比如:function newButton_touchEvent(),在里面写上事件的代码
-- 然后在setup()中赋值:newButton.touch_event = {newButton_touchEvent}
-- 以上
self.isTouched = false
end
function Joybutton:draw()
if self.visible == false then
return
end
local dx, dy
local textW, textH
if self.drawMode == CENTER then
dx = self.x - self.width / 2
dy = self.y - self.height / 2
elseif self.drawMode == CORNER then
dx = self.x
dy = self.y
end
pushStyle()
spriteMode(CORNER)
textMode(CORNER)
rectMode(CORNER)
smooth()
if self.isTouched == false then
if self.backImg ~= "" then
sprite(self.backImg, dx, dy, self.width, self.height)
end
if self.foreImg[1] ~= "" then
if self.foreImg[4] == 0 and self.foreImg[5] == 0 then
sprite(self.foreImg[1], dx + self.foreImg[2], dy + self.foreImg[3])
else
sprite(self.foreImg[1], dx + self.foreImg[2], dy + self.foreImg[3],
self.foreImg[4], self.foreImg[5])
end
end
strokeWidth(self.strokewidth)
stroke(self.strokecolor)
fill(self.backcolor)
rect(dx, dy, self.width, self.height)
else
if self.touchedBackimg ~= "" then
sprite(self.touchedBackimg, dx, dy, self.width, self.height)
end
if self.touchedForeimg[1] ~= "" then
if self.touchedForeimg[4] == 0 and self.touchedForeimg[5] == 0 then
sprite(self.touchedForeimg[1], dx + self.touchedForeimg[2],
dy + self.touchedForeimg[3])
else
sprite(self.touchedForeimg[1], dx + self.touchedForeimg[2],
dy + self.touchedForeimg[3], self.touchedForeimg[4], self.touchedForeimg[5])
end
end
strokeWidth(self.touchedStrokewidth)
stroke(self.touchedStrokecolor)
fill(self.touchedBackcolor)
rect(dx, dy, self.width, self.height)
end
if self.enabled == false then
fill(110, 110, 110)
elseif self.isTouched == true then
fill(self.touchedTextcolor)
else
fill(self.textcolor)
end
font(self.font)
fontSize(self.fontsize)
textAlign(CENTER)
textWrapWidth(self.width)
textW, textH = textSize(self.caption)
text(self.caption, dx + self.width / 2 - textW / 2, dy + self.height / 2 - textH / 2)
popStyle()
end
function Joybutton:touched(touch)
if self.visible == false or self.enabled == false then
return
end
local dx, dy
if self.drawMode == CENTER then
dx = self.x - self.width / 2
dy = self.y - self.height / 2
elseif self.drawMode == CORNER then
dx = self.x
dy = self.y
end
if touch.x >= dx and touch.x <= dx + self.width and touch.y >= dy and
touch.y <= dy + self.height then
self.isTouched = true
sound(self.touchedSound)
self.touch_event[1]()
else
self.isTouched = false
end
end