Skip to content

Commit

Permalink
added "write" command to the stage's PEN palette
Browse files Browse the repository at this point in the history
prints text in proportional font at specified size wrapping lines "scrolling" to the end
  • Loading branch information
jmoenig committed Nov 24, 2023
1 parent e705116 commit a6d4b3d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## in development:
* **New Features:**
* new 'text' list conversion selector, recursively joins all text and number leaf elements with spaces in between, filtering out and ignoring whitespace
* added "write" command to the stage's PEN primitives palette - prints text in proportional font at specified size wrapping lines "scrolling" to the end
* **Notable Changes:**
* "reshape" now treats zero-ish (0, "", false) values in its dimension input as place-holders to accomodate the whole source list
* updated "Just Words" library for the new "text" list selector, removed now redundant "append words" reporter
Expand All @@ -16,7 +17,8 @@
2023-11-24
* objects: optimized scanning variable memory for function dependencies - speeds up editing custom blocks in projects with large lists
* lists: turned off experimental list indices wrapping by default, not sure whether this is a good idea after all
* blocks: fixed "relabel" for HOF primitives (MAP, FIND, KEEP, COMBINE)
* blocks: fixed "relabel" for HOF primitives (MAP, FIND, KEEP, COMBINE)
* objects: added "write" command to the stage's PEN primitives palette - prints text in proportional font at specified size wrapping lines "scrolling" to the end

2023-11-23
* lists, gui: let list indices wrap around bounds, e.g. 0 returns the last element, -1 the second last etc.
Expand Down
51 changes: 51 additions & 0 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9636,6 +9636,8 @@ StageMorph.prototype.blockTemplates = function (
blocks.push(block('changeBackgroundColorDimension'));
blocks.push(block('setBackgroundColorDimension'));
blocks.push('-');
blocks.push(block('write'));
blocks.push('-');
blocks.push(block('reportPenTrailsAsCostume'));
blocks.push('-');
blocks.push(block('doPasteOn'));
Expand Down Expand Up @@ -10148,6 +10150,55 @@ StageMorph.prototype.setColorDimension = function (idx, num) {
this.rerender();
};

// StageMorph writing on the trails canvas

StageMorph.prototype.write = function (text, size) { // +++
var fontSize = Math.max(3, +size || 1) * this.scale,
textMorph, ctx, img;

// make sure text can be printed
if (typeof text !== 'string' && typeof text !== 'number') {
throw new Error(
localize('can only write text or numbers, not a') + ' ' +
typeof text
);
}

// use a TextMorph for layout
textMorph = new TextMorph(
text,
fontSize,
null, // fontStyle
null, // bold - this.bubbleFontIsBold,
null, // italic
null, // alignment 'center'
this.width() - fontSize
);

// try to contrast the background
textMorph.setColor(this.getColorDimension(2) > 50 ? BLACK : WHITE);

// stamp the text onstage
ctx = this.penTrails().getContext('2d');
img = textMorph.getImage();
if (img.width < 1 || (img.height < 1)) {
// too small to draw
return;
}
ctx.save();
ctx.scale(1 / this.scale, 1 / this.scale);
ctx.drawImage(
img,
fontSize / 2,
Math.min(0, (this.height() - img.height))
);
ctx.restore();
this.changed();
this.cachedPenTrailsMorph = null;
};

// StageMorph "pen" attributes for the background

StageMorph.prototype.getColorDimension =
SpriteMorph.prototype.getColorDimension;

Expand Down

0 comments on commit a6d4b3d

Please sign in to comment.