Advertisement
beezing

Touch input demo - Coders

Nov 3rd, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.92 KB | None | 0 0
  1. -- Touch demo
  2. -- by @beezing
  3.  
  4. sin = math.sin
  5. cos = math.cos
  6. random = math.random
  7. drawText = writeTextAtPosition
  8.  
  9. x0 = -1
  10. y0 = -1
  11.  
  12. function setRandomColors(val)
  13.   setColor(random(val), random(val), random(val))
  14. end
  15.  
  16. function rectangle(x, y, width, height)
  17.   move(x, y)
  18.   draw(x+width, y)
  19.   draw(x+width, y+height)
  20.   draw(x, y+height)
  21.   draw(x, y)
  22. end
  23.  
  24. function circle(x, y, r)
  25.   move(x+r, y)
  26.   for i = 0, 3.14*2+0.1, 0.1 do
  27.     draw(x+r*cos(i), y+r*sin(i))
  28.   end
  29. end
  30.  
  31. function follow(x,y)
  32.   setLineWidth(2)
  33.   setColor(255,255,255)
  34.   circle(x0,y0,29)
  35.   circle(x0,y0,30)
  36.   circle(x0,y0,31)
  37.   setLineWidth(1)
  38.   setColor(0,0,0)
  39.   circle(x,y,30)
  40. end
  41.  
  42. function doodle(x,y)
  43.   --drawPoint(x,y)
  44.   draw(x,y)
  45. end
  46.  
  47. function dot(x,y)
  48.   setRandomColors(192)
  49.   setLineWidth(25)
  50.   circle(x,y,1)
  51.   setLineWidth(random(3))
  52. end
  53.  
  54. function drawButton(x,y,w,h,t)
  55.   setLineWidth(2)
  56.   rectangle(x,y,w,h)
  57.   drawText(t,x+28,y+24)
  58.   drawText(t,x+29,y+24)
  59. end
  60.  
  61. function setupCanvas()
  62.   clearCanvas()
  63.   setColor(0,0,0)
  64.   drawButton(100,110,124,30,'C L E A R')
  65.   drawText('Touch and doodle...', 100,100)
  66.   move(384,384)
  67.   setRandomColors(192)
  68. end
  69.  
  70. function touched(x, y)
  71.   return (x > 0) and (y > 0)
  72. end
  73.  
  74. function touch()
  75.   x,y = currentTouch()
  76.   if touched(x,y) then
  77.     --draw(x,y)
  78.     --move(x,y)
  79.  
  80.     if (x > 100) and (x < 224) and
  81.        (y > 110) and (y < 140) then
  82.       setColor(192,0,0)
  83.       drawButton(100,110,124,30,'C L E A R')
  84.     else
  85.       doodle(x,y)
  86.       --follow(x,y)
  87.     end
  88.  
  89.     x0 = x
  90.     y0 = y
  91.   end
  92. end
  93.  
  94. function tap()
  95.   x,y = lastTouch()
  96.   if touched(x,y) then
  97.     --draw(x,y)
  98.     move(x,y)
  99.  
  100.     if (x > 100) and (x < 224) and
  101.        (y > 110) and (y < 140) then
  102.       setupCanvas()
  103.     else
  104.       dot(x,y)
  105.     end
  106.  
  107.     x0 = x
  108.     y0 = y
  109.   end
  110. end
  111.  
  112. setupCanvas()
  113. while true do
  114.   disableScreenRefresh()
  115.   touch()
  116.   tap()
  117.   enableScreenRefresh()
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement