| | 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 | |
|
| | 148 | | [Header("Configuration")] |
| | 149 | | [SerializeField] internal CarouselComponentModel model; |
| | 150 | |
|
| 164 | 151 | | internal List<BaseComponentView> instantiatedItems = new List<BaseComponentView>(); |
| | 152 | | internal Coroutine itemsCoroutine; |
| | 153 | | internal int currentItemIndex = 0; |
| | 154 | | internal int currentDotIndex = 0; |
| | 155 | | internal float currentFinalNormalizedPos; |
| | 156 | | internal bool isInTransition = false; |
| | 157 | |
|
| | 158 | | public override void Awake() |
| | 159 | | { |
| 156 | 160 | | base.Awake(); |
| | 161 | |
|
| 156 | 162 | | StartCoroutine(RegisterCurrentInstantiatedItems()); |
| 156 | 163 | | ConfigureManualButtonsEvents(); |
| 156 | 164 | | } |
| | 165 | |
|
| | 166 | | public override void Start() |
| | 167 | | { |
| 87 | 168 | | if (model.automaticTransition) |
| 83 | 169 | | StartCarousel(); |
| 87 | 170 | | } |
| | 171 | |
|
| | 172 | | public void Configure(CarouselComponentModel newModel) |
| | 173 | | { |
| 1 | 174 | | model = newModel; |
| 1 | 175 | | RefreshControl(); |
| 1 | 176 | | } |
| | 177 | |
|
| | 178 | | public override void RefreshControl() |
| | 179 | | { |
| 1 | 180 | | if (model == null) |
| 0 | 181 | | return; |
| | 182 | |
|
| 1 | 183 | | SetSpaceBetweenItems(model.spaceBetweenItems); |
| 1 | 184 | | SetTimeBetweenItems(model.timeBetweenItems); |
| 1 | 185 | | SetAnimationTransitionTime(model.animationTransitionTime); |
| 1 | 186 | | SetAnimationCurve(model.animationCurve); |
| 1 | 187 | | SetBackgroundColor(model.backgroundColor); |
| 1 | 188 | | SetManualControlsActive(model.showManualControls); |
| 1 | 189 | | ResizeAllItems(); |
| 1 | 190 | | GenerateDotsSelector(); |
| 1 | 191 | | } |
| | 192 | |
|
| | 193 | | public override void OnScreenSizeChanged() |
| | 194 | | { |
| 26 | 195 | | base.OnScreenSizeChanged(); |
| | 196 | |
|
| 26 | 197 | | ResizeAllItems(); |
| 26 | 198 | | } |
| | 199 | |
|
| | 200 | | public override void Dispose() |
| | 201 | | { |
| 458 | 202 | | base.Dispose(); |
| | 203 | |
|
| 458 | 204 | | StopCarousel(); |
| 458 | 205 | | DestroyInstantiatedItems(); |
| 458 | 206 | | } |
| | 207 | |
|
| | 208 | | public void SetSpaceBetweenItems(float newSpace) |
| | 209 | | { |
| 2 | 210 | | model.spaceBetweenItems = newSpace; |
| | 211 | |
|
| 2 | 212 | | if (horizontalLayout == null) |
| 0 | 213 | | return; |
| | 214 | |
|
| 2 | 215 | | horizontalLayout.spacing = newSpace; |
| 2 | 216 | | } |
| | 217 | |
|
| 0 | 218 | | public void SetTimeBetweenItems(float newTime) { model.timeBetweenItems = newTime; } |
| | 219 | |
|
| 0 | 220 | | public void SetAnimationTransitionTime(float newTime) { model.animationTransitionTime = newTime; } |
| | 221 | |
|
| 0 | 222 | | public void SetAnimationCurve(AnimationCurve newCurve) { model.animationCurve = newCurve; } |
| | 223 | |
|
| | 224 | | public void SetBackgroundColor(Color newColor) |
| | 225 | | { |
| 2 | 226 | | model.backgroundColor = newColor; |
| | 227 | |
|
| 2 | 228 | | if (background == null) |
| 2 | 229 | | return; |
| | 230 | |
|
| 0 | 231 | | background.color = newColor; |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | public void SetManualControlsActive() => |
| 0 | 235 | | SetManualControlsActive(model.showManualControls); |
| | 236 | |
|
| | 237 | | public void SetManualControlsActive(bool isActived) |
| | 238 | | { |
| 792 | 239 | | model.showManualControls = isActived; |
| | 240 | |
|
| 792 | 241 | | if (previousButton == null || nextButton == null) |
| 0 | 242 | | return; |
| | 243 | |
|
| 792 | 244 | | int currentNumberOfItems = itemsContainer.childCount; |
| 792 | 245 | | previousButton.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 792 | 246 | | nextButton.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 792 | 247 | | dotsSelector.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 792 | 248 | | } |
| | 249 | |
|
| | 250 | | public void SetItems(BaseComponentView prefab, int amountOfItems) |
| | 251 | | { |
| 0 | 252 | | DestroyInstantiatedItems(); |
| | 253 | |
|
| 0 | 254 | | for (int i = 0; i < amountOfItems; i++) |
| | 255 | | { |
| 0 | 256 | | BaseComponentView instanciatedItem = Instantiate(prefab); |
| 0 | 257 | | CreateItem(instanciatedItem, $"Item{i}"); |
| | 258 | | } |
| | 259 | |
|
| 0 | 260 | | SetManualControlsActive(model.showManualControls); |
| 0 | 261 | | GenerateDotsSelector(); |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | public void SetItems(List<BaseComponentView> items) |
| | 265 | | { |
| 15 | 266 | | DestroyInstantiatedItems(); |
| | 267 | |
|
| 92 | 268 | | for (int i = 0; i < items.Count; i++) |
| | 269 | | { |
| 31 | 270 | | CreateItem(items[i], $"Item{i}"); |
| | 271 | | } |
| | 272 | |
|
| 15 | 273 | | SetManualControlsActive(model.showManualControls); |
| 15 | 274 | | GenerateDotsSelector(); |
| 15 | 275 | | } |
| | 276 | |
|
| | 277 | | public void AddItemWithDotsSelector(BaseComponentView item) |
| | 278 | | { |
| 2 | 279 | | CreateItem(item, $"Item{instantiatedItems.Count}"); |
| 2 | 280 | | SetManualControlsActive(model.showManualControls); |
| 2 | 281 | | GenerateDotsSelector(); |
| 2 | 282 | | } |
| | 283 | |
|
| | 284 | | public void AddItem(BaseComponentView item) => |
| 6 | 285 | | CreateItem(item, $"Item{instantiatedItems.Count}"); |
| | 286 | |
|
| | 287 | | public void RemoveItem(BaseComponentView item) |
| | 288 | | { |
| 2 | 289 | | BaseComponentView itemToRemove = instantiatedItems.FirstOrDefault(x => x == item); |
| 1 | 290 | | if (itemToRemove != null) |
| | 291 | | { |
| 1 | 292 | | Destroy(itemToRemove.gameObject); |
| 1 | 293 | | instantiatedItems.Remove(item); |
| | 294 | | } |
| | 295 | |
|
| 1 | 296 | | SetManualControlsActive(model.showManualControls); |
| 1 | 297 | | GenerateDotsSelector(); |
| 1 | 298 | | } |
| | 299 | |
|
| 0 | 300 | | public List<BaseComponentView> GetItems() { return instantiatedItems; } |
| | 301 | |
|
| | 302 | | public List<BaseComponentView> ExtractItems() |
| | 303 | | { |
| 634 | 304 | | List<BaseComponentView> extractedItems = new List<BaseComponentView>(); |
| 1874 | 305 | | foreach (BaseComponentView item in instantiatedItems) |
| | 306 | | { |
| 303 | 307 | | if (item != null) |
| 303 | 308 | | item.transform.SetParent(null); |
| | 309 | |
|
| 303 | 310 | | extractedItems.Add(item); |
| | 311 | | } |
| | 312 | |
|
| 634 | 313 | | instantiatedItems.Clear(); |
| 634 | 314 | | SetManualControlsActive(model.showManualControls); |
| | 315 | |
|
| 634 | 316 | | return extractedItems; |
| | 317 | | } |
| | 318 | |
|
| | 319 | | public void RemoveItems() |
| | 320 | | { |
| 120 | 321 | | DestroyInstantiatedItems(); |
| 120 | 322 | | SetManualControlsActive(model.showManualControls); |
| 120 | 323 | | } |
| | 324 | |
|
| | 325 | | public void StartCarousel( |
| | 326 | | int fromIndex = 0, |
| | 327 | | bool startInmediately = false, |
| | 328 | | CarouselDirection direction = CarouselDirection.Right, |
| | 329 | | bool changeDirectionAfterFirstTransition = false, |
| | 330 | | int numberOfInitialJumps = 1) |
| | 331 | | { |
| 137 | 332 | | StopCarousel(); |
| | 333 | |
|
| 137 | 334 | | if (isActiveAndEnabled) |
| 137 | 335 | | itemsCoroutine = StartCoroutine(RunCarouselCoroutine(fromIndex, startInmediately, direction, changeDirection |
| 137 | 336 | | } |
| | 337 | |
|
| | 338 | | public void StopCarousel() |
| | 339 | | { |
| 596 | 340 | | if (itemsCoroutine == null) |
| 586 | 341 | | return; |
| | 342 | |
|
| 10 | 343 | | StopCoroutine(itemsCoroutine); |
| | 344 | |
|
| 10 | 345 | | itemsCoroutine = null; |
| 10 | 346 | | isInTransition = false; |
| 10 | 347 | | } |
| | 348 | |
|
| | 349 | | public void GoToPreviousItem() |
| | 350 | | { |
| 3 | 351 | | if (isInTransition) |
| 0 | 352 | | return; |
| | 353 | |
|
| 3 | 354 | | StartCarousel( |
| | 355 | | fromIndex: currentItemIndex, |
| | 356 | | startInmediately: true, |
| | 357 | | direction: CarouselDirection.Left, |
| | 358 | | changeDirectionAfterFirstTransition: true, |
| | 359 | | numberOfInitialJumps: 1); |
| 3 | 360 | | } |
| | 361 | |
|
| | 362 | | public void ResetCarousel() |
| | 363 | | { |
| 0 | 364 | | int index = 0; |
| 0 | 365 | | SetSelectedDot(index); |
| | 366 | |
|
| 0 | 367 | | } |
| | 368 | |
|
| | 369 | | public void GoToNextItem() |
| | 370 | | { |
| 5 | 371 | | if (isInTransition) |
| 0 | 372 | | return; |
| | 373 | |
|
| 5 | 374 | | StartCarousel( |
| | 375 | | fromIndex: currentItemIndex, |
| | 376 | | startInmediately: true, |
| | 377 | | direction: CarouselDirection.Right, |
| | 378 | | changeDirectionAfterFirstTransition: false, |
| | 379 | | numberOfInitialJumps: 1); |
| 5 | 380 | | } |
| | 381 | |
|
| | 382 | | public void MakeJumpFromDotsSelector(int numberOfJumps, CarouselDirection direction) |
| | 383 | | { |
| 1 | 384 | | if (isInTransition) |
| 0 | 385 | | return; |
| | 386 | |
|
| 1 | 387 | | StartCarousel( |
| | 388 | | fromIndex: currentItemIndex, |
| | 389 | | startInmediately: true, |
| | 390 | | direction: direction, |
| | 391 | | changeDirectionAfterFirstTransition: direction == CarouselDirection.Left, |
| | 392 | | numberOfInitialJumps: numberOfJumps); |
| 1 | 393 | | } |
| | 394 | |
|
| | 395 | | internal void ConfigureManualButtonsEvents() |
| | 396 | | { |
| 158 | 397 | | if (previousButton != null) |
| | 398 | | { |
| 158 | 399 | | previousButton.onClick.RemoveAllListeners(); |
| 158 | 400 | | previousButton.onClick.AddListener(GoToPreviousItem); |
| | 401 | | } |
| | 402 | |
|
| 158 | 403 | | if (nextButton != null) |
| | 404 | | { |
| 158 | 405 | | nextButton.onClick.RemoveAllListeners(); |
| 158 | 406 | | nextButton.onClick.AddListener(GoToNextItem); |
| | 407 | | } |
| 158 | 408 | | } |
| | 409 | |
|
| | 410 | | internal void CreateItem(BaseComponentView newItem, string name) |
| | 411 | | { |
| 40 | 412 | | if (newItem == null) |
| 0 | 413 | | return; |
| | 414 | |
|
| 40 | 415 | | newItem.transform.SetParent(itemsContainer); |
| 40 | 416 | | newItem.transform.localPosition = Vector3.zero; |
| 40 | 417 | | newItem.transform.localScale = Vector3.one; |
| 40 | 418 | | newItem.name = name; |
| | 419 | |
|
| 40 | 420 | | instantiatedItems.Add(newItem); |
| | 421 | |
|
| 40 | 422 | | ResizeItem((RectTransform)newItem.transform); |
| 40 | 423 | | } |
| | 424 | |
|
| | 425 | | internal void ResizeItem(RectTransform item) |
| | 426 | | { |
| 40 | 427 | | ((RectTransform)item.transform).sizeDelta = new Vector2(viewport.rect.width, viewport.rect.height); |
| | 428 | |
|
| 40 | 429 | | int currentNumberOfItems = itemsContainer.childCount; |
| 40 | 430 | | itemsContainer.offsetMin = Vector2.zero; |
| 40 | 431 | | float extraSpace = (currentNumberOfItems - 1) * model.spaceBetweenItems; |
| 40 | 432 | | itemsContainer.offsetMax = new Vector2(viewport.rect.width * (currentNumberOfItems - 1) + extraSpace, 0); |
| 40 | 433 | | } |
| | 434 | |
|
| | 435 | | internal void ResizeAllItems() |
| | 436 | | { |
| 44 | 437 | | if (itemsScroll.horizontalNormalizedPosition != 0f) |
| 0 | 438 | | itemsScroll.horizontalNormalizedPosition = 0f; |
| | 439 | |
|
| 44 | 440 | | if (model.automaticTransition) |
| 44 | 441 | | StartCarousel(); |
| | 442 | |
|
| 88 | 443 | | foreach (Transform child in itemsContainer) |
| | 444 | | { |
| 0 | 445 | | ResizeItem((RectTransform)child); |
| | 446 | | } |
| 44 | 447 | | } |
| | 448 | |
|
| | 449 | | internal void DestroyInstantiatedItems() |
| | 450 | | { |
| 593 | 451 | | List<BaseComponentView> itemsToDestroy = ExtractItems(); |
| | 452 | |
|
| 1776 | 453 | | foreach (BaseComponentView itemToDestroy in itemsToDestroy) |
| | 454 | | { |
| 295 | 455 | | if (itemToDestroy != null) |
| 295 | 456 | | DestroyImmediate(itemToDestroy.gameObject); |
| | 457 | | } |
| | 458 | |
|
| 593 | 459 | | itemsToDestroy.Clear(); |
| | 460 | |
|
| 593 | 461 | | instantiatedItems.Clear(); |
| | 462 | |
|
| 593 | 463 | | itemsContainer.offsetMin = Vector2.zero; |
| 593 | 464 | | itemsContainer.offsetMax = Vector2.zero; |
| 593 | 465 | | } |
| | 466 | |
|
| | 467 | | internal IEnumerator RunCarouselCoroutine( |
| | 468 | | int fromIndex = 0, |
| | 469 | | bool startInmediately = false, |
| | 470 | | CarouselDirection direction = CarouselDirection.Right, |
| | 471 | | bool changeDirectionAfterFirstTransition = false, |
| | 472 | | int numberOfInitialJumps = 1) |
| | 473 | | { |
| 137 | 474 | | currentItemIndex = fromIndex; |
| 137 | 475 | | SetSelectedDot(currentItemIndex); |
| | 476 | |
|
| 137 | 477 | | bool continueCarrousel = true; |
| 137 | 478 | | while (gameObject.activeInHierarchy && itemsContainer.childCount > 1 && continueCarrousel) |
| | 479 | | { |
| 10 | 480 | | float elapsedTime = 0f; |
| | 481 | |
|
| 10 | 482 | | if (!startInmediately) |
| | 483 | | { |
| 1 | 484 | | while (elapsedTime < model.timeBetweenItems) |
| | 485 | | { |
| 1 | 486 | | if (!model.pauseOnFocus || (model.pauseOnFocus && !isFocused)) |
| 1 | 487 | | elapsedTime += Time.deltaTime; |
| | 488 | |
|
| 1 | 489 | | yield return null; |
| | 490 | | } |
| | 491 | |
|
| | 492 | | } |
| | 493 | |
|
| 9 | 494 | | if (instantiatedItems.Count > 0) |
| | 495 | | { |
| 9 | 496 | | if (direction == CarouselDirection.Right) |
| | 497 | | { |
| 6 | 498 | | SetSelectedDot(currentItemIndex == (instantiatedItems.Count - 1) ? 0 : currentItemIndex + numberOfIn |
| 6 | 499 | | yield return RunRightAnimation(numberOfInitialJumps); |
| | 500 | |
|
| 0 | 501 | | if (changeDirectionAfterFirstTransition) |
| | 502 | | { |
| 0 | 503 | | direction = CarouselDirection.Left; |
| 0 | 504 | | changeDirectionAfterFirstTransition = false; |
| | 505 | | } |
| 0 | 506 | | continueCarrousel = model.automaticTransition; |
| 0 | 507 | | } |
| | 508 | | else |
| | 509 | | { |
| 3 | 510 | | SetSelectedDot(currentItemIndex == 0 ? (instantiatedItems.Count - 1) : currentItemIndex - numberOfIn |
| 3 | 511 | | yield return RunLeftAnimation(numberOfInitialJumps); |
| | 512 | |
|
| 0 | 513 | | if (changeDirectionAfterFirstTransition) |
| | 514 | | { |
| 0 | 515 | | direction = CarouselDirection.Right; |
| 0 | 516 | | changeDirectionAfterFirstTransition = false; |
| | 517 | | } |
| 0 | 518 | | continueCarrousel = model.automaticTransition; |
| | 519 | | } |
| | 520 | | } |
| | 521 | |
|
| 0 | 522 | | startInmediately = false; |
| 0 | 523 | | numberOfInitialJumps = 1; |
| | 524 | | } |
| 127 | 525 | | } |
| | 526 | |
|
| | 527 | | internal IEnumerator RunRightAnimation(int numberOfJumps = 1) |
| | 528 | | { |
| 6 | 529 | | if (currentItemIndex == instantiatedItems.Count - 1) |
| | 530 | | { |
| 0 | 531 | | currentItemIndex = 0; |
| 0 | 532 | | yield return RunAnimationCoroutine(CarouselDirection.Left, instantiatedItems.Count - 1); |
| 0 | 533 | | } |
| | 534 | | else |
| | 535 | | { |
| 6 | 536 | | currentItemIndex += numberOfJumps; |
| 6 | 537 | | yield return RunAnimationCoroutine(CarouselDirection.Right, numberOfJumps); |
| | 538 | | } |
| 0 | 539 | | } |
| | 540 | |
|
| | 541 | | internal IEnumerator RunLeftAnimation(int numberOfJumps = 1) |
| | 542 | | { |
| 3 | 543 | | if (currentItemIndex == 0) |
| | 544 | | { |
| 3 | 545 | | currentItemIndex = instantiatedItems.Count - 1; |
| 3 | 546 | | yield return RunAnimationCoroutine(CarouselDirection.Right, instantiatedItems.Count - 1); |
| 0 | 547 | | } |
| | 548 | | else |
| | 549 | | { |
| 0 | 550 | | currentItemIndex -= numberOfJumps; |
| 0 | 551 | | yield return RunAnimationCoroutine(CarouselDirection.Left, numberOfJumps); |
| | 552 | | } |
| 0 | 553 | | } |
| | 554 | |
|
| | 555 | | internal IEnumerator RunAnimationCoroutine(CarouselDirection direction, int numberOfJumps = 1) |
| | 556 | | { |
| 9 | 557 | | isInTransition = true; |
| 9 | 558 | | float currentAnimationTime = 0f; |
| 9 | 559 | | float initialNormalizedPos = itemsScroll.horizontalNormalizedPosition; |
| | 560 | |
|
| 9 | 561 | | if (direction == CarouselDirection.Right) |
| 9 | 562 | | currentFinalNormalizedPos = initialNormalizedPos + ((float)numberOfJumps / (instantiatedItems.Count - 1)); |
| | 563 | | else |
| 0 | 564 | | currentFinalNormalizedPos = initialNormalizedPos - ((float)numberOfJumps / (instantiatedItems.Count - 1)); |
| | 565 | |
|
| 9 | 566 | | while (currentAnimationTime <= model.animationTransitionTime) |
| | 567 | | { |
| 9 | 568 | | itemsScroll.horizontalNormalizedPosition = Mathf.Clamp01(Mathf.Lerp( |
| | 569 | | initialNormalizedPos, |
| | 570 | | currentFinalNormalizedPos, |
| | 571 | | model.animationCurve.Evaluate(currentAnimationTime / model.animationTransitionTime))); |
| | 572 | |
|
| 9 | 573 | | currentAnimationTime += Time.deltaTime; |
| | 574 | |
|
| 9 | 575 | | yield return null; |
| | 576 | | } |
| | 577 | |
|
| 0 | 578 | | itemsScroll.horizontalNormalizedPosition = currentFinalNormalizedPos; |
| 0 | 579 | | isInTransition = false; |
| 0 | 580 | | } |
| | 581 | |
|
| | 582 | | public void GenerateDotsSelector() |
| | 583 | | { |
| 38 | 584 | | List<GameObject> dotsToRemove = new List<GameObject>(); |
| 226 | 585 | | foreach (Transform child in dotsSelector.transform) |
| | 586 | | { |
| 75 | 587 | | if (child.gameObject == dotButtonTemplate.gameObject) |
| | 588 | | continue; |
| | 589 | |
|
| 37 | 590 | | dotsToRemove.Add(child.gameObject); |
| | 591 | | } |
| | 592 | |
|
| 150 | 593 | | foreach (GameObject dotToRemove in dotsToRemove) |
| | 594 | | { |
| 37 | 595 | | Utils.SafeDestroy(dotToRemove); |
| | 596 | | } |
| | 597 | |
|
| 152 | 598 | | for (int i = 0; i < itemsContainer.childCount; i++) |
| | 599 | | { |
| 38 | 600 | | Button newDotButton = Instantiate(dotButtonTemplate, dotsSelector.transform); |
| 38 | 601 | | newDotButton.gameObject.SetActive(true); |
| 38 | 602 | | newDotButton.onClick.AddListener(() => |
| | 603 | | { |
| 0 | 604 | | int dotButtonIndex = newDotButton.transform.GetSiblingIndex() - 1; |
| 0 | 605 | | if (dotButtonIndex != currentDotIndex) |
| | 606 | | { |
| 0 | 607 | | MakeJumpFromDotsSelector( |
| | 608 | | Mathf.Abs(dotButtonIndex - currentDotIndex), |
| | 609 | | dotButtonIndex > currentDotIndex ? CarouselDirection.Right : CarouselDirection.Left); |
| | 610 | | } |
| 0 | 611 | | }); |
| | 612 | | } |
| | 613 | |
|
| 38 | 614 | | SetSelectedDot(0); |
| 38 | 615 | | } |
| | 616 | |
|
| | 617 | | internal void SetSelectedDot(int index) |
| | 618 | | { |
| 185 | 619 | | int currentIndex = 0; |
| 185 | 620 | | currentDotIndex = -1; |
| 1426 | 621 | | foreach (Transform child in dotsSelector.transform) |
| | 622 | | { |
| 528 | 623 | | if (child.gameObject == dotButtonTemplate.gameObject) |
| | 624 | | continue; |
| | 625 | |
|
| 343 | 626 | | if (currentIndex == index) |
| | 627 | | { |
| 162 | 628 | | child.GetComponent<Image>().color = dotSelectedColor; |
| 162 | 629 | | child.transform.localScale = Vector3.one * 1.5f; |
| 162 | 630 | | currentDotIndex = index; |
| 162 | 631 | | } |
| | 632 | | else |
| | 633 | | { |
| 181 | 634 | | child.GetComponent<Image>().color = dotUnselectedColor; |
| 181 | 635 | | child.transform.localScale = Vector3.one; |
| | 636 | | } |
| | 637 | |
|
| 343 | 638 | | currentIndex++; |
| | 639 | | } |
| 185 | 640 | | } |
| | 641 | |
|
| | 642 | | internal IEnumerator RegisterCurrentInstantiatedItems() |
| | 643 | | { |
| 156 | 644 | | instantiatedItems.Clear(); |
| | 645 | |
|
| 840 | 646 | | foreach (Transform child in itemsContainer) |
| | 647 | | { |
| 264 | 648 | | BaseComponentView existingItem = child.GetComponent<BaseComponentView>(); |
| 264 | 649 | | if (existingItem != null) |
| 264 | 650 | | instantiatedItems.Add(existingItem); |
| | 651 | | else |
| 0 | 652 | | Destroy(child.gameObject); |
| | 653 | | } |
| | 654 | |
|
| | 655 | | // In the first loading, before calculating the size of the current items, it is needed to wait for a frame in o |
| | 656 | | // allow time for the carousel viewport to get its final size to be able to execute the 'ResizeAllItems' functio |
| 156 | 657 | | yield return null; |
| | 658 | |
|
| 17 | 659 | | ResizeAllItems(); |
| 17 | 660 | | SetManualControlsActive(model.showManualControls); |
| 17 | 661 | | GenerateDotsSelector(); |
| 17 | 662 | | } |
| | 663 | | } |