From dbbda793adf64bf093fbe4b361bd596f47ce4467 Mon Sep 17 00:00:00 2001 From: Victor-Crawshaw Date: Fri, 6 Dec 2024 21:07:22 -0500 Subject: [PATCH 1/4] feat(targets/sprites): comment in constructor for targets +sprite duplicate method --- src/engine/target.js | 5 +++-- src/sprites/rendered-target.js | 2 +- src/sprites/sprite.js | 12 +++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/engine/target.js b/src/engine/target.js index 166e443697b..114dd127613 100644 --- a/src/engine/target.js +++ b/src/engine/target.js @@ -20,9 +20,10 @@ class Target extends EventEmitter { /** * @param {Runtime} runtime Reference to the runtime. * @param {?Blocks} blocks Blocks instance for the blocks owned by this target. + * @param {?Object.} comments Array of comments owned by this target. * @constructor */ - constructor (runtime, blocks) { + constructor (runtime, blocks, comments) { super(); if (!blocks) { @@ -55,7 +56,7 @@ class Target extends EventEmitter { * Key is the comment id. * @type {Object.} */ - this.comments = {}; + this.comments = comments || {}; /** * Dictionary of custom state for this target. * This can be used to store target-specific custom state for blocks which need it. diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index e4a12e244d9..d3230cce139 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -15,7 +15,7 @@ class RenderedTarget extends Target { * @constructor */ constructor (sprite, runtime) { - super(runtime, sprite.blocks); + super(runtime, sprite.blocks, sprite.comments); /** * Reference to the sprite that this is a render of. diff --git a/src/sprites/sprite.js b/src/sprites/sprite.js index a38fa129c60..c4fbcebcd9e 100644 --- a/src/sprites/sprite.js +++ b/src/sprites/sprite.js @@ -54,6 +54,12 @@ class Sprite { if (this.runtime && this.runtime.audioEngine) { this.soundBank = this.runtime.audioEngine.createBank(); } + /** + * Dictionary of comments for this target. + * Key is the comment id. + * @type {Object.} + */ + this.comments = {}; } /** @@ -145,7 +151,11 @@ class Sprite { newSprite.blocks.createBlock(block); }); - + Object.keys(this.comments).forEach(commentId => { + const newComment = this.comments[commentId]; + newSprite.comments[newComment.id] = newComment; + }); + const allNames = this.runtime.targets.map(t => t.sprite.name); newSprite.name = StringUtil.unusedName(this.name, allNames); From 2080ee3598a015322760b96d56f8aa614c216bad Mon Sep 17 00:00:00 2001 From: John-Crawshaw <124415414+John-Crawshaw@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:08:52 -0500 Subject: [PATCH 2/4] test(target/sprites): ensuring duplicated sprites have the same comment as the original --- test/unit/sprites_rendered-target.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/unit/sprites_rendered-target.js b/test/unit/sprites_rendered-target.js index a9823424a26..0ac65c806eb 100644 --- a/test/unit/sprites_rendered-target.js +++ b/test/unit/sprites_rendered-target.js @@ -45,6 +45,29 @@ test('blocks get new id on duplicate', t => { }); }); +test('comments are duplicated when duplicating target', t => { + const r = new Runtime(); + const s = new Sprite(null, r); + const rt = new RenderedTarget(s, r); + rt.createComment('testCommentId', null, 'testcomment', 0, 0, 100, 100, false); + return rt.duplicate().then(duplicate => { + // ensure duplicated comment exists + t.equal(Object.keys(duplicate.comments).length, 1); + + // ensure comment was duplicated correctly + const dupComment = duplicate.comments.testCommentId; + t.equal(dupComment.id, 'testCommentId'); + t.equal(dupComment.text, 'testcomment'); + t.equal(dupComment.x, 0); + t.equal(dupComment.y, 0); + t.equal(dupComment.width, 100); + t.equal(dupComment.height, 100); + t.equal(dupComment.minimized, false); + + t.end(); + }); +}); + test('direction', t => { const r = new Runtime(); const s = new Sprite(null, r); From d4a6e3d669774c4a5f887d65d9aed5ba4de5332a Mon Sep 17 00:00:00 2001 From: John-Crawshaw <124415414+John-Crawshaw@users.noreply.github.com> Date: Sun, 8 Dec 2024 12:59:06 -0500 Subject: [PATCH 3/4] "test(targets/sprites): added test for duping sprite without comments --- test/unit/sprites_rendered-target.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/unit/sprites_rendered-target.js b/test/unit/sprites_rendered-target.js index 0ac65c806eb..c6400d3626e 100644 --- a/test/unit/sprites_rendered-target.js +++ b/test/unit/sprites_rendered-target.js @@ -68,6 +68,16 @@ test('comments are duplicated when duplicating target', t => { }); }); +test('no comments are duplicated when duplicating target with no comments', t => { + const r = new Runtime(); + const s = new Sprite(null, r); + const rt = new RenderedTarget(s, r); + return rt.duplicate().then(duplicate => { + t.equal(Object.keys(duplicate.comments).length, 0); + t.end(); + }); +}); + test('direction', t => { const r = new Runtime(); const s = new Sprite(null, r); From 3ac84514616cc6506306f5e9f4aded6f8b793cad Mon Sep 17 00:00:00 2001 From: Victor-Crawshaw Date: Sun, 8 Dec 2024 19:39:04 -0500 Subject: [PATCH 4/4] style(sprites): added documentation for sprite.js duplicate method change --- src/sprites/sprite.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sprites/sprite.js b/src/sprites/sprite.js index c4fbcebcd9e..243882e6325 100644 --- a/src/sprites/sprite.js +++ b/src/sprites/sprite.js @@ -151,6 +151,7 @@ class Sprite { newSprite.blocks.createBlock(block); }); + // Logic to handle new sprite retaining the comments from the current sprite Object.keys(this.comments).forEach(commentId => { const newComment = this.comments[commentId]; newSprite.comments[newComment.id] = newComment;