//============================================================================= // EISAskSystem.js //============================================================================= /*: * @author Kino * @plugindesc A plugin that allows the developer to create intext information. * * @param Brackets * @desc Include brackets around key words / phrases (T/F) * @default T * * @help * Version: 1.03 * * This plugin creates the ASK system like in the game Wild Arms 3. * It allows you to select text within a message window, and ask about that word or phrase. * * Setup - Creating Context Word / Phrase * To create a context word use: * \ASK[ Word or phrase you want to ask ] within the message window. * These words can then be selected with the arrow keys. * Right / Down advances the pointer, left / up moves backward. * * Functions * KR.AskManager.isWordSelect(index) * -Each word is indexed from 0 to maximum words in the plugin once it appears in * a message window. You can then assign them to game variables to do conditional * branching. This function checks if the word has been selected or not. * Example: KR.AskManager.isWordSelected(0) * * KR.AskManager.clearWorldList() * - This script call clears the world list. This is to give the developer * better control over when the word list should be cleared. For example, * to evaluate the conditional branch after the word once the message window * closes. * Example: KR.AskManager.clearWordList(); * * * Contact me via twitter: EISKino, or on the rpg maker forums. * Username on forums: Kino. * Hope this plugin helps, and enjoy! * --Kino */ //============================================================================= // Namespace Initialization //============================================================================= var KR = KR || {}; KR.Plugins = KR.Plugins || {}; (function($) { var parameters = PluginManager.parameters("EISAskSystem"); var includeBrackets = String(parameters.Brackets); $.Plugins.AskSystem = function() { function AskManager() { } AskManager.contextItems = []; AskManager.hasContextItems = false; AskManager.addContextItem = function(contextItem) { this.contextItems.push(contextItem); this.setHasContextItems(true); }; AskManager.getContextItem = function(index) { if(this.contextItems[index] !== undefined) return this.contextItems[index]; else return null; }; AskManager.contextItemLength = function() { return this.contextItems.length; }; AskManager.clearContextItemList = function() { this.contextItems.length = 0; }; AskManager.isContextSelected = function(index) { var contextItem = this.getContextItem(index); if(contextItem !== null) return (contextItem.selected === true) ? 1 : 0; }; AskManager.contextItemsAvailable = function() { return this.hasContextItems; }; AskManager.includeBrackets = function() { return /F/ig.test(includeBrackets); }; AskManager.setHasContextItems = function(boolean) { this.hasContextItems = boolean; }; //============================================================================= // Window_Base //============================================================================= Window_Base.prototype.obtainEscapeParam = function(textState) { var arr = /^\[\d+\]/.exec(textState.text.slice(textState.index)); var arr2 = /^\[(.*?)\]/.exec(textState.text.slice(textState.index)); if (arr) { textState.index += arr[0].length; return parseInt(arr[0].slice(1)); } else if(arr2) { var string = String(arr2[1]); if(AskManager.includeBrackets()) { var regex = new RegExp('\\[' + string + '\]', 'ig'); textState.text = textState.text.replace(regex, string); } return ({string:string, x: textState.x, y: textState.y}); } else { return ''; } }; //============================================================================= // Window_Message //============================================================================= var WindowMessage_initialize = Window_Message.prototype.initialize; Window_Message.prototype.initialize = function() { WindowMessage_initialize.call(this); this._index = -1; this.wordList = []; this.notProcessing = true; }; var WindowMessage_processEscapeCharacter = Window_Message.prototype.processEscapeCharacter; Window_Message.prototype.processEscapeCharacter = function(code, textState) { switch(code) { case 'ASK': this.addAsk(this.obtainEscapeParam(textState)); break; default: WindowMessage_processEscapeCharacter.call(this, code, textState); break; } }; var WindowMessage_update = Window_Message.prototype.update; Window_Message.prototype.update = function() { if(AskManager.contextItemsAvailable()) this.processInput(); this.selectWord(); this.cancelSelection(); WindowMessage_update.call(this); }; Window_Message.prototype.processInput = function() { if( (this._index > -1) || (this.isCursorMovable()) ){ if(Input.isTriggered("down") || Input.isTriggered("right")) { this.cursorMoveForward(); } if(Input.isTriggered("up") || Input.isTriggered("left")) { this.cursorMoveBackward(); } } }; Window_Message.prototype.isCursorMovable = function() { if(this.wordList.length > 0) return true; else return false; }; Window_Message.prototype.cursorMoveForward = function() { if(this._index < this.wordList.length) this.select(1); }; Window_Message.prototype.cursorMoveBackward = function() { if(this._index > - 1) this.select(-1); }; Window_Message.prototype.index = function() { return this._index; }; Window_Message.prototype.select = function(index) { this._index += index; if(this.index() === this.wordList.length) this._index = 0; if(this.index() === -1) this._index = this.wordList.length -1; this.updateCursor(); }; Window_Message.prototype.isSelectingWord = function() { if(this.index() > -1 && AskManager.contextItemsAvailable()) { return true; } else { return false; } }; var WindowMessage_isTriggered = Window_Message.prototype.isTriggered; Window_Message.prototype.isTriggered = function() { if(AskManager.contextItemsAvailable()) { return (Input.isRepeated('ok') || TouchInput.isRepeated()); } else { return WindowMessage_isTriggered.call(this); } }; Window_Message.prototype.selectWord = function() { if( (this.index() > -1 && this.index() < this.wordList.length) && (Input.isPressed('ok')) ) { var word = AskManager.getContextItem(this.index() + (AskManager.contextItemLength() - this.wordList.length)); word.selected = true; this.cancelSelection(); } }; Window_Message.prototype.cancelSelection = function() { if( (this.index() > -1) && (Input.isTriggered('cancel')) ) { this.setCursorRect(0, 0, 0, 0); this._index = -1; } }; Window_Message.prototype.updateCursor = function() { if(this.index() > -1 && (this.index() < this.wordList.length)) { rect = this.itemRectForContextWord(this.index()); this.setCursorRect(rect.x, rect.y, rect.width, rect.height); } else { this.setCursorRect(0, 0, 0, 0); } }; Window_Message.prototype.itemRectForContextWord = function(index) { var contextItem = this.getWord(index); var rect = new Rectangle(); rect.x = contextItem.x; rect.y = contextItem.y; var padding = (AskManager.includeBrackets() === false) ? this.textPadding() * 4 : this.textPadding() * 0.5; rect.width = this.textWidth(contextItem.phrase) + padding; rect.height = this.lineHeight(); return rect; }; Window_Message.prototype.addAsk = function(textState) { AskManager.addContextItem({phrase:textState.string, selected:false, x: textState.x, y: textState.y }); this.wordList.push({phrase:textState.string, selected:false, x: textState.x, y: textState.y }); }; Window_Message.prototype.getWord = function(index) { if(this.wordList[index] !== undefined) return this.wordList[index]; else return null; }; Window_Message.prototype.clearWordProcessing = function() { this.notProcessing = true; AskManager.setHasContextItems(false); this.setCursorRect(0, 0, 0, 0); this._index = -1; }; var WindowMessage_terminateMessage = Window_Message.prototype.terminateMessage; Window_Message.prototype.terminateMessage = function() { this.clearWordProcessing(); this.wordList.length = 0; WindowMessage_terminateMessage.call(this); }; //============================================================================= // Helpers //============================================================================= $.AskManager = {}; $.AskManager.isWordSelected = function(index) { return AskManager.isContextSelected(index - 1); }; $.AskManager.clearWordList = function() { AskManager.clearContextItemList(); }; }; $.Plugins.AskSystem(); })(KR);