// by VienDesu! Poring Team 2024


//=============================================================================
// ** ONSControls
//=============================================================================
class ONSControls {};
//=============================================================================
// * Plugin options
//=============================================================================
ONSControls.options = PluginManager.parameters('VND_ONSControls');
//=============================================================================
// * Controls canvas
//=============================================================================
ONSControls.createCanvas = function() {
    const canvas = new PIXI.Application({
        width: innerWidth,
        height: innerHeight,
        autoResize: true,
        resolution: devicePixelRatio,
        transparent: true
    });
    document.body.appendChild(canvas.view);
    canvas.view.id = "ControlsCanvas";
    canvas.view.style.zIndex = this.options.zIndex;
    canvas.view.style.position = "absolute";
    canvas.view.style.left = 0;
    canvas.view.style.right = 0;
    canvas.view.style.down = 0;
    canvas.view.style.top = 0;
    canvas.stage.interactive = true;
    var idleInterval = setInterval(ONSControls.playIdleAnimation, 60000);
    canvas.stage.on("pointerdown", () => {
        this.stopIdleAnimation();
        clearInterval(idleInterval);
        idleInterval = setInterval(ONSControls.playIdleAnimation, 60000);
    })
    canvas.vh = (persent) => {
        return canvas.screen.height * persent;
    }
    this._controlsCanvas = canvas;
}
//=============================================================================
// * A/B/X/Y Buttons
//=============================================================================
ONSControls.createButtons = function() {
    const container = new PIXI.Container();
    const buttons = [
        new PIXI.Sprite.fromImage("js/porting/assets/omori_a_button.png"), 
        new PIXI.Sprite.fromImage("js/porting/assets/omori_x_button.png"), 
        new PIXI.Sprite.fromImage("js/porting/assets/omori_b_button.png"),
        new PIXI.Sprite.fromImage("js/porting/assets/omori_y_button.png")
    ]
    buttons[0].on("pointerdown", (event) => {this.sendEvent(event, "BUTTON_A")})
              .on("pointerup", (event) => {this.sendEvent(event, "BUTTON_A")});
    buttons[1].on("pointerdown", (event) => {this.sendEvent(event, "BUTTON_X")})
              .on("pointerup", (event) => {this.sendEvent(event, "BUTTON_X")});
    buttons[2].on("pointerdown", (event) => {this.sendEvent(event, "BUTTON_B")})
              .on("pointerup", (event) => {this.sendEvent(event, "BUTTON_B")});
    buttons[3].on("pointerdown", (event) => {this.sendEvent(event, "BUTTON_Y")})
              .on("pointerup", (event) => {this.sendEvent(event, "BUTTON_Y")});
    for (btn of buttons) {
        btn.anchor.set(0.5);
        btn.hitArea = new PIXI.Circle(0, 0, 40);
        container.addChild(btn);
    }
    this._controlsCanvas.stage.addChild(container);
    this._buttonsContainer = container;
}
//=============================================================================
// * DPad Buttons
//=============================================================================
ONSControls.createDPad = function() {
    const container = new PIXI.Sprite.fromImage("js/porting/assets/omori_dpad.png");
    container.anchor.set(0.5);
    container.interactive = true;
    container.cursor = "pointer";
    container.pressed = false;
    container.pressed_button = null;
    container.down = function(x, y) {
        if (Math.abs(x) > container.width / 2 || Math.abs(y) > container.height / 2) {
            return; // Pointer out of dpad, ignoring event
        }
        var threshold = container.width / 5;
        var button;
        if (y < -threshold) {
            button = "DPAD_UP";
        } else if (y > threshold) {
            button = "DPAD_DOWN";
        }
        if (x < -threshold) {
            button = "DPAD_LEFT";
        } else if (x > threshold) {
            button = "DPAD_RIGHT";
        }
        if (button !== container.pressed_button && button !== undefined) {
            if (container.pressed_button !== null) {
                ONSControls.sendEvent({type: "pointerup"}, container.pressed_button);
            }
            container.pressed = true;
            container.pressed_button = button;
            ONSControls.sendEvent({type: "pointerdown"}, button);
        }
    }
    container.upAll = function() {
        container.pressed = false;
        container.pressed_button = null;
        ONSControls.sendEvent({type: "pointerup"}, "DPAD_RIGHT");
        ONSControls.sendEvent({type: "pointerup"}, "DPAD_DOWN");
        ONSControls.sendEvent({type: "pointerup"}, "DPAD_UP");
        ONSControls.sendEvent({type: "pointerup"}, "DPAD_LEFT");
    }
    container.on("pointerup", () => {container.upAll();})
    container.on("pointerupoutside", () => {container.upAll();})
    container.on("pointerdown", (event) => {
        var x = event.data.global.x - this._dPadContainer.x;
        var y = event.data.global.y - this._dPadContainer.y;
        container.down(x, y);
    })
    container.on("pointermove", (event) => {
        if (container.pressed) {
            // DPad pressed, set up button
            var x = event.data.global.x - this._dPadContainer.x;
            var y = event.data.global.y - this._dPadContainer.y;
            container.down(x, y);
        } else {
            // DPad not pressed, clear all buttons
            container.upAll();
        }
    })
    this._controlsCanvas.stage.addChild(container);
    this._dPadContainer = container;
}
//=============================================================================
// * LB/RB Buttons
//=============================================================================
ONSControls.createBumpers = function() {
    const LBsprite = new PIXI.Sprite.fromImage("js/porting/assets/omori_lb_button.png");
    const RBsprite = new PIXI.Sprite.fromImage("js/porting/assets/omori_rb_button.png");
    LBsprite.anchor.set(0.5);
    LBsprite.interactive = true;
    LBsprite.on("pointerdown", (event) => {this.sendEvent(event, "BUTTON_LB")})
            .on("pointerup", (event) => {this.sendEvent(event, "BUTTON_LB")})
    RBsprite.anchor.set(0.5);
    RBsprite.interactive = true;
    RBsprite.on("pointerdown", (event) => {this.sendEvent(event, "BUTTON_RB")})
            .on("pointerup", (event) => {this.sendEvent(event, "BUTTON_RB")})
    this._controlsCanvas.stage.addChild(LBsprite);
    this._controlsCanvas.stage.addChild(RBsprite);
    this._LBsprite = LBsprite;
    this._RBsprite = RBsprite;
}
//=============================================================================
// * Show Button
//=============================================================================
ONSControls.createAdditionalButtons = function() {
    const showButton = new PIXI.Sprite.fromImage("js/porting/assets/omori_show_button.png");
    showButton.anchor.set(0.5);
    showButton.interactive = true;
    showButton.on("pointerdown", this.toggle)
    this._controlsCanvas.stage.addChild(showButton);
    this._showButton = showButton;
}
//=============================================================================
// * Check idle animation (spinning buttons)
//=============================================================================
ONSControls.idleAnimation = function() {
    ONSControls._buttonsContainer.rotation -= 0.1;
    ONSControls._dPadContainer.rotation += 0.1;
    ONSControls._LBsprite.rotation += 0.1;
    ONSControls._RBsprite.rotation -= 0.1;
    ONSControls._showButton.rotation += 0.1;
}
//=============================================================================
// * Check idle animation (spinning buttons)
//=============================================================================
ONSControls.isIdlePlaying = false;
//=============================================================================
// * Play idle animation (spinning buttons)
//=============================================================================
ONSControls.playIdleAnimation = function() {
    if (!ONSControls.isIdlePlaying) {
        ONSControls.isIdlePlaying = true;
        ONSControls._controlsCanvas.ticker.add(ONSControls.idleAnimation);
    }
}
//=============================================================================
// * Stop idle animation (spinning buttons)
//=============================================================================
ONSControls.stopIdleAnimation = function() {
    ONSControls._controlsCanvas.ticker.remove(ONSControls.idleAnimation);
    ONSControls._buttonsContainer.rotation = 0;
    ONSControls._dPadContainer.rotation = 0;
    ONSControls._LBsprite.rotation = 0;
    ONSControls._RBsprite.rotation = 0;
    ONSControls._showButton.rotation = 0;
}
//=============================================================================
// * Send event to virtual controller
//=============================================================================
ONSControls.sendEvent = function(event, button) {
    const key = VirtualGamepad.keys.find((element) => element.name == button);
    if (event.type === "pointerdown") {
        console.log(`ONSControls: Pointer down for ${button}`); // Remove in release
    }
    VirtualGamepad.gamepad.buttons[key.code].pressed = event.type === "pointerdown" ? true : false
}
//=============================================================================
// * Toggle controls
//=============================================================================
ONSControls.toggle = function() {
    const elements = [
        ONSControls._buttonsContainer, ONSControls._dPadContainer, 
        ONSControls._LBsprite, ONSControls._RBsprite
    ]
    for (elem of elements) {
        if (elem.visible) {
            elem.visible = false;
        } else {
            elem.visible = true;
        }
    }
}
//=============================================================================
// * Update controls parameters
//=============================================================================
ONSControls.updateButtons = function() {

    // Update A/B/X/Y
    const buttonsSize = ConfigManager.ONSConfig.buttonsSize;
    const container = this._buttonsContainer;
    const buttons = container.children;
    for (btn of buttons) {
        btn.width = buttonsSize;
        btn.height = buttonsSize;
        btn.interactive = true;
    }
    buttons[0].y = buttonsSize / 2 + buttonsSize / 4;
    buttons[1].x = -(buttonsSize / 2 + buttonsSize / 4);
    buttons[2].x = buttonsSize / 2 + buttonsSize / 4;
    buttons[3].y = -(buttonsSize / 2 + buttonsSize / 4);
    container.width = buttonsSize * 2;
    container.height = buttonsSize * 2;
    container.alpha = ConfigManager.ONSConfig.buttonsOpacity;
    container.position.set(ConfigManager.ONSConfig.buttonsX, ConfigManager.ONSConfig.buttonsY);

    // Update DPAD
    const dPadSize = ConfigManager.ONSConfig.dPadSize;
    const dpadContainer = this._dPadContainer;
    dpadContainer.width = dPadSize;
    dpadContainer.height = dPadSize;
    dpadContainer.alpha = ConfigManager.ONSConfig.buttonsOpacity;
    dpadContainer.position.set(ConfigManager.ONSConfig.dPadX, ConfigManager.ONSConfig.dPadY);

    // Update LB/RB
    const LBsprite = this._LBsprite;
    const RBsprite = this._RBsprite;
    LBsprite.position.set(ConfigManager.ONSConfig.LBX, ConfigManager.ONSConfig.LBY);
    LBsprite.alpha = ConfigManager.ONSConfig.buttonsOpacity;
    LBsprite.width = ConfigManager.ONSConfig.bumpersWidth;
    LBsprite.height = ConfigManager.ONSConfig.bumpersHeight;
    RBsprite.position.set(ConfigManager.ONSConfig.RBX, ConfigManager.ONSConfig.RBY);
    RBsprite.alpha = ConfigManager.ONSConfig.buttonsOpacity;
    RBsprite.width = ConfigManager.ONSConfig.bumpersWidth;
    RBsprite.height = ConfigManager.ONSConfig.bumpersHeight;

    // Update additional
    const showButton = this._showButton;
    showButton.position.set(ConfigManager.ONSConfig.showX, ConfigManager.ONSConfig.showY);
    showButton.width = ConfigManager.ONSConfig.additonalSize;
    showButton.height = ConfigManager.ONSConfig.additonalSize;
    showButton.alpha = ConfigManager.ONSConfig.buttonsOpacity;
    console.log("ONSControls: Controls updated");
}

