Author Topic: Creating a calendar  (Read 5064 times)

Tripwire

  • Full Member
  • ***
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 163
    • View Profile
Creating a calendar
« on: October 16, 2013, 02:15:35 AM »
Hi,

I'm trying to create a calendar with NGUI. There seems to be no one who have tried this, so here goes. I asked on the Unity3D form aswell, and this was one of the answers:
  1. class TestCalendar {
  2.  
  3.     @MenuItem("Test/Calendar")
  4.  
  5.     static function Test() {
  6.  
  7.         var now = System.DateTime.Now;
  8.  
  9.        
  10.  
  11.         // build ngui
  12.  
  13.         var Refresh = function() {
  14.  
  15.             Debug.Log("Refreshing");
  16.  
  17.        
  18.  
  19.             // NGUI clear the panel with days buttons
  20.  
  21.            
  22.  
  23.             // get first day of month
  24.  
  25.             var i = now.AddDays(-(now.Day - 1));
  26.  
  27.            
  28.  
  29.             // interate in days until change the month
  30.  
  31.             while (i.Month == now.Month) {
  32.  
  33.                 Debug.Log(i);
  34.  
  35.                 i = i.AddDays(1);
  36.  
  37.                
  38.  
  39.                 // NGUI add a new button for this day
  40.  
  41.             }
  42.  
  43.            
  44.  
  45.             // NGUI reposition all buttons with UIGrid
  46.  
  47.         };
  48.  
  49.        
  50.  
  51.         var OnClickNext = function() {
  52.  
  53.             Debug.Log("On user click next month");
  54.  
  55.             now = now.AddMonths(1);
  56.  
  57.             Refresh();
  58.  
  59.         };
  60.  
  61.        
  62.  
  63.         Refresh();
  64.  
  65.        
  66.  
  67.         // simulate someone clicking in next
  68.  
  69.         OnClickNext();
  70.  
  71.     }
  72.  
  73. }
  74.  

I'm trying as I go here so i'll post updates. But does anybody else have experience, ideas or code they can share? Would be nice to make this public for everyone to create calendars with NGUI.

Tripwire

  • Full Member
  • ***
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 163
    • View Profile
Re: Creating a calendar
« Reply #1 on: October 16, 2013, 07:43:13 AM »
Right, so i've got it almost working now. Here's the script now have:
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Globalization;
  5.  
  6. public class CalendarPopup : MonoBehaviour {
  7.        
  8.         public UILabel[] DayLabels;                     //Holds 35 labels
  9.         public string[] Months;                         //Holds the months
  10.         public UILabel HeaderLabel;                     //The label used to show the Month
  11.  
  12.         private int monthCounter = DateTime.Now.Month - 1;     
  13.         private int yearCounter = 0;
  14.        
  15.         private DateTime iMonth;
  16.         private DateTime curDisplay;
  17.        
  18.         void Start()
  19.         {
  20.                 CreateMonths();                                
  21.                 CreateCalendar();
  22.         }
  23.        
  24.         /*Adds al the months to the Months Array and sets the current month
  25.         in the header label*/
  26.         void CreateMonths()
  27.         {
  28.                 Months = new string[12];
  29.                 iMonth = new DateTime(2000, 1, 1);
  30.                
  31.                 for(int i = 0; i < 12; ++i)
  32.                 {
  33.                         iMonth = new DateTime(2009, i+1, 1);
  34.                         Months[i] = iMonth.ToString("MMMM");
  35.                 }
  36.                
  37.                 HeaderLabel.text = Months[DateTime.Now.Month - 1] + " " + DateTime.Now.Year;
  38.         }
  39.        
  40.         /*Sets the days to their correct labels*/
  41.         void CreateCalendar()
  42.         {
  43.                 curDisplay = iMonth;
  44.                
  45.                 while(curDisplay.Month == iMonth.Month)
  46.                 {
  47.                         DayLabels[curDisplay.Day - 1].text = curDisplay.Day.ToString();
  48.                         curDisplay = curDisplay.AddDays(1);
  49.                 }
  50.         }
  51.        
  52.         /*when right arrow clicked go to next month */
  53.         public void nextMonth()
  54.         {
  55.                 monthCounter++;
  56.                 if(monthCounter > 11)
  57.                 {
  58.                         monthCounter = 0;
  59.                         yearCounter++;
  60.                 }
  61.                
  62.                 HeaderLabel.text = Months[monthCounter] + " " + (DateTime.Now.Year + yearCounter);
  63.                 clearLabels();
  64.                 iMonth = iMonth.AddMonths(1);
  65.                 CreateCalendar();
  66.         }
  67.        
  68.         /*when left arrow clicked go to previous month */
  69.         public void previousMonth()
  70.         {
  71.                 monthCounter--;
  72.                 if(monthCounter < 0)
  73.                 {
  74.                         monthCounter = 11;
  75.                         yearCounter--;
  76.                 }
  77.                
  78.                 HeaderLabel.text = Months[monthCounter]  + " " + (DateTime.Now.Year+ yearCounter);
  79.                 clearLabels();
  80.                 iMonth = iMonth.AddMonths(-1);
  81.                 CreateCalendar();
  82.         }
  83.        
  84.         /*clears all the day labels*/
  85.         void clearLabels()
  86.         {
  87.                 for(int x = 0; x < DayLabels.Length; x++)
  88.                 {
  89.                         DayLabels[x].text = null;
  90.                 }
  91.         }
  92. }
  93.  

The only thing which isn't working right is the DayLabels aren't being set to the correct positions. For instance 1 October 2013 starts on Tuesday which should be position 1 in the DayLabels array. But it starts at Array position 0.

Anyone have any idea how to fix the last problem?

Tripwire

  • Full Member
  • ***
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 163
    • View Profile
Re: Creating a calendar
« Reply #2 on: October 18, 2013, 03:16:10 AM »
anyone?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Creating a calendar
« Reply #3 on: October 18, 2013, 05:54:29 PM »
I don't think I've seen of anyone doing a calendar before, so I'm not sure if anyone would have a suggestion here.