|
| 1 | +import { Engine, Entity, components, InputAction } from '../../../packages/@dcl/ecs/src' |
| 2 | +import { testComponentSerialization } from './assertion' |
| 3 | + |
| 4 | +const ROOT_ENTITY = 0 as Entity |
| 5 | + |
| 6 | +describe('TouchScreenControls convenience helpers', () => { |
| 7 | + it('hideAll() hides every gamepad button', () => { |
| 8 | + const engine = Engine() |
| 9 | + const TouchScreenControls = components.TouchScreenControls(engine) |
| 10 | + |
| 11 | + TouchScreenControls.hideAll() |
| 12 | + |
| 13 | + const value = TouchScreenControls.getOrNull(ROOT_ENTITY) |
| 14 | + expect(value?.touchInputs.length).toBe(8) |
| 15 | + expect(value?.touchInputs.every((t) => t.hide)).toBe(true) |
| 16 | + }) |
| 17 | + |
| 18 | + it('showAll() clears the button hide list but leaves joystick/crosshair untouched', () => { |
| 19 | + const engine = Engine() |
| 20 | + const TouchScreenControls = components.TouchScreenControls(engine) |
| 21 | + |
| 22 | + TouchScreenControls.hideJoystick() |
| 23 | + TouchScreenControls.hideCrosshair() |
| 24 | + TouchScreenControls.hideAll() |
| 25 | + TouchScreenControls.showAll() |
| 26 | + |
| 27 | + const value = TouchScreenControls.getOrNull(ROOT_ENTITY) |
| 28 | + expect(value?.touchInputs).toEqual([]) |
| 29 | + // showAll only affects buttons — joystick/crosshair stay hidden |
| 30 | + expect(value?.hideJoystick).toBe(true) |
| 31 | + expect(value?.hideCrosshair).toBe(true) |
| 32 | + }) |
| 33 | + |
| 34 | + it('hide() merges the given actions into the current config', () => { |
| 35 | + const engine = Engine() |
| 36 | + const TouchScreenControls = components.TouchScreenControls(engine) |
| 37 | + |
| 38 | + TouchScreenControls.hide([InputAction.IA_JUMP]) |
| 39 | + TouchScreenControls.hide([InputAction.IA_PRIMARY]) |
| 40 | + |
| 41 | + const value = TouchScreenControls.getOrNull(ROOT_ENTITY) |
| 42 | + const hidden = value?.touchInputs.map((t) => t.inputAction) ?? [] |
| 43 | + expect(hidden).toContain(InputAction.IA_JUMP) |
| 44 | + expect(hidden).toContain(InputAction.IA_PRIMARY) |
| 45 | + }) |
| 46 | + |
| 47 | + it('hide() preserves a previously configured icon (no data loss)', () => { |
| 48 | + const engine = Engine() |
| 49 | + const TouchScreenControls = components.TouchScreenControls(engine) |
| 50 | + |
| 51 | + const icon = { tex: { $case: 'texture' as const, texture: { src: 'custom-jump' } } } |
| 52 | + TouchScreenControls.createOrReplace(ROOT_ENTITY, { |
| 53 | + touchInputs: [{ inputAction: InputAction.IA_JUMP, hide: false, icon }], |
| 54 | + hideJoystick: false, |
| 55 | + hideCrosshair: false |
| 56 | + }) |
| 57 | + |
| 58 | + TouchScreenControls.hide([InputAction.IA_JUMP]) |
| 59 | + |
| 60 | + const entry = TouchScreenControls.getOrNull(ROOT_ENTITY)?.touchInputs.find( |
| 61 | + (t) => t.inputAction === InputAction.IA_JUMP |
| 62 | + ) |
| 63 | + expect(entry?.hide).toBe(true) |
| 64 | + expect(entry?.icon).toEqual(icon) |
| 65 | + }) |
| 66 | + |
| 67 | + it('setMainAction() sets the central button action', () => { |
| 68 | + const engine = Engine() |
| 69 | + const TouchScreenControls = components.TouchScreenControls(engine) |
| 70 | + |
| 71 | + TouchScreenControls.setMainAction(InputAction.IA_PRIMARY) |
| 72 | + |
| 73 | + expect(TouchScreenControls.getOrNull(ROOT_ENTITY)?.mainAction).toBe(InputAction.IA_PRIMARY) |
| 74 | + }) |
| 75 | + |
| 76 | + it('hideJoystick()/showJoystick() and hideCrosshair()/showCrosshair() toggle visibility', () => { |
| 77 | + const engine = Engine() |
| 78 | + const TouchScreenControls = components.TouchScreenControls(engine) |
| 79 | + |
| 80 | + TouchScreenControls.hideJoystick() |
| 81 | + TouchScreenControls.hideCrosshair() |
| 82 | + expect(TouchScreenControls.getOrNull(ROOT_ENTITY)?.hideJoystick).toBe(true) |
| 83 | + expect(TouchScreenControls.getOrNull(ROOT_ENTITY)?.hideCrosshair).toBe(true) |
| 84 | + |
| 85 | + TouchScreenControls.showJoystick() |
| 86 | + TouchScreenControls.showCrosshair() |
| 87 | + expect(TouchScreenControls.getOrNull(ROOT_ENTITY)?.hideJoystick).toBe(false) |
| 88 | + expect(TouchScreenControls.getOrNull(ROOT_ENTITY)?.hideCrosshair).toBe(false) |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +describe('Generated TouchScreenControls ProtoBuf', () => { |
| 93 | + it('should serialize/deserialize TouchScreenControls', () => { |
| 94 | + const newEngine = Engine() |
| 95 | + const TouchScreenControls = components.TouchScreenControls(newEngine) |
| 96 | + |
| 97 | + testComponentSerialization(TouchScreenControls, { |
| 98 | + touchInputs: [ |
| 99 | + { inputAction: InputAction.IA_SECONDARY, hide: true, icon: undefined }, |
| 100 | + { |
| 101 | + inputAction: InputAction.IA_JUMP, |
| 102 | + hide: false, |
| 103 | + icon: { |
| 104 | + tex: { |
| 105 | + $case: 'texture', |
| 106 | + texture: { |
| 107 | + src: 'custom-jump', |
| 108 | + wrapMode: undefined, |
| 109 | + filterMode: undefined, |
| 110 | + offset: undefined, |
| 111 | + tiling: undefined |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + ], |
| 117 | + mainAction: InputAction.IA_PRIMARY, |
| 118 | + hideJoystick: true, |
| 119 | + hideCrosshair: true |
| 120 | + }) |
| 121 | + |
| 122 | + testComponentSerialization(TouchScreenControls, { |
| 123 | + touchInputs: [], |
| 124 | + mainAction: undefined, |
| 125 | + hideJoystick: false, |
| 126 | + hideCrosshair: false |
| 127 | + }) |
| 128 | + }) |
| 129 | +}) |
0 commit comments