| | 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 | |
|
| 149 | 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 | | { |
| 142 | 160 | | base.Awake(); |
| | 161 | |
|
| 142 | 162 | | StartCoroutine(RegisterCurrentInstantiatedItems()); |
| 142 | 163 | | ConfigureManualButtonsEvents(); |
| 142 | 164 | | } |
| | 165 | |
|
| | 166 | | public override void Start() |
| | 167 | | { |
| 73 | 168 | | if (model.automaticTransition) |
| 73 | 169 | | StartCarousel(); |
| 73 | 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 | | { |
| 424 | 202 | | base.Dispose(); |
| | 203 | |
|
| 424 | 204 | | StopCarousel(); |
| 424 | 205 | | DestroyInstantiatedItems(); |
| 424 | 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 | |
|
| 4 | 218 | | public void SetTimeBetweenItems(float newTime) { model.timeBetweenItems = newTime; } |
| | 219 | |
|
| 4 | 220 | | public void SetAnimationTransitionTime(float newTime) { model.animationTransitionTime = newTime; } |
| | 221 | |
|
| 4 | 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 | | { |
| 738 | 239 | | model.showManualControls = isActived; |
| | 240 | |
|
| 738 | 241 | | if (previousButton == null || nextButton == null) |
| 0 | 242 | | return; |
| | 243 | |
|
| 738 | 244 | | int currentNumberOfItems = itemsContainer.childCount; |
| 738 | 245 | | previousButton.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 738 | 246 | | nextButton.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 738 | 247 | | dotsSelector.gameObject.SetActive(isActived && currentNumberOfItems > 1); |
| 738 | 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 | |
|
| 1 | 300 | | public List<BaseComponentView> GetItems() { return instantiatedItems; } |
| | 301 | |
|
| | 302 | | public List<BaseComponentView> ExtractItems() |
| | 303 | | { |
| 590 | 304 | | List<BaseComponentView> extractedItems = new List<BaseComponentView>(); |
| 1730 | 305 | | foreach (BaseComponentView item in instantiatedItems) |
| | 306 | | { |
| 275 | 307 | | if (item != null) |
| 275 | 308 | | item.transform.SetParent(null); |
| | 309 | |
|
| 275 | 310 | | extractedItems.Add(item); |
| | 311 | | } |
| | 312 | |
|
| 590 | 313 | | instantiatedItems.Clear(); |
| 590 | 314 | | SetManualControlsActive(model.showManualControls); |
| | 315 | |
|
| 590 | 316 | | return extractedItems; |
| | 317 | | } |
| | 318 | |
|
| | 319 | | public void RemoveItems() |
| | 320 | | { |
| 110 | 321 | | DestroyInstantiatedItems(); |
| 110 | 322 | | SetManualControlsActive(model.showManualControls); |
| 110 | 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 | | { |
| 123 | 332 | | StopCarousel(); |
| | 333 | |
|
| 123 | 334 | | if (isActiveAndEnabled) |
| 123 | 335 | | itemsCoroutine = StartCoroutine(RunCarouselCoroutine(fromIndex, startInmediately, direction, changeDirection |
| 123 | 336 | | } |
| | 337 | |
|
| | 338 | | public void StopCarousel() |
| | 339 | | { |
| 548 | 340 | | if (itemsCoroutine == null) |
| 542 | 341 | | return; |
| | 342 | |
|
| 6 | 343 | | StopCoroutine(itemsCoroutine); |
| | 344 | |
|
| 6 | 345 | | itemsCoroutine = null; |
| 6 | 346 | | isInTransition = false; |
| 6 | 347 | | } |
| | 348 | |
|
| | 349 | | public void GoToPreviousItem() |
| | 350 | | { |
| 2 | 351 | | if (isInTransition) |
| 0 | 352 | | return; |
| | 353 | |
|
| 2 | 354 | | StartCarousel( |
| | 355 | | fromIndex: currentItemIndex, |
| | 356 | | startInmediately: true, |
| | 357 | | direction: CarouselDirection.Left, |
| | 358 | | changeDirectionAfterFirstTransition: true, |
| | 359 | | numberOfInitialJumps: 1); |
| 2 | 360 | | } |
| | 361 | |
|
| | 362 | | public void ResetCarousel() |
| | 363 | | { |
| 0 | 364 | | int index = 0; |
| 0 | 365 | | SetSelectedDot(index); |
| 0 | 366 | | } |
| | 367 | |
|
| | 368 | | public void GoToNextItem() |
| | 369 | | { |
| 2 | 370 | | if (isInTransition) |
| 0 | 371 | | return; |
| | 372 | |
|
| 2 | 373 | | StartCarousel( |
| | 374 | | fromIndex: currentItemIndex, |
| | 375 | | startInmediately: true, |
| | 376 | | direction: CarouselDirection.Right, |
| | 377 | | changeDirectionAfterFirstTransition: false, |
| | 378 | | numberOfInitialJumps: 1); |
| 2 | 379 | | } |
| | 380 | |
|
| | 381 | | public void MakeJumpFromDotsSelector(int numberOfJumps, CarouselDirection direction) |
| | 382 | | { |
| 1 | 383 | | if (isInTransition) |
| 0 | 384 | | return; |
| | 385 | |
|
| 1 | 386 | | StartCarousel( |
| | 387 | | fromIndex: currentItemIndex, |
| | 388 | | startInmediately: true, |
| | 389 | | direction: direction, |
| | 390 | | changeDirectionAfterFirstTransition: direction == CarouselDirection.Left, |
| | 391 | | numberOfInitialJumps: numberOfJumps); |
| 1 | 392 | | } |
| | 393 | |
|
| | 394 | | internal void ConfigureManualButtonsEvents() |
| | 395 | | { |
| 144 | 396 | | if (previousButton != null) |
| | 397 | | { |
| 144 | 398 | | previousButton.onClick.RemoveAllListeners(); |
| 144 | 399 | | previousButton.onClick.AddListener(GoToPreviousItem); |
| | 400 | | } |
| | 401 | |
|
| 144 | 402 | | if (nextButton != null) |
| | 403 | | { |
| 144 | 404 | | nextButton.onClick.RemoveAllListeners(); |
| 144 | 405 | | nextButton.onClick.AddListener(GoToNextItem); |
| | 406 | | } |
| 144 | 407 | | } |
| | 408 | |
|
| | 409 | | internal void CreateItem(BaseComponentView newItem, string name) |
| | 410 | | { |
| 40 | 411 | | if (newItem == null) |
| 0 | 412 | | return; |
| | 413 | |
|
| 40 | 414 | | newItem.transform.SetParent(itemsContainer); |
| 40 | 415 | | newItem.transform.localPosition = Vector3.zero; |
| 40 | 416 | | newItem.transform.localScale = Vector3.one; |
| 40 | 417 | | newItem.name = name; |
| | 418 | |
|
| 40 | 419 | | instantiatedItems.Add(newItem); |
| | 420 | |
|
| 40 | 421 | | ResizeItem((RectTransform)newItem.transform); |
| 40 | 422 | | } |
| | 423 | |
|
| | 424 | | internal void ResizeItem(RectTransform item) |
| | 425 | | { |
| 40 | 426 | | ((RectTransform)item.transform).sizeDelta = new Vector2(viewport.rect.width, viewport.rect.height); |
| | 427 | |
|
| 40 | 428 | | int currentNumberOfItems = itemsContainer.childCount; |
| 40 | 429 | | itemsContainer.offsetMin = Vector2.zero; |
| 40 | 430 | | float extraSpace = (currentNumberOfItems - 1) * model.spaceBetweenItems; |
| 40 | 431 | | itemsContainer.offsetMax = new Vector2(viewport.rect.width * (currentNumberOfItems - 1) + extraSpace, 0); |
| 40 | 432 | | } |
| | 433 | |
|
| | 434 | | internal void ResizeAllItems() |
| | 435 | | { |
| 44 | 436 | | if (itemsScroll.horizontalNormalizedPosition != 0f) |
| 0 | 437 | | itemsScroll.horizontalNormalizedPosition = 0f; |
| | 438 | |
|
| 44 | 439 | | if (model.automaticTransition) |
| 44 | 440 | | StartCarousel(); |
| | 441 | |
|
| 88 | 442 | | foreach (Transform child in itemsContainer) |
| | 443 | | { |
| 0 | 444 | | ResizeItem((RectTransform)child); |
| | 445 | | } |
| 44 | 446 | | } |
| | 447 | |
|
| | 448 | | internal void DestroyInstantiatedItems() |
| | 449 | | { |
| 549 | 450 | | List<BaseComponentView> itemsToDestroy = ExtractItems(); |
| | 451 | |
|
| 1632 | 452 | | foreach (BaseComponentView itemToDestroy in itemsToDestroy) |
| | 453 | | { |
| 267 | 454 | | if (itemToDestroy != null) |
| 267 | 455 | | DestroyImmediate(itemToDestroy.gameObject); |
| | 456 | | } |
| | 457 | |
|
| 549 | 458 | | itemsToDestroy.Clear(); |
| | 459 | |
|
| 549 | 460 | | instantiatedItems.Clear(); |
| | 461 | |
|
| 549 | 462 | | itemsContainer.offsetMin = Vector2.zero; |
| 549 | 463 | | itemsContainer.offsetMax = Vector2.zero; |
| 549 | 464 | | } |
| | 465 | |
|
| | 466 | | internal IEnumerator RunCarouselCoroutine( |
| | 467 | | int fromIndex = 0, |
| | 468 | | bool startInmediately = false, |
| | 469 | | CarouselDirection direction = CarouselDirection.Right, |
| | 470 | | bool changeDirectionAfterFirstTransition = false, |
| | 471 | | int numberOfInitialJumps = 1) |
| | 472 | | { |
| 123 | 473 | | currentItemIndex = fromIndex; |
| 123 | 474 | | SetSelectedDot(currentItemIndex); |
| 123 | 475 | | bool continueCarrousel = true; |
| 123 | 476 | | while (gameObject.activeInHierarchy && itemsContainer.childCount > 1 && continueCarrousel) |
| | 477 | | { |
| 6 | 478 | | float elapsedTime = 0f; |
| | 479 | |
|
| 6 | 480 | | if (!startInmediately) |
| | 481 | | { |
| 1 | 482 | | while (elapsedTime < model.timeBetweenItems) |
| | 483 | | { |
| 1 | 484 | | if (!model.pauseOnFocus || (model.pauseOnFocus && !isFocused)) |
| 1 | 485 | | elapsedTime += Time.deltaTime; |
| | 486 | |
|
| 1 | 487 | | yield return null; |
| | 488 | | } |
| | 489 | |
|
| | 490 | | } |
| | 491 | |
|
| 5 | 492 | | if (instantiatedItems.Count > 0) |
| | 493 | | { |
| 5 | 494 | | if (direction == CarouselDirection.Right) |
| | 495 | | { |
| 3 | 496 | | SetSelectedDot(currentItemIndex == (instantiatedItems.Count - 1) ? 0 : currentItemIndex + numberOfIn |
| 3 | 497 | | yield return RunRightAnimation(numberOfInitialJumps); |
| | 498 | |
|
| 0 | 499 | | if (changeDirectionAfterFirstTransition) |
| | 500 | | { |
| 0 | 501 | | direction = CarouselDirection.Left; |
| 0 | 502 | | changeDirectionAfterFirstTransition = false; |
| | 503 | | } |
| 0 | 504 | | continueCarrousel = model.automaticTransition; |
| | 505 | | } |
| | 506 | | else |
| | 507 | | { |
| 2 | 508 | | SetSelectedDot(currentItemIndex == 0 ? (instantiatedItems.Count - 1) : currentItemIndex - numberOfIn |
| 2 | 509 | | yield return RunLeftAnimation(numberOfInitialJumps); |
| | 510 | |
|
| 0 | 511 | | if (changeDirectionAfterFirstTransition) |
| | 512 | | { |
| 0 | 513 | | direction = CarouselDirection.Right; |
| 0 | 514 | | changeDirectionAfterFirstTransition = false; |
| | 515 | | } |
| 0 | 516 | | continueCarrousel = model.automaticTransition; |
| | 517 | | } |
| | 518 | | } |
| | 519 | |
|
| 0 | 520 | | startInmediately = false; |
| 0 | 521 | | numberOfInitialJumps = 1; |
| | 522 | | } |
| 117 | 523 | | } |
| | 524 | |
|
| | 525 | | internal IEnumerator RunRightAnimation(int numberOfJumps = 1) |
| | 526 | | { |
| 3 | 527 | | if (currentItemIndex == instantiatedItems.Count - 1) |
| | 528 | | { |
| 0 | 529 | | currentItemIndex = 0; |
| 0 | 530 | | yield return RunAnimationCoroutine(CarouselDirection.Left, instantiatedItems.Count - 1); |
| | 531 | | } |
| | 532 | | else |
| | 533 | | { |
| 3 | 534 | | currentItemIndex += numberOfJumps; |
| 3 | 535 | | yield return RunAnimationCoroutine(CarouselDirection.Right, numberOfJumps); |
| | 536 | | } |
| 0 | 537 | | } |
| | 538 | |
|
| | 539 | | internal IEnumerator RunLeftAnimation(int numberOfJumps = 1) |
| | 540 | | { |
| 2 | 541 | | if (currentItemIndex == 0) |
| | 542 | | { |
| 2 | 543 | | currentItemIndex = instantiatedItems.Count - 1; |
| 2 | 544 | | yield return RunAnimationCoroutine(CarouselDirection.Right, instantiatedItems.Count - 1); |
| | 545 | | } |
| | 546 | | else |
| | 547 | | { |
| 0 | 548 | | currentItemIndex -= numberOfJumps; |
| 0 | 549 | | yield return RunAnimationCoroutine(CarouselDirection.Left, numberOfJumps); |
| | 550 | | } |
| 0 | 551 | | } |
| | 552 | |
|
| | 553 | | internal IEnumerator RunAnimationCoroutine(CarouselDirection direction, int numberOfJumps = 1) |
| | 554 | | { |
| 5 | 555 | | isInTransition = true; |
| 5 | 556 | | float currentAnimationTime = 0f; |
| 5 | 557 | | float initialNormalizedPos = itemsScroll.horizontalNormalizedPosition; |
| | 558 | |
|
| 5 | 559 | | if (direction == CarouselDirection.Right) |
| 5 | 560 | | currentFinalNormalizedPos = initialNormalizedPos + ((float)numberOfJumps / (instantiatedItems.Count - 1)); |
| | 561 | | else |
| 0 | 562 | | currentFinalNormalizedPos = initialNormalizedPos - ((float)numberOfJumps / (instantiatedItems.Count - 1)); |
| | 563 | |
|
| 5 | 564 | | while (currentAnimationTime <= model.animationTransitionTime) |
| | 565 | | { |
| 5 | 566 | | itemsScroll.horizontalNormalizedPosition = Mathf.Clamp01(Mathf.Lerp( |
| | 567 | | initialNormalizedPos, |
| | 568 | | currentFinalNormalizedPos, |
| | 569 | | model.animationCurve.Evaluate(currentAnimationTime / model.animationTransitionTime))); |
| | 570 | |
|
| 5 | 571 | | currentAnimationTime += Time.deltaTime; |
| | 572 | |
|
| 5 | 573 | | yield return null; |
| | 574 | | } |
| | 575 | |
|
| 0 | 576 | | itemsScroll.horizontalNormalizedPosition = currentFinalNormalizedPos; |
| 0 | 577 | | isInTransition = false; |
| 0 | 578 | | } |
| | 579 | |
|
| | 580 | | public void GenerateDotsSelector() |
| | 581 | | { |
| 38 | 582 | | List<GameObject> dotsToRemove = new List<GameObject>(); |
| 226 | 583 | | foreach (Transform child in dotsSelector.transform) |
| | 584 | | { |
| 75 | 585 | | if (child.gameObject == dotButtonTemplate.gameObject) |
| | 586 | | continue; |
| | 587 | |
|
| 37 | 588 | | dotsToRemove.Add(child.gameObject); |
| | 589 | | } |
| | 590 | |
|
| 150 | 591 | | foreach (GameObject dotToRemove in dotsToRemove) |
| | 592 | | { |
| 37 | 593 | | Utils.SafeDestroy(dotToRemove); |
| | 594 | | } |
| | 595 | |
|
| 152 | 596 | | for (int i = 0; i < itemsContainer.childCount; i++) |
| | 597 | | { |
| 38 | 598 | | Button newDotButton = Instantiate(dotButtonTemplate, dotsSelector.transform); |
| 38 | 599 | | newDotButton.gameObject.SetActive(true); |
| 38 | 600 | | newDotButton.onClick.AddListener(() => |
| | 601 | | { |
| 0 | 602 | | int dotButtonIndex = newDotButton.transform.GetSiblingIndex() - 1; |
| 0 | 603 | | if (dotButtonIndex != currentDotIndex) |
| | 604 | | { |
| 0 | 605 | | MakeJumpFromDotsSelector( |
| | 606 | | Mathf.Abs(dotButtonIndex - currentDotIndex), |
| | 607 | | dotButtonIndex > currentDotIndex ? CarouselDirection.Right : CarouselDirection.Left); |
| | 608 | | } |
| 0 | 609 | | }); |
| | 610 | | } |
| | 611 | |
|
| 38 | 612 | | SetSelectedDot(0); |
| 38 | 613 | | } |
| | 614 | |
|
| | 615 | | internal void SetSelectedDot(int index) |
| | 616 | | { |
| 167 | 617 | | int currentIndex = 0; |
| 167 | 618 | | currentDotIndex = -1; |
| 1282 | 619 | | foreach (Transform child in dotsSelector.transform) |
| | 620 | | { |
| 474 | 621 | | if (child.gameObject == dotButtonTemplate.gameObject) |
| | 622 | | continue; |
| | 623 | |
|
| 307 | 624 | | if (currentIndex == index) |
| | 625 | | { |
| 144 | 626 | | child.GetComponent<Image>().color = dotSelectedColor; |
| 144 | 627 | | child.transform.localScale = Vector3.one * 1.5f; |
| 144 | 628 | | currentDotIndex = index; |
| | 629 | | } |
| | 630 | | else |
| | 631 | | { |
| 163 | 632 | | child.GetComponent<Image>().color = dotUnselectedColor; |
| 163 | 633 | | child.transform.localScale = Vector3.one; |
| | 634 | | } |
| | 635 | |
|
| 307 | 636 | | currentIndex++; |
| | 637 | | } |
| 167 | 638 | | } |
| | 639 | |
|
| | 640 | | public void CleanInstantiatedItems() |
| | 641 | | { |
| 0 | 642 | | instantiatedItems = new List<BaseComponentView>(); |
| 0 | 643 | | } |
| | 644 | |
|
| | 645 | | public void ResetManualCarousel() |
| | 646 | | { |
| 0 | 647 | | isInTransition = false; |
| 0 | 648 | | currentItemIndex = 0; |
| 0 | 649 | | SetSelectedDot(0); |
| 0 | 650 | | } |
| | 651 | |
|
| | 652 | | internal IEnumerator RegisterCurrentInstantiatedItems() |
| | 653 | | { |
| 142 | 654 | | instantiatedItems.Clear(); |
| | 655 | |
|
| 756 | 656 | | foreach (Transform child in itemsContainer) |
| | 657 | | { |
| 236 | 658 | | BaseComponentView existingItem = child.GetComponent<BaseComponentView>(); |
| 236 | 659 | | if (existingItem != null) |
| 236 | 660 | | instantiatedItems.Add(existingItem); |
| | 661 | | else |
| 0 | 662 | | Destroy(child.gameObject); |
| | 663 | | } |
| | 664 | |
|
| | 665 | | // In the first loading, before calculating the size of the current items, it is needed to wait for a frame in o |
| | 666 | | // allow time for the carousel viewport to get its final size to be able to execute the 'ResizeAllItems' functio |
| 142 | 667 | | yield return null; |
| | 668 | |
|
| 17 | 669 | | ResizeAllItems(); |
| 17 | 670 | | SetManualControlsActive(model.showManualControls); |
| 17 | 671 | | GenerateDotsSelector(); |
| 17 | 672 | | } |
| | 673 | | } |