查看: 108|回复: 0

[转载发布] 【求移植】MV的显示文字插件

[复制链接]
  • TA的每日心情
    开心
    2024-5-10 09:55
  • 签到天数: 37 天

    连续签到: 3 天

    [LV.5]常住居民I

    2028

    主题

    32

    回帖

    7260

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    0
    卡币
    5184
    OK点
    16
    积分
    7260
    发表于 同元九百九十六年三月二日(春) | 显示全部楼层 |阅读模式
    JS 代码下载
    1. //=============================================================================
    2. // DTextPicture.js
    3. // ----------------------------------------------------------------------------
    4. // Copyright (c) 2015 Triacontane
    5. // This software is released under the MIT License.
    6. // [url]http://opensource.org/licenses/mit-license.php[/url]
    7. // ----------------------------------------------------------------------------
    8. // Version
    9. // 1.2.2 2016/03/28 データベース情報を簡単に出力する制御文字を追加
    10. // 1.2.1 2016/01/29 コマンド「D_TEXT_SETTING」の実装が「D_TEST_SETTING」になっていたので修正(笑)
    11. // 1.2.0 2016/01/27 複数行表示に対応
    12. //                  文字列の揃えと背景色を設定する機能を追加
    13. //                  変数をゼロ埋めして表示する機能を追加
    14. // 1.1.3 2015/12/10 戦闘画面でもピクチャを使用できるよう修正
    15. //                  描画後にデバッグ画面等を開いて変数を修正した場合、再描画で変更が反映されてしまう問題を修正
    16. // 1.1.2 2015/11/07 描画文字列に半角スペースが含まれていた場合も問題なく実行できるよう修正
    17. // 1.1.0 2015/11/07 制御文字\C[n] \I[n] \{ \} に対応(\$と表示スピード制御系以外全部)
    18. // 1.0.1 2015/11/07 RPGツクールMV(日本語版)に合わせてコメントの表記を変更
    19. // 1.0.0 2015/11/06 初版
    20. // ----------------------------------------------------------------------------
    21. // [Blog]   : [url]http://triacontane.blogspot.jp/[/url]
    22. // [Twitter]: [url]https://twitter.com/triacontane/[/url]
    23. // [GitHub] : [url]https://github.com/triacontane/[/url]
    24. //=============================================================================
    25. /*:
    26. * @plugindesc 動的文字列ピクチャ生成プラグイン
    27. * @author トリアコンタン
    28. *
    29. * @help 指定した文字列でピクチャを動的に生成するコマンドを提供します。
    30. * 以下の手順で表示します。
    31. *  1 : プラグインコマンド[D_TEXT]で描画したい文字列と引数を指定(下記の例参照)
    32. *  2 : プラグインコマンド[D_TEXT_SETTING]で背景色や揃えを指定(任意)
    33. *  3 : イベントコマンド「ピクチャの表示」で「画像」を未選択に指定。
    34. * ※ 1の時点ではピクチャは表示されないので必ずセットで呼び出してください。
    35. * ※ ピクチャ表示前にD_TEXTを複数回実行すると、複数行表示できます。
    36. *
    37. * プラグインコマンド詳細
    38. *   イベントコマンド「プラグインコマンド」から実行。
    39. *   (引数の間は半角スペースで区切る)
    40. *
    41. *  D_TEXT [描画文字列] [文字サイズ] : 動的文字列ピクチャ生成の準備
    42. *  例:D_TEXT テスト文字列 32
    43. *
    44. * 表示後は通常のピクチャと同様に移動や回転、消去ができます。
    45. * また、変数やアクターの表示など制御文字にも対応しています。
    46. *
    47. *  D_TEXT_SETTING ALIGN [揃え] : 揃え(左揃え、中央揃え、右揃え)の設定
    48. *  0:左揃え 1:中央揃え 2:右揃え
    49. *
    50. *  例:D_TEXT_SETTING ALIGN 0
    51. *      D_TEXT_SETTING ALIGN CENTER
    52. *
    53. *  D_TEXT_SETTING BG_COLOR [背景色] : 背景色の設定(CSSの色指定と同様の書式)
    54. *
    55. *  例:D_TEXT_SETTING BG_COLOR black
    56. *      D_TEXT_SETTING BG_COLOR #336699
    57. *      D_TEXT_SETTING BG_COLOR rgba(255,255,255,0.5)
    58. *
    59. * これらの設定はD_TEXTと同様、ピクチャを表示する前に行ってください。
    60. *
    61. * 対応制御文字一覧(イベントコマンド「文章の表示」と同一です)
    62. * \V[n]
    63. * \N[n]
    64. * \P[n]
    65. * \G
    66. * \C[n]
    67. * \I[n]
    68. * \{
    69. * \}
    70. *
    71. * 専用制御文字
    72. * \V[n,m](m桁分のゼロ埋めした変数の値)
    73. * \item[n]   n 番のアイテム情報(アイコン+名称)
    74. * \weapon[n] n 番の武器情報(アイコン+名称)
    75. * \armor[n]  n 番の防具情報(アイコン+名称)
    76. * \skill[n]  n 番のスキル情報(アイコン+名称)
    77. * \state[n]  n 番のステート情報(アイコン+名称)
    78. *
    79. * 利用規約:
    80. *  作者に無断で改変、再配布が可能で、利用形態(商用、18禁利用等)
    81. *  についても制限はありません。
    82. *  このプラグインはもうあなたのものです。
    83. */
    84. (function () {
    85.     var getCommandName = function (command) {
    86.         return (command || '').toUpperCase();
    87.     };
    88.     var getArgNumber = function (arg, min, max) {
    89.         if (arguments.length < 2) min = -Infinity;
    90.         if (arguments.length < 3) max = Infinity;
    91.         return (parseInt(convertEscapeCharacters(arg.toString()), 10) || 0).clamp(min, max);
    92.     };
    93.     var getArgString = function (arg, upperFlg) {
    94.         arg = convertEscapeCharacters(arg);
    95.         return upperFlg ? arg.toUpperCase() : arg;
    96.     };
    97.     var connectArgs = function (args, startIndex, endIndex) {
    98.         if (arguments.length < 2) startIndex = 0;
    99.         if (arguments.length < 3) endIndex = args.length;
    100.         var text = '';
    101.         for (var i = startIndex; i < endIndex; i++) {
    102.             text += args[i];
    103.             if (i < endIndex - 1) text += ' ';
    104.         }
    105.         return text;
    106.     };
    107.     var convertEscapeCharacters = function(text) {
    108.         if (text == null) text = '';
    109.         var window = SceneManager.getHiddenWindow();
    110.         return window ? window.convertEscapeCharacters(text) : text;
    111.     };
    112.     SceneManager.getHiddenWindow = function() {
    113.         return this._scene._hiddenWindow;
    114.     };
    115.     //=============================================================================
    116.     // Game_Interpreter
    117.     //  プラグインコマンド[D_TEXT]を追加定義します。
    118.     //=============================================================================
    119.     var _Game_Interpreter_pluginCommand      = Game_Interpreter.prototype.pluginCommand;
    120.     Game_Interpreter.prototype.pluginCommand = function (command, args) {
    121.         _Game_Interpreter_pluginCommand.apply(this, arguments);
    122.         try {
    123.             this.pluginCommandDTextPicture(command, args);
    124.         } catch (e) {
    125.             if ($gameTemp.isPlaytest() && Utils.isNwjs()) {
    126.                 var window = require('nw.gui').Window.get();
    127.                 if (!window.isDevToolsOpen()) {
    128.                     var devTool = window.showDevTools();
    129.                     devTool.moveTo(0, 0);
    130.                     devTool.resizeTo(Graphics.width, Graphics.height);
    131.                     window.focus();
    132.                 }
    133.             }
    134.             console.log('プラグインコマンドの実行中にエラーが発生しました。');
    135.             console.log('- コマンド名  : ' + command);
    136.             console.log('- コマンド引数 : ' + args);
    137.             console.log('- エラー原因   : ' + e.toString());
    138.         }
    139.     };
    140.     Game_Interpreter.textAlignMapper = {
    141.         LEFT:0, CENTER:1, RIGHT:2, 左:0, 中央:1, 右:2
    142.     };
    143.     Game_Interpreter.prototype.pluginCommandDTextPicture = function(command, args) {
    144.         switch (getCommandName(command)) {
    145.             case 'D_TEXT' :
    146.                 if (isNaN(args[args.length - 1])) args.push($gameScreen.dTextSize || 28);
    147.                 var fontSize = getArgNumber(args.pop());
    148.                 $gameScreen.setDTextPicture(getArgString(connectArgs(args), false), fontSize);
    149.                 break;
    150.             case 'D_TEXT_SETTING':
    151.                 switch (getCommandName(args[0])) {
    152.                     case 'ALIGN' :
    153.                         $gameScreen.dTextAlign = isNaN(args[1]) ?
    154.                             Game_Interpreter.textAlignMapper[getArgString(args[1], true)] : getArgNumber(args[1], 0, 2);
    155.                         break;
    156.                     case 'BG_COLOR' :
    157.                         $gameScreen.dTextBackColor = getArgString(connectArgs(args, 1));
    158.                         break;
    159.                     case 'FONT':
    160.                         $gameScreen.setFont(getArgString(args[1]));
    161.                         break;
    162.                 }
    163.                 break;
    164.         }
    165.     };
    166.     //=============================================================================
    167.     // Game_Screen
    168.     //  動的ピクチャ用のプロパティを追加定義します。
    169.     //=============================================================================
    170.     var _Game_Screen_clear = Game_Screen.prototype.clear;
    171.     Game_Screen.prototype.clear = function() {
    172.         _Game_Screen_clear.call(this);
    173.         this.clearDTextPicture();
    174.     };
    175.     Game_Screen.prototype.clearDTextPicture = function() {
    176.         this.dTextValue = null;
    177.         this.dTextSize  = 0;
    178.         this.dTextAlign = 0;
    179.         this.dTextBackColor = null;
    180.         this.dTextFont = null;
    181.     };
    182.     Game_Screen.prototype.setDTextPicture = function(value, size) {
    183.         if (!this.dTextValue) this.dTextValue = [];
    184.         this.dTextValue.push(value);
    185.         this.dTextSize = size;
    186.     };
    187.     Game_Screen.prototype.getDTextPictureInfo = function() {
    188.         return {value:this.dTextValue, size:this.dTextSize, align:this.dTextAlign,
    189.             color:this.dTextBackColor, font:this.dTextFont};
    190.     };
    191.     Game_Screen.prototype.isSettingDText = function() {
    192.         return !!this.dTextValue;
    193.     };
    194.     Game_Screen.prototype.setFont = function(name) {
    195.         if (Graphics.isFontLoaded(name)) {
    196.             this.dTextFont = name;
    197.         }
    198.     };
    199.     //=============================================================================
    200.     // Game_Picture
    201.     //  動的ピクチャ用のプロパティを追加定義し、表示処理を動的ピクチャ対応に変更します。
    202.     //=============================================================================
    203.     var _Game_Picture_initBasic = Game_Picture.prototype.initBasic;
    204.     Game_Picture.prototype.initBasic = function() {
    205.         _Game_Picture_initBasic.call(this);
    206.         this.dTextValue = null;
    207.         this.dTextInfo = null;
    208.     };
    209.     var _Game_Picture_show = Game_Picture.prototype.show;
    210.     Game_Picture.prototype.show = function(name, origin, x, y, scaleX,
    211.                                            scaleY, opacity, blendMode) {
    212.         if ($gameScreen.isSettingDText()) {
    213.             arguments[0] = Date.now().toString();
    214.             var textValue = "";
    215.             this.dTextInfo = $gameScreen.getDTextPictureInfo();
    216.             this.dTextInfo.value.forEach(function(text) {
    217.                 textValue +=  text + '\n';
    218.             }.bind(this));
    219.             this.dTextInfo.value = textValue;
    220.         } else {
    221.             this.dTextInfo = null;
    222.         }
    223.         $gameScreen.clearDTextPicture();
    224.         _Game_Picture_show.apply(this, arguments);
    225.     };
    226.     var _Window_Base_convertEscapeCharacters = Window_Base.prototype.convertEscapeCharacters;
    227.     Window_Base.prototype.convertEscapeCharacters = function(text) {
    228.         text = _Window_Base_convertEscapeCharacters.call(this, text);
    229.         text = text.replace(/\x1bV\[(\d+)\,(\d+)\]/gi, function() {
    230.             return $gameVariables.value(parseInt(arguments[1])).padZero(arguments[2]);
    231.         }.bind(this));
    232.         text = text.replace(/\x1bITEM\[(\d+)\]/gi, function() {
    233.             var item = $dataItems[getArgNumber(arguments[1], 1, $dataItems.length)];
    234.             return item ? '\x1bi[' + item.iconIndex + ']' + item.name : '';
    235.         }.bind(this));
    236.         text = text.replace(/\x1bWEAPON\[(\d+)\]/gi, function() {
    237.             var item = $dataWeapons[getArgNumber(arguments[1], 1, $dataWeapons.length)];
    238.             return item ? '\x1bi[' + item.iconIndex + ']' + item.name : '';
    239.         }.bind(this));
    240.         text = text.replace(/\x1bARMOR\[(\d+)\]/gi, function() {
    241.             var item = $dataArmors[getArgNumber(arguments[1], 1, $dataArmors.length)];
    242.             return item ? '\x1bi[' + item.iconIndex + ']' + item.name : '';
    243.         }.bind(this));
    244.         text = text.replace(/\x1bSKILL\[(\d+)\]/gi, function() {
    245.             var item = $dataSkills[getArgNumber(arguments[1], 1, $dataSkills.length)];
    246.             return item ? '\x1bi[' + item.iconIndex + ']' + item.name : '';
    247.         }.bind(this));
    248.         text = text.replace(/\x1bSTATE\[(\d+)\]/gi, function() {
    249.             var item = $dataStates[getArgNumber(arguments[1], 1, $dataStates.length)];
    250.             return item ? '\x1bi[' + item.iconIndex + ']' + item.name : '';
    251.         }.bind(this));
    252.         return text;
    253.     };
    254.     //=============================================================================
    255.     // Sprite_Picture
    256.     //  画像の動的生成を追加定義します。
    257.     //=============================================================================
    258.     var _Sprite_Picture_loadBitmap = Sprite_Picture.prototype.loadBitmap;
    259.     Sprite_Picture.prototype.loadBitmap = function() {
    260.         this.dTextInfo = this.picture().dTextInfo;
    261.         if (this.dTextInfo) {
    262.             this.makeDynamicBitmap();
    263.         } else {
    264.             _Sprite_Picture_loadBitmap.call(this);
    265.         }
    266.     };
    267.     Sprite_Picture.prototype.makeDynamicBitmap = function() {
    268.         this.textWidths = [];
    269.         this.hiddenWindow = SceneManager.getHiddenWindow();
    270.         if (this.dTextInfo.font) this.hiddenWindow.contents.fontFace = this.dTextInfo.font;
    271.         if (this.dTextInfo.size > 0) this.hiddenWindow.contents.fontSize = this.dTextInfo.size;
    272.         var bitmapVirtual = new Bitmap_Virtual();
    273.         this._processText(bitmapVirtual);
    274.         this.bitmap = new Bitmap(bitmapVirtual.width, bitmapVirtual.height);
    275.         if (this.dTextInfo.font) this.bitmap.fontFace = this.dTextInfo.font;
    276.         if (this.dTextInfo.color) this.bitmap.fillAll(this.dTextInfo.color);
    277.         this._processText(this.bitmap);
    278.         this.hiddenWindow = null;
    279.     };
    280.     Sprite_Picture.prototype._processText = function(bitmap) {
    281.         var textState = {index: 0, x: 0, y: 0, text: this.dTextInfo.value, left:0, line:-1, height:0};
    282.         this._processNewLine(textState, bitmap);
    283.         textState.height = this.hiddenWindow.calcTextHeight(textState, false);
    284.         textState.index  = 0;
    285.         while (textState.text[textState.index]) {
    286.             this._processCharacter(textState, bitmap);
    287.         }
    288.     };
    289.     Sprite_Picture.prototype._processCharacter = function(textState, bitmap) {
    290.         if (textState.text[textState.index] === '\x1b') {
    291.             var code = this.hiddenWindow.obtainEscapeCode(textState);
    292.             switch (code) {
    293.                 case 'C':
    294.                     bitmap.textColor = this.hiddenWindow.textColor(this.hiddenWindow.obtainEscapeParam(textState));
    295.                     break;
    296.                 case 'I':
    297.                     this._processDrawIcon(this.hiddenWindow.obtainEscapeParam(textState), textState, bitmap);
    298.                     break;
    299.                 case '{':
    300.                     this.hiddenWindow.makeFontBigger();
    301.                     break;
    302.                 case '}':
    303.                     this.hiddenWindow.makeFontSmaller();
    304.                     break;
    305.                 case 'F':
    306.                     switch (this.hiddenWindow.obtainEscapeParam(textState)) {
    307.                         case 'I':
    308.                             bitmap.fontItalic = true;
    309.                             break;
    310.                         case '/':
    311.                         case 'N':
    312.                             bitmap.fontItalic = false;
    313.                             break;
    314.                     }
    315.                     break;
    316.             }
    317.         } else if (textState.text[textState.index] === '\n') {
    318.             this._processNewLine(textState, bitmap);
    319.         } else {
    320.             var c = textState.text[textState.index++];
    321.             var w = this.hiddenWindow.textWidth(c);
    322.             bitmap.fontSize = this.hiddenWindow.contents.fontSize;
    323.             bitmap.drawText(c, textState.x, textState.y, w * 2, textState.height, "left");
    324.             textState.x += w;
    325.         }
    326.     };
    327.     Sprite_Picture.prototype._processNewLine = function(textState, bitmap) {
    328.         if (bitmap instanceof Bitmap_Virtual)
    329.             this.textWidths[textState.line] = textState.x;
    330.         this.hiddenWindow.processNewLine(textState);
    331.         textState.line++;
    332.         if (bitmap instanceof Bitmap)
    333.             textState.x = (bitmap.width - this.textWidths[textState.line]) / 2 * this.dTextInfo.align;
    334.     };
    335.     Sprite_Picture.prototype._processDrawIcon = function(iconIndex, textState, bitmap) {
    336.         var iconBitmap = ImageManager.loadSystem('IconSet');
    337.         var pw = Window_Base._iconWidth;
    338.         var ph = Window_Base._iconHeight;
    339.         var sx = iconIndex % 16 * pw;
    340.         var sy = Math.floor(iconIndex / 16) * ph;
    341.         bitmap.blt(iconBitmap, sx, sy, pw, ph, textState.x + 2, textState.y + (textState.height - ph) / 2);
    342.         textState.x += Window_Base._iconWidth + 4;
    343.     };
    344.     //=============================================================================
    345.     // Scene_Map
    346.     //  動的ピクチャ作成用の隠しウィンドウを追加定義します。
    347.     //=============================================================================
    348.     var _Scene_Map_createDisplayObjects = Scene_Map.prototype.createDisplayObjects;
    349.     Scene_Map.prototype.createDisplayObjects = function() {
    350.         this._hiddenWindow = new Window_Base(1,1,1,1);
    351.         this._hiddenWindow.hide();
    352.         this._hiddenWindow.deactivate();
    353.         _Scene_Map_createDisplayObjects.call(this);
    354.         this.addChild(this._hiddenWindow);
    355.     };
    356.     //=============================================================================
    357.     // Scene_Battle
    358.     //  動的ピクチャ作成用の隠しウィンドウを追加定義します。
    359.     //=============================================================================
    360.     var _Scene_Battle_createDisplayObjects = Scene_Battle.prototype.createDisplayObjects;
    361.     Scene_Battle.prototype.createDisplayObjects = function() {
    362.         this._hiddenWindow = new Window_Base(1,1,1,1);
    363.         this._hiddenWindow.hide();
    364.         this._hiddenWindow.deactivate();
    365.         _Scene_Battle_createDisplayObjects.call(this);
    366.         this.addChild(this._hiddenWindow);
    367.     };
    368.     //=============================================================================
    369.     // Bitmap_Virtual
    370.     //  サイズを計算するための仮想ビットマップクラス
    371.     //=============================================================================
    372.     function Bitmap_Virtual() {
    373.         this.initialize.apply(this, arguments);
    374.     }
    375.     Bitmap_Virtual.prototype.initialize = function() {
    376.         this.window = SceneManager.getHiddenWindow();
    377.         this.width = 0;
    378.         this.height = 0;
    379.     };
    380.     Bitmap_Virtual.prototype.drawText = function(text, x, y, maxWidth, lineHeight, align) {
    381.         this.width  = Math.max(x + this.window.textWidth(text), this.width);
    382.         this.height = Math.max(y + this.window.contents.fontSize + 8, this.height);
    383.     };
    384.     Bitmap_Virtual.prototype.blt = function(source, sx, sy, sw, sh, dx, dy, dw, dh) {
    385.         this.width  = Math.max(dx + (dw || sw), this.width);
    386.         this.height = Math.max(dy + (dy || sy), this.height);
    387.     };
    388. })();
    复制代码



    自己用好几年MV一直都在用的插件,可以把文字以图片形式显示UI,感觉也是一个MZ必备的插件
    无奈自己太菜不会移植插件
    求大佬移植!

                 本帖来自P1论坛作者79160475,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=483684  若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
    天天去同能,天天有童年!
    回复 论坛版权

    使用道具 举报

    ahome_bigavatar:guest
    ahome_bigavatar:welcomelogin
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2024-5-20 11:30 , Processed in 0.048404 second(s), 41 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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