| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Builder |
| | 8 | | { |
| | 9 | | public class NewProjectSecondStepView : BaseComponentView |
| | 10 | | { |
| | 11 | | private const int MAX_PARCELS = 32; |
| | 12 | |
|
| | 13 | | private const string MAX_PARCELS_TEXT = " parcels"; |
| | 14 | |
|
| | 15 | | public event Action OnBackPressed; |
| | 16 | | public event Action<int, int> OnNextPressed; |
| | 17 | |
|
| | 18 | | [Header("Design variables")] |
| | 19 | | [SerializeField] internal Color normalTextColor; |
| | 20 | | [SerializeField] internal Color errorTextColor; |
| | 21 | | [SerializeField] private GridContainerComponentModel gridModel; |
| | 22 | | [SerializeField] private BaseComponentView parcelImagePrefab; |
| | 23 | |
|
| | 24 | | [Header("References")] |
| | 25 | | [SerializeField] private LimitInputField rowsInputField; |
| | 26 | | [SerializeField] private LimitInputField columsInputField; |
| | 27 | | [SerializeField] internal TextMeshProUGUI parcelText; |
| | 28 | | [SerializeField] internal GameObject errorGameObject; |
| | 29 | | [SerializeField] internal GameObject gridGameObject; |
| | 30 | | [SerializeField] private GridContainerComponentView gridView; |
| | 31 | |
|
| | 32 | | [SerializeField] internal ButtonComponentView nextButton; |
| | 33 | | [SerializeField] private ButtonComponentView backButton; |
| | 34 | |
|
| 12 | 35 | | internal int rows = 2; |
| 12 | 36 | | internal int colums = 2; |
| | 37 | |
|
| | 38 | | public override void Start() |
| | 39 | | { |
| 0 | 40 | | base.Start(); |
| | 41 | |
|
| 0 | 42 | | rowsInputField.OnInputChange += RowsChanged; |
| 0 | 43 | | columsInputField.OnInputChange += ColumnsChanged; |
| | 44 | |
|
| 0 | 45 | | backButton.onClick.AddListener(BackPressed); |
| 0 | 46 | | nextButton.onClick.AddListener(NextPressed); |
| 0 | 47 | | gridView.Configure(gridModel); |
| 0 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | public override void RefreshControl() { } |
| | 51 | |
|
| | 52 | | internal void RowsChanged(string value) |
| | 53 | | { |
| | 54 | | //We ensure that the minimum size of the row is 1 |
| 1 | 55 | | if (string.IsNullOrEmpty(value) || value == "0") |
| 0 | 56 | | rows = 1; |
| | 57 | | else |
| 1 | 58 | | rows = Mathf.Abs(Int32.Parse(value)); |
| 1 | 59 | | ValueChanged(); |
| 1 | 60 | | } |
| | 61 | |
|
| | 62 | | internal void ColumnsChanged(string value) |
| | 63 | | { |
| | 64 | | //We ensure that the minimum size of the column is 1 |
| 1 | 65 | | if (string.IsNullOrEmpty(value) || value == "0") |
| 0 | 66 | | colums = 1; |
| | 67 | | else |
| 1 | 68 | | colums = Mathf.Abs(Int32.Parse(value)); |
| 1 | 69 | | ValueChanged(); |
| 1 | 70 | | } |
| | 71 | |
|
| | 72 | | private void ValueChanged() |
| | 73 | | { |
| 2 | 74 | | if (rows * colums > MAX_PARCELS) |
| | 75 | | { |
| 0 | 76 | | ShowError(); |
| 0 | 77 | | } |
| | 78 | | else |
| | 79 | | { |
| 2 | 80 | | gridModel.constraintCount = rows; |
| 2 | 81 | | gridView.SetItems(parcelImagePrefab, rows * colums); |
| 2 | 82 | | gridView.Configure(gridModel); |
| 2 | 83 | | ShowGrid(); |
| | 84 | | } |
| 2 | 85 | | } |
| | 86 | |
|
| | 87 | | internal void ShowError() |
| | 88 | | { |
| 1 | 89 | | errorGameObject.SetActive(true); |
| 1 | 90 | | gridGameObject.SetActive(false); |
| | 91 | |
|
| 1 | 92 | | parcelText.color = errorTextColor; |
| | 93 | |
|
| 1 | 94 | | parcelText.text = (rows * colums) + MAX_PARCELS_TEXT; |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | internal void ShowGrid() |
| | 98 | | { |
| 3 | 99 | | errorGameObject.SetActive(false); |
| 3 | 100 | | gridGameObject.SetActive(true); |
| | 101 | |
|
| 3 | 102 | | parcelText.color = normalTextColor; |
| | 103 | |
|
| 3 | 104 | | parcelText.text = $"{(rows * colums)} parcels = {(rows * DCL.Configuration.ParcelSettings.PARCEL_SIZE)}x{col |
| 3 | 105 | | } |
| | 106 | |
|
| | 107 | | internal void NextPressed() |
| | 108 | | { |
| 1 | 109 | | if (!nextButton.IsInteractable()) |
| 0 | 110 | | return; |
| | 111 | |
|
| 1 | 112 | | OnNextPressed?.Invoke(rows, colums); |
| 1 | 113 | | } |
| | 114 | |
|
| 2 | 115 | | internal void BackPressed() { OnBackPressed?.Invoke(); } |
| | 116 | | } |
| | 117 | | } |