如何创建按钮 API

我正在尝试使用ComputerCraft中的Lua制作一个易于使用的按钮API,但遇到了一些麻烦。当我执行以下操作时:

os.loadAPI("button")
action=function()
    term.clear()
    term.setCursorPos(1,1)
    print("Hello!")
end

button.newButton("B1",5,5,20,10)
button.drawButton("B1",colors.orange,colors.white)
button.onClick("B1",action,true)

什么都没有发生,甚至没有绘制颜色。我进行了测试,当我将像colors.white这样的内容存储为变量,然后打印变量时,它会返回该颜色的数字代码,这来自于colors API。这是我的代码:

--要使用newButton函数,执行以下操作:
--button.newButton(exampleButton)

--要使用onClick函数,请按如下方式创建变量:
--exampleFunc=function()
--(代码)
--end
--然后使用相同的变量调用onClick:

--button.onClick(exampleButton,exampleFunc)

buttons={}
xPos=0
yPos=0

function removeButton(buttonName)
    for key, fields in pairs(buttons) do
        if key == buttonName then
            table.remove(button,buttonName)
        else
            print("ERROR: button name not available")
        end
    end
end

function onClick(buttonName,action,boolean)
    for key, fields in pairs(buttons) do
        if boolean then
            testClick(action)
        end
    end
end

function drawSeparateButton(x,y,w,h,outLineColor,fillColor)
    if key == buttonName then
        x=buttons[buttonName]["x"]
        y=buttons[buttonName]["y"]
        w=buttons[buttonName]["w"]
        h=buttons[buttonName]["h"]
        paintutils.drawBox(x,y,x+(w-1),y+(h-1),outLineColor)
        paintutils.drawFilledBox(x+1,y+1,x+(w-2),y+(h-2),fillColor)
    end
end

function testClick(action)
    for key, fields in ipairs(buttons) do
        x=buttons[buttonName]["x"]
        y=buttons[buttonName]["y"]
        w=buttons[buttonName]["w"]
        h=buttons[buttonName]["h"]
        x2=x+(w-1)
        y2=y+(h-1)
        button,xPos,yPos=os.pullEvent("mouse_click")
        if xPos>=x and xPos<=x2 and yPos>=y and yPos<=y2 then
            action()
        end
    end
end

function newButton(buttonName,X,Y,W,H)
    buttons[buttonName] = {x=X,y=Y,w=W,h=H}
end

function drawButton(buttonName,outLineColor,fillColor)
    for key, fields in ipairs(buttons) do
        if key == buttonName then
            x=buttons[buttonName]["x"]
            y=buttons[buttonName]["y"]
            w=buttons[buttonName]["w"]
            h=buttons[buttonName]["h"]
            x2=x+w-1
            y2=y+h-1
            x3=x+1
            y3=y+1
            x4=x+w-2
            y4=y+h-2
            paintutils.drawBox(x,y,x2,y2,outLineColor)
            paintutils.drawFilledBox(x3,y3,x4,y4,fillColor)
        elseif key ~= buttonName then
            print("Button name not availabel")
        end
    end
end

我只需要能够将像colors.white这样的颜色存储为变量并将其返回为colors.white,而不是颜色代码。我还需要能够检查哪个按钮被点击并运行用户指定的函数。

点赞
用户7370273
用户7370273

我将浏览你的原型代码并指出我看到的一些错误,也会尝试回答你的问题。我会假设你想在一个表格中设置一个键值并从外部访问它。


对于你的问题,一个快速简短的答案是你可以将表格嵌套在表格中,并通过键或索引访问它们。但是,我会提出一个设计更改,将 exampleFunc 存储为每个按钮表格的成员,以将其与特定的按钮相关联。

例如:

buttons = {}
buttons.playButton = {x=0, y=0, w=10, h=10, func=function() return end}
buttons.quitButton = {x=0, y=30, w=10, h=10, func=function() return end}
...
buttons.quitButton.x = 10
buttons.playButton.func()

表格具有键值结构,其中键可以是字符串或数字。根据键的数据类型,有多种访问数组的方法。

例如,我们可以写成 buttons.quitButton.x = 10,也可以写成 buttons["quitButton"].x = 10buttons["quitButton"]["x"] = 10buttons.quitButton["x"] = 10

这个页面 是学习 Lua 表格的一个很好的起点。


根据这个页面os.pullEvent() 是阻塞的,你只能每次鼠标点击检查一个按钮是否被点击。考虑循环遍历你的 buttons 表格,检查每个按钮是否落在它的矩形范围内。一旦找到鼠标点击的按钮,就可以调用它的 func 成员。在我们讨论这种方法时,while true do 循环是完全不必要的。


function removeButton(buttonName)
    for buttonName in pairs(button) do
    ...

function newButton(buttonName)
    state=true
    for buttonName in pairs(buttons) do
    ...

你可能来自 Python 背景,其中存在 if element in list 语句,但 Lua 没有这样的语句。你使用的 for 循环正在循环遍历列表的每个成员。你也没有捕获 pairs() 函数返回的所有变量。解决这个问题的方法可能如下:

function buttonFunction(buttonName)
    for key, fields in pairs(buttons) do
        if key == buttonName then
        ...
    end
end

有多个实例引用变量 button,但实际上要引用的是 buttons

2017-01-05 16:55:01