Advertisement
beezing

Domino card class - update #1 - Codea

Dec 10th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.60 KB | None | 0 0
  1. -- Domino card class - update #1
  2. -- by: @beezing
  3.  
  4. Domino = class()
  5.  
  6. -- default values
  7. local cardWidth      = 100
  8. local cardHeight     = cardWidth*2
  9. local dotSize        = cardWidth/4
  10. local separatorWidth = cardWidth/20+1
  11. local defDotColor    = color(176,   0,   0, 255) -- red
  12. local defBackColor   = color(255, 255, 176, 255) -- yellow
  13. local defStripeCol1  = color(  0,  32, 223, 255) -- blue
  14. local defStripeCol2  = color(223,  32,   0, 255) -- red
  15.  
  16. -- create a domino card with the given numbers
  17. function Domino:init(top, bottom)
  18.     -- limit numbers
  19.     self.top       = math.min(math.max(top, 0), 6)
  20.     self.bottom    = math.min(math.max(bottom, 0), 6)
  21.     -- geometry
  22.     self.position  = vec2(0, 0)
  23.     self.scale     = 1.0
  24.     self.angle     = 0
  25.     -- status
  26.     self.closed    = false
  27.     self.hold      = false
  28.     -- colors
  29.     self.dotColor  = defDotColor
  30.     self.backColor = defBackColor
  31. end
  32.  
  33. -- draw domino dots of number
  34. function Domino:drawDots(number)
  35.     -- offset from center
  36.     ofs = dotSize * self.scale
  37.     -- adjust dot size
  38.     local size = 0
  39.     if number == 1 then
  40.         size = dotSize * self.scale * 2.0 -- dot size is twice bigger for 1
  41.     elseif number == 6 then 
  42.         size = dotSize * self.scale * 0.9 -- dot size is 10% smaller for 6
  43.     else
  44.         size = dotSize * self.scale
  45.     end
  46.     -- draw dots
  47.     if number == 1 then
  48.         ellipse(0, 0, size)
  49.     elseif number == 2 then
  50.         ellipse(-ofs,-ofs, size)
  51.         ellipse( ofs, ofs, size)
  52.     elseif number == 3 then
  53.         ellipse(-ofs,-ofs, size)
  54.         ellipse(   0,   0, size)
  55.         ellipse( ofs, ofs, size)
  56.     elseif number == 4 then
  57.         ellipse(-ofs,-ofs, size)
  58.         ellipse(-ofs, ofs, size)
  59.         ellipse( ofs,-ofs, size)
  60.         ellipse( ofs, ofs, size)
  61.     elseif number == 5 then
  62.         ellipse(   0,   0, size)
  63.         ellipse(-ofs,-ofs, size)
  64.         ellipse(-ofs, ofs, size)
  65.         ellipse( ofs,-ofs, size)
  66.         ellipse( ofs, ofs, size)
  67.     elseif number == 6 then
  68.         ellipse(-ofs,   0, size)
  69.         ellipse( ofs,   0, size)
  70.         ellipse(-ofs,-ofs, size)
  71.         ellipse(-ofs, ofs, size)
  72.         ellipse( ofs,-ofs, size)
  73.         ellipse( ofs, ofs, size)
  74.     end
  75. end
  76.  
  77. -- draw stripes for the backside
  78. function Domino:drawStripes()
  79.     -- realign coordinate origin
  80.     translate(-cardWidth/2 * self.scale, -cardHeight/2 * self.scale)
  81.     -- set drawing properties
  82.     rectMode(CORNER)
  83.     lineCapMode(SQUARE)
  84.     -- compute stripe properties
  85.     local div = 10 -- number of stripes
  86.     local bar = cardWidth * self.scale / div -- width of split bar
  87.     local mid = bar/2 -- center of split bar
  88.     local wid = bar-1 -- width of stripe
  89.     -- draw stripes
  90.     strokeWidth(wid)
  91.     for i = 1, 2*div do
  92.         -- draw short stripes
  93.         stroke(defStripeCol1)
  94.         if i % 2 == 0 then
  95.             line((i/2-1)*bar+mid, 0, (i/2-1)*bar+mid, cardHeight * self.scale) 
  96.         end
  97.         -- draw long stripes
  98.         stroke(defStripeCol2)
  99.         line(0, (i-1)*bar+mid, cardWidth * self.scale, (i-1)*bar+mid)
  100.     end
  101. end
  102.  
  103. -- draw opened card
  104. function Domino:drawOpened()
  105.     -- save matrix
  106.     pushStyle()  
  107.     pushMatrix()
  108.     -- transform geometry
  109.     translate(self.position.x, self.position.y)
  110.     rotate(self.angle)        
  111.     -- draw background
  112.     rectMode(CENTER)
  113.     fill(self.backColor)
  114.     rect(0, 0, cardWidth * self.scale, cardHeight * self.scale)
  115.     -- draw separator line
  116.     local l = 0.8 -- line length percentage
  117.     lineCapMode(PROJECT)
  118.     strokeWidth(separatorWidth * self.scale)
  119.     stroke(self.dotColor)
  120.     line(-l/2 * cardWidth * self.scale, 0, l/2 * cardWidth * self.scale, 0)        
  121.     -- draw dots
  122.     noStroke()
  123.     fill(self.dotColor)
  124.     local c = cardWidth/2 -- dot center
  125.     -- draw top number
  126.     pushMatrix()
  127.     translate(0, c * self.scale)
  128.     self:drawDots(self.top)
  129.     popMatrix()
  130.     -- draw bottom number
  131.     pushMatrix()
  132.     translate(0, -c * self.scale)
  133.     self:drawDots(self.bottom)
  134.     popMatrix()
  135.     -- restore matrix
  136.     popMatrix()
  137.     popStyle()
  138. end
  139.  
  140. -- draw closed card
  141. function Domino:drawClosed()
  142.     -- save matrix
  143.     pushStyle()
  144.     pushMatrix()
  145.     -- transform geometry
  146.     translate(self.position.x, self.position.y)
  147.     rotate(self.angle)
  148.     -- draw background
  149.     fill(255, 255, 255, 255)
  150.     rectMode(CENTER)
  151.     rect(0, 0, cardWidth * self.scale, cardHeight * self.scale)
  152.     -- draw backside ornament (only stripes for now)
  153.     self:drawStripes()
  154.     -- restore geometry
  155.     popMatrix()
  156.     popStyle()
  157. end
  158.  
  159. -- handle card touch
  160. function Domino:touched()
  161.     local w = cardWidth * self.scale / 2
  162.     local h = cardHeight * self.scale / 2
  163.     -- rotate touch position
  164.     local t = vec2(CurrentTouch.x, CurrentTouch.y)
  165.     --t = t:rotate(math.rad(self.angle))
  166.     -- detect touch
  167.     if CurrentTouch.state == BEGAN then
  168.         if t.x >= self.position.x-w and
  169.            t.x <= self.position.x-w + w*2 and
  170.            t.y >= self.position.y-h and
  171.            t.y <= self.position.y-h + h*2 and
  172.            self.hold == false then
  173.             self.hold = true
  174.         end
  175.     end
  176.     -- detect drag
  177.     if CurrentTouch.state == MOVING then
  178.         if t.x >= self.position.x-w and
  179.            t.x <= self.position.x-w + w*2 and
  180.            t.y >= self.position.y-h and
  181.            t.y <= self.position.y-h + h*2 and
  182.            self.hold == true then
  183.             self.position = vec2(t.x, t.y)
  184.         end
  185.     end
  186.     -- detect end of touch
  187.     if CurrentTouch.state == ENDED then
  188.         if t.x >= self.position.x-w and
  189.            t.x <= self.position.x-w + w*2 and
  190.            t.y >= self.position.y-h and
  191.            t.y <= self.position.y-h + h*2 and
  192.            self.hold == true then
  193.             self.hold = false
  194.         end
  195.     end
  196. end
  197.  
  198. -- draw card
  199. function Domino:draw()
  200.     if self.closed == true then
  201.         self:drawClosed()
  202.     else
  203.         self:drawOpened()
  204.     end
  205. end
  206.  
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement