/* * Created by JacobKay - 2022.08.24 */ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace ZTools { public class ZCalendarController { public int Year { set; get; } public int Month { set; get; } public int Day { set; get; } /// /// ��ǰ�Ƿ�������ѡ��״̬ /// private bool isInRange = false; public bool IsInRange { get { return isInRange; } } private string week; private DateTime now; private int days; /// /// ��ǰѡ�е�λ�� /// public Vector3 pos; private int lastMonthDays; private int nextMonthDays; public ZCalendar zCalendar; public ZCalendarModel zCalendarModel; public DateTime nowTime = DateTime.Today; private int lastMonthEmptyDays; bool isShow = true; public bool isInit = false; /// /// ����������ɫ /// public Color greyColor; public System.Globalization.ChineseLunisolarCalendar cncld = new System.Globalization.ChineseLunisolarCalendar(); /// /// ũ���� /// public string[] lunarMonths = { "��", "��", "��", "��", "��", "��", "��", "��", "��", "ʮ", "ʮһ", "��" }; public string[] lunarDaysT = { "��", "ʮ", "إ", "��" }; /// /// ũ���� /// public string[] lunarDays = { "һ", "��", "��", "��", "��", "��", "��", "��", "��", "ʮ" }; DateTime monthFirstDay; /// /// ��ʼ�� /// /// public void Init() { zCalendarModel.zCalendarController = this; zCalendarModel.Init(); if (zCalendarModel.isStaticCalendar) return; // ��̬�������ɹر� if (zCalendarModel.isPopupCalendar) { zCalendarModel.btnClose.onClick.AddListener(() => { Hide(); }); } zCalendarModel.btnLastYear.onClick.AddListener(LastYear); zCalendarModel.btnNextYear.onClick.AddListener(NextYear); zCalendarModel.btnLastMonth.onClick.AddListener(LastMonth); zCalendarModel.btnNextMonth.onClick.AddListener(NextMonth); Hide(); } /// /// ���չ涨ʱ���ʼ������ /// public void InitDate(DateTime date) { now = date; DestroyAllChildren(); UpdateYear(); UpdateMonth(); UpdateDays(); UpdateData(); if (!isInit) { isInit = true; // 安全检查:确保 zCalendar 不为空,防止空引用异常 if (zCalendar != null) { zCalendar.DateComplete(); } } } void LastYear() { now = now.AddYears(-1); DestroyAllChildren(); UpdateYear(); UpdateMonth(); UpdateDays(); UpdateData(); } void NextYear() { now = now.AddYears(1); DestroyAllChildren(); UpdateYear(); UpdateMonth(); UpdateDays(); UpdateData(); } void LastMonth() { now = now.AddMonths(-1); DestroyAllChildren(); UpdateYear(); UpdateMonth(); UpdateDays(); UpdateData(); } void NextMonth() { now = now.AddMonths(1); DestroyAllChildren(); UpdateYear(); UpdateMonth(); UpdateDays(); UpdateData(); } List dayItemList = new List(); /// /// ���������������ѡ��ʱ��ʱ����Ҫ�жϵ�ǰ����ѡ��״̬ /// /// public void ChangeRangeType(ZCalendarDayItem dayItem) { isInRange = !isInRange; if (dayItemList.Count >= 2) { dayItemList.Clear(); } if (dayItemList.Count == 0) { dayItemList.Add(dayItem); } else { int compare = DateTime.Compare(dayItemList[0].dateTime, dayItem.dateTime); if (compare <= 0) { dayItemList.Add(dayItem); } else { dayItemList.Insert(0, dayItem); } } if (!isInRange) { // 安全检查:确保列表中有至少2个元素才调用 RangeCalendar,防止数组越界异常 if (dayItemList.Count >= 2) { zCalendar.RangeCalendar(dayItemList[0], dayItemList[1]); } } } /// /// ��ʾ���� /// public void Show() { if (pos != null && !isShow) { isShow = true; //zCalendar.transform.localPosition = pos; if(zCalendarModel.writeTime!= null) { Vector3 mousePosition = Input.mousePosition + new Vector3(-960, -540, 0); //��mousePositionתΪzCalendarModel�������������� Debug.Log("�������" + (mousePosition)); zCalendar.transform.localPosition = mousePosition + new Vector3(0, -280, 0); } else { zCalendar.transform.localPosition = pos; } zCalendar.transform.localScale = Vector3.one; } } /// /// �������� /// public void Hide() { if (isShow && !isInRange) { isShow = false; //Debug.Log("��������"); zCalendar.transform.localPosition = new Vector3(pos.x, 5000, pos.z); zCalendar.transform.localScale = Vector3.zero; } } /// /// ��ѯ������ /// void UpdateYear() { Year = now.Year; } /// /// ��ѯ������ /// void UpdateMonth() { Month = int.Parse(now.Month.ToString("00")); } /// /// ����Ҫ��ѯ���� /// /// void UpdateDays() { days = DateTime.DaysInMonth(Year, Month); if (Day == 0) { Day = now.Day; } else if (Day > days) { Day = days; } } /// /// ������ʾ�·� /// void UpdateData() { zCalendarModel.SetTimeTxt(Year, Month); FillLastMonth(); for (int i = 0; i < days; i++) { AddDayItem(monthFirstDay.AddDays(i)); } FillNextMonth(); } /// /// �Զ�����ϸ������� /// void FillLastMonth() { monthFirstDay = new DateTime(Year, Month, 1); lastMonthEmptyDays = GetLastMonthDays(); if (zCalendarModel.autoFillDate) { for (int i = lastMonthEmptyDays; i > 0; i--) { AddDayItem(monthFirstDay.AddDays(-i)); } } else { for (int i = 0; i < lastMonthEmptyDays; i++) { ZCalendarDayItem dayItem = zCalendarModel.Instantiate(); dayItem.zCalendarController = this; dayItem.CloseClickAble(); } } } /// /// �����¸��µ�ʱ�� /// void FillNextMonth() { int nextMonthDays = 42 - (lastMonthEmptyDays + days); if (nextMonthDays != 0) { if (zCalendarModel.autoFillDate) { DateTime lastDay = monthFirstDay.AddDays(days); for (int i = 0; i < nextMonthDays; i++) { AddDayItem(lastDay.AddDays(i)); } } } } /// /// �������ڶ��� /// void AddDayItem(DateTime dateTime) { ZCalendarDayItem dayItem = zCalendarModel.Instantiate(); dayItem.zCalendarController = this; dayItem.Init(dateTime, nowTime); // 安全检查:确保 zCalendar 不为空,防止空引用异常 if (zCalendar != null) { zCalendar.UpdateDate(dayItem); } if (!isInRange && dayItemList.Count >= 2) { // 安全检查:确保列表中有至少2个元素,防止数组越界异常 dayItem.IsRangeDayItem(dayItemList[0], dayItemList[1]); } } /// /// �ж���һ�����м��� /// /// int GetLastMonthDays() { string firstWeek = new DateTime(Year, Month, 1).DayOfWeek.ToString(); return (int)Enum.Parse(typeof(DayOfWeek), firstWeek); } /// /// ɾ���������� /// void DestroyAllChildren() { List lst = new List(); int count = zCalendarModel.dayContent.childCount; for (int i = 0; i < count; i++) { Transform child = zCalendarModel.dayContent.GetChild(i); lst.Add(child); } for (int i = 0; i < lst.Count; i++) { MonoBehaviour.Destroy(lst[i].gameObject); } } } }