Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
adjust slots to work with only single attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodBoyDigital committed Jun 6, 2024
1 parent 2995300 commit b648f12
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
61 changes: 38 additions & 23 deletions src/Spine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ export class Spine extends Container implements View
public skeletonBounds: SkeletonBounds;
private _debug?: ISpineDebugRenderer | undefined = undefined;

readonly _slotsObject: { slot: Slot; container: Container }[] = [];
readonly _slotsObject: Record<string, {slot:Slot, container:Container}> = Object.create(null);

private getSlotFromRef(slotRef: number | string | Slot): Slot
{
let slot: Slot | null;
Expand Down Expand Up @@ -503,10 +504,12 @@ export class Spine extends Container implements View
*/
private updateSlotAttachments()
{
for (let i = 0; i < this._slotsObject.length; i++)
for (const i in this._slotsObject)
{
const slotAttachment = this._slotsObject[i];

if (!slotAttachment) continue;

const { slot, container } = slotAttachment;

container.visible = this.skeleton.drawOrder.includes(slot);
Expand Down Expand Up @@ -599,22 +602,38 @@ export class Spine extends Container implements View
* @param container - The container to attach to the slot
* @param slotRef - The slot id or slot to attach to
*/
addSlotObject(slotRef: number | string | Slot, container: Container)
addSlotObject(slot: number | string | Slot, container: Container)
{
this.removeSlotObject(slotRef, container);
slot = this.getSlotFromRef(slot);

container.includeInBuild = false;
// need to check in on the container too...
for (const i in this._slotsObject)
{
if (this._slotsObject[i]?.container === container)
{
this.removeSlotObject(this._slotsObject[i].slot);
}
}

slotRef = this.getSlotFromRef(slotRef);
this.removeSlotObject(slot);

container.includeInBuild = false;

// TODO only add once??
this.addChild(container);

// TODO search for copies... - one container - to one bone!
this._slotsObject.push({
slot: slotRef,
this._slotsObject[slot.data.name] = {
container,
});
slot
};

const renderGroup = this.renderGroup || this.parentRenderGroup;

if (renderGroup)
{
renderGroup.structureDidChange = true;
}
}

/**
Expand All @@ -623,24 +642,20 @@ export class Spine extends Container implements View
* @param container - The container to detach from the slot
* @param slot - The slot id or slot to detach from
*/
removeSlotObject(slot: number | string | Slot, container: Container)
removeSlotObject(slot: number | string | Slot)
{
container.includeInBuild = true;

slot = this.getSlotFromRef(slot);

this.removeChild(container);
const container = this._slotsObject[slot.data.name]?.container;

for (let i = 0; i < this._slotsObject.length; i++)
if (container)
{
const mapping = this._slotsObject[i];
this.removeChild(container);

if (mapping.slot === slot && mapping.container === container)
{
this._slotsObject.splice(i, 1);
break;
}
container.includeInBuild = true;
}

this._slotsObject[slot.data.name] = null;
}

/**
Expand All @@ -649,11 +664,11 @@ export class Spine extends Container implements View
* @param slotRef - The slot id or slot to get the attachment from
* @returns - The container attached to the slot
*/
getSlotObject(slotRef: number | string | Slot)
getSlotObject(slot: number | string | Slot)
{
slotRef = this.getSlotFromRef(slotRef);
slot = this.getSlotFromRef(slot);

return this._slotsObject.find((mapping) => mapping.slot === slotRef)?.container;
return this._slotsObject[slot.data.name].container;
}

updateBounds()
Expand Down
2 changes: 1 addition & 1 deletion src/SpinePipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class SpinePipe implements RenderPipe<Spine>
}
}

const containerAttachment = spine._slotsObject.find((mapping) => mapping.slot === slot);
const containerAttachment = spine._slotsObject[slot.data.name];

if (containerAttachment)
{
Expand Down

0 comments on commit b648f12

Please sign in to comment.