Skip to content

Commit

Permalink
Merge pull request #1 from yaleksander/main
Browse files Browse the repository at this point in the history
added dynamic speed and sound control
  • Loading branch information
SpunkyAmbassador authored Jul 11, 2024
2 parents 99b033a + 33a36c3 commit 0d8e776
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 60 deletions.
144 changes: 85 additions & 59 deletions code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,100 @@ import { RPM } from "../path.js"

const pluginName = "Typewriter Text";

// Typewriter plugin code - Start
let isWaiting = false;
const textSpeed = RPM.Manager.Plugins.getParameter(pluginName, "Text speed variable ID");
const textSound = RPM.Manager.Plugins.getParameter(pluginName, "Text sound variable ID");

RPM.Manager.Plugins.registerCommand(pluginName, "Show Text", (id, text, minTime, maxTime, x, y, width, height) => {
let message = [];
let index = 0;
function updateWindow(id, x, y, width, height, wholeText, count)
{
RPM.Manager.Songs.playSound(RPM.Core.Game.current.variables[textSound], 0.05);
spawnWindow(id, x, y, height, width, wholeText.substring(0, count));
if (count < wholeText.length)
setTimeout(updateWindow, Math.max(RPM.Core.Game.current.variables[textSpeed] * 1000, 1), id, x, y, width, height, wholeText, ++count);
else
for (var i = 0; i < RPM.Manager.Stack.displayedPictures.length; i++)
if (RPM.Manager.Stack.displayedPictures[i][0] === id)
RPM.Manager.Stack.displayedPictures[i][1].typewriterTextPlugin_doneTyping = true;
}

let interval = setInterval(() => {
message.push(text[index]);
spawnWindow(id, x, y, height, width, message.join(""));
index++;
if (index >= text.length) {
clearInterval(interval);
isWaiting = false;
}
}, Math.floor(Math.random() * (maxTime - minTime + 1) + minTime));
RPM.Manager.Plugins.registerCommand(pluginName, "Is done typing", (textID, varID) =>
{
RPM.Core.Game.current.variables[varID] = false;
for (var i = 0; i < RPM.Manager.Stack.displayedPictures.length; i++)
if (RPM.Manager.Stack.displayedPictures[i][0] === textID)
RPM.Core.Game.current.variables[varID] = !!RPM.Manager.Stack.displayedPictures[i][1].typewriterTextPlugin_doneTyping;
});

// Typewriter plugin code - Start
RPM.Manager.Plugins.registerCommand(pluginName, "Show Text", (id, text, x, y, width, height) =>
{
var i;
text = text.toString();
while (true) // not the best practice but works in this scenario
{
i = text.search(/[^\\]\\n/); // regex for "find \n except when it's \\n"
if (i === -1)
break;
text = text.slice(0, i + 1) + "\n" + text.slice(i + 3);
}
updateWindow(id, x, y, width, height, text.replace("\\\\n", "\\n"), 0);
});
// Typewriter plugin code - End

// "Multiple window boxes" plugin code by @Russo - Start
RPM.Core.WindowBox.prototype.draw = function (isChoice = false, windowDimension = this.windowDimension, contentDimension = this.contentDimension) {
if (this.content)
this.content.drawBehind(contentDimension[0], contentDimension[1], contentDimension[2], contentDimension[3]);
// "Multiple window boxes" plugin code by @Russo (https://github.com/yaleksander/RPM-Plugin-Multiple-window-boxes) - Start
RPM.Core.WindowBox.prototype.draw = function (isChoice = false, windowDimension = this.windowDimension, contentDimension = this.contentDimension)
{
if (this.content)
this.content.drawBehind(contentDimension[0], contentDimension[1], contentDimension[2], contentDimension[3]);

// Single line alteration from source code
!!this.customWindowSkin ? this.customWindowSkin.drawBox(windowDimension, this.selected, this.bordersVisible) : RPM.Datas.Systems.getCurrentWindowSkin().drawBox(windowDimension, this.selected, this.bordersVisible);
// Single line alteration from source code
!!this.customWindowSkin ? this.customWindowSkin.drawBox(windowDimension, this.selected, this.bordersVisible) : RPM.Datas.Systems.getCurrentWindowSkin().drawBox(windowDimension, this.selected, this.bordersVisible);

if (this.content) {
if (!isChoice && this.limitContent) {
RPM.Common.Platform.ctx.save();
RPM.Common.Platform.ctx.beginPath();
RPM.Common.Platform.ctx.rect(contentDimension[0], contentDimension[1] -
RPM.Common.ScreenResolution.getScreenY(this.padding[3] / 2), contentDimension[2], contentDimension[3] + RPM.Common.ScreenResolution.getScreenY(this.padding[3]));
RPM.Common.Platform.ctx.clip();
}
if (isChoice)
this.content.drawChoice(contentDimension[0], contentDimension[1], contentDimension[2], contentDimension[3]);
else
this.content.draw(contentDimension[0], contentDimension[1], contentDimension[2], contentDimension[3]);
if (!isChoice && this.limitContent)
RPM.Common.Platform.ctx.restore();
}
if (this.content)
{
if (!isChoice && this.limitContent)
{
RPM.Common.Platform.ctx.save();
RPM.Common.Platform.ctx.beginPath();
RPM.Common.Platform.ctx.rect(contentDimension[0], contentDimension[1] - RPM.Common.ScreenResolution.getScreenY(this.padding[3] / 2), contentDimension[2], contentDimension[3] + RPM.Common.ScreenResolution.getScreenY(this.padding[3]));
RPM.Common.Platform.ctx.clip();
}
if (isChoice)
this.content.drawChoice(contentDimension[0], contentDimension[1], contentDimension[2], contentDimension[3]);
else
this.content.draw(contentDimension[0], contentDimension[1], contentDimension[2], contentDimension[3]);
if (!isChoice && this.limitContent)
RPM.Common.Platform.ctx.restore();
}
}

// Tweaked this code to be a function instead of command
function spawnWindow(id, x, y, width, height, text) {
const value = [id, new RPM.Core.WindowBox(x, y, width, height,
{
content: new RPM.Graphic.Message(text.toString(), -1, 0, 0),
padding: RPM.Core.WindowBox.VERY_SMALL_PADDING_BOX
})];
value[1].content.update();
value[1].customWindowSkin = RPM.Datas.Systems.getCurrentWindowSkin();
const p = RPM.Manager.Stack.displayedPictures;
var ok = false;
for (var i = 0; i < p.length; i++) {
if (id === p[i][0]) {
p[i] = value;
ok = true;
break;
}
else if (id < p[i][0]) {
p.splice(i, 0, value);
ok = true;
break;
}
}
if (!ok)
p.push(value);
function spawnWindow(id, x, y, width, height, text)
{
const value = [id, new RPM.Core.WindowBox(x, y, width, height,
{
content: new RPM.Graphic.Message(text.toString(), -1, 0, 0),
padding: RPM.Core.WindowBox.VERY_SMALL_PADDING_BOX
})];
value[1].content.update();
value[1].customWindowSkin = RPM.Datas.Systems.getCurrentWindowSkin();
const p = RPM.Manager.Stack.displayedPictures;
var ok = false;
for (var i = 0; i < p.length; i++)
{
if (id === p[i][0])
{
p[i] = value;
ok = true;
break;
}
else if (id < p[i][0])
{
p.splice(i, 0, value);
ok = true;
break;
}
}
if (!ok)
p.push(value);
};
// "Multiple window boxes" plugin code by @Russo - End
2 changes: 1 addition & 1 deletion details.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"author":"SpunkyAmbassador","category":3,"commands":[{"defaultParameters":[{"description":"ID of the text box","id":-1,"name":"id"},{"defaultValue":{"k":8,"v":""},"id":-1,"name":"text"},{"defaultValue":{"k":12,"v":50},"description":"Minimum time interval between typing letters in miliseconds","id":-1,"name":"minTime"},{"defaultValue":{"k":12,"v":100},"description":"Maximum time interval between typing letters in miliseconds","id":-1,"name":"maxTime"},{"description":"x position of text box","id":-1,"name":"x"},{"description":"y position of text box","id":-1,"name":"y"},{"defaultValue":{"k":12,"v":100},"description":"Height of text box","id":-1,"name":"height"},{"defaultValue":{"k":12,"v":640},"description":"Width of text box","id":-1,"name":"width"}],"description":"Shows text in typewriting effect. Printing letter by letter.","id":1,"name":"Show Text"}],"description":"Prints messages letter by letter, giving it a typewriter effect.\n\n---\n\nTutorial -\nTo add typewriting text, just use \"Show Text\" command and tweak the parameters to your liking.\n\nTo remove text box, use \"Remove A Picture\" command. The ID of picture has to be same as ID of the box.\n\n---\n\nCredits to @Russo (russodafederal) for \"Multiple Window Boxes\" plugin as majority of code is reused from that plugin.","name":"Typewriter Text","tutorial":"See Description","website":"https://github.com/SpunkyAmbassador/Typewriter-Text"}
{"author":"SpunkyAmbassador","category":3,"commands":[{"defaultParameters":[{"description":"ID of the text box","id":-1,"name":"id"},{"defaultValue":{"k":8,"v":""},"id":-1,"name":"text"},{"description":"x position of text box","id":-1,"name":"x"},{"description":"y position of text box","id":-1,"name":"y"},{"defaultValue":{"k":12,"v":100},"description":"Height of text box","id":-1,"name":"height"},{"defaultValue":{"k":12,"v":640},"description":"Width of text box","id":-1,"name":"width"}],"description":"Shows text in typewriting effect. Printing letter by letter.","id":1,"name":"Show Text"},{"defaultParameters":[{"description":"ID of the text to be checked","id":-1,"name":"textID"},{"description":"ID of the variable to store the result","id":-1,"name":"varID"}],"description":"Checks if a text of given ID is done typing and stores the result (ON/OFF) on a variable","id":2,"name":"Is done typing"}],"defaultParameters":[{"description":"ID of the variable that holds the character typing interval, in seconds","id":-1,"name":"Text speed variable ID"},{"description":"ID of the variable that holds the sound to be played when each character is typed","id":-1,"name":"Text sound variable ID"}],"description":"Prints messages letter by letter, giving it a typewriter effect.\n\n---\n\nTutorial -\nTo add typewriting text, just use \"Show Text\" command and tweak the parameters to your liking.\n\nTo remove text box, use \"Remove A Picture\" command. The ID of picture has to be same as ID of the box.\n\n---\n\nCredits to @Russo (russodafederal) for \"Multiple Window Boxes\" plugin as majority of code is reused from that plugin.","name":"Typewriter Text","parameters":[{"defaultValue":{"k":12,"v":33},"description":"ID of the variable that holds the character typing interval, in seconds","id":-1,"name":"Text speed variable ID"},{"defaultValue":{"k":12,"v":34},"description":"ID of the variable that holds the sound to be played when each character is typed","id":-1,"name":"Text sound variable ID"}],"tutorial":"See Description","type":1,"version":"1.1.0","website":"https://github.com/SpunkyAmbassador/Typewriter-Text"}

0 comments on commit 0d8e776

Please sign in to comment.