Player Input in ShowText Window Plugin [RMMZ]

Player Input Text MZ
playerInputText.js

◆ Plugin Overview
This RPG Maker MZ plugin allows players to input their own text into a Show Text window, styled like NPC dialogue.
It offers customizable options including actor image, name, font size, input text color, and placeholder settings.

The plugin enhances interactive storytelling by enabling players to actively participate in conversations and scenarios. It's designed to be flexible and easy to integrate into various game events, making it a versatile tool for RPG creators seeking to add personalized player interaction in their games.

Screenshot 2024-01-16 at 12.06.34.png


◆ DEMO


◆ Plugin Commands
▶ inputText


◆ Configurable Arguments
▶ Actor Image
▶ Actor Name
▶ Text Font Size
▶ Text Color
▶ Placeholder text
▶ Placeholder Text Color

◆ Download Plugin code

Spoiler
JavaScript:
/*:
 * @target MZ
 * @plugindesc [RPG Maker MZ] [Version 1.1.1] [Gamer Tools Studio]
 * @author Gamer Tool Studio
 *
 * @command PlayerInput
 * @text Player Input
 * @desc Allows the player to input their own message.
 *
 * @arg actorName
 * @text Actor Name
 * @desc The name of the actor. Default is main player name.
 * @type text
 * @default
 *
 * @arg actorFaceImage
 * @text Actor Face Image
 * @desc The face image of the actor. Default is main player image.
 * @type file
 * @dir img/faces/
 * @default
 *
 * @arg actorFaceImageIndex
 * @text Actor Face Image Index
 * @desc The face image index of the actor. Default is main player face index.
 * @type number
 * @default 0
 *
 * @arg placeholderText
 * @text Placeholder Text
 * @desc The placeholder text. Default is "Enter your message...".
 * @type text
 * @default Enter your message...
 *
 * @arg inputVariable
 * @text Input Variable
 * @desc The variable to store the input text. Default is variable 19.
 * @type variable
 * @default 19
 *
 * @help PlayerInputText.js
 *
 * Allows players to input text in a Show Text window with the style of the game window skins and store it in a custom variable.
 * Use the plugin command "Player Input" in an event to trigger this feature.
 */

(() => {
    const pluginName = "PlayerInputText";

    PluginManager.registerCommand(pluginName, "PlayerInput", args => {
        const actorName = args.actorName || $gameParty.leader().name();
        const actorFaceImage = args.actorFaceImage || $gameParty.leader().faceName();
        const actorFaceImageIndex = parseInt(args.actorFaceImageIndex, 10) || $gameParty.leader().faceIndex();
        const placeholderText = args.placeholderText ? args.placeholderText : 'Enter your message...';

        // Set up the game message with speaker name and face image, but no text.
        $gameMessage.setFaceImage(actorFaceImage, actorFaceImageIndex);
        $gameMessage.setSpeakerName(actorName);
        $gameMessage.add(placeholderText);

        // Ensure the message window activates input mode once it's open.
        const scene = SceneManager._scene;
        if (scene instanceof Scene_Map) {
            const messageWindow = scene._messageWindow;
            if (messageWindow) {
                messageWindow.prepareInputWindow(args);
                messageWindow.activateInput();
            }
        }
    });

    // Prepare Input
    Window_Message.prototype.prepareInputWindow = function(args) {
        this._inputArgs = args;
        this._inputVariable = parseInt(args.inputVariable, 10) || 19;
        this._inputLines = [''];

        // Set speaker name to actorName each time before displaying the input window.
        const actorName = args.actorName || $gameParty.leader().name();
        $gameMessage.setSpeakerName(actorName);

        this.activateInput();
        this.open();
        this.setPositionType();
        this.refreshInputWindow(); // This will now reflect the updated speakerName
    };

    Window_Message.prototype.setPositionType = function() {
        // Position type: 0 (top), 1 (middle), 2 (bottom)
        const positionType = 2; // Force to bottom for input
        this.y = this.calculateY(positionType);
    };

    Window_Message.prototype.calculateY = function(positionType) {
        const messageY = {
            0: 0, // Top
            1: (Graphics.boxHeight - this.height) / 2, // Middle
            2: Graphics.boxHeight - this.height // Bottom
        };
        return messageY[positionType];
    };

     // Activate Input
    Window_Message.prototype.activateInput = function() {
        if (this._inputActive) return; // Prevent multiple activations

        this._originalKeyMapper = Object.assign({}, Input.keyMapper);
        this._overrideKeyMapperForTextInput();

        this._inputActive = true;
        this._boundHandleInput = this.handleInput.bind(this);
        document.addEventListener('keydown', this._boundHandleInput);

        this._lastInputTime = 0; // Debounce setup
        this.refreshInputWindow();
    };

    // Deactivate Input
    Window_Message.prototype.deactivateInput = function() {
        document.removeEventListener('keydown', this._boundHandleInput);
        Input.keyMapper = this._originalKeyMapper;
        this._inputActive = false;
    };

    // Temporarily override key mappings for special keys.
    Window_Message.prototype._overrideKeyMapperForTextInput = function() {
        Input.keyMapper[32] = 'space'; 
        Input.keyMapper[90] = 'z';     
        Input.keyMapper[88] = 'x';
        Input.keyMapper[87] = 'w';
    };

    // Handle Input
    Window_Message.prototype.handleInput = function(event) {
        if (!this._inputActive || !this.isOpen()) return;

        const currentLineIndex = this._inputLines.length - 1;
        let currentLine = this._inputLines[currentLineIndex];

        if (event.key === 'Enter') {
            this.processInput();
            event.preventDefault();
        } else if (event.key === 'Backspace') {
            if (currentLine.length > 0) {
                this._inputLines[currentLineIndex] = currentLine.slice(0, -1);
            } else if (this._inputLines.length > 1) {
                this._inputLines.pop();
            }
            this.refreshInputWindow();
        } else if (event.key.length === 1) {
            if (currentLine.length < 40) {
                this._inputLines[currentLineIndex] += event.key;
            } else if (this._inputLines.length < 4) {
                this._inputLines.push(event.key);
            }
            this.refreshInputWindow();
        }
    };

    // Process Input
    Window_Message.prototype.processInput = function() {
        const inputText = this._inputLines.join('\n');
        $gameVariables.setValue(this._inputVariable, inputText);
        this.deactivateInput();
        this.close();

        // Reset the speakerName after processing the input.
        $gameMessage.setSpeakerName('');
    };

    // Refresh Input Window
    Window_Message.prototype.refreshInputWindow = function() {
        if (!this._inputLines) {
            return; // Guard clause
        }
        
        // Face Image and text input positioning
        const faceImageWidth = 144;
        const textMargin = 15;
        const textStartX = faceImageWidth + textMargin;


        // This sets the text to start at the very top of the window.
        const textStartY = 0;


        // This ensures the actor's face image remains on the screen.
        this.contents.clearRect(textStartX, textStartY, this.contents.width - textStartX, this.contents.height);

        this.resetFontSettings();

        const args = this._inputArgs;

        
        const textToShow = this._inputLines.join('').trim() !== '' ? this._inputLines.join('\n') : "Enter your message...";

        // Draw the text at the calculated position.
        this.drawTextEx(textToShow, textStartX, textStartY);
    };

    // Clear contents
    Window_Message.prototype.clear = function() {
        this.contents.clear(); // Clears the window's drawing contents
        this._textState = null; // Reset text state
    };
})();

◆ Terms of Use
This plugin is released under MIT license.
You can download it by free, make a copy, modify and redistribute it as you want. (However, you should not delete the copyright claim and the MIT license clause written inside the plugin)
❤ Would be great if you put "Gamer Tool Studio " on the credits 🙂
This plugin should meet my needs.

But I encountered a problem while using it. After starting the game, a blank text box will appear above the game interface. When touching an event, a text input window will not appear.


屏幕截图 2024-01-22 165101.png
Sorry to hear about that @beiduo.
I also found ran into that issue and the problem seems to be related to the way i'm dealing with the "Esc" key while using the plugin command.

I'm fixing it right now and i'll update the code here very soon.
@beiduo i just updated the file with a patch to fix the bug you reported. (MAKE SURE YOU'RE USING VERSION 1.0.2)

The code still has some issues in the way some of the arguments are being handled but it should work flawlessly if you're using the default styles of RMMZ.

Let me know how it goes
Hello! Can I ask where those text are stored? I mean, can I store it somewhere so the game can check it later in game lookingfor your previous answer for example?
Hello! Can I ask where those text are stored? I mean, can I store it somewhere so the game can check it later in game lookingfor your previous answer for example?
Yes, text inputs are stored by default in variable nr 19 of the game by default.
You can change to whatever variable you want in the plugin command
Yes, text inputs are stored by default in variable nr 19 of the game by default.
You can change to whatever variable you want in the plugin command
I see, can I change it during game, so I can store more than one (in more than one variable) text for example? that would be perfect.
Sure. You can set a new value for the variable to store the input as many times as you wish.
You just need to create a new even or event page and host the plugin command, this time with the new variable value
Fantastic, thank you so much.
This is awesome! I already found and integrated a player input plugin for my Inworld plugin, but I'm gonna make it compatible with this too.
Sorry to hear about that @beiduo.
I also found ran into that issue and the problem seems to be related to the way i'm dealing with the "Esc" key while using the plugin command.

I'm fixing it right now and i'll update the code here very soon.

It still doesn't work!

00.jpg
Can you share your event setup?
Are there no more logs in the console?
Are you using other plugins affecting teneral styles? If so, place the one under all of them
Can you share your event setup?
Are there no more logs in the console?
Are you using other plugins affecting teneral styles? If so, place the one under all of them

No other plugins were used.

There are no more logs.

000.jpg
And is the event condition set to action button or player touch? or is it parallel or autorun?
Actually can't really debug much more from here and that's a weird behavior cause in your logs the plugin is triggered but does nothing.
And is the event condition set to action button or player touch? or is it parallel or autorun?
Actually can't really debug much more from here and that's a weird behavior cause in your logs the plugin is triggered but does nothing.
I tried it on MZ 1.5 and 1.8 respectively, and it was the same.
Does anyone else have this issue?
Can you provide a DEMO?
I tried it on MZ 1.5 and 1.8 respectively, and it was the same.
Does anyone else have this issue?
Can you provide a DEMO?

That's weird. I just created a new empty project with the plugin and its working fine.

You can download the project folder and try it out for yourself on your end:


Let me know how it goes
I've tried it, sometimes it works and sometimes it doesn't work, whether using the OK key or the mouse. If you use the OK key , the game may not be able to play.

text.gif
@beiduo,

I've updated the plugin code to VERSION 1.1.1

I changed the approach and it should work fine in any scenarion or game flow now.

Please lmk if it worked fine for you
Your work is very admirable, but unfortunately, there was no response this time.

ttt.gif
@beiduo, you mean, the window didn't even pop up?

Can you confirm what are the special keys in your keyboard (the ones used for action button and cancel)?

Original thread: https://forums.rpgmakerweb.com/threads/player-input-in-showtext-window-plugin-rmmz.164839/