// JavaScript Document//=============================================================================
// O_OptionCustom.js
//=============================================================================

/*:
 * @plugindesc OptionCustom.
 * @author 
 *
 * @help This plugin does not provide plugin commands.
 */

/*:ja
 * @plugindesc オプションにゲーム終了とフルスクリーン切替を追加
 * @author 
 *
 * @help このプラグインには、プラグインコマンドはありません。
 */
(function() {
	
	var LOCK_FLG_F4_KEY=false;//オプション画面を開いている間はF4キーをロック
	
	ConfigManager.alwaysFullScreen  = false;//フルスクリーン

//***オプション画面
	Window_Options.prototype.addGeneralOptions = function() {
		this.addCommand(TextManager.alwaysDash, 'alwaysDash');
		this.addCommand(TextManager.commandRemember, 'commandRemember');
		this.addCommand("Full Screen", 'alwaysFullScreen');
	};
	
	
	Window_Options.prototype.callCancelHandler = function() {
		LOCK_FLG_F4_KEY=false;//F4キーのロック解除
		this.callHandler('cancel');
	};
	var _Window_Options_prototype_initialize=Window_Options.prototype.initialize;
	Window_Options.prototype.initialize = function() {
		LOCK_FLG_F4_KEY=true;//F4キーのロック開始
		_Window_Options_prototype_initialize.call(this);
	};
	
//configにパラメータを追加
	var _ConfigManager_makeData=ConfigManager.makeData;
	ConfigManager.makeData = function() {
		var config=_ConfigManager_makeData.call(this);
		config.alwaysFullScreen=this.alwaysFullScreen;
		return config;
	};
	
	var _ConfigManager_applyData=ConfigManager.applyData;
	ConfigManager.applyData = function(config) {
		_ConfigManager_applyData.call(this,config);
		this.alwaysFullScreen = this.readVolume(config, 'alwaysFullScreen');
	};

//***オプションメニューでのスクリーン切替
	var _Window_Options_prototype_processOk=Window_Options.prototype.processOk;
	Window_Options.prototype.processOk = function() {
		_Window_Options_prototype_processOk.call(this);
		
		var symbol = this.commandSymbol(this.index());
		if(symbol=="alwaysFullScreen"){
			Graphics.updateFullScreen(this.getConfigValue(symbol));
		}
	};
	
//***タイトルメニューに終了を追加
	var _Window_TitleCommand_makeCommandList = Window_TitleCommand.prototype.makeCommandList;
	Window_TitleCommand.prototype.makeCommandList = function() {
		_Window_TitleCommand_makeCommandList.call(this);
		this.addCommand('Exit', 'endGame');
	};
	var _Scene_Title_createCommandWindow =Scene_Title.prototype.createCommandWindow;
	Scene_Title.prototype.createCommandWindow = function() {
		_Scene_Title_createCommandWindow.call(this);
		this._commandWindow.setHandler('endGame', this.endGame.bind(this));
	};
	Scene_Title.prototype.endGame = function() {
		this._commandWindow.close();
		this.fadeOutAll();
		SceneManager.exit();
	};

//***終了メニューにゲーム終了を追加
	var _Window_GameEnd_prototype_makeCommandList = Window_GameEnd.prototype.makeCommandList;
	Window_GameEnd.prototype.makeCommandList = function() {
		this.addCommand(TextManager.toTitle, 'toTitle');
		this.addCommand('Exit', 'endGame');
    	this.addCommand(TextManager.cancel,  'cancel');
	};
	
	Scene_GameEnd.prototype.createCommandWindow = function() {
		this._commandWindow = new Window_GameEnd();
		this._commandWindow.setHandler('toTitle',  this.commandToTitle.bind(this));
		this._commandWindow.setHandler('endGame',  this.endGame.bind(this));
		this._commandWindow.setHandler('cancel',   this.popScene.bind(this));
		this.addWindow(this._commandWindow);
	};
	Scene_GameEnd.prototype.endGame = function() {
		this._commandWindow.close();
		this.fadeOutAll();
		SceneManager.exit();
	};
	
//***フルスクリーン起動処理
  	var _Scene_Boot_start = Scene_Boot.prototype.start;
    Scene_Boot.prototype.start = function() {
        _Scene_Boot_start.call(this);
		if(ConfigManager.alwaysFullScreen==100){
			ConfigManager.alwaysFullScreen=false;	
		}
       Graphics.updateFullScreen(ConfigManager.alwaysFullScreen);
    };
	
	Graphics.updateFullScreen=function(flg){
		if(flg){
			if(this._isFullScreen){
				this._requestFullScreen();		
			}
		}else{
			if(this._isFullScreen){
				
				this._cancelFullScreen();
			}
		}
	};
//****F4キーでのフルスクリーン切替をconfigに記録
	var _Graphics__switchFullScreen=Graphics._switchFullScreen;
	Graphics._switchFullScreen = function() {
		if(LOCK_FLG_F4_KEY){
			return;	//オプションウィンドウが開いてるならF4キーコマンドは無効
		}
		
		_Graphics__switchFullScreen.call(this);
		
		var value=true;
		if (this._isFullScreen()) {
			value=false;
		}
		ConfigManager["alwaysFullScreen"]=value;
		ConfigManager.save();
	};

})();