//=============================================================================
// CodeInput.js
//=============================================================================
/*:
* @plugindesc Provides answer keyboard input
* @author Poring
*
* @param Min width
* @desc Minimal width for the Input Text Field
* @default 128
*
* @param Text Color
* @desc Color for the answer text
* @default #999999
*
* @param Placeholder
* @desc Text displayed in case of empty answer
* @default Your answer?
*
* @param Placeholder Color
* @desc Color for the placeholder
* @default #999999
*
* @help
*
* Plugin Command:
* CodeInput open "My Answer" question_img # Show the input field with image 'question_img' and set correct answer
* CodeInput hide # Hide the input field
*
* Result:
* - You can get Player input from $gameTemp.questionInputAnswer
* - You can check if Player answered correct by check of the $gameTemp.isPlayerAnswered
* if it's 'true', player passed the question, in other case the input was closed without answer or with
* incorrect answer
*/
/*:ru
* @plugindesc Отображает поле ввода ответа с клавиатуры
* @author Поринг Попорингович
*
* @param Минимальная ширина
* @desc Минимальная ширина поля ввода
* @default 128
*
* @param Цвет текста
* @desc Цвет текста ответа
* @default #ffffff
*
* @param Плейсхолдер
* @desc Текст, отображаемый в случае, когда ответ пуст
* @default Ваш ответ?
*
* @param Цвет плейсхолдера
* @desc Цвет для отображения плейсхолдера
* @default #999999
*
* @help
*
* Plugin Command:
* CodeInput open "Ответ" question_img # Показать поле ввода с картинкой 'question_img' и задать верный ответ "Ответ"
* CodeInput hide # Скрыть поле ввода
*
* Result:
* - Что ввел пользователь, можно узнать из $gameTemp.questionInputAnswer
* - Узнать, ответил ли Игрок верно, можно из $gameTemp.isPlayerAnswered
* Если значение 'true' - игрок дал верный ответ, в ином случае, он либо отменил ввод, либо дал
* неверный ответ
*/
(function() {
const ANSWER_WAIT_MODE = 'answer_sentence';
const KEY_CODE_ENTER = 13;
const KEY_CODE_ESCAPE = 27;
const KEY_CODE_BACKSPACE = 8;
const KEY_CODE_SPACE = 32;
var parameters = PluginManager.parameters('CodeInput');
const MIN_INPUT_FIELD_WIDTH = parameters['Min input field width'];
const TEXT_COLOR = parameters['Text Color'];
const PLACEHOLDER_COLOR = parameters['Placeholder Color'];
const PLACEHOLDER_TEXT = parameters['Placeholder'];
var correctAnswer = null;
var sceneCenterX = 0;
// Game_Interpreter
Game_Interpreter.prototype.command356 = function() {
var params = this._params[0];
params = params.replace(/((\"[\w\dа-яА-Я\s]+\")|([^\s][\w\dа-яА-Я]+[\s$]))/g, '$1,');
params = params.replace(/[\s\n\"]*\,[\s\n\"]*/g, ',');
var args = params.split(',');
var command = args.shift();
this.pluginCommand(command, args);
return true;
};
var _Game_Interpreter_updateWaitMode = Game_Interpreter.prototype.updateWaitMode;
Game_Interpreter.prototype.updateWaitMode = function() {
var waiting = false;
switch (this._waitMode) {
case ANSWER_WAIT_MODE:
waiting = Input._isDirectInput();
break;
default:
break;
}
return waiting || _Game_Interpreter_updateWaitMode();
};
// 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 === 'CodeInput') {
switch (args[0]) {
case 'open':
if (args[1] != undefined && args[1] != null) {
Input._lastChars = "";
Input._lastPressedKeyCode = null;
$gameTemp.isPlayerAnswered = false;
correctAnswer = args[1];
var imageFilename = (args[2] != undefined) ? args[2] : null;
if (SceneManager._scene.constructor === Scene_Map) {
var sceneMap = SceneManager._scene;
sceneMap.showAnswerInput(imageFilename);
}
}
break;
case 'close':
if (SceneManager._scene.constructor === Scene_Map) {
var sceneMap = SceneManager._scene;
sceneMap._answerInputWindow.hide();
}
break;
}
}
};
// Input
Input.keyMapper[8] = 'backspace';
Input._isDirectInput = function() {
var scene = SceneManager._scene;
var isSceneMapActive = scene.constructor === Scene_Map;
if (!isSceneMapActive) {
return false;
}
var isAnswerWindowExist = scene._answerInputWindow != undefined && scene._answerInputWindow != null;
var isAnswerWindowVisible = scene._answerInputWindow.visible;
return isAnswerWindowExist && isAnswerWindowVisible;
}
Input._shouldIgnoreSymbolKey = function(keyCode) {
var isDirectInput = this._isDirectInput();
var isKeyInShortList = (keyCode == KEY_CODE_ENTER || keyCode == KEY_CODE_SPACE ||
keyCode == KEY_CODE_BACKSPACE || keyCode == KEY_CODE_ESCAPE);
return isDirectInput && !isKeyInShortList;
};
var _Input_setupEventHandlers = Input._setupEventHandlers;
Input._setupEventHandlers = function () {
_Input_setupEventHandlers.call(this);
document.addEventListener('keypress', this._onKeyPress.bind(this));
};
var _Input_shouldPreventDefault = Input._shouldPreventDefault;
Input._shouldPreventDefault = function(keyCode) {
if (!this.isDirectInput) {
return _Input_shouldPreventDefault(keyCode);
}
return false;
}
var _Input_onKeyDown = Input._onKeyDown;
Input._onKeyDown = function(event) {
if (!this._shouldIgnoreSymbolKey(event.keyCode)) {
_Input_onKeyDown.call(this, event);
this._lastPressedKeyCode = event.keyCode;
}
};
Input._onKeyPress = function(event) {
if (event.keyCode == KEY_CODE_ENTER || !event.charCode || !this._isDirectInput()) {
return;
}
if (this._lastChars != undefined) {
this._lastChars += String.fromCharCode(event.charCode);
} else {
this._lastChars = String.fromCharCode(event.charCode);
}
console.log(this._lastChars);
};
Input.lastCharsInput = function() {
var lastCharsInput = this._lastChars;
this._lastChars = "";
return lastCharsInput;
}
// Game_Picture
Game_Picture.prototype.show = function(name, origin, x, y, scaleX,
scaleY, opacity, blendMode) {
if (name != null) this._name = name;
if (origin != null) this._origin = origin;
if (x != null) this._x = x;
if (y != null) this._y = y;
if (scaleX != null) this._scaleX = scaleX;
if (scaleY != null) this._scaleY = scaleY;
if (opacity != null) this._opacity = opacity;
if (blendMode != null) this._blendMode = blendMode;
this.initTarget.call(this);
this.initTone.call(this);
this.initRotation.call(this);
};
// Scene setup
var _Scene_Map_createDisplayObjects = Scene_Map.prototype.createDisplayObjects;
Scene_Map.prototype.createDisplayObjects = function() {
_Scene_Map_createDisplayObjects.call(this);
this.createAnswerWindow();
};
Scene_Map.prototype.createAnswerWindow = function() {
this._answerInputWindow = new Window_Answer_Input();
this.addWindow(this._answerInputWindow);
this._answerInputWindow.hide();
};
Scene_Map.prototype.showAnswerInput = function(imageFilename) {
//._answerInputWindow.setQuestionImage(imageFilename);
if (imageFilename) {
this.showPictureWithFilename(imageFilename);
}
this._answerInputWindow.show();
};
Scene_Map.prototype.showPictureWithFilename = function(filename) {
var pictureId = $gameScreen._pictures.length;
$gameScreen.showPicture(pictureId, filename, null, 225, 46, null, null, 255, null);
var imageSprite = new Sprite_Picture(pictureId);
this.addChild(imageSprite);
this.questionImage = imageSprite;
this.questionImage.visible = true;
};
Scene_Map.prototype.hidePictureWithSprite = function(pictureSprite) {
console.log("Erase picture" + pictureSprite._pictureId);
$gameScreen.erasePicture(pictureSprite._pictureId);
this.removeChild(pictureSprite);
}
Scene_Map.prototype.hideAnswerInput = function() {
this._answerInputWindow.hide();
this.hidePictureWithSprite(this.questionImage);
};
var _Scene_Map_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function() {
if (!this._answerInputWindow.visible && this.questionImage) {
this.hidePictureWithSprite(this.questionImage);
}
if (this._answerInputWindow.visible) {
this._answerInputWindow.update();
Input.clear();
}
if (this.questionImage) {
this.questionImage.update();
if (this.questionImage.bitmap) {
var scaleX = this.questionImage.bitmap.width / 20;
var scaleY = this.questionImage.bitmap.height / 20;
var maxScale = (scaleX > scaleY) ? scaleX : scaleY;
//console.log("Scales: " + scaleX + ", " + scaleY);
this.questionImage.picture()._targetScaleX = 1 / maxScale;
this.questionImage.picture()._targetScaleY = 1 / maxScale;
}
}
_Scene_Map_update.call(this);
};
// Window setup
function Window_Answer_Input() {
this.initialize.apply(this, arguments);
}
Window_Answer_Input.prototype = Object.create(Window_Base.prototype);
Window_Answer_Input.prototype.constructor = Window_Answer_Input;
Window_Answer_Input.prototype.initialize = function() {
Window_Base.prototype.initialize.call(this, 244, 442, 320, 72);
this.answer = "";
this.cursorDisplayed = true;
this.cursorCounter = 0;
sceneCenterX = SceneManager._scene._windowLayer._width * 0.5;
};
Window_Answer_Input.prototype.sizeToFit = function(text) {
var textWidth = this.textWidth(text);
var windowWidth = textWidth + this.standardPadding() * 2 + 16;
if (windowWidth < MIN_INPUT_FIELD_WIDTH) {
windowWidth = MIN_INPUT_FIELD_WIDTH;
}
if (windowWidth > SceneManager._scene._windowLayer._width) {
windowWidth = SceneManager._scene._windowLayer._width;
}
var inputFieldX = sceneCenterX - windowWidth * 0.5;
this.move(inputFieldX, this.y, windowWidth, this.height);
this.createContents();
};
Window_Answer_Input.prototype.setQuestionImage = function(filename) {
};
Window_Answer_Input.prototype.show = function() {
$gameMap._interpreter.setWaitMode(ANSWER_WAIT_MODE);
$gameTemp.isPlayerAnswered = false;
this.shouldHide = false;
this.answer = "";
Window_Base.prototype.show.call(this);
};
Window_Answer_Input.prototype.hide = function() {
$gameMap._interpreter.setWaitMode('');
Window_Base.prototype.hide.call(this);
if (this.questionImage != undefined && this.questionImage != null) {
this.removeChild(this.questionImage);
}
};
Window_Answer_Input.prototype.update = function() {
this.hideIfNeeded();
if (this.visible) {
if (this.questionImage != undefined && this.questionImage != null) {
this.questionImage.update();
}
this.updateInput();
this.updateAnswerText();
}
}
Window_Answer_Input.prototype.updateInput = function() {
if (Input.lastCharsInput != undefined) {
this.answer += Input.lastCharsInput();
}
if (Input.isRepeated("backspace")) {
if (this.answer.length > 0) {
this.answer = this.answer.substring(0, this.answer.length - 1);
}
} else if (Input.isTriggered("ok") && Input._lastPressedKeyCode == KEY_CODE_ENTER) {
if (this.answer.length > 0) {
this.validateAnswer(this.answer);
} else {
this.shouldHide = true;
}
} else if (Input.isTriggered('escape')) {
console.log("Escape pressed");
this.shouldHide = true;
}
}
Window_Answer_Input.prototype.hideIfNeeded = function() {
if (this.shouldHide) {
this.hide();
}
};
Window_Answer_Input.prototype.updateAnswerText = function() {
this.contents.clear();
var textWithCursor = this.answer + "_";
var textWithSpace = this.answer + " ";
var displayedText = this.cursorDisplayed ? textWithCursor : textWithSpace;
this.cursorCounter++;
if (this.cursorCounter >= 60) {
this.cursorDisplayed = !this.cursorDisplayed;
this.cursorCounter = 0;
}
var longestText = (displayedText.length > PLACEHOLDER_TEXT.length) ? displayedText : PLACEHOLDER_TEXT;
this.sizeToFit(longestText);
if (this.answer.length <= 0) {
this.contents.textColor = PLACEHOLDER_COLOR;
this.drawText(PLACEHOLDER_TEXT, 0, 0, this.contents.width, 32, 'center');
}
this.contents.textColor = TEXT_COLOR;
this.drawText(displayedText, 0, 0, this.contents.width, 32, 'center');
}
Window_Answer_Input.prototype.validateAnswer = function(answer) {
if (answer.toLowerCase() === correctAnswer.toLowerCase()) {
$gameTemp.isPlayerAnswered = true;
}
$gameTemp.questionInputAnswer = this.answer;
this.shouldHide = true;
}
})();