public void UpdateDisplay() { // don't do anything if we aren't actually active in the hierarchy // (basically, we're either inactive or a prefab) if (!this.gameObject.activeInHierarchy) { return; } if (Config.Sizing.OverrideTransformHeight) { Ref_DatePickerTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Config.Sizing.PreferredHeight); } UpdateBorder(); UpdateHeader(); UpdateWeekDayHeaders(); UpdateDaySection(); // Free all buttons in the pool buttonPool.FreeAll(); // Clear existing data Ref_DayTable.ClearRows(); // Day Names var dayNameRow = Ref_DayTable.AddRow(0); dayNameRow.dontUseTableRowBackground = true; // Empty cell if we're showing the Week Numbers column if (Config.WeekDays.ShowWeekNumbers) { var header = Instantiate(Ref_Template_DayName); dayNameRow.AddCell(header.Cell); Ref_DayTable.ColumnWidths[0] = Config.WeekDays.WeekNumberColumnWidth; } else { Ref_DayTable.ColumnWidths[0] = 0; } var dayNames = DatePickerUtilities.GetAbbreviatedDayNames(); foreach (var dayName in dayNames) { var header = Instantiate(Ref_Template_DayName); dayNameRow.AddCell(header.Cell); header.HeaderText.text = dayName; } // Validate our Date Range (if necessary. This method will output an error message if we fail) bool dateRangeValid = Config.DateRange.Validate(); // Dates var days = DatePickerUtilities.GetDateRangeForDisplay(VisibleDate.Date); TableRow row = null; int weekNumber = 1; DateTimeFormatInfo currentDateTimeFormatInfo = DateTimeFormatInfo.CurrentInfo; foreach (var day in days) { if (day.DayOfWeek == DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek) { row = Ref_DayTable.AddRow(0); if (Config.WeekDays.ShowWeekNumbers) { if (Config.WeekDays.WeekNumberMode == WeekNumberMode.WeekOfYear) { weekNumber = currentDateTimeFormatInfo.Calendar.GetWeekOfYear(day, Config.WeekDays.CalendarWeekRule, currentDateTimeFormatInfo.FirstDayOfWeek); } var weekNumberCell = Instantiate(Ref_Template_DayName); row.AddCell(weekNumberCell.Cell); weekNumberCell.HeaderText.text = weekNumber.ToString(); weekNumber++; } } if (!Config.Misc.ShowDatesInOtherMonths && !DatePickerUtilities.DateFallsWithinMonth(day, VisibleDate)) { // add an empty cell row.AddCell(); } else { var dayType = GetDayTypeForDate(day); var dayItem = buttonPool.GetButton(dayType); //var dayItem = Instantiate(GetDayTemplateForDate(day)); row.AddCell(dayItem.Cell); dayItem.Text.text = day.Day.ToString(); dayItem.DatePicker = this; dayItem.Date = day; dayItem.name = day.ToDateString(); dayItem.IsTemplate = false; dayItem.Button.interactable = true; if (dateRangeValid) // if the date range is not valid, then don't attempt to use it { if ((Config.DateRange.RestrictFromDate && day.CompareTo(Config.DateRange.FromDate) < 0) || (Config.DateRange.RestrictToDate && day.CompareTo(Config.DateRange.ToDate) > 0)) { dayItem.Button.interactable = false; } } } } if (Ref_InputField != null && Ref_InputFieldContainer != null && Ref_InputFieldToggleButtonCell != null) { Ref_InputField.text = SelectedDate.HasValue ? SelectedDate.Date.ToString(Config.Format.DateFormat) : ""; if (Ref_ScreenOverlay != null) { Ref_ScreenOverlay.color = Config.Modal.ScreenOverlayColor; } var valueBefore = Ref_InputFieldContainer.ColumnWidths.ToList(); if (Config.InputField.ShowToggleButton) { Ref_InputFieldContainer.ColumnWidths = new List <float> { 0, Config.InputField.ToggleButtonWidth }; Ref_InputFieldToggleButtonCell.gameObject.SetActive(true); } else { Ref_InputFieldContainer.ColumnWidths = new List <float> { 0 }; Ref_InputFieldToggleButtonCell.gameObject.SetActive(false); } if (!valueBefore.SequenceEqual(Ref_InputFieldContainer.ColumnWidths)) { Ref_InputFieldContainer.UpdateLayout(); } } }
bool DrawDatePicker(Rect position, SerializedProperty property, SerializedProperty showDatePickerProperty) { if (!VisibleDate.HasValue) { VisibleDate = SelectedDate; } float startX = position.x; var calendarPosition = new Rect(position.x, position.y, 224, 256); EditorGUI.DrawRect(calendarPosition, new Color(0, 0, 0, 0.1f)); var rowSize = 224f / 7f; var columnSize = rowSize; // previous year button if (GUI.Button(new Rect(position.x, position.y, rowSize, columnSize), doubleLeftArrowImage)) { VisibleDate = VisibleDate.Value.AddYears(-1); } position.x += columnSize; // previous month button if (GUI.Button(new Rect(position.x, position.y, rowSize, columnSize), leftArrowImage)) { VisibleDate = VisibleDate.Value.AddMonths(-1); } position.x += columnSize; // month name GUI.Label(new Rect(position.x, position.y, columnSize * 3, rowSize), VisibleDate.Value.ToString("MMM yyyy"), monthNameStyle); position.x += columnSize * 3; // next month button if (GUI.Button(new Rect(position.x, position.y, columnSize, rowSize), rightArrowImage)) { VisibleDate = VisibleDate.Value.AddMonths(+1); } position.x += columnSize; // next year button if (GUI.Button(new Rect(position.x, position.y, columnSize, rowSize), doubleRightArrowImage)) { VisibleDate = VisibleDate.Value.AddYears(+1); } // next row position.x = startX; position.y += rowSize; // Day of week headers var dayNames = DatePickerUtilities.GetAbbreviatedDayNames().Select(d => d.Substring(0, 1)).ToArray(); foreach (var dayName in dayNames) { GUI.Label(new Rect(position.x, position.y, columnSize, rowSize), dayName, dayNameStyle); position.x += columnSize; } // Start outputing day buttons var days = DatePickerUtilities.GetDateRangeForDisplay(VisibleDate.Value); var selectedDateString = SelectedDate.ToDateString(); var returnValue = true; foreach (var day in days) { if (day.DayOfWeek == DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek) { // Next row position.x = startX; position.y += rowSize; } var style = dayButtonStyle; if (day.Month == VisibleDate.Value.Month) { var backColor = new Color(0, 0, 0, 0.5f); if (day.ToDateString() == selectedDateString) { style = dayButtonStyle_CurrentDay; backColor = Color.green; } EditorGUI.DrawRect(new Rect(position.x, position.y, rowSize, columnSize), backColor); } else { style = dayButtonStyle_OtherMonth; } if (GUI.Button(new Rect(position.x + 1, position.y + 1, columnSize - 2, rowSize - 2), day.Day.ToString(), style)) { property.stringValue = day.ToDateString(); VisibleDate = day; returnValue = false; // hide the datepicker } position.x += columnSize; } return(returnValue); }