Advertisement
beezing

Radial calender - Codea

Jul 30th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. sin = math.sin
  2. cos = math.cos
  3. rad = math.rad
  4.  
  5. WEEKDAYS = {"MON","TUE","WED","THU","FRI","SAT","SUN"}
  6. MONTHS   = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"}
  7.  
  8. function arc(x, y, a, b, r, t)
  9.   ellipseMode(CENTER)
  10.   noStroke()
  11.   -- arc using ellipse
  12.   r = (r-t)/2 - 1
  13.   local s = dial
  14.   for i = rad(a), rad(b), 1/s do
  15.     ellipse(x+r*cos(i), y+r*sin(i), t+1,t+1)
  16.   end
  17. end
  18.  
  19. function cal2deg(val, div)
  20.   return 90 - (360/div) * val
  21. end
  22.  
  23. function deg2cal(degree, r)
  24.   r  = r/2 - 1
  25.   mx = r * cos(rad(degree))
  26.   my = r * sin(rad(degree))
  27.   return mx, my
  28. end
  29.  
  30. function getLocalDateTime()
  31.   return os.date("*t")
  32. end
  33.  
  34. function setColor(r, g, b, a)
  35.   fill(r, g, b, a)
  36.   stroke(r, g, b, a)
  37. end
  38.  
  39. function sweep()
  40.   if last_sec == second then 
  41.     sweepStep = sweepStep + 1 
  42.   else 
  43.     maxStep   = sweepStep + 1
  44.     sweepStep = 0 
  45.     last_sec  = second
  46.   end
  47. end
  48.  
  49. function drawTick(div, r, t)
  50.   ellipseMode(CENTER)
  51.   setColor(0, 0, 0, 255)
  52.   -- tick marks
  53.   for i = 0, 359, 360/div do
  54.     local tx, ty = deg2cal(90-i, r)
  55.     ellipse(x+tx, y+ty, t)
  56.   end
  57. end
  58.  
  59. function drawDial()
  60.   ellipseMode(CENTER)
  61.   noFill()
  62.   strokeWidth(thick)
  63.   -- rainbow color
  64.   stroke(48, 0, 48, 255)
  65.   ellipse(x,y,inner+1*dial)
  66.   stroke(32, 0, 64, 255)
  67.   ellipse(x,y,inner+2*dial)
  68.   stroke(0, 0, 64, 255)
  69.   ellipse(x,y,inner+3*dial)
  70.   stroke(0, 64, 0, 255)
  71.   ellipse(x,y,inner+4*dial)
  72.   stroke(64, 64, 0, 255)
  73.   ellipse(x,y,inner+5*dial)
  74.   stroke(64, 32, 0, 255)
  75.   ellipse(x,y,inner+6*dial)
  76.   stroke(64, 0, 0, 255)
  77.   ellipse(x,y,inner+7*dial)
  78.   stroke(128, 128, 128, 255)
  79.   ellipse(x,y,thick)
  80.   strokeWidth(space)
  81.   stroke(128, 128, 128, 255)
  82.   ellipse(x,y,inner+7*dial+4*space)
  83.   -- tick mark
  84.   drawTick(  10,inner+1*dial-thick,thick/5)
  85.   drawTick(  12,inner+2*dial-thick,thick/5)
  86.   drawTick(days,inner+3*dial-thick,thick/5)
  87.   drawTick(   7,inner+4*dial-thick,thick/5)
  88.   drawTick(  12,inner+5*dial-thick,thick/5)
  89.   drawTick(  60,inner+6*dial-thick,thick/5)
  90.   drawTick(  60,inner+7*dial-thick,thick/5)
  91. end
  92.  
  93. function drawValue()
  94.   textMode(CORNER)
  95.   --font("DB LCD Temp")
  96.   font("Courier-Bold")
  97.   fontSize(thick/2)
  98.   local w,h = textSize("0")
  99.   fill(48, 0, 48, 255)
  100.   text(dt.sec,x,y+4*dial-thick/2+h/2-space)
  101.   fill(32, 0, 64, 255)
  102.   text(dt.min,x,y+3.5*dial-thick/2+h/2-space)
  103.   fill(0, 0, 64, 255)
  104.   text(dt.hour,x,y+3*dial-thick/2+h/2-space)
  105.   fill(0, 64, 0, 255)
  106.   text(WEEKDAYS[dt.wday],x,y+2.5*dial-thick/2+h/2-space)
  107.   fill(64, 64, 0, 255)
  108.   text(dt.day,x,y+2*dial-thick/2+h/2-space)
  109.   fill(64, 32, 0, 255)
  110.   text(MONTHS[dt.month],x,y+1.5*dial-thick/2+h/2-space)
  111.   fill(64, 0, 0, 255)
  112.   text(2000+dt.year,x,y+1*dial-thick/2+h/2-space)
  113. end
  114.  
  115. function setup()
  116.   print("RADIAL CALENDAR")
  117.   print("Started: "..os.date())
  118.  
  119.   x = WIDTH/2
  120.   y = HEIGHT/2
  121.   thick = 30
  122.   space = thick/10
  123.   inner = 2*(thick+space)+thick
  124.   dial  = 2*(thick+space)
  125.  
  126.   last_sec  = getLocalDateTime().sec
  127.   sweepStep = 0
  128.   maxStep   = 30
  129.  
  130.   --watch("sweepStep")
  131.   watch("maxStep")
  132.   --watch("second")
  133.   watch("days")
  134.   iparameter("thick",10,50,thick)
  135.   iparameter("space",1,10,space)
  136. end
  137.  
  138. function draw()
  139.     days = 31
  140.     background(0)
  141.     noSmooth()
  142.     drawDial()
  143.     
  144.     frame_rate = 30-DeltaTime*30
  145.     inner = 2*(thick+space)+thick
  146.     dial  = 2*(thick+space)
  147.     
  148.     -- read and adjust datetime
  149.     dt = getLocalDateTime()
  150.     second = dt.sec
  151.     hour = dt.hour
  152.     if hour >= 12 then hour = hour - 12 end
  153.     dt.wday = dt.wday - 1
  154.     if dt.wday == 0 then dt.wday = 7 end
  155.     if dt.month % 2 == 0 then days = 30 else days = 31 end
  156.     if dt.month == 2 then 
  157.       if dt.year % 4 == 0 then days = 29 else days = 28 end
  158.     end
  159.     if dt.year >= 2000 then dt.year = dt.year - 2000 end
  160.     
  161.     sweep()
  162.     noStroke()
  163.     strokeWidth(0)
  164.     -- time
  165.     setColor(255, 0, 0, 255)
  166.     arc(x,y,cal2deg(dt.sec+sweepStep/maxStep,60),90,inner+7*dial,thick)
  167.     --arc(x,y,cal2deg(dt.sec,60),                90,inner+7*dial,thick)
  168.     setColor(255, 128, 0, 255)
  169.     arc(x,y,cal2deg(dt.min + dt.sec/60,60),    90,inner+6*dial,thick)
  170.     setColor(255, 255, 0, 255) 
  171.     arc(x,y,cal2deg(hour + dt.min/60,12),      90,inner+5*dial,thick)
  172.     -- day
  173.     setColor(0, 255, 0, 255)
  174.     arc(x,y,cal2deg(dt.wday-1 + dt.hour/24,7),   90,inner+4*dial,thick)
  175.     -- date
  176.     setColor(0, 0, 255, 255)
  177.     arc(x,y,cal2deg(dt.day-1 + dt.hour/24,days), 90,inner+3*dial,thick)
  178.     setColor(128, 0, 255, 255)
  179.     arc(x,y,cal2deg(dt.month-1 + dt.day/days,12),90,inner+2*dial,thick)
  180.     setColor(128, 0, 128, 255)
  181.     arc(x,y,cal2deg(dt.year-1 + dt.month/12,100),90,inner+1*dial,thick)
  182.     
  183.     --drawValue()
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement