Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local widget = require( "widget" )
- local storyboard = require( "storyboard" )
- local scene = storyboard.newScene()
- -- Our scene
- function scene:createScene( event )
- local group = self.view
- local date = os.date( "*t" )
- local curDay = date.day
- local curMonth = date.month
- local curYear = date.year
- -- Creates a background
- local function create_bg()
- local background = display.newImage( "assets/background.png", true )
- group:insert( background )
- end
- --Returns name of month[i]
- local get_month_name =
- {
- "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
- }
- --Returns short name of month[i]
- local get_short_month_name =
- {
- "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"
- }
- --Returns days in a month, including for leap year
- local function get_days_in_month(month, year)
- local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
- local d = days_in_month[month]
- -- check for leap year
- if (month == 2) then
- if year%4==0 and (year%100~=0 or year%400==0) then
- d = 29
- end
- end
- return d
- end
- --Returns day of week.
- local get_day_of_week =
- {
- "sun", "mon", "tue", "wed", "thu", "fri", "sat"
- }
- -- Creates the calendar
- local function create_calender( year, month, startDay, endDay )
- --Creates a "calender box" which shows selected month, year and the weekdays.
- local calBox = display.newRect( 0, 32, display.contentWidth, 37 )
- calBox:setFillColor( 0, 0, 0 )
- calBox.alpha = 0.4
- group:insert( calBox )
- --Selected month name
- local monthName = display.newText( get_month_name[month], 0, 0, 0, 0, native.systemFont, 20 )
- monthName:setTextColor( 255, 255, 255 )
- monthName:setReferencePoint(display.CenterLeftReferencePoint)
- monthName.x = 5
- monthName.y = calBox.y - 5
- group:insert( monthName )
- --Selected year name
- local yearName = display.newText( year, 0, 0, 0, 0, native.systemFont, 20 )
- yearName:setTextColor( 255, 255, 255 )
- yearName:setReferencePoint(display.CenterLeftReferencePoint)
- yearName.x = display.contentWidth - yearName.contentWidth
- yearName.y = calBox.y - 5
- group:insert( yearName )
- local x = 0
- local y = calBox.y + calBox.contentHeight*0.5
- local startNr = startDay + 1 --The starting day for the month. Adds 1 because a table index starts at 1.
- local endNr = endDay
- local goMonth = false
- local dayNr = 1
- --print(endDay)
- --Create a 5*7 grid.
- for j = 1, 5 do --5 rows
- for i = 1, 7 do --7 columns
- if i == startNr then
- goMonth = true
- end
- --Creates a box for each day.
- local dayBox = display.newRect( x, y, display.contentWidth/7, 50 )
- dayBox:setFillColor( 255, 255, 255)
- dayBox.alpha = 0.4
- group:insert( dayBox )
- --Check where to start the selected month.
- if goMonth then
- local curDayText
- --If it's the first day of the month, show the name of the month.
- if dayNr == 1 then
- curDayText = dayNr .. " " .. get_short_month_name[month]
- else
- curDayText = dayNr
- end
- --A text representing each day nr.
- local dayText = display.newText( curDayText, 0, 0, 0, 0, native.systemFont, 10 )
- dayText:setTextColor( 0, 0, 0 )
- dayText:setReferencePoint(display.CenterLeftReferencePoint)
- dayText.x = x + 3
- dayText.y = y + 7
- --If it's the end of the month, stop showing day numbers.
- if dayNr == endNr then
- goMonth = false
- else
- dayNr = dayNr + 1
- end
- group:insert( dayText )
- end
- --Print out the day names on the calBox.
- if j == 1 then
- local dayText = display.newText( get_day_of_week[i], 0, 0, 44, 0, native.systemFont, 14 )
- dayText.x = dayBox.x + dayBox.contentWidth * 0.25
- dayText.y = calBox.y + dayText.contentHeight - 3
- group:insert( dayText )
- end
- --Create horizontal frames
- if j ~= 1 then
- local frameBottom = display.newRect( x, y, dayBox.contentWidth+1, 1 )
- frameBottom:setFillColor( 0, 0, 0 )
- frameBottom.alpha = 1
- group:insert( frameBottom )
- end
- --Create vertical frames
- if i ~= 1 then
- local frameRight = display.newRect( x, y, 1, dayBox.contentHeight )
- frameRight:setFillColor( 0, 0, 0 )
- frameRight.alpha = 1
- group:insert( frameRight )
- end
- x = x + display.contentWidth/7
- end
- y = y + 50
- x = 0
- end
- end
- local prevMonth
- local nextMonth
- --Creates buttons for going back and forth in months.
- local function create_buttons()
- local prevButton = widget.newButton
- {
- left = 15,
- top = 320,
- width = 80,
- height = 40,
- label = "<<",
- onRelease = jump_to_prev_month,
- }
- prevButton.xScale = 0.5
- prevButton.yScale = 0.5
- group:insert( prevButton )
- local nextButton = widget.newButton
- {
- left = display.contentWidth - 100,
- top = 320,
- width = 80,
- height = 40,
- label = ">>",
- onRelease = jump_to_next_month,
- }
- nextButton.xScale = 0.5
- nextButton.yScale = 0.5
- group:insert( nextButton )
- end
- --Remove objects
- local function remove_objects()
- for i=group.numChildren,1,-1 do
- local child = group[i]
- child.parent:remove( child )
- child = nil
- end
- end
- --Get the first day of the month
- local function get_start_day( cur_month, cur_year )
- local test = os.time{year = cur_year, month=cur_month, day=1}
- return tonumber(os.date("%w", test))
- end
- --Get the last day of the month
- local function get_end_day( cur_month, cur_year )
- return tonumber(get_days_in_month(cur_month, curYear))
- end
- --Go to the previous month.
- function jump_to_prev_month()
- --Remove all objects
- remove_objects()
- --Set the current month back to the previous month.
- curMonth = curMonth - 1
- --If the month is before January, go back to previous year.
- if curMonth < 1 then
- curMonth = 12
- curYear = curYear - 1
- end
- local startDayofMonth = get_start_day( curMonth, curYear )
- local endDayofMonth = get_end_day( curMonth, curYear )
- --Create background, buttons and calender.
- create_bg()
- create_buttons()
- create_calender( curYear, curMonth, startDayofMonth, endDayofMonth )
- end
- --Go to the next month.
- function jump_to_next_month()
- --Remove all objects
- remove_objects()
- --Set the current month back to the next month.
- curMonth = curMonth + 1
- --If the month is after December, go to the next year.
- if curMonth > 12 then
- curMonth = 1
- curYear = curYear + 1
- end
- local startDayofMonth = get_start_day( curMonth, curYear )
- local endDayofMonth = get_end_day( curMonth, curYear )
- --Create background, buttons and calender.
- create_bg()
- create_buttons()
- create_calender( curYear, curMonth, startDayofMonth, endDayofMonth )
- end
- --Create main
- local function main()
- local startDayofMonth = get_start_day( curMonth, curYear )
- local endDayofMonth = get_end_day( curMonth, curYear )
- --Create background, buttons and calender.
- create_bg()
- create_buttons()
- create_calender( curYear, curMonth, startDayofMonth, endDayofMonth )
- end
- main()
- end
- scene:addEventListener( "createScene" )
- return scene
Advertisement
Add Comment
Please, Sign In to add comment