| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public interface ICarouselComponentView |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Set the distance between carousel items. |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="newSpace">Distance between items.</param> |
| | 14 | | void SetSpaceBetweenItems(float newSpace); |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Set the time that will be pass between carousel items. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="newTime">Time between items.</param> |
| | 20 | | void SetTimeBetweenItems(float newTime); |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Set the time that will be pass during the transition between items. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="newTime">Transition time between items.</param> |
| | 26 | | void SetAnimationTransitionTime(float newTime); |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Set the animation curve that will be used for the animation between items. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="newCurve">Animation curve between items.</param> |
| | 32 | | void SetAnimationCurve(AnimationCurve newCurve); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Set the color of the carousel background. |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="newColor">Background color.</param> |
| | 38 | | void SetBackgroundColor(Color newColor); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Activates/Deactivates the controls to go to the next/previous item manually. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="isActived">True for activating the manual controls.</param> |
| | 44 | | void SetManualControlsActive(bool isActived); |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Set the items of the carousel. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="items">List of UI components.</param> |
| | 50 | | void SetItems(List<BaseComponentView> items); |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Creates the items of the carousel from the prefab. All previously existing items will be removed. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="prefab">Prefab to create items</param> |
| | 56 | | /// <param name="amountOfItems">Amounts of items to be created</param> |
| | 57 | | void SetItems(BaseComponentView prefab, int amountOfItems); |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Adds a new item in the carousel. |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="item">An UI component.</param> |
| | 63 | | void AddItem(BaseComponentView item); |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Adds a new item in the carousel and update carousel dot selector. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="item">An UI component.</param> |
| | 69 | | void AddItemWithDotsSelector(BaseComponentView item); |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Remove an item from the carousel. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="item">An UI component</param> |
| | 75 | | void RemoveItem(BaseComponentView item); |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Get all the items of the carousel. |
| | 79 | | /// </summary> |
| | 80 | | /// <returns>The list of items.</returns> |
| | 81 | | List<BaseComponentView> GetItems(); |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Extract all items out of the carousel. |
| | 85 | | /// </summary> |
| | 86 | | /// <returns>The list of extracted items.</returns> |
| | 87 | | List<BaseComponentView> ExtractItems(); |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Remove all existing items from the carousel. |
| | 91 | | /// </summary> |
| | 92 | | void RemoveItems(); |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Start carousel animation. |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="fromIndex">It specifies from where item the carousel will start.</param> |
| | 98 | | /// <param name="startInmediately">True to directly execute the first transition.</param> |
| | 99 | | /// <param name="direction">Set the direction of the carousel animations: right or left.</param> |
| | 100 | | /// <param name="changeDirectionAfterFirstTransition">True to change the carousel direction just after the first tra |
| | 101 | | /// <param name="numberOfInitialJumps">Number of jumps that will be executed in the first transition.</param> |
| | 102 | | void StartCarousel(int fromIndex, bool startInmediately, CarouselDirection direction, bool changeDirectionAfterFirst |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Stop carousel animation. |
| | 106 | | /// </summary> |
| | 107 | | void StopCarousel(); |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Force the carousel to show the previous item. |
| | 111 | | /// </summary> |
| | 112 | | void GoToPreviousItem(); |
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Force the carousel to show the next item. |
| | 116 | | /// </summary> |
| | 117 | | void GoToNextItem(); |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Force the carousel to jump to a specific item. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="numberOfJumps">Number of jumps that will be executed during the transition.</param> |
| | 123 | | /// <param name="direction">Direction in which to make the jumps.</param> |
| | 124 | | void MakeJumpFromDotsSelector(int numberOfJumps, CarouselDirection direction); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | public enum CarouselDirection |
| | 128 | | { |
| | 129 | | Right, |
| | 130 | | Left |
| | 131 | | } |
| | 132 | |
|
| | 133 | | public class CarouselComponentView : BaseComponentView, ICarouselComponentView, IComponentModelConfig<CarouselComponentM |
| | 134 | | { |
| | 135 | | [Header("Prefab References")] |
| | 136 | | [SerializeField] internal RectTransform itemsContainer; |
| | 137 | | [SerializeField] internal HorizontalLayoutGroup horizontalLayout; |
| | 138 | | [SerializeField] internal ScrollRect itemsScroll; |
| | 139 | | [SerializeField] internal RectTransform viewport; |
| | 140 | | [SerializeField] internal Image background; |
| | 141 | | [SerializeField] internal Button previousButton; |
| | 142 | | [SerializeField] internal Button nextButton; |
| | 143 | | [SerializeField] internal HorizontalLayoutGroup dotsSelector; |
| | 144 | | [SerializeField] internal Button dotButtonTemplate; |
| | 145 | | [SerializeField] internal Color dotSelectedColor; |
| | 146 | | [SerializeField] internal Color dotUnselectedColor; |
| | 147 | | [SerializeField] internal bool showOnFocus = false; |
| | 148 | |
|
| | 149 | | [Header("Configuration")] |
| | 150 | | [SerializeField] internal CarouselComponentModel model; |
| | 151 | |
|
| 79 | 152 | | internal List<BaseComponentView> instantiatedItems = new List<BaseComponentView>(); |
| | 153 | | internal Coroutine itemsCoroutine; |
| | 154 | | internal int currentItemIndex = 0; |
| | 155 | | internal int currentDotIndex = 0; |
| | 156 | | internal float currentFinalNormalizedPos; |
| | 157 | | internal bool isInTransition = false; |
| | 158 | |
|
| | 159 | | public override void Awake() |
| | 160 | | { |
| 75 | 161 | | base.Awake(); |
| | 162 | |
|
| 75 | 163 | | StartCoroutine(RegisterCurrentInstantiatedItems()); |
| 75 | 164 | | ConfigureManualButtonsEvents(); |
| 75 | 165 | | } |
| | 166 | |
|
| | 167 | | public void Start() |
| | 168 | | { |
| 34 | 169 | | if (model.automaticTransition) |
| 34 | 170 | | StartCarousel(); |
| 34 | 171 | | } |
| | 172 | |
|
| | 173 | | public void Configure(CarouselComponentModel newModel) |
| | 174 | | { |
| 1 | 175 | | model = newModel; |
| 1 | 176 | | RefreshControl(); |
| 1 | 177 | | } |
| | 178 | |
|
| | 179 | | public override void RefreshControl() |
| | 180 | | { |
| 1 | 181 | | if (model == null) |
| 0 | 182 | | return; |
| | 183 | |
|
| 1 | 184 | | SetSpaceBetweenItems(model.spaceBetweenItems); |
| 1 | 185 | | SetTimeBetweenItems(model.timeBetweenItems); |
| 1 | 186 | | SetAnimationTransitionTime(model.animationTransitionTime); |
| 1 | 187 | | SetAnimationCurve(model.animationCurve); |
| 1 | 188 | | SetBackgroundColor(model.backgroundColor); |
| 1 | 189 | | SetManualControlsActive(model.showManualControls); |
| 1 | 190 | | ResizeAllItems(); |
| 1 | 191 | | GenerateDotsSelector(); |
| 1 | 192 | | } |
| | 193 | |
|
| | 194 | | public override void OnScreenSizeChanged() |
| | 195 | | { |
| 6 | 196 | | base.OnScreenSizeChanged(); |
| | 197 | |
|
| 6 | 198 | | ResizeAllItems(); |
| 6 | 199 | | } |
| | 200 | |
|
| | 201 | | public override void Dispose() |
| | 202 | | { |
| 209 | 203 | | base.Dispose(); |
| | 204 | |
|
| 209 | 205 | | StopCarousel(); |
| 209 | 206 | | DestroyInstantiatedItems(); |
| 209 | 207 | | } |
| | 208 | |
|
| | 209 | | public void SetSpaceBetweenItems(float newSpace) |
| | 210 | | { |
| 2 | 211 | | model.spaceBetweenItems = newSpace; |
| | 212 | |
|
| 2 | 213 | | if (horizontalLayout == null) |
| 0 | 214 | | return; |
| | 215 | |
|
| 2 | 216 | | horizontalLayout.spacing = newSpace; |
| 2 | 217 | | } |
| | 218 | |
|
| | 219 | | public void SetTimeBetweenItems(float newTime) |
| | 220 | | { |
| 2 | 221 | | model.timeBetweenItems = newTime; |
| 2 | 222 | | } |
| | 223 | |
|
| | 224 | | public void SetAnimationTransitionTime(float newTime) |
| | 225 | | { |
| 2 | 226 | | model.animationTransitionTime = newTime; |
| 2 | 227 | | } |
| | 228 | |
|
| | 229 | | public void SetAnimationCurve(AnimationCurve newCurve) |
| | 230 | | { |
| 2 | 231 | | model.animationCurve = newCurve; |
| 2 | 232 | | } |
| | 233 | |
|
| | 234 | | public void SetBackgroundColor(Color newColor) |
| | 235 | | { |
| 2 | 236 | | model.backgroundColor = newColor; |
| | 237 | |
|
| 2 | 238 | | if (background == null) |
| 2 | 239 | | return; |
| | 240 | |
|
| 0 | 241 | | background.color = newColor; |
| 0 | 242 | | } |
| | 243 | |
|
| | 244 | | public void SetManualControlsActive() => |
| 0 | 245 | | SetManualControlsActive(model.showManualControls); |
| | 246 | |
|
| | 247 | | public void SetManualControlsActive(bool isActived) |
| | 248 | | { |
| 359 | 249 | | model.showManualControls = isActived; |
| | 250 | |
|
| 359 | 251 | | if (previousButton == null || nextButton == null) |
| 0 | 252 | | return; |
| | 253 | |
|
| 359 | 254 | | int currentNumberOfItems = itemsContainer.childCount; |
| 359 | 255 | | previousButton.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 359 | 256 | | nextButton.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 359 | 257 | | dotsSelector.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 359 | 258 | | } |
| | 259 | |
|
| | 260 | | public override void OnFocus() |
| | 261 | | { |
| 0 | 262 | | base.OnFocus(); |
| | 263 | |
|
| 0 | 264 | | if (previousButton == null || nextButton == null || !showOnFocus) |
| 0 | 265 | | return; |
| | 266 | |
|
| 0 | 267 | | int currentNumberOfItems = itemsContainer.childCount; |
| 0 | 268 | | previousButton.gameObject.SetActive(currentNumberOfItems > 1); |
| 0 | 269 | | nextButton.gameObject.SetActive(currentNumberOfItems > 1); |
| 0 | 270 | | } |
| | 271 | |
|
| | 272 | | public override void OnLoseFocus() |
| | 273 | | { |
| 75 | 274 | | base.OnLoseFocus(); |
| | 275 | |
|
| 75 | 276 | | if (previousButton == null || nextButton == null || !showOnFocus) |
| 75 | 277 | | return; |
| | 278 | |
|
| 0 | 279 | | previousButton.gameObject.SetActive(false); |
| 0 | 280 | | nextButton.gameObject.SetActive(false); |
| 0 | 281 | | } |
| | 282 | |
|
| | 283 | | public void SetItems(BaseComponentView prefab, int amountOfItems) |
| | 284 | | { |
| 0 | 285 | | DestroyInstantiatedItems(); |
| | 286 | |
|
| 0 | 287 | | for (int i = 0; i < amountOfItems; i++) |
| | 288 | | { |
| 0 | 289 | | BaseComponentView instanciatedItem = Instantiate(prefab); |
| 0 | 290 | | CreateItem(instanciatedItem, $"Item{i}"); |
| | 291 | | } |
| | 292 | |
|
| 0 | 293 | | SetManualControlsActive(model.showManualControls); |
| 0 | 294 | | GenerateDotsSelector(); |
| 0 | 295 | | } |
| | 296 | |
|
| | 297 | | public void SetItems(List<BaseComponentView> items) |
| | 298 | | { |
| 15 | 299 | | DestroyInstantiatedItems(); |
| | 300 | |
|
| 92 | 301 | | for (int i = 0; i < items.Count; i++) |
| | 302 | | { |
| 31 | 303 | | CreateItem(items[i], $"Item{i}"); |
| | 304 | | } |
| | 305 | |
|
| 15 | 306 | | SetManualControlsActive(model.showManualControls); |
| 15 | 307 | | GenerateDotsSelector(); |
| 15 | 308 | | } |
| | 309 | |
|
| | 310 | | public void AddItemWithDotsSelector(BaseComponentView item) |
| | 311 | | { |
| 2 | 312 | | CreateItem(item, $"Item{instantiatedItems.Count}"); |
| 2 | 313 | | SetManualControlsActive(model.showManualControls); |
| 2 | 314 | | GenerateDotsSelector(); |
| 2 | 315 | | } |
| | 316 | |
|
| | 317 | | public void AddItem(BaseComponentView item) => |
| 2 | 318 | | CreateItem(item, $"Item{instantiatedItems.Count}"); |
| | 319 | |
|
| | 320 | | public void RemoveItem(BaseComponentView item) |
| | 321 | | { |
| 2 | 322 | | BaseComponentView itemToRemove = instantiatedItems.FirstOrDefault(x => x == item); |
| 1 | 323 | | if (itemToRemove != null) |
| | 324 | | { |
| 1 | 325 | | Destroy(itemToRemove.gameObject); |
| 1 | 326 | | instantiatedItems.Remove(item); |
| | 327 | | } |
| | 328 | |
|
| 1 | 329 | | SetManualControlsActive(model.showManualControls); |
| 1 | 330 | | GenerateDotsSelector(); |
| 1 | 331 | | } |
| | 332 | |
|
| 1 | 333 | | public List<BaseComponentView> GetItems() { return instantiatedItems; } |
| | 334 | |
|
| | 335 | | public List<BaseComponentView> ExtractItems() |
| | 336 | | { |
| 286 | 337 | | List<BaseComponentView> extractedItems = new List<BaseComponentView>(); |
| 744 | 338 | | foreach (BaseComponentView item in instantiatedItems) |
| | 339 | | { |
| 86 | 340 | | if (item != null) |
| 86 | 341 | | item.transform.SetParent(null); |
| | 342 | |
|
| 86 | 343 | | extractedItems.Add(item); |
| | 344 | | } |
| | 345 | |
|
| 286 | 346 | | instantiatedItems.Clear(); |
| 286 | 347 | | SetManualControlsActive(model.showManualControls); |
| | 348 | |
|
| 286 | 349 | | return extractedItems; |
| | 350 | | } |
| | 351 | |
|
| | 352 | | public void RemoveItems() |
| | 353 | | { |
| 46 | 354 | | DestroyInstantiatedItems(); |
| 46 | 355 | | SetManualControlsActive(model.showManualControls); |
| 46 | 356 | | } |
| | 357 | |
|
| | 358 | | public void StartCarousel( |
| | 359 | | int fromIndex = 0, |
| | 360 | | bool startInmediately = false, |
| | 361 | | CarouselDirection direction = CarouselDirection.Right, |
| | 362 | | bool changeDirectionAfterFirstTransition = false, |
| | 363 | | int numberOfInitialJumps = 1) |
| | 364 | | { |
| 53 | 365 | | StopCarousel(); |
| | 366 | |
|
| 53 | 367 | | if (isActiveAndEnabled) |
| 53 | 368 | | itemsCoroutine = StartCoroutine(RunCarouselCoroutine(fromIndex, startInmediately, direction, changeDirection |
| 53 | 369 | | } |
| | 370 | |
|
| | 371 | | public void StopCarousel() |
| | 372 | | { |
| 263 | 373 | | if (itemsCoroutine == null) |
| 257 | 374 | | return; |
| | 375 | |
|
| 6 | 376 | | StopCoroutine(itemsCoroutine); |
| | 377 | |
|
| 6 | 378 | | itemsCoroutine = null; |
| 6 | 379 | | isInTransition = false; |
| 6 | 380 | | } |
| | 381 | |
|
| | 382 | | public void GoToPreviousItem() |
| | 383 | | { |
| 2 | 384 | | if (isInTransition) |
| 0 | 385 | | return; |
| | 386 | |
|
| 2 | 387 | | StartCarousel( |
| | 388 | | fromIndex: currentItemIndex, |
| | 389 | | startInmediately: true, |
| | 390 | | direction: CarouselDirection.Left, |
| | 391 | | changeDirectionAfterFirstTransition: true, |
| | 392 | | numberOfInitialJumps: 1); |
| 2 | 393 | | } |
| | 394 | |
|
| | 395 | | public void ResetCarousel() |
| | 396 | | { |
| 0 | 397 | | int index = 0; |
| 0 | 398 | | SetSelectedDot(index); |
| 0 | 399 | | } |
| | 400 | |
|
| | 401 | | public void GoToNextItem() |
| | 402 | | { |
| 2 | 403 | | if (isInTransition) |
| 0 | 404 | | return; |
| | 405 | |
|
| 2 | 406 | | StartCarousel( |
| | 407 | | fromIndex: currentItemIndex, |
| | 408 | | startInmediately: true, |
| | 409 | | direction: CarouselDirection.Right, |
| | 410 | | changeDirectionAfterFirstTransition: false, |
| | 411 | | numberOfInitialJumps: 1); |
| 2 | 412 | | } |
| | 413 | |
|
| | 414 | | public void MakeJumpFromDotsSelector(int numberOfJumps, CarouselDirection direction) |
| | 415 | | { |
| 1 | 416 | | if (isInTransition) |
| 0 | 417 | | return; |
| | 418 | |
|
| 1 | 419 | | StartCarousel( |
| | 420 | | fromIndex: currentItemIndex, |
| | 421 | | startInmediately: true, |
| | 422 | | direction: direction, |
| | 423 | | changeDirectionAfterFirstTransition: direction == CarouselDirection.Left, |
| | 424 | | numberOfInitialJumps: numberOfJumps); |
| 1 | 425 | | } |
| | 426 | |
|
| | 427 | | internal void ConfigureManualButtonsEvents() |
| | 428 | | { |
| 77 | 429 | | if (previousButton != null) |
| | 430 | | { |
| 77 | 431 | | previousButton.onClick.RemoveAllListeners(); |
| 77 | 432 | | previousButton.onClick.AddListener(GoToPreviousItem); |
| | 433 | | } |
| | 434 | |
|
| 77 | 435 | | if (nextButton != null) |
| | 436 | | { |
| 77 | 437 | | nextButton.onClick.RemoveAllListeners(); |
| 77 | 438 | | nextButton.onClick.AddListener(GoToNextItem); |
| | 439 | | } |
| 77 | 440 | | } |
| | 441 | |
|
| | 442 | | internal void CreateItem(BaseComponentView newItem, string name) |
| | 443 | | { |
| 36 | 444 | | if (newItem == null) |
| 0 | 445 | | return; |
| | 446 | |
|
| 36 | 447 | | newItem.transform.SetParent(itemsContainer); |
| 36 | 448 | | newItem.transform.localPosition = Vector3.zero; |
| 36 | 449 | | newItem.transform.localScale = Vector3.one; |
| 36 | 450 | | newItem.name = name; |
| | 451 | |
|
| 36 | 452 | | instantiatedItems.Add(newItem); |
| | 453 | |
|
| 36 | 454 | | ResizeItem((RectTransform)newItem.transform); |
| 36 | 455 | | } |
| | 456 | |
|
| | 457 | | internal void ResizeItem(RectTransform item) |
| | 458 | | { |
| 36 | 459 | | ((RectTransform)item.transform).sizeDelta = new Vector2(viewport.rect.width, viewport.rect.height); |
| | 460 | |
|
| 36 | 461 | | int currentNumberOfItems = itemsContainer.childCount; |
| 36 | 462 | | itemsContainer.offsetMin = Vector2.zero; |
| 36 | 463 | | float extraSpace = (currentNumberOfItems - 1) * model.spaceBetweenItems; |
| 36 | 464 | | itemsContainer.offsetMax = new Vector2(viewport.rect.width * (currentNumberOfItems - 1) + extraSpace, 0); |
| 36 | 465 | | } |
| | 466 | |
|
| | 467 | | internal void ResizeAllItems() |
| | 468 | | { |
| 13 | 469 | | if (itemsScroll.horizontalNormalizedPosition != 0f) |
| 0 | 470 | | itemsScroll.horizontalNormalizedPosition = 0f; |
| | 471 | |
|
| 13 | 472 | | if (model.automaticTransition) |
| 13 | 473 | | StartCarousel(); |
| | 474 | |
|
| 26 | 475 | | foreach (Transform child in itemsContainer) |
| | 476 | | { |
| 0 | 477 | | ResizeItem((RectTransform)child); |
| | 478 | | } |
| 13 | 479 | | } |
| | 480 | |
|
| | 481 | | internal void DestroyInstantiatedItems() |
| | 482 | | { |
| 270 | 483 | | List<BaseComponentView> itemsToDestroy = ExtractItems(); |
| | 484 | |
|
| 704 | 485 | | foreach (BaseComponentView itemToDestroy in itemsToDestroy) |
| | 486 | | { |
| 82 | 487 | | if (itemToDestroy != null) |
| 82 | 488 | | DestroyImmediate(itemToDestroy.gameObject); |
| | 489 | | } |
| | 490 | |
|
| 270 | 491 | | itemsToDestroy.Clear(); |
| | 492 | |
|
| 270 | 493 | | instantiatedItems.Clear(); |
| | 494 | |
|
| 270 | 495 | | itemsContainer.offsetMin = Vector2.zero; |
| 270 | 496 | | itemsContainer.offsetMax = Vector2.zero; |
| 270 | 497 | | } |
| | 498 | |
|
| | 499 | | internal IEnumerator RunCarouselCoroutine( |
| | 500 | | int fromIndex = 0, |
| | 501 | | bool startInmediately = false, |
| | 502 | | CarouselDirection direction = CarouselDirection.Right, |
| | 503 | | bool changeDirectionAfterFirstTransition = false, |
| | 504 | | int numberOfInitialJumps = 1) |
| | 505 | | { |
| 53 | 506 | | currentItemIndex = fromIndex; |
| 53 | 507 | | SetSelectedDot(currentItemIndex); |
| 53 | 508 | | bool continueCarrousel = true; |
| 53 | 509 | | while (gameObject.activeInHierarchy && itemsContainer.childCount > 1 && continueCarrousel) |
| | 510 | | { |
| 6 | 511 | | float elapsedTime = 0f; |
| | 512 | |
|
| 6 | 513 | | if (!startInmediately) |
| | 514 | | { |
| 1 | 515 | | while (elapsedTime < model.timeBetweenItems) |
| | 516 | | { |
| 1 | 517 | | if (!model.pauseOnFocus || (model.pauseOnFocus && !isFocused)) |
| 1 | 518 | | elapsedTime += Time.deltaTime; |
| | 519 | |
|
| 1 | 520 | | yield return null; |
| | 521 | | } |
| | 522 | |
|
| | 523 | | } |
| | 524 | |
|
| 5 | 525 | | if (instantiatedItems.Count > 0) |
| | 526 | | { |
| 5 | 527 | | if (direction == CarouselDirection.Right) |
| | 528 | | { |
| 3 | 529 | | SetSelectedDot(currentItemIndex == (instantiatedItems.Count - 1) ? 0 : currentItemIndex + numberOfIn |
| 3 | 530 | | yield return RunRightAnimation(numberOfInitialJumps); |
| | 531 | |
|
| 0 | 532 | | if (changeDirectionAfterFirstTransition) |
| | 533 | | { |
| 0 | 534 | | direction = CarouselDirection.Left; |
| 0 | 535 | | changeDirectionAfterFirstTransition = false; |
| | 536 | | } |
| 0 | 537 | | continueCarrousel = model.automaticTransition; |
| | 538 | | } |
| | 539 | | else |
| | 540 | | { |
| 2 | 541 | | SetSelectedDot(currentItemIndex == 0 ? (instantiatedItems.Count - 1) : currentItemIndex - numberOfIn |
| 2 | 542 | | yield return RunLeftAnimation(numberOfInitialJumps); |
| | 543 | |
|
| 0 | 544 | | if (changeDirectionAfterFirstTransition) |
| | 545 | | { |
| 0 | 546 | | direction = CarouselDirection.Right; |
| 0 | 547 | | changeDirectionAfterFirstTransition = false; |
| | 548 | | } |
| 0 | 549 | | continueCarrousel = model.automaticTransition; |
| | 550 | | } |
| | 551 | | } |
| | 552 | |
|
| 0 | 553 | | startInmediately = false; |
| 0 | 554 | | numberOfInitialJumps = 1; |
| | 555 | | } |
| 47 | 556 | | } |
| | 557 | |
|
| | 558 | | internal IEnumerator RunRightAnimation(int numberOfJumps = 1) |
| | 559 | | { |
| 3 | 560 | | if (currentItemIndex == instantiatedItems.Count - 1) |
| | 561 | | { |
| 0 | 562 | | currentItemIndex = 0; |
| 0 | 563 | | yield return RunAnimationCoroutine(CarouselDirection.Left, instantiatedItems.Count - 1); |
| | 564 | | } |
| | 565 | | else |
| | 566 | | { |
| 3 | 567 | | currentItemIndex += numberOfJumps; |
| 3 | 568 | | yield return RunAnimationCoroutine(CarouselDirection.Right, numberOfJumps); |
| | 569 | | } |
| 0 | 570 | | } |
| | 571 | |
|
| | 572 | | internal IEnumerator RunLeftAnimation(int numberOfJumps = 1) |
| | 573 | | { |
| 2 | 574 | | if (currentItemIndex == 0) |
| | 575 | | { |
| 2 | 576 | | currentItemIndex = instantiatedItems.Count - 1; |
| 2 | 577 | | yield return RunAnimationCoroutine(CarouselDirection.Right, instantiatedItems.Count - 1); |
| | 578 | | } |
| | 579 | | else |
| | 580 | | { |
| 0 | 581 | | currentItemIndex -= numberOfJumps; |
| 0 | 582 | | yield return RunAnimationCoroutine(CarouselDirection.Left, numberOfJumps); |
| | 583 | | } |
| 0 | 584 | | } |
| | 585 | |
|
| | 586 | | internal IEnumerator RunAnimationCoroutine(CarouselDirection direction, int numberOfJumps = 1) |
| | 587 | | { |
| 5 | 588 | | isInTransition = true; |
| 5 | 589 | | float currentAnimationTime = 0f; |
| 5 | 590 | | float initialNormalizedPos = itemsScroll.horizontalNormalizedPosition; |
| | 591 | |
|
| 5 | 592 | | if (direction == CarouselDirection.Right) |
| 5 | 593 | | currentFinalNormalizedPos = initialNormalizedPos + ((float)numberOfJumps / (instantiatedItems.Count - 1)); |
| | 594 | | else |
| 0 | 595 | | currentFinalNormalizedPos = initialNormalizedPos - ((float)numberOfJumps / (instantiatedItems.Count - 1)); |
| | 596 | |
|
| 5 | 597 | | while (currentAnimationTime <= model.animationTransitionTime) |
| | 598 | | { |
| 5 | 599 | | itemsScroll.horizontalNormalizedPosition = Mathf.Clamp01(Mathf.Lerp( |
| | 600 | | initialNormalizedPos, |
| | 601 | | currentFinalNormalizedPos, |
| | 602 | | model.animationCurve.Evaluate(currentAnimationTime / model.animationTransitionTime))); |
| | 603 | |
|
| 5 | 604 | | currentAnimationTime += Time.deltaTime; |
| | 605 | |
|
| 5 | 606 | | yield return null; |
| | 607 | | } |
| | 608 | |
|
| 0 | 609 | | itemsScroll.horizontalNormalizedPosition = currentFinalNormalizedPos; |
| 0 | 610 | | isInTransition = false; |
| 0 | 611 | | } |
| | 612 | |
|
| | 613 | | public void GenerateDotsSelector() |
| | 614 | | { |
| 27 | 615 | | List<GameObject> dotsToRemove = new List<GameObject>(); |
| 124 | 616 | | foreach (Transform child in dotsSelector.transform) |
| | 617 | | { |
| 35 | 618 | | if (child.gameObject == dotButtonTemplate.gameObject) |
| | 619 | | continue; |
| | 620 | |
|
| 8 | 621 | | dotsToRemove.Add(child.gameObject); |
| | 622 | | } |
| | 623 | |
|
| 70 | 624 | | foreach (GameObject dotToRemove in dotsToRemove) |
| | 625 | | { |
| 8 | 626 | | Utils.SafeDestroy(dotToRemove); |
| | 627 | | } |
| | 628 | |
|
| 130 | 629 | | for (int i = 0; i < itemsContainer.childCount; i++) |
| | 630 | | { |
| 38 | 631 | | Button newDotButton = Instantiate(dotButtonTemplate, dotsSelector.transform); |
| 38 | 632 | | newDotButton.gameObject.SetActive(true); |
| 38 | 633 | | newDotButton.onClick.AddListener(() => |
| | 634 | | { |
| 0 | 635 | | int dotButtonIndex = newDotButton.transform.GetSiblingIndex() - 1; |
| 0 | 636 | | if (dotButtonIndex != currentDotIndex) |
| | 637 | | { |
| 0 | 638 | | MakeJumpFromDotsSelector( |
| | 639 | | Mathf.Abs(dotButtonIndex - currentDotIndex), |
| | 640 | | dotButtonIndex > currentDotIndex ? CarouselDirection.Right : CarouselDirection.Left); |
| | 641 | | } |
| 0 | 642 | | }); |
| | 643 | | } |
| | 644 | |
|
| 27 | 645 | | SetSelectedDot(0); |
| 27 | 646 | | } |
| | 647 | |
|
| | 648 | | internal void SetSelectedDot(int index) |
| | 649 | | { |
| 86 | 650 | | int currentIndex = 0; |
| 86 | 651 | | currentDotIndex = -1; |
| 558 | 652 | | foreach (Transform child in dotsSelector.transform) |
| | 653 | | { |
| 193 | 654 | | if (child.gameObject == dotButtonTemplate.gameObject) |
| | 655 | | continue; |
| | 656 | |
|
| 107 | 657 | | if (currentIndex == index) |
| | 658 | | { |
| 72 | 659 | | child.GetComponent<Image>().color = dotSelectedColor; |
| 72 | 660 | | child.transform.localScale = Vector3.one * 1.5f; |
| 72 | 661 | | currentDotIndex = index; |
| | 662 | | } |
| | 663 | | else |
| | 664 | | { |
| 35 | 665 | | child.GetComponent<Image>().color = dotUnselectedColor; |
| 35 | 666 | | child.transform.localScale = Vector3.one; |
| | 667 | | } |
| | 668 | |
|
| 107 | 669 | | currentIndex++; |
| | 670 | | } |
| 86 | 671 | | } |
| | 672 | |
|
| | 673 | | public void ResetManualCarousel() |
| | 674 | | { |
| 0 | 675 | | isInTransition = false; |
| 0 | 676 | | currentItemIndex = 0; |
| 0 | 677 | | SetSelectedDot(0); |
| 0 | 678 | | } |
| | 679 | |
|
| | 680 | | internal IEnumerator RegisterCurrentInstantiatedItems() |
| | 681 | | { |
| 75 | 682 | | instantiatedItems.Clear(); |
| | 683 | |
|
| 252 | 684 | | foreach (Transform child in itemsContainer) |
| | 685 | | { |
| 51 | 686 | | BaseComponentView existingItem = child.GetComponent<BaseComponentView>(); |
| 51 | 687 | | if (existingItem != null) |
| 51 | 688 | | instantiatedItems.Add(existingItem); |
| | 689 | | else |
| 0 | 690 | | Destroy(child.gameObject); |
| | 691 | | } |
| | 692 | |
|
| | 693 | | // In the first loading, before calculating the size of the current items, it is needed to wait for a frame in o |
| | 694 | | // allow time for the carousel viewport to get its final size to be able to execute the 'ResizeAllItems' functio |
| 75 | 695 | | yield return null; |
| | 696 | |
|
| 6 | 697 | | ResizeAllItems(); |
| 6 | 698 | | SetManualControlsActive(model.showManualControls); |
| 6 | 699 | | GenerateDotsSelector(); |
| 6 | 700 | | } |
| | 701 | | } |