joelwe

Untitled

Apr 12th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.00 KB | None | 0 0
  1. local widget = require( "widget" )
  2. local storyboard = require( "storyboard" )
  3. local scene = storyboard.newScene()
  4. -- Our scene
  5. function scene:createScene( event )
  6.     local group = self.view
  7.  
  8.     local date = os.date( "*t" )
  9.     local curDay = date.day
  10.     local curMonth = date.month
  11.     local curYear = date.year
  12.  
  13.     -- Creates a background
  14.     local function create_bg()
  15.         local background = display.newImage( "assets/background.png", true )
  16.         group:insert( background )
  17.     end
  18.  
  19.     --Returns name of month[i]
  20.     local get_month_name =
  21.     {
  22.         "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  23.     }
  24.    
  25.     --Returns short name of month[i]
  26.     local get_short_month_name =
  27.     {
  28.         "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"
  29.     }
  30.  
  31.     --Returns days in a month, including for leap year
  32.     local function get_days_in_month(month, year)
  33.         local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }  
  34.         local d = days_in_month[month]
  35.        
  36.         -- check for leap year
  37.         if (month == 2) then
  38.             if year%4==0 and (year%100~=0 or year%400==0) then
  39.                 d = 29
  40.             end
  41.         end
  42.        
  43.         return d  
  44.     end
  45.    
  46.     --Returns day of week.
  47.     local get_day_of_week =
  48.     {
  49.         "sun", "mon", "tue", "wed", "thu", "fri", "sat"
  50.     }
  51.  
  52.     -- Creates the calendar
  53.     local function create_calender( year, month, startDay, endDay )
  54.  
  55.         --Creates a "calender box" which shows selected month, year and the weekdays.
  56.         local calBox = display.newRect( 0, 32, display.contentWidth, 37 )
  57.         calBox:setFillColor( 0, 0, 0 )
  58.         calBox.alpha = 0.4
  59.         group:insert( calBox )
  60.  
  61.         --Selected month name
  62.         local monthName = display.newText( get_month_name[month], 0, 0, 0, 0, native.systemFont, 20 )
  63.         monthName:setTextColor( 255, 255, 255 )
  64.         monthName:setReferencePoint(display.CenterLeftReferencePoint)
  65.         monthName.x = 5
  66.         monthName.y = calBox.y - 5
  67.         group:insert( monthName )
  68.  
  69.         --Selected year name
  70.         local yearName = display.newText( year, 0, 0, 0, 0, native.systemFont, 20 )
  71.         yearName:setTextColor( 255, 255, 255 )
  72.         yearName:setReferencePoint(display.CenterLeftReferencePoint)
  73.         yearName.x = display.contentWidth - yearName.contentWidth
  74.         yearName.y = calBox.y - 5
  75.         group:insert( yearName )
  76.  
  77.         local x = 0
  78.         local y = calBox.y + calBox.contentHeight*0.5
  79.         local startNr = startDay + 1 --The starting day for the month. Adds 1 because a table index starts at 1.
  80.         local endNr = endDay
  81.         local goMonth = false
  82.         local dayNr = 1
  83.        
  84.         --print(endDay)
  85.  
  86.         --Create a 5*7 grid.
  87.         for j = 1, 5 do --5 rows
  88.             for i = 1, 7 do --7 columns
  89.                
  90.                 if i == startNr then
  91.                     goMonth = true
  92.                 end
  93.  
  94.                 --Creates a box for each day.
  95.                 local dayBox = display.newRect( x, y, display.contentWidth/7, 50 )
  96.                 dayBox:setFillColor( 255, 255, 255)
  97.                 dayBox.alpha = 0.4
  98.                 group:insert( dayBox )
  99.  
  100.                 --Check where to start the selected month.
  101.                 if goMonth then
  102.                     local curDayText
  103.                     --If it's the first day of the month, show the name of the month.
  104.                     if dayNr == 1 then
  105.                         curDayText = dayNr .. " " .. get_short_month_name[month]
  106.                     else
  107.                         curDayText = dayNr
  108.                     end
  109.                    
  110.                     --A text representing each day nr.
  111.                     local dayText = display.newText( curDayText, 0, 0, 0, 0, native.systemFont, 10 )
  112.                     dayText:setTextColor( 0, 0, 0 )
  113.                     dayText:setReferencePoint(display.CenterLeftReferencePoint)
  114.                     dayText.x = x + 3
  115.                     dayText.y = y + 7
  116.  
  117.                     --If it's the end of the month, stop showing day numbers.
  118.                     if dayNr == endNr then
  119.                         goMonth = false
  120.                     else
  121.                         dayNr = dayNr + 1
  122.                     end
  123.                     group:insert( dayText )
  124.                 end
  125.  
  126.                 --Print out the day names on the calBox.
  127.                 if j == 1 then
  128.                     local dayText = display.newText( get_day_of_week[i], 0, 0, 44, 0, native.systemFont, 14 )
  129.                     dayText.x = dayBox.x + dayBox.contentWidth * 0.25
  130.                     dayText.y = calBox.y + dayText.contentHeight - 3
  131.                     group:insert( dayText )
  132.                 end
  133.                
  134.                 --Create horizontal frames
  135.                 if j ~= 1 then
  136.                     local frameBottom = display.newRect( x, y, dayBox.contentWidth+1, 1 )
  137.                     frameBottom:setFillColor( 0, 0, 0 )
  138.                     frameBottom.alpha = 1
  139.                     group:insert( frameBottom )
  140.                 end
  141.                
  142.                 --Create vertical frames
  143.                 if i ~= 1 then
  144.                     local frameRight = display.newRect( x, y, 1, dayBox.contentHeight )
  145.                     frameRight:setFillColor( 0, 0, 0 )
  146.                     frameRight.alpha = 1
  147.                     group:insert( frameRight )
  148.                 end
  149.  
  150.                 x = x + display.contentWidth/7
  151.  
  152.             end
  153.  
  154.             y = y + 50
  155.             x = 0
  156.         end
  157.     end
  158.  
  159.     local prevMonth
  160.     local nextMonth
  161.  
  162.     --Creates buttons for going back and forth in months.
  163.     local function create_buttons()
  164.  
  165.         local prevButton = widget.newButton
  166.         {
  167.             left = 15,
  168.             top = 320,
  169.             width = 80,
  170.             height = 40,
  171.             label = "<<",
  172.             onRelease = jump_to_prev_month,
  173.         }
  174.         prevButton.xScale = 0.5
  175.         prevButton.yScale = 0.5
  176.  
  177.         group:insert( prevButton )
  178.  
  179.         local nextButton = widget.newButton
  180.         {
  181.             left = display.contentWidth - 100,
  182.             top = 320,
  183.             width = 80,
  184.             height = 40,
  185.             label = ">>",
  186.             onRelease = jump_to_next_month,
  187.         }
  188.         nextButton.xScale = 0.5
  189.         nextButton.yScale = 0.5
  190.  
  191.         group:insert( nextButton )
  192.     end
  193.    
  194.     --Remove objects
  195.     local function remove_objects()
  196.         for i=group.numChildren,1,-1 do
  197.             local child = group[i]
  198.             child.parent:remove( child )
  199.             child = nil
  200.         end
  201.     end
  202.    
  203.     --Get the first day of the month
  204.     local function get_start_day( cur_month, cur_year )
  205.         local test = os.time{year = cur_year, month=cur_month, day=1}
  206.         return tonumber(os.date("%w", test))
  207.     end
  208.    
  209.     --Get the last day of the month
  210.     local function get_end_day( cur_month, cur_year )
  211.         return tonumber(get_days_in_month(cur_month, curYear))
  212.     end
  213.  
  214.     --Go to the previous month.
  215.     function jump_to_prev_month()
  216.         --Remove all objects
  217.         remove_objects()
  218.        
  219.         --Set the current month back to the previous month.
  220.         curMonth = curMonth - 1
  221.        
  222.         --If the month is before January, go back to previous year.
  223.         if curMonth < 1 then
  224.             curMonth = 12
  225.             curYear = curYear - 1
  226.         end
  227.        
  228.         local startDayofMonth = get_start_day( curMonth, curYear )
  229.         local endDayofMonth = get_end_day( curMonth, curYear )
  230.  
  231.         --Create background, buttons and calender.
  232.         create_bg()
  233.         create_buttons()
  234.         create_calender( curYear, curMonth, startDayofMonth, endDayofMonth )
  235.  
  236.     end
  237.  
  238.     --Go to the next month.
  239.     function jump_to_next_month()
  240.         --Remove all objects
  241.         remove_objects()
  242.  
  243.         --Set the current month back to the next month.
  244.         curMonth = curMonth + 1
  245.         --If the month is after December, go to the next year.
  246.         if curMonth > 12 then
  247.             curMonth = 1
  248.             curYear = curYear + 1
  249.         end
  250.        
  251.         local startDayofMonth = get_start_day( curMonth, curYear )
  252.         local endDayofMonth = get_end_day( curMonth, curYear )
  253.  
  254.         --Create background, buttons and calender.
  255.         create_bg()
  256.         create_buttons()
  257.         create_calender( curYear, curMonth, startDayofMonth, endDayofMonth )
  258.     end
  259.    
  260.     --Create main
  261.     local function main()
  262.         local startDayofMonth = get_start_day( curMonth, curYear )
  263.         local endDayofMonth = get_end_day( curMonth, curYear )
  264.        
  265.         --Create background, buttons and calender.
  266.         create_bg()
  267.         create_buttons()
  268.         create_calender( curYear, curMonth, startDayofMonth, endDayofMonth )
  269.     end
  270.     main()
  271.  
  272. end
  273.  
  274. scene:addEventListener( "createScene" )
  275.  
  276. return scene
Advertisement
Add Comment
Please, Sign In to add comment