| | 1 | | using DCL.ECS7.InternalComponents; |
| | 2 | | using DCL.ECSRuntime; |
| | 3 | | using Google.Protobuf; |
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | namespace DCL.ECSComponents.UIAbstractElements |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Base class to register deserializer and serializer of UI Component, |
| | 10 | | /// and create a builder for handler. |
| | 11 | | /// </summary> |
| | 12 | | /// <typeparam name="T">Type of component</typeparam> |
| | 13 | | /// <typeparam name="THandler">Type of handler</typeparam> |
| | 14 | | /// <typeparam name="TFeedbackResult">Type of feedback component (such as 'text value' for TextField)</typeparam> |
| | 15 | | public abstract class UIElementRegisterBase<T, THandler, TFeedbackResult> : UIElementRegisterBase<T, THandler> |
| | 16 | | where T: IMessage<T>, new() |
| | 17 | | where THandler: class, IECSComponentHandler<T> |
| | 18 | | where TFeedbackResult : IMessage<TFeedbackResult>, new() |
| | 19 | | { |
| | 20 | | private readonly int feedbackResultComponentId; |
| | 21 | |
|
| | 22 | | protected UIElementRegisterBase(int componentId, int feedbackResultComponentId, |
| | 23 | | ECSComponentsFactory factory, IECSComponentWriter componentWriter, |
| | 24 | | IInternalECSComponent<InternalUiContainer> internalUiContainer, |
| | 25 | | HandlerBuilder handlerBuilder) |
| 42 | 26 | | : base(componentId, factory, componentWriter, internalUiContainer, handlerBuilder) |
| | 27 | | { |
| 42 | 28 | | this.feedbackResultComponentId = feedbackResultComponentId; |
| | 29 | |
|
| 42 | 30 | | factory.AddOrReplaceComponent(feedbackResultComponentId, ProtoSerialization.Deserialize<TFeedbackResult>, |
| 0 | 31 | | () => null); |
| | 32 | |
|
| 42 | 33 | | componentWriter.AddOrReplaceComponentSerializer<TFeedbackResult>(feedbackResultComponentId, ProtoSerializati |
| 42 | 34 | | } |
| | 35 | |
|
| | 36 | | public sealed override void Dispose() |
| | 37 | | { |
| 4 | 38 | | factory.RemoveComponent(feedbackResultComponentId); |
| 4 | 39 | | componentWriter.RemoveComponentSerializer(feedbackResultComponentId); |
| | 40 | |
|
| 4 | 41 | | base.Dispose(); |
| 4 | 42 | | } |
| | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Base class to register deserializer and serializer of UI Component, |
| | 47 | | /// and create a builder for handler. |
| | 48 | | /// </summary> |
| | 49 | | /// <typeparam name="T">Type of component</typeparam> |
| | 50 | | /// <typeparam name="THandler">Type of handler</typeparam> |
| | 51 | | public abstract class UIElementRegisterBase<T, THandler> : IDisposable |
| | 52 | | where T: IMessage<T>, new() |
| | 53 | | where THandler: class, IECSComponentHandler<T> |
| | 54 | | { |
| | 55 | | protected internal delegate THandler HandlerBuilder(IInternalECSComponent<InternalUiContainer> container, int co |
| | 56 | |
|
| | 57 | | protected readonly ECSComponentsFactory factory; |
| | 58 | | protected readonly IECSComponentWriter componentWriter; |
| | 59 | | private readonly int componentId; |
| | 60 | |
|
| | 61 | | protected UIElementRegisterBase(int componentId, ECSComponentsFactory factory, |
| | 62 | | IECSComponentWriter componentWriter, IInternalECSComponent<InternalUiContainer> internalUiContainer, |
| | 63 | | HandlerBuilder handlerBuilder) |
| | 64 | | { |
| | 65 | | factory.AddOrReplaceComponent(componentId, ProtoSerialization.Deserialize<T>, |
| | 66 | | () => handlerBuilder(internalUiContainer, componentId)); |
| | 67 | |
|
| | 68 | | componentWriter.AddOrReplaceComponentSerializer<T>(componentId, ProtoSerialization.Serialize); |
| | 69 | |
|
| | 70 | | this.factory = factory; |
| | 71 | | this.componentWriter = componentWriter; |
| | 72 | | this.componentId = componentId; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | protected virtual void DisposeImpl() { } |
| | 76 | |
|
| | 77 | | public virtual void Dispose() |
| | 78 | | { |
| | 79 | | factory.RemoveComponent(componentId); |
| | 80 | | componentWriter.RemoveComponentSerializer(componentId); |
| | 81 | | DisposeImpl(); |
| | 82 | | } |
| | 83 | | } |
| | 84 | | } |