じ☆ve冰风 发表于 前天 21:43

【插件】对话文本默认居中

Drillup系列里那个文本居中的插件需要每一行手动输入标签代码来实现,不是很友好,我就想要个默认居中显示文本的,然后我找到了这个可以调整文本显示位置的插件,进行了一些小小的改动,将默认的设置调整为了居中显示,依然可以通过标签\TA和\TA手动实现左对齐和右对齐。
可以兼容YEP的信息核心和Drillup系列的插件,但目前发现会有个小小的瑕疵,对于一些用到自适应窗口的插件功能会有影响,例如地图上显示一个漂浮窗文字,这个漂浮窗需要手动调整自适应进行大概10个单位的向右偏移,或者你在显示完的文本后面多打两个空格把显示框撑开,否则最后会有半个字的为止被遮挡。
//=============================================================================// RS_MessageAlign.js//=============================================================================/*: * @help// MIT 许可证// 版权所有 (c) 2017 biud436// --------------------------------------------------------------------------// 可免费用于商业和非商业用途。//====================================================================/*: * @plugindesc    此插件允许您对齐消息系统中的文本。 * @author biud436 *** @help   * ============================================================================= * MIT 许可证 * 版权所有 (c) 2017 biud436 * 可免费用于商业和非商业用途。 * ============================================================================= * -------------------------------------------------------------------------------------- * 该插件是一个附加组件,因此应该将其安装在某个地方 * 位于‘YEP_MessageCore’下方。 * * 所有文本代码在绘制文本之前进行处理。 * 您可以为每一行设置不同的文本对齐方式。 * * \TA - 左 * \TA - CENTER(使用标签更方便) * \TA - 右 * * 使用 Yanfly Message Core 时: * - 不要使用像“px”和“py”这样的文本代码。 *——不要使用自动换行。 * ================================================================================= * 例子 * -------------------------------------------------------------------------------------- * *例 1:) * 所有场景都有默认实现 * 用于创建、启动和更新,可以 * 在派生类中。然后是场景类* 有很多成员函数。 * *例 2:) * \TA Game_Player 对象继承自 * \TA Game_Character 类并实现 * 其自身的功能。 * *例 3:) * 5 秒后隐藏! * * ================================================================================= * 变更日志 * -------------------------------------------------------------------------------------- * 2017.01.25 (v1.0.0) - 首次发布 * 2017.06.25 (v1.0.1) - 修复每行重置字体设置的问题 * 2017.07.23 (v1.0.2) - 修复新行中对齐无法处理的问题 * 2017.10.13 (v1.0.3) - 修复类名。 * 2018.05.09 (v1.0.4) - 添加了一个存储最后对齐值的变量。 * 2018.07.09 (v1.0.5) : * - 修复了左对齐、居中对齐、右对齐的文本填充。 * - 现在即使没有 YEP Message Core 也可以正常工作。 * 2018.08.14 (v1.0.6) - 修复了 LF(换行符)和 CR(回车符) * 2018.11.05 (v1.0.7) : * - 添加了文本代码,例如 、、、、、 * 2018.12.22 (v1.0.8) : * - 现在可以在滚动文本窗口和项目窗口中使用文本对齐。 * 2019.03.18 (v1.0.9) : * - 为 Galv 的消息样式兼容性添加了一些内容。 * 2019.04.13 (v1.0.10): * - 修复了滚动文本不起作用的问题。 * 2019.04.15 (v1.0.12) : * - 添加了使用名为 \fs 的文本代码时重新计算文本高度的功能。 * - 修复了每行重置字体的错误。 * 2019.08.29 (v1.0.13) : * - 修复文本左对齐时从第二行开始添加空格的问题。 * 2019.09.23 (v1.0.14) : * - 修复与 YEP_StatAllocation 和 YEP_StatusMenuCore 插件冲突的问题。 * 2020.05.13 (v1.0.15): * - 删除未使用的值。 * 2020.08.13 (v1.0.16): * - 修复了在原始模式下使用“\!”,“\。”,“\|”等文本代码时出现两次运行的问题。 * 2023.07.13 (v1.0.28) : * - 转换为 ES6(对象字面量简写、扩展语法、箭头函数、Const/Let) * 2023.07.15 (v1.0.29) : * - 重构代码后修复了未定义名为“tx”的变量的问题。 * 2023.11.08 (v1.0.30) : * - 修复了与 YEP_EventMiniLabel 插件的兼容性问题。 * 2025.03.06 (v1.1.0) : * - 修复了使用 YEP_ExtMesPack1 插件中的名称框文本代码时的默认文本对齐问题。 */ //============================================================================// eslint-disable-next-line no-var var Imported = Imported || {}; Imported.RS_MessageAlign = true; RS = window.RS || {}; RS.MessageAlign = RS.MessageAlign || {}; (function () {'use strict';//============================================================================// Game_Message//============================================================================const alias_Game_Message_clear = Game_Message.prototype.clear;Game_Message.prototype.clear = function () {    alias_Game_Message_clear.call(this);    this._align = [];    this._alignLast = undefined; // Changed from -1};Game_Message.prototype.setAlign = function (n) {    this._align = this._align || [];    this._alignLast = n;    this._align.push(n);};Game_Message.prototype.getAlign = function () {    if (this._align.length > 0) {      return this._align; // Peek at next alignment without shifting    }//return this._alignLast; // Returns undefined when no alignment set      return 1; // 默认居中};    Game_Message.prototype.clearAlignLast = function () {    this._alignLast = -1;};//============================================================================// Window_Base//============================================================================Window_Base.prototype.isUsedTextWidthEx = function () {    let ret = false;    if (Imported.YEP_MessageCore && this._checkWordWrapMode) {      ret = true;    }    if (!Imported.YEP_MessageCore) {      ret = this._isUsedTextWidth;    }    if (Imported.YEP_EventMiniLabel) {      ret = true;    }    return ret;};const alias_Window_Base_convertEscapeCharacters =    Window_Base.prototype.convertEscapeCharacters;Window_Base.prototype.convertEscapeCharacters = function (text) {    text = alias_Window_Base_convertEscapeCharacters.call(this, text);    text = text.replace(/\\/g, '\x1b');    // eslint-disable-next-line no-control-regex    text = text.replace(/\x1b\x1b/g, '\\');    text = text.replace(/(?:)/gi, () => {      return '\x1bTA';    });    text = text.replace(/(?:)/gi, () => {      return '\x1bTA';    });    text = text.replace(/(?:)/gi, () => {      return '\x1bTA';    });    text = text.replace(      // eslint-disable-next-line no-control-regex      /\x1bTA\[(\d+)\]/gi,      (...args) => {      if (!this.isUsedTextWidthEx()) {          $gameMessage.setAlign(Number(args || 0));      }      return '';      }    );    text = text.replace(/||/gi, () => {      return '\x1bAEND';    });    return text;};const alias_Window_Base_processEscapeCharacter =    Window_Base.prototype.processEscapeCharacter;Window_Base.prototype.processEscapeCharacter = function (code, textState) {    switch (code) {      case 'AEND':      $gameMessage.clearAlignLast();      break;      default:      alias_Window_Base_processEscapeCharacter.call(this, code, textState);    }};Window_Base.prototype.processAlign = function (textState) {    textState = textState || this._textState;    const alignment = $gameMessage.getAlign();    // Only process valid alignments    if (typeof alignment !== 'number' || alignment < 0 || alignment > 2) {      return; // Preserve original alignment    }    switch (alignment) {      case 0:      this.setAlignLeft(textState);      break;      case 1:      this.setAlignCenter(textState);      break;      case 2:      this.setAlignRight(textState);      break;    }    // Remove processed alignment    if ($gameMessage._align.length > 0) {      $gameMessage._align.shift();    }};const alias_Window_Base_processNewLine = Window_Base.prototype.processNewLine;Window_Base.prototype.processNewLine = function (textState) {    alias_Window_Base_processNewLine.call(this, textState);    this.processAlign(textState);};if (!Imported.YEP_MessageCore) {    Window_Base.prototype.saveFontSettings = function () {      this._messageDesc = {};      this._messageDesc.fontFace = this.contents.fontFace;      this._messageDesc.fontSize = this.contents.fontSize;      this._messageDesc.textColor = this.contents.textColor;    };    Window_Base.prototype.restoreFontSettings = function () {      if (!this._messageDesc) return;      this.contents.fontFace = this._messageDesc.fontFace;      this.contents.fontSize = this._messageDesc.fontSize;      this.contents.textColor = this._messageDesc.textColor;      this._messageDesc = undefined;    };}Window_Base.prototype.calcTextWidth = function (text) {    let tempText = text;    tempText = tempText.split(/[\r\n]+/);    let textWidth = 0;    // Galv's Message Styles Compatibility    if (Imported.Galv_MessageStyles) {      let ret = 0;      let faceoffset = Window_Base._faceWidth + 25;      if (Imported.Galv_MessageBusts) {      if ($gameMessage.bustPos === 1) {          faceoffset = 0;      } else {          faceoffset = Galv.MB.w;      }      }      // Calc X Offset      let xO = $gameMessage._faceName ? faceoffset : 0;      // eslint-disable-next-line no-unused-vars      xO += Galv.Mstyle.padding + Galv.Mstyle.padding; // Added padding      if (this.pTarget != null) {      this.resetFontSettings();      ret = this.testWidthEx(tempText);      this.resetFontSettings();      textWidth = Math.max(textWidth, ret);      if (textWidth !== 0) return textWidth;      }    }    if (Imported.YEP_MessageCore) {      const setting = this._wordWrap;      this._wordWrap = false;      this.saveCurrentWindowSettings();      this._checkWordWrapMode = true;      textWidth = this.drawTextExForAlign(tempText, 0, this.contents.height);      this._checkWordWrapMode = false;      this.restoreCurrentWindowSettings();      this.clearCurrentWindowSettings();      this._wordWrap = setting;    } else {      this.saveFontSettings();      this._isUsedTextWidth = true;      textWidth = this.drawTextExForAlign(tempText, 0, this.contents.height);      this.restoreFontSettings();      this._isUsedTextWidth = false;    }    return textWidth;};if (Imported.YEP_MessageCore) {    Window_Base.prototype.calcTextHeight = function (textState, all) {      'use strict';      const lastFontSize = this.contents.fontSize;      let textHeight = 0;      const lines = textState.text.slice(textState.index).split('\n');      const maxLines = all ? lines.length : 1;      for (let i = 0; i < maxLines; i++) {      let maxFontSize = this.contents.fontSize;      // eslint-disable-next-line no-control-regex, no-useless-escape      const regExp = /\x1b[\{\}]|\x1bFS\[(\d+)\]/gi;      for (;;) {          const array = regExp.exec(lines);          if (array) {            if (array === '\x1b{') {            this.makeFontBigger();            }            if (array === '\x1b}') {            this.makeFontSmaller();            }            if (array.contains('\x1bfs'.toLowerCase())) {            this.contents.fontSize = parseInt(array, 10);            }            if (maxFontSize < this.contents.fontSize) {            maxFontSize = this.contents.fontSize;            }          } else {            break;          }      }      textHeight += maxFontSize + 8;      }      this.contents.fontSize = lastFontSize;      return textHeight;    };}Window_Base.prototype.newLineX = function () {    return this.textPadding();};Window_Base.prototype.setAlignLeft = function (textState) {    textState.x = this.newLineX();    textState.left = textState.x;};Window_Base.prototype.setAlignCenter = function (textState) {    const padding = this.textPadding();    const tx = this.calcTextWidth(textState.text.slice(textState.index));    textState.x =      (this.newLineX() + this.contentsWidth() + padding) / 2 - tx / 2;    textState.left = textState.x;};Window_Base.prototype.setAlignRight = function (textState) {    const padding = this.textPadding();    const tx = this.calcTextWidth(textState.text.slice(textState.index));    textState.x = this.contentsWidth() - padding - tx;    textState.left = textState.x;};Window_Base.prototype.doFirstLineAlign = function (textState) {    const isValid = !this.isUsedTextWidthEx();    if (isValid) {      this.processAlign(textState);    }};Window_Base.prototype.drawTextExForAlign = function (text, x, y) {    if (text) {      const textState = { index: 0, x, y, left: x };      textState.text = this.convertEscapeCharacters(text);      textState.height = this.calcTextHeight(textState, false);      while (textState.index < textState.text.length) {      this.processCharacter(textState);      }      return textState.x - x;    }    return 0;};const alias_origin_Window_Base_drawTextEx = Window_Base.prototype.drawTextEx;Window_Base.prototype.drawTextEx = function (text, x, y) {    if (text) {      this.resetFontSettings();      const textState = { index: 0, x, y, left: x };      textState.text = this.convertEscapeCharacters(text);      textState.height = this.calcTextHeight(textState, false);      this.doFirstLineAlign(textState);      while (textState.index < textState.text.length) {      this.processCharacter(textState);      }      return textState.x - x;    }    return 0;};//============================================================================// Window_Message//============================================================================// Galv's Message Styles Compatibilityif (Imported.Galv_MessageStyles) {    Window_Message.prototype.textPadding = function () {      let faceoffset = Window_Base._faceWidth + 25;      if (Imported.Galv_MessageBusts) {      if ($gameMessage.bustPos === 1) {          faceoffset = 0;      } else {          faceoffset = Galv.MB.w;      }      }      // Calc X Offset      let xO = $gameMessage._faceName ? faceoffset : 0;      xO += Galv.Mstyle.padding + Galv.Mstyle.padding; // Added padding      return xO;    };}Window_Message.prototype.processAlign = function (textState) {    textState = textState || this._textState;    switch ($gameMessage.getAlign()) {      case 1:      this.setAlignCenter(textState);      break;      case 2:      this.setAlignRight(textState);      break;      default:      this.setAlignLeft(textState);      break;    }};if (!Imported.YEP_MessageCore) {    const alias_Window_Message_startPause = Window_Message.prototype.startPause;    Window_Message.prototype.startPause = function () {      if (this.isUsedTextWidthEx()) return;      alias_Window_Message_startPause.call(this);    };    const alias_Window_Message_startWait = Window_Message.prototype.startWait;    Window_Message.prototype.startWait = function (count) {      if (this.isUsedTextWidthEx()) return;      alias_Window_Message_startWait.call(this, count);    };}const alias_Window_Message_startMessage_setAlignCenter =    Window_Message.prototype.startMessage;Window_Message.prototype.startMessage = function () {    alias_Window_Message_startMessage_setAlignCenter.call(this);    this.processAlign();};//============================================================================// Window_ChoiceList//============================================================================Window_ChoiceList.prototype.drawTextEx = function (text, x, y) {    return alias_origin_Window_Base_drawTextEx.call(this, text, x, y);};//============================================================================// Window_ScrollText//============================================================================Window_ScrollText.prototype.refresh = function () {    const textState = { index: 0 };    textState.text = this.convertEscapeCharacters(this._text);    this.resetFontSettings();    this._allTextHeight = this.calcTextHeight(textState, true);    this.createContents();    this.origin.y = -this.height;    this.processAlign(textState);    this.drawTextEx(this._text, this.textPadding(), 1);};})();复制代码
            本帖来自P1论坛作者xbentwo,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=497307若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
页: [1]
查看完整版本: 【插件】对话文本默认居中