155 lines
4.9 KiB
C#
155 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using TMPro;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using WI;
|
|
|
|
public class UI_Calendar : UIBase
|
|
{
|
|
public List<UI_CalendarItem> calendarItemList = new List<UI_CalendarItem>();
|
|
public GameObject prefab_calendarItem;
|
|
public RectTransform Text_Sunday;
|
|
public RectTransform Text_Monday;
|
|
public RectTransform ItemRoot;
|
|
public TextMeshProUGUI Text_Day;
|
|
public Button Button_PrevMonth;
|
|
public Button Button_NextMonth;
|
|
|
|
private const int totalItemNum = 42;
|
|
|
|
private DateTime originDateTime;
|
|
private DateTime curDateTime;
|
|
|
|
private TextMeshProUGUI targetText;
|
|
public float startSpace;
|
|
public float endSpace;
|
|
|
|
public Action<TextMeshProUGUI> onSelectDate;
|
|
|
|
public override void AfterAwake()
|
|
{
|
|
Button_PrevMonth.onClick.AddListener(OnClickPrevMonth);
|
|
Button_NextMonth.onClick.AddListener(OnClickNextMonth);
|
|
|
|
prefab_calendarItem = Resources.Load<GameObject>("Prefabs/UI/PRF_UI_CalendarItem");
|
|
float sundayPosX = Text_Sunday.anchoredPosition.x;
|
|
float mondayPosX = Text_Monday.anchoredPosition.x;
|
|
float space = mondayPosX - sundayPosX;
|
|
Vector2 prefabSize = prefab_calendarItem.GetComponent<RectTransform>().sizeDelta;
|
|
Vector2 itemStartPos = new Vector2(Text_Sunday.anchoredPosition.x, Text_Sunday.anchoredPosition.y - startSpace);
|
|
|
|
for (int i = 0; i < totalItemNum; i++)
|
|
{
|
|
UI_CalendarItem item = Instantiate(prefab_calendarItem.gameObject, ItemRoot).GetComponent<UI_CalendarItem>();
|
|
item.onClickEvent += OnClickItem;
|
|
item.GetComponent<RectTransform>().anchoredPosition = new Vector3((i % 7) * space + itemStartPos.x, itemStartPos.y - (i / 7) * endSpace);
|
|
calendarItemList.Add(item);
|
|
}
|
|
}
|
|
|
|
public void Open(TextMeshProUGUI target)
|
|
{
|
|
targetText = target;
|
|
originDateTime = Convert.ToDateTime(targetText.text);
|
|
curDateTime = Convert.ToDateTime(targetText.text);
|
|
SetActive(true);
|
|
SetCalendar();
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
SetActive(false);
|
|
}
|
|
void Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
if (RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition))
|
|
return;
|
|
|
|
Close();
|
|
}
|
|
}
|
|
|
|
void SetCalendar()
|
|
{
|
|
DateTime firstDay = curDateTime.AddDays(-(curDateTime.Day - 1));
|
|
int weekIndex = (int)firstDay.DayOfWeek;
|
|
int curMonthDaysCount = DateTime.DaysInMonth(curDateTime.Year, curDateTime.Month);
|
|
DateTime lastMonth = firstDay.AddDays(-1);
|
|
int lastMonthDaysCount = DateTime.DaysInMonth(lastMonth.Year, lastMonth.Month);
|
|
DateTime today = DateTime.Today;
|
|
|
|
int date = 0;
|
|
|
|
for (int i = 0; i < totalItemNum; i++)
|
|
{
|
|
if (i >= weekIndex)
|
|
{
|
|
DateTime day = firstDay.AddDays(date);
|
|
if (day.Month == firstDay.Month) // ÇöÀç ´Þ
|
|
{
|
|
calendarItemList[i].SetDate(day, Color.white);
|
|
|
|
if (originDateTime.Date == day.Date)
|
|
{
|
|
calendarItemList[i].ShowSelectUI();
|
|
}
|
|
else if(today == day.Date)
|
|
{
|
|
calendarItemList[i].ShowTodayUI();
|
|
}
|
|
}
|
|
else // ´ÙÀ½ ´Þ
|
|
{
|
|
int nextMonthDay = date + 1 - curMonthDaysCount;
|
|
DateTime nextMonthDate = new DateTime(day.Year, day.Month, nextMonthDay);
|
|
calendarItemList[i].SetDate(nextMonthDate, Color.gray);
|
|
|
|
if (today == nextMonthDate)
|
|
{
|
|
calendarItemList[i].ShowTodayUI();
|
|
}
|
|
}
|
|
|
|
date++;
|
|
}
|
|
else // ÀÌÀü ´Þ
|
|
{
|
|
DateTime LastMonthDate = new DateTime(lastMonth.Year, lastMonth.Month, lastMonthDaysCount - (weekIndex - i - 1));
|
|
calendarItemList[i].SetDate(LastMonthDate, Color.gray);
|
|
|
|
if (today == LastMonthDate)
|
|
{
|
|
calendarItemList[i].ShowTodayUI();
|
|
}
|
|
}
|
|
}
|
|
|
|
Text_Day.text = $"{curDateTime.ToString("MMMM", new CultureInfo("en-US"))} {curDateTime.Year}";
|
|
}
|
|
|
|
private void OnClickItem(DateTime date)
|
|
{
|
|
targetText.text = date.ToString("yyyy-MM-dd");
|
|
onSelectDate?.Invoke(targetText);
|
|
Close();
|
|
}
|
|
|
|
private void OnClickPrevMonth()
|
|
{
|
|
curDateTime = curDateTime.AddMonths(-1);
|
|
SetCalendar();
|
|
}
|
|
|
|
private void OnClickNextMonth()
|
|
{
|
|
curDateTime = curDateTime.AddMonths(1);
|
|
SetCalendar();
|
|
}
|
|
}
|