//=============================================================================
// O_MenuCustom.js
//=============================================================================

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

/*:ja
 * @plugindesc メニュー画面設定
 * @author 
 *
 * @help 
 * プラグインコマンド
 * backToItemMenu コモンイベントからアイテム一覧に戻る
 */
 
(function(){
	var $back_to_itemMenu = false;
	

//*** タイトルメニュー位置
	var _Window_TitleCommand_updatePlacement = Window_TitleCommand.prototype.updatePlacement;
    Window_TitleCommand.prototype.updatePlacement = function() {
        _Window_TitleCommand_updatePlacement.call(this);
        this.x = 146;
        this.y = 348;
       // this.setBackgroundType(1);
    };
    Window_TitleCommand.prototype.windowWidth = function() {
        return 200;
    };

//***** メインステータスウィドウ
	Window_MenuStatus.prototype.numVisibleRows = function() {
		return 1;
	};
	Window_MenuStatus.prototype.drawItemImage = function(index) {
        var actor = $gameParty.members()[index];
        var rect = this.itemRectForText(index);
        var w = rect.width;//Math.min(rect.width, 144);
        var h = rect.height;//Math.min(rect.height, 144);
        var lineHeight = this.lineHeight();
        this.changePaintOpacity(actor.isBattleMember());
        //this.drawActorFace(actor, rect.x, rect.y + lineHeight * 2.5, w, h);
        this.changePaintOpacity(true);
    };
	Window_MenuStatus.prototype.drawItemStatus = function(index) {
		var actor = $gameParty.members()[index];
		var rect = this.itemRect(index);
		var x = rect.x;// + 162;
		var y = rect.y;// + rect.height / 2 - this.lineHeight() * 1.5;
		var width = rect.width - x - this.textPadding();
		this.drawActorSimpleStatus(actor, x, y, width);
	};
	
//***** メインメニューでステータス表示
	Window_MenuStatus.prototype.drawActorSimpleStatus = function(actor, x, y, width) {	
		
		//立ち絵表示
		var bitmap = ImageManager.loadSystem('stand_main_00');
		if(bitmap){
			var pic_x = 350;
			var pic_y = -20;
			this.contents.blt(bitmap, 0, 0, bitmap.width, bitmap.height, pic_x, pic_y);//,bitmap.width*0.45,bitmap.height*0.45);
		}
		
		//ここから下の原文は「Window_Status.prototype.drawBasicInfo」でrpg_windows.jsを検索
		
		var lineHeight = this.lineHeight();
		
		this.drawActorName(actor, 6, y);
		this.drawActorClass(actor, 192, y);
		this.drawActorNickname(actor, 432, y);
		
		this.drawHorzLine(lineHeight - 10);
		
		y+= (lineHeight * 2) -10;
		
		
		this.drawActorLevel(actor, x, y + lineHeight * 0);
		this.drawActorIcons(actor, x, y + lineHeight * 1);
		this.drawActorHp(actor, x, y + lineHeight * 2);
		this.drawActorTp(actor, x, y + lineHeight * 3);
		//this.drawActorMp(actor, x, y + lineHeight * 3);
		
		
		var expTotal = TextManager.expTotal.format(TextManager.exp);
		var expNext = TextManager.expNext.format(TextManager.level);
		var value1 = actor.currentExp();
		var value2 = actor.nextRequiredExp();
		if (actor.isMaxLevel()) {
			value1 = '-------';
			value2 = '-------';
		}
		
		var exp_x=204;
		var exp_w=200;//270
		this.changeTextColor(this.systemColor());
		this.drawText(expTotal, exp_x, y + lineHeight * 0, exp_w);
		this.drawText(expNext, exp_x, y + lineHeight * 2, exp_w);
		this.resetTextColor();
		this.drawText(value1, exp_x, y + lineHeight * 1, exp_w, 'right');
		this.drawText(value2, exp_x, y + lineHeight * 3, exp_w, 'right');
	
		this.drawHorzLine((y+lineHeight * 4)-10);
		 
		y += (lineHeight * 5) - 10;
		
		var lineHeight = this.lineHeight();
		for (var i = 0; i < 6; i++) {
			var paramId = i + 2;
			var y2 = y + (lineHeight * i) -10;
			this.changeTextColor(this.systemColor());
			this.drawText(TextManager.param(paramId), x, y2, 160);
			this.resetTextColor();
			this.drawText(actor.param(paramId), x + 160, y2, 60, 'right');
		}
				
		var firstEroText = this.getFirstEroText();
		if(firstEroText != 0){
			var y2 = y + (lineHeight * 6) - 20;
			var Num_Shift_X = 165;
			var x_2ndRow = 235;
			this.drawHorzLine(y2);
			
			y2 += lineHeight - 10;
			this.changeTextColor(this.textColor(27));
			this.drawText("Citizens slept with", x, y2, Num_Shift_X);
			this.resetTextColor();
			this.drawText($gameVariables.value(16), x + Num_Shift_X, y2, 60, 'right');
			
			//2列名
			/*
			if($gameVariables.value(19) > 0){
				this.changeTextColor(this.textColor(27));
				this.drawText("セフレの数", x + x_2ndRow, y2, Num_Shift_X);
				this.resetTextColor();
				this.drawText($gameVariables.value(19), x + Num_Shift_X + x_2ndRow, y2, 60, 'right');
			}*/
			
			y2 += lineHeight;
			this.changeTextColor(this.textColor(27));
			this.drawText("Creampies", x, y2, 160);
			this.resetTextColor();
			this.drawText($gameVariables.value(17), x + Num_Shift_X, y2, 60, 'right');
			
			y2 += lineHeight;
			if($gameVariables.value(18) > 0){
				this.changeTextColor(this.textColor(27));
				this.drawText("Orgasms", x, y2, Num_Shift_X);
				this.resetTextColor();
				this.drawText($gameVariables.value(18), x + Num_Shift_X, y2, 60, 'right');	
			}
			
			y2 += lineHeight;
			this.changeTextColor(this.textColor(27));
			this.drawText('First experience', x, y2, 110);
			this.resetTextColor();
			
			var erotext_width = 450;
			if(firstEroText.length > 60){
				erotext_width = 630;
			}
			this.drawText(firstEroText, x + 120, y2, erotext_width);
			
			
			
		}
		
		var equip_x=225;//456
		var equips = actor.equips();
		var count = Math.min(equips.length, 6);
		for (var i = 0; i < count; i++) {
			this.drawItemName(equips[i], equip_x, y + this.lineHeight() * i);
		}
		
		/*
		this.drawHorzLine(y+lineHeight * 6);
		
		y+=lineHeight * 7;
		
		this.drawTextEx(actor.profile(), x, y);
		*/
		
	};
	
	Window_MenuStatus.prototype.getFirstEroText = function(){
		var text = "";
	//	var eventID = 127;
		var eventID = $gameVariables.value(15);
		switch(eventID){
		case 127:
			text = "Fleur gets raped by a man whose face she didn't she, while she was stuck in a wall in a back alley.";
			break;
		case 129:
			text = "Fleur gets raped by a red clown, whilst jammed into a box.";
			break;
		case 135:
			text = "Fleur gets raped by playboys after passing out drunk.";
			break;
		case 137:
			text = "Fleur gets raped by old men while sleeping in the bathhouse.";
			break;
		}
		
		return text;
	};
	
	Window_Base.prototype.drawActorClass = function(actor, x, y, width) {
		width = width || 220;
		this.resetTextColor();
		var text = actor.currentClass().name;
		if($gameSwitches.value(13)){
			text = "Prostitute " + text;
		}else if($gameSwitches.value(12)){
			text = "Perverted " + text;
		}else if($gameSwitches.value(11)){
			text = "Sexually Liberal " + text;
		}
		
		this.drawText(text, x, y, width);
	};
	
	//ステータスの武器アイコンサイズ調整
	Window_MenuStatus.prototype.drawIcon = function(iconIndex, x, y) {
		var bitmap = ImageManager.loadSystem('IconSet');
		var pw = Window_Base._iconWidth;
		var ph = Window_Base._iconHeight;
		var sx = iconIndex % 16 * pw;
		var sy = Math.floor(iconIndex / 16) * ph;
		this.contents.blt(bitmap, sx, sy, pw, ph, x+5, y, pw * 0.8 ,ph * 0.8);
	};
	
	Window_MenuStatus.prototype.drawHorzLine = function(y) {
		var lineY = y + this.lineHeight() / 2 - 1;
		this.contents.paintOpacity = 48;
		this.contents.fillRect(0, lineY, this.contentsWidth()-200, 2, this.normalColor());
		this.contents.paintOpacity = 255;	
	};


//***** アイテムメニューの配置変更
	var _Scene_Item_prototype_create = Scene_Item.prototype.create;
	Scene_Item.prototype.create = function() {
		_Scene_Item_prototype_create.call(this);
		this._itemWindow.y=this._categoryWindow.height;
		this._categoryWindow.y=0;
		this._helpWindow.y=this._itemWindow.height+this._categoryWindow.height;
	};
	
//***** アイテムでコモンイベント実行後にアイエムメニューに戻る処理
//プラグインコマンド backToItemMenu
	Scene_Item.prototype.popScene = function() {
		if($back_to_itemMenu){
			$back_to_itemMenu = false;
			SceneManager.goto(Scene_Menu);
		}else{
			SceneManager.pop();
		}
	};
	Scene_Item.prototype.update = function() {
		if($back_to_itemMenu){
			$back_to_itemMenu = false;
			this.onCategoryOk();
			this._categoryWindow.deactivate();
			if ($gameParty.numItems($gameParty.lastItem()) > 0){
				this.onItemOk();
				this._itemWindow.deactivate();
			}
		}
		Scene_Base.prototype.update.call(this);
	};
	
	
	

	
//***** オリジナルメニューの追加
	//var _Window_MenuCommand_prototype_addMainCommands = Window_MenuCommand.prototype.addMainCommands;
	Window_MenuCommand.prototype.addMainCommands = function() {
		 var enabled = this.areMainCommandsEnabled();
		if (this.needsCommand('item')) {
			this.addCommand(TextManager.item, 'item', enabled);
		}
		
		this.addCommand('Journey Hints', 'quest', true);
		this.addCommand('Kingdom Map', 'world_map', true);
		
		if (this.needsCommand('equip')) {
			this.addCommand(TextManager.equip, 'equip', enabled);
		}
			
		if (this.needsCommand('skill')) {
			this.addCommand(TextManager.skill, 'skill', enabled);
		}
		/*
		if (this.needsCommand('status')) {
			this.addCommand(TextManager.status, 'status', enabled);
		}*/
	};
	
	/*
	Window_MenuCommand.prototype.addOriginalCommands = function() {
		this.addCommand('王国マップ', 'world_map', true);
	};
	*/
	
	//ロードメニューの追加
	var _Window_MenuCommand_prototype_addSaveCommand = Window_MenuCommand.prototype.addSaveCommand;
	Window_MenuCommand.prototype.addSaveCommand = function() {
		_Window_MenuCommand_prototype_addSaveCommand.call(this);
    	this.addCommand('Load', 'load', this.isLoadEnabled());
	};
	//ロードメニュー有効判定
	Window_MenuCommand.prototype.isLoadEnabled = function() {
		var res=true;
		if(DataManager.isEventTest()){
			res = false;
		}else{
			res = DataManager.isAnySavefileExists();
		}
		return res;
	};
	

	Scene_Menu.prototype.createCommandWindow = function() {
		this._commandWindow = new Window_MenuCommand(0, 0);
		this._commandWindow.setHandler('item',      this.commandItem.bind(this));
		this._commandWindow.setHandler('status',    this.commandPersonal.bind(this));
		this._commandWindow.setHandler('formation', this.commandFormation.bind(this));
		this._commandWindow.setHandler('options',   this.commandOptions.bind(this));
		this._commandWindow.setHandler('save',      this.commandSave.bind(this));
		this._commandWindow.setHandler('load', 		 this.commandLoad.bind(this));
		this._commandWindow.setHandler('gameEnd',   this.commandGameEnd.bind(this));
		this._commandWindow.setHandler('cancel',    this.popScene.bind(this));
		
		this._commandWindow.setHandler('equip',     this.commandEquip.bind(this));
		this._commandWindow.setHandler('skill',     this.commandSkill.bind(this));
		
		this._commandWindow.setHandler('quest',   	this.commandQuestList.bind(this));
		this._commandWindow.setHandler('world_map',	this.commandWorldMap.bind(this));
				
		this.addWindow(this._commandWindow);
	};
	
	//[スキル]でアクター選択を飛ばす
	Scene_Menu.prototype.commandSkill = function() {
		SceneManager.push(Scene_Skill);
	}
	//[装備]でアクター選択を飛ばす;
	Scene_Menu.prototype.commandEquip = function() {
		SceneManager.push(Scene_Equip);
	};
	
	//[ロード]
	Scene_Menu.prototype.commandLoad = function() {
    	SceneManager.push(Scene_Load);
    };
	
	//[クエスト]
	Scene_Menu.prototype.commandQuestList = function() {
    	SceneManager.push(Scene_MenuQuest);
    };
	
	Scene_Menu.prototype.commandWorldMap = function() {
    	SceneManager.push(Scene_MenuWorldMap);
    };
		

//***** スキル　Scene_Skillの動作制御
	
	var _Scene_Skill_prototype_create=Scene_Skill.prototype.create;
	Scene_Skill.prototype.create = function() {
		_Scene_Skill_prototype_create.call(this);
		
		//window位置調整
		this._statusWindow.y=Graphics.boxHeight;//ステータスウィンドウは画面外へ
		this._itemWindow.y=this._skillTypeWindow.height;
		this._itemWindow.height=Graphics.boxHeight-this._helpWindow.height-this._skillTypeWindow.height;
		this._helpWindow.y=this._itemWindow.y + this._itemWindow.height;
		
		//スキルタイプIDをセットしてから魔法ウィンドウをアクティブ化
		this._itemWindow.setStypeId(1);
		this._itemWindow.activate();
	};
	Scene_Skill.prototype.start = function() {
		Scene_ItemBase.prototype.start.call(this);
		this.refreshActor();
		
		this._itemWindow.selectLast();
		
	};

	//魔法選択ウィンドウでのキャンセルでメインメニューに戻る
	Scene_Skill.prototype.onItemCancel = function() {
		this._itemWindow.deselect();
		SceneManager.pop();
	};
	//スキルタイプウィンドウはダミーウィンドウだけ作っておいて利用しない
	/*Scene_Skill.prototype.createSkillTypeWindow = function() {
		this._skillTypeWindow = new Window_SkillType();
	};*/
	//スキルタイプウィンドウのカスタマイズ
	Window_SkillType.prototype.initialize = function(x, y) {
    	Window_Command.prototype.initialize.call(this, 0, 0);
    	this._actor = null;
	};
	Window_SkillType.prototype.windowWidth = function() {
		return Graphics.boxWidth;
	};
	Window_SkillType.prototype.numVisibleRows = function() {
		return 1;
	};
	Window_SkillType.prototype.maxCols = function() {
		return 1;
	};
	Window_SkillType.prototype.itemTextAlign = function() {
		return 'center';
	};
	Window_SkillType.prototype.updateCursor = function() {
        this.setCursorRect(0, 0, 0, 0);
	};
	
//***** 装備画面の配置調整
	Scene_Equip.prototype.create = function() {
		Scene_MenuBase.prototype.create.call(this);
		this.createHelpWindow();
		this.createCommandWindow();
		this.createStatusWindow();
		this.createSlotWindow();
		this.createItemWindow();
		this._helpWindow.y   = this._itemWindow.y + this._itemWindow.height;
		this.refreshActor();
		
	};
	Scene_Equip.prototype.createCommandWindow = function() {
		var wx = 0;
		var wy = 0;
		var ww = Graphics.boxWidth;
		this._commandWindow = new Window_EquipCommand(wx, wy, ww);
		this._commandWindow.setHelpWindow(this._helpWindow);
		this._commandWindow.setHandler('equip',    this.commandEquip.bind(this));
		this._commandWindow.setHandler('optimize', this.commandOptimize.bind(this));
		this._commandWindow.setHandler('clear',    this.commandClear.bind(this));
		this._commandWindow.setHandler('cancel',   this.popScene.bind(this));
		this._commandWindow.setHandler('pagedown', this.nextActor.bind(this));
		this._commandWindow.setHandler('pageup',   this.previousActor.bind(this));
		this.addWindow(this._commandWindow);
	};
	
	Scene_Equip.prototype.createStatusWindow = function() {
		this._statusWindow = new Window_EquipStatus(Graphics.boxWidth - 312, this._commandWindow.height);
		this.addWindow(this._statusWindow);
	};
	
	Scene_Equip.prototype.createSlotWindow = function() {
		var wx = 0;
		var wy = this._commandWindow.height;
		var ww = Graphics.boxWidth - this._statusWindow.width;
		var wh = this._statusWindow.height;
		this._slotWindow = new Window_EquipSlot(wx, wy, ww, wh);
		this._slotWindow.setHelpWindow(this._helpWindow);
		this._slotWindow.setStatusWindow(this._statusWindow);
		this._slotWindow.setHandler('ok',       this.onSlotOk.bind(this));
		this._slotWindow.setHandler('cancel',   this.onSlotCancel.bind(this));
		this.addWindow(this._slotWindow);
	};
	
	Scene_Equip.prototype.createItemWindow = function() {
		var wx = 0;
		var wy = this._statusWindow.y + this._statusWindow.height;
		var ww = Graphics.boxWidth;
		var wh = Graphics.boxHeight - wy - this._helpWindow.height;
		this._itemWindow = new Window_EquipItem(wx, wy, ww, wh);
		this._itemWindow.setHelpWindow(this._helpWindow);
		this._itemWindow.setStatusWindow(this._statusWindow);
		this._itemWindow.setHandler('ok',     this.onItemOk.bind(this));
		this._itemWindow.setHandler('cancel', this.onItemCancel.bind(this));
		this._slotWindow.setItemWindow(this._itemWindow);
		this.addWindow(this._itemWindow);
	};

	//***** 装備ステータスWindowのサイズ調整
	Window_EquipStatus.prototype.numVisibleRows = function() {
		return 6;
	};
	Window_EquipStatus.prototype.refresh = function() {
		this.contents.clear();
		if (this._actor) {
			for (var i = 0; i < 6; i++) {
				this.drawItem(0, this.lineHeight() * i, 2 + i);
			}
		}
	};
	Window_EquipStatus.prototype.standardFontSize = function() {
		return 20;//defo 28
	};
	Window_EquipStatus.prototype.lineHeight = function() {
		return 26;//defo 36
	};
	
	
	//***** 装備スロットWindowのサイズ調整
	Window_EquipSlot.prototype.standardFontSize = function() {
		return 24;//defo 28
	};
	Window_EquipSlot.prototype.lineHeight = function() {
		return 30;//defo 36
	};
	Window_EquipSlot.prototype.drawIcon = function(iconIndex, x, y) {
		var bitmap = ImageManager.loadSystem('IconSet');
		var pw = Window_Base._iconWidth;
		var ph = Window_Base._iconHeight;
		var sx = iconIndex % 16 * pw;
		var sy = Math.floor(iconIndex / 16) * ph;
		this.contents.blt(bitmap, sx, sy, pw, ph, x, y, pw * 0.8 ,ph * 0.8);
	};

//***** 王国マップ メニュー
	function Scene_MenuWorldMap() {
		this.initialize.apply(this, arguments);
	}

	Scene_MenuWorldMap.prototype = Object.create(Scene_MenuBase.prototype);
	Scene_MenuWorldMap.prototype.constructor = Scene_Menu;
	
	Scene_MenuWorldMap.prototype.create = function() {
		Scene_MenuBase.prototype.create.call(this);
		this.createMapWindow();
		this.createComandWindow();
	};
	
	Scene_MenuWorldMap.prototype.createMapWindow = function(){
		this._worldMapWindow = new Window_WorldMap();
		this.addWindow(this._worldMapWindow);
	};
	Scene_MenuWorldMap.prototype.createComandWindow = function(){
		this._commandWindow = new Window_WorldMapCommand();
		this._commandWindow.setHandler('ok',		this.popScene.bind(this));
		this._commandWindow.setHandler('cancel',this.popScene.bind(this));
		this.addWindow(this._commandWindow);
		
	};
	
	//マップを表示するだけのウィンドウ
	//プラグイン「WindowBackImages」で背景差し替え
	function Window_WorldMap() {
        this.initialize.apply(this, arguments);
	}
    Window_WorldMap.prototype             = Object.create(Window_Base.prototype);
    Window_WorldMap.prototype.constructor = Window_WorldMap;

    Window_WorldMap.prototype.initialize = function() {
        Window_Base.prototype.initialize.call(this, Graphics.boxWidth/2, Graphics.boxHeight/2);
    };
	Window_WorldMap.prototype.windowWidth = function() {
		return Graphics.boxWidth;
	};
	Window_WorldMap.prototype.windowHeight = function() {
		return Graphics.boxHeight;
	};
	
	//マップを閉じるためのコマンドウィンドウ
	function Window_WorldMapCommand() {
        this.initialize.apply(this, arguments);
	}
    Window_WorldMapCommand.prototype             = Object.create(Window_Command.prototype);
    Window_WorldMapCommand.prototype.constructor = Window_WorldMapCommand;
	
	 Window_WorldMapCommand.prototype.initialize = function() {
        Window_Command.prototype.initialize.call(this, 0,0);
		this.activate();
		this.visible = false;
		this.setBackgroundType(2);//枠なし
    };
	
	Window_WorldMapCommand.prototype.processTouch = function() {
		if (this.isOpenAndActive()) {
			if (TouchInput.isTriggered()) {
				this._touching = true;
				this.onTouch(true);
			} else if (TouchInput.isCancelled()) {
				if (this.isCancelEnabled()) {
					this.processCancel();
				}
			}
			if (this._touching) {
				if (TouchInput.isPressed()) {
					this.onTouch(false);
				} else {
					this._touching = false;
				}
			}
		} else {
			this._touching = false;
		}
	};

	Window_WorldMapCommand.prototype.onTouch = function(triggered) {
		if (triggered && this.isTouchOkEnabled()) {
			this.processOk();
		}
	};

		
	Window_WorldMapCommand.prototype.isCurrentItemEnabled = function() {
		return true;
	};
	Window_WorldMapCommand.prototype.playOkSound = function() {
		AudioManager.playSe({'name':'Book1','volume':90,'pitch':100,'pan':0});
	};
	
	Window_WorldMapCommand.prototype.processCancel = function() {
		AudioManager.playSe({'name':'Book1','volume':90,'pitch':100,'pan':0});
		this.updateInputData();
		this.deactivate();
		this.callCancelHandler();
	};


//***** プラグインコマンド	
	var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
	Game_Interpreter.prototype.pluginCommand = function(command, args) {
		_Game_Interpreter_pluginCommand.call(this, command, args);
		switch (command) {
		case 'backToItemMenu':
			if(!$gameParty.inBattle()){
			   $back_to_itemMenu = true;
			   SceneManager.push(Scene_Item);
			}
			break;
		}
	};

	
})();

//**** メニュータイトルウィンドウ
//各メニュー画面のトップに表示される一行タイトルウィンドウ
function Window_MenuTitle() {
	this.initialize.apply(this, arguments);
}

Window_MenuTitle.prototype = Object.create(Window_Base.prototype);
Window_MenuTitle.prototype.constructor = Window_MenuTitle;
Window_MenuTitle.prototype.initialize = function(text) {
    var width = Graphics.boxWidth;
    var height = this.fittingHeight(1);
    Window_Base.prototype.initialize.call(this, 0, 0, width, height);
    this.drawText(text, 0, 0, width - (this.standardPadding()*2), 'center');
};
