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