| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Collections; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Assertions; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.Components |
| | 9 | | { |
| | 10 | | [System.Serializable] |
| | 11 | | public struct UIValue |
| | 12 | | { |
| | 13 | | public enum Unit |
| | 14 | | { |
| | 15 | | PERCENT, |
| | 16 | | PIXELS |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public float value; |
| | 20 | | public Unit type; |
| | 21 | |
|
| | 22 | | public void SetPixels(float value) |
| | 23 | | { |
| | 24 | | this.type = Unit.PIXELS; |
| | 25 | | this.value = value; |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public void SetPercent(float value) |
| | 29 | | { |
| | 30 | | this.type = Unit.PERCENT; |
| | 31 | | this.value = value; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public UIValue(float value, Unit unitType = Unit.PIXELS) |
| | 35 | | { |
| | 36 | | this.value = value; |
| | 37 | | this.type = unitType; |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public float GetScaledValue(float parentSize) |
| | 41 | | { |
| | 42 | | if (type == Unit.PIXELS) |
| | 43 | | return value; |
| | 44 | |
|
| | 45 | | // Prevent division by zero |
| | 46 | | if (parentSize <= Mathf.Epsilon) |
| | 47 | | parentSize = 1; |
| | 48 | |
|
| | 49 | | return value / 100 * parentSize; |
| | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public class UIShape<ReferencesContainerType, ModelType> : UIShape |
| | 54 | | where ReferencesContainerType : UIReferencesContainer |
| | 55 | | where ModelType : UIShape.Model |
| | 56 | | { |
| | 57 | | public UIShape() { } |
| | 58 | |
|
| | 59 | | new public ModelType model { get { return base.model as ModelType; } set { base.model = value; } } |
| | 60 | |
|
| | 61 | | new public ReferencesContainerType referencesContainer { get { return base.referencesContainer as ReferencesCont |
| | 62 | |
|
| | 63 | | public override ComponentUpdateHandler CreateUpdateHandler() { return new UIShapeUpdateHandler<ReferencesContain |
| | 64 | |
|
| | 65 | | bool raiseOnAttached; |
| | 66 | | bool firstApplyChangesCall; |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// This is called by UIShapeUpdateHandler before calling ApplyChanges. |
| | 70 | | /// </summary> |
| | 71 | | public void PreApplyChanges(BaseModel newModel) |
| | 72 | | { |
| | 73 | | model = (ModelType) newModel; |
| | 74 | |
|
| | 75 | | raiseOnAttached = false; |
| | 76 | | firstApplyChangesCall = false; |
| | 77 | |
|
| | 78 | | if (referencesContainer == null) |
| | 79 | | { |
| | 80 | | referencesContainer = InstantiateUIGameObject<ReferencesContainerType>(referencesContainerPrefabName); |
| | 81 | |
|
| | 82 | | raiseOnAttached = true; |
| | 83 | | firstApplyChangesCall = true; |
| | 84 | | } |
| | 85 | | else if (ReparentComponent(referencesContainer.rectTransform, model.parentComponent)) |
| | 86 | | { |
| | 87 | | raiseOnAttached = true; |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public override void RaiseOnAppliedChanges() |
| | 92 | | { |
| | 93 | | RefreshDCLLayout(); |
| | 94 | |
|
| | 95 | | #if UNITY_EDITOR |
| | 96 | | SetComponentDebugName(); |
| | 97 | | #endif |
| | 98 | |
|
| | 99 | | // We hide the component visibility when it's created (first applychanges) |
| | 100 | | // as it has default values and appears in the middle of the screen |
| | 101 | | if (firstApplyChangesCall) |
| | 102 | | referencesContainer.canvasGroup.alpha = 0f; |
| | 103 | | else |
| | 104 | | referencesContainer.canvasGroup.alpha = model.visible ? model.opacity : 0f; |
| | 105 | |
|
| | 106 | | referencesContainer.canvasGroup.blocksRaycasts = model.visible && model.isPointerBlocker; |
| | 107 | |
|
| | 108 | | base.RaiseOnAppliedChanges(); |
| | 109 | |
|
| | 110 | | if (raiseOnAttached && parentUIComponent != null) |
| | 111 | | { |
| | 112 | | UIReferencesContainer[] parents = referencesContainer.GetComponentsInParent<UIReferencesContainer>(true) |
| | 113 | |
|
| | 114 | | for (int i = 0; i < parents.Length; i++) |
| | 115 | | { |
| | 116 | | UIReferencesContainer parent = parents[i]; |
| | 117 | | if (parent.owner != null) |
| | 118 | | { |
| | 119 | | parent.owner.OnChildAttached(parentUIComponent, this); |
| | 120 | | } |
| | 121 | | } |
| | 122 | | } |
| | 123 | | } |
| | 124 | | } |
| | 125 | |
|
| | 126 | | public class UIShape : BaseDisposable |
| | 127 | | { |
| | 128 | | [System.Serializable] |
| | 129 | | public class Model : BaseModel |
| | 130 | | { |
| | 131 | | public string name; |
| | 132 | | public string parentComponent; |
| 610 | 133 | | public bool visible = true; |
| 610 | 134 | | public float opacity = 1f; |
| 610 | 135 | | public string hAlign = "center"; |
| 610 | 136 | | public string vAlign = "center"; |
| 610 | 137 | | public UIValue width = new UIValue(100f); |
| 610 | 138 | | public UIValue height = new UIValue(50f); |
| 610 | 139 | | public UIValue positionX = new UIValue(0f); |
| 610 | 140 | | public UIValue positionY = new UIValue(0f); |
| 610 | 141 | | public bool isPointerBlocker = true; |
| | 142 | | public string onClick; |
| | 143 | |
|
| 51 | 144 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 145 | | } |
| | 146 | |
|
| 145 | 147 | | public override string componentName => GetDebugName(); |
| 0 | 148 | | public virtual string referencesContainerPrefabName => ""; |
| | 149 | | public UIReferencesContainer referencesContainer; |
| | 150 | | public RectTransform childHookRectTransform; |
| | 151 | |
|
| 0 | 152 | | private BaseVariable<Vector2Int> screenSize => DataStore.i.screen.size; |
| | 153 | |
|
| 0 | 154 | | public UIShape parentUIComponent { get; protected set; } |
| | 155 | |
|
| 120 | 156 | | public UIShape() |
| | 157 | | { |
| 120 | 158 | | screenSize.OnChange += OnScreenResize; |
| 120 | 159 | | model = new Model(); |
| 120 | 160 | | } |
| | 161 | |
|
| 37 | 162 | | private void OnScreenResize(Vector2Int current, Vector2Int previous) => RefreshAll(); |
| | 163 | |
|
| 0 | 164 | | public override int GetClassId() { return (int) CLASS_ID.UI_IMAGE_SHAPE; } |
| | 165 | |
|
| | 166 | | public string GetDebugName() |
| | 167 | | { |
| 145 | 168 | | Model model = (Model) this.model; |
| 145 | 169 | | if (string.IsNullOrEmpty(model.name)) |
| | 170 | | { |
| 138 | 171 | | return GetType().Name; |
| | 172 | | } |
| | 173 | | else |
| | 174 | | { |
| 7 | 175 | | return GetType().Name + " - " + model.name; |
| | 176 | | } |
| | 177 | | } |
| | 178 | |
|
| 0 | 179 | | public override IEnumerator ApplyChanges(BaseModel newJson) { return null; } |
| | 180 | |
|
| | 181 | | internal T InstantiateUIGameObject<T>(string prefabPath) where T : UIReferencesContainer |
| | 182 | | { |
| 77 | 183 | | Model model = (Model) this.model; |
| 77 | 184 | | GameObject uiGameObject = null; |
| 77 | 185 | | bool targetParentExists = !string.IsNullOrEmpty(model.parentComponent) && |
| | 186 | | scene.disposableComponents.ContainsKey(model.parentComponent); |
| | 187 | |
|
| 77 | 188 | | if (targetParentExists) |
| | 189 | | { |
| 23 | 190 | | if (scene.disposableComponents.ContainsKey(model.parentComponent)) |
| | 191 | | { |
| 23 | 192 | | parentUIComponent = (scene.disposableComponents[model.parentComponent] as UIShape); |
| 23 | 193 | | } |
| | 194 | | else |
| | 195 | | { |
| 0 | 196 | | parentUIComponent = scene.GetSharedComponent<UIScreenSpace>(); |
| | 197 | | } |
| 0 | 198 | | } |
| | 199 | | else |
| | 200 | | { |
| 54 | 201 | | parentUIComponent = scene.GetSharedComponent<UIScreenSpace>(); |
| | 202 | | } |
| | 203 | |
|
| 77 | 204 | | uiGameObject = |
| | 205 | | UnityEngine.Object.Instantiate( |
| | 206 | | Resources.Load(prefabPath), |
| | 207 | | parentUIComponent != null ? parentUIComponent.childHookRectTransform : null) as GameObject; |
| 77 | 208 | | referencesContainer = uiGameObject.GetComponent<T>(); |
| | 209 | |
|
| 77 | 210 | | referencesContainer.rectTransform.SetToMaxStretch(); |
| | 211 | |
|
| 77 | 212 | | childHookRectTransform = referencesContainer.childHookRectTransform; |
| | 213 | |
|
| 77 | 214 | | referencesContainer.owner = this; |
| | 215 | |
|
| 77 | 216 | | return referencesContainer as T; |
| | 217 | | } |
| | 218 | |
|
| | 219 | | public virtual void RefreshAll() |
| | 220 | | { |
| 130 | 221 | | RefreshDCLLayoutRecursively(refreshSize: true, refreshAlignmentAndPosition: false); |
| 130 | 222 | | FixMaxStretchRecursively(); |
| 130 | 223 | | RefreshDCLLayoutRecursively_Internal(refreshSize: false, refreshAlignmentAndPosition: true); |
| 130 | 224 | | } |
| | 225 | |
|
| | 226 | | public void RefreshDCLLayout(bool refreshSize = true, bool refreshAlignmentAndPosition = true) |
| | 227 | | { |
| 511 | 228 | | RectTransform parentRT = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 229 | |
|
| 511 | 230 | | if (refreshSize) |
| | 231 | | { |
| 316 | 232 | | RefreshDCLSize(parentRT); |
| | 233 | | } |
| | 234 | |
|
| 511 | 235 | | if (refreshAlignmentAndPosition) |
| | 236 | | { |
| | 237 | | // Alignment (Alignment uses size so we should always align AFTER resizing) |
| 340 | 238 | | RefreshDCLAlignmentAndPosition(parentRT); |
| | 239 | | } |
| 511 | 240 | | } |
| | 241 | |
|
| | 242 | | protected virtual void RefreshDCLSize(RectTransform parentTransform = null) |
| | 243 | | { |
| 300 | 244 | | if (parentTransform == null) |
| | 245 | | { |
| 0 | 246 | | parentTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 247 | | } |
| | 248 | |
|
| 300 | 249 | | Model model = (Model) this.model; |
| | 250 | |
|
| 300 | 251 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, |
| | 252 | | model.width.GetScaledValue(parentTransform.rect.width)); |
| 300 | 253 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, |
| | 254 | | model.height.GetScaledValue(parentTransform.rect.height)); |
| 300 | 255 | | } |
| | 256 | |
|
| | 257 | | public void RefreshDCLAlignmentAndPosition(RectTransform parentTransform = null) |
| | 258 | | { |
| 340 | 259 | | if (parentTransform == null) |
| | 260 | | { |
| 0 | 261 | | parentTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 262 | | } |
| | 263 | |
|
| 340 | 264 | | referencesContainer.layoutElement.ignoreLayout = false; |
| 340 | 265 | | ConfigureAlignment(referencesContainer.layoutGroup); |
| 340 | 266 | | Utils.ForceRebuildLayoutImmediate(parentTransform); |
| 340 | 267 | | referencesContainer.layoutElement.ignoreLayout = true; |
| | 268 | |
|
| 340 | 269 | | Model model = (Model) this.model; |
| | 270 | | // Reposition |
| 340 | 271 | | Vector3 position = Vector3.zero; |
| 340 | 272 | | position.x = model.positionX.GetScaledValue(parentTransform.rect.width); |
| 340 | 273 | | position.y = model.positionY.GetScaledValue(parentTransform.rect.height); |
| | 274 | |
|
| 340 | 275 | | position = Utils.Sanitize(position); |
| 340 | 276 | | referencesContainer.layoutElementRT.localPosition += position; |
| 340 | 277 | | } |
| | 278 | |
|
| | 279 | | public virtual void RefreshDCLLayoutRecursively(bool refreshSize = true, |
| | 280 | | bool refreshAlignmentAndPosition = true) |
| | 281 | | { |
| 154 | 282 | | RefreshDCLLayoutRecursively_Internal(refreshSize, refreshAlignmentAndPosition); |
| 154 | 283 | | } |
| | 284 | |
|
| | 285 | | public void RefreshDCLLayoutRecursively_Internal(bool refreshSize = true, |
| | 286 | | bool refreshAlignmentAndPosition = true) |
| | 287 | | { |
| 284 | 288 | | UIShape rootParent = GetRootParent(); |
| | 289 | |
|
| 284 | 290 | | Assert.IsTrue(rootParent != null, "root parent must never be null"); |
| | 291 | |
|
| 284 | 292 | | if (rootParent.referencesContainer == null) |
| 68 | 293 | | return; |
| | 294 | |
|
| 216 | 295 | | Utils.InverseTransformChildTraversal<UIReferencesContainer>( |
| | 296 | | (x) => |
| | 297 | | { |
| 510 | 298 | | if (x.owner != null) |
| | 299 | | { |
| 366 | 300 | | x.owner.RefreshDCLLayout(refreshSize, refreshAlignmentAndPosition); |
| | 301 | | } |
| 510 | 302 | | }, |
| | 303 | | rootParent.referencesContainer.transform); |
| 216 | 304 | | } |
| | 305 | |
|
| | 306 | | public void FixMaxStretchRecursively() |
| | 307 | | { |
| 130 | 308 | | UIShape rootParent = GetRootParent(); |
| | 309 | |
|
| 130 | 310 | | Assert.IsTrue(rootParent != null, "root parent must never be null"); |
| | 311 | |
|
| 130 | 312 | | if (rootParent.referencesContainer == null) |
| 34 | 313 | | return; |
| | 314 | |
|
| 96 | 315 | | Utils.InverseTransformChildTraversal<UIReferencesContainer>( |
| | 316 | | (x) => |
| | 317 | | { |
| 243 | 318 | | if (x.owner != null) |
| | 319 | | { |
| 171 | 320 | | x.rectTransform.SetToMaxStretch(); |
| | 321 | | } |
| 243 | 322 | | }, |
| | 323 | | rootParent.referencesContainer.transform); |
| 96 | 324 | | } |
| | 325 | |
|
| | 326 | | protected bool ReparentComponent(RectTransform targetTransform, string targetParent) |
| | 327 | | { |
| 68 | 328 | | bool targetParentExists = !string.IsNullOrEmpty(targetParent) && |
| | 329 | | scene.disposableComponents.ContainsKey(targetParent); |
| | 330 | |
|
| 68 | 331 | | if (targetParentExists && parentUIComponent == scene.disposableComponents[targetParent]) |
| | 332 | | { |
| 22 | 333 | | return false; |
| | 334 | | } |
| | 335 | |
|
| 46 | 336 | | if (parentUIComponent != null) |
| | 337 | | { |
| 46 | 338 | | UIReferencesContainer[] parents = referencesContainer.GetComponentsInParent<UIReferencesContainer>(true) |
| | 339 | |
|
| 186 | 340 | | foreach (var parent in parents) |
| | 341 | | { |
| 47 | 342 | | if (parent.owner != null) |
| | 343 | | { |
| 47 | 344 | | parent.owner.OnChildDetached(parentUIComponent, this); |
| | 345 | | } |
| | 346 | | } |
| | 347 | | } |
| | 348 | |
|
| 46 | 349 | | if (targetParentExists) |
| | 350 | | { |
| 22 | 351 | | parentUIComponent = scene.disposableComponents[targetParent] as UIShape; |
| 22 | 352 | | } |
| | 353 | | else |
| | 354 | | { |
| 24 | 355 | | parentUIComponent = scene.GetSharedComponent<UIScreenSpace>(); |
| | 356 | | } |
| | 357 | |
|
| 46 | 358 | | targetTransform.SetParent(parentUIComponent.childHookRectTransform, false); |
| 46 | 359 | | return true; |
| | 360 | | } |
| | 361 | |
|
| | 362 | | public UIShape GetRootParent() |
| | 363 | | { |
| 432 | 364 | | UIShape parent = null; |
| | 365 | |
|
| 432 | 366 | | if (parentUIComponent != null && !(parentUIComponent is UIScreenSpace)) |
| | 367 | | { |
| 18 | 368 | | parent = parentUIComponent.GetRootParent(); |
| 18 | 369 | | } |
| | 370 | | else |
| | 371 | | { |
| 414 | 372 | | parent = this; |
| | 373 | | } |
| | 374 | |
|
| 432 | 375 | | return parent; |
| | 376 | | } |
| | 377 | |
|
| | 378 | | protected void ConfigureAlignment(LayoutGroup layout) |
| | 379 | | { |
| 340 | 380 | | Model model = (Model) this.model; |
| 340 | 381 | | switch (model.vAlign) |
| | 382 | | { |
| | 383 | | case "top": |
| 7 | 384 | | switch (model.hAlign) |
| | 385 | | { |
| | 386 | | case "left": |
| 2 | 387 | | layout.childAlignment = TextAnchor.UpperLeft; |
| 2 | 388 | | break; |
| | 389 | | case "right": |
| 2 | 390 | | layout.childAlignment = TextAnchor.UpperRight; |
| 2 | 391 | | break; |
| | 392 | | default: |
| 3 | 393 | | layout.childAlignment = TextAnchor.UpperCenter; |
| 3 | 394 | | break; |
| | 395 | | } |
| | 396 | |
|
| | 397 | | break; |
| | 398 | | case "bottom": |
| 29 | 399 | | switch (model.hAlign) |
| | 400 | | { |
| | 401 | | case "left": |
| 8 | 402 | | layout.childAlignment = TextAnchor.LowerLeft; |
| 8 | 403 | | break; |
| | 404 | | case "right": |
| 19 | 405 | | layout.childAlignment = TextAnchor.LowerRight; |
| 19 | 406 | | break; |
| | 407 | | default: |
| 2 | 408 | | layout.childAlignment = TextAnchor.LowerCenter; |
| 2 | 409 | | break; |
| | 410 | | } |
| | 411 | |
|
| | 412 | | break; |
| | 413 | | default: // center |
| 304 | 414 | | switch (model.hAlign) |
| | 415 | | { |
| | 416 | | case "left": |
| 2 | 417 | | layout.childAlignment = TextAnchor.MiddleLeft; |
| 2 | 418 | | break; |
| | 419 | | case "right": |
| 2 | 420 | | layout.childAlignment = TextAnchor.MiddleRight; |
| 2 | 421 | | break; |
| | 422 | | default: |
| 300 | 423 | | layout.childAlignment = TextAnchor.MiddleCenter; |
| | 424 | | break; |
| | 425 | | } |
| | 426 | |
|
| | 427 | | break; |
| | 428 | | } |
| 300 | 429 | | } |
| | 430 | |
|
| | 431 | | protected void SetComponentDebugName() |
| | 432 | | { |
| 145 | 433 | | if (referencesContainer == null || model == null) |
| | 434 | | { |
| 0 | 435 | | return; |
| | 436 | | } |
| | 437 | |
|
| 145 | 438 | | referencesContainer.name = componentName; |
| 145 | 439 | | } |
| | 440 | |
|
| | 441 | | public override void Dispose() |
| | 442 | | { |
| 84 | 443 | | if (childHookRectTransform) |
| 51 | 444 | | Utils.SafeDestroy(childHookRectTransform.gameObject); |
| | 445 | |
|
| 84 | 446 | | screenSize.OnChange -= OnScreenResize; |
| | 447 | |
|
| 84 | 448 | | base.Dispose(); |
| 84 | 449 | | } |
| | 450 | |
|
| 114 | 451 | | public virtual void OnChildAttached(UIShape parentComponent, UIShape childComponent) { } |
| | 452 | |
|
| 38 | 453 | | public virtual void OnChildDetached(UIShape parentComponent, UIShape childComponent) { } |
| | 454 | | } |
| | 455 | | } |