RPG Maker MZ
Exit v1.0

On desktop environments, you generally want an exit button. This simple plugin does just that, add an exit button.

Download (20.14 kB, 143 times downloaded)

Demo (2.82 MB, 66 times downloaded)

Placement

Make sure to place this plugin below the plugins that this plugin requires, but above plugins that rely on this plugin.

Usage

For the most part, this plugin can be configured using plugin parameters and is mostly plug and play. The exit options will be added to the title scene and the game end scene.

Plugin commands

Exit game

A simple command that allows you to exit the game.

Arguments:

Force Exit If enabled, will skip the exit confirmation scene.

This plugin overwrites default functionality. Make sure you check whether or not the plugin is compatible with other plugins by checking which functions they overwrite. Below is the list of methods it overwrites:

  • Scene_GameEnd.prototype.commandWindowRect
  • Scene_GameEnd.prototype.createCommandWindow
  • Scene_Title.prototype.commandWindowRect
  • Scene_Title.prototype.createCommandWindow
  • Window_GameEnd.prototype.makeCommandList
  • Window_TitleCommand.prototype.makeCommandList
/******************************************************************************
 * CXJ_MZ_CoreImprovements.js                                                 *
 ******************************************************************************
 * By G.A.M. Kertopermono, a.k.a. GaryCXJk                                    *
 ******************************************************************************
 * License: MIT                                                               *
 ******************************************************************************
 * Copyright (c) 2022, G.A.M. Kertopermono                                    *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    *
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        *
 * DEALINGS IN THE SOFTWARE.                                                  *
 ******************************************************************************/

/*:
 * @target MZ
 * @plugindesc Adds an exit option.
 * @author G.A.M. Kertopermono
 *
 * @help
 * ============================================================================
 * = About                                                                    =
 * ============================================================================
 *
 * On desktop environments, you generally want an exit button. This simple
 * plugin does just that, add an exit button.
 *
 * ============================================================================
 * = Requirements                                                             =
 * ============================================================================
 *
 * This plugin requires the following plugins to work:
 *
 * * CXJ_MZ.CoreEssentials: ^1.5
 *
 * ============================================================================
 * = Placement                                                                =
 * ============================================================================
 *
 * Make sure to place this plugin below the plugins that this plugin requires,
 * but above plugins that rely on this plugin.
 *
 * ============================================================================
 * = Usage                                                                    =
 * ============================================================================
 *
 * For the most part, this plugin can be configured using plugin parameters
 * and is mostly plug and play. The exit options will be added to the title
 * scene and the game end scene.
 *
 * ---------------
 * Plugin commands
 * ---------------
 *
 * Exit game
 * ---------
 *
 * A simple command that allows you to exit the game.
 *
 * Arguments:
 *
 * Force Exit - If enabled, will skip the exit confirmation scene.
 *
 * ============================================================================
 * = Changelog                                                                =
 * ============================================================================
 *
 * 1.0 (2022-07-17)
 * ----------------
 *
 * * Initial release
 *
 * ============================================================================
 * = Compatibility                                                            =
 * ============================================================================
 *
 * This plugin overwrites default functionality. Make sure you check whether or
 * not the plugin is compatible with other plugins by checking which functions
 * they overwrite. Below is the list of methods it overwrites:
 *
 * * Scene_GameEnd.prototype.commandWindowRect
 * * Scene_GameEnd.prototype.createCommandWindow
 * * Scene_Title.prototype.commandWindowRect
 * * Scene_Title.prototype.createCommandWindow
 * * Window_GameEnd.prototype.makeCommandList
 * * Window_TitleCommand.prototype.makeCommandList
 *
 * ============================================================================
 * = License                                                                  =
 * ============================================================================
 *
 * Copyright (c) 2022, G.A.M. Kertopermono
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * ============================================================================
 *
 * @param windowWidth
 * @text Window Width
 * @default 300
 * @type number
 * @min -9999999
 *
 * @param exitButton
 * @text Exit Button
 *
 * @param exitButton.title
 * @text Title visibility
 * @desc Add exit button on title?
 * @type boolean
 * @on Yes
 * @off No
 * @default true
 * @parent exitButton
 *
 * @param.exitButton.titlePlacement
 * @text Title placement
 * @desc Where the exit button should be placed.
 * @type text
 * @parent exitButton
 *
 * @param exitButton.gameEnd
 * @text Game end visibility
 * @desc Add exit button on game end?
 * @type boolean
 * @on Yes
 * @off No
 * @default true
 * @parent exitButton
 *
 * @param.exitButton.gameEndPlacement
 * @text Game end placement
 * @desc Where the exit button should be placed.
 * @type text
 * @default cancel
 * @parent exitButton
 *
 * @param text
 * @text Text
 *
 * @param text.title
 * @text Home: Quit Game
 * @type string
 * @default Quit Game
 * @parent text
 *
 * @param text.gameEnd
 * @text End Game: To Desktop
 * @type string
 * @default To Desktop
 * @parent text
 *
 * @param text.confirm
 * @text Exit: Confirmation
 * @type string
 * @default Are you sure you want to quit?
 * @parent text
 *
 * @command exit
 * @text Exit game
 * @desc Exits the game.
 *
 * @arg forceExit
 * @type boolean
 * @text Force Exit
 * @desc Forces the game to exit.
 * @default false
 * @on Yes
 * @off No
 *
 */

