Skip to content

Instantly share code, notes, and snippets.

@KyoPanda
Last active June 4, 2017 20:11
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 KyoPanda/28102d6187f57ada8e0029064dacff60 to your computer and use it in GitHub Desktop.
Save KyoPanda/28102d6187f57ada8e0029064dacff60 to your computer and use it in GitHub Desktop.
Panda's Text Justify
/*global Window_Base, Window_Message*/
/*:
* @plugindesc
* Makes the text within messages windows justified.
*
* @author Kyo Panda
*
* @help
*
* MIT License
*
* Copyright (c) 2017 Kyo Panda
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*:pt
* @plugindesc
* Faz com que os textos nas janelas de mensagens sejam justificados.
*
* @author Kyo Panda
*
* @help
*
* MIT License
*
* Copyright (c) 2017 Kyo Panda
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function() {
'use strict';
/**
* Variable conventionally used for identifying imported plugins.
*
* @namespace Imported
* @type {Object}
*/
var Imported = window.Imported || {};
/**
* Marks Panda's Text Justify plugin as imported.
*
* @name Imported#Panda_TextJustify
* @type {Boolean}
*/
Imported.Panda_TextJustify = true;
/**
* Variable used to namespace Panda's plugins.
*
* @namespace Panda
* @type {Object}
*/
var Panda = window.Panda || {};
/**
* The namespace for Panda's Text Justify
*
* @namespace Panda.TextJustify
* @type {Object}
*/
Panda.TextJustify = Panda.TextJustify || {};
/**
* The version of Panda's Text Justify
*
* @name Panda.TextJustify#version
* @type {String}
*/
Panda.TextJustify.version = '1.0.0';
/**
* Short alias for Panda's Text Justify
*
* @ignore
*/
var $ = Panda.TextJustify;
/**
* Namespace for holding aliases for Panda's Text Justify.
*
* @namespace Panda.TextJustify.Aliases
* @type {Object}
*/
$.Aliases = $.Aliases || {};
/**
* Namespace for holding Window_Base's aliases.
*
* @namespace Panda.TextJustify.Aliases.Window_Base
* @type {Object}
*/
$.Aliases.Window_Base = $.Aliases.Window_Base || {};
/**
* Superclass of the windows in the game.
* @external Window_Base
*/
/**
* Alias for special text draw.
*
* @function drawTextEx;
* @memberof Panda.TextJustify.Aliases.Window_Base
*/
$.Aliases.Window_Base.drawTextEx = (
Window_Base.prototype.drawTextEx
);
/**
* Draws the text parsing special characters.
*
* @function drawTextEx
* @memberof external:Window_Base.prototype
*
* @param {String} text The text to be drawn.
* @param {Number} x The x coordinate of the text.
* @param {Number} y The y coordinate of the text.
* @param {Boolean} justified If the text is justified.
*
* @returns {void}
*/
Window_Base.prototype.drawTextEx = function(text, x, y, justified) {
if (text && justified) {
var textState = { index: 0, x: x, y: y, left: x };
textState.text = this.convertEscapeCharacters(text);
textState.height = this.calcTextHeight(textState, false);
this.resetFontSettings();
this.calcSpaceWidth(textState);
while (textState.index < textState.text.length) {
this.processCharacter(textState);
}
return textState.x - x;
} else {
return $.Aliases.Window_Base.drawTextEx.apply(this, arguments);
}
};
/**
* Alias for character processing.
*
* @function processCharacter;
* @memberof Panda.TextJustify.Aliases.Window_Base
*/
$.Aliases.Window_Base.processCharacter = (
Window_Base.prototype.processCharacter
);
/**
* Process characters being written on the window.
*
* @function processCharacter
* @memberof external:Window_Base.prototype
*
* @param {Object} textState The object containing the text data
* @param {Number} textState.x The x coordinate for the left of the text
* @param {Number} textState.y The y coordinate for the top of the text
* @param {Number} textState.left The x coordinate for the line start
* @param {Number} textState.height The line height
* @param {String} textState.text The text to be written
* @param {String} textState.text The text to be written
*
* @returns {void}
*/
Window_Base.prototype.processCharacter = function(textState) {
if (
textState.text[textState.index] === ' '
&& typeof textState.spaceWidth !== 'undefined'
&& typeof textState.lineIndex !== 'undefined'
) {
this.processSpace(textState);
return;
}
$.Aliases.Window_Base.processCharacter.apply(this, arguments);
};
/**
* Process the space character on text.
*
* @function processSpace
* @memberof external:Window_Base.prototype
*
* @param {Object} textState The object containing the text data
* @param {Number} textState.x The x coordinate for the left of the text
* @param {Number} textState.y The y coordinate for the top of the text
* @param {Number} textState.left The x coordinate for the line start
* @param {Number} textState.height The line height
* @param {String} textState.text The text to be written
*
* @returns {void}
*/
Window_Base.prototype.processSpace = function(textState) {
textState.index++;
textState.x += textState.spaceWidth[textState.lineIndex];
};
/**
* Alias for new line processing.
*
* @function processNewLine;
* @memberof Panda.TextJustify.Aliases.Window_Base
*/
$.Aliases.Window_Base.processNewLine = (
Window_Base.prototype.processNewLine
);
/**
* Process new lines in the text.
*
* @function processNewLine
* @memberof external:Window_Base.prototype
*
* @param {Object} textState The object containing the text data
* @param {Number} textState.x The x coordinate for the left of the text
* @param {Number} textState.y The y coordinate for the top of the text
* @param {Number} textState.left The x coordinate for the line start
* @param {Number} textState.height The line height
* @param {String} textState.text The text to be written
*
* @returns {void}
*/
Window_Base.prototype.processNewLine = function(textState) {
if (typeof textState.lineIndex !== 'undefined') {
textState.lineIndex++;
}
$.Aliases.Window_Base.processNewLine.apply(this, arguments);
};
/**
* Calculates the space width for justified text.
*
* @function calcSpaceWidth
* @memberof external:Window_Base.prototype
*
* @param {Object} textState The object containing the text data
* @param {Number} textState.x The x coordinate for the left of the text
* @param {Number} textState.y The y coordinate for the top of the text
* @param {Number} textState.left The x coordinate for the line start
* @param {Number} textState.height The line height
* @param {String} textState.text The text to be written
*
* @returns {void}
*/
Window_Base.prototype.calcSpaceWidth = function(textState) {
var defaultSpaceWidth = this.textWidth(' ');
var lineEndRegex = /(\.|:)\s*?$/i;
var spaceRegex = / /g;
var cw = this.contents.width - textState.left;
textState.lineIndex = 0;
textState.spaceWidth = [];
textState.text
.split('\n')
.map(function(line) {
return line.trim();
})
.forEach(function(line, index, arr) {
if (
this.textWidth(line) > cw
|| index === arr.length - 1
|| lineEndRegex.test(line)
) {
textState.spaceWidth[index] = defaultSpaceWidth;
return;
}
var _textState = {
x: textState.x,
y: textState.y,
index: 0,
left: textState.left,
height: textState.height,
text: line.replace(spaceRegex, '')
};
while (_textState.index < _textState.text.length) {
this.processCharacter(_textState);
}
var tw = _textState.x - _textState.left;
textState.spaceWidth[index] = (
(cw - tw) / (line.split(' ').length - 1)
);
}, this)
;
this.contents.clear();
};
/**
* Namespace for holding Window Message's aliases.
*
* @namespace Panda.TextJustify.Aliases.Window_Message
* @type {Object}
*/
$.Aliases.Window_Message = $.Aliases.Window_Message || {};
/**
* Window responsible for showing messages.
*
* @external Window_Message
*/
/**
* Alias for new message page.
*
* @function newPage
* @memberof Panda.TextJustify.Aliases.Window_Message
*/
$.Aliases.Window_Message.newPage = Window_Message.prototype.newPage;
/**
* Builds a new page of text.
*
* @function newPage
* @memberof external:Window_Message.prototype
*
* @param {Object} textState The object containing the text data
* @param {Number} textState.x The x coordinate for the left of the text
* @param {Number} textState.y The y coordinate for the top of the text
* @param {Number} textState.left The x coordinate for the line start
* @param {Number} textState.height The line height
* @param {String} textState.text The text to be written
*
* @returns {void}
*/
Window_Message.prototype.newPage = function(textState) {
$.Aliases.Window_Message.newPage.call(this, textState);
if (textState.text) {
this.calcSpaceWidth(textState);
}
};
/**
* Returns the namespaces to the global context.
*/
window.Imported = Imported;
window.Panda = Panda;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment