| | 1 | | using System; |
| | 2 | | using DCL.ECS7; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSComponents; |
| | 5 | | using DCL.ECSRuntime; |
| | 6 | |
|
| | 7 | | namespace ECSSystems.InputSenderSystem |
| | 8 | | { |
| | 9 | | public static class ECSInputSenderSystem |
| | 10 | | { |
| | 11 | | private class State |
| | 12 | | { |
| | 13 | | public IInternalECSComponent<InternalInputEventResults> inputResultComponent; |
| | 14 | | public IECSComponentWriter componentWriter; |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public static Action CreateSystem( |
| | 18 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 19 | | IECSComponentWriter componentWriter) |
| | 20 | | { |
| 4 | 21 | | var state = new State() |
| | 22 | | { |
| | 23 | | inputResultComponent = inputResultComponent, |
| | 24 | | componentWriter = componentWriter |
| | 25 | | }; |
| 7 | 26 | | return () => Update(state); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | private static void Update(State state) |
| | 30 | | { |
| 3 | 31 | | var inputResults = state.inputResultComponent.GetForAll(); |
| 3 | 32 | | var writer = state.componentWriter; |
| | 33 | |
|
| 12 | 34 | | for (int i = 0; i < inputResults.Count; i++) |
| | 35 | | { |
| 3 | 36 | | var model = inputResults[i].value.model; |
| 3 | 37 | | if (!model.dirty) |
| | 38 | | continue; |
| | 39 | |
|
| 2 | 40 | | var scene = inputResults[i].value.scene; |
| 2 | 41 | | var entity = inputResults[i].value.entity; |
| | 42 | |
|
| 2 | 43 | | PBPointerEventsResult result = new PBPointerEventsResult(); |
| 2 | 44 | | result.Commands.Capacity = model.events.Count; |
| | 45 | |
|
| | 46 | | // using foreach to iterate through queue without removing it elements |
| | 47 | | // if it proves too slow we should switch the queue for a list |
| 24 | 48 | | foreach (InternalInputEventResults.EventData inputEvent in model.events) |
| | 49 | | { |
| 10 | 50 | | result.Commands.Add(new PBPointerEventsResult.Types.PointerCommand() |
| | 51 | | { |
| | 52 | | Analog = inputEvent.analog, |
| | 53 | | Button = inputEvent.button, |
| | 54 | | Hit = inputEvent.hit, |
| | 55 | | State = inputEvent.type, |
| | 56 | | Timestamp = inputEvent.timestamp |
| | 57 | | }); |
| | 58 | | } |
| | 59 | |
|
| 2 | 60 | | writer.PutComponent(scene.sceneData.id, |
| | 61 | | entity.entityId, |
| | 62 | | ComponentID.POINTER_EVENTS_RESULT, |
| | 63 | | result, |
| | 64 | | ECSComponentWriteType.SEND_TO_SCENE | ECSComponentWriteType.WRITE_STATE_LOCALLY); |
| | 65 | | } |
| 3 | 66 | | } |
| | 67 | | } |
| | 68 | | } |