Skip to content

Instantly share code, notes, and snippets.

@Intasx
Last active April 14, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Intasx/0f7f8d0fbdd4f49f4bd42ec782eaeb0c to your computer and use it in GitHub Desktop.
Save Intasx/0f7f8d0fbdd4f49f4bd42ec782eaeb0c to your computer and use it in GitHub Desktop.
Makes a typing effect. Created for Miniscript ver. 1.5
// -- Typewriter class --
import "chars"
import "stringUtil"
typewriter = {}
typewriter.defaultSound = new Sound
typewriter.txtDisp = display(3)
typewriter.snd = new Sound
playDefaultSound = function()
typewriter.snd.init(0.05, noteFreq(90))
typewriter.snd.play(0.01)
end function
// Set the initial values.
// x, y: cell coordinates where the text should be printed.
// delay: how fast the typing must be (values between 0.01 and 0.1 work fine.)
// color: color of every cell.
// clear: whether or not to clear the text display every time .type is called.
// playSound: a function that gets executed on every character. Useful to play a sound.
// toPress: key to press to skip the text typing (as returned by key.get)
typewriter.init = function(x=0, y=0, delay=0.05, color="#FF8000FF", clear=true, playSound, toPress=" ")
self.x = x
self.y = y
self.delay = delay
self.color = color
self.clear = clear
if playSound then
self.playSound = @playSound
else
self.playSound = @playDefaultSound
end if
self.toPress = toPress
self.ended = true // if text typing ended or not
self.skip = false // when .toPress key is pressed it shows the whole text
self.canSkip = true // whether the user can skip the text typing or not
self.textWidth = 66 // Where the text should add a new line, in cell coords
end function
// Once called it will start typing the text in x, y coords
typewriter.type = function(text)
self.ended = false
self.skip = false
if self.clear then
self.txtDisp.clear
end if
// Make the text fit the screen adding new lines
text = text.wrap(self.textWidth-self.x).join(chars.returnKey)
initialX = self.x
currentCharX = self.x
currentCharY = self.y
for i in text.indexes
// x coord should go back to the initial value when a new line is found
if text[i] == chars.returnKey then
currentCharX = initialX - i - 1
currentCharY = currentCharY - 1
continue
end if
// Show the character
self.txtDisp.setCellColor currentCharX+i, currentCharY, self.color
self.txtDisp.setCell currentCharX+i, currentCharY, text[i]
if key.available then
if key.get == self.toPress then
self.skip = true
end if
key.clear
end if
if not self.skip then
// Periods and commas must have a little more delay
// TODO: Let the user decide? Let the user set custom delays per character?
if text[i] == "." then
wait self.delay+0.5
else if text[i] == "," then
wait self.delay+0.2
else
// Don't play the sound on spaces
if text[i] != " " then
self.playSound
end if
wait self.delay
end if
end if
end for
self.ended = true
end function
// -- End of typewriter class --
globals.Typewriter = typewriter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment