Source: Window_NumberInput.js

Window_NumberInput.js

//-----------------------------------------------------------------------------
// Window_NumberInput
//
// The window used for the event command [Input Number].
/**
 * The window used for the event command [Input Number].
 *
 * @class
 * @extends Window_Selectable
 */
function Window_NumberInput() {
    this.initialize(...arguments);
}

Window_NumberInput.prototype = Object.create(Window_Selectable.prototype);
Window_NumberInput.prototype.constructor = Window_NumberInput;

Window_NumberInput.prototype.initialize = function() {
    Window_Selectable.prototype.initialize.call(this, new Rectangle());
    this._number = 0;
    this._maxDigits = 1;
    this.openness = 0;
    this.createButtons();
    this.deactivate();
    this._canRepeat = false;
};

/**
 * Sets the message window
 *
 * @param {Window_Message} messageWindow - The message window
 */
Window_NumberInput.prototype.setMessageWindow = function(messageWindow) {
    this._messageWindow = messageWindow;
};

/**
 * Start processing for the window
 */
Window_NumberInput.prototype.start = function() {
    this._maxDigits = $gameMessage.numInputMaxDigits();
    this._number = $gameVariables.value($gameMessage.numInputVariableId());
    this._number = this._number.clamp(0, Math.pow(10, this._maxDigits) - 1);
    this.updatePlacement();
    this.placeButtons();
    this.createContents();
    this.refresh();
    this.open();
    this.activate();
    this.select(0);
};

/**
 * Updates the placement of the window
 */
Window_NumberInput.prototype.updatePlacement = function() {
    const messageY = this._messageWindow.y;
    const spacing = 8;
    this.width = this.windowWidth();
    this.height = this.windowHeight();
    this.x = (Graphics.boxWidth - this.width) / 2;
    if (messageY >= Graphics.boxHeight / 2) {
        this.y = messageY - this.height - spacing;
    } else {
        this.y = messageY + this._messageWindow.height + spacing;
    }
};

/**
 * Get the width of the window in pixels
 *
 * @return {number} The width of the window
 */
Window_NumberInput.prototype.windowWidth = function() {
    const totalItemWidth = this.maxCols() * this.itemWidth();
    const totalButtonWidth = this.totalButtonWidth();
    return Math.max(totalItemWidth, totalButtonWidth) + this.padding * 2;
};

/**
 * Get the height of the window in pixels
 *
 * @return {number} The height of the window
 */
Window_NumberInput.prototype.windowHeight = function() {
    if (ConfigManager.touchUI) {
        return this.fittingHeight(1) + this.buttonSpacing() + 48;
    } else {
        return this.fittingHeight(1);
    }
};

/**
 * Get the maximum amount of columns in the window
 *
 * @return {number} The maximum amount of columns in the window
 */
Window_NumberInput.prototype.maxCols = function() {
    return this._maxDigits;
};

/**
 * Get the maximum amount of items in the window
 *
 * @return {number} The maximum amount of items in the window
 */
Window_NumberInput.prototype.maxItems = function() {
    return this._maxDigits;
};

/**
 * Get the width of an item
 *
 * @return {number} The width of an item
 */
Window_NumberInput.prototype.itemWidth = function() {
    return 48;
};

/**
 * Get the rectangle for an item at the given index
 * 
 * @param {number} index - The index to get the rect for
 * @return {Rectangle} The rectangle for the item's drawing area
 */
Window_NumberInput.prototype.itemRect = function(index) {
    const rect = Window_Selectable.prototype.itemRect.call(this, index);
    const innerMargin = this.innerWidth - this.maxCols() * this.itemWidth();
    rect.x += innerMargin / 2;
    return rect;
};

/**
 * Check if scrolling is enabled. In Window_NumberInput, this will always return false by default
 *
 * @return {boolean} True if scrolling is enabled
 */
Window_NumberInput.prototype.isScrollEnabled = function() {
    return false;
};

/**
 * Check if hovering is enabled. In Window_NumberInput, this will always return false by default
 *
 * @return {boolean} True if hovering is enabled
 */
Window_NumberInput.prototype.isHoverEnabled = function() {
    return false;
};

/**
 * Create the Touch UI buttons
 */
