| | 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 | | { |
| 4 | 40 | | base.Start(); |
| | 41 | |
|
| 4 | 42 | | rowsInputField.OnInputChange += RowsChanged; |
| 4 | 43 | | rowsInputField.OnInputLostFocus += RowsInputLostFocus; |
| | 44 | |
|
| 4 | 45 | | columsInputField.OnInputChange += ColumnsChanged; |
| 4 | 46 | | columsInputField.OnInputLostFocus += ColumnsInputLostFocus; |
| | 47 | |
|
| 4 | 48 | | backButton.onClick.AddListener(BackPressed); |
| 4 | 49 | | nextButton.onClick.AddListener(NextPressed); |
| 4 | 50 | | gridView.Configure(gridModel); |
| 4 | 51 | | } |
| | 52 | |
|
| 0 | 53 | | public override void RefreshControl() { } |
| | 54 | |
|
| | 55 | | internal void RowsInputLostFocus() |
| | 56 | | { |
| 0 | 57 | | if (!string.IsNullOrEmpty(rowsInputField.GetValue())) |
| 0 | 58 | | return; |
| | 59 | |
|
| 0 | 60 | | rows = 1; |
| 0 | 61 | | rowsInputField.SetText(rows.ToString()); |
| 0 | 62 | | ValueChanged(rowsInputField); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | internal void ColumnsInputLostFocus() |
| | 66 | | { |
| 0 | 67 | | if (!string.IsNullOrEmpty(columsInputField.GetValue())) |
| 0 | 68 | | return; |
| | 69 | |
|
| 0 | 70 | | colums = 1; |
| 0 | 71 | | columsInputField.SetText(colums.ToString()); |
| 0 | 72 | | ValueChanged(columsInputField); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | internal void RowsChanged(string value) |
| | 76 | | { |
| | 77 | | //We ensure that the minimum size of the row is 1 |
| 1 | 78 | | if (string.IsNullOrEmpty(value) || value == "0") |
| | 79 | | { |
| 0 | 80 | | if(rowsInputField.HasFocus()) |
| 0 | 81 | | return; |
| 0 | 82 | | rows = 1; |
| 0 | 83 | | rowsInputField.SetText(rows.ToString()); |
| 0 | 84 | | } |
| | 85 | | else |
| | 86 | | { |
| 1 | 87 | | rows = Mathf.Abs(Int32.Parse(value)); |
| | 88 | | } |
| 1 | 89 | | ValueChanged(rowsInputField); |
| 1 | 90 | | } |
| | 91 | |
|
| | 92 | | internal void ColumnsChanged(string value) |
| | 93 | | { |
| | 94 | | //We ensure that the minimum size of the column is 1 |
| 1 | 95 | | if (string.IsNullOrEmpty(value) || value == "0") |
| | 96 | | { |
| 0 | 97 | | if(columsInputField.HasFocus()) |
| 0 | 98 | | return; |
| 0 | 99 | | colums = 1; |
| 0 | 100 | | columsInputField.SetText(colums.ToString()); |
| 0 | 101 | | } |
| | 102 | | else |
| | 103 | | { |
| 1 | 104 | | colums = Mathf.Abs(Int32.Parse(value)); |
| | 105 | | } |
| 1 | 106 | | ValueChanged(columsInputField); |
| 1 | 107 | | } |
| | 108 | |
|
| | 109 | | private void ValueChanged(LimitInputField origin) |
| | 110 | | { |
| 2 | 111 | | if (rows * colums > MAX_PARCELS) |
| | 112 | | { |
| 0 | 113 | | ShowError(); |
| 0 | 114 | | origin.SetError(); |
| 0 | 115 | | } |
| | 116 | | else |
| | 117 | | { |
| 2 | 118 | | columsInputField.InputAvailable(); |
| 2 | 119 | | rowsInputField.InputAvailable(); |
| | 120 | |
|
| 2 | 121 | | gridModel.constraintCount = rows; |
| 2 | 122 | | gridView.SetItems(parcelImagePrefab, rows * colums); |
| 2 | 123 | | gridView.Configure(gridModel); |
| 2 | 124 | | ShowGrid(); |
| | 125 | | } |
| 2 | 126 | | } |
| | 127 | |
|
| | 128 | | internal void ShowError() |
| | 129 | | { |
| 1 | 130 | | nextButton.SetInteractable(false); |
| 1 | 131 | | errorGameObject.SetActive(true); |
| 1 | 132 | | gridGameObject.SetActive(false); |
| | 133 | |
|
| 1 | 134 | | parcelText.color = errorTextColor; |
| | 135 | |
|
| 1 | 136 | | parcelText.text = (rows * colums) + MAX_PARCELS_TEXT; |
| 1 | 137 | | } |
| | 138 | |
|
| | 139 | | internal void ShowGrid() |
| | 140 | | { |
| 3 | 141 | | nextButton.SetInteractable(true); |
| 3 | 142 | | errorGameObject.SetActive(false); |
| 3 | 143 | | gridGameObject.SetActive(true); |
| | 144 | |
|
| 3 | 145 | | parcelText.color = normalTextColor; |
| | 146 | |
|
| 3 | 147 | | parcelText.text = $"{(rows * colums)} parcels = {(rows * DCL.Configuration.ParcelSettings.PARCEL_SIZE)}x{col |
| 3 | 148 | | } |
| | 149 | |
|
| | 150 | | internal void NextPressed() |
| | 151 | | { |
| 1 | 152 | | if (!nextButton.IsInteractable()) |
| 0 | 153 | | return; |
| | 154 | |
|
| 1 | 155 | | OnNextPressed?.Invoke(rows, colums); |
| 1 | 156 | | } |
| | 157 | |
|
| 2 | 158 | | internal void BackPressed() { OnBackPressed?.Invoke(); } |
| | 159 | | } |
| | 160 | | } |