
/*:ru
 * @plugindesc v.1.0 Изменяет опции
 * @author DK (Денис Кузнецов)
 * @help Открыть файл и настроить команды
*/

var Switch_In_Options = {};
var Switch_In_Cheats = {};
var Commands_In_Cheats = {};

//===========================================================================
// Настройки плагина
// Plugin settings
//===========================================================================

// Настройки переключателей в опциях
// Switch_In_Options[Название команды] = Switch ID
/*
Switch_In_Options['Some option 1'] = 14;
Switch_In_Options['Some option 2'] = 15;
Switch_In_Options['Some option 3'] = 16;
*/
// Настройки переключателей в читах
// Switch_In_Cheats[Название команды] = { show_id: Switch ID, switch_id: Switch ID }
// show_id: номер переключателя, который отвечает за отображение названия команды
// switch_id: номер переключателя, который отвечает за включение команды ON/OFF
// запятая для последней команды не ставится
Switch_In_Cheats['Some option 1'] = { show_id: 20, switch_id: 17 },
Switch_In_Cheats['Some option 2'] = { show_id: 21, switch_id: 18 },
Switch_In_Cheats['Some option 3'] = { show_id: 22, switch_id: 19 }

// Настройки комманд в читах
// Commands_In_Cheats[Название команды] = { show_id: Switch ID, common_event_id: Common Event ID }
// show_id: номер переключателя, который отвечает за отображение названия команды
// common_event_id: номер общего события, которое резервируется для карты игры
// запятая для последней команды не ставится
Commands_In_Cheats['Some option 4'] = { show_id: 26, common_event_id: 10 },
Commands_In_Cheats['Some option 5'] = { show_id: 27, common_event_id: 11 },
Commands_In_Cheats['Some option 6'] = { show_id: 28, common_event_id: 12 }

//===========================================================================
// Конец настройки плагина
// End of plugin settings
//===========================================================================

//===========================================================================
// Game Temp
//===========================================================================

var Options_Game_Temp_initialize = Game_Temp.prototype.initialize;
Game_Temp.prototype.initialize = function() {
	Options_Game_Temp_initialize.call(this);
    this._commonEventId = [];
};

Game_Temp.prototype.reserveCommonEvent = function(commonEventId) {
	if (this._commonEventId.contains(commonEventId)) return;
    this._commonEventId.push(commonEventId);
};

Game_Temp.prototype.clearCommonEvent = function() {
};

Game_Temp.prototype.isCommonEventReserved = function() {
    return this._commonEventId.length > 0;
};

Game_Temp.prototype.reservedCommonEvent = function() {
    return $dataCommonEvents[this._commonEventId.shift()];
};

//===========================================================================
// Scene Options
//===========================================================================

var Options_Scene_Options_create = Scene_Options.prototype.create;
Scene_Options.prototype.create = function() {
    Options_Scene_Options_create.call(this);
    this.createCheatsWindow();
};

var Options_Scene_Options_createOptionsWindow = Scene_Options.prototype.createOptionsWindow;
Scene_Options.prototype.createOptionsWindow = function() {
	Options_Scene_Options_createOptionsWindow.call(this);
    this._optionsWindow.setHandler('cheats', this.handlerOptionsCheats.bind(this));
};

Scene_Options.prototype.createCheatsWindow = function() {
	this.cheats_window = new Window_Cheats();
	this.cheats_window.setHandler('cancel', this.handlerCheatsCancel.bind(this));
	this.cheats_window.deactivate();
	this.cheats_window.hide();
	this.addWindow(this.cheats_window);
};

Scene_Options.prototype.handlerOptionsCheats = function() {
	this._optionsWindow.deactivate();
	this._optionsWindow.hide();
	this._optionsWindow.sliders[0].deactivate();
	this._optionsWindow.sliders[1].deactivate();
	this.cheats_window.refresh();
	this.cheats_window.activate();
	this.cheats_window.show();
};

Scene_Options.prototype.handlerCheatsCancel = function() {
	this._optionsWindow.activate();
	this._optionsWindow.show();
	this._optionsWindow.sliders[0].activate();
	this._optionsWindow.sliders[1].activate();
	this.cheats_window.deactivate();
	this.cheats_window.hide();
};

var Options_Scene_Options_update = Scene_Options.prototype.update;
Scene_Options.prototype.update = function() {
	Options_Scene_Options_update.call(this);
	if (this.touch_input) return;
	TouchInput.update();
	this._optionsWindow.sliders[0].activate();
	this._optionsWindow.sliders[1].activate();
	this.touch_input = true;
}

//===========================================================================
// Window Options
//===========================================================================

