设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 92|回复: 0
打印 上一主题 下一主题

[搬运汉化] 【插件】对话文本默认居中

[复制链接]

Lv2.观梦者

梦石
0
星屑
360
在线时间
60 小时
注册时间
2025-2-14
帖子
13
跳转到指定楼层
1
发表于 2025-4-27 21:01:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 xbentwo 于 2025-4-27 21:03 编辑

Drillup系列里那个文本居中的插件需要每一行手动输入标签代码来实现,不是很友好,我就想要个默认居中显示文本的,然后我找到了这个可以调整文本显示位置的插件,进行了一些小小的改动,将默认的设置调整为了居中显示,依然可以通过标签\TA[0]和\TA[2]手动实现左对齐和右对齐。
可以兼容YEP的信息核心和Drillup系列的插件,但目前发现会有个小小的瑕疵,对于一些用到自适应窗口的插件功能会有影响,例如地图上显示一个漂浮窗文字,这个漂浮窗需要手动调整自适应进行大概10个单位的向右偏移,或者你在显示完的文本后面多打两个空格把显示框撑开,否则最后会有半个字的为止被遮挡。
  1. //=============================================================================
  2. // RS_MessageAlign.js
  3. //=============================================================================

  4. /*:
  5. * @help  
  6. // MIT 许可证
  7. // 版权所有 (c) 2017 biud436
  8. // --------------------------------------------------------------------------
  9. // 可免费用于商业和非商业用途。
  10. //====================================================================

  11. /*:
  12. * @plugindesc [v1.1.0]   此插件允许您对齐消息系统中的文本。
  13. * @author biud436
  14. *
  15. *
  16. * @help  
  17. * =============================================================================
  18. * MIT 许可证
  19. * 版权所有 (c) 2017 biud436
  20. * 可免费用于商业和非商业用途。
  21. * =============================================================================
  22. * --------------------------------------------------------------------------------------
  23. * 该插件是一个附加组件,因此应该将其安装在某个地方
  24. * 位于‘YEP_MessageCore’下方。
  25. *
  26. * 所有文本代码在绘制文本之前进行处理。
  27. * 您可以为每一行设置不同的文本对齐方式。
  28. *
  29. * \TA[0] - 左
  30. * \TA[1] - CENTER(使用 <CENTER> 标签更方便)
  31. * \TA[2] - 右
  32. *
  33. * 使用 Yanfly Message Core 时:
  34. * - 不要使用像“px”和“py”这样的文本代码。
  35. *——不要使用自动换行。
  36. * =================================================================================
  37. * 例子
  38. * --------------------------------------------------------------------------------------
  39. *
  40. *例 1:)
  41. * <CENTER>所有场景都有默认实现
  42. * 用于创建、启动和更新,可以
  43. * 在派生类中。然后是场景类 </CENTER>
  44. * 有很多成员函数。
  45. *
  46. *例 2:)
  47. * \TA[1] Game_Player 对象继承自
  48. * \TA[2] Game_Character 类并实现
  49. * <CENTER>其自身的功能。</CENTER>
  50. *
  51. *例 3:)
  52. * <CENTER>5 秒后隐藏!
  53. *
  54. * =================================================================================
  55. * 变更日志
  56. * --------------------------------------------------------------------------------------
  57. * 2017.01.25 (v1.0.0) - 首次发布
  58. * 2017.06.25 (v1.0.1) - 修复每行重置字体设置的问题
  59. * 2017.07.23 (v1.0.2) - 修复新行中对齐无法处理的问题
  60. * 2017.10.13 (v1.0.3) - 修复类名。
  61. * 2018.05.09 (v1.0.4) - 添加了一个存储最后对齐值的变量。
  62. * 2018.07.09 (v1.0.5) :
  63. * - 修复了左对齐、居中对齐、右对齐的文本填充。
  64. * - 现在即使没有 YEP Message Core 也可以正常工作。
  65. * 2018.08.14 (v1.0.6) - 修复了 LF(换行符)和 CR(回车符)
  66. * 2018.11.05 (v1.0.7) :
  67. * - 添加了文本代码,例如 <LEFT>、<CENTER>、<RIGHT>、</LEFT>、</CENTER>、</RIGHT>
  68. * 2018.12.22 (v1.0.8) :
  69. * - 现在可以在滚动文本窗口和项目窗口中使用文本对齐。
  70. * 2019.03.18 (v1.0.9) :
  71. * - 为 Galv 的消息样式兼容性添加了一些内容。
  72. * 2019.04.13 (v1.0.10):
  73. * - 修复了滚动文本不起作用的问题。
  74. * 2019.04.15 (v1.0.12) :
  75. * - 添加了使用名为 \fs[x] 的文本代码时重新计算文本高度的功能。
  76. * - 修复了每行重置字体的错误。
  77. * 2019.08.29 (v1.0.13) :
  78. * - 修复文本左对齐时从第二行开始添加空格的问题。
  79. * 2019.09.23 (v1.0.14) :
  80. * - 修复与 YEP_StatAllocation 和 YEP_StatusMenuCore 插件冲突的问题。
  81. * 2020.05.13 (v1.0.15):
  82. * - 删除未使用的值。
  83. * 2020.08.13 (v1.0.16):
  84. * - 修复了在原始模式下使用“\!”,“\。”,“\|”等文本代码时出现两次运行的问题。
  85. * 2023.07.13 (v1.0.28) :
  86. * - 转换为 ES6(对象字面量简写、扩展语法、箭头函数、Const/Let)
  87. * 2023.07.15 (v1.0.29) :
  88. * - 重构代码后修复了未定义名为“tx”的变量的问题。
  89. * 2023.11.08 (v1.0.30) :
  90. * - 修复了与 YEP_EventMiniLabel 插件的兼容性问题。
  91. * 2025.03.06 (v1.1.0) :
  92. * - 修复了使用 YEP_ExtMesPack1 插件中的名称框文本代码时的默认文本对齐问题。
  93. */

  94. //============================================================================
  95. // eslint-disable-next-line no-var
  96. var Imported = Imported || {};
  97. Imported.RS_MessageAlign = true;

  98. RS = window.RS || {};
  99. RS.MessageAlign = RS.MessageAlign || {};

  100. (function () {
  101.   'use strict';

  102.   //============================================================================
  103.   // Game_Message
  104.   //============================================================================

  105.   const alias_Game_Message_clear = Game_Message.prototype.clear;
  106.   Game_Message.prototype.clear = function () {
  107.     alias_Game_Message_clear.call(this);
  108.     this._align = [];
  109.     this._alignLast = undefined; // Changed from -1
  110.   };

  111.   Game_Message.prototype.setAlign = function (n) {
  112.     this._align = this._align || [];
  113.     this._alignLast = n;
  114.     this._align.push(n);
  115.   };

  116.   Game_Message.prototype.getAlign = function () {
  117.     if (this._align.length > 0) {
  118.       return this._align[0]; // Peek at next alignment without shifting
  119.     }
  120.   //  return this._alignLast; // Returns undefined when no alignment set
  121.       return 1; // 默认居中
  122.   };
  123.   

  124.   Game_Message.prototype.clearAlignLast = function () {
  125.     this._alignLast = -1;
  126.   };

  127.   //============================================================================
  128.   // Window_Base
  129.   //============================================================================

  130.   Window_Base.prototype.isUsedTextWidthEx = function () {
  131.     let ret = false;
  132.     if (Imported.YEP_MessageCore && this._checkWordWrapMode) {
  133.       ret = true;
  134.     }
  135.     if (!Imported.YEP_MessageCore) {
  136.       ret = this._isUsedTextWidth;
  137.     }
  138.     if (Imported.YEP_EventMiniLabel) {
  139.       ret = true;
  140.     }

  141.     return ret;
  142.   };

  143.   const alias_Window_Base_convertEscapeCharacters =
  144.     Window_Base.prototype.convertEscapeCharacters;
  145.   Window_Base.prototype.convertEscapeCharacters = function (text) {
  146.     text = alias_Window_Base_convertEscapeCharacters.call(this, text);
  147.     text = text.replace(/\\/g, '\x1b');
  148.     // eslint-disable-next-line no-control-regex
  149.     text = text.replace(/\x1b\x1b/g, '\\');
  150.     text = text.replace(/(?:<LEFT>)/gi, () => {
  151.       return '\x1bTA[0]';
  152.     });
  153.     text = text.replace(/(?:<CENTER>)/gi, () => {
  154.       return '\x1bTA[1]';
  155.     });
  156.     text = text.replace(/(?:<RIGHT>)/gi, () => {
  157.       return '\x1bTA[2]';
  158.     });
  159.     text = text.replace(
  160.       // eslint-disable-next-line no-control-regex
  161.       /\x1bTA\[(\d+)\]/gi,
  162.       (...args) => {
  163.         if (!this.isUsedTextWidthEx()) {
  164.           $gameMessage.setAlign(Number(args[1] || 0));
  165.         }
  166.         return '';
  167.       }
  168.     );
  169.     text = text.replace(/<\/LEFT>|<\/CENTER>|<\/RIGHT>/gi, () => {
  170.       return '\x1bAEND';
  171.     });
  172.     return text;
  173.   };

  174.   const alias_Window_Base_processEscapeCharacter =
  175.     Window_Base.prototype.processEscapeCharacter;
  176.   Window_Base.prototype.processEscapeCharacter = function (code, textState) {
  177.     switch (code) {
  178.       case 'AEND':
  179.         $gameMessage.clearAlignLast();
  180.         break;
  181.       default:
  182.         alias_Window_Base_processEscapeCharacter.call(this, code, textState);
  183.     }
  184.   };

  185.   Window_Base.prototype.processAlign = function (textState) {
  186.     textState = textState || this._textState;
  187.     const alignment = $gameMessage.getAlign();

  188.     // Only process valid alignments
  189.     if (typeof alignment !== 'number' || alignment < 0 || alignment > 2) {
  190.       return; // Preserve original alignment
  191.     }

  192.     switch (alignment) {
  193.       case 0:
  194.         this.setAlignLeft(textState);
  195.         break;
  196.       case 1:
  197.         this.setAlignCenter(textState);
  198.         break;
  199.       case 2:
  200.         this.setAlignRight(textState);
  201.         break;
  202.     }

  203.     // Remove processed alignment
  204.     if ($gameMessage._align.length > 0) {
  205.       $gameMessage._align.shift();
  206.     }
  207.   };

  208.   const alias_Window_Base_processNewLine = Window_Base.prototype.processNewLine;
  209.   Window_Base.prototype.processNewLine = function (textState) {
  210.     alias_Window_Base_processNewLine.call(this, textState);
  211.     this.processAlign(textState);
  212.   };

  213.   if (!Imported.YEP_MessageCore) {
  214.     Window_Base.prototype.saveFontSettings = function () {
  215.       this._messageDesc = {};
  216.       this._messageDesc.fontFace = this.contents.fontFace;
  217.       this._messageDesc.fontSize = this.contents.fontSize;
  218.       this._messageDesc.textColor = this.contents.textColor;
  219.     };

  220.     Window_Base.prototype.restoreFontSettings = function () {
  221.       if (!this._messageDesc) return;
  222.       this.contents.fontFace = this._messageDesc.fontFace;
  223.       this.contents.fontSize = this._messageDesc.fontSize;
  224.       this.contents.textColor = this._messageDesc.textColor;
  225.       this._messageDesc = undefined;
  226.     };
  227.   }

  228.   Window_Base.prototype.calcTextWidth = function (text) {
  229.     let tempText = text;
  230.     tempText = tempText.split(/[\r\n]+/);
  231.     let textWidth = 0;

  232.     // Galv's Message Styles Compatibility
  233.     if (Imported.Galv_MessageStyles) {
  234.       let ret = 0;

  235.       let faceoffset = Window_Base._faceWidth + 25;

  236.       if (Imported.Galv_MessageBusts) {
  237.         if ($gameMessage.bustPos === 1) {
  238.           faceoffset = 0;
  239.         } else {
  240.           faceoffset = Galv.MB.w;
  241.         }
  242.       }

  243.       // Calc X Offset
  244.       let xO = $gameMessage._faceName ? faceoffset : 0;
  245.       // eslint-disable-next-line no-unused-vars
  246.       xO += Galv.Mstyle.padding[1] + Galv.Mstyle.padding[3]; // Added padding

  247.       if (this.pTarget != null) {
  248.         this.resetFontSettings();
  249.         ret = this.testWidthEx(tempText[0]);
  250.         this.resetFontSettings();
  251.         textWidth = Math.max(textWidth, ret);
  252.         if (textWidth !== 0) return textWidth;
  253.       }
  254.     }

  255.     if (Imported.YEP_MessageCore) {
  256.       const setting = this._wordWrap;
  257.       this._wordWrap = false;
  258.       this.saveCurrentWindowSettings();
  259.       this._checkWordWrapMode = true;
  260.       textWidth = this.drawTextExForAlign(tempText[0], 0, this.contents.height);
  261.       this._checkWordWrapMode = false;
  262.       this.restoreCurrentWindowSettings();
  263.       this.clearCurrentWindowSettings();
  264.       this._wordWrap = setting;
  265.     } else {
  266.       this.saveFontSettings();
  267.       this._isUsedTextWidth = true;
  268.       textWidth = this.drawTextExForAlign(tempText[0], 0, this.contents.height);
  269.       this.restoreFontSettings();
  270.       this._isUsedTextWidth = false;
  271.     }

  272.     return textWidth;
  273.   };

  274.   if (Imported.YEP_MessageCore) {
  275.     Window_Base.prototype.calcTextHeight = function (textState, all) {
  276.       'use strict';

  277.       const lastFontSize = this.contents.fontSize;
  278.       let textHeight = 0;
  279.       const lines = textState.text.slice(textState.index).split('\n');
  280.       const maxLines = all ? lines.length : 1;

  281.       for (let i = 0; i < maxLines; i++) {
  282.         let maxFontSize = this.contents.fontSize;
  283.         // eslint-disable-next-line no-control-regex, no-useless-escape
  284.         const regExp = /\x1b[\{\}]|\x1bFS\[(\d+)\]/gi;
  285.         for (;;) {
  286.           const array = regExp.exec(lines[i]);
  287.           if (array) {
  288.             if (array[0] === '\x1b{') {
  289.               this.makeFontBigger();
  290.             }
  291.             if (array[0] === '\x1b}') {
  292.               this.makeFontSmaller();
  293.             }
  294.             if (array[0].contains('\x1bfs'.toLowerCase())) {
  295.               this.contents.fontSize = parseInt(array[1], 10);
  296.             }
  297.             if (maxFontSize < this.contents.fontSize) {
  298.               maxFontSize = this.contents.fontSize;
  299.             }
  300.           } else {
  301.             break;
  302.           }
  303.         }
  304.         textHeight += maxFontSize + 8;
  305.       }

  306.       this.contents.fontSize = lastFontSize;

  307.       return textHeight;
  308.     };
  309.   }

  310.   Window_Base.prototype.newLineX = function () {
  311.     return this.textPadding();
  312.   };

  313.   Window_Base.prototype.setAlignLeft = function (textState) {
  314.     textState.x = this.newLineX();
  315.     textState.left = textState.x;
  316.   };

  317.   Window_Base.prototype.setAlignCenter = function (textState) {
  318.     const padding = this.textPadding();
  319.     const tx = this.calcTextWidth(textState.text.slice(textState.index));
  320.     textState.x =
  321.       (this.newLineX() + this.contentsWidth() + padding) / 2 - tx / 2;
  322.     textState.left = textState.x;
  323.   };

  324.   Window_Base.prototype.setAlignRight = function (textState) {
  325.     const padding = this.textPadding();
  326.     const tx = this.calcTextWidth(textState.text.slice(textState.index));
  327.     textState.x = this.contentsWidth() - padding - tx;
  328.     textState.left = textState.x;
  329.   };

  330.   Window_Base.prototype.doFirstLineAlign = function (textState) {
  331.     const isValid = !this.isUsedTextWidthEx();
  332.     if (isValid) {
  333.       this.processAlign(textState);
  334.     }
  335.   };

  336.   Window_Base.prototype.drawTextExForAlign = function (text, x, y) {
  337.     if (text) {
  338.       const textState = { index: 0, x, y, left: x };
  339.       textState.text = this.convertEscapeCharacters(text);
  340.       textState.height = this.calcTextHeight(textState, false);
  341.       while (textState.index < textState.text.length) {
  342.         this.processCharacter(textState);
  343.       }
  344.       return textState.x - x;
  345.     }

  346.     return 0;
  347.   };

  348.   const alias_origin_Window_Base_drawTextEx = Window_Base.prototype.drawTextEx;
  349.   Window_Base.prototype.drawTextEx = function (text, x, y) {
  350.     if (text) {
  351.       this.resetFontSettings();
  352.       const textState = { index: 0, x, y, left: x };
  353.       textState.text = this.convertEscapeCharacters(text);
  354.       textState.height = this.calcTextHeight(textState, false);
  355.       this.doFirstLineAlign(textState);
  356.       while (textState.index < textState.text.length) {
  357.         this.processCharacter(textState);
  358.       }
  359.       return textState.x - x;
  360.     }

  361.     return 0;
  362.   };

  363.   //============================================================================
  364.   // Window_Message
  365.   //============================================================================

  366.   // Galv's Message Styles Compatibility
  367.   if (Imported.Galv_MessageStyles) {
  368.     Window_Message.prototype.textPadding = function () {
  369.       let faceoffset = Window_Base._faceWidth + 25;

  370.       if (Imported.Galv_MessageBusts) {
  371.         if ($gameMessage.bustPos === 1) {
  372.           faceoffset = 0;
  373.         } else {
  374.           faceoffset = Galv.MB.w;
  375.         }
  376.       }

  377.       // Calc X Offset
  378.       let xO = $gameMessage._faceName ? faceoffset : 0;
  379.       xO += Galv.Mstyle.padding[1] + Galv.Mstyle.padding[3]; // Added padding

  380.       return xO;
  381.     };
  382.   }

  383.   Window_Message.prototype.processAlign = function (textState) {
  384.     textState = textState || this._textState;
  385.     switch ($gameMessage.getAlign()) {
  386.       case 1:
  387.         this.setAlignCenter(textState);
  388.         break;
  389.       case 2:
  390.         this.setAlignRight(textState);
  391.         break;
  392.       default:
  393.         this.setAlignLeft(textState);
  394.         break;
  395.     }
  396.   };

  397.   if (!Imported.YEP_MessageCore) {
  398.     const alias_Window_Message_startPause = Window_Message.prototype.startPause;
  399.     Window_Message.prototype.startPause = function () {
  400.       if (this.isUsedTextWidthEx()) return;
  401.       alias_Window_Message_startPause.call(this);
  402.     };

  403.     const alias_Window_Message_startWait = Window_Message.prototype.startWait;
  404.     Window_Message.prototype.startWait = function (count) {
  405.       if (this.isUsedTextWidthEx()) return;
  406.       alias_Window_Message_startWait.call(this, count);
  407.     };
  408.   }

  409.   const alias_Window_Message_startMessage_setAlignCenter =
  410.     Window_Message.prototype.startMessage;
  411.   Window_Message.prototype.startMessage = function () {
  412.     alias_Window_Message_startMessage_setAlignCenter.call(this);
  413.     this.processAlign();
  414.   };

  415.   //============================================================================
  416.   // Window_ChoiceList
  417.   //============================================================================
  418.   Window_ChoiceList.prototype.drawTextEx = function (text, x, y) {
  419.     return alias_origin_Window_Base_drawTextEx.call(this, text, x, y);
  420.   };

  421.   //============================================================================
  422.   // Window_ScrollText
  423.   //============================================================================

  424.   Window_ScrollText.prototype.refresh = function () {
  425.     const textState = { index: 0 };
  426.     textState.text = this.convertEscapeCharacters(this._text);
  427.     this.resetFontSettings();
  428.     this._allTextHeight = this.calcTextHeight(textState, true);
  429.     this.createContents();
  430.     this.origin.y = -this.height;
  431.     this.processAlign(textState);
  432.     this.drawTextEx(this._text, this.textPadding(), 1);
  433.   };
  434. })();
复制代码
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2025-5-14 03:32

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表