//=============================================================================
// Phileas_OptionsManager.js
//=============================================================================
// [Update History]
// 2023.July.23 Ver1.0.0 First Release
// 2023.July.24 Ver1.1.0 Fixed a bug hiding standard options
// Automatic correction of incorrect variable values.
// Now you can specify the status text of the variable.
/*:
* @target MZ
* @plugindesc Configures the options menu, adds an unlimited number of custom options.
* @author Phileas
*
* @param Options
* @type struct<Option>[]
* @default []
*
* @param Fullscreen option
* @type struct<Fullscreen>
* @default {"Add 'Fullscreen' option?":"false","Fullscreen option name":"Fullscreen","Position":"Bottom"}
*
* @param Show 'Always Dash' option?
* @type boolean
* @default true
*
* @param Show 'Command Remember' option?
* @type boolean
* @default true
*
* @param Show 'Touch UI' option?
* @type boolean
* @default true
*
* @param Show 'BGM Volume' option?
* @type boolean
* @default true
*
* @param Show 'BGS Volume' option?
* @type boolean
* @default true
*
* @param Show 'ME Volume' option?
* @type boolean
* @default true
*
* @param Show 'SE Volume' option?
* @type boolean
* @default true
*
* @param Volume offset
* @type number
* @default 20
* @desc The value by which the volume changes in one click.
*
* @help
* The plugin does not provide commands.
* The options menu is configured via the plugin parameters.
*
* Custom options are set in the plugin parameters via the structure. Each option adjusts a switch or variable.
* If a switch is selected in the option settings, the option will adjust it. Otherwise, you need to select a variable!
* When selecting a variable, you can adjust the minimum and maximum values, as well as offset.
* You can choose whether to place the new option at the beginning, middle or end of the menu.
*
* You can also add the Fullscreen command to the options menu and input any name for it.
* If you enable the Remember option in the Fullscreen structure, the plugin will remember the state of the fullscreen (even if the option is hidden from the options menu).
* That is, if the fullscreen was turned on, when restarting the game will start in fullscreen!
*
* You can hide standard options with a number of options that start with the word Show.
* You can also use Volume offset to adjust the step length when changing the volume of music and sounds.
*
* You can always write to the author if you need other features or even plugins.
* Boosty: https://boosty.to/phileas
* RPG Maker Web: https://forums.rpgmakerweb.com/index.php?members/phileas.176075/
* RPG Maker Union: https://rpgmakerunion.ru/id/phileas
* Email: olek.olegovich gmail.com
* Telegram: olekolegovich
*
* [License]
* This plugin is released under MIT license.
* http://opensource.org/licenses/mit-license.php
*/
/*~struct~Option:
* @param Option name
* @type string
* @default Option
*
* @param Option switch
* @type switch
* @desc If the option should control the switch, select it here. Otherwise, do not assign a switch to the option in any case.
*
* @param Option variable
* @type variable
* @desc If the option should control the variable, select it here.
*
* @param Variable min value
* @type number
* @desc If it is variable option.
* @default 0
*
* @param Variable max value
* @type number
* @desc If it is variable option.
* @default 100
*
* @param Variable offset
* @type number
* @desc If it is variable option. The value by which the variable changes in one click.
* @default 20
*
* @param Variable status text
* @desc The text that will be written to the right of the variable value. You can input an empty string.
* @default %
*
* @param Position
* @desc The position of the option in the options menu.
* @type combo
* @option Top
* @option Middle
* @option Bottom
* @default Bottom
*/
/*~struct~Fullscreen:
* @param Add 'Fullscreen' option?
* @type boolean
* @default false
* @desc Adds a fullscreen option to the Options window.
*
* @param Fullscreen option name
* @default Fullscreen
*
* @param Position
* @desc The position of the option in the options menu.
* @type combo
* @option Top
* @option Middle
* @option Bottom
* @default Bottom
*
* @param Remember
* @type boolean
* @default true
* @desc If the value is true, the option value will be restored when the game is restarted.
*/
(function() {
//--------DATA:
var parameters = PluginManager.parameters("Phileas_OptionsManager");
var customOptions = parsePluginParamArray(parameters["Options"]) || [];
var topCustomOptions = [];
var middleCustomOptions = [];
var bottomCustomOptions = [];
var fullscreenOptions = getFullsreenOptionArray(JSON.parse(parameters["Fullscreen option"]) || {"Add 'Fullscreen' option?":false,"Fullscreen option name":"Fullscreen","Position":"Bottom"});
var showAlwaysDash = parameters["Show 'Always Dash' option?"] == "true";
var showCommandRemember = parameters["Show 'Command Remember' option?"] == "true";
var showTouchUI = parameters["Show 'Touch UI' option?"] == "true";
var showBGMVolume = parameters["Show 'BGM Volume' option?"] == "true";
var showBGSVolume = parameters["Show 'BGS Volume' option?"] == "true";
var showMEVolume = parameters["Show 'ME Volume' option?"] == "true";
var showSEVolume = parameters["Show 'SE Volume' option?"] == "true";
var volumeOffset = Number(parameters["Volume offset"]) || 20;
setCustomOptionsDictionaries();
//--------MY CODE:
function getClamp(number, min, max) {
return Math.min(Math.max(number, min), max);
}
function parsePluginParamArray(data) {
if (data == undefined) {
return undefined;
}
let arr = JSON.parse(data);
for (let i = 0; i < arr.length; ++i) {
arr[i] = JSON.parse(arr[i]);
}
return arr;
}
function getFullsreenOptionArray(dict) {
let pos = dict["Position"];
switch (pos) {
case "Top":
pos = 0;
break;
case "Middle":
pos = 1;
break;
default:
pos = 2;
break;
}
return [dict["Add 'Fullscreen' option?"] == "true", dict["Fullscreen option name"], pos, dict["Remember"] == "true"];
}
function setCustomOptionsDictionaries() {
let dict = {};
for (let i in customOptions) {
let opt = customOptions[i];
let tag = "phileasCustomOption" + i;
let isSwitchOption = opt["Option switch"] != "";
opt["Option switch"] = Number(opt["Option switch"]);
opt["Option variable"] = Number(opt["Option variable"]);
opt["Variable min value"] = Number(opt["Variable min value"]);
opt["Variable max value"] = Number(opt["Variable max value"]);
opt["Variable offset"] = Number(opt["Variable offset"]);
if (isSwitchOption) {
tag += "Switch";
dict[tag] = [true, opt["Option switch"]];
}
else {
tag += "Volume";
dict[tag] = [false, opt["Option variable"], opt["Variable min value"], opt["Variable max value"], opt["Variable offset"], opt["Variable status text"]];
}
switch (opt["Position"]) {
case "Top":
topCustomOptions.push([tag, opt["Option name"], isSwitchOption]);
break;
case "Middle":
middleCustomOptions.push([tag, opt["Option name"], isSwitchOption]);
break;
default:
bottomCustomOptions.push([tag, opt["Option name"], isSwitchOption]);
break;
}
}
customOptions = dict;
}
Window_Options.prototype.addFullscreenOption = function(pos) {
if (fullscreenOptions[0] === true && fullscreenOptions[2] == pos) {
this.addCommand(fullscreenOptions[1], "fullscreen");
}
}
Window_Options.prototype.addCustomOptions = function(pos) {
let arr = undefined;
switch (pos) {
case 0:
arr = topCustomOptions;
break;
case 1:
arr = middleCustomOptions;
break;
default:
arr = bottomCustomOptions;
break;
}
for (let i in arr) {
this.addCommand(arr[i][1], arr[i][0]);
let co = customOptions[arr[i][0]];
let n = co[1];
let value = this.getConfigValue(arr[i][0]);
if (value == undefined) {
if (arr[i][2] === true) {
value = $gameSwitches.value(n);
}
else {
const varValue = $gameVariables.value(n);
if (varValue < co[2]) {
$gameVariables.setValue(n, co[2]);
}
else if (varValue > co[3]) {
$gameVariables.setValue(n, co[3]);
}
value = $gameVariables.value(n);
}
this.changeValue(arr[i][0], value);
}
}
}
//--------CHANGED CORE:
Window_Options.prototype.makeCommandList = function() {
this.addFullscreenOption(0);
this.addCustomOptions(0);
this.addGeneralOptions();
this.addFullscreenOption(1);
this.addCustomOptions(1);
this.addVolumeOptions();
this.addFullscreenOption(2);
this.addCustomOptions(2);
};
Window_Options.prototype.addGeneralOptions = function() {
if (showAlwaysDash) {
this.addCommand(TextManager.alwaysDash, "alwaysDash");
}
if (showCommandRemember) {
this.addCommand(TextManager.commandRemember, "commandRemember");
}
if (showTouchUI) {
this.addCommand(TextManager.touchUI, "touchUI");
}
};
Window_Options.prototype.addVolumeOptions = function() {
if (showBGMVolume) {
this.addCommand(TextManager.bgmVolume, "bgmVolume");
}
if (showBGSVolume) {
this.addCommand(TextManager.bgsVolume, "bgsVolume");
}
if (showMEVolume) {
this.addCommand(TextManager.meVolume, "meVolume");
}
if (showSEVolume) {
this.addCommand(TextManager.seVolume, "seVolume");
}
};
const Origin_changeVolume = Window_Options.prototype.changeVolume;
Window_Options.prototype.changeVolume = function(symbol, forward, wrap) {
let opt = customOptions[symbol];
if (opt == undefined || opt[0] == true) {
Origin_changeVolume.call(this, symbol, forward, wrap);
return;
}
const lastValue = this.getConfigValue(symbol);
const offset = opt[4];
let value = lastValue + (forward ? offset : -offset);
if (value > opt[3] && wrap) {
value = opt[2];
} else {
value = getClamp(value, opt[2], opt[3]);
}
this.changeValue(symbol, value);
$gameVariables.setValue(opt[1], value);
};
Window_Options.prototype.volumeOffset = function() {
return volumeOffset;
};
const Origin_processOk = Window_Options.prototype.processOk;
Window_Options.prototype.processOk = function() {
const index = this.index();
const symbol = this.commandSymbol(index);
if (symbol == "fullscreen") {
Graphics._switchFullScreen();
}
else {
let opt = customOptions[symbol];
if (opt != undefined && opt[0] == true) {
$gameSwitches.setValue(opt[1], !$gameSwitches.value(opt[1]));
}
Origin_processOk.call(this);
}
};
Window_Options.prototype.statusText = function(index) {
const symbol = this.commandSymbol(index);
const value = this.getConfigValue(symbol);
if (this.isVolumeSymbol(symbol)) {
let opt = customOptions[symbol];
return opt != undefined ? value + opt[5] : this.volumeStatusText(value);
}
return this.booleanStatusText(value);
};
Origin_switchFullscreen = Graphics._switchFullScreen;
Graphics._switchFullScreen = function() {
if (SceneManager._scene instanceof Scene_Options) {
for (var i = 0; i < SceneManager._scene._windowLayer.children.length; ++i) {
if (SceneManager._scene._windowLayer.children[i] instanceof Window_Options) {
SceneManager._scene._windowLayer.children[i].changeValue("fullscreen", !ConfigManager["fullscreen"]);
break;
}
}
}
else {
ConfigManager["fullscreen"] = !ConfigManager["fullscreen"];
}
ConfigManager.save();
Origin_switchFullscreen.call(this);
};
Origin_makeData = ConfigManager.makeData;
ConfigManager.makeData = function() {
const config = Origin_makeData.call(this);
config.fullscreen = fullscreenOptions[3] === true ? ConfigManager["fullscreen"] : undefined;
for (tag in customOptions) {
let opt = customOptions[tag];
customOptions[tag].push(opt[0] ? $gameSwitches.value(opt[1]) : $gameVariables.value(opt[1]));
}
config.phileasCustomOption = customOptions;
return config;
};
Origin_applyData = ConfigManager.applyData;
ConfigManager.applyData = function(config) {
if (config.fullscreen != undefined) {
ConfigManager["fullscreen"] = config.fullscreen;
}
if (config.phileasCustomOption != undefined) {
for (tag in config.phileasCustomOption) {
let opt = config.phileasCustomOption[tag];
ConfigManager[tag] = opt[opt.length - 1];
}
}
Origin_applyData.call(this, config);
};
const Origin_SceneBoot_start = Scene_Boot.prototype.start;
Scene_Boot.prototype.start = function () {
Origin_SceneBoot_start.call(this);
if (ConfigManager["fullscreen"] === true) {
Graphics._requestFullScreen();
}
};
const Origin_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
Origin_createGameObjects.call(this);
for (tag in customOptions) {
let opt = customOptions[tag];
if (opt[0] === true) {
$gameSwitches.setValue(opt[1], ConfigManager[tag]);
}
else {
$gameVariables.setValue(opt[1], ConfigManager[tag]);
}
}
}
}());