var Options_Window_Options_initialize = Window_Options.prototype.initialize;
Window_Options.prototype.initialize = function() {
	Options_Window_Options_initialize.call(this);
	this.addVolumeOptions();
};

Window_Options.prototype.generalOptionsLength = function() {
	return 2;
}

Window_Options.prototype.switchesLength = function() {
	return Object.keys(Switch_In_Options).length;
};

Window_Options.prototype.volumeLength = function() {
	return 4;
};

Window_Options.prototype.cheatsLength = function() {
	return 1;
};

Window_Options.prototype.windowHeight = function() {
	return this.lineHeight() * (this.generalOptionsLength() + this.switchesLength() + this.volumeLength() + this.cheatsLength()) + this.standardPadding() * 2 + 50;
};

Window_Options.prototype.makeCommandList = function() {
	this.addGeneralOptions();
	this.addSwitches();
	this.addCheats();
};

Window_Options.prototype.addSwitches = function() {
	for(var key in Switch_In_Options) this.addCommand(key, key);
};

Window_Options.prototype.addVolumeOptions = function() {
	var text = ['Music Volume', 'Sound Volume'];
	var symbols = ['bgmVolume', 'bgsVolume'];
	this.sliders = [];
	var standard_padding = this.standardPadding();
	var line_height = this.lineHeight();
	var x = standard_padding;
	var y = line_height * (this.generalOptionsLength() + this.switchesLength() + 1) + standard_padding + 40;
	var dy = line_height * 2;
	for(var i = 0; i < text.length; i++)
	{
		var symbol = symbols[i];
		var slider = new DK_Options_Slider(x, y + i * dy, this.contentsWidth());
		slider.setupId(i);
		slider.deactivate();
		slider.setupValue(ConfigManager[symbol]);
		slider.setClickHandler(this.sliderClick.bind(this));
		slider.start();
		this.sliders.push(slider);
		this.addChild(slider);
		this.drawText(text[i], 0, y + i * dy - line_height - standard_padding, this.contentsWidth(), 'center');
	}
};

Window_Options.prototype.sliderClick = function(value, id) {
	var symbols = [['bgmVolume', 'meVolume'], ['bgsVolume', 'seVolume']];
	var array = symbols[id];
	for(var i = 0; i < array.length; i++)
	{
		var symbol = array[i];
		ConfigManager[symbol] = value;
	}
};

Window_Options.prototype.addCheats = function() {
	this.addCommand('Cheats', 'cheats');
};

Window_Options.prototype.itemRect = function(index) {
    var rect = new Rectangle();
    var maxCols = this.maxCols();
    rect.width = this.itemWidth();
    rect.height = this.itemHeight();
    rect.x = index % maxCols * (rect.width + this.spacing());
    rect.y = Math.floor(index / maxCols) * rect.height;
	if (index === this.maxItems() - 1)
		rect.y = this.windowHeight() - this.lineHeight() - this.standardPadding() * 2;
    return rect;
};

Window_Options.prototype.drawItem = function(index) {
    var rect = this.itemRectForText(index);
    var statusWidth = this.statusWidth();
    var titleWidth = rect.width - statusWidth;
    this.resetTextColor();
    this.changePaintOpacity(this.isCommandEnabled(index));
    this.drawText(this.commandName(index), rect.x, rect.y, titleWidth, 'left');
	if (index !== this.maxItems() - 1)
		this.drawText(this.statusText(index), titleWidth, rect.y, statusWidth, 'right');
};

var Options_Window_Options_booleanStatusText = Window_Options.prototype.booleanStatusText;
Window_Options.prototype.booleanStatusText = function(value) {
	if ($gameSystem.isRussian())
		return value ? 'ВКЛ' : 'ВЫКЛ';
	else
		return Options_Window_Options_booleanStatusText.call(this, value);
};

var Options_Window_Options_getConfigValue = Window_Options.prototype.getConfigValue;
Window_Options.prototype.getConfigValue = function(symbol) {
	var volume = Options_Window_Options_getConfigValue.call(this, symbol);
	if (!Switch_In_Options[symbol]) return volume;
	return $gameSwitches.value(Switch_In_Options[symbol]);
};

var Options_Window_Options_setConfigValue = Window_Options.prototype.setConfigValue;
Window_Options.prototype.setConfigValue = function(symbol, volume) {
	Options_Window_Options_setConfigValue.call(this, symbol, volume);
	if (!Switch_In_Options[symbol]) return;
	$gameSwitches.setValue(Switch_In_Options[symbol], volume);
};

var Options_Window_Options_processOk = Window_Options.prototype.processOk;
Window_Options.prototype.processOk = function() {
	var index = this.index();
    var symbol = this.commandSymbol(index);
	if (symbol === 'cheats') return Window_Command.prototype.processOk.call(this);
	Options_Window_Options_processOk.call(this);
};

//===========================================================================
// Window Cheats
//===========================================================================

function Window_Cheats() {
    this.initialize.apply(this, arguments);
}

Window_Cheats.prototype = Object.create(Window_Command.prototype);
Window_Cheats.prototype.constructor = Window_Cheats;

Window_Cheats.prototype.initialize = function() {
    Window_Command.prototype.initialize.call(this, 0, 0);
    this.updatePlacement();
};

Window_Cheats.prototype.windowWidth = function() {
    return 400;
};

Window_Cheats.prototype.updatePlacement = Window_Options.prototype.updatePlacement;

Window_Cheats.prototype.switchesLength = function() {
	return Object.keys(Switch_In_Cheats).length;
};

Window_Cheats.prototype.makeCommandList = function() {
	this.addSwitches();
	this.addCommands();
};

Window_Cheats.prototype.addSwitches = function() {
	for(var key in Switch_In_Cheats) this.addCommand(key, key);
};

Window_Cheats.prototype.addCommands = function() {
	for(var key in Commands_In_Cheats) this.addCommand(key, key);
};

Window_Cheats.prototype.booleanStatusText = Window_Options.prototype.booleanStatusText;

Window_Cheats.prototype.commandName = function(index) {
	var symbol = this.commandSymbol(index);
	if (index < this.switchesLength())
		var obj = Switch_In_Cheats[symbol];
	else
		var obj = Commands_In_Cheats[symbol];
	return $gameSwitches.value(obj.show_id) ? symbol : '???';
};

Window_Cheats.prototype.cheatEnabled = function(obj) {
	return $gameSwitches.value(obj.show_id);
};

Window_Cheats.prototype.drawItem = function(index) {
	Window_Command.prototype.drawItem.call(this, index);
	if (index >= this.switchesLength()) return;
	var rect = this.itemRectForText(index);
	var symbol = this.commandSymbol(index);
	var obj = Switch_In_Cheats[symbol];
	if (!this.cheatEnabled(obj)) return;
	var text = this.booleanStatusText($gameSwitches.value(obj.switch_id));
	this.drawText(text, 0, rect.y, this.contentsWidth(), 'right');
};

Window_Cheats.prototype.processOk = function() {
	var index = this.index();
	var symbol = this.commandSymbol(index);
	if (index < this.switchesLength())
	{
		var obj = Switch_In_Cheats[symbol];
		if (!this.cheatEnabled(obj)) return SoundManager.playBuzzer();
		var id = obj.switch_id;
		var value = $gameSwitches.value(id);
		$gameSwitches.setValue(id, !value);
		this.redrawItem(this.findSymbol(symbol));
	}
	else
	{
		var obj = Commands_In_Cheats[symbol];
		if (!this.cheatEnabled(obj)) return SoundManager.playBuzzer();
		$gameTemp.reserveCommonEvent(obj.common_event_id);   
	}
	SoundManager.playOk();
};

//===========================================================================
// DK Options Slider
//===========================================================================

function DK_Options_Slider() {
	this.initialize.apply(this, arguments);
};

DK_Options_Slider.prototype = Object.create(DKTools_Slider.prototype);
DK_Options_Slider.prototype.constructor = DKTools_Slider;

DK_Options_Slider.prototype.minHeight = function() {
	return 24;
};

DK_Options_Slider.prototype.sliderX = function() {
	return 0;
};

DK_Options_Slider.prototype.sliderWidth = function() {
	return this.bitmap_width;
};

DK_Options_Slider.prototype.createSlider = function() {
	this.removeSlider();
	this.bitmap = new Bitmap(this.bitmap_width, this.bitmap_height);
	this.fillRect(this.slider_color);
};

//===========================================================================
// DataManager Manager
//===========================================================================

var Options_DataManager_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
	Options_DataManager_createGameObjects.call(this);
	ConfigManager.load();
};

//===========================================================================
// Config Manager
//===========================================================================

var Options_ConfigManager_makeData = ConfigManager.makeData;
ConfigManager.makeData = function() {
    var config = Options_ConfigManager_makeData.call(this);
	for(var key in Switch_In_Options) config[key] = $gameSwitches.value(Switch_In_Options[key]);
    return config;
};

var Options_ConfigManager_applyData = ConfigManager.applyData;
ConfigManager.applyData = function(config) {
	Options_ConfigManager_applyData.call(this, config);
	if (!$gameSwitches) return;
	for(var key in Switch_In_Options)
		$gameSwitches.setValue(Switch_In_Options[key], this.readFlag(config, key));
};