| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSRuntime; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UIElements; |
| | 7 | |
|
| | 8 | | namespace DCL.ECSComponents |
| | 9 | | { |
| | 10 | | public class UITransformHandler : IECSComponentHandler<PBUiTransform> |
| | 11 | | { |
| | 12 | | private readonly IInternalECSComponent<InternalUiContainer> internalUiContainer; |
| | 13 | | private readonly int componentId; |
| | 14 | |
|
| 17 | 15 | | public UITransformHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer, int componentId) |
| | 16 | | { |
| 17 | 17 | | this.internalUiContainer = internalUiContainer; |
| 17 | 18 | | this.componentId = componentId; |
| 17 | 19 | | } |
| | 20 | |
|
| 0 | 21 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 22 | |
|
| | 23 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 24 | | { |
| 1 | 25 | | var containerData = internalUiContainer.GetFor(scene, entity); |
| 1 | 26 | | if (containerData != null) |
| | 27 | | { |
| 1 | 28 | | var containerModel = containerData.model; |
| 1 | 29 | | containerModel.components.Remove(componentId); |
| | 30 | |
|
| | 31 | | // do parent detach only if not child of root entity |
| | 32 | | // since ui element without transform should be always attached |
| | 33 | | // to the root entity |
| 1 | 34 | | if (containerModel.parentId != SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 35 | | { |
| 1 | 36 | | containerModel.parentElement?.Remove(containerModel.rootElement); |
| 1 | 37 | | containerModel.parentElement = null; |
| | 38 | | } |
| 1 | 39 | | containerModel.parentId = SpecialEntityId.SCENE_ROOT_ENTITY; |
| 1 | 40 | | internalUiContainer.PutFor(scene, entity, containerModel); |
| | 41 | | } |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBUiTransform model) |
| | 45 | | { |
| 2 | 46 | | var containerModel = internalUiContainer.GetFor(scene, entity)?.model ?? new InternalUiContainer(); |
| | 47 | |
|
| 2 | 48 | | containerModel.components.Add(componentId); |
| | 49 | |
|
| 2 | 50 | | if (containerModel.parentId != model.Parent) |
| | 51 | | { |
| 1 | 52 | | containerModel.parentId = model.Parent; |
| 1 | 53 | | containerModel.parentElement?.Remove(containerModel.rootElement); |
| 1 | 54 | | containerModel.parentElement = null; |
| | 55 | | } |
| | 56 | |
|
| 2 | 57 | | VisualElement element = containerModel.rootElement; |
| | 58 | |
|
| 2 | 59 | | SetUpVisualElement(element, model); |
| 2 | 60 | | internalUiContainer.PutFor(scene, entity, containerModel); |
| 2 | 61 | | } |
| | 62 | |
|
| | 63 | | private static void SetUpVisualElement(VisualElement element, PBUiTransform model) |
| | 64 | | { |
| 2 | 65 | | element.style.display = GetDisplay(model.Display); |
| 2 | 66 | | element.style.overflow = GetOverflow(model.Overflow); |
| | 67 | |
|
| | 68 | | // Flex |
| 2 | 69 | | element.style.flexDirection = GetFlexDirection(model.FlexDirection); |
| 2 | 70 | | if (!float.IsNaN(model.FlexBasis)) |
| 1 | 71 | | element.style.flexBasis = new Length(model.FlexBasis, GetUnit(model.FlexBasisUnit)); |
| | 72 | | else |
| 1 | 73 | | element.style.flexBasis = new StyleLength(StyleKeyword.Auto); |
| | 74 | |
|
| 2 | 75 | | element.style.flexGrow = model.FlexGrow; |
| 2 | 76 | | element.style.flexShrink = model.FlexShrink; |
| 2 | 77 | | element.style.flexWrap = GetWrap(model.FlexWrap); |
| | 78 | |
|
| | 79 | | // Align |
| 2 | 80 | | if (model.AlignContent != YGAlign.YgaFlexStart) |
| 2 | 81 | | element.style.alignContent = GetAlign(model.AlignContent); |
| 2 | 82 | | if (model.AlignItems != YGAlign.YgaAuto) |
| 1 | 83 | | element.style.alignItems = GetAlign(model.AlignItems); |
| 2 | 84 | | if (model.AlignSelf != YGAlign.YgaAuto) |
| 1 | 85 | | element.style.alignSelf = GetAlign(model.AlignSelf); |
| 2 | 86 | | element.style.justifyContent = GetJustify(model.JustifyContent); |
| | 87 | |
|
| | 88 | | // Layout size |
| 2 | 89 | | if (!float.IsNaN(model.Height)) |
| 2 | 90 | | element.style.height = new Length(model.Height, GetUnit(model.HeightUnit)); |
| 2 | 91 | | if (!float.IsNaN(model.Width)) |
| 2 | 92 | | element.style.width = new Length(model.Width, GetUnit(model.WidthUnit)); |
| | 93 | |
|
| 2 | 94 | | if (!float.IsNaN(model.MaxWidth)) |
| 1 | 95 | | element.style.maxWidth = new Length(model.MaxWidth, GetUnit(model.MaxWidthUnit)); |
| | 96 | | else |
| 1 | 97 | | element.style.maxWidth = new StyleLength(StyleKeyword.Auto); |
| 2 | 98 | | if (!float.IsNaN(model.MaxHeight)) |
| 2 | 99 | | element.style.maxHeight = new Length(model.MaxHeight, GetUnit(model.MaxHeightUnit)); |
| | 100 | | else |
| 0 | 101 | | element.style.maxHeight = new StyleLength(StyleKeyword.Auto); |
| | 102 | |
|
| 2 | 103 | | if (!float.IsNaN(model.MinHeight)) |
| 2 | 104 | | element.style.minHeight = new Length(model.MinHeight, GetUnit(model.MinHeightUnit)); |
| 2 | 105 | | if (!float.IsNaN(model.MinWidth)) |
| 2 | 106 | | element.style.minWidth = new Length(model.MinWidth, GetUnit(model.MinWidthUnit)); |
| | 107 | |
|
| | 108 | | // Paddings |
| 2 | 109 | | if (!Mathf.Approximately(model.PaddingBottom, 0)) |
| 1 | 110 | | element.style.paddingBottom = new Length(model.PaddingBottom, GetUnit(model.PaddingBottomUnit)); |
| 2 | 111 | | if (!Mathf.Approximately(model.PaddingLeft, 0)) |
| 0 | 112 | | element.style.paddingLeft = new Length(model.PaddingLeft, GetUnit(model.PaddingLeftUnit)); |
| 2 | 113 | | if (!Mathf.Approximately(model.PaddingRight, 0)) |
| 1 | 114 | | element.style.paddingRight = new Length(model.PaddingRight, GetUnit(model.PaddingRightUnit)); |
| 2 | 115 | | if (!Mathf.Approximately(model.PaddingTop, 0)) |
| 1 | 116 | | element.style.paddingTop = new Length(model.PaddingTop, GetUnit(model.PaddingTopUnit)); |
| | 117 | |
|
| | 118 | | // Margins |
| 2 | 119 | | if (!Mathf.Approximately(model.MarginLeft, 0)) |
| 0 | 120 | | element.style.marginLeft = new Length(model.MarginLeft, GetUnit(model.MarginLeftUnit)); |
| 2 | 121 | | if (!Mathf.Approximately(model.MarginRight, 0)) |
| 1 | 122 | | element.style.marginRight = new Length(model.MarginRight, GetUnit(model.MarginRightUnit)); |
| 2 | 123 | | if (!Mathf.Approximately(model.MarginBottom, 0)) |
| 1 | 124 | | element.style.marginBottom = new Length(model.MarginBottom, GetUnit(model.MarginBottomUnit)); |
| 2 | 125 | | if (!Mathf.Approximately(model.MarginTop, 0)) |
| 1 | 126 | | element.style.marginTop = new Length(model.MarginTop, GetUnit(model.MarginTopUnit)); |
| | 127 | |
|
| | 128 | | // Borders |
| 2 | 129 | | element.style.borderBottomWidth = model.BorderBottom; |
| 2 | 130 | | element.style.borderLeftWidth = model.BorderLeft; |
| 2 | 131 | | element.style.borderRightWidth = model.BorderRight; |
| 2 | 132 | | element.style.borderTopWidth = model.BorderTop; |
| | 133 | |
|
| | 134 | | // Position |
| 2 | 135 | | element.style.position = GetPosition(model.PositionType); |
| 2 | 136 | | } |
| | 137 | |
|
| | 138 | | private static LengthUnit GetUnit(YGUnit unit) |
| | 139 | | { |
| | 140 | | switch (unit) |
| | 141 | | { |
| | 142 | | case YGUnit.YguPoint: |
| 0 | 143 | | return LengthUnit.Pixel; |
| | 144 | | case YGUnit.YguPercent: |
| 0 | 145 | | return LengthUnit.Percent; |
| | 146 | | default: |
| 0 | 147 | | return LengthUnit.Pixel; |
| | 148 | | } |
| | 149 | | } |
| | 150 | |
|
| | 151 | | private static StyleEnum<Overflow> GetOverflow(YGOverflow overflow) |
| | 152 | | { |
| | 153 | | switch (overflow) |
| | 154 | | { |
| | 155 | | case YGOverflow.YgoVisible: |
| 2 | 156 | | return Overflow.Visible; |
| | 157 | | case YGOverflow.YgoHidden: |
| 0 | 158 | | return Overflow.Hidden; |
| | 159 | | default: |
| 0 | 160 | | return Overflow.Visible; |
| | 161 | | } |
| | 162 | | } |
| | 163 | |
|
| | 164 | | private static StyleEnum<DisplayStyle> GetDisplay(YGDisplay display) |
| | 165 | | { |
| | 166 | | switch (display) |
| | 167 | | { |
| | 168 | | case YGDisplay.YgdFlex: |
| 2 | 169 | | return DisplayStyle.Flex; |
| | 170 | | case YGDisplay.YgdNone: |
| 0 | 171 | | return DisplayStyle.None; |
| | 172 | | default: |
| 0 | 173 | | return DisplayStyle.Flex; |
| | 174 | | } |
| | 175 | | } |
| | 176 | |
|
| | 177 | | private static StyleEnum<Justify> GetJustify(YGJustify justify) |
| | 178 | | { |
| | 179 | | switch (justify) |
| | 180 | | { |
| | 181 | | case YGJustify.YgjFlexStart: |
| 1 | 182 | | return Justify.FlexStart; |
| | 183 | | case YGJustify.YgjCenter: |
| 0 | 184 | | return Justify.Center; |
| | 185 | | case YGJustify.YgjFlexEnd: |
| 0 | 186 | | return Justify.FlexEnd; |
| | 187 | | case YGJustify.YgjSpaceBetween: |
| 0 | 188 | | return Justify.SpaceBetween; |
| | 189 | | case YGJustify.YgjSpaceAround: |
| 1 | 190 | | return Justify.SpaceAround; |
| | 191 | | default: |
| 0 | 192 | | return Justify.FlexStart; |
| | 193 | | } |
| | 194 | | } |
| | 195 | |
|
| | 196 | | private static StyleEnum<Wrap> GetWrap(YGWrap wrap) |
| | 197 | | { |
| | 198 | | switch (wrap) |
| | 199 | | { |
| | 200 | | case YGWrap.YgwNoWrap: |
| 1 | 201 | | return Wrap.NoWrap; |
| | 202 | | case YGWrap.YgwWrap: |
| 0 | 203 | | return Wrap.Wrap; |
| | 204 | | case YGWrap.YgwWrapReverse: |
| 1 | 205 | | return Wrap.WrapReverse; |
| | 206 | | default: |
| 0 | 207 | | return Wrap.Wrap; |
| | 208 | | } |
| | 209 | | } |
| | 210 | |
|
| | 211 | | private static StyleEnum<FlexDirection> GetFlexDirection(YGFlexDirection direction) |
| | 212 | | { |
| | 213 | | switch (direction) |
| | 214 | | { |
| | 215 | | case YGFlexDirection.YgfdColumn: |
| 1 | 216 | | return FlexDirection.Column; |
| | 217 | | case YGFlexDirection.YgfdColumnReverse: |
| 1 | 218 | | return FlexDirection.ColumnReverse; |
| | 219 | | case YGFlexDirection.YgfdRow: |
| 0 | 220 | | return FlexDirection.Row; |
| | 221 | | case YGFlexDirection.YgfdRowReverse: |
| 0 | 222 | | return FlexDirection.RowReverse; |
| | 223 | | default: |
| 0 | 224 | | return FlexDirection.Row; |
| | 225 | | } |
| | 226 | | } |
| | 227 | |
|
| | 228 | | private static StyleEnum<UnityEngine.UIElements.Position> GetPosition(YGPositionType positionType) |
| | 229 | | { |
| | 230 | | switch (positionType) |
| | 231 | | { |
| | 232 | | case YGPositionType.YgptRelative: |
| 0 | 233 | | return UnityEngine.UIElements.Position.Relative; |
| | 234 | | case YGPositionType.YgptAbsolute: |
| 1 | 235 | | return UnityEngine.UIElements.Position.Absolute; |
| | 236 | | default: |
| 1 | 237 | | return UnityEngine.UIElements.Position.Relative; |
| | 238 | | } |
| | 239 | | } |
| | 240 | |
|
| | 241 | | private static StyleEnum<Align> GetAlign(YGAlign align) |
| | 242 | | { |
| | 243 | | switch (align) |
| | 244 | | { |
| | 245 | | case YGAlign.YgaAuto: |
| 1 | 246 | | return Align.Auto; |
| | 247 | | case YGAlign.YgaFlexStart: |
| 0 | 248 | | return Align.FlexStart; |
| | 249 | | case YGAlign.YgaCenter: |
| 2 | 250 | | return Align.Center; |
| | 251 | | case YGAlign.YgaFlexEnd: |
| 0 | 252 | | return Align.FlexEnd; |
| | 253 | | case YGAlign.YgaStretch: |
| 1 | 254 | | return Align.Stretch; |
| | 255 | | default: |
| 0 | 256 | | return Align.Auto; |
| | 257 | | } |
| | 258 | | } |
| | 259 | | } |
| | 260 | | } |