2020-10-24 00:41:55 +11:00
|
|
|
|
using System;
|
2020-11-03 20:59:13 +11:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2021-03-18 17:17:29 +11:00
|
|
|
|
using UnityExplorer.Core.Config;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
using UnityExplorer.UI.Utility;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
namespace UnityExplorer.UI.Models
|
2020-10-24 00:41:55 +11:00
|
|
|
|
{
|
|
|
|
|
public enum Turn
|
|
|
|
|
{
|
|
|
|
|
Left,
|
|
|
|
|
Right
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
public class PageHandler : UIModel, IEnumerator
|
2020-10-24 00:41:55 +11:00
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
public PageHandler(SliderScrollbar scroll)
|
2020-10-24 00:41:55 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
ItemsPerPage = ConfigManager.Default_Page_Limit?.Value ?? 20;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
m_scrollbar = scroll;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
public event Action OnPageChanged;
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
// UI members
|
2020-11-15 21:11:43 +11:00
|
|
|
|
private readonly SliderScrollbar m_scrollbar;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
private GameObject m_pageUIHolder;
|
|
|
|
|
private Text m_currentPageLabel;
|
|
|
|
|
|
|
|
|
|
public override GameObject UIRoot => m_pageUIHolder;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
|
2020-10-24 00:41:55 +11:00
|
|
|
|
// For now this is just set when the PageHandler is created, based on config.
|
|
|
|
|
// At some point I might make it possible to change this after creation again.
|
2020-11-03 20:59:13 +11:00
|
|
|
|
public int ItemsPerPage { get; }
|
|
|
|
|
|
|
|
|
|
// IEnumerator.Current
|
|
|
|
|
public object Current => m_currentIndex;
|
|
|
|
|
private int m_currentIndex = 0;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
public int CurrentPage
|
|
|
|
|
{
|
|
|
|
|
get => m_currentPage;
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-11-13 18:46:36 +11:00
|
|
|
|
if (value < PageCount)
|
2020-11-08 21:04:41 +11:00
|
|
|
|
m_currentPage = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-24 00:41:55 +11:00
|
|
|
|
private int m_currentPage;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
// set and maintained by owner of list
|
|
|
|
|
private int m_listCount;
|
|
|
|
|
public int ListCount
|
|
|
|
|
{
|
|
|
|
|
get => m_listCount;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
m_listCount = value;
|
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
if (PageCount <= 0 && m_pageUIHolder.activeSelf)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
|
|
|
|
m_pageUIHolder.SetActive(false);
|
|
|
|
|
}
|
2020-11-13 18:46:36 +11:00
|
|
|
|
else if (PageCount > 0 && !m_pageUIHolder.activeSelf)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
|
|
|
|
m_pageUIHolder.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefreshUI();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 18:46:36 +11:00
|
|
|
|
public int PageCount => (int)Math.Ceiling(ListCount / (decimal)ItemsPerPage) - 1;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
|
|
|
|
|
// The index of the first element of the current page
|
2020-11-03 20:59:13 +11:00
|
|
|
|
public int StartIndex
|
2020-10-24 00:41:55 +11:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-11-03 20:59:13 +11:00
|
|
|
|
int offset = m_currentPage * ItemsPerPage;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
|
|
|
|
|
if (offset >= ListCount)
|
|
|
|
|
{
|
|
|
|
|
offset = 0;
|
|
|
|
|
m_currentPage = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 20:59:13 +11:00
|
|
|
|
public int EndIndex
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
int end = StartIndex + ItemsPerPage;
|
|
|
|
|
if (end >= ListCount)
|
|
|
|
|
end = ListCount - 1;
|
|
|
|
|
return end;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IEnumerator.MoveNext()
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
{
|
|
|
|
|
m_currentIndex++;
|
|
|
|
|
return m_currentIndex < StartIndex + ItemsPerPage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IEnumerator.Reset()
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
m_currentIndex = StartIndex - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerator<int> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
while (MoveNext())
|
|
|
|
|
{
|
|
|
|
|
yield return m_currentIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-24 00:41:55 +11:00
|
|
|
|
public void TurnPage(Turn direction)
|
|
|
|
|
{
|
2020-11-08 21:04:41 +11:00
|
|
|
|
bool didTurn = false;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
if (direction == Turn.Left)
|
|
|
|
|
{
|
|
|
|
|
if (m_currentPage > 0)
|
|
|
|
|
{
|
|
|
|
|
m_currentPage--;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
didTurn = true;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-13 18:46:36 +11:00
|
|
|
|
if (m_currentPage < PageCount)
|
2020-10-24 00:41:55 +11:00
|
|
|
|
{
|
|
|
|
|
m_currentPage++;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
didTurn = true;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-08 21:04:41 +11:00
|
|
|
|
if (didTurn)
|
|
|
|
|
{
|
2020-11-15 21:11:43 +11:00
|
|
|
|
if (m_scrollbar != null)
|
|
|
|
|
m_scrollbar.m_scrollbar.value = 1;
|
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
OnPageChanged?.Invoke();
|
|
|
|
|
RefreshUI();
|
|
|
|
|
}
|
2020-10-24 00:41:55 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Show() => m_pageUIHolder?.SetActive(true);
|
|
|
|
|
|
|
|
|
|
public void Hide() => m_pageUIHolder?.SetActive(false);
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public void RefreshUI()
|
|
|
|
|
{
|
2021-04-15 20:18:03 +10:00
|
|
|
|
m_currentPageLabel.text = $"Page {CurrentPage + 1} / {CurrentPage + 1}";
|
|
|
|
|
|
|
|
|
|
// TODO
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
public override void ConstructUI(GameObject parent)
|
2020-10-24 00:41:55 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
m_pageUIHolder = UIFactory.CreateHorizontalGroup(parent, "PageHandlerButtons", false, true, true, true);
|
2020-10-24 00:41:55 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Image image = m_pageUIHolder.GetComponent<Image>();
|
2020-11-08 21:04:41 +11:00
|
|
|
|
image.color = new Color(0.2f, 0.2f, 0.2f, 0.5f);
|
2020-10-24 00:41:55 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
UIFactory.SetLayoutElement(m_pageUIHolder, minHeight: 25, minWidth: 100, flexibleWidth: 5000);
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
var leftBtnObj = UIFactory.CreateButton(m_pageUIHolder,
|
|
|
|
|
"BackBtn",
|
|
|
|
|
"◄",
|
|
|
|
|
() => { TurnPage(Turn.Left); },
|
2021-03-30 19:50:04 +11:00
|
|
|
|
new Color(0.15f, 0.15f, 0.15f));
|
|
|
|
|
|
|
|
|
|
UIFactory.SetLayoutElement(leftBtnObj.gameObject, flexibleWidth: 1500, minWidth: 25, minHeight: 25);
|
|
|
|
|
|
|
|
|
|
m_currentPageLabel = UIFactory.CreateLabel(m_pageUIHolder, "PageLabel", "Page 1 / TODO", TextAnchor.MiddleCenter);
|
|
|
|
|
|
|
|
|
|
UIFactory.SetLayoutElement(m_currentPageLabel.gameObject, minWidth: 100, flexibleWidth: 40);
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
Button rightBtn = UIFactory.CreateButton(m_pageUIHolder,
|
|
|
|
|
"RightBtn",
|
2021-03-30 19:50:04 +11:00
|
|
|
|
"►",
|
2021-04-15 20:18:03 +10:00
|
|
|
|
() => { TurnPage(Turn.Right); },
|
2021-03-30 19:50:04 +11:00
|
|
|
|
new Color(0.15f, 0.15f, 0.15f));
|
|
|
|
|
|
|
|
|
|
UIFactory.SetLayoutElement(rightBtn.gameObject, flexibleWidth: 1500, minWidth: 25, minHeight: 25);
|
2020-10-24 00:41:55 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
ListCount = 0;
|
2020-10-24 00:41:55 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|