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