(() => {
  window.CXJ_MZ = window.CXJ_MZ || {};
  const {
    CXJ_MZ
  } = window;
  CXJ_MZ.Exit = CXJ_MZ.Exit || {};
  CXJ_MZ.Exit.version = '1.0';

  if (!CXJ_MZ.CoreEssentials) {
    throw new Error('CoreEssentials has not been initialized. Make sure you load CoreEssentials before this plugin.');
  }

  if (!CXJ_MZ.CoreEssentials.isVersion('CXJ_MZ.CoreEssentials', '1.5')) {
    throw new Error('The correct version of CoreEssentials has not been loaded (required version: 1.5).');
  }

  const {
    CoreEssentials,
    Exit,
  } = CXJ_MZ;

  const pluginName = 'CXJ_MZ_Exit';

  /* ------------------------------------------------------------------------
   * - Default parameters                                                   -
   * ------------------------------------------------------------------------
   */

  const parameters = CoreEssentials.getParameters(pluginName, {
    windowWidth: 300,
    'exitButton.title': true,
    'exitButton.titlePlacement': '',
    'exitButton.gameEnd': true,
    'exitButton.gameEndPlacement': 'cancel',
    'text.title': 'Quit Game',
    'text.gameEnd': 'To Desktop',
    'text.confirm': 'Are you sure you want to quit?',
  }, {
    windowWidth: 'number',
    'exitButton.title': 'boolean',
    'exitButton.titlePlacement': 'text',
    'exitButton.gameEnd': 'boolean',
    'exitButton.gameEndPlacement': 'cancel',
    'text.title': 'text',
    'text.gameEnd': 'text',
    'text.confirm': 'text',
  });
  /* ------------------------------------------------------------------------
   * - Plugin commands                                                      -
   * ------------------------------------------------------------------------
   */

  /**
   * Sets the main font face.
   *
   * @param {args} - Arguments.
   * @param {args.fontFace} - The font face.
   */
   PluginManager.registerCommand(pluginName, "exit", (args) => {
    const { forceExit: _value } = args;
    const forceExit = JSON.parse(_value);
    if (forceExit) {
      SceneManager.exit();
    } else {
      SceneManager.push(CXJ_MZ.Exit.Scene_ExitConfirm);
    }
  });

  /* ------------------------------------------------------------------------
   * - Private variables                                                    -
   * ------------------------------------------------------------------------
   */

  let hasLocalizationHelper = null;
  const paramTextRemap = {
    'title': 'command.exit',
    'gameEnd': 'command.toDesktop',
    'confirm': 'message.exitConfirm',
  };
  const text = {};

  /* --------------------------------------------------------------------------
   * - Private functions                                                      -
   * -                                                                        -
   * - These are helper functions that aren't meant to be used outside the    -
   * - plugin.                                                                -
   * --------------------------------------------------------------------------
   */

  /**
   * Retrieves a localized string from the parameters.
   */
   const getLocalizedText = (key) => {
    if (hasLocalizationHelper === null) {
      hasLocalizationHelper = CXJ_MZ.CoreEssentials.isVersion('CXJ_MZ.LocalizationHelper', '1.0');
    }
    if (hasLocalizationHelper) {
      return CXJ_MZ.LocalizationHelper.line(paramTextRemap[key] || key);
    }
    return '';
  };

  /**
   * Retrieves a string from the parameters.
   * @param {string} key => The string key.
   * @returns {string} The requested text string.
   */
  const getText = (key) => getLocalizedText(key) || parameters[`text.${key}`] || '';

  /* We'll bind the getText function to properties
   * on the text object, since addCommand only
   * accepts strings by default.
   */
  Object.defineProperties(text, {
    title: {
      get: getText.bind(null, 'title'),
    },
    gameEnd: {
      get: getText.bind(null, 'gameEnd'),
    },
    confirm: {
      get: getText.bind(null, 'confirm'),
    },
    ok: {
      get: () => getLocalizedText(Exit.LOCALIZATION.OK) || 'OK',
    },
  });

  /* ------------------------------------------------------------------------
   * - Plugin properties                                                    -
   * ------------------------------------------------------------------------
   */

  Exit.LOCALIZATION = {
    OK: 'general.ok',
  };

  Object.seal(Exit.LOCALIZATION);

  (() => {
    //-----------------------------------------------------------------------------
    // Window_ExitConfirm
    //

    /**
     * An exit confirmation window.
     */
    class Window_ExitConfirm extends Window_HorzCommand {
      _labelLines = null;

      initialize(rect) {
        super.initialize(rect);
        this.initializeWindow(rect);
        this.openness = 0;
        this.select(1);
        this.open();
      }

      makeCommandList() {
        this.addCommand(text.ok, 'ok');
        this.addCommand(TextManager.cancel, 'cancel');
      }

      maxCols() {
          return 2;
      };

      itemRect(index) {
        const rect = super.itemRect(index);
        const labelLines = this.textLabel().length;
        rect.y+= labelLines * this.itemHeight();
        return rect;
      }

      textLabel() {
        if (this._labelLines) {
          return this._labelLines;
        }
        const { confirm } = text;
        const lines = confirm.split(' ');
        let textLabel = [''];
        while (lines.length) {
          const line = textLabel[textLabel.length - 1];
          const space = line.length ? ' ' : '';
          const word = lines.shift();
          const testLine = `${line}${space}${word}`;
          const width = this.textWidth(testLine);
          if (width > (this.innerWidth - this.colSpacing())) {
            textLabel.push(word);
          } else {
            textLabel[textLabel.length - 1] = testLine;
          }
        }
        this._labelLines = textLabel;
        return textLabel;
      }

      initializeWindow() {
        const currentHeight = this.height;
        const labelLines = this.textLabel();
        const height = currentHeight + labelLines.length * this.itemHeight();
        const y = (Graphics.boxHeight - height) / 2;
        this.move(this.x, y, this.width, height);
        this.createContents();
        this.refresh();
        labelLines.forEach((line, index) => {
          this.drawText(line, this.colSpacing() / 2, index * this.itemHeight() + this.rowSpacing() / 2, this.innerWidth - this.colSpacing(), 'center');
        });
      }
    }
    Exit.Window_ExitConfirm = Window_ExitConfirm;

    /**
     * An exit confirmation scene.
     */
    class Scene_ExitConfirm extends Scene_MenuBase {
      create() {
        super.create();
        this.createCommandWindow();
      }

      stop() {
        super.stop();
        this._commandWindow.close();
      }

      createCommandWindow() {
        const rect = this.commandWindowRect();
        this._commandWindow = new Window_ExitConfirm(rect);
        this._commandWindow.setHandler("ok", this.exitGame.bind(this));
        this._commandWindow.setHandler("cancel", this.popScene.bind(this));
        this.addWindow(this._commandWindow);
      }

      exitGame() {
        this._commandWindow.close();
        this.fadeOutAll();
        SceneManager.exit();
      }

      mainCommandWidth() {
        let windowWidth = parameters.windowWidth;
        if (windowWidth <= 0) {
          windowWidth = Graphics.boxWidgh + windowWidth;
        }
        return Math.min(Graphics.boxWidth, Math.max(0, windowWidth));
      }

      commandWindowRect() {
          const ww = this.mainCommandWidth();
          const wh = this.calcWindowHeight(1, true);
          const wx = (Graphics.boxWidth - ww) / 2;
          const wy = (Graphics.boxHeight - wh) / 2;
          return new Rectangle(wx, wy, ww, wh);
      };
    }
    Exit.Scene_ExitConfirm = Scene_ExitConfirm;

    /* --------------------------------------------------------------------
     * - Scene_GameEnd.prototype.commandWindowRect (Override)             -
     * --------------------------------------------------------------------
     */
    sgeCommandWindowRect = CoreEssentials.setNoConflict('Scene_GameEnd.prototype.commandWindowRect');
    Scene_GameEnd.prototype.commandWindowRect = function() {
      const rect = sgeCommandWindowRect.call(this);
      const newHeight = this.calcWindowHeight(this.availableCommands(), true);
      rect.y-= (newHeight - rect.height) / 2;
      rect.height = newHeight;
      return rect;
    };

    /* --------------------------------------------------------------------
     * - Scene_GameEnd.prototype.availableCommands (New)                  -
     * --------------------------------------------------------------------
     */
    Scene_GameEnd.prototype.availableCommands = function() {
      let commands = 2;
      if (parameters['exitButton.title'] && Utils.isNwjs()) {
        commands+= 1;
      }
      return commands;
    }

    /* --------------------------------------------------------------------
     * - Scene_GameEnd.prototype.createCommandWindow (Override)           -
     * --------------------------------------------------------------------
     */
    CoreEssentials.registerFunctionExtension('Scene_GameEnd.prototype.createCommandWindow', function() {
      this._commandWindow.setHandler("exit", this.commandExit.bind(this));
    });

    /* --------------------------------------------------------------------
     * - Scene_GameEnd.prototype.commandExit (New)                        -
     * --------------------------------------------------------------------
     */
    Scene_GameEnd.prototype.commandExit = function() {
      this._commandWindow.close();
      SceneManager.push(Scene_ExitConfirm);
    }

    /* --------------------------------------------------------------------
     * - Scene_Title.prototype.commandWindowRect (Override)               -
     * --------------------------------------------------------------------
     */
    stCommandWindowRect = CoreEssentials.setNoConflict('Scene_Title.prototype.commandWindowRect');
    Scene_Title.prototype.commandWindowRect = function() {
      const rect = stCommandWindowRect.call(this);
      const newHeight = this.calcWindowHeight(this.availableCommands(), true);
      rect.y-= (newHeight - rect.height);
      rect.height = newHeight;
      return rect;
    };

    /* --------------------------------------------------------------------
     * - Scene_Title.prototype.availableCommands (New)                    -
     * --------------------------------------------------------------------
     */
    Scene_Title.prototype.availableCommands = function() {
      let commands = 3;
      if (parameters['exitButton.title'] && Utils.isNwjs()) {
        commands+= 1;
      }
      return commands;
    }

    /* --------------------------------------------------------------------
     * - Scene_Title.prototype.createCommandWindow (Override)             -
     * --------------------------------------------------------------------
     */
    CoreEssentials.registerFunctionExtension('Scene_Title.prototype.createCommandWindow', function() {
      this._commandWindow.setHandler("exit", this.commandExit.bind(this));
    });

    /* --------------------------------------------------------------------
     * - Scene_Title.prototype.commandExit (New)                          -
     * --------------------------------------------------------------------
     */
    Scene_Title.prototype.commandExit = function() {
      this._commandWindow.close();
      SceneManager.push(Scene_ExitConfirm);
    }

    /* --------------------------------------------------------------------
     * - Window_GameEnd.prototype.makeCommandList (Override)              -
     * --------------------------------------------------------------------
     */
    CoreEssentials.registerFunctionExtension('Window_GameEnd.prototype.makeCommandList', function() {
      if (parameters['exitButton.gameEnd'] && Utils.isNwjs()) {
        this.addCommand(
          text.gameEnd,
          'exit',
          true,
          null,
          'cancel',
        );
      }
    });

    /* --------------------------------------------------------------------
     * - Window_TitleCommand.prototype.makeCommandList (Override)         -
     * --------------------------------------------------------------------
     */
    CoreEssentials.registerFunctionExtension('Window_TitleCommand.prototype.makeCommandList', function() {
      if (parameters['exitButton.title'] && Utils.isNwjs()) {
        this.addCommand(
          text.title,
          'exit',
        );
      }
    })
  })();
})();
                                

Creator: GaryCXJk

Release date: 2022-07-17

Downloads: 0

License: The MIT License

Requirements: