| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Components; |
| | 5 | | using DCL.Controllers; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL.Models; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL |
| | 11 | | { |
| | 12 | | public class RuntimeComponentFactory : IRuntimeComponentFactory |
| | 13 | | { |
| | 14 | | protected delegate IComponent ComponentBuilder(int classId); |
| | 15 | |
|
| 734 | 16 | | protected Dictionary<int, ComponentBuilder> builders = new Dictionary<int, ComponentBuilder>(); |
| | 17 | |
|
| 0 | 18 | | public IPoolableComponentFactory poolableComponentFactory { get; private set; } |
| | 19 | |
|
| | 20 | | public void Initialize() |
| | 21 | | { |
| 734 | 22 | | CoroutineStarter.Start(InitializeCoroutine()); |
| 734 | 23 | | } |
| | 24 | |
|
| | 25 | | IEnumerator InitializeCoroutine() |
| | 26 | | { |
| 1468 | 27 | | yield return null; |
| 1468 | 28 | | poolableComponentFactory.PrewarmPools(); |
| 1468 | 29 | | } |
| | 30 | |
|
| 734 | 31 | | public RuntimeComponentFactory(IPoolableComponentFactory poolableComponentFactory = null) |
| | 32 | | { |
| 734 | 33 | | this.poolableComponentFactory = poolableComponentFactory ?? PoolableComponentFactory.Create(); |
| | 34 | |
|
| | 35 | | // Transform |
| 734 | 36 | | builders.Add((int) CLASS_ID_COMPONENT.TRANSFORM, BuildComponent<DCLTransform>); |
| 734 | 37 | | builders.Add((int) CLASS_ID_COMPONENT.AVATAR_ATTACH, BuildComponent<AvatarAttachComponent>); |
| | 38 | |
|
| | 39 | | // Shapes |
| 734 | 40 | | builders.Add((int) CLASS_ID.BOX_SHAPE, BuildComponent<BoxShape>); |
| 734 | 41 | | builders.Add((int) CLASS_ID.SPHERE_SHAPE, BuildComponent<SphereShape>); |
| 734 | 42 | | builders.Add((int) CLASS_ID.CYLINDER_SHAPE, BuildComponent<CylinderShape>); |
| 734 | 43 | | builders.Add((int) CLASS_ID.CONE_SHAPE, BuildComponent<ConeShape>); |
| 734 | 44 | | builders.Add((int) CLASS_ID.PLANE_SHAPE, BuildComponent<PlaneShape>); |
| 734 | 45 | | builders.Add((int) CLASS_ID.GLTF_SHAPE, BuildComponent<GLTFShape>); |
| 734 | 46 | | builders.Add((int) CLASS_ID.NFT_SHAPE, BuildComponent<NFTShape>); |
| 734 | 47 | | builders.Add((int) CLASS_ID.OBJ_SHAPE, BuildComponent<OBJShape>); |
| 734 | 48 | | builders.Add((int) CLASS_ID_COMPONENT.TEXT_SHAPE, BuildPoolableComponent); |
| | 49 | |
|
| 734 | 50 | | builders.Add((int) CLASS_ID_COMPONENT.BILLBOARD, BuildPoolableComponent); |
| 734 | 51 | | builders.Add((int) CLASS_ID_COMPONENT.SMART_ITEM, BuildPoolableComponent); |
| | 52 | |
|
| | 53 | | // Materials |
| 734 | 54 | | builders.Add((int) CLASS_ID.BASIC_MATERIAL, BuildComponent<BasicMaterial>); |
| 734 | 55 | | builders.Add((int) CLASS_ID.PBR_MATERIAL, BuildComponent<PBRMaterial>); |
| 734 | 56 | | builders.Add((int) CLASS_ID.TEXTURE, BuildComponent<DCLTexture>); |
| 734 | 57 | | builders.Add((int) CLASS_ID.AVATAR_TEXTURE, BuildComponent<DCLAvatarTexture>); |
| | 58 | |
|
| | 59 | | // Audio |
| 734 | 60 | | builders.Add((int) CLASS_ID.AUDIO_CLIP, BuildComponent<DCLAudioClip>); |
| 734 | 61 | | builders.Add((int) CLASS_ID_COMPONENT.AUDIO_SOURCE, BuildPoolableComponent); |
| 734 | 62 | | builders.Add((int) CLASS_ID_COMPONENT.AUDIO_STREAM, BuildPoolableComponent); |
| | 63 | |
|
| | 64 | | // UI |
| 734 | 65 | | builders.Add((int) CLASS_ID.UI_INPUT_TEXT_SHAPE, BuildComponent<UIInputText>); |
| 734 | 66 | | builders.Add((int) CLASS_ID.UI_FULLSCREEN_SHAPE, BuildComponent<UIScreenSpace>); |
| 734 | 67 | | builders.Add((int) CLASS_ID.UI_SCREEN_SPACE_SHAPE, BuildComponent<UIScreenSpace>); |
| 734 | 68 | | builders.Add((int) CLASS_ID.UI_CONTAINER_RECT, BuildComponent<UIContainerRect>); |
| 734 | 69 | | builders.Add((int) CLASS_ID.UI_SLIDER_SHAPE, BuildComponent<UIScrollRect>); |
| 734 | 70 | | builders.Add((int) CLASS_ID.UI_CONTAINER_STACK, BuildComponent<UIContainerStack>); |
| 734 | 71 | | builders.Add((int) CLASS_ID.UI_IMAGE_SHAPE, BuildComponent<UIImage>); |
| 734 | 72 | | builders.Add((int) CLASS_ID.UI_TEXT_SHAPE, BuildComponent<UIText>); |
| 734 | 73 | | builders.Add((int) CLASS_ID.FONT, BuildComponent<DCLFont>); |
| | 74 | |
|
| | 75 | | // Video |
| 734 | 76 | | builders.Add((int) CLASS_ID.VIDEO_CLIP, BuildComponent<DCLVideoClip>); |
| 734 | 77 | | builders.Add((int) CLASS_ID.VIDEO_TEXTURE, BuildComponent<DCLVideoTexture>); |
| | 78 | |
|
| | 79 | | // Builder in world |
| 734 | 80 | | builders.Add((int) CLASS_ID.NAME, BuildComponent<DCLName>); |
| 734 | 81 | | builders.Add((int) CLASS_ID.LOCKED_ON_EDIT, BuildComponent<DCLLockedOnEdit>); |
| 734 | 82 | | builders.Add((int) CLASS_ID.VISIBLE_ON_EDIT, BuildComponent<DCLVisibleOnEdit>); |
| | 83 | |
|
| | 84 | | // Events |
| 734 | 85 | | builders.Add((int) CLASS_ID_COMPONENT.UUID_ON_UP, BuildUUIDComponent<OnPointerUp>); |
| 734 | 86 | | builders.Add((int) CLASS_ID_COMPONENT.UUID_ON_DOWN, BuildUUIDComponent<OnPointerDown>); |
| 734 | 87 | | builders.Add((int) CLASS_ID_COMPONENT.UUID_ON_CLICK, BuildUUIDComponent<OnClick>); |
| 734 | 88 | | builders.Add((int) CLASS_ID_COMPONENT.UUID_ON_HOVER_ENTER, BuildUUIDComponent<OnPointerHoverEnter>); |
| 734 | 89 | | builders.Add((int) CLASS_ID_COMPONENT.UUID_ON_HOVER_EXIT, BuildUUIDComponent<OnPointerHoverExit>); |
| | 90 | |
|
| | 91 | | // Others |
| 734 | 92 | | builders.Add((int) CLASS_ID_COMPONENT.AVATAR_SHAPE, BuildPoolableComponent); |
| 734 | 93 | | builders.Add((int) CLASS_ID_COMPONENT.ANIMATOR, BuildPoolableComponent); |
| 734 | 94 | | builders.Add((int) CLASS_ID_COMPONENT.GIZMOS, BuildPoolableComponent); |
| 734 | 95 | | builders.Add((int) CLASS_ID_COMPONENT.AVATAR_MODIFIER_AREA, BuildPoolableComponent); |
| 734 | 96 | | builders.Add((int) CLASS_ID_COMPONENT.QUEST_TRACKING_INFORMATION, BuildPoolableComponent); |
| 734 | 97 | | builders.Add((int) CLASS_ID_COMPONENT.CAMERA_MODE_AREA, BuildComponent<CameraModeArea>); |
| | 98 | |
|
| 734 | 99 | | CoroutineStarter.Start(InitializeCoroutine()); |
| 734 | 100 | | } |
| | 101 | |
|
| 47 | 102 | | private IComponent BuildPoolableComponent(int classId) { return poolableComponentFactory.CreateItemFromId<BaseCo |
| | 103 | |
|
| | 104 | | protected T BuildComponent<T>(int classId) |
| | 105 | | where T : IComponent, new() |
| | 106 | | { |
| 834 | 107 | | return new T(); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | private T BuildUUIDComponent<T>(int classId) |
| | 111 | | where T : UnityEngine.Component |
| | 112 | | { |
| 53 | 113 | | var go = new GameObject("UUID Component"); |
| 53 | 114 | | T newComponent = go.GetOrCreateComponent<T>(); |
| 53 | 115 | | return newComponent; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | public IComponent CreateComponent(int classId) |
| | 119 | | { |
| 934 | 120 | | if (!builders.ContainsKey(classId)) |
| | 121 | | { |
| 0 | 122 | | Debug.LogError($"Unknown classId"); |
| 0 | 123 | | return null; |
| | 124 | | } |
| | 125 | |
|
| 934 | 126 | | IComponent newComponent = builders[classId](classId); |
| | 127 | |
|
| 934 | 128 | | return newComponent; |
| | 129 | | } |
| | 130 | |
|
| | 131 | | public void Dispose() |
| | 132 | | { |
| 734 | 133 | | } |
| | 134 | | } |
| | 135 | | } |