Advertisement
modern_algebra

GlobalTextCodes

Oct 31st, 2015
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. //  GlobalTextCodes.js
  3. //=============================================================================
  4. //  Version: 1.0.2
  5. //  Date: 21 November 2015
  6. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. /*:
  8.  * @plugindesc Use basic escape codes in any window for any text
  9.  * @author Modern Algebra (rmrk.net)
  10.  * @help This plugin allows you to use basic escape codes in any text field, so long
  11.  * as you include \* in the text field. By default, the options are limited to
  12.  * \c[n]; \i[n]; \v[x]; \n[x]; \pn[x]; \{; \}; and \G. They may be expanded by
  13.  * other message scripts which add escape codes, but not likely by all such
  14.  * escape codes as some would be necessarily restricted to the message window.
  15.  *
  16.  * When using codes like \c[n] and others that change the font settings,
  17.  * remember that you should set it back to normal afterwards. In other words,
  18.  * write "\*\c[3]Text\c[0]" and not just "\*\c[3]Text"
  19.  */
  20. //=============================================================================
  21.  
  22. var Imported = Imported || {};
  23. Imported.MA_GlobalTextCodes = true;
  24.  
  25. var ModernAlgebra = ModernAlgebra || {};
  26. ModernAlgebra.GTC = {};
  27.  
  28. (function() {
  29.    
  30.     // Draw Text - Check for \* and drawTextEx if it is present
  31.     ModernAlgebra.GTC.window_Base_drawText =
  32.             Window_Base.prototype.drawText;
  33.     Window_Base.prototype.drawText = function() {
  34.         var text = arguments[0];
  35.         if ((typeof text === 'string' || text instanceof String) && text.match(/\\\*/i)) {
  36.             var tx = this.magtcCalculateAlignmentExX.apply(this, arguments);
  37.             // Draw Special Text if the \* code is present
  38.             this.drawTextEx(text, tx, arguments[2]);
  39.         } else {
  40.             // Draw Normal Text otherwise
  41.             ModernAlgebra.GTC.window_Base_drawText.apply(this, arguments);
  42.         }
  43.     };
  44.    
  45.     // Convert Escape Characters - delete \* codes
  46.     ModernAlgebra.GTC.window_Base_convertEscapeCharacters =
  47.             Window_Base.prototype.convertEscapeCharacters;
  48.     Window_Base.prototype.convertEscapeCharacters = function() {
  49.         var text = ModernAlgebra.GTC.window_Base_convertEscapeCharacters.apply(this, arguments)
  50.         text = text.replace(/\x1b\*/, ''); // Remove \* Codes
  51.         return text;
  52.     };
  53.    
  54.     // Calculate Alignment Ex X - Get display X when using different alignment
  55.     Window_Base.prototype.magtcCalculateAlignmentExX = function(text, tx, y, mw, align) {
  56.         if (align === 'center' || align === 'right') {
  57.             // Calculate line length and adjust x based on alignment
  58.             var tw = this.magtcMeasureTextEx(text);
  59.             var blankSpace = mw - tw;
  60.             if (align === 'center') {
  61.                 blankSpace = blankSpace / 2;
  62.             }
  63.             tx = tx + blankSpace;
  64.         }
  65.         return tx
  66.     };
  67.    
  68.     // Measure text that has escape codes
  69.     Window_Base.prototype.magtcMeasureTextEx = function(text) {
  70.         // Create temporary bitmap for testing to accomodate other scripts with unknown codes
  71.         this._magtcAlignmentTesting = true;
  72.         var realContents = this.contents;
  73.         this.contents = new Bitmap(24, 24);
  74.         this.resetFontSettings();
  75.         // Draw TextEx on the temporary Bitmap
  76.         var firstLine = text.match(/^\n?(.+)\n?/)[0] || '';
  77.         var tw = this.drawTextEx(firstLine, 0, 0);
  78.         // Restore normal contents
  79.         this._magtcAlignmentTesting = null;
  80.         this.contents = realContents;
  81.         return tw
  82.     }
  83.    
  84. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement