캘린더 추가
This commit is contained in:
139
Assets/Scripts/PGD/UI_Calendar.cs
Normal file
139
Assets/Scripts/PGD/UI_Calendar.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
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 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 - space);
|
||||
|
||||
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) * space);
|
||||
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 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.black);
|
||||
|
||||
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 dayInLastMonth = new DateTime(lastMonth.Year, lastMonth.Month, lastMonthDaysCount - (weekIndex - i - 1));
|
||||
calendarItemList[i].SetDate(dayInLastMonth, Color.gray);
|
||||
|
||||
if (today == dayInLastMonth)
|
||||
{
|
||||
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");
|
||||
Close();
|
||||
}
|
||||
|
||||
private void OnClickPrevMonth()
|
||||
{
|
||||
curDateTime = curDateTime.AddMonths(-1);
|
||||
SetCalendar();
|
||||
}
|
||||
|
||||
private void OnClickNextMonth()
|
||||
{
|
||||
curDateTime = curDateTime.AddMonths(1);
|
||||
SetCalendar();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PGD/UI_Calendar.cs.meta
Normal file
11
Assets/Scripts/PGD/UI_Calendar.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ab8c7ef5fa70aa4fb6b5cb0f1aea1a0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
73
Assets/Scripts/PGD/UI_CalendarItem.cs
Normal file
73
Assets/Scripts/PGD/UI_CalendarItem.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
using WI;
|
||||
|
||||
public class UI_CalendarItem : UIBase, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
|
||||
{
|
||||
DateTime targetDate;
|
||||
|
||||
public Action<DateTime> onClickEvent;
|
||||
public Image HoverUI;
|
||||
public Image SelectUI;
|
||||
public Image TodayUI;
|
||||
public TextMeshProUGUI Text_Day;
|
||||
|
||||
public void OnDisable()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
HoverUI.gameObject.SetActive(false);
|
||||
SelectUI.gameObject.SetActive(false);
|
||||
TodayUI.gameObject.SetActive(false);
|
||||
Text_Day.text = string.Empty;
|
||||
}
|
||||
|
||||
public void SetDate(DateTime date, Color color)
|
||||
{
|
||||
Init();
|
||||
targetDate = date;
|
||||
Text_Day.text = date.Day.ToString();
|
||||
Text_Day.color = color;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
SetActive(false);
|
||||
}
|
||||
|
||||
public void ShowSelectUI()
|
||||
{
|
||||
SelectUI.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void ShowTodayUI()
|
||||
{
|
||||
TodayUI.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
onClickEvent?.Invoke(targetDate);
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
if (TodayUI.IsActive())
|
||||
return;
|
||||
|
||||
HoverUI.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
HoverUI.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PGD/UI_CalendarItem.cs.meta
Normal file
11
Assets/Scripts/PGD/UI_CalendarItem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 82f43be8f5583ee4c8d6f3ef1b8bb308
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user