Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give Sprites Comment Attribute and allow comment duplication #5111

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/engine/target.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string, *>} comments Array of comments owned by this target.
* @constructor
*/
constructor (runtime, blocks) {
constructor (runtime, blocks, comments) {
super();

if (!blocks) {
Expand Down Expand Up @@ -55,7 +56,7 @@ class Target extends EventEmitter {
* Key is the comment id.
* @type {Object.<string,*>}
*/
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.
Expand Down
2 changes: 1 addition & 1 deletion src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion src/sprites/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<string.*>}
*/
this.comments = {};
}

/**
Expand Down Expand Up @@ -145,7 +151,12 @@ 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;
});

const allNames = this.runtime.targets.map(t => t.sprite.name);
newSprite.name = StringUtil.unusedName(this.name, allNames);

Expand Down
33 changes: 33 additions & 0 deletions test/unit/sprites_rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@ 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('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);
Expand Down
Loading