Javascript Simple Text Input Script

1726755077302.png

Introduction
An extremely simple Text to Input script. This is primarily made as an experiment, but hey it works. Please note that you cannot input other languages using this plugin. I only made this just to test something out. If you need a simple input prompt, you can use this.

Tutorial
  • Make a new Javascript file and insert the code. It's very easy to adjust.
  • Call the script using showTextInput() or showTextInput(variableID)
Plugin Code
Spoiler
JavaScript:
class TextInputDisplay {
    constructor() {
        this.inputText = "";
        this.bitmap = null;
        this.sprite = null;
        this.keydownHandler = null;
        this.targetVariableId = 0;
    }

    static show(variableId = 1) {
        if (!TextInputDisplay.instance) {
            TextInputDisplay.instance = new TextInputDisplay();
        }
        TextInputDisplay.instance.targetVariableId = variableId - 1;
        TextInputDisplay.instance.initialize();
    }

    initialize() {
        this.createVisuals();
        this.setupKeyListener();
    }

    createVisuals() {
        this.bitmap = new gs.Bitmap(400, 50);
        this.bitmap.font.name = "Arial";
        this.bitmap.font.size = 32;
        this.bitmap.font.border = true;
        this.bitmap.font.italic = false;
        this.bitmap.drawText(0, 0, this.bitmap.width, this.bitmap.height, "Enter your name:");

        this.sprite = new gs.Sprite(Graphics.viewport);
        this.sprite.bitmap = this.bitmap;
        this.sprite.x = (Graphics.width - this.bitmap.width) / 2;
        this.sprite.y = (Graphics.height - this.bitmap.height) / 2;
        this.sprite.srcRect = new gs.Rect(0, 0, this.bitmap.width, this.bitmap.height);
    }

    setupKeyListener() {
        this.keydownHandler = (event) => {
            const keyName = event.key;
            if (keyName === "Backspace") {
                this.inputText = this.inputText.slice(0, -1);
            } else if (keyName === "Enter") {
                this.storeInput();
                return;
            } else if (keyName.length === 1) {
                this.inputText += keyName;
            }
            this.updateText(`Enter your name: ${this.inputText}`);
        };
        document.addEventListener('keydown', this.keydownHandler);
    }

    storeInput() {
        GameManager.variableStore.strings[this.targetVariableId] = this.inputText;
        console.log(`Stored input in Variable ${this.targetVariableId + 1}:`, this.inputText);
        this.hide();
    }

    updateText(newText) {
        if (this.bitmap) {
            this.bitmap.clear();
            this.bitmap.drawText(0, 0, this.bitmap.width, this.bitmap.height, newText);
        }
    }

    hide() {
        if (this.sprite) {
            this.sprite.visible = false;
            this.sprite.dispose();
            this.sprite = null;
        }
        if (this.bitmap) {
            this.bitmap.dispose();
            this.bitmap = null;
        }
        if (this.keydownHandler) {
            document.removeEventListener('keydown', this.keydownHandler);
            this.keydownHandler = null;
        }
        TextInputDisplay.instance = null;
    }

    static hide() {
        if (TextInputDisplay.instance) {
            TextInputDisplay.instance.hide();
        }
    }

    static getInputText() {
        return TextInputDisplay.instance ? TextInputDisplay.instance.inputText : "";
    }
}

// Global functions for script calls
window.showTextInput = (variableId) => TextInputDisplay.show(variableId);
window.hideTextInput = TextInputDisplay.hide;
window.getTextInput = TextInputDisplay.getInputText;

Example Scene Command
Just copy paste this to a scene.
Spoiler
JSON:
[
    {
        "id": "vn.UIAccess",
        "params": {
            "saveMenu": 0,
            "loadMenu": 0,
            "generalMenu": 0,
            "backlog": 0,
            "fieldFlags": {
                "saveMenu": 0,
                "loadMenu": 0,
                "generalMenu": 0,
                "backlog": 0
            }
        },
        "uid": "354C74AC1E55B446010944C823C76F32C5F1",
        "indent": 0
    },
    {
        "id": "gs.Script",
        "params": {
            "script": "// Show the text input and store the result in Global Variable 1\nshowTextInput(1);"
        },
        "indent": 0,
        "uid": "95BABDD9097288418369C567FF149263925E",
        "expanded": true
    },
    {
        "id": "gs.AddHotspot",
        "params": {
            "numberDomain": "com.degica.vnm.default",
            "number": 0,
            "shape": "pixel",
            "positionType": 0,
            "actions": {
                "onClick": {
                    "type": 0,
                    "bindValue": 0,
                    "bindValueVariable": {
                        "scope": 0,
                        "index": 0
                    },
                    "commonEventId": 0,
                    "label": "1",
                    "switch": {
                        "scope": 1,
                        "index": 0
                    },
                    "scene": null
                },
                "onEnter": {
                    "type": 0,
                    "bindValue": 0,
                    "bindValueVariable": {
                        "scope": 0,
                        "index": 0
                    },
                    "commonEventId": 0,
                    "label": "1",
                    "switch": {
                        "scope": 1,
                        "index": 0
                    },
                    "scene": null
                },
                "onSelect": {
                    "type": 0,
                    "bindValue": 0,
                    "bindValueVariable": {
                        "scope": 0,
                        "index": 0
                    },
                    "commonEventId": 0,
                    "label": "",
                    "switch": {
                        "scope": 1,
                        "index": 0
                    },
                    "scene": null
                },
                "onDeselect": {
                    "type": 0,
                    "bindValue": 0,
                    "bindValueVariable": {
                        "scope": 0,
                        "index": 0
                    },
                    "commonEventId": 0,
                    "label": "",
                    "switch": {
                        "scope": 1,
                        "index": 0
                    },
                    "scene": null
                },
                "onLeave": {
                    "type": 0,
                    "bindValue": 0,
                    "bindValueVariable": {
                        "scope": 0,
                        "index": 0
                    },
                    "commonEventId": 0,
                    "label": "",
                    "switch": {
                        "scope": 1,
                        "index": 0
                    },
                    "scene": null
                },
                "onDrag": {
                    "type": 0,
                    "bindValue": 0,
                    "bindValueVariable": {
                        "scope": 0,
                        "index": 0
                    },
                    "commonEventId": 0,
                    "label": "",
                    "switch": {
                        "scope": 1,
                        "index": 0
                    },
                    "scene": null
                },
                "onDrop": {
                    "type": 0,
                    "bindValue": 0,
                    "bindValueVariable": {
                        "scope": 0,
                        "index": 0
                    },
                    "commonEventId": 0,
                    "label": "",
                    "switch": {
                        "scope": 1,
                        "index": 0
                    },
                    "scene": null
                },
                "onDropReceive": {
                    "type": 0,
                    "bindValue": 0,
                    "bindValueVariable": {
                        "scope": 0,
                        "index": 0
                    },
                    "commonEventId": 0,
                    "label": "",
                    "switch": {
                        "scope": 1,
                        "index": 0
                    },
                    "scene": null
                }
            },
            "dragging": {
                "enabled": 0,
                "variable": {
                    "scope": 1,
                    "index": 0,
                    "domain": "com.degica.vnm.default"
                },
                "horizontal": 1,
                "vertical": 0,
                "rect": {
                    "x": 0,
                    "y": 0,
                    "size": {
                        "width": 200,
                        "height": 50
                    }
                }
            },
            "position": 1,
            "box": {
                "x": 869,
                "y": 540,
                "angle": 0,
                "zoom": 1,
                "data": {},
                "color": {
                    "red": 0,
                    "green": 0,
                    "blue": 0,
                    "alpha": 160
                },
                "size": {
                    "width": 300,
                    "height": 100
                }
            },
            "previewBackground": {
                "name": "$live_preview_snapshot"
            },
            "baseGraphic": {
                "name": "diamond-large-off.png",
                "folderPath": "Graphics/Pictures/UI",
                "hue": 0,
                "opacity": 255,
                "blending": 0,
                "tone": {
                    "red": 0,
                    "green": 0,
                    "blue": 0,
                    "grey": 0
                }
            }
        },
        "indent": 0,
        "uid": "BD13950519839549068A5C860A11FDA181E9",
        "expanded": false
    },
    {
        "id": "gs.Idle",
        "params": {},
        "indent": 0,
        "uid": "DEE1B47527135243DE2BEC90CB140AA6D2CF"
    },
    {
        "id": "gs.Label",
        "params": {
            "name": "1"
        },
        "indent": 0,
        "uid": "D33F4A985E283040885B2B18AD8924B66B13"
    },
    {
        "id": "gs.EraseHotspot",
        "params": {
            "numberDomain": "com.degica.vnm.default",
            "number": 0
        },
        "indent": 0,
        "uid": "FBAC9D21078527400C4BA4577590FA2493E1"
    },
    {
        "id": "gs.ShowMessage",
        "params": {
            "waitForCompletion": 1,
            "duration": 15,
            "expressionId": null,
            "custom": {
                "object": {
                    "x": 0,
                    "y": 0,
                    "size": {
                        "width": 300,
                        "height": 100
                    }
                }
            },
            "message": {
                "lcId": null,
                "defaultText": "Your name is... \nGlobal: {GT:1}\nPersistent: {PT:1}\nLocal: {LT:1}\nList: {PL:1,0} "
            },
            "position": 0,
            "characterId": 0,
            "partial": 0,
            "fieldFlags": {
                "duration": 1
            },
            "expressions": [],
            "animations": []
        },
        "indent": 0,
        "uid": "333188D280E4974BCC48ECB22FB9E856A601",
        "expanded": true
    }
].novelsharp/vn-scene-command

Original thread: https://forums.rpgmakerweb.com/threads/simple-text-input-script.172003/