ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决random - Touch Lua's Draw Library Basics

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

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

PLEASE REFER TO THE BOTTOM PORTION OF THIS QUESTION TO SEE THE FULL PROGRAM. THE SNIPPETS I USE IN THE BEGINNING ARE PART OF THAT PROGRAM (NUMPAD.LUA)

Okay, so now for the questions:

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

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

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

•What's height and width doing in there? I thought there were only x and 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

Lastly, referencing the program as a whole... •How does the button know when you tap on it? Specifically what are the lines of code and how does it work? (I have a very faint understanding of track touches btw)

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()

Thank you so much for any help you can offer! I know this is a lot of questions, but this knowledge can really help propel me forward!

random lua draw
|
  this question
asked Apr 17 '16 at 2:12 Samuel F 15 6

 | 

2 Answers
2

解决方法

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

Since when do you have to purchase something to answer programming questions? All your questions are on absolute Lua basics anyway.

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

The dot operator is used to index table members. So b.x will give you the value that belongs to key "x" in table b. Its syntactic sugar for b["x"].

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

The function createButtons creates an empty table and fills it with buttons represented by a table b that stores various button properties.

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

Because the author of that program wants to add and multiply. Here the coordinates b.x and b.y are calculated. (j-1) is used because j starts at 1, but he needs it to start at 0 for this calculation. The 2 for loops will create 4 rows of buttons, each containing 3 buttons as the x coordinate depends on parameter j while y depends on parameter i.

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

The button needs dimensions, not only a location. As b is created as a local variable within the for loop all its properties are set here. They may be changed later.

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

Your mainloop will call draw.doevents() every cycle. So at some point there will be an event that will cause touchBegan to be called. The button itself does not know that it was tapped. function touchBegan(x,y) will search if one of the buttons was hit at x,y by calling lookupButton(x,y) and call its action function. action is either one of dotAction, digitAction or clearAction. So if you tap on a digit button digitAction() will be called e.g.

Do yourself a favour and read a book on Lua or at least do a Lua tutorial. Befor diving into third party libraries. If you don't know how to index the most common Lua type or that + and * are arrithmetic operators, you will have a very hard time understanding code and you will not be productive in any way.


|
  this answer
edited Apr 17 '16 at 10:18 answered Apr 17 '16 at 10:00 Piglet 3,315 1 7 16      I have never seen Touch Lua in my life. But I could answer all your questions, because I know Lua. I hope that motivates you a bit :) –  Piglet Apr 17 '16 at 10:25      Thank you so much! I may have more questions as I further analyze this, but this is great enough. As you mentioned in the last paragraph of your answer, "Lua Tutorial," where can I find this? I was only able to use Lua's website which to my knowledge didn't have a draw library tutorial (I'll look into this). All I had to reference was a list of commands and what they did in the app, which was not very helpful for me. –  Samuel F Apr 17 '16 at 12:13      you don't need a draw library tutorial, you need a Lua tutorial as you obviously have no idea of Lua. Once you know Lua well enough you will be able to read/understand code and effectively use libraries. Its like reading texts. If you don't know all the words you won't get the meaning. In your case you do not even know all characters. And if you can't read, you will have a hard time trying to write. –  Piglet Apr 17 '16 at 14:17      Where can I get this tutorial? –  Samuel F Apr 17 '16 at 14:19      lua.org/start.html checkout this page and the book Programming in Lua linked there. Also have a look at the official Lua reference. For tutorials simply google. Any will do. –  Piglet Apr 17 '16 at 14:27  |  show more comment

This program in particular uses tables to represent buttons, it does this to make it more manageable. The height and width are the same way. If you are new to the draw library, reviewing code like this is only going to confuse you. Most of your questions are actually about OOP honestly

Also this question probably isn't suitable for Stack Overflow right now. If you want help/a small tutorial on the subject feel free to PM me on the Touch Lua forum, my username is warspyking, but as it stands you need to either revise or delete this question.


|
  this answer
answered Apr 17 '16 at 2:42 warspyking 2,009 1 5 27      Respectable, I will delete once I have my reply. Perhaps I do need to hit the basics before I do the hard stuff. –  Samuel F Apr 17 '16 at 12:28

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。