Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into protocol-squad
Browse files Browse the repository at this point in the history
  • Loading branch information
leanmendoza committed Dec 19, 2024
2 parents c9ef0d5 + 0c727b8 commit 759139d
Show file tree
Hide file tree
Showing 67 changed files with 1,464 additions and 534 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"--async-stack-traces"
],
"args": [
"${fileBasename}",
"${file}",
"--verbose",
"--no-cache",
"-i"
Expand Down
44 changes: 44 additions & 0 deletions packages/@dcl/ecs/src/components/extended/InputModifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { IEngine, LastWriteWinElementSetComponentDefinition } from '../../engine'
import { InputModifier, PBInputModifier_StandardInput, PBInputModifier } from '../generated/index.gen'
import {} from '../generated/InputModifier.gen'

/**
* @public
*/
export interface InputModifierHelper {
/**
* @returns a input modifier mode
*/
Standard: (standard: PBInputModifier_StandardInput) => PBInputModifier['mode']
}

/**
* @public
*/
export interface InputModifierComponentDefinitionExtended
extends LastWriteWinElementSetComponentDefinition<PBInputModifier> {
/**
* InputModifier helper with constructor
*/
Mode: InputModifierHelper
}

const InputModifierHelper: InputModifierHelper = {
Standard(standard: PBInputModifier_StandardInput) {
return {
$case: 'standard',
standard
}
}
}

export function defineInputModifierComponent(
engine: Pick<IEngine, 'defineComponentFromSchema'>
): InputModifierComponentDefinitionExtended {
const theComponent = InputModifier(engine)

return {
...theComponent,
Mode: InputModifierHelper
}
}
5 changes: 5 additions & 0 deletions packages/@dcl/ecs/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { defineTransformComponent, TransformComponentExtended } from './manual/T
import { AudioStreamComponentDefinitionExtended, defineAudioStreamComponent } from './extended/AudioStream'
import { MediaState } from './generated/pb/decentraland/sdk/components/common/media_state.gen'
import { defineVirtualCameraComponent, VirtualCameraComponentDefinitionExtended } from './extended/VirtualCamera'
import { defineInputModifierComponent, InputModifierComponentDefinitionExtended } from './extended/InputModifier'

export * from './generated/index.gen'

Expand Down Expand Up @@ -60,6 +61,10 @@ export const Tween: LwwComponentGetter<TweenComponentDefinitionExtended> = (engi
export const VirtualCamera: LwwComponentGetter<VirtualCameraComponentDefinitionExtended> = (engine) =>
defineVirtualCameraComponent(engine)

/* @__PURE__*/
export const InputModifier: LwwComponentGetter<InputModifierComponentDefinitionExtended> = (engine) =>
defineInputModifierComponent(engine)

/**
* @alpha
*/
Expand Down
1 change: 1 addition & 0 deletions packages/@dcl/ecs/src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export type { NameComponent, NameType } from './manual/Name'
export type { ISyncComponents, ISyncComponentsType } from './manual/SyncComponents'
export type { INetowrkEntity, INetowrkEntityType } from './manual/NetworkEntity'
export type { INetowrkParent, INetowrkParentType } from './manual/NetworkParent'
export type { InputModifierHelper, InputModifierComponentDefinitionExtended } from './extended/InputModifier'
5 changes: 4 additions & 1 deletion packages/@dcl/ecs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
TweenComponentDefinitionExtended,
INetowrkEntity,
INetowrkParent,
VirtualCameraComponentDefinitionExtended
VirtualCameraComponentDefinitionExtended,
InputModifierComponentDefinitionExtended
} from './components/types'
import { NameComponent } from './components/manual/Name'

Expand All @@ -47,6 +48,8 @@ export const MeshCollider: MeshColliderComponentDefinitionExtended = /* @__PURE_
export const Name: NameComponent = components.Name(engine)
export const Tween: TweenComponentDefinitionExtended = /* @__PURE__*/ components.Tween(engine)
export const VirtualCamera: VirtualCameraComponentDefinitionExtended = /* @__PURE__*/ components.VirtualCamera(engine)
export const InputModifier: InputModifierComponentDefinitionExtended = /* @__PURE__*/ components.InputModifier(engine)

/**
* @alpha
* This is going to be used for sync components through a server.
Expand Down
15 changes: 7 additions & 8 deletions packages/@dcl/ecs/src/systems/crdt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,14 @@ export function crdtSceneSystem(engine: PreEngine, onProcessEntityComponentChang
...message,
messageBuffer: buffer.buffer().subarray(offset, buffer.currentWriteOffset())
})
}
if (onProcessEntityComponentChange) {
const rawValue =
message.type === CrdtMessageType.PUT_COMPONENT || message.type === CrdtMessageType.APPEND_VALUE
? component.get(message.entityId)
: undefined

if (onProcessEntityComponentChange) {
const rawValue =
message.type === CrdtMessageType.PUT_COMPONENT || message.type === CrdtMessageType.APPEND_VALUE
? component.get(message.entityId)
: undefined

onProcessEntityComponentChange(message.entityId, message.type, component, rawValue)
}
onProcessEntityComponentChange(message.entityId, message.type, component, rawValue)
}
}
}
Expand Down
81 changes: 78 additions & 3 deletions packages/@dcl/ecs/src/systems/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ export interface PointerEventsSystem {
*/
removeOnPointerUp(entity: Entity): void

/**
* @public
* Remove the callback for onPointerHoverEnter event
* @param entity - Entity where the callback was attached
*/
removeOnPointerHoverEnter(entity: Entity): void

/**
* @public
* Remove the callback for onPointerHoverLeave event
* @param entity - Entity where the callback was attached
*/
removeOnPointerHoverLeave(entity: Entity): void

/**
* @internal
* Execute callback when the user clicks the entity.
Expand Down Expand Up @@ -88,6 +102,28 @@ export interface PointerEventsSystem {
* @param opts - Opts to trigger Feedback and Button
*/
onPointerUp(entity: Entity, cb: EventSystemCallback, opts?: Partial<EventSystemOptions>): void

/**
* @public
* Execute callback when the user place the pointer over the entity
* @param pointerData - Entity to attach the callback - Opts to trigger Feedback and Button
* @param cb - Function to execute when click fires
*/
onPointerHoverEnter(
pointerData: { entity: Entity; opts?: Partial<EventSystemOptions> },
cb: EventSystemCallback
): void

/**
* @public
* Execute callback when the user take the pointer out of the entity
* @param pointerData - Entity to attach the callback - Opts to trigger Feedback and Button
* @param cb - Function to execute when click fires
*/
onPointerHoverLeave(
pointerData: { entity: Entity; opts?: Partial<EventSystemOptions> },
cb: EventSystemCallback
): void
}

