-
-
Save vegeta897/c018eb4cf7c2d90408c03445c6e6484d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TextBox.prototype.scrollMessage = function(cb) { | |
var self = this; | |
function complete() { | |
self.remove(); | |
cb(); | |
} | |
this.textMetrics = TextBlotter.calculateMetrics({ text: this.text, maxWidth: TEXTBOX_MAX_WIDTH }); | |
if(this.text.trim() === '' || this.textMetrics.lines.length === 0 | |
|| this.textMetrics.lines[0].chars.length === 0) { // No message to show | |
complete(); | |
return; | |
} | |
var scrollSpeed = calcScrollSpeed(this.text); | |
var lineNumber = 0; | |
var lineChar = 0; | |
var lineChars = self.textMetrics.lines[lineNumber].chars.length; | |
for(var nl = 1; nl < TEXTBOX_LINES_PER_PAGE; nl++) { | |
var nextLine = self.textMetrics.lines[lineNumber + nl]; | |
if(nextLine) lineChars += nextLine.chars.length; else break; | |
} | |
//console.log(this.parent.username,'says:',this.text); | |
var addLetter = function() { | |
lineChar++; | |
self.blotText({ | |
text: self.text, metrics: self.textMetrics, maxChars: lineChar, | |
lineStart: lineNumber, lineCount: TEXTBOX_LINES_PER_PAGE | |
}); | |
if(lineChar === lineChars) { // Line set finished? | |
lineNumber += TEXTBOX_LINES_PER_PAGE; | |
if(lineNumber >= self.textMetrics.lines.length) { // Last line complete? | |
self.tickDelay(function() { | |
self.tickRepeat(function(progress) { | |
self.canvas = TextBlotter.transition({ | |
bg: TEXTBOX_BG_COLOR, metrics: self.textMetrics, progress: 1 - progress.percent, | |
lineCount : Math.min(self.textMetrics.lines.length, TEXTBOX_LINES_PER_PAGE) | |
}); | |
self.updateScreen(); | |
self.updateSprite(); | |
}, TEXTBOX_CLOSE_TIME, complete); | |
}, scrollSpeed * TEXTBOX_FINAL_DELAY); | |
} else { // Still more lines | |
lineChar = 0; | |
lineChars = self.textMetrics.lines[lineNumber].chars.length; | |
for(var nl = 1; nl < TEXTBOX_LINES_PER_PAGE; nl++) { | |
nextLine = self.textMetrics.lines[lineNumber + nl]; | |
if(nextLine) lineChars += nextLine.chars.length; else break; | |
} | |
self.tickDelay(addLetter, scrollSpeed * TEXTBOX_PAGE_DELAY); // Begin next line | |
} | |
} else { // Line not finished, continue | |
self.tickDelay(addLetter, scrollSpeed); | |
} | |
}; | |
this.tickRepeat(function(progress) { | |
self.canvas = TextBlotter.transition({ | |
bg: TEXTBOX_BG_COLOR, metrics: self.textMetrics, progress: progress.percent, | |
lineCount : Math.min(self.textMetrics.lines.length, TEXTBOX_LINES_PER_PAGE) | |
}); | |
self.updateScreen(); | |
self.updateSprite(); | |
}, TEXTBOX_OPEN_TIME, function() { | |
self.tickDelay(addLetter, scrollSpeed) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment