//=============================================================================
// InputForm.js
// PUBLIC DOMAIN
// ----------------------------------------------------------------------------
// 2017/09/03 Fixed a bug that prevented you from pressing the "Confirm" button on iOS & disabled clicking on the back game screen
// 2018/12/06 Input field size is adjusted to fit screen size & Fixed bug of screen misalignment on iPhone & Font size setting & Default value setting
// 2020/08/22 Compatible with RPG Maker MZ
// Raw js code found at https://raw.githubusercontent.com/krmbn0576/rpgmakermv/master/js/plugins/111_InputForm.js
// Raw css code found at
https://raw.githubusercontent.com/krmbn0576/rpgmakermv/master/css/111_InputForm.css
// 2024/05/21 Added an argument to assign 'type number' to inputform & added a second argument to assign a specific css file (111_InputForm.css remains the default)
//=============================================================================
/*:
* @target MZ
* @plugindesc Create a form and enter text (revised and modified version)
* @author 111, Kuramubon, Lady JJ
*
*
* @help
* Display the character input form and let the user enter characters.
* Unlike the event command "Name input processing",
* You can use familiar keyboard input and flick input.
* You can also enter any characters you want, including kanji.
*
* ---Preparation---
* Put 111_InputForm.css in the css folder in the game folder (create it if you don't have it).
* By the way, you can change the design, width, etc. of the input form by modifying this file.
*If you don't know how to do it, just search for "how to write css" or something similar!
*
* ---How to use with RPG Maker MZ---
* You can display the input field using the plugin command.
* At a minimum, adjust the display position with "X position of input field" and "Y position of input field".
* It works if you set a variable in "Substitution destination of input result".
* Please adjust other parameters if necessary.
*
* ---How to use in RPG Maker MV---
* ◆Plugin command: InputForm x=350;y=200;v=11;max=5;
* Something like that. In this example, it will be displayed at x350,y200 and the result will be saved in variable number 11.
* The maximum number of characters is 5 (you can omit max to make it unlimited).
*
* If you want to create a timeout, add if_s=3;
* You can force quit “if switch 3 is turned on”
* Let's create an event to turn on switch 3 in the parallel events
* (Hammering point 1) In this case, at the moment of forced termination,
* Note that the text is stored securely in the result variable.
*
* The next event command will not be read until input is complete
* (Stuck point 2) Until the next event command is loaded
* Be careful not to overwrite the resulting variable with other parallel processing as there is a short delay.
*
*
* Function addition:
* Inputform (omitted) btn_x=100;btn_y=100;
* The position of the "Confirm" button can be finely adjusted by writing it as *.
* Value is relative to the text box, default is btn_x=0;btn_y=50;
*
* (Added 2018/12/06)
* The scale of the input field and OK button now expands and contracts to match the screen scale.
*
* Inputform (omitted) font_size=30;
* You can change the font size of the input field and decision button.
* If font_size is not specified, font_size=24.
*
* Inputform (omitted) placeholder=text;
* You can display the contents of the "text" in the input field from the beginning.
* Please use this when setting a default name.
* If you specify placeholder=$;, the contents of variable v will be displayed.
*
*License:
* There are no restrictions on how you use this plugin. Feel free to use it however you like.
*
* @command show
* @text Processing character input
* @desc
*
* @arg target_x
* @type number
* @text X position of the input field
*
* @arg target_y
* @type number
* @text Y position of the input field
*
* @arg variables_id
* @type variable
* @text Variable ID to store input result
*
* @arg max_count
* @type number
* @text Maximum number of characters
*
* @arg if_switch_id
* @type switch
* @text ON Forcibly terminate input at
*
* @arg button_x
* @type number
* @min -10000
* @default 0
* @text Relative X position of the enter button
*
* @arg button_y
* @type number
* @min -10000
* @default 50
* @text Relative Y position of the enter button
*
* @arg unit_font_size
* @type number
* @default 24
* @text Font size
*
* @arg placeholder
* @type string
* @text Initial value of input field
*
*@arg attr_wintype
*@type number
*@default 0
*@text Type of Window (0 = text, 1 = number)
*
*@arg css_file
*@type string
*@default 111_InputForm
*@text Name of css file to use (default is 111_InputForm)
*/
(function() {
function stopPropagation(event) {
event.stopPropagation();
}
//Add css function moved to Game_Interpreter.prototype._inputForm
// To disable key input
Input.form_mode = false;
var _Input_onKeyDown = Input._onKeyDown;
Input._onKeyDown = function(event) {
if(Input.form_mode)return;
_Input_onKeyDown.call(this , event)
};
var _Input_onKeyUp = Input._onKeyUp;
Input._onKeyUp = function(event) {
if(Input.form_mode)return;
_Input_onKeyUp.call(this , event)
};
// Do not load the next event command until the input is complete
var _Game_Interpreter_updateWaitMode =
Game_Interpreter.prototype.updateWaitMode;
Game_Interpreter.prototype.updateWaitMode = function(){
if(this._waitMode == 'input_form')return true;
return _Game_Interpreter_updateWaitMode.call(this);
}
HTMLElement.prototype.postionAdjust = function(screen_postion , target_postion, unitFontSize){
this.style.left = screen_postion[0] + target_postion[0] * Graphics._realScale + "px";
this.style.top = screen_postion[1] + target_postion[1] * Graphics._realScale + "px";
this.style.fontSize = unitFontSize * Graphics._realScale + "px";
this.style.maxWidth = 'calc(100% - ' + this.style.left + ')';
this.style.maxHeight = 'calc(100% - ' + this.style.top + ')';
};
// Enable arguments like x=350;y=200;
var argHash = function(text , arg_names){
var _args = new Array(arg_names.length);
var ary = text.split(";");
ary.forEach(function(str){
var s_ary = str.split("=");
var prop = s_ary[0].toLowerCase();
var value = s_ary[1];
_args[arg_names.indexOf(prop)] = value;
});
return _args;
}
//=============================================================================
// Game_Interpreter - register plugin commands
//=============================================================================
var _Game_Interpreter_pluginCommand =
Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
_Game_Interpreter_pluginCommand.call(this, command, args);
if (command === 'InputForm'){
var _ary = argHash(args[0] , ["x" , "y" , "v" , "max" , "if_s", "btn_x", "btn_y", "font_size", "placeholder","attr_wintype", "css_file"]);
var target_x = +_ary[0];
var target_y = +_year[1];
var variables_id = +_ary[2];
var max_count = _ary[3] || null;
var if_switch_id = Number(_ary[4]) || null;
var button_x = +_ary[5] || 0;
var button_y = _ary[6] === '' || isNaN(_and[6]) ? 50 : +_and[6];
var unit_font_size = _ary[7] === '' || isNaN(_and[7]) ? 24 : +_and[7];
var placeholder = _ary[8];
var attr_wintype = ary[9];
var css_file = ary[10];
this._inputForm(target_x, target_y, variables_id, max_count, if_switch_id, button_x, button_y, unit_font_size, placeholder, attr_wintype, css_file);
}
};
if (PluginManager.registerCommand) {
PluginManager.registerCommand("111_InputForm2", "show", function(args) {
var { target_x, target_y, variables_id, max_count, if_switch_id, button_x, button_y, unit_font_size, placeholder, attr_wintype, css_file } = args;
this._inputForm(+target_x, +target_y, +variables_id, +max_count, +if_switch_id, +button_x, +button_y, +unit_font_size, placeholder, attr_wintype, css_file);
});
}
Game_Interpreter.prototype._inputForm = function(target_x, target_y, variables_id, max_count, if_switch_id, button_x, button_y, unit_font_size, placeholder, attr_wintype, css_file) {
var interpreter = this;
var gui = {
input : null ,
submit : null ,
is_pc : true ,
init : function(){
this.is_pc = Utils.isNwjs();
this.create();
this.input.focus();
this.screenAdjust();
} ,
create : function(){
// input form
this.input = document.createElement('input');
this.input.setAttribute('id', '_111_input');
// Autocomplete (Default = 'on', set of 'off' if desired)
this.input.autocomplete = 'off';// change to 'on' if desired
// Type of window (0 = text, 1 = number)
if (attr_wintype == 1) {this.input.setAttribute('type','number')};
if(max_count)this.input.setAttribute('maxlength', max_count);
if (placeholder === '$') {
placeholder = $gameVariables.value(variables_id);
}
this.input.setAttribute('value', placeholder || '');
document.body.appendChild(this.input);
// Link css file
var cssFile = css_file;
var css = document.createElement('link');
css.rel = "stylesheet";
css.type = 'text/css';
css.href = './css/'+css_file+'.css';
var b_top = document.getElementsByTagName('head')[0];
b_top.appendChild(css);
// send button
this.submit = document.createElement('input');
this.submit.setAttribute('type', 'submit');
this.submit.setAttribute('id', '_111_submit');
this.submit.setAttribute('value', 'Submit');
document.body.appendChild(this.submit);
} ,
success : function(){
$gameVariables.setValue(variables_id , this.input.value);
this.end();
} ,
cancel : function(){
$gameVariables.setValue(variables_id , this.input.value);
this.end();
} ,
start : function(){
interpreter.setWaitMode('input_form');
Input.clear();
Input.form_mode = true;
// SceneManager._scene.stop();
} ,
end : function(){
this.input.remove(); // document.body.removeChild(this.input);
this.submit.remove();
window.removeEventListener("resize", resizeEvent, false);
interpreter.setWaitMode('');
Input.form_mode = false;
clearInterval(_event);
// SceneManager._scene.start();
} ,
screenAdjust : function(){ // Adjust to the position based on the top left of the canvas
var screen_x , screen_y;
var _canvas = document.getElementById('UpperCanvas') || document.getElementById('gameCanvas');
var rect = _canvas.getBoundingClientRect();
screen_x = rect.left;
screen_y = rect.top;
this.input.postionAdjust([screen_x,screen_y] , [target_x,target_y], unit_font_size);
this.submit.postionAdjust([screen_x,screen_y] , [target_x + button_x,target_y + button_y], unit_font_size);
}
}
//
gui.init();
// Event to send gui.input.onkeydown = function(e){
gui.input.addEventListener("keydown" ,function(e){
if(e.keyCode === 13){ // Send with decision key
Input.clear();
gui.success();
// Stop event propagation to parent (so that keydown of document does not react)
e.stopPropagation();
}
});
gui.input.addEventListener("mousedown", stopPropagation); // Prevent clicks on the back game screen
gui.input.addEventListener("touchstart", stopPropagation); // Measures for not being able to receive click events on iOS
gui.submit.addEventListener("mousedown", stopPropagation); // Prevent clicks on the back game screen
gui.submit.addEventListener("touchstart", stopPropagation); // Measures for not being able to receive click events on iOS
gui.submit.addEventListener("click" ,function(){ // Submit button click
gui.success();
return false;
});
// event to cancel
if (if_switch_id) {
var _event = setInterval(function(){
if($gameSwitches.value(if_switch_id)){
// clearInterval(_event);
gui.cancel();
}
}, 1);
}
// On the web, there is an event that recalculates the % every time the window size changes.
//if(! gui.is_pc){
var resizeEvent = gui.screenAdjust.bind(gui);
window.addEventListener("resize", resizeEvent, false);
//}
//
gui.start();
};
})();