Window_NumberInput.prototype.createButtons = function() {
    this._buttons = [];
    if (ConfigManager.touchUI) {
        for (const type of ["down", "up", "ok"]) {
            const button = new Sprite_Button(type);
            this._buttons.push(button);
            this.addInnerChild(button);
        }
        this._buttons[0].setClickHandler(this.onButtonDown.bind(this));
        this._buttons[1].setClickHandler(this.onButtonUp.bind(this));
        this._buttons[2].setClickHandler(this.onButtonOk.bind(this));
    }
};

/**
 * Place the Touch UI buttons
 */
Window_NumberInput.prototype.placeButtons = function() {
    const sp = this.buttonSpacing();
    const totalWidth = this.totalButtonWidth();
    let x = (this.innerWidth - totalWidth) / 2;
    for (const button of this._buttons) {
        button.x = x;
        button.y = this.buttonY();
        x += button.width + sp;
    }
};

/**
 * Get the horizontal space taken up by the Touch UI buttons
 *
 * @return {number} Amount of horizontal space taken up by the Touch UI buttons
 */
Window_NumberInput.prototype.totalButtonWidth = function() {
    const sp = this.buttonSpacing();
    return this._buttons.reduce((r, button) => r + button.width + sp, -sp);
};

/**
 * Get the spacing between Touch UI buttons
 *
 * @return {number} Spacing between buttons
 */
Window_NumberInput.prototype.buttonSpacing = function() {
    return 8;
};

/**
 * Get the y coordinate of the Touch UI buttons
 *
 * @return {number} y coordinate of the Touch UI buttons
 */
Window_NumberInput.prototype.buttonY = function() {
    return this.itemHeight() + this.buttonSpacing();
};

Window_NumberInput.prototype.update = function() {
    Window_Selectable.prototype.update.call(this);
    this.processDigitChange();
};

/**
 * Processing when a digit changes
 */
Window_NumberInput.prototype.processDigitChange = function() {
    if (this.isOpenAndActive()) {
        if (Input.isRepeated("up")) {
            this.changeDigit(true);
        } else if (Input.isRepeated("down")) {
            this.changeDigit(false);
        }
    }
};

/**
 * Change a digit
 *
 * @param {boolean} up - If digit is increasing or not
 */
Window_NumberInput.prototype.changeDigit = function(up) {
    const index = this.index();
    const place = Math.pow(10, this._maxDigits - 1 - index);
    let n = Math.floor(this._number / place) % 10;
    this._number -= n * place;
    if (up) {
        n = (n + 1) % 10;
    } else {
        n = (n + 9) % 10;
    }
    this._number += n * place;
    this.refresh();
    this.playCursorSound();
};

/**
 * Check if touch OK is enabled. In Window_NumberInput, this will always return false by default
 *
 * @return {boolean} True if touch OK is enabled
 */
Window_NumberInput.prototype.isTouchOkEnabled = function() {
    return false;
};

/**
 * Check if ok is enabled. In Window_NumberInput, this will always return true by default
 *
 * @return {boolean} True if ok is enabled
 */
Window_NumberInput.prototype.isOkEnabled = function() {
    return true;
};

/**
 * Check if cancel is enabled. In Window_NumberInput, this will always return false by default
 *
 * @return {boolean} True if cancel is enabled
 */
Window_NumberInput.prototype.isCancelEnabled = function() {
    return false;
};

/**
 * Processing for OK input
 */
Window_NumberInput.prototype.processOk = function() {
    this.playOkSound();
    $gameVariables.setValue($gameMessage.numInputVariableId(), this._number);
    this._messageWindow.terminateMessage();
    this.updateInputData();
    this.deactivate();
    this.close();
};

/**
 * Draws the item at the given index
 *
 * @param {number} index - The index to draw
 */
Window_NumberInput.prototype.drawItem = function(index) {
    const rect = this.itemLineRect(index);
    const align = "center";
    const s = this._number.padZero(this._maxDigits);
    const c = s.slice(index, index + 1);
    this.resetTextColor();
    this.drawText(c, rect.x, rect.y, rect.width, align);
};

/**
 * Handling for Touch UI Up Button press
 */
Window_NumberInput.prototype.onButtonUp = function() {
    this.changeDigit(true);
};

/**
 * Handling for Touch UI Down Button press
 */
Window_NumberInput.prototype.onButtonDown = function() {
    this.changeDigit(false);
};

/**
 * Handling for Touch UI Ok Button press
 */
Window_NumberInput.prototype.onButtonOk = function() {
    this.processOk();
};