| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSRuntime; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ECSComponents |
| | 8 | | { |
| | 9 | | public class UIBackgroundHandler : IECSComponentHandler<PBUiBackground> |
| | 10 | | { |
| | 11 | | private readonly IInternalECSComponent<InternalUiContainer> internalUiContainer; |
| | 12 | | private readonly int componentId; |
| | 13 | |
|
| 4 | 14 | | public UIBackgroundHandler(IInternalECSComponent<InternalUiContainer> internalUiContainer, int componentId) |
| | 15 | | { |
| 4 | 16 | | this.internalUiContainer = internalUiContainer; |
| 4 | 17 | | this.componentId = componentId; |
| 4 | 18 | | } |
| | 19 | |
|
| 0 | 20 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 21 | |
|
| | 22 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 23 | | { |
| 1 | 24 | | RemoveFromContainer(scene, entity); |
| 1 | 25 | | } |
| | 26 | |
|
| | 27 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBUiBackground model) |
| | 28 | | { |
| 6 | 29 | | if (model.BackgroundColor == null || model.BackgroundColor.A == 0) |
| | 30 | | { |
| 2 | 31 | | RemoveFromContainer(scene, entity); |
| 2 | 32 | | } |
| | 33 | | else |
| | 34 | | { |
| 4 | 35 | | var containerModel = internalUiContainer.GetFor(scene, entity)?.model ?? new InternalUiContainer(); |
| 4 | 36 | | containerModel.components.Add(componentId); |
| 4 | 37 | | containerModel.rootElement.style.backgroundColor = model.BackgroundColor.ToUnityColor(); |
| 4 | 38 | | internalUiContainer.PutFor(scene, entity, containerModel); |
| | 39 | | } |
| 4 | 40 | | } |
| | 41 | |
|
| | 42 | | private void RemoveFromContainer(IParcelScene scene, IDCLEntity entity) |
| | 43 | | { |
| 3 | 44 | | var containerData = internalUiContainer.GetFor(scene, entity); |
| 3 | 45 | | if (containerData != null) |
| | 46 | | { |
| 3 | 47 | | var containerModel = containerData.model; |
| 3 | 48 | | if (containerModel.components.Remove(componentId)) |
| | 49 | | { |
| 3 | 50 | | containerModel.rootElement.style.backgroundColor = Color.clear; |
| 3 | 51 | | internalUiContainer.PutFor(scene, entity, containerModel); |
| | 52 | | } |
| | 53 | | } |
| 3 | 54 | | } |
| | 55 | | } |
| | 56 | | } |