RPG Maker MZ
Text Addon v1.1.1

This plugin adds several new text codes for you to use in your game.

Download (15.97 kB, 123 times downloaded)

Placement

Make sure to place this plugin below the plugins that this plugin requires, but above plugins that rely on this plugin.

Usage

Text codes

\align[alignment]
\align[alignment, processNewLine]

This code allows you to align your text.

Arguments:

alignment The alignment of the text. Must be either left, start, center, end or right. On text that's rendered from left to right, start will align to the left and end will align to the right, on text that's rendered from right to left, this is reversed. Both left and right will force the alignment regardless of reading direction.
processNewLine optional When set to true, will add a new line prior to rendering the rest of the text.

Plugin commands

Set text alignment

This allows you to set the text alignment for the duration of the current event. This will override the plugin parameter settings.

Arguments:

Alignment The alignment.

This plugin overwrites default functionality. Make sure you check whether or not the plugin is compatible with other plugins by checking which functions they overwrite. Below is the list of methods it overwrites:

  • Game_Interpreter.prototype.terminate
  • Window_Base.prototype.processNewLine
  • Window_Base.prototype.createTextState
  • Window_Message.prototype.startMessage

Download Text Addon v1.1 (15.39 kB, 102 times downloaded)

Download Text Addon v1.0 (9.08 kB, 128 times downloaded)

/******************************************************************************
 * CXJ_MZ_TextAddon.js                                                        *
 ******************************************************************************
 * By G.A.M. Kertopermono, a.k.a. GaryCXJk                                    *
 ******************************************************************************
 * License: MIT                                                               *
 ******************************************************************************
 * Copyright (c) 2020-2022, G.A.M. Kertopermono                               *
 *                                                                            *
 * 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.                                                  *
 ******************************************************************************/

/*:
 * @target MZ
 * @plugindesc Adds new text codes.
 * @author G.A.M. Kertopermono
 *
 * @help
 * ============================================================================
 * = About                                                                    =
 * ============================================================================
 *
 * This plugin adds several new text codes for you to use in your game.
 *
 * ============================================================================
 * = Requirements                                                             =
 * ============================================================================
 *
 * This plugin requires the following plugins to work:
 *
 * * CXJ_MZ.CoreEssentials: ^1.4.2.1
 * * CXJ_MZ.TextHelper: ^1.1
 *
 * ============================================================================
 * = Placement                                                                =
 * ============================================================================
 *
 * Make sure to place this plugin below the plugins that this plugin requires,
 * but above plugins that rely on this plugin.
 *
 * ============================================================================
 * = Usage                                                                    =
 * ============================================================================
 *
 * ----------
 * Text codes
 * ----------
 *
 * \align[alignment]
 * \align[alignment, processNewLine]
 *
 * This code allows you to align your text.
 *
 * Arguments:
 *
 * alignment      - The alignment of the text. Must be either left, start,
 *                  center, end or right. On text that's rendered from left
 *                  to right, start will align to the left and end will align
 *                  to the right, on text that's rendered from right to left,
 *                  this is reversed. Both left and right will force the
 *                  alignment regardless of reading direction.
 * processNewLine - (optional) When set to true, will add a new line prior to
 *                  rendering the rest of the text.
 *
 * ---------------
 * Plugin commands
 * ---------------
 *
 * Set text alignment
 * ------------------
 *
 * This allows you to set the text alignment for the duration of the current
 * event. This will override the plugin parameter settings.
 *
 * Arguments:
 *
 * Alignment - The alignment.
 *
 * ============================================================================
 * = Changelog                                                                =
 * ============================================================================
 *
 * 1.1.1 (2022-07-10)
 * ------------------
 *
 * * Added left and right alignment.
 *
 * 1.1 (2022-07-05)
 * ----------------
 *
 * * Added new plugin command to set the alignment.
 * * Fixed: When having a portrait, the text won't align properly.
 *
 * 1.0 (2022-07-04)
 * ----------------
 *
 * * Initial release
 *
 * ============================================================================
 * = Compatibility                                                            =
 * ============================================================================
 *
 * This plugin overwrites default functionality. Make sure you check whether or
 * not the plugin is compatible with other plugins by checking which functions
 * they overwrite. Below is the list of methods it overwrites:
 *
 * * Game_Interpreter.prototype.terminate
 * * Window_Base.prototype.processNewLine
 * * Window_Base.prototype.createTextState
 * * Window_Message.prototype.startMessage
 *
 * ============================================================================
 * = License                                                                  =
 * ============================================================================
 *
 * Copyright (c) 2020, G.A.M. Kertopermono
 *
 * 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.
 *
 * ============================================================================
 *
 * @param defaultAlignment
 * @text Default alignment
 * @desc Sets the default alignment for message windows.
 * @type select
 * @default start
 * @option Start
 * @value start
 * @option Center
 * @value center
 * @option End
 * @value end
 *
 * @command setAlignment
 * @text Set text alignment
 * @desc Sets the text alignment.
 *
 * @arg alignment
 * @type select
 * @default null
 * @option Default
 * @value null
 * @option Left
 * @value left
 * @option Start
 * @value start
 * @option Center
 * @value center
 * @option End
 * @value end
 * @option Right
 * @value right
 * @text Alignment
 * @desc The alignment.
 */

(() => {
  window.CXJ_MZ = window.CXJ_MZ || {};
  const {
    CXJ_MZ
  } = window;
  CXJ_MZ.TextAddon = CXJ_MZ.TextAddon || {};
  CXJ_MZ.TextAddon.version = '1.0.0';

  if (!CXJ_MZ.CoreEssentials) {
    throw new Error('CoreEssentials has not been initialized. Make sure you load CoreEssentials before this plugin.');
  }

  if (!CXJ_MZ.CoreEssentials.isVersion('CXJ_MZ.CoreEssentials', '1.4.2.1')) {
    throw new Error('The correct version of CoreEssentials has not been loaded (required version: 1.4.2.1).');
  }

  CXJ_MZ.CoreEssentials.checkDependencies({
    'CXJ_MZ.TextHelper': '1.1',
  }, true);

  const {
    CoreEssentials,
    TextAddon,
    TextHelper,
  } = CXJ_MZ;

  const pluginName = 'CXJ_MZ_TextAddon';

  /* ------------------------------------------------------------------------
   * - Private variables                                                    -
   * ------------------------------------------------------------------------
   */

  const pluginSettings = {
    alignment: null,
  };

  /* ------------------------------------------------------------------------
   * - Plugin commands                                                      -
   * ------------------------------------------------------------------------
   */

  PluginManager.registerCommand(pluginName, "setAlignment", function(args) {
    TextAddon.setAlignment(args.alignment === 'null' ? null : args.alignment);
  });

  /* ------------------------------------------------------------------------
   * - Default parameters                                                   -
   * ------------------------------------------------------------------------
   */

  const parameters = CoreEssentials.getParameters(pluginName, {
    defaultAlignment: 'start',
  }, {
    defaultAlignment: 'text',
  });

  /* --------------------------------------------------------------------------
   * - Plugin methods                                                         -
   * --------------------------------------------------------------------------
   */

  TextAddon.setAlignment = function(alignment) {
    pluginSettings.alignment = alignment;
  }

  TextAddon.getAlignment = function() {
    return pluginSettings.alignment ?? parameters.defaultAlignment ?? 'start';
  }

  TextAddon.resetAlignment = function() {
    pluginSettings.alignment = null;
  }

  /* --------------------------------------------------------------------------
   * - Message codes                                                          -
   * --------------------------------------------------------------------------
   */

  TextHelper.addMessageCode('align\\[(start|end|left|right|center)(?:, ?(true|false))?\\]', function(textState, alignment, processNewLine = 'false') {
    textState.alignment = alignment;
    if (processNewLine === 'false') {
      textState.y -= textState.height;
    }
    this.processNewLine(textState);
  }, 'process', false);
  TextHelper.addMessageCode('align\\[(start|end|left|right|center)(?:, ?(true|false))?\\]',
    (textState, alignment, processNewLine = 'false') => [false, processNewLine !== 'false'],
    'length',
    false,
  );

  /* --------------------------------------------------------------------------
   * - Overrides and additions                                                -
   * --------------------------------------------------------------------------
   */
  (() => {
    /* --------------------------------------------------------------------
     * - Game_Interpreter.prototype.terminate (Override)                  -
     * --------------------------------------------------------------------
     */

    CoreEssentials.registerFunctionExtension('Game_Interpreter.prototype.terminate', function() {
      TextAddon.resetAlignment();
    });

    /* --------------------------------------------------------------------
     * - Window_Message.prototype.alignText (New)                         -
     * --------------------------------------------------------------------
     */

    /**
     *
     * @param {object} textState
     */
    Window_Base.prototype.alignText = function(textState) {
      if (textState.alignment !== 'start') {
        const width = this.getTextLineLength(textState);
        const innerWidth = this.getAlignWidth();

        switch (textState.alignment) {
          case 'end':
            textState.x = textState.startX + ((textState.rtl ? -1 : 1) * (innerWidth - width));
            break;
          case 'left':
            if (textState.rtl) {
              textState.x = textState.startX - innerWidth + width;
            }
            break;
          case 'right':
            if (!textState.rtl) {
              textState.x = textState.startX + innerWidth - width;
            }
            break;
          case 'center':
            textState.x = Math[textState.rtl ? 'ceil' : 'floor'](textState.startX + (((textState.rtl ? -1 : 1) * (innerWidth - width)) / 2));
          default:
            break;
        }
      }
    }

    /* --------------------------------------------------------------------
     * - Window_Message.prototype.getAlignWidth (New)                     -
     * --------------------------------------------------------------------
     */

    /**
     *
     * @returns {number}
     */
    Window_Base.prototype.getAlignWidth = function() {
      return this.contentsWidth() - this.itemPadding();
    }

    /* --------------------------------------------------------------------
     * - Window_Base.prototype.processNewLine (Override)                  -
     * --------------------------------------------------------------------
     */

    /**
     * @method processNewLine
     * @param {object} textState
     */
    const wbProcessNewLine = CoreEssentials.setNoConflict('Window_Base.prototype.processNewLine');
    Window_Base.prototype.processNewLine = function(textState) {
      wbProcessNewLine.call(this, textState);
      this.alignText(textState);
    }

    /* --------------------------------------------------------------------
     * - Window_Base.prototype.createTextState (Override)                 -
     * --------------------------------------------------------------------
     */

    /**
     * @method createTextState
     * @param {string} text
     * @param {number} x
     * @param {number} y
     * @param {number} width
     */
    const wbCreateTextState = CoreEssentials.setNoConflict('Window_Base.prototype.createTextState');
    Window_Base.prototype.createTextState = function(text, x, y, width) {
      const textState = wbCreateTextState.call(this, text, x, y, width);
      textState.alignment = this.getTextAlignment();
      this.alignText(textState);
      return textState;
    }

    /* --------------------------------------------------------------------
     * - Window_Base.prototype.getTextAlignment (New)                     -
     * --------------------------------------------------------------------
     */

    /**
     *
     * @returns {string}
     */
    Window_Base.prototype.getTextAlignment = function() {
      return 'start';
    }

    /* --------------------------------------------------------------------
     * - Window_Message.prototype.startMessage (Override)                 -
     * --------------------------------------------------------------------
     */

    CoreEssentials.registerFunctionExtension('Window_Message.prototype.startMessage', function() {
      this.alignText(this._textState);
    });

    /* --------------------------------------------------------------------
     * - Window_Message.prototype.getAlignWidth (New)                     -
     * --------------------------------------------------------------------
     */

    /**
     *
     * @returns {number}
     */
    Window_Message.prototype.getAlignWidth = function() {
      const faceExists = $gameMessage.faceName() !== "";
      const faceWidth = ImageManager.faceWidth;
      const spacing = 20;
      const margin = faceExists ? faceWidth + spacing : 4;
      return this.contentsWidth() - margin;
    }

    /* --------------------------------------------------------------------
     * - Window_Message.prototype.getTextAlignment (New)                  -
     * --------------------------------------------------------------------
     */

    /**
     *
     * @returns {string}
     */
    Window_Message.prototype.getTextAlignment = function() {
      return TextAddon.getAlignment();
    }
  })();
})();
                                

Creator: GaryCXJk

Release date: 2022-07-04

Last updated: 2022-07-10

Downloads: 0

License: The MIT License

Requirements: