Touch Lua 的绘图库基础知识

Touch Lua's Draw Library Basics

警告:此问题仅适用于已购买并了解 Draw Library 的 Touch Lua 用户。

请参阅此问题的底部部分以查看完整程序。我在开头使用的片段是该程序的一部分 (NUMPAD.LUA)

好的,现在开始提问:

•“.”有什么用在 "b" 和 "x" 之间?或者 "b" 和 "draw"?等等...

•table如何设置按钮?请超级具体?

•为什么第7行和第8行有“+”、“*”和“(j-1)”?

•高度和宽度在那里做什么?我还以为只有x和y呢

function createButtons()
   buttons = { }
   local c = 1
   for i = 1, 4 do
      for j = 1, 3 do
         local b = { }
         b.x = 80 + 60 * (j-1)
         b.y = 160 + 60 * (i-1)
         b.width = 40
         b.height = 40
         b.color = draw.blue
         b.draw = drawButton
         b.action = digitAction
         buttons[c] = b
         c = c + 1
      end
   end
   buttons[1].title = '7'
   buttons[2].title = '8'
   buttons[3].title = '9'
   buttons[4].title = '4'
   buttons[5].title = '5'
   buttons[6].title = '6'
   buttons[7].title = '1'
   buttons[8].title = '2'
   buttons[9].title = '3'
   buttons[10].title = '0'
   buttons[11].title = '.'
   buttons[11].action = dotAction
   buttons[12].title = 'C'
   buttons[12].color = draw.orange
   buttons[12].action = clearAction
end

最后,将程序作为一个整体引用... •按钮如何知道您何时点击它?具体是什么代码行,它是如何工作的? (顺便说一句,我对轨道触摸的理解非常模糊)

function digitAction(button)
   if string.len(display.title) < 16 then
  sys.alert('tock')
  if display.started then
     display.title = display.title .. button.title
  else
     if button.title ~= '0' then
        display.title = button.title
        display.started = true
     end
  end
   else
  sys.alert('tink')
   end
end

function dotAction(button)
   if string.find(display.title, '.', 1, true) then
  sys.alert('tink')
   else
  display.title = display.title .. '.'
  display.started = true
  sys.alert('tock')
   end
end

function clearAction(button)
   sys.alert('tock')
   display.title = '0'
   display.started= false
end

function createDisplay()
   display = { }
   display.x = 60
   display.y = 100
   display.width = 200
   display.height = 40
   display.title = '0'
   display.color = draw.red
   display.started = false
end

function createButtons()
   buttons = { }
   local c = 1
   for i = 1, 4 do
  for j = 1, 3 do
     local b = { }
     b.x = 80 + 60 * (j-1)
     b.y = 160 + 60 * (i-1)
     b.width = 40
     b.height = 40
     b.color = draw.blue
     b.draw = drawButton
     b.action = digitAction
     buttons[c] = b
     c = c + 1
  end
   end
   buttons[1].title = '7'
   buttons[2].title = '8'
   buttons[3].title = '9'
   buttons[4].title = '4'
   buttons[5].title = '5'
   buttons[6].title = '6'
   buttons[7].title = '1'
   buttons[8].title = '2'
   buttons[9].title = '3'
   buttons[10].title = '0'
   buttons[11].title = '.'
   buttons[11].action = dotAction
   buttons[12].title = 'C'
   buttons[12].color = draw.orange
   buttons[12].action = clearAction
end

function drawDisplay()
   draw.setfont('Helvetica', 20)
   draw.setlinestyle(2, 'butt')
   local x1, y1 = display.x, display.y
   local x2, y2 = x1 + display.width, y1 + display.height
   draw.roundedrect(x1, y1, x2, y2, 10, display.color)
   local w, h = draw.stringsize(display.title)
   local x = x2 - 10 - w
   local y = y1 + (display.height - h)/2
   draw.string(display.title, x, y, draw.black)
end

function drawButton(b)
   draw.setfont('Helvetica', 18)
   draw.setlinestyle(2, 'butt')
   local x1, y1 = b.x, b.y
   local x2, y2 = x1+b.width, y1+b.height
   draw.roundedrect(x1, y1, x2, y2, 10, b.color)

   local w, h = draw.stringsize(b.title)
   local x = b.x + (b.width - w)/2
   local y = b.y + (b.height - h)/2
   draw.string(b.title, x, y, draw.black)

end

function drawButtons()
   for i = 1, #buttons do
  buttons[i].draw(buttons[i])
   end
end

function lookupButton(x, y)
   for i = 1, #buttons do
  local b = buttons[i]
  if x > b.x and x < b.x+b.width and y > b.y and y < b.y+b.height then
     return b
  end
   end
   return nil
end

function drawScreen()
   draw.beginframe()
   draw.clear()
   drawDisplay()
   drawButtons()
   draw.endframe()
end

function touchBegan(x, y)
   local b = lookupButton(x, y)
   if b then
  b.action(b)
   end
end

function touchMoved(x, y)
end

function touchEnded(x, y)
end

function init()
   draw.setscreen(1)
   draw.settitle('Num Pad')
   draw.clear()
   draw.tracktouches (touchBegan, touchMoved, touchEnded)

   createButtons()
   createDisplay()
end

function mainloop()
   while true do
  draw.doevents()
  drawScreen()
  draw.sleep(30)
   end
end

function main()
   init()
   mainloop()
end

-- start program
main()

非常感谢您提供的任何帮助!我知道这是很多问题,但这些知识确实可以帮助推动我前进!

该程序特别使用表格来表示按钮,这样做是为了使其更易于管理。高度和宽度是一样的。如果您是绘图库的新手,那么查看这样的代码只会让您感到困惑。你的大部分问题实际上都是关于 OOP 的

此外,这个问题现在可能不适合 Stack Overflow。如果你想要 help/a 关于这个主题的小教程,请随时在 Touch Lua 论坛上私信我,我的用户名是 warspyking,但目前你需要修改或删除这个问题。

WARNING: This question is only for Touch Lua users who have purchased and have knowledge of the Draw Library

从什么时候开始你必须购买东西来回答编程问题?无论如何,你所有的问题都是绝对 Lua 基础知识。

What's the use of "." between "b" and "x"? Or "b" and "draw"? Etc...

点运算符用于索引 table 成员。所以 b.x 会给你 table b 中属于键 "x" 的值。它是 b["x"].

的语法糖

How does the table set up the button? Please be super specific?

函数 createButtons 创建一个空 table 并用存储各种按钮属性的 table b 表示的按钮填充它。

Why is there a "+", "*", and "(j-1)" in lines 7 and 8?

因为那个程序的作者想要加法和乘法。这里计算坐标 b.xb.y。使用(j-1)是因为j是从1开始的,但是他为了这个计算需要它从0开始。 2 个 for 循环将创建 4 行按钮,每行包含 3 个按钮,因为 x 坐标取决于参数 j 而 y 取决于参数 i。

What's height and width doing in there? I thought there were only x and y.

按钮需要尺寸,而不仅仅是位置。由于 b 在 for 循环中被创建为局部变量,因此它的所有属性都在此处设置。稍后可能会更改。

How does the button know when you tap on it? Specifically what are the lines of code and how does it work?

您的主循环将在每个循环中调用 draw.doevents()。所以在某些时候会有一个事件导致 touchBegan 被调用。 按钮本身并不知道它被点击了。函数 touchBegan(x,y) 将通过调用 lookupButton(x,y) 并调用其操作函数来搜索是否在 x,y 处点击了其中一个按钮。 action 是 dotAction、digitAction 或 clearAction 之一。因此,如果您点击数字按钮,将调用 digitAction(),例如

帮自己一个忙,读一本关于 Lua 的书,或者至少做一个 Lua 教程。在深入研究第三方库之前。如果您不知道如何索引最常见的 Lua 类型或 + 和 * 是算术运算符,您将很难理解代码,并且您将无法以任何方式提高工作效率。