ONSControls.disableTouch = function() {
    TouchInput.update = function() {return;};
}





//=============================================================================
// ** VirtualGamepad
//=============================================================================
class VirtualGamepad {};
//=============================================================================
// * Virtual gamepad mapping
//=============================================================================
VirtualGamepad.keys = [
    {name: "BUTTON_A", code: 0},
    {name: "BUTTON_B", code: 1},
    {name: "BUTTON_X", code: 2},
    {name: "BUTTON_Y", code: 3},
    {name: "BUTTON_LB", code: 4},
    {name: "BUTTON_RB", code: 5},
    {name: "BUTTON_LT", code: 6},
    {name: "BUTTON_RT", code: 7},
    {name: "BUTTON_SELECT", code: 8},
    {name: "BUTTON_START", code: 9},
    {name: "DPAD_UP", code: 12},
    {name: "DPAD_DOWN", code: 13},
    {name: "DPAD_LEFT", code: 14},
    {name: "DPAD_RIGHT", code: 15},
    {name: "BACK", code: 16}
]
//=============================================================================
// * Define gamepad
//=============================================================================
VirtualGamepad.gamepad = {
    id: ONSControls.options.gamepadId || 'builtin',
    index: ONSControls.options.gamepadIndex || 0,
    connected: true,
    timestamp: Math.floor(Date.now() / 1000),
    mapping: "standard",
    axes: [0, 0, 0, 0],
    buttons: Array.apply(0, Array(17)).map(function () {
        return {
            pressed: false,
            touched: false,
            value: 0
        };
    })
}
//=============================================================================
// * Replace first gamepad with virtual
//=============================================================================
VirtualGamepad.replaceNavigator = function() {
    var gamepads = navigator.getGamepads();
    navigator.getGamepads = function() {
        var index = ONSControls.options.gamepadIndex;
        gamepads[index] = VirtualGamepad.gamepad;
        return gamepads;
    }
}
//=============================================================================
// * Connect virtual gamepad
//=============================================================================
VirtualGamepad.originalNavigator = navigator.getGamepads;
VirtualGamepad.connect = function () {
    this.replaceNavigator();
    const event = new Event("gamepadconnected");
    event.gamepad = this.gamepad;
    window.dispatchEvent(event);
    console.log(`ONSControls: Virtual gamepad connected with index ${this.gamepad.index}`);
}
//=============================================================================
// * Disconnect virtual gamepad
//=============================================================================
VirtualGamepad.disconnect = function () {
    const event = new Event("gamepaddisconnected");
    this.gamepad.axes = [0, 0, 0, 0];
    this.gamepad.buttons = Array.apply(0, Array(17)).map(function () {
        return {
            pressed: false,
            touched: false,
            value: 0
        };
    })
    window.dispatchEvent(event);
    //VirtualGamepad.originalNavigator.getGamepads.call();
    console.log(`ONSControls: Virtual gamepad disconnected with index ${this.gamepad.index}`);
}