/**
Expand All @@ -100,7 +136,9 @@ export function createPointerEventsSystem(engine: IEngine, inputSystem: IInputSy
enum EventType {
Click,
Down,
Up
Up,
HoverEnter,
HoverLeave
}
type EventMapType = Map<EventType, { cb: EventSystemCallback; opts: EventSystemOptions }>

Expand Down Expand Up @@ -135,6 +173,10 @@ export function createPointerEventsSystem(engine: IEngine, inputSystem: IInputSy
function getPointerEvent(eventType: EventType) {
if (eventType === EventType.Up) {
return PointerEventType.PET_UP
} else if (eventType === EventType.HoverLeave) {
return PointerEventType.PET_HOVER_LEAVE
} else if (eventType === EventType.HoverEnter) {
return PointerEventType.PET_HOVER_ENTER
}
return PointerEventType.PET_DOWN
}
Expand Down Expand Up @@ -164,7 +206,12 @@ export function createPointerEventsSystem(engine: IEngine, inputSystem: IInputSy
checkNotThenable(cb(command.up), 'Click event returned a thenable. Only synchronous functions are allowed')
}

if (eventType === EventType.Down || eventType === EventType.Up) {
if (
eventType === EventType.Down ||
eventType === EventType.Up ||
eventType === EventType.HoverEnter ||
eventType === EventType.HoverLeave
) {
const command = inputSystem.getInputCommand(opts.button, getPointerEvent(eventType), entity)
if (command) {
checkNotThenable(cb(command), 'Event handler returned a thenable. Only synchronous functions are allowed')
Expand Down Expand Up @@ -198,6 +245,24 @@ export function createPointerEventsSystem(engine: IEngine, inputSystem: IInputSy
setPointerEvent(entity, PointerEventType.PET_UP, options)
}

const onPointerHoverEnter: PointerEventsSystem['onPointerHoverEnter'] = (...args) => {
const [data, cb] = args
const { entity, opts } = data
const options = getDefaultOpts(opts)
removeEvent(entity, EventType.HoverEnter)
getEvent(entity).set(EventType.HoverEnter, { cb, opts: options })
setPointerEvent(entity, PointerEventType.PET_HOVER_ENTER, options)
}

const onPointerHoverLeave: PointerEventsSystem['onPointerHoverLeave'] = (...args) => {
const [data, cb] = args
const { entity, opts } = data
const options = getDefaultOpts(opts)
removeEvent(entity, EventType.HoverLeave)
getEvent(entity).set(EventType.HoverLeave, { cb, opts: options })
setPointerEvent(entity, PointerEventType.PET_HOVER_LEAVE, options)
}

return {
removeOnClick(entity: Entity) {
removeEvent(entity, EventType.Click)
Expand All @@ -211,6 +276,14 @@ export function createPointerEventsSystem(engine: IEngine, inputSystem: IInputSy
removeEvent(entity, EventType.Up)
},

removeOnPointerHoverEnter(entity: Entity) {
removeEvent(entity, EventType.HoverEnter)
},

removeOnPointerHoverLeave(entity: Entity) {
removeEvent(entity, EventType.HoverLeave)
},

onClick(value, cb) {
const { entity } = value
const options = getDefaultOpts(value.opts)
Expand All @@ -223,6 +296,8 @@ export function createPointerEventsSystem(engine: IEngine, inputSystem: IInputSy
},

onPointerDown,
onPointerUp
onPointerUp,
onPointerHoverEnter,
onPointerHoverLeave
}
}
Loading

0 comments on commit 759139d

Please sign in to comment.