ScrollListControl.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using Immersal.REST;
  12. namespace Immersal.Samples.Mapping.ScrollList
  13. {
  14. [RequireComponent(typeof(ScrollRect))]
  15. public class ScrollListControl : MonoBehaviour {
  16. [SerializeField]
  17. private GameObject itemTemplate = null;
  18. [SerializeField]
  19. private Transform contentParent = null;
  20. private ScrollRect scrollRect;
  21. private List<GameObject> items = new List<GameObject>();
  22. private SDKJob[] m_Data;
  23. private List<int> m_ActiveMaps;
  24. [SerializeField]
  25. private GameObject loadMoreButton = null;
  26. [SerializeField]
  27. private int displayedItemCount = 50;
  28. public void SetData(SDKJob[] data, List<int> activeMaps)
  29. {
  30. bool newDataAvailable = false;
  31. if (m_Data != null)
  32. {
  33. newDataAvailable = data.Length > m_Data.Length;
  34. }
  35. m_Data = data;
  36. m_ActiveMaps = activeMaps;
  37. GenerateItems(0, items.Count==0?displayedItemCount:items.Count, false, newDataAvailable);
  38. }
  39. public void LoadMore()
  40. {
  41. GenerateItems(items.Count, items.Count + displayedItemCount, true);
  42. }
  43. public void GenerateItems(int from, int to, bool append = false, bool newDataAvailable = false)
  44. {
  45. to = Mathf.Clamp(to, 1, m_Data.Length);
  46. if (to == items.Count && !newDataAvailable)
  47. {
  48. //update exising items
  49. for (int i = 0; i < items.Count; i++)
  50. {
  51. ScrollListItem scrollListItem = items[i].GetComponent<ScrollListItem>();
  52. scrollListItem.data = m_Data[i];
  53. }
  54. return;
  55. }
  56. if (items.Count > 0 && !append) {
  57. DestroyItems();
  58. }
  59. if (m_Data != null && m_Data.Length > 0)
  60. {
  61. for (int i = from; i < to; i++) {
  62. SDKJob job = m_Data[i];
  63. GameObject item = Instantiate(itemTemplate);
  64. items.Add(item);
  65. item.name = item.name + "_" + i;
  66. item.SetActive(true);
  67. item.transform.SetParent(contentParent, false);
  68. ScrollListItem scrollListItem = item.GetComponent<ScrollListItem>();
  69. scrollListItem.PopulateData(job, IsActive(job.id));
  70. }
  71. }
  72. if (newDataAvailable)
  73. {
  74. ScrollToTop();
  75. }
  76. loadMoreButton.SetActive(m_Data.Length > to);
  77. loadMoreButton.transform.SetAsLastSibling();
  78. }
  79. private bool IsActive(int mapId)
  80. {
  81. foreach (int id in m_ActiveMaps)
  82. {
  83. if (mapId == id)
  84. return true;
  85. }
  86. return false;
  87. }
  88. public void DestroyItems()
  89. {
  90. foreach (GameObject item in items) {
  91. Destroy(item);
  92. }
  93. items.Clear();
  94. }
  95. private void ScrollToTop() {
  96. if (scrollRect != null)
  97. scrollRect.normalizedPosition = new Vector2(0, 1);
  98. }
  99. private void ScrollToBottom() {
  100. if (scrollRect != null)
  101. scrollRect.normalizedPosition = new Vector2(0, 0);
  102. }
  103. private void OnEnable() {
  104. scrollRect = GetComponent<ScrollRect>();
  105. ScrollToTop();
  106. }
  107. }
  108. }