| | 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 | | public bool isLayoutDirty { get; protected set; } |
| | 153 | | protected System.Action OnLayoutRefresh; |
| | 154 | |
|
| 0 | 155 | | private BaseVariable<Vector2Int> screenSize => DataStore.i.screen.size; |
| | 156 | |
|
| 0 | 157 | | public UIShape parentUIComponent { get; protected set; } |
| | 158 | |
|
| | 159 | | private Coroutine layoutRefreshWatcher; |
| | 160 | |
|
| 120 | 161 | | public UIShape() |
| | 162 | | { |
| 120 | 163 | | screenSize.OnChange += OnScreenResize; |
| 120 | 164 | | model = new Model(); |
| | 165 | |
|
| 120 | 166 | | if ( layoutRefreshWatcher == null ) |
| 120 | 167 | | layoutRefreshWatcher = CoroutineStarter.Start(LayoutRefreshWatcher()); |
| 120 | 168 | | } |
| | 169 | |
|
| 37 | 170 | | private void OnScreenResize(Vector2Int current, Vector2Int previous) => RefreshAll(); |
| | 171 | |
|
| 0 | 172 | | public override int GetClassId() { return (int) CLASS_ID.UI_IMAGE_SHAPE; } |
| | 173 | |
|
| | 174 | | public string GetDebugName() |
| | 175 | | { |
| 145 | 176 | | Model model = (Model) this.model; |
| | 177 | |
|
| 145 | 178 | | if (string.IsNullOrEmpty(model.name)) |
| | 179 | | { |
| 138 | 180 | | return GetType().Name; |
| | 181 | | } |
| | 182 | | else |
| | 183 | | { |
| 7 | 184 | | return GetType().Name + " - " + model.name; |
| | 185 | | } |
| | 186 | | } |
| | 187 | |
|
| | 188 | | public override IEnumerator ApplyChanges(BaseModel newJson) |
| | 189 | | { |
| 0 | 190 | | return null; |
| | 191 | | } |
| | 192 | |
|
| | 193 | | internal T InstantiateUIGameObject<T>(string prefabPath) where T : UIReferencesContainer |
| | 194 | | { |
| 77 | 195 | | Model model = (Model) this.model; |
| | 196 | |
|
| 77 | 197 | | GameObject uiGameObject = null; |
| | 198 | |
|
| 77 | 199 | | bool targetParentExists = !string.IsNullOrEmpty(model.parentComponent) && |
| | 200 | | scene.disposableComponents.ContainsKey(model.parentComponent); |
| | 201 | |
|
| 77 | 202 | | if (targetParentExists) |
| | 203 | | { |
| 23 | 204 | | if (scene.disposableComponents.ContainsKey(model.parentComponent)) |
| | 205 | | { |
| 23 | 206 | | parentUIComponent = (scene.disposableComponents[model.parentComponent] as UIShape); |
| 23 | 207 | | } |
| | 208 | | else |
| | 209 | | { |
| 0 | 210 | | parentUIComponent = scene.GetSharedComponent<UIScreenSpace>(); |
| | 211 | | } |
| 0 | 212 | | } |
| | 213 | | else |
| | 214 | | { |
| 54 | 215 | | parentUIComponent = scene.GetSharedComponent<UIScreenSpace>(); |
| | 216 | | } |
| | 217 | |
|
| 77 | 218 | | uiGameObject = |
| | 219 | | Object.Instantiate( |
| | 220 | | Resources.Load(prefabPath), |
| | 221 | | parentUIComponent != null ? parentUIComponent.childHookRectTransform : null) as GameObject; |
| | 222 | |
|
| 77 | 223 | | referencesContainer = uiGameObject.GetComponent<T>(); |
| | 224 | |
|
| 77 | 225 | | referencesContainer.rectTransform.SetToMaxStretch(); |
| | 226 | |
|
| 77 | 227 | | childHookRectTransform = referencesContainer.childHookRectTransform; |
| | 228 | |
|
| 77 | 229 | | referencesContainer.owner = this; |
| | 230 | |
|
| 77 | 231 | | return referencesContainer as T; |
| | 232 | | } |
| | 233 | |
|
| | 234 | | IEnumerator LayoutRefreshWatcher() |
| | 235 | | { |
| 53 | 236 | | while (true) |
| | 237 | | { |
| | 238 | | // WaitForEndOfFrame doesn't work in batch mode |
| 4392 | 239 | | yield return Application.isBatchMode ? null : new WaitForEndOfFrame(); |
| | 240 | |
|
| 4272 | 241 | | if ( !isLayoutDirty ) |
| | 242 | | continue; |
| | 243 | |
|
| 53 | 244 | | RefreshAll(); |
| | 245 | | } |
| | 246 | | } |
| | 247 | |
|
| | 248 | | public virtual void RefreshAll() |
| | 249 | | { |
| | 250 | | // We are not using the _Internal here because the method is overridden |
| | 251 | | // by some UI shapes. |
| 90 | 252 | | RefreshDCLLayoutRecursively(refreshSize: true, refreshAlignmentAndPosition: false); |
| 90 | 253 | | FixMaxStretchRecursively(); |
| 90 | 254 | | RefreshDCLLayoutRecursively_Internal(refreshSize: false, refreshAlignmentAndPosition: true); |
| 90 | 255 | | isLayoutDirty = false; |
| 90 | 256 | | OnLayoutRefresh?.Invoke(); |
| 90 | 257 | | OnLayoutRefresh = null; |
| 90 | 258 | | } |
| | 259 | |
|
| | 260 | | public virtual void MarkLayoutDirty( System.Action OnRefresh = null ) |
| | 261 | | { |
| 93 | 262 | | UIShape rootParent = GetRootParent(); |
| | 263 | |
|
| 93 | 264 | | Assert.IsTrue(rootParent != null, "root parent must never be null"); |
| | 265 | |
|
| 93 | 266 | | if (rootParent.referencesContainer == null) |
| 0 | 267 | | return; |
| | 268 | |
|
| 93 | 269 | | rootParent.isLayoutDirty = true; |
| | 270 | |
|
| 93 | 271 | | if ( OnRefresh != null ) |
| 24 | 272 | | rootParent.OnLayoutRefresh += OnRefresh; |
| 93 | 273 | | } |
| | 274 | |
|
| | 275 | | public void RefreshDCLLayout(bool refreshSize = true, bool refreshAlignmentAndPosition = true) |
| | 276 | | { |
| 365 | 277 | | RectTransform parentRT = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 278 | |
|
| 365 | 279 | | if (refreshSize) |
| | 280 | | { |
| 243 | 281 | | RefreshDCLSize(parentRT); |
| | 282 | | } |
| | 283 | |
|
| 365 | 284 | | if (refreshAlignmentAndPosition) |
| | 285 | | { |
| | 286 | | // Alignment (Alignment uses size so we should always align AFTER resizing) |
| 267 | 287 | | RefreshDCLAlignmentAndPosition(parentRT); |
| | 288 | | } |
| 365 | 289 | | } |
| | 290 | |
|
| | 291 | | protected virtual void RefreshDCLSize(RectTransform parentTransform = null) |
| | 292 | | { |
| 227 | 293 | | if (parentTransform == null) |
| 0 | 294 | | parentTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 295 | |
|
| 227 | 296 | | Model model = (Model) this.model; |
| | 297 | |
|
| 227 | 298 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, |
| | 299 | | model.width.GetScaledValue(parentTransform.rect.width)); |
| 227 | 300 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, |
| | 301 | | model.height.GetScaledValue(parentTransform.rect.height)); |
| 227 | 302 | | } |
| | 303 | |
|
| | 304 | | public void RefreshDCLAlignmentAndPosition(RectTransform parentTransform = null) |
| | 305 | | { |
| 267 | 306 | | if (parentTransform == null) |
| 0 | 307 | | parentTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 308 | |
|
| 267 | 309 | | referencesContainer.layoutElement.ignoreLayout = false; |
| 267 | 310 | | ConfigureAlignment(referencesContainer.layoutGroup); |
| 267 | 311 | | Utils.ForceRebuildLayoutImmediate(parentTransform); |
| 267 | 312 | | referencesContainer.layoutElement.ignoreLayout = true; |
| | 313 | |
|
| 267 | 314 | | Model model = (Model) this.model; |
| | 315 | |
|
| | 316 | | // Reposition |
| 267 | 317 | | Vector3 position = Vector3.zero; |
| 267 | 318 | | position.x = model.positionX.GetScaledValue(parentTransform.rect.width); |
| 267 | 319 | | position.y = model.positionY.GetScaledValue(parentTransform.rect.height); |
| | 320 | |
|
| 267 | 321 | | position = Utils.Sanitize(position); |
| 267 | 322 | | referencesContainer.layoutElementRT.localPosition += position; |
| 267 | 323 | | } |
| | 324 | |
|
| | 325 | | public virtual void RefreshDCLLayoutRecursively(bool refreshSize = true, |
| | 326 | | bool refreshAlignmentAndPosition = true) |
| | 327 | | { |
| 114 | 328 | | RefreshDCLLayoutRecursively_Internal(refreshSize, refreshAlignmentAndPosition); |
| 114 | 329 | | } |
| | 330 | |
|
| | 331 | | public void RefreshDCLLayoutRecursively_Internal(bool refreshSize = true, |
| | 332 | | bool refreshAlignmentAndPosition = true) |
| | 333 | | { |
| 204 | 334 | | UIShape rootParent = GetRootParent(); |
| | 335 | |
|
| 204 | 336 | | Assert.IsTrue(rootParent != null, "root parent must never be null"); |
| | 337 | |
|
| 204 | 338 | | if (rootParent.referencesContainer == null) |
| 68 | 339 | | return; |
| | 340 | |
|
| 136 | 341 | | Utils.InverseTransformChildTraversal<UIReferencesContainer>( |
| | 342 | | (x) => |
| | 343 | | { |
| 304 | 344 | | if (x.owner != null) |
| 220 | 345 | | x.owner.RefreshDCLLayout(refreshSize, refreshAlignmentAndPosition); |
| 304 | 346 | | }, |
| | 347 | | rootParent.referencesContainer.transform); |
| 136 | 348 | | } |
| | 349 | |
|
| | 350 | | public void FixMaxStretchRecursively() |
| | 351 | | { |
| 90 | 352 | | UIShape rootParent = GetRootParent(); |
| | 353 | |
|
| 90 | 354 | | Assert.IsTrue(rootParent != null, "root parent must never be null"); |
| | 355 | |
|
| 90 | 356 | | if (rootParent.referencesContainer == null) |
| 34 | 357 | | return; |
| | 358 | |
|
| 56 | 359 | | Utils.InverseTransformChildTraversal<UIReferencesContainer>( |
| | 360 | | (x) => |
| | 361 | | { |
| 140 | 362 | | if (x.owner != null) |
| | 363 | | { |
| 98 | 364 | | x.rectTransform.SetToMaxStretch(); |
| | 365 | | } |
| 140 | 366 | | }, |
| | 367 | | rootParent.referencesContainer.transform); |
| 56 | 368 | | } |
| | 369 | |
|
| | 370 | | protected bool ReparentComponent(RectTransform targetTransform, string targetParent) |
| | 371 | | { |
| 68 | 372 | | bool targetParentExists = !string.IsNullOrEmpty(targetParent) && |
| | 373 | | scene.disposableComponents.ContainsKey(targetParent); |
| | 374 | |
|
| 68 | 375 | | if (targetParentExists && parentUIComponent == scene.disposableComponents[targetParent]) |
| | 376 | | { |
| 22 | 377 | | return false; |
| | 378 | | } |
| | 379 | |
|
| 46 | 380 | | if (parentUIComponent != null) |
| | 381 | | { |
| 46 | 382 | | UIReferencesContainer[] parents = referencesContainer.GetComponentsInParent<UIReferencesContainer>(true) |
| | 383 | |
|
| 186 | 384 | | foreach (var parent in parents) |
| | 385 | | { |
| 47 | 386 | | if (parent.owner != null) |
| | 387 | | { |
| 47 | 388 | | parent.owner.OnChildDetached(parentUIComponent, this); |
| | 389 | | } |
| | 390 | | } |
| | 391 | | } |
| | 392 | |
|
| 46 | 393 | | if (targetParentExists) |
| | 394 | | { |
| 22 | 395 | | parentUIComponent = scene.disposableComponents[targetParent] as UIShape; |
| 22 | 396 | | } |
| | 397 | | else |
| | 398 | | { |
| 24 | 399 | | parentUIComponent = scene.GetSharedComponent<UIScreenSpace>(); |
| | 400 | | } |
| | 401 | |
|
| 46 | 402 | | targetTransform.SetParent(parentUIComponent.childHookRectTransform, false); |
| 46 | 403 | | return true; |
| | 404 | | } |
| | 405 | |
|
| | 406 | | public UIShape GetRootParent() |
| | 407 | | { |
| 393 | 408 | | UIShape parent = null; |
| | 409 | |
|
| 393 | 410 | | if (parentUIComponent != null && !(parentUIComponent is UIScreenSpace)) |
| | 411 | | { |
| 6 | 412 | | parent = parentUIComponent.GetRootParent(); |
| 6 | 413 | | } |
| | 414 | | else |
| | 415 | | { |
| 387 | 416 | | parent = this; |
| | 417 | | } |
| | 418 | |
|
| 393 | 419 | | return parent; |
| | 420 | | } |
| | 421 | |
|
| | 422 | | protected void ConfigureAlignment(LayoutGroup layout) |
| | 423 | | { |
| 267 | 424 | | Model model = (Model) this.model; |
| 267 | 425 | | switch (model.vAlign) |
| | 426 | | { |
| | 427 | | case "top": |
| 7 | 428 | | switch (model.hAlign) |
| | 429 | | { |
| | 430 | | case "left": |
| 2 | 431 | | layout.childAlignment = TextAnchor.UpperLeft; |
| 2 | 432 | | break; |
| | 433 | | case "right": |
| 2 | 434 | | layout.childAlignment = TextAnchor.UpperRight; |
| 2 | 435 | | break; |
| | 436 | | default: |
| 3 | 437 | | layout.childAlignment = TextAnchor.UpperCenter; |
| 3 | 438 | | break; |
| | 439 | | } |
| | 440 | |
|
| | 441 | | break; |
| | 442 | | case "bottom": |
| 26 | 443 | | switch (model.hAlign) |
| | 444 | | { |
| | 445 | | case "left": |
| 8 | 446 | | layout.childAlignment = TextAnchor.LowerLeft; |
| 8 | 447 | | break; |
| | 448 | | case "right": |
| 16 | 449 | | layout.childAlignment = TextAnchor.LowerRight; |
| 16 | 450 | | break; |
| | 451 | | default: |
| 2 | 452 | | layout.childAlignment = TextAnchor.LowerCenter; |
| 2 | 453 | | break; |
| | 454 | | } |
| | 455 | |
|
| | 456 | | break; |
| | 457 | | default: // center |
| 234 | 458 | | switch (model.hAlign) |
| | 459 | | { |
| | 460 | | case "left": |
| 2 | 461 | | layout.childAlignment = TextAnchor.MiddleLeft; |
| 2 | 462 | | break; |
| | 463 | | case "right": |
| 2 | 464 | | layout.childAlignment = TextAnchor.MiddleRight; |
| 2 | 465 | | break; |
| | 466 | | default: |
| 230 | 467 | | layout.childAlignment = TextAnchor.MiddleCenter; |
| | 468 | | break; |
| | 469 | | } |
| | 470 | |
|
| | 471 | | break; |
| | 472 | | } |
| 230 | 473 | | } |
| | 474 | |
|
| | 475 | | protected void SetComponentDebugName() |
| | 476 | | { |
| 145 | 477 | | if (referencesContainer == null || model == null) |
| | 478 | | { |
| 0 | 479 | | return; |
| | 480 | | } |
| | 481 | |
|
| 145 | 482 | | referencesContainer.name = componentName; |
| 145 | 483 | | } |
| | 484 | |
|
| | 485 | | public override void Dispose() |
| | 486 | | { |
| 84 | 487 | | if (childHookRectTransform) |
| 51 | 488 | | Utils.SafeDestroy(childHookRectTransform.gameObject); |
| | 489 | |
|
| 84 | 490 | | screenSize.OnChange -= OnScreenResize; |
| | 491 | |
|
| 84 | 492 | | if ( layoutRefreshWatcher != null ) |
| 84 | 493 | | CoroutineStarter.Stop(layoutRefreshWatcher); |
| | 494 | |
|
| 84 | 495 | | base.Dispose(); |
| 84 | 496 | | } |
| | 497 | |
|
| 114 | 498 | | public virtual void OnChildAttached(UIShape parentComponent, UIShape childComponent) { } |
| | 499 | |
|
| 38 | 500 | | public virtual void OnChildDetached(UIShape parentComponent, UIShape childComponent) { } |
| | 501 | | } |
| | 502 | | } |