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