KayelGee / token-attacher

MIT License
11 stars 18 forks source link

Recalculate attached element offset on 'createToken' #110

Closed Aedif closed 2 months ago

Aedif commented 5 months ago

Hello!

Would it be possible for you to add an additional function call to recalculate the attached element offsets upon a token being created? Essentially going into the 'updateToken' flow without an actual update being performed.

At the moment if a prefab rotation is updated via the Token Configuration window spawning the token on the map will not reflect this new rotation value until a subsequent update is performed such as token movement:

https://github.com/KayelGee/token-attacher/assets/7693704/a638431c-aff2-40b1-8f21-e5e2093e6b6d

A minor problem I know... but! It would greatly assist the Mass Edit module to do prefab manipulation in preview mode:

https://github.com/KayelGee/token-attacher/assets/7693704/52922a91-3447-42c2-acda-c3d4c811b01d

While I could dig deep into the token-attacher flags and reverse engineer the correct values for the attached element offsets it would be far far simpler if you could do that on your end.

Hope you can help out with this, and thanks for a great module!

Aedif commented 5 months ago
static async regenerateAttachedFromPrototype(type, token, prototypeAttached, grid_multi, options={},  return_data = false){
    grid_multi = mergeObject({size:1, w: 1, h:1}, grid_multi);
    let pasted = {};
    let toCreate = {};
    for (const key in prototypeAttached) {
        if (prototypeAttached.hasOwnProperty(key) && key !== "unknown") {
            let layer = TokenAttacher.getLayerOrCollection(key);

            let pos = TokenAttacher.getCenter(type, token.document ?? token);
            if(!toCreate.hasOwnProperty(key)) toCreate[key] = [];
            toCreate[key] = await TokenAttacher.pasteObjects(layer, prototypeAttached[key], pos, grid_multi, {}, true);
            >>> if(toCreate[key]) {
            >>> let updates = await TokenAttacher.offsetPositionOfElements(key, toCreate[key], type, token.document, {});
            >>> for(let i = 0; i< updates.length; i++) {
            >>>     delete updates[i]._id;
            >>>     mergeObject(toCreate[key][i], updates[i])
            >>> }
            >>>}
            if(!toCreate[key]) delete toCreate[key];                    
        }
    }

Something like this seems to do the trick.

KayelGee commented 2 months ago

Added based on your PR in the next release, although the solution is a little different.