Advertisement
Sunspider

Textbox Customization - Compiled Version

Feb 11th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. 💬📐
  3. @file textbox customizer
  4. @summary
  5. @license MIT
  6. @version 1.0.0
  7. @requires Bitsy Version: 6.1
  8. @author Dana Holdampf
  9.  
  10. @description
  11. This hack lets you edit the appearance, position, and other properties of the textbox.
  12. It lets you draw a border made of sprites, replace the continue arrow, etc.
  13. TODO: WRITE A MORE THOROUGH DESCRIPTION
  14.  
  15. HOW TO USE:
  16. 1. Copy-paste this script into a script tag after the bitsy source
  17. 2. Edit hackOptions below, as needed
  18.  
  19. TODO: WRITE A HOW-TO
  20.  
  21. Colors are defined as 4 values, [red,green,blue,alpha], each ranging from 0-255.
  22. Alpha is opacity; colors can be translucent by reducing Alpha below 255. 0 is fully transparent.
  23. NIGHT NOTE: Maybe recalculate center position, considering textbox margins in center calculation?
  24. */
  25. this.hacks = this.hacks || {};
  26. (function (exports, bitsy) {
  27. 'use strict';
  28. var hackOptions = {
  29.     // Determines where the textbox is positioned. "shift" moves the textbox based on the player's position.
  30.     verticalPosition: "shift", // options are "top", "center", "bottom", or "shift" (moves based on player position)
  31.     horizontalPosition: "center", // options are "left", "center", "right", or "shift" (moves based on player position)
  32.  
  33.     textboxColor: [0,0,0,255], // Colors text and textbox are drawn, in [R,G,B,A]
  34.     textboxWidth: 124, // Width of textbox in pixels. Default 104.
  35.     textboxMarginX: 4, // Pixels of space outside the left (or right) of the textbox. Default 12.
  36.     textboxMarginY: 4, // Pixels of space outside the top (or bottom) of the textbox. Default 12.
  37.  
  38.     textColor: [255,255,255,255], // Default color of text.
  39.     textMinLines: 1, // Minimum number of rows for text. Determines starting textbox height. Default 2.
  40.     textMaxLines: 2, // Maximum number of rows for text. Determines max textbox height. Default 2.
  41.     textScale: 2, // Scaling factor for text. Default 2.
  42.     textSpeed: 50, // Time to print each character, in milliseconds. Default 50.
  43.     textPaddingX: 0, // Default color of text.
  44.     textPaddingY: 2, // Default color of text.
  45.  
  46.     // Colors the borders are drawn using, in [R,G,B,A]
  47.     borderColor: [128,128,128,255],
  48.     borderBGColor: [0,0,0,255],
  49.     borderMidColor: [51,51,51,255], // Color used for middle border
  50.    
  51.     // Border is drawn past the edges of the textbox.
  52.     borderWidth: 8, // Width of border sprites, in pixels. Default 8.
  53.     borderHeight: 8, // Height of border sprites, in pixels. Default 8.
  54.     borderScale: 2, // Scaling factor for border sprites. Default 2.
  55.  
  56.     // Color the continue arrow is drawn using, in [R,G,B,A]
  57.     arrowColor: [255,255,255,255],
  58.     arrowBGColor: [0,0,0,255],
  59.     arrowScale: 2, // Scaling factor for arrow sprite. Default 2.
  60.  
  61.     // Position of textbox continue arrow, on bottom of textbox. Uncomment one.
  62.     arrowAlign: "right", // Options are: "right", "center", or "left" aligned
  63.  
  64.     // Pixels of padding the arrow is inset from the edge by
  65.     arrowInsetX: 12,
  66.     arrowInsetY: 0,
  67.  
  68.     // Arrow Sprite. 1 draws a pixel in main Color, 0 draws in BG Color.
  69.     arrowWidth: 8, // Width of arrow sprite below, in pixels
  70.     arrowHeight: 5, // Height of arrow sprite below, in pixels
  71.  
  72.     arrowSprite: [
  73.         1,1,1,1,1,1,1,1,
  74.         0,1,1,1,1,1,1,0,
  75.         0,0,1,1,1,1,0,0,
  76.         0,0,0,1,1,0,0,0,
  77.         0,0,0,0,0,0,0,0
  78.     ],
  79.     borderUL: [
  80.         0,0,0,0,0,0,0,0,
  81.         0,0,0,0,0,0,0,0,
  82.         0,0,1,1,1,1,1,1,
  83.         0,0,1,1,1,1,1,1,
  84.         0,0,1,1,0,0,0,0,
  85.         0,0,1,1,0,0,0,0,
  86.         0,0,1,1,0,0,0,0,
  87.         0,0,1,1,0,0,0,0,
  88.     ],
  89.     borderU: [
  90.         0,0,0,0,0,0,0,0,
  91.         0,0,0,0,0,0,0,0,
  92.         1,1,1,1,1,1,1,1,
  93.         1,1,1,1,1,1,1,1,
  94.         0,0,0,0,0,0,0,0,
  95.         0,0,0,0,0,0,0,0,
  96.         0,0,0,0,0,0,0,0,
  97.         0,0,0,0,0,0,0,0,
  98.     ],
  99.     borderUR: [
  100.         0,0,0,0,0,0,0,0,
  101.         0,0,0,0,0,0,0,0,
  102.         1,1,1,1,1,1,0,0,
  103.         1,1,1,1,1,1,0,0,
  104.         0,0,0,0,1,1,0,0,
  105.         0,0,0,0,1,1,0,0,
  106.         0,0,0,0,1,1,0,0,
  107.         0,0,0,0,1,1,0,0,
  108.     ],
  109.     borderL: [
  110.         0,0,1,1,0,0,0,0,
  111.         0,0,1,1,0,0,0,0,
  112.         0,0,1,1,0,0,0,0,
  113.         0,0,1,1,0,0,0,0,
  114.         0,0,1,1,0,0,0,0,
  115.         0,0,1,1,0,0,0,0,
  116.         0,0,1,1,0,0,0,0,
  117.         0,0,1,1,0,0,0,0,
  118.     ],
  119.     borderR: [
  120.         0,0,0,0,1,1,0,0,
  121.         0,0,0,0,1,1,0,0,
  122.         0,0,0,0,1,1,0,0,
  123.         0,0,0,0,1,1,0,0,
  124.         0,0,0,0,1,1,0,0,
  125.         0,0,0,0,1,1,0,0,
  126.         0,0,0,0,1,1,0,0,
  127.         0,0,0,0,1,1,0,0,
  128.     ],
  129.     borderDL: [
  130.         0,0,1,1,0,0,0,0,
  131.         0,0,1,1,0,0,0,0,
  132.         0,0,1,1,0,0,0,0,
  133.         0,0,1,1,0,0,0,0,
  134.         0,0,1,1,1,1,1,1,
  135.         0,0,1,1,1,1,1,1,
  136.         0,0,0,0,0,0,0,0,
  137.         0,0,0,0,0,0,0,0,
  138.     ],
  139.     borderD: [
  140.         0,0,0,0,0,0,0,0,
  141.         0,0,0,0,0,0,0,0,
  142.         0,0,0,0,0,0,0,0,
  143.         0,0,0,0,0,0,0,0,
  144.         1,1,1,1,1,1,1,1,
  145.         1,1,1,1,1,1,1,1,
  146.         0,0,0,0,0,0,0,0,
  147.         0,0,0,0,0,0,0,0,
  148.     ],
  149.     borderDR: [
  150.         0,0,0,0,1,1,0,0,
  151.         0,0,0,0,1,1,0,0,
  152.         0,0,0,0,1,1,0,0,
  153.         0,0,0,0,1,1,0,0,
  154.         1,1,1,1,1,1,0,0,
  155.         1,1,1,1,1,1,0,0,
  156.         0,0,0,0,0,0,0,0,
  157.         0,0,0,0,0,0,0,0,
  158.     ],
  159.     borderM: [
  160.         0,0,0,0,0,0,0,0,
  161.         0,0,0,0,0,0,0,0,
  162.         0,0,0,0,0,0,0,0,
  163.         0,0,0,0,0,0,0,0,
  164.         0,0,0,0,0,0,0,0,
  165.         0,0,0,0,0,0,0,0,
  166.         0,0,0,0,0,0,0,0,
  167.         0,0,0,0,0,0,0,0,
  168.     ],
  169.  
  170.     // You can define alternate Dialog Styles here, which can override the default style above.
  171.     // You can switch styles from dialog with the {dialogStyle "styleName"} command.
  172.     initialDialogStyle: "none", // Initial Dialog Style used on load. Can be changed to any dialog style defined below.
  173.  
  174.     // These Dialog Styles are meant as examples. Feel free to edit, rename, or remove them.
  175.     dialogStyle: {
  176.         custom: {
  177.             // Copy any hackOptions from above into this section, and modify them, to create a new style.
  178.             // Any options you redefine here will override the default style, while this style is active.
  179.            
  180.         },
  181.         // An example style, which emulates the original Bitsy textbox.
  182.         vanilla: {
  183.             verticalPosition: "shift",
  184.             horizontalPosition: "center",
  185.             textboxWidth: 104,
  186.             textboxMarginX: 12,
  187.             textboxMarginY: 12,
  188.             textMinLines: 2,
  189.             textMaxLines: 2,
  190.             textPaddingX: 8,
  191.             textPaddingY: 8,
  192.             borderWidth: 0,
  193.             borderHeight: 0,
  194.             arrowScale: 4,
  195.             arrowInsetX: 12,
  196.             arrowInsetY: 0,
  197.             arrowWidth: 5,
  198.             arrowHeight: 3,
  199.             arrowSprite: [
  200.                 1,1,1,1,1,
  201.                 0,1,1,1,0,
  202.                 0,0,1,0,0,
  203.             ],
  204.         },
  205.         // An example style, drawn with a transparent background.
  206.         transparent: {
  207.             textboxColor: [255,255,255,0],
  208.             borderBGColor: [255,255,255,0],
  209.             arrowBGColor: [255,255,255,0],
  210.         },
  211.         // An example style, that centers the textbox as with Starting or Ending text.
  212.         centered: {
  213.             verticalPosition: "center",
  214.             horizontalPosition: "center",
  215.         },
  216.         // An example style, that positions the textbox vertically, on the left or right side.
  217.         vertical: {
  218.             verticalPosition: "center",
  219.             horizontalPosition: "shift",
  220.             textboxWidth: 32,
  221.             textMinLines: 16,
  222.             textMaxLines: 16,
  223.         },
  224.         // An example style, which positions a resizing textbox in the corner opposite the player.
  225.         corner: {
  226.             verticalPosition: "shift",
  227.             horizontalPosition: "shift",
  228.             textboxWidth: 64,
  229.             textMinLines: 1,
  230.             textMaxLines: 8,
  231.         },
  232.         // An example style, which inverts the normal textbox colors
  233.         inverted: {
  234.             textboxColor: [255,255,255,255],
  235.             textColor: [0,0,0,255],
  236.             borderColor: [128,128,128,255],
  237.             borderBGColor: [255,255,255,255],
  238.             borderMidColor: [204,204,204,255],
  239.             arrowColor: [0,0,0,255],
  240.             arrowBGColor: [255,255,255,255],
  241.         },
  242.         smallBorder: {
  243.             borderWidth: 4,
  244.             borderHeight: 4,
  245.             borderUL: [
  246.                 0,0,0,0,
  247.                 0,1,1,1,
  248.                 0,1,0,1,
  249.                 0,1,1,1,
  250.             ],
  251.             borderU: [
  252.                 0,0,0,0,
  253.                 1,1,1,1,
  254.                 0,0,0,0,
  255.                 0,0,0,0,
  256.             ],
  257.             borderUR: [
  258.                 0,0,0,0,
  259.                 1,1,1,0,
  260.                 1,0,1,0,
  261.                 1,1,1,0,
  262.             ],
  263.             borderL: [
  264.                 0,1,0,0,
  265.                 0,1,0,0,
  266.                 0,1,0,0,
  267.                 0,1,0,0,
  268.             ],
  269.             borderR: [
  270.                 0,0,1,0,
  271.                 0,0,1,0,
  272.                 0,0,1,0,
  273.                 0,0,1,0,
  274.             ],
  275.             borderDL: [
  276.                 0,1,1,1,
  277.                 0,1,0,1,
  278.                 0,1,1,1,
  279.                 0,0,0,0,
  280.             ],
  281.             borderD: [
  282.                 0,0,0,0,
  283.                 0,0,0,0,
  284.                 1,1,1,1,
  285.                 0,0,0,0,
  286.             ],
  287.             borderDR: [
  288.                 1,1,1,0,
  289.                 1,0,1,0,
  290.                 1,1,1,0,
  291.                 0,0,0,0,
  292.             ],
  293.             borderM: [
  294.                 0,0,0,0,
  295.                 0,0,0,0,
  296.                 0,0,0,0,
  297.                 0,0,0,0,
  298.             ],
  299.         },
  300.     },
  301. };
  302.  
  303. bitsy = bitsy && bitsy.hasOwnProperty('default') ? bitsy['default'] : bitsy;
  304.  
  305. /**
  306. @file utils
  307. @summary miscellaneous bitsy utilities
  308. @author Sean S. LeBlanc
  309. */
  310.  
  311. /*
  312. Helper used to replace code in a script tag based on a search regex
  313. To inject code without erasing original string, using capturing groups; e.g.
  314.     inject(/(some string)/,'injected before $1 injected after')
  315. */
  316. function inject(searchRegex, replaceString) {
  317.     // find the relevant script tag
  318.     var scriptTags = document.getElementsByTagName('script');
  319.     var scriptTag;
  320.     var code;
  321.     for (var i = 0; i < scriptTags.length; ++i) {
  322.         scriptTag = scriptTags[i];
  323.         var matchesSearch = scriptTag.textContent.search(searchRegex) !== -1;
  324.         var isCurrentScript = scriptTag === document.currentScript;
  325.         if (matchesSearch && !isCurrentScript) {
  326.             code = scriptTag.textContent;
  327.             break;
  328.         }
  329.     }
  330.  
  331.     // error-handling
  332.     if (!code) {
  333.         throw 'Couldn\'t find "' + searchRegex + '" in script tags';
  334.     }
  335.  
  336.     // modify the content
  337.     code = code.replace(searchRegex, replaceString);
  338.  
  339.     // replace the old script tag with a new one using our modified code
  340.     var newScriptTag = document.createElement('script');
  341.     newScriptTag.textContent = code;
  342.     scriptTag.insertAdjacentElement('afterend', newScriptTag);
  343.     scriptTag.remove();
  344. }
  345.  
  346. /**
  347.  * Helper for getting an array with unique elements
  348.  * @param  {Array} array Original array
  349.  * @return {Array}       Copy of array, excluding duplicates
  350.  */
  351. function unique(array) {
  352.     return array.filter(function (item, idx) {
  353.         return array.indexOf(item) === idx;
  354.     });
  355. }
  356.  
  357. /**
  358.  
  359. @file kitsy-script-toolkit
  360. @summary makes it easier and cleaner to run code before and after Bitsy functions or to inject new code into Bitsy script tags
  361. @license WTFPL (do WTF you want)
  362. @version 4.0.1
  363. @requires Bitsy Version: 4.5, 4.6
  364. @author @mildmojo
  365.  
  366. @description
  367. HOW TO USE:
  368.   import {before, after, inject, addDialogTag, addDeferredDialogTag} from "./helpers/kitsy-script-toolkit";
  369.  
  370.   before(targetFuncName, beforeFn);
  371.   after(targetFuncName, afterFn);
  372.   inject(searchRegex, replaceString);
  373.   addDialogTag(tagName, dialogFn);
  374.   addDeferredDialogTag(tagName, dialogFn);
  375.  
  376.   For more info, see the documentation at:
  377.   https://github.com/seleb/bitsy-hacks/wiki/Coding-with-kitsy
  378. */
  379.  
  380.  
  381. // Ex: inject(/(names.sprite.set\( name, id \);)/, '$1console.dir(names)');
  382. function inject$1(searchRegex, replaceString) {
  383.     var kitsy = kitsyInit();
  384.     kitsy.queuedInjectScripts.push({
  385.         searchRegex: searchRegex,
  386.         replaceString: replaceString
  387.     });
  388. }
  389.  
  390. function kitsyInit() {
  391.     // return already-initialized kitsy
  392.     if (bitsy.kitsy) {
  393.         return bitsy.kitsy;
  394.     }
  395.  
  396.     // Initialize kitsy
  397.     bitsy.kitsy = {
  398.         queuedInjectScripts: [],
  399.         queuedBeforeScripts: {},
  400.         queuedAfterScripts: {}
  401.     };
  402.  
  403.     var oldStartFunc = bitsy.startExportedGame;
  404.     bitsy.startExportedGame = function doAllInjections() {
  405.         // Only do this once.
  406.         bitsy.startExportedGame = oldStartFunc;
  407.  
  408.         // Rewrite scripts and hook everything up.
  409.         doInjects();
  410.         applyAllHooks();
  411.  
  412.         // Start the game
  413.         bitsy.startExportedGame.apply(this, arguments);
  414.     };
  415.  
  416.     return bitsy.kitsy;
  417. }
  418.  
  419.  
  420. function doInjects() {
  421.     bitsy.kitsy.queuedInjectScripts.forEach(function (injectScript) {
  422.         inject(injectScript.searchRegex, injectScript.replaceString);
  423.     });
  424.     _reinitEngine();
  425. }
  426.  
  427. function applyAllHooks() {
  428.     var allHooks = unique(Object.keys(bitsy.kitsy.queuedBeforeScripts).concat(Object.keys(bitsy.kitsy.queuedAfterScripts)));
  429.     allHooks.forEach(applyHook);
  430. }
  431.  
  432. function applyHook(functionName) {
  433.     var functionNameSegments = functionName.split('.');
  434.     var obj = bitsy;
  435.     while (functionNameSegments.length > 1) {
  436.         obj = obj[functionNameSegments.shift()];
  437.     }
  438.     var lastSegment = functionNameSegments[0];
  439.     var superFn = obj[lastSegment];
  440.     var superFnLength = superFn ? superFn.length : 0;
  441.     var functions = [];
  442.     // start with befores
  443.     functions = functions.concat(bitsy.kitsy.queuedBeforeScripts[functionName] || []);
  444.     // then original
  445.     if (superFn) {
  446.         functions.push(superFn);
  447.     }
  448.     // then afters
  449.     functions = functions.concat(bitsy.kitsy.queuedAfterScripts[functionName] || []);
  450.  
  451.     // overwrite original with one which will call each in order
  452.     obj[lastSegment] = function () {
  453.         var returnVal;
  454.         var args = [].slice.call(arguments);
  455.         var i = 0;
  456.  
  457.         function runBefore() {
  458.             // All outta functions? Finish
  459.             if (i === functions.length) {
  460.                 return returnVal;
  461.             }
  462.  
  463.             // Update args if provided.
  464.             if (arguments.length > 0) {
  465.                 args = [].slice.call(arguments);
  466.             }
  467.  
  468.             if (functions[i].length > superFnLength) {
  469.                 // Assume funcs that accept more args than the original are
  470.                 // async and accept a callback as an additional argument.
  471.                 return functions[i++].apply(this, args.concat(runBefore.bind(this)));
  472.             } else {
  473.                 // run synchronously
  474.                 returnVal = functions[i++].apply(this, args);
  475.                 if (returnVal && returnVal.length) {
  476.                     args = returnVal;
  477.                 }
  478.                 return runBefore.apply(this, args);
  479.             }
  480.         }
  481.  
  482.         return runBefore.apply(this, arguments);
  483.     };
  484. }
  485.  
  486. function _reinitEngine() {
  487.     // recreate the script and dialog objects so that they'll be
  488.     // referencing the code with injections instead of the original
  489.     bitsy.scriptModule = new bitsy.Script();
  490.     bitsy.scriptInterpreter = bitsy.scriptModule.CreateInterpreter();
  491.  
  492.     bitsy.dialogModule = new bitsy.Dialog();
  493.     bitsy.dialogRenderer = bitsy.dialogModule.CreateRenderer();
  494.     bitsy.dialogBuffer = bitsy.dialogModule.CreateBuffer();
  495. }
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505. // =============================================================
  506. // | RE-IMPLIMENTED LONG DIALOG HACK INJECTS |/////////////////|
  507. // =============================================================
  508. // Modified from Sean leBlanc's Long Dialog hack, to include textbox borders and padding
  509. // Needed to recalculate textbox height, based on current style parameters.
  510. // Added textMinLines and textMaxLines to hackOptions parameters, to include in style swapping
  511.  
  512. // OLD RETURN VALUE FOR REF: textboxInfo.padding_vert + (textboxInfo.padding_vert + relativeFontHeight()) * Math.max(${hackOptions.minRows}, dialogBuffer.CurPage().indexOf(dialogBuffer.CurRow())+Math.sign(dialogBuffer.CurCharCount())) + textboxInfo.arrow_height; }
  513.  
  514. // override textbox height (and recalculate textboxWidth)
  515. inject$1(/textboxInfo\.height = .+;/, `Object.defineProperty(textboxInfo, 'height', {
  516.     get() { return 2 + textPaddingY + borderHeight + (relativeFontHeight() * Math.max(${textMinLines}, dialogBuffer.CurPage().indexOf(dialogBuffer.CurRow())+Math.sign(dialogBuffer.CurCharCount()))); }
  517. })`);
  518. // prevent textbox from caching
  519. inject$1(/(if\(textboxInfo\.img == null\))/, '// $1');
  520. // rewrite hard-coded row limit
  521. inject$1(/(else if \(curRowIndex )== 0/g, '$1< ' + textMaxLines + ' - 1');
  522. inject$1(/(if\( lastPage\.length) <= 1( \) {)/, '$1 < ' + textMaxLines + ' $2');
  523.  
  524. // Add dynamic recalculation of textbox width? may need to be added after textboxInfo.height inject?
  525. //inject(/textboxInfo\.width = .+;/, `Object.defineProperty(textboxInfo, 'width', {
  526. //  get() { return textboxWidth; }
  527. //})`);
  528.  
  529.  
  530.  
  531.  
  532.  
  533. // ==========================================================================================================================
  534. // NIGHT NOTES: So... there's scope problems of some sort? Something to do with modules, I think.
  535. //
  536. // The Dialog function can't access hackOptions, or the functions defined in the hack.
  537. // It's in a virtual machine or something and can't access functions/variables?
  538. // I probably need to inject scripts for the entire function into the Dialog hack (unless there's a way to give it access)?
  539. // Keep an eye out for buggy regex; specifically check the lazy line-break-inclusive wildcards, and escaped characters.
  540. // Hack worked fine, but haven't gotten to test injected version of the hack yet, due to the scope issue.
  541. //
  542. // Once the scope issue is resolved, there's a chance hack functionality was borked or half-implemented.
  543. // Keep an eye out!
  544. //
  545. // Later, test style swapping after runtime, to make sure all the parameters get updated.
  546. // Some of the parameters might not get updated.
  547. // ==========================================================================================================================
  548.  
  549.  
  550.  
  551.  
  552.  
  553. // =============================================================
  554. // | HACK SCRIPT INJECTS |/////////////////////////////////////|
  555. // =============================================================
  556.  
  557. // Injects textbox style variables at top of Dialog function
  558. var dialogInject = `function Dialog() {
  559. console.log ("Injected Dialog Function has been called");
  560. var verticalPosition = hackOptions.verticalPosition;
  561. var horizontalPosition = hackOptions.horizontalPosition;
  562. var textboxColor = hackOptions.textboxColor;
  563. var textboxWidth = hackOptions.textboxWidth;
  564. var textboxHeight = hackOptions.textboxHeight;
  565. var textboxMarginX = hackOptions.textboxMarginX;
  566. var textboxMarginY = hackOptions.textboxMarginY;
  567. var textColor = hackOptions.textColor;
  568. var textMinLines = hackOptions.textMinLines;
  569. var textMaxLines = hackOptions.textMaxLines;
  570. var textScale = hackOptions.textScale;
  571. var textSpeed = hackOptions.textSpeed;
  572. var textPaddingX = hackOptions.textPaddingX;
  573. var textPaddingY = hackOptions.textPaddingY;
  574. var borderColor = hackOptions.borderColor;
  575. var borderBGColor = hackOptions.borderBGColor;
  576. var borderMidColor = hackOptions.borderMidColor;
  577. var borderWidth = hackOptions.borderWidth;
  578. var borderHeight = hackOptions.borderHeight;
  579. var borderScale = hackOptions.borderScale;
  580. var arrowColor = hackOptions.arrowColor;
  581. var arrowBGColor = hackOptions.arrowBGColor;
  582. var arrowScale = hackOptions.arrowScale;
  583. var arrowAlign = hackOptions.arrowAlign;
  584. var arrowInsetX = hackOptions.arrowInsetX;
  585. var arrowInsetY = hackOptions.arrowInsetY;
  586. var arrowWidth = hackOptions.arrowWidth;
  587. var arrowHeight = hackOptions.arrowHeight;
  588. var arrowSprite = hackOptions.arrowSprite;
  589. var borderUL = hackOptions.borderUL;
  590. var borderU = hackOptions.borderU;
  591. var borderUR = hackOptions.borderUR;
  592. var borderL = hackOptions.borderL;
  593. var borderR = hackOptions.borderR;
  594. var borderDL = hackOptions.borderDL;
  595. var borderD = hackOptions.borderD;
  596. var borderDR = hackOptions.borderDR;
  597. var borderM = hackOptions.borderM;
  598. `;
  599. inject$1(/function Dialog\(\) {/, dialogInject);
  600.  
  601. // Replaces initial textbox parameters, based on currently active style (or defaults).
  602. // Recalculates textbox parameters, even values no longer used with hack, for compatability.
  603. var textboxInfoReplace = `setTextboxStyle(hackOptions.initialDialogStyle);
  604.  
  605. var textboxInfo = {
  606.     img : null,
  607.     width : textboxWidth,
  608.     height : (textPaddingY + borderHeight + 2),
  609.     top : textboxMarginY,
  610.     left : textboxMarginX,
  611.     bottom : textboxMarginY,
  612.     font_scale : 0.5, //hackOptions.textScale/4;
  613.     padding_vert : 2,
  614.     padding_horz : 4,
  615.     arrow_height : (textPaddingY + borderHeight - 4),
  616. };`;
  617. inject$1(/var textboxInfo = .+?};/s, textboxInfoReplace);
  618.  
  619. // Replaces ClearTextbox function to include border-drawing scripts
  620. var clearTextboxReplace = `this.ClearTextbox = function() {
  621.     if(context == null) return;
  622.  
  623.     //create new image none exists
  624.     if(textboxInfo.img == null)
  625.         textboxInfo.img = context.createImageData(textboxInfo.width*scale, textboxInfo.height*scale);
  626.  
  627.     // Draw Textbox Background based on BGColor (indices are R,G,B, and A)
  628.     for (var i=0;i<textboxInfo.img.data.length;i+=4)
  629.     {
  630.         textboxInfo.img.data[i+0]=textboxColor[0];
  631.         textboxInfo.img.data[i+1]=textboxColor[1];
  632.         textboxInfo.img.data[i+2]=textboxColor[2];
  633.         textboxInfo.img.data[i+3]=textboxColor[3];
  634.     }
  635.  
  636.     drawTextboxBorder();
  637. };`;
  638. inject$1(/this.ClearTextbox = .+?};/s, clearTextboxReplace);
  639.  
  640. // Replaces Draw Textbox function, with function that supports vertical and horizontal shifting
  641. var drawTextboxReplace = `this.DrawTextbox = function() {
  642.     if(context == null) return;
  643.  
  644.     // Textbox defaults to center-aligned
  645.     var textboxXPosition = ((width/2)-(textboxInfo.width/2))*scale;
  646.     var textboxYPosition = ((height/2)-(textboxInfo.height/2))*scale;
  647.  
  648.     if (isCentered) {
  649.         context.putImageData(textboxInfo.img, textboxXPosition, textboxYPosition);
  650.     }
  651.     else {
  652.         if (verticalPosition.toLowerCase() = "shift") {
  653.             if (player().y < mapsize/2) {
  654.                 //player on bottom half, so draw on top
  655.                 textboxYPosition = ((height-textboxInfo.top-textboxInfo.height)*scale);
  656.             }
  657.             else {
  658.                 textboxYPosition = textboxInfo.top*scale
  659.             }
  660.         }
  661.         else if (verticalPosition.toLowerCase() = "top") {
  662.             textboxYPosition = ((height-textboxInfo.top-textboxInfo.height)*scale);
  663.         }
  664.         else if (verticalPosition.toLowerCase() = "bottom") {
  665.             textboxYPosition = textboxInfo.top*scale
  666.         }
  667.  
  668.         if (horizontalPosition.toLowerCase() = "shift") {
  669.             if (player().x < mapsize/2) {
  670.                 // player on left half, so draw on right
  671.                 textboxXPosition = ((width-textboxInfo.left-textboxInfo.width)*scale);
  672.             }
  673.             else {
  674.                 textboxXPosition = textboxInfo.left*scale
  675.             }
  676.         }
  677.         else if (horizontalPosition.toLowerCase() = "right") {
  678.             textboxXPosition = ((width-textboxInfo.left-textboxInfo.width)*scale);
  679.         }
  680.         else if (horizontalPosition.toLowerCase() = "left") {
  681.             textboxXPosition = textboxInfo.left*scale
  682.         }
  683.  
  684.         // Draw the Textbox
  685.         context.putImageData(textboxInfo.img, textboxXPosition, textboxYPosition);
  686.     }
  687. };`;
  688. inject$1(/this.DrawTextbox = .+?};/s, drawTextboxReplace);
  689.  
  690. // Replace DrawNextArrow function, to support custom sprites, colors, and arrow repositioning
  691. var drawNextArrowReplace = `this.DrawNextArrow = function() {
  692.     // Arrow sprite is center-bottom by default
  693.     var top = ((4/arrowScale)*textboxInfo.height - arrowHeight - arrowInsetY) *  arrowScale;
  694.     var left = ((4/textboxArrowScale)*textboxInfo.width - arrowXSize) * textboxArrowScale*0.5;
  695.  
  696.     // Reposition arrow based on arrowAlign and RTL settings (flipped for RTL Languages)
  697.     if (arrowAlign.toLowerCase() == "left") {
  698.         if (textDirection === TextDirection.RightToLeft) {
  699.             left = ((4/arrowScale)*textboxInfo.width - arrowWidth - arrowInsetX) *  arrowScale;
  700.         }
  701.         else {
  702.             left = (arrowXPadding) * textboxArrowScale;
  703.         }
  704.     }
  705.     else if (arrowAlign.toLowerCase() == "right") {
  706.         if (textDirection === TextDirection.RightToLeft) {
  707.             left = (arrowXPadding) * textboxArrowScale;
  708.         }
  709.         else {
  710.             left = ((4/arrowScale)*textboxInfo.width - arrowWidth - arrowInsetX) *  arrowScale;
  711.         }
  712.     }
  713.    
  714.     // Draw arrow sprite pixels on textbox
  715.     for (var y = 0; y < arrowHeight; y++) {
  716.         for (var x = 0; x < arrowWidth; x++) {
  717.             var i = (y * arrowWidth) + x;
  718.  
  719.             //scaling hooplah
  720.             for (var sy = 0; sy < arrowScale; sy++) {
  721.                 for (var sx = 0; sx < arrowScale; sx++) {
  722.                     var pxl = 4 * ( ((top+(y*arrowScale)+sy) * (textboxInfo.width*4)) + (left+(x*arrowScale)+sx) );
  723.                     // Draws arrow's pixels in Arrow Color
  724.                     if (arrowSprite[i] == 1) {
  725.                         textboxInfo.img.data[pxl+0] = arrowColor[0];
  726.                         textboxInfo.img.data[pxl+1] = arrowColor[1];
  727.                         textboxInfo.img.data[pxl+2] = arrowColor[2];
  728.                         textboxInfo.img.data[pxl+3] = arrowColor[3];
  729.                     }
  730.                     // Draws arrow's bg pixels using Arrow BG Color
  731.                     else {
  732.                         textboxInfo.img.data[pxl+0] = arrowBGColor[0];
  733.                         textboxInfo.img.data[pxl+1] = arrowBGColor[1];
  734.                         textboxInfo.img.data[pxl+2] = arrowBGColor[2];
  735.                         textboxInfo.img.data[pxl+3] = arrowBGColor[3];
  736.                     }
  737.                 }
  738.             }
  739.         }
  740.     }
  741. };`;
  742. inject$1(/this.DrawNextArrow = .+?};/s, drawNextArrowReplace);
  743.  
  744. // Inject to support custom text scaling
  745. inject$1(/var text_scale = .+?;/, 'var text_scale = textScale;');
  746.  
  747. // Injects to support text padding within the textbox
  748. var topTextPaddingReplace = `var top = (2 * textPaddingY) + (borderHeight * 2) + (row * 2 * scale) + (row * font.getHeight() * textScale) + Math.floor( char.offset.y );`;
  749. var leftTextPaddingReplace = `var left = (2* textPaddingX) + (borderWidth * 2) + (leftPos * textScale) + Math.floor( char.offset.x );`;
  750. inject$1(/var top = \(4 \* scale\) \+ \(row \* 2 \* scale\) .+?\);/, topTextPaddingReplace);
  751. inject$1(/var left = \(4 \* scale\) \+ \(leftPos \* text_scale\) .+?\);/, leftTextPaddingReplace);
  752.  
  753. // Inject to support custom text speeds
  754. inject$1(/var nextCharMaxTime = .+?;/, 'var nextCharMaxTime = textSpeed;');
  755.  
  756. // Inject to support custom default text color
  757. inject$1(/this.color = .+?};/, 'this.color = { r:textColor[0], g:textColor[1], b:textColor[2], a:textColor[3] };');
  758.  
  759. // Inject to support dynamic textbox resizing
  760. inject$1(/var pixelsPerRow = .+?;/, 'var pixelsPerRow = (textboxWidth*2) - (borderWidth*2) - (textPaddingX*2);');
  761.  
  762. exports.hackOptions = hackOptions;
  763.  
  764. }(this.hacks.textbox_customizer = this.hacks.textbox_customizer || {}, window));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement