Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- 💬📐
- @file textbox customizer
- @summary
- @license MIT
- @version 1.0.0
- @requires Bitsy Version: 6.1
- @author Dana Holdampf
- @description
- This hack lets you edit the appearance, position, and other properties of the textbox.
- It lets you draw a border made of sprites, replace the continue arrow, etc.
- TODO: WRITE A MORE THOROUGH DESCRIPTION
- HOW TO USE:
- 1. Copy-paste this script into a script tag after the bitsy source
- 2. Edit hackOptions below, as needed
- TODO: WRITE A HOW-TO
- Colors are defined as 4 values, [red,green,blue,alpha], each ranging from 0-255.
- Alpha is opacity; colors can be translucent by reducing Alpha below 255. 0 is fully transparent.
- NIGHT NOTE: Maybe recalculate center position, considering textbox margins in center calculation?
- */
- import {
- after,
- // addDualDialogTag,
- inject
- } from './helpers/kitsy-script-toolkit';
- export var hackOptions = {
- // Determines where the textbox is positioned. "shift" moves the textbox based on the player's position.
- verticalPosition: "shift", // options are "top", "center", "bottom", or "shift" (moves based on player position)
- horizontalPosition: "center", // options are "left", "center", "right", or "shift" (moves based on player position)
- textboxColor: [0,0,0,255], // Colors text and textbox are drawn, in [R,G,B,A]
- textboxWidth: 124, // Width of textbox in pixels. Default 104.
- textboxMarginX: 4, // Pixels of space outside the left (or right) of the textbox. Default 12.
- textboxMarginY: 4, // Pixels of space outside the top (or bottom) of the textbox. Default 12.
- textColor: [255,255,255,255], // Default color of text.
- textMinLines: 1, // Minimum number of rows for text. Determines starting textbox height. Default 2.
- textMaxLines: 2, // Maximum number of rows for text. Determines max textbox height. Default 2.
- textScale: 2, // Scaling factor for text. Default 2.
- textSpeed: 50, // Time to print each character, in milliseconds. Default 50.
- textPaddingX: 0, // Default color of text.
- textPaddingY: 2, // Default color of text.
- // Colors the borders are drawn using, in [R,G,B,A]
- borderColor: [128,128,128,255],
- borderBGColor: [0,0,0,255],
- borderMidColor: [51,51,51,255], // Color used for middle border
- // Border is drawn past the edges of the textbox.
- borderWidth: 8, // Width of border sprites, in pixels. Default 8.
- borderHeight: 8, // Height of border sprites, in pixels. Default 8.
- borderScale: 2, // Scaling factor for border sprites. Default 2.
- // Color the continue arrow is drawn using, in [R,G,B,A]
- arrowColor: [255,255,255,255],
- arrowBGColor: [0,0,0,255],
- arrowScale: 2, // Scaling factor for arrow sprite. Default 2.
- // Position of textbox continue arrow, on bottom of textbox. Uncomment one.
- arrowAlign: "right", // Options are: "right", "center", or "left" aligned
- // Pixels of padding the arrow is inset from the edge by
- arrowInsetX: 12,
- arrowInsetY: 0,
- // Arrow Sprite. 1 draws a pixel in main Color, 0 draws in BG Color.
- arrowWidth: 8, // Width of arrow sprite below, in pixels
- arrowHeight: 5, // Height of arrow sprite below, in pixels
- arrowSprite: [
- 1,1,1,1,1,1,1,1,
- 0,1,1,1,1,1,1,0,
- 0,0,1,1,1,1,0,0,
- 0,0,0,1,1,0,0,0,
- 0,0,0,0,0,0,0,0
- ],
- borderUL: [
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,1,1,1,1,1,1,
- 0,0,1,1,1,1,1,1,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- ],
- borderU: [
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- ],
- borderUR: [
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 1,1,1,1,1,1,0,0,
- 1,1,1,1,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- ],
- borderL: [
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- ],
- borderR: [
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- ],
- borderDL: [
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,0,0,0,0,
- 0,0,1,1,1,1,1,1,
- 0,0,1,1,1,1,1,1,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- ],
- borderD: [
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- ],
- borderDR: [
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 0,0,0,0,1,1,0,0,
- 1,1,1,1,1,1,0,0,
- 1,1,1,1,1,1,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- ],
- borderM: [
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- ],
- // You can define alternate Dialog Styles here, which can override the default style above.
- // You can switch styles from dialog with the {dialogStyle "styleName"} command.
- initialDialogStyle: "none", // Initial Dialog Style used on load. Can be changed to any dialog style defined below.
- // These Dialog Styles are meant as examples. Feel free to edit, rename, or remove them.
- dialogStyle: {
- custom: {
- // Copy any hackOptions from above into this section, and modify them, to create a new style.
- // Any options you redefine here will override the default style, while this style is active.
- },
- // An example style, which emulates the original Bitsy textbox.
- vanilla: {
- verticalPosition: "shift",
- horizontalPosition: "center",
- textboxWidth: 104,
- textboxMarginX: 12,
- textboxMarginY: 12,
- textMinLines: 2,
- textMaxLines: 2,
- textPaddingX: 8,
- textPaddingY: 8,
- borderWidth: 0,
- borderHeight: 0,
- arrowScale: 4,
- arrowInsetX: 12,
- arrowInsetY: 0,
- arrowWidth: 5,
- arrowHeight: 3,
- arrowSprite: [
- 1,1,1,1,1,
- 0,1,1,1,0,
- 0,0,1,0,0,
- ],
- },
- // An example style, drawn with a transparent background.
- transparent: {
- textboxColor: [255,255,255,0],
- borderBGColor: [255,255,255,0],
- arrowBGColor: [255,255,255,0],
- },
- // An example style, that centers the textbox as with Starting or Ending text.
- centered: {
- verticalPosition: "center",
- horizontalPosition: "center",
- },
- // An example style, that positions the textbox vertically, on the left or right side.
- vertical: {
- verticalPosition: "center",
- horizontalPosition: "shift",
- textboxWidth: 32,
- textMinLines: 16,
- textMaxLines: 16,
- },
- // An example style, which positions a resizing textbox in the corner opposite the player.
- corner: {
- verticalPosition: "shift",
- horizontalPosition: "shift",
- textboxWidth: 64,
- textMinLines: 1,
- textMaxLines: 8,
- },
- // An example style, which inverts the normal textbox colors
- inverted: {
- textboxColor: [255,255,255,255],
- textColor: [0,0,0,255],
- borderColor: [128,128,128,255],
- borderBGColor: [255,255,255,255],
- borderMidColor: [204,204,204,255],
- arrowColor: [0,0,0,255],
- arrowBGColor: [255,255,255,255],
- },
- smallBorder: {
- borderWidth: 4,
- borderHeight: 4,
- borderUL: [
- 0,0,0,0,
- 0,1,1,1,
- 0,1,0,1,
- 0,1,1,1,
- ],
- borderU: [
- 0,0,0,0,
- 1,1,1,1,
- 0,0,0,0,
- 0,0,0,0,
- ],
- borderUR: [
- 0,0,0,0,
- 1,1,1,0,
- 1,0,1,0,
- 1,1,1,0,
- ],
- borderL: [
- 0,1,0,0,
- 0,1,0,0,
- 0,1,0,0,
- 0,1,0,0,
- ],
- borderR: [
- 0,0,1,0,
- 0,0,1,0,
- 0,0,1,0,
- 0,0,1,0,
- ],
- borderDL: [
- 0,1,1,1,
- 0,1,0,1,
- 0,1,1,1,
- 0,0,0,0,
- ],
- borderD: [
- 0,0,0,0,
- 0,0,0,0,
- 1,1,1,1,
- 0,0,0,0,
- ],
- borderDR: [
- 1,1,1,0,
- 1,0,1,0,
- 1,1,1,0,
- 0,0,0,0,
- ],
- borderM: [
- 0,0,0,0,
- 0,0,0,0,
- 0,0,0,0,
- 0,0,0,0,
- ],
- },
- },
- };
- // =============================================================
- // | RE-IMPLIMENTED LONG DIALOG HACK INJECTS |/////////////////|
- // =============================================================
- // Modified from Sean leBlanc's Long Dialog hack, to include textbox borders and padding
- // Needed to recalculate textbox height, based on current style parameters.
- // Added textMinLines and textMaxLines to hackOptions parameters, to include in style swapping
- // 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; }
- // override textbox height (and recalculate textboxWidth)
- inject(/textboxInfo\.height = .+;/, `Object.defineProperty(textboxInfo, 'height', {
- get() { return 2 + textPaddingY + borderHeight + (relativeFontHeight() * Math.max(${textMinLines}, dialogBuffer.CurPage().indexOf(dialogBuffer.CurRow())+Math.sign(dialogBuffer.CurCharCount()))); }
- })`);
- // prevent textbox from caching
- inject(/(if\(textboxInfo\.img == null\))/, '// $1');
- // rewrite hard-coded row limit
- inject(/(else if \(curRowIndex )== 0/g, '$1< ' + textMaxLines + ' - 1');
- inject(/(if\( lastPage\.length) <= 1( \) {)/, '$1 < ' + textMaxLines + ' $2');
- // Add dynamic recalculation of textbox width? may need to be added after textboxInfo.height inject?
- //inject(/textboxInfo\.width = .+;/, `Object.defineProperty(textboxInfo, 'width', {
- // get() { return textboxWidth; }
- //})`);
- // ==========================================================================================================================
- // NIGHT NOTES: So... there's scope problems of some sort? Something to do with modules, I think.
- //
- // The Dialog function can't access hackOptions, or the functions defined in the hack.
- // It's in a virtual machine or something and can't access functions/variables?
- // I probably need to inject scripts for the entire function into the Dialog hack (unless there's a way to give it access)?
- // Keep an eye out for buggy regex; specifically check the lazy line-break-inclusive wildcards, and escaped characters.
- // Hack worked fine, but haven't gotten to test injected version of the hack yet, due to the scope issue.
- //
- // Once the scope issue is resolved, there's a chance hack functionality was borked or half-implemented.
- // Keep an eye out!
- //
- // Later, test style swapping after runtime, to make sure all the parameters get updated.
- // Some of the parameters might not get updated.
- // ==========================================================================================================================
- // =============================================================
- // | HACK SCRIPT INJECTS |/////////////////////////////////////|
- // =============================================================
- // Injects textbox style variables at top of Dialog function
- var dialogInject = `function Dialog() {
- console.log ("Injected Dialog Function has been called");
- var verticalPosition = hackOptions.verticalPosition;
- var horizontalPosition = hackOptions.horizontalPosition;
- var textboxColor = hackOptions.textboxColor;
- var textboxWidth = hackOptions.textboxWidth;
- var textboxHeight = hackOptions.textboxHeight;
- var textboxMarginX = hackOptions.textboxMarginX;
- var textboxMarginY = hackOptions.textboxMarginY;
- var textColor = hackOptions.textColor;
- var textMinLines = hackOptions.textMinLines;
- var textMaxLines = hackOptions.textMaxLines;
- var textScale = hackOptions.textScale;
- var textSpeed = hackOptions.textSpeed;
- var textPaddingX = hackOptions.textPaddingX;
- var textPaddingY = hackOptions.textPaddingY;
- var borderColor = hackOptions.borderColor;
- var borderBGColor = hackOptions.borderBGColor;
- var borderMidColor = hackOptions.borderMidColor;
- var borderWidth = hackOptions.borderWidth;
- var borderHeight = hackOptions.borderHeight;
- var borderScale = hackOptions.borderScale;
- var arrowColor = hackOptions.arrowColor;
- var arrowBGColor = hackOptions.arrowBGColor;
- var arrowScale = hackOptions.arrowScale;
- var arrowAlign = hackOptions.arrowAlign;
- var arrowInsetX = hackOptions.arrowInsetX;
- var arrowInsetY = hackOptions.arrowInsetY;
- var arrowWidth = hackOptions.arrowWidth;
- var arrowHeight = hackOptions.arrowHeight;
- var arrowSprite = hackOptions.arrowSprite;
- var borderUL = hackOptions.borderUL;
- var borderU = hackOptions.borderU;
- var borderUR = hackOptions.borderUR;
- var borderL = hackOptions.borderL;
- var borderR = hackOptions.borderR;
- var borderDL = hackOptions.borderDL;
- var borderD = hackOptions.borderD;
- var borderDR = hackOptions.borderDR;
- var borderM = hackOptions.borderM;
- `;
- inject(/function Dialog\(\) {/, dialogInject);
- // Replaces initial textbox parameters, based on currently active style (or defaults).
- // Recalculates textbox parameters, even values no longer used with hack, for compatability.
- var textboxInfoReplace = `setTextboxStyle(hackOptions.initialDialogStyle);
- var textboxInfo = {
- img : null,
- width : textboxWidth,
- height : (textPaddingY + borderHeight + 2),
- top : textboxMarginY,
- left : textboxMarginX,
- bottom : textboxMarginY,
- font_scale : 0.5, //hackOptions.textScale/4;
- padding_vert : 2,
- padding_horz : 4,
- arrow_height : (textPaddingY + borderHeight - 4),
- };`;
- inject(/var textboxInfo = .+?};/s, textboxInfoReplace);
- // Replaces ClearTextbox function to include border-drawing scripts
- var clearTextboxReplace = `this.ClearTextbox = function() {
- if(context == null) return;
- //create new image none exists
- if(textboxInfo.img == null)
- textboxInfo.img = context.createImageData(textboxInfo.width*scale, textboxInfo.height*scale);
- // Draw Textbox Background based on BGColor (indices are R,G,B, and A)
- for (var i=0;i<textboxInfo.img.data.length;i+=4)
- {
- textboxInfo.img.data[i+0]=textboxColor[0];
- textboxInfo.img.data[i+1]=textboxColor[1];
- textboxInfo.img.data[i+2]=textboxColor[2];
- textboxInfo.img.data[i+3]=textboxColor[3];
- }
- drawTextboxBorder();
- };`
- inject(/this.ClearTextbox = .+?};/s, clearTextboxReplace);
- // Replaces Draw Textbox function, with function that supports vertical and horizontal shifting
- var drawTextboxReplace = `this.DrawTextbox = function() {
- if(context == null) return;
- // Textbox defaults to center-aligned
- var textboxXPosition = ((width/2)-(textboxInfo.width/2))*scale;
- var textboxYPosition = ((height/2)-(textboxInfo.height/2))*scale;
- if (isCentered) {
- context.putImageData(textboxInfo.img, textboxXPosition, textboxYPosition);
- }
- else {
- if (verticalPosition.toLowerCase() = "shift") {
- if (player().y < mapsize/2) {
- //player on bottom half, so draw on top
- textboxYPosition = ((height-textboxInfo.top-textboxInfo.height)*scale);
- }
- else {
- textboxYPosition = textboxInfo.top*scale
- }
- }
- else if (verticalPosition.toLowerCase() = "top") {
- textboxYPosition = ((height-textboxInfo.top-textboxInfo.height)*scale);
- }
- else if (verticalPosition.toLowerCase() = "bottom") {
- textboxYPosition = textboxInfo.top*scale
- }
- if (horizontalPosition.toLowerCase() = "shift") {
- if (player().x < mapsize/2) {
- // player on left half, so draw on right
- textboxXPosition = ((width-textboxInfo.left-textboxInfo.width)*scale);
- }
- else {
- textboxXPosition = textboxInfo.left*scale
- }
- }
- else if (horizontalPosition.toLowerCase() = "right") {
- textboxXPosition = ((width-textboxInfo.left-textboxInfo.width)*scale);
- }
- else if (horizontalPosition.toLowerCase() = "left") {
- textboxXPosition = textboxInfo.left*scale
- }
- // Draw the Textbox
- context.putImageData(textboxInfo.img, textboxXPosition, textboxYPosition);
- }
- };`;
- inject(/this.DrawTextbox = .+?};/s, drawTextboxReplace);
- // Replace DrawNextArrow function, to support custom sprites, colors, and arrow repositioning
- var drawNextArrowReplace = `this.DrawNextArrow = function() {
- // Arrow sprite is center-bottom by default
- var top = ((4/arrowScale)*textboxInfo.height - arrowHeight - arrowInsetY) * arrowScale;
- var left = ((4/textboxArrowScale)*textboxInfo.width - arrowXSize) * textboxArrowScale*0.5;
- // Reposition arrow based on arrowAlign and RTL settings (flipped for RTL Languages)
- if (arrowAlign.toLowerCase() == "left") {
- if (textDirection === TextDirection.RightToLeft) {
- left = ((4/arrowScale)*textboxInfo.width - arrowWidth - arrowInsetX) * arrowScale;
- }
- else {
- left = (arrowXPadding) * textboxArrowScale;
- }
- }
- else if (arrowAlign.toLowerCase() == "right") {
- if (textDirection === TextDirection.RightToLeft) {
- left = (arrowXPadding) * textboxArrowScale;
- }
- else {
- left = ((4/arrowScale)*textboxInfo.width - arrowWidth - arrowInsetX) * arrowScale;
- }
- }
- // Draw arrow sprite pixels on textbox
- for (var y = 0; y < arrowHeight; y++) {
- for (var x = 0; x < arrowWidth; x++) {
- var i = (y * arrowWidth) + x;
- //scaling hooplah
- for (var sy = 0; sy < arrowScale; sy++) {
- for (var sx = 0; sx < arrowScale; sx++) {
- var pxl = 4 * ( ((top+(y*arrowScale)+sy) * (textboxInfo.width*4)) + (left+(x*arrowScale)+sx) );
- // Draws arrow's pixels in Arrow Color
- if (arrowSprite[i] == 1) {
- textboxInfo.img.data[pxl+0] = arrowColor[0];
- textboxInfo.img.data[pxl+1] = arrowColor[1];
- textboxInfo.img.data[pxl+2] = arrowColor[2];
- textboxInfo.img.data[pxl+3] = arrowColor[3];
- }
- // Draws arrow's bg pixels using Arrow BG Color
- else {
- textboxInfo.img.data[pxl+0] = arrowBGColor[0];
- textboxInfo.img.data[pxl+1] = arrowBGColor[1];
- textboxInfo.img.data[pxl+2] = arrowBGColor[2];
- textboxInfo.img.data[pxl+3] = arrowBGColor[3];
- }
- }
- }
- }
- }
- };`;
- inject(/this.DrawNextArrow = .+?};/s, drawNextArrowReplace);
- // Inject to support custom text scaling
- inject(/var text_scale = .+?;/, 'var text_scale = textScale;');
- // Injects to support text padding within the textbox
- var topTextPaddingReplace = `var top = (2 * textPaddingY) + (borderHeight * 2) + (row * 2 * scale) + (row * font.getHeight() * textScale) + Math.floor( char.offset.y );`;
- var leftTextPaddingReplace = `var left = (2* textPaddingX) + (borderWidth * 2) + (leftPos * textScale) + Math.floor( char.offset.x );`;
- inject(/var top = \(4 \* scale\) \+ \(row \* 2 \* scale\) .+?\);/, topTextPaddingReplace);
- inject(/var left = \(4 \* scale\) \+ \(leftPos \* text_scale\) .+?\);/, leftTextPaddingReplace);
- // Inject to support custom text speeds
- inject(/var nextCharMaxTime = .+?;/, 'var nextCharMaxTime = textSpeed;');
- // Inject to support custom default text color
- inject(/this.color = .+?};/, 'this.color = { r:textColor[0], g:textColor[1], b:textColor[2], a:textColor[3] };');
- // Inject to support dynamic textbox resizing
- inject(/var pixelsPerRow = .+?;/, 'var pixelsPerRow = (textboxWidth*2) - (borderWidth*2) - (textPaddingX*2);');
- //var pixelsPerRow = (textboxWidth*2) - (borderWidth*2) - (textPaddingX*2); // hard-coded fun times!!! 192
- // =============================================================
- // | HACK FUNCTIONS |//////////////////////////////////////////|
- // =============================================================
- // Resets Textbox Style to Default
- function resetTextboxStyle() {
- console.log ("RESETTING TO DEFAULT TEXTBOX STYLE");
- verticalPosition = hackOptions.verticalPosition;
- horizontalPosition = hackOptions.horizontalPosition;
- textboxColor = hackOptions.textboxColor;
- textboxWidth = hackOptions.textboxWidth;
- textboxMarginX = hackOptions.textboxMarginX;
- textboxMarginY = hackOptions.textboxMarginY;
- textColor = hackOptions.textColor;
- textMinLines = hackOptions.textMinLines;
- textMaxLines = hackOptions.textMaxLines;
- textScale = hackOptions.textScale;
- textSpeed = hackOptions.textSpeed;
- textPaddingX = hackOptions.textPaddingX;
- textPaddingY = hackOptions.textPaddingY;
- borderColor = hackOptions.borderColor;
- borderBGColor = hackOptions.borderBGColor;
- borderMidColor = hackOptions.borderMidColor;
- borderWidth = hackOptions.borderWidth;
- borderHeight = hackOptions.borderHeight;
- borderScale = hackOptions.borderScale;
- arrowColor = hackOptions.arrowColor;
- arrowBGColor = hackOptions.arrowBGColor;
- arrowScale = hackOptions.arrowScale;
- arrowAlign = hackOptions.arrowAlign;
- arrowInsetX = hackOptions.arrowInsetX;
- arrowInsetY = hackOptions.arrowInsetY;
- arrowWidth = hackOptions.arrowWidth;
- arrowHeight = hackOptions.arrowHeight;
- arrowSprite = hackOptions.arrowSprite;
- borderUL = hackOptions.borderUL;
- borderU = hackOptions.borderU;
- borderUR = hackOptions.borderUR;
- borderL = hackOptions.borderL;
- borderR = hackOptions.borderR;
- borderDL = hackOptions.borderDL;
- borderD = hackOptions.borderD;
- borderDR = hackOptions.borderDR;
- borderM = hackOptions.borderM;
- }
- // Used to set or switch the Textbox Style.
- function setTextboxStyle(newStyle) {
- console.log ("SETTING TEXTBOX STYLE TO: " + newStyle);
- // If style doesn't exist, resets to default
- if (hackOptions.dialogStyle[newStyle] == undefined) {
- resetTextboxStyle();
- return false;
- }
- verticalPosition = (hackOptions.dialogStyle[newStyle].verticalPosition != undefined ? hackOptions.dialogStyle[newStyle].verticalPosition : hackOptions.verticalPosition);
- horizontalPosition = (hackOptions.dialogStyle[newStyle].horizontalPosition != undefined ? hackOptions.dialogStyle[newStyle].horizontalPosition : hackOptions.horizontalPosition);
- textboxColor = (hackOptions.dialogStyle[newStyle].textboxColor != undefined ? hackOptions.dialogStyle[newStyle].textboxColor : hackOptions.textboxColor);
- textboxWidth = (hackOptions.dialogStyle[newStyle].textboxWidth != undefined ? hackOptions.dialogStyle[newStyle].textboxWidth : hackOptions.textboxWidth);
- textboxMarginX = (hackOptions.dialogStyle[newStyle].textboxMarginX != undefined ? hackOptions.dialogStyle[newStyle].textboxMarginX : hackOptions.textboxMarginX);
- textboxMarginY = (hackOptions.dialogStyle[newStyle].textboxMarginY != undefined ? hackOptions.dialogStyle[newStyle].textboxMarginY : hackOptions.textboxMarginY);
- textColor = (hackOptions.dialogStyle[newStyle].textColor != undefined ? hackOptions.dialogStyle[newStyle].textMinLines : hackOptions.textMinLines);
- textMinLines = (hackOptions.dialogStyle[newStyle].textMinLines != undefined ? hackOptions.dialogStyle[newStyle].textMinLines : hackOptions.textMinLines);
- textMaxLines = (hackOptions.dialogStyle[newStyle].textMaxLines != undefined ? hackOptions.dialogStyle[newStyle].textMaxLines : hackOptions.textMaxLines);
- textScale = (hackOptions.dialogStyle[newStyle].textScale != undefined ? hackOptions.dialogStyle[newStyle].textScale : hackOptions.textScale);
- textSpeed = (hackOptions.dialogStyle[newStyle].textSpeed != undefined ? hackOptions.dialogStyle[newStyle].textSpeed : hackOptions.textSpeed);
- textPaddingX = (hackOptions.dialogStyle[newStyle].textPaddingX != undefined ? hackOptions.dialogStyle[newStyle].textPaddingX : hackOptions.textPaddingX);
- textPaddingY = (hackOptions.dialogStyle[newStyle].textPaddingY != undefined ? hackOptions.dialogStyle[newStyle].textPaddingY : hackOptions.textPaddingY);
- borderColor = (hackOptions.dialogStyle[newStyle].borderColor != undefined ? hackOptions.dialogStyle[newStyle].borderColor : hackOptions.borderColor);
- borderBGColor = (hackOptions.dialogStyle[newStyle].borderBGColor != undefined ? hackOptions.dialogStyle[newStyle].borderBGColor : hackOptions.borderBGColor);
- borderMidColor = (hackOptions.dialogStyle[newStyle].borderMidColor != undefined ? hackOptions.dialogStyle[newStyle].borderMidColor : hackOptions.borderMidColor);
- borderWidth = (hackOptions.dialogStyle[newStyle].borderWidth != undefined ? hackOptions.dialogStyle[newStyle].borderWidth : hackOptions.borderWidth);
- borderHeight = (hackOptions.dialogStyle[newStyle].borderHeight != undefined ? hackOptions.dialogStyle[newStyle].borderHeight : hackOptions.borderHeight);
- borderScale = (hackOptions.dialogStyle[newStyle].borderScale != undefined ? hackOptions.dialogStyle[newStyle].borderScale : hackOptions.borderScale);
- arrowColor = (hackOptions.dialogStyle[newStyle].arrowColor != undefined ? hackOptions.dialogStyle[newStyle].arrowColor : hackOptions.arrowColor);
- arrowBGColor = (hackOptions.dialogStyle[newStyle].arrowBGColor != undefined ? hackOptions.dialogStyle[newStyle].arrowBGColor : hackOptions.arrowBGColor);
- arrowScale = (hackOptions.dialogStyle[newStyle].arrowScale != undefined ? hackOptions.dialogStyle[newStyle].arrowScale : hackOptions.arrowScale);
- arrowAlign = (hackOptions.dialogStyle[newStyle].arrowAlign != undefined ? hackOptions.dialogStyle[newStyle].arrowAlign : hackOptions.arrowAlign);
- arrowInsetX = (hackOptions.dialogStyle[newStyle].arrowInsetX != undefined ? hackOptions.dialogStyle[newStyle].arrowInsetX : hackOptions.arrowInsetX);
- arrowInsetY = (hackOptions.dialogStyle[newStyle].arrowInsetY != undefined ? hackOptions.dialogStyle[newStyle].arrowInsetY : hackOptions.arrowInsetY);
- arrowWidth = (hackOptions.dialogStyle[newStyle].arrowWidth != undefined ? hackOptions.dialogStyle[newStyle].arrowWidth : hackOptions.arrowWidth);
- arrowHeight = (hackOptions.dialogStyle[newStyle].arrowHeight != undefined ? hackOptions.dialogStyle[newStyle].arrowHeight : hackOptions.arrowHeight);
- arrowSprite = (hackOptions.dialogStyle[newStyle].arrowSprite != undefined ? hackOptions.dialogStyle[newStyle].arrowSprite : hackOptions.arrowSprite);
- borderUL = (hackOptions.dialogStyle[newStyle].borderUL != undefined ? hackOptions.dialogStyle[newStyle].borderUL : hackOptions.borderUL);
- borderU = (hackOptions.dialogStyle[newStyle].borderU != undefined ? hackOptions.dialogStyle[newStyle].borderU : hackOptions.borderU);
- borderUR = (hackOptions.dialogStyle[newStyle].borderUR != undefined ? hackOptions.dialogStyle[newStyle].borderUR : hackOptions.borderUR);
- borderL = (hackOptions.dialogStyle[newStyle].borderL != undefined ? hackOptions.dialogStyle[newStyle].borderL : hackOptions.borderL);
- borderR = (hackOptions.dialogStyle[newStyle].borderR != undefined ? hackOptions.dialogStyle[newStyle].borderR : hackOptions.borderR);
- borderDL = (hackOptions.dialogStyle[newStyle].borderDL != undefined ? hackOptions.dialogStyle[newStyle].borderDL : hackOptions.borderDL);
- borderD = (hackOptions.dialogStyle[newStyle].borderD != undefined ? hackOptions.dialogStyle[newStyle].borderD : hackOptions.borderD);
- borderDR = (hackOptions.dialogStyle[newStyle].borderDR != undefined ? hackOptions.dialogStyle[newStyle].borderDR : hackOptions.borderDR);
- borderM = (hackOptions.dialogStyle[newStyle].borderM != undefined ? hackOptions.dialogStyle[newStyle].borderM : hackOptions.borderM);
- // Reset textbox.Info values? TODO: overwrite instances of textboxInfo with injects
- textboxInfo.width = textboxWidth;
- // textboxInfo.height = textboxHeight;
- textboxInfo.top = textboxMarginY;
- textboxInfo.left = textboxMarginX;
- textboxInfo.bottom = textboxMarginY;
- // textboxInfo.font_scale = 0.5; //hackOptions.textScale/4;
- // textboxInfo.arrow_height = borderHeight+textPaddingY-4;
- }
- // TODO: pass it the textbox image as a parameter?
- // TODO: Cache it at the start of the dialog, so it needn't be recalculated each frame?
- function drawTextboxBorder() {
- // Iterates through each pixel of the textbox, and determines if a border pixel should be drawn.
- // Compares 1D array of textbox pixels to 1D arrays for each border sprite, to determine whether to draw a pixel.
- for (var y = 0; y < textboxInfo.height * borderScale; y++) {
- for (var x = 0; x < (textboxInfo.width) * borderScale; x++) {
- // NOTE: borderXId and borderYId translate message box coordinates to border tile coordinates.
- // Use modulo to translate textbox space into tiles, for drawing borders pixel-by-pixel.
- // For bottom/right borders, border sprites should be anchored to the bottom/right instead.
- // borderYId and borderXId get pixel coordinates anchored to the top or left
- // borderDId and borderRId get pixel coordinates anchored to down or right (for right/bottom edges)
- var borderYId = (y % borderHeight);
- var borderXId = (x % borderWidth);
- var borderDId = ((y+(textboxInfo.height*borderScale)) % borderHeight);
- var borderRId = ((x+(textboxInfo.width*borderScale)) % borderWidth);
- // NOTE: There's a weird bug with odd vs even textbox heights. Here's a hacky fix for now.
- // TODO: Handle decimal heights? Still bugs with those (may come up with bitsy's text scaling)
- if (textboxInfo.height % 2 != 0) {
- borderDId = ((y-4+(textboxInfo.height*borderScale)) % borderHeight);
- }
- // Calculates index in the 1D array of the border sprite.
- var borderPxl = (borderYId*borderWidth) + borderXId;
- var bottomPxl = (borderDId*borderWidth) + borderXId;
- var rightPxl = (borderYId*borderWidth) + borderRId;
- var bottomRightPxl = (borderDId*borderWidth) + borderRId;
- // Determines if the current pixel is along one of the textbox's edges, or if it's in the center.
- var borderTop = y < borderHeight;
- var borderBottom = y >= textboxInfo.height*borderScale-borderHeight;
- var borderLeft = x < borderWidth;
- var borderRight = x >= textboxInfo.width*borderScale-borderWidth;
- // Does this pixel is draw???
- // 1= draw a border pixel, 0= No! Draw a border bg pixel instead
- var borderDraw;
- // Retrieve pixel data from appropriate sprite. Special handling for bottom/right borders!
- if (borderBottom) {
- // Bottom Left Corner
- if (borderLeft) { borderDraw = borderDL[bottomPxl]; }
- // Bottom Right Corner
- else if (borderRight) { borderDraw = borderDR[bottomRightPxl]; }
- // Bottom Middle Edge
- else { borderDraw = borderD[bottomPxl]; }
- }
- else if (borderTop) {
- // Top Left Corner
- if (borderLeft) { borderDraw = borderUL[borderPxl]; }
- // Top Right Corner
- else if (borderRight) { borderDraw = borderUR[rightPxl]; }
- // Top Middle Edge
- else { borderDraw = borderU[borderPxl]; }
- }
- // Left Edge
- else if (borderLeft) { borderDraw = borderL[borderPxl]; }
- // Right Edge
- else if (borderRight) { borderDraw = borderR[rightPxl]; }
- // Middle
- else { borderDraw = borderM[borderPxl]; }
- //scaling shenanigans (maps sprite scale pixels to bitsy/screen-scale pixels)
- for (var sy = 0; sy < borderScale; sy++) {
- for (var sx = 0; sx < borderScale; sx++) {
- var pxl = 4 * ( (((borderScale*y)+sy) * (4*textboxInfo.width)) + (borderScale*x)+sx );
- // If it's a border pixel, Retrieves RGBA values for the border, and draws it.
- if (borderDraw) {
- if (borderTop || borderBottom || borderLeft || borderRight) {
- textboxInfo.img.data[pxl+0] = borderColor[0];
- textboxInfo.img.data[pxl+1] = borderColor[1];
- textboxInfo.img.data[pxl+2] = borderColor[2];
- textboxInfo.img.data[pxl+3] = borderColor[3];
- }
- else {
- textboxInfo.img.data[pxl+0] = borderMidColor[0];
- textboxInfo.img.data[pxl+1] = borderMidColor[1];
- textboxInfo.img.data[pxl+2] = borderMidColor[2];
- textboxInfo.img.data[pxl+3] = borderMidColor[3];
- }
- //DEBUG COLORS. UNCOMMENT TO DRAW PIXELS BASED ON TEXTBOX COORDINATES, 0-255
- /*textboxInfo.img.data[pxl+0] = 204-(x);
- textboxInfo.img.data[pxl+1] = 204-(x+y);
- textboxInfo.img.data[pxl+2] = 204-(y*2);
- textboxInfo.img.data[pxl+3] = 255;
- //DRAWS TOP-LEFT-ANCHORED GRID TILE COORDINATES IN GREY
- if (y % borderHeight == 0 || x % borderWidth == 0) {
- textboxInfo.img.data[pxl+0] = 128;
- textboxInfo.img.data[pxl+1] = 128;
- textboxInfo.img.data[pxl+2] = 128;
- textboxInfo.img.data[pxl+3] = 255;
- }*/
- }
- // If it's a border BG pixel, gets RGBA colors based on if it's on an edge or in middle.
- else {
- if (borderTop || borderBottom || borderLeft || borderRight) {
- textboxInfo.img.data[pxl+0] = borderBGColor[0];
- textboxInfo.img.data[pxl+1] = borderBGColor[1];
- textboxInfo.img.data[pxl+2] = borderBGColor[2];
- textboxInfo.img.data[pxl+3] = borderBGColor[3];
- }
- else {
- textboxInfo.img.data[pxl+0] = textboxColor[0];
- textboxInfo.img.data[pxl+1] = textboxColor[1];
- textboxInfo.img.data[pxl+2] = textboxColor[2];
- textboxInfo.img.data[pxl+3] = textboxColor[3];
- }
- //DEBUG COLORS. UNCOMMENT TO DRAW PIXELS BASED ON TEXTBOX COORDINATES, 0-255
- /*textboxInfo.img.data[pxl+0] = (x)+102;
- textboxInfo.img.data[pxl+1] = (x+y);
- textboxInfo.img.data[pxl+2] = (y*2)+51;
- textboxInfo.img.data[pxl+3] = 255;
- //DRAWS TOP-LEFT-ANCHORED GRID COORDINATES IN GREY
- if (y % borderHeight == 0 || x % borderWidth == 0) {
- textboxInfo.img.data[pxl+0] = 76;
- textboxInfo.img.data[pxl+1] = 76;
- textboxInfo.img.data[pxl+2] = 76;
- textboxInfo.img.data[pxl+3] = 255;
- }*/
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment