【问题标题】:How to make a button API如何制作按钮 API
【发布时间】:2017-05-20 06:03:10
【问题描述】:

我正在尝试使用 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之类的东西存储为变量,然后打印变量时,它会返回该颜色的数字代码,它来自颜色API。这是我所拥有的:

--to use the newButton function, do this:
--button.newButton(exampleButton)

--to use onClick function, create a variable like this:
--exampleFunc=function()
--(code)
--end
--Then call onClick with the same variable:

--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 返回,而不是颜色代码。我还需要能够检查单击了哪个按钮并在单击其中一个按钮时运行用户指定的功能。

【问题讨论】:

  • 在函数newButton 中,for...do 应替换为if...then
  • 我已经尝试过了,但是在 'in' 附近会出现一个错误,说 'then' 应该是。

标签: arrays lua


【解决方案1】:

我将浏览您的原型代码并指出我看到的一些错误,并尝试回答您的问题。我将假设您要将表中的键值设置为数组并从外部访问它。


对您的问题的快速而简短的回答是,您可以将表存储在表中并通过键或索引访问它们。但是,我要进行的设计更改是将您的 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 = 10buttons["quitButton"]["x"] = 10buttons.quitButton["x"] = 10,而不是写buttons.quitButton.x = 10

This page 是了解 Lua 表的绝佳起点。


根据this pageos.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

当您的意思是 buttons 时,您在多个实例中引用变量 button

【讨论】:

  • 谢谢,其中一些非常有帮助,但是,表格对我来说仍然是一个新事物,我不确定“键”是什么,我假设它就像一个 id给定值?
  • 键值对类似于变量。键是可以识别值的任何句柄(变量名),值是您要存储的任何数据(变量值)。在同一个表中,您可以有多个具有相同值的唯一键,但不能有一个键引用多个值,除非该值是另一个表。另外,如果您认为我的回答对您有帮助且令人满意,请务必点击分数下方的复选标记,将其标记为已接受。
  • 另外,由于 Lua 对我来说还是有点新,我不确定如何循环遍历按钮表。
  • 我建议阅读this页面的“迭代器”部分。
猜你喜欢
  • 1970-01-01
  • 2011-12-28
  • 2021-10-23
  • 2012-05-03
  • 2012-03-21
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多