//=============================================================================
// ** ConfigManager
//=============================================================================

ONSControls.configManager = function() {
//=============================================================================
// * Class Variables
//=============================================================================
ConfigManager.ONSConfig ||= {};
ConfigManager.ONSConfig.buttonsScale ||= this.options.buttonsScale;
ConfigManager.ONSConfig.buttonsOpacity ||= this.options.buttonsOpacity;
ConfigManager.ONSConfig.controlsOffsetX ||= 32;
ConfigManager.ONSConfig.controlsOffsetY ||= 16;
ConfigManager.ONSConfig.buttonsSize ||= ONSControls._controlsCanvas.vh(0.18) * ConfigManager.ONSConfig.buttonsScale;
ConfigManager.ONSConfig.buttonsX ||= ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.buttonsSize + ConfigManager.ONSConfig.controlsOffsetX);;
ConfigManager.ONSConfig.buttonsY ||= ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.buttonsSize + ConfigManager.ONSConfig.controlsOffsetY);
ConfigManager.ONSConfig.dPadSize ||= ONSControls._controlsCanvas.vh(0.36) * ConfigManager.ONSConfig.buttonsScale;
ConfigManager.ONSConfig.dPadX ||= ConfigManager.ONSConfig.dPadSize / 2 + ConfigManager.ONSConfig.controlsOffsetX;
ConfigManager.ONSConfig.dPadY ||= ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.dPadSize / 2 + ConfigManager.ONSConfig.controlsOffsetY);
ConfigManager.ONSConfig.bumpersOffsetX ||= 16;
ConfigManager.ONSConfig.bumpersOffsetY ||= ONSControls._controlsCanvas.vh(0.30);
ConfigManager.ONSConfig.bumpersWidth ||= ONSControls._controlsCanvas.vh(0.188) * ConfigManager.ONSConfig.buttonsScale;
ConfigManager.ONSConfig.bumpersHeight ||= ONSControls._controlsCanvas.vh(0.12) * ConfigManager.ONSConfig.buttonsScale;
ConfigManager.ONSConfig.LBX ||= ConfigManager.ONSConfig.bumpersOffsetX + ConfigManager.ONSConfig.bumpersWidth / 2;
ConfigManager.ONSConfig.LBY ||= (ConfigManager.ONSConfig.bumpersOffsetY + ConfigManager.ONSConfig.bumpersHeight / 2);
ConfigManager.ONSConfig.RBX ||= ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.bumpersOffsetX + ConfigManager.ONSConfig.bumpersWidth / 2);
ConfigManager.ONSConfig.RBY ||= (ConfigManager.ONSConfig.bumpersOffsetY + ConfigManager.ONSConfig.bumpersHeight / 2);
ConfigManager.ONSConfig.additionalOffset ||= ONSControls._controlsCanvas.vh(0.03);
ConfigManager.ONSConfig.additonalSize ||= ONSControls._controlsCanvas.vh(0.06) * ConfigManager.ONSConfig.buttonsScale;
ConfigManager.ONSConfig.showX ||= ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.additonalSize / 2 + ConfigManager.ONSConfig.additionalOffset);
ConfigManager.ONSConfig.showY ||= ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.additonalSize / 2 + ConfigManager.ONSConfig.additionalOffset);
//=============================================================================
// * Restore defaults
//=============================================================================
var _ConfigManager_restoreDefaultConfig = ConfigManager.restoreDefaultConfig;
ConfigManager.restoreDefaultConfig = function () {
    _ConfigManager_restoreDefaultConfig.apply(this, arguments);
    const fs = require("fs");j
    const path = require('path');
    var base = path.dirname(process.mainModule.filename);
    base = path.join(base, 'save/');
    if (fs.existsSync(base + "config.rpgsave")) { fs.unlinkSync(base + "config.rpgsave"); }
    ConfigManager.ONSConfig.buttonsScale = this.options.buttonsScale;
    ConfigManager.ONSConfig.buttonsOpacity = this.options.buttonsOpacity;
    ConfigManager.ONSConfig.controlsOffsetX = 32;
    ConfigManager.ONSConfig.controlsOffsetY = 16;
    ConfigManager.ONSConfig.buttonsSize = ONSControls._controlsCanvas.vh(0.18) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.buttonsX = ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.buttonsSize + ConfigManager.ONSConfig.controlsOffsetX);;
    ConfigManager.ONSConfig.buttonsY = ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.buttonsSize + ConfigManager.ONSConfig.controlsOffsetY);
    ConfigManager.ONSConfig.dPadSize = ONSControls._controlsCanvas.vh(0.36) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.dPadX = ConfigManager.ONSConfig.dPadSize / 2 + ConfigManager.ONSConfig.controlsOffsetX;
    ConfigManager.ONSConfig.dPadY = ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.dPadSize / 2 + ConfigManager.ONSConfig.controlsOffsetY);
    ConfigManager.ONSConfig.bumpersOffsetX = 16;
    ConfigManager.ONSConfig.bumpersOffsetY = ONSControls._controlsCanvas.vh(0.30);
    ConfigManager.ONSConfig.bumpersWidth = ONSControls._controlsCanvas.vh(0.188) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.bumpersHeight = ONSControls._controlsCanvas.vh(0.12) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.LBX = ConfigManager.ONSConfig.bumpersOffsetX + ConfigManager.ONSConfig.bumpersWidth / 2;
    ConfigManager.ONSConfig.LBY = (ConfigManager.ONSConfig.bumpersOffsetY + ConfigManager.ONSConfig.bumpersHeight / 2);
    ConfigManager.ONSConfig.RBX = ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.bumpersOffsetX + ConfigManager.ONSConfig.bumpersWidth / 2);
    ConfigManager.ONSConfig.RBY = (ConfigManager.ONSConfig.bumpersOffsetY + ConfigManager.ONSConfig.bumpersHeight / 2);
    ConfigManager.ONSConfig.additionalOffset = ONSControls._controlsCanvas.vh(0.03);
    ConfigManager.ONSConfig.additonalSize = ONSControls._controlsCanvas.vh(0.06) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.showX = ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.additonalSize / 2 + ConfigManager.ONSConfig.additionalOffset);
    ConfigManager.ONSConfig.showY = ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.additonalSize / 2 + ConfigManager.ONSConfig.additionalOffset);
    ConfigManager.applyData(ConfigManager);
    let needsRestore = confirm(LanguageManager.languageData().text.System.plugins.optionsMenu.alertMessages["restoreGeneral"]);
    if (!!needsRestore) { DataManager._restoreGlobalInfo(); }
}
//=============================================================================
// * Update all controls data
//=============================================================================
ConfigManager.ONSConfig.updateData = function() {
    ConfigManager.ONSConfig.buttonsScale = ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.buttonsOpacity = ConfigManager.ONSConfig.buttonsOpacity;
    ConfigManager.ONSConfig.controlsOffsetX = ConfigManager.ONSConfig.controlsOffsetX;
    ConfigManager.ONSConfig.controlsOffsetY = ConfigManager.ONSConfig.controlsOffsetY;
    ConfigManager.ONSConfig.buttonsSize = ONSControls._controlsCanvas.vh(0.18) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.buttonsX = ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.buttonsSize + ConfigManager.ONSConfig.controlsOffsetX);;
    ConfigManager.ONSConfig.buttonsY = ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.buttonsSize + ConfigManager.ONSConfig.controlsOffsetY);
    ConfigManager.ONSConfig.dPadSize = ONSControls._controlsCanvas.vh(0.36) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.dPadX = ConfigManager.ONSConfig.dPadSize / 2 + ConfigManager.ONSConfig.controlsOffsetX;
    ConfigManager.ONSConfig.dPadY = ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.dPadSize / 2 + ConfigManager.ONSConfig.controlsOffsetY);
    ConfigManager.ONSConfig.bumpersOffsetX = ConfigManager.ONSConfig.bumpersOffsetX;
    ConfigManager.ONSConfig.bumpersOffsetY = ConfigManager.ONSConfig.bumpersOffsetY;
    ConfigManager.ONSConfig.bumpersWidth = ONSControls._controlsCanvas.vh(0.188) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.bumpersHeight = ONSControls._controlsCanvas.vh(0.12) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.LBX = ConfigManager.ONSConfig.bumpersOffsetX + ConfigManager.ONSConfig.bumpersWidth / 2;
    ConfigManager.ONSConfig.LBY = (ConfigManager.ONSConfig.bumpersOffsetY + ConfigManager.ONSConfig.bumpersHeight / 2);
    ConfigManager.ONSConfig.RBX = ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.bumpersOffsetX + ConfigManager.ONSConfig.bumpersWidth / 2);
    ConfigManager.ONSConfig.RBY = (ConfigManager.ONSConfig.bumpersOffsetY + ConfigManager.ONSConfig.bumpersHeight / 2);
    ConfigManager.ONSConfig.additionalOffset = ConfigManager.ONSConfig.additionalOffset;
    ConfigManager.ONSConfig.additonalSize = ONSControls._controlsCanvas.vh(0.06) * ConfigManager.ONSConfig.buttonsScale;
    ConfigManager.ONSConfig.showX = ONSControls._controlsCanvas.screen.width - (ConfigManager.ONSConfig.additonalSize / 2 + ConfigManager.ONSConfig.additionalOffset);
    ConfigManager.ONSConfig.showY = ONSControls._controlsCanvas.screen.height - (ConfigManager.ONSConfig.additonalSize / 2 + ConfigManager.ONSConfig.additionalOffset);
    ONSControls.updateButtons();
}
}
//=============================================================================
// * Original functions
//=============================================================================
var ConfigManager_makeData = ConfigManager.makeData;
var ConfigManager_applyData = ConfigManager.applyData;
//=============================================================================
// * Make Data
//=============================================================================
var _ConfigManager_makeData = ConfigManager.makeData;
ConfigManager.makeData = function () {
    var config = _ConfigManager_makeData.apply(this, arguments);
    config.ONSConfig = {};
    config.ONSConfig.controlsOffsetX = this.ONSConfig.controlsOffsetX;
    config.ONSConfig.controlsOffsetY = this.ONSConfig.controlsOffsetY;
    config.ONSConfig.buttonsScale = this.ONSConfig.buttonsScale;
    config.ONSConfig.buttonsOpacity = this.ONSConfig.buttonsOpacity;
    config.ONSConfig.additionalOffset = this.ONSConfig.additionalOffset;
    config.ONSConfig.buttonsSize = this.ONSConfig.buttonsSize;
    config.ONSConfig.buttonsX = this.ONSConfig.buttonsX;
    config.ONSConfig.buttonsY = this.ONSConfig.buttonsY;
    config.ONSConfig.dPadSize = this.ONSConfig.dPadSize;
    config.ONSConfig.dPadX = this.ONSConfig.dPadX;
    config.ONSConfig.dPadY = this.ONSConfig.dPadY;
    config.ONSConfig.bumpersWidth = this.ONSConfig.bumpersWidth;
    config.ONSConfig.bumpersHeight = this.ONSConfig.bumpersHeight;
    config.ONSConfig.bumpersOffsetX = this.ONSConfig.bumpersOffsetX;
    config.ONSConfig.bumpersOffsetY = this.ONSConfig.bumpersOffsetY;
    config.ONSConfig.LBX = this.ONSConfig.LBX;
    config.ONSConfig.LBY = this.ONSConfig.LBY;
    config.ONSConfig.RBX = this.ONSConfig.RBX;
    config.ONSConfig.RBY = this.ONSConfig.RBY;
    config.ONSConfig.additionalOffset = this.ONSConfig.additionalOffset;
    config.ONSConfig.additonalSize = this.ONSConfig.additonalSize;
    config.ONSConfig.showX = this.ONSConfig.showX;
    config.ONSConfig.showY = this.ONSConfig.showY;
    return config;
}
//=============================================================================
// * Apply Data
//=============================================================================
var _ConfigManager_applyData = ConfigManager.applyData; 
ConfigManager.applyData = function (config) {
    _ConfigManager_applyData.apply(this, arguments);
    try {
        this.ONSConfig = config.ONSConfig;
        this.ONSConfig.controlsOffsetX = config.ONSConfig.controlsOffsetX;
        this.ONSConfig.controlsOffsetY = config.ONSConfig.controlsOffsetY;
        this.ONSConfig.buttonsScale = config.ONSConfig.buttonsScale;
        this.ONSConfig.buttonsOpacity = config.ONSConfig.buttonsOpacity;
        this.ONSConfig.additionalOffset = config.ONSConfig.additionalOffset;
        this.ONSConfig.buttonsSize = config.ONSConfig.buttonsSize;
        this.ONSConfig.buttonsX = config.ONSConfig.buttonsX;
        this.ONSConfig.buttonsY = config.ONSConfig.buttonsY;
        this.ONSConfig.dPadSize = config.ONSConfig.dPadSize;
        this.ONSConfig.dPadX = config.ONSConfig.dPadX;
        this.ONSConfig.dPadY = config.ONSConfig.dPadY;
        this.ONSConfig.bumpersWidth = config.ONSConfig.bumpersWidth;
        this.ONSConfig.bumpersHeight = config.ONSConfig.bumpersHeight;
        this.ONSConfig.bumpersOffsetX = config.ONSConfig.bumpersOffsetX;
        this.ONSConfig.bumpersOffsetY = config.ONSConfig.bumpersOffsetY;
        this.ONSConfig.LBX = config.ONSConfig.LBX;
        this.ONSConfig.LBY = config.ONSConfig.LBY;
        this.ONSConfig.RBX = config.ONSConfig.RBX;
        this.ONSConfig.RBY = config.ONSConfig.RBY;
        this.ONSConfig.additionalOffset = config.ONSConfig.additionalOffset;
        this.ONSConfig.additonalSize = config.ONSConfig.additonalSize;
        this.ONSConfig.showX = config.ONSConfig.showX;
        this.ONSConfig.showY = config.ONSConfig.showY;
    } catch {
        console.log("ONSControls loaded in first time, skip config reading.");
    }
}





//=============================================================================
// ** Window_OmoMenuOptionsONSControls
//-----------------------------------------------------------------------------
// The window for showing ONSControls options in the OMORI options menu
//=============================================================================
function Window_OmoMenuOptionsONSControls() { this.initialize.apply(this, arguments); }
Window_OmoMenuOptionsONSControls.prototype = Object.create(Window_Selectable.prototype);
Window_OmoMenuOptionsONSControls.prototype.constructor = Window_OmoMenuOptionsONSControls;
//=============================================================================
// * Object Initialization
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.initialize = function () {
    // Make Options List
    this.makeOptionsList();
    // Super Call
    Window_Selectable.prototype.initialize.call(this, 0, 0, this.windowWidth(), this.windowHeight());
    // Create Option Bars
    this.createOptionBars();
    this.select(0);
    // Refresh
    this.refresh();
};
//=============================================================================
// * Settings
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.isUsingCustomCursorRectSprite = function () { return true; };
Window_OmoMenuOptionsONSControls.prototype.standardPadding = function () { return 8; }
Window_OmoMenuOptionsONSControls.prototype.windowWidth = function () { return Graphics.width - 20; };
Window_OmoMenuOptionsONSControls.prototype.windowHeight = function () { return 318; }
Window_OmoMenuOptionsONSControls.prototype.maxItems = function () { return this._optionsList.length; };
Window_OmoMenuOptionsONSControls.prototype.maxCols = function () { return 1; };
Window_OmoMenuOptionsONSControls.prototype.itemHeight = function () { return 75; };
Window_OmoMenuOptionsONSControls.prototype.spacing = function () { return 5; };
Window_OmoMenuOptionsONSControls.prototype.customCursorRectXOffset = function () { return 15; }
Window_OmoMenuOptionsONSControls.prototype.customCursorRectYOffset = function () { return -18; }
//=============================================================================
// * Height
//=============================================================================
Object.defineProperty(Window_OmoMenuOptionsONSControls.prototype, 'height', {
    get: function () { return this._height; },
    set: function (value) {
        this._height = value;
        this._refreshAllParts();
        // If Option Sprites Exist
        if (this._optionSprites) {
            for (var i = 0; i < this._optionSprites.length; i++) {
                var sprite = this._optionSprites[i];
                sprite.visible = value >= (sprite.y + sprite.height)
            };
        }
    },
    configurable: true
});
//=============================================================================
// * Create Option Bars
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.createOptionBars = function () {
    // Initialize Option Sprites
    this._optionSprites = [];
    // Create Bitmap
    var bitmap = new Bitmap(400, 40);
    // Iterate from 0 to 100
    for (var i = 0; i < 100; i++) {
        var x = (i + 4) + (i % 2);;
        var x = (i * 4);
        bitmap.fillRect(x, 0, 2, 20, 'rgba(100, 100, 100, 1)');
        bitmap.fillRect(x, 20, 2, 20, 'rgba(255, 255, 255, 1)');
    };
    // Create Sprites
    for (var i = 0; i < 8; i++) {
        var sprite = new Sprite(bitmap);
        var index = Math.floor(i / 2);
        var rect = this.itemRect(index);
        sprite.x = rect.x + 60;
        sprite.y = rect.y + 50;
        // sprite.y += (i % 2) * 20;
        sprite.setFrame(0, (i % 2) * 20, bitmap.width, 20);
        this._optionSprites.push(sprite);
        this.addChild(sprite);
    };
};
//=============================================================================
// * Make Options List
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.makeOptionsList = function () {
    // Get Text
    //var text = LanguageManager.getPluginText('optionsMenu', 'audio');
    var text = {
        buttonsScale: {help: "Change on-screen controls scale.", text: "CONTROLS SCALE", persent: true, maxValue: 500},
        buttonsOpacity: {help: "Change on-screen controls opacity.", text: "CONTROLS OPACITY", persent: true, maxValue: 100},
        bumpersOffsetX: {help: "Change x coordinate offset of LB/RB.", text: "LB/RB X OFFSET", persent: false, maxValue: 500},
        bumpersOffsetY: {help: "Change y coordinate offset of LB/RB.", text: "LB/RB Y OFFSET", persent: false, maxValue: 500}
    }
    // Get Config
    var config = ConfigManager;
    // Get Options
    var options = Object.keys(text);
    // Initialize Options List
    this._optionsList = [];
    // Go Through Options
    for (var i = 0; i < options.length; i++) {
        // Get Name
        var name = options[i];
        // Get Data
        var data = text[name];
        // Add Option
        if (data.persent) {
            this._optionsList.push({ header: data.text + ':', config: name, option: ConfigManager.ONSConfig[name] * 100, helpText: data.help, persent: data.persent, maxValue: data.maxValue });
        } else {
            this._optionsList.push({ header: data.text + ':', config: name, option: ConfigManager.ONSConfig[name], helpText: data.help, persent: data.persent, maxValue: data.maxValue });
        }
    };
}
//=============================================================================
// * Draw Item
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.drawItem = function (index) {
    // Get Item Rect
    var rect = this.itemRect(index);
    // Get Data
    var data = this._optionsList[index];
    // If Data Exists
    if (data) {
        // Draw Header
        this.contents.drawText(data.header, rect.x + 50, rect.y, rect.width, 24);
        // Update option bar
        this.updateOptionBar(index, data.option, data.persent);
    };
};
//=============================================================================
// * Call Update Help
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.callUpdateHelp = function () {
    // Run Original Function
    Window_Selectable.prototype.callUpdateHelp.call(this);
    // If Help Window Exist
    if (this._helpWindow) {
        this._helpWindow.setText(this._optionsList[this.index()].helpText);
    };
};
//=============================================================================
// * Cursor Right
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.cursorRight = function (wrap) {
    // Super Call
    Window_Selectable.prototype.cursorRight.call(this, wrap);
    // Get Data
    var data = this._optionsList[this.index()];
    // Get Data
    if (data) {
        var rate = Input.isLongPressed('right') ? 5 : 5
        data.option = Math.min(data.option + rate, data.maxValue);
        this.updateOptionBar(this.index(), data.option, data.persent);
    };
};
//=============================================================================
// * Cursor Left
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.cursorLeft = function (wrap) {
    // Super Call
    Window_Selectable.prototype.cursorLeft.call(this, wrap);
    // Get Data
    var data = this._optionsList[this.index()];
    // Get Data
    if (data) {
        var rate = Input.isLongPressed('left') ? 5 : 5
        data.option = Math.max(data.option - rate, 0);
        this.updateOptionBar(this.index(), data.option, data.persent);
    };
};
//=============================================================================
// * Cursor Left
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.updateOptionBar = function (index, option, persent) {
    // Get Data
    var data = this._optionsList[index];
    // Get Back and Front Sprite
    var front = this._optionSprites[(index * 2) + 1];
    front._frame.width = option / (data.maxValue / 100) * 4;
    front._refresh();
    // Get Itm Rect
    var rect = this.itemRect(index);
    rect.x += 415; rect.y += 27; rect.width = 100; rect.height = 40;
    this.contents.clearRect(rect.x, rect.y, rect.width, rect.height);
    if (persent) {
        this.contents.drawText(option + '%', rect.x, rect.y, rect.width, rect.height, 'right');
        // Set Option
        ConfigManager.ONSConfig[data.config] = option / 100;
    } else {
        this.contents.drawText(Math.round(option) + 'px', rect.x, rect.y, rect.width, rect.height, 'right');
        // Set Option
        ConfigManager.ONSConfig[data.config] = option;
    }
    ConfigManager.ONSConfig.updateData();
};
//=============================================================================
// * Add to options menus
//=============================================================================
Window_OmoMenuOptionsONSControls.prototype.add = function () {
    Scene_OmoMenuOptions.prototype.createONSControlsOptionsWindow = function () {
        // Create ONSControls Options Window
        this._onscontrolsOptionsWindow = new Window_OmoMenuOptionsONSControls();
        this._onscontrolsOptionsWindow.x = 10;
        this._onscontrolsOptionsWindow.y = 10;
        this._onscontrolsOptionsWindow.setHandler('cancel', this.onOptionWindowCancel.bind(this));
        this._onscontrolsOptionsWindow.height = 0;
        this._onscontrolsOptionsWindow.visible = false;
        this.addChild(this._onscontrolsOptionsWindow);
    };
    Scene_OmoMenuOptions.prototype.optionWindows = function () {
        return [this._generalOptionsWindow, this._audioOptionsWindow, this._controlOptionsWindow, this._onscontrolsOptionsWindow, this._systemOptionsWindow]
    }
    Scene_OmoriTitleScreen.prototype.createONSControlsOptionsWindow = function () {
        // Create Audio Options Window
        this._onscontrolsOptionsWindow = new Window_OmoMenuOptionsONSControls();
        this._onscontrolsOptionsWindow.setHandler('cancel', this.onOptionWindowCancel.bind(this));
        this._onscontrolsOptionsWindow.visible = false;
        this._optionsWindowsContainer.addChild(this._onscontrolsOptionsWindow);
    };
    Scene_OmoriTitleScreen.prototype.optionWindows = function () {
        return [this._generalOptionsWindow, this._audioOptionsWindow, this._controlOptionsWindow, this._onscontrolsOptionsWindow, this._systemOptionsWindow]
    }
    Scene_OmoMenuOptions.prototype.create = function () {
        // Super Call
        Scene_OmoMenuBase.prototype.create.call(this);
        this.createHelpWindow();
        this.createStatusWindows();
        this.createGoldWindow();
      
        this.createGeneralOptionsWindow();
        this.createAudioOptionsWindow();
        this.createControllerOptionsWindow();
        this.createONSControlsOptionsWindow();
        this.createSystemOptionsWindow();
        this.createOptionCategoriesWindow();
        // this.createHelpWindow();
        this.createCommandWindow();
        this.createExitPromptWindow();
    };
    Window_OmoMenuOptionsCategory.prototype.standardPadding = function () { return 7; }
    Window_OmoMenuOptionsCategory.prototype.maxCols = function () { return 5; };
    Window_OmoMenuOptionsCategory.prototype.makeCommandList = function () {
        const localization = LanguageManager.getMessageData("XX_BLUE.Omori_Mainmenu_Sceneoptions").commands
        this.addCommand(localization[0], 'ok');
        this.addCommand(localization[1], 'ok');
        // this.addCommand('GAMEPLAY', 'ok');
        this.addCommand(localization[2], 'ok');
        this.addCommand("TOUCH", 'ok');
        this.addCommand(localization[3], 'ok');
    };
}





//=============================================================================
// * Plugin init
//=============================================================================
ONSControls.initialize = function() {
    console.log("ONSControls: Initialized");
    this.createCanvas();
    this.configManager();
    this.createButtons();
    this.createDPad();
    this.createBumpers();
    this.createAdditionalButtons();
    this.updateButtons();
    this.disableTouch();
    VirtualGamepad.connect();
    Window_OmoMenuOptionsONSControls.prototype.add();
}