VPA-Keyboard

For planets.nu -- Add VPA-key strokes, and commands

Від 31.05.2016. Дивіться остання версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        VPA-Keyboard
// @author      Quapla
// @copyright   Basecode: Kero van Gelder, 2016 - latest: Quapla
// @license     Lesser Gnu Public License, version 3
// @description For planets.nu -- Add VPA-key strokes, and commands
// @namespace   quapla/VPA-Keyboard
// @include     http://planets.nu/*
// @include     http://play.planets.nu/*
// @include     http://test.planets.nu/*
// @version     1.0.1
// @grant       none
// ==/UserScript==

// Reference:   Kero van Gelder's Keayboard: http://chmeee.org/ext/planets.nu/Keyboard.user.js


sharedContent.prototype.planetSurvey_McNimblesToolkit = sharedContent.prototype.planetSurvey;
sharedContent.prototype.planetSurvey = function(planetId) {
    vgap.currentPlanetSurvey = planetId;
    this.planetSurvey_McNimblesToolkit(planetId);
}

sharedContent.prototype.editNote_McNimblesToolkit = sharedContent.prototype.editNote;
sharedContent.prototype.editNote = function(noteId, noteType) {
    vgap.currentNoteId = noteId;
    vgap.currentNoteType = noteType;
    this.editNote_McNimblesToolkit(noteId, noteType);
    $("#EditNote").focus();
}

vgapShipScreen.prototype.shipMission_McNimblesToolkit = vgapShipScreen.prototype.shipMission;
vgapShipScreen.prototype.shipMission = function() {
    this.shipMissionOpen = true;
    this.shipMission_McNimblesToolkit();
}

vgapShipScreen.prototype.selectMission_McNimblesToolkit = vgapShipScreen.prototype.selectMission;
vgapShipScreen.prototype.selectMission = function(missionId) {
    this.selectMission_McNimblesToolkit(missionId);
    this.shipMissionOpen = false;
}

vgapShipScreen.prototype.primaryEnemy_McNimblesToolkit = vgapShipScreen.prototype.primaryEnemy;
vgapShipScreen.prototype.primaryEnemy = function() {
    this.primaryEnemyOpen = true;
    this.primaryEnemy_McNimblesToolkit();
}
vgapShipScreen.prototype.selectEnemy_McNimblesToolkit = vgapShipScreen.prototype.selectEnemy;
vgapShipScreen.prototype.selectEnemy = function(enemyId) {
    this.selectEnemy_McNimblesToolkit(enemyId);
    this.primaryEnemyOpen = false;
}
vgaPlanets.prototype.closeMore_McNimblesToolkit = vgaPlanets.prototype.closeMore;
vgaPlanets.prototype.closeMore = function(callback) {
    vgap.closeMore_McNimblesToolkit(callback);
    this.primaryEnemyOpen = false;
    vgap.currentPlanetSurvey = null;
    vgap.shipScreen.shipMissionOpen = false;
}

vgaPlanets.prototype.hotkey_McNimblesToolkit = vgaPlanets.prototype.hotkey;
vgaPlanets.prototype.hotkey = function(event) {
    if (vgap.shipScreen.primaryEnemyOpen) {
        var enemy = 0;
        if (event.keyCode >= 48 && event.keyCode <= 57) enemy = event.keyCode - 48;
        if (event.keyCode >= 65 && event.keyCode <= 90) enemy = event.keyCode - 65 + 10;
        if (enemy <= vgap.players.length) {
            vgap.shipScreen.selectEnemy(enemy);
            return;
        }
    }

    if (vgap.shipScreen.shipMissionOpen) {
        var missionId = -1;
        if (event.keyCode == 67) missionId = 9;       // 'c', Cloak
        else if (event.keyCode == 69) missionId = 0;  // 'e', Explore
        else if (event.keyCode == 73) missionId = 7;  // 'i', Intercept
        else if (event.keyCode == 75) missionId = 3;  // 'k', Kill
        else if (event.keyCode == 76) missionId = 2;  // 'l', Lay Mines
        else if (event.keyCode == 77) missionId = 1;  // 'm', Mine Sweep
        else if (event.keyCode == 83 && event.shiftKey) missionId = 8;  // 'S', Special / Super Spy
        else if (event.keyCode == 83) missionId = 4;  // 's', Sensor Sweep
        else if (event.keyCode == 84) missionId = 6;  // 't', Tow
        if (missionId >= 0) {
            vgap.shipScreen.selectMission(missionId);
            return;
        }
    }

    if (vgap.editNoteOpen) {
        if (event.keyCode == 13 && event.ctrlKey) {
            this.saveNote(vgap.currentNoteId, vgap.currentNoteType);
            return;
        }
    }

    if (event.keyCode == 27) {
        if (this.findObjectFeatureActivated) vgap.map.hideFindObjectFeature();
        else if (vgap.moreOpen) this.closeMore();
        else if (this.lcOpen) this.deselectAll();
        else this.hotkey_McNimblesToolkit(event);
        return;
    }

    if (vgap.hotkeysOn) {
        switch (event.keyCode) {
        case 8:
            if ((this.starbaseScreenOpen || this.planetScreenOpen || this.shipScreenOpen) && !this.moreOpen) {
                vgap.rPrev();
                return;
            }
        case 78: // 'n'
            if (event.shiftKey) {
                if (this.planetScreenOpen) {
                    shtml.editNote(vgap.planetScreen.planet.id, 1);
                    return;
                } else if (vgap.currentPlanetSurvey > 0) { // ship screen is open, but unowned planet is selected
                    shtml.editNote(vgap.currentPlanetSurvey, 1);
                    return
                } else if (this.shipScreenOpen) {
                    shtml.editNote(vgap.shipScreen.ship.id, 2);
                    return;
                } else if (this.starbaseScreenOpen) {
                    shtml.editNote(vgap.starbaseScreen.starbase.id, 3);
                    return;
                }
            }
            break;
        case 88: // 'x'
            this.map.clearTools();
            return;
        case 90: // 'z'
            this.map.zoomFarInOrOut_OnWaypointOrPlanet();
            return;
        case 188: // '<'
            if (event.shiftKey) {
                vgap.unrotateActiveObject();
                return;
            }
        case 190: // '>'
            if (event.shiftKey) {
                vgap.rotateActiveObject();
                return;
            }
        }
    }

    this.hotkey_McNimblesToolkit(event);
}

vgapMap.prototype.zoomFarInOrOut_OnWaypointOrPlanet = function() {
    var zoomedOut = (this.zoom < 29);
    if (zoomedOut) {
        this.zoomFactorBeforeZoomingInDeeply = this.zoom;
        this.setZoom(42);
    } else {
        this.setZoom(this.zoomFactorBeforeZoomingInDeeply);
    }
    var x, y;
    if (this.activeShip) {
        x = this.activeShip.targetx;
        y = this.activeShip.targety;
    } else if (this.activePlanet) {
        x = this.activePlanet.x;
        y = this.activePlanet.y;
    } else {
        x = this.centerX;
        y = this.centerY;
    }

    this.centerX = x;
    this.centerY = y;
    this.canvas.x = x - $(window).width() / 2 / this.zoom;
    this.canvas.y = y - $(window).height() / 2 / this.zoom;
    this.draw();
}

vgaPlanets.prototype.rotateActiveObject = function() {
    if (vgap.planetScreenOpen) {
        var planet = vgap.map.activePlanet;
        if (planet.isbase) {
            vgap.map.selectStarbase(planet.id);
        } else {
            var ships = vgap.shipsAt(planet.x, planet.y);
            if (ships.length > 0) vgap.map.selectShip(ships[0].id);
            // else only a planet
        }
    } else if (vgap.starbaseScreenOpen) {
        var planet = vgap.map.activePlanet;
        var ships = vgap.shipsAt(planet.x, planet.y);
        if (ships.length > 0) {
            vgap.map.selectShip(ships[0].id);
        } else {
            vgap.map.selectPlanet(planet.id);
        }
    } else if (vgap.shipScreenOpen) {
        var ship = vgap.map.activeShip;
        var ships = vgap.shipsAt(ship.x, ship.y)
        var index = ships.indexOf(ship);
        if (index == ships.length - 1) {
            var planet = vgap.planetAt(ship.x, ship.y)
            if (planet) {
                vgap.map.selectPlanet(planet.id);
            } else if (ships.length > 1) {
                vgap.map.selectShip(ships[0].id);
            } // else Cannot rotate 1 ship
        } else {
            vgap.map.selectShip(ships[index+1].id);
        }
    }
}

vgaPlanets.prototype.unrotateActiveObject = function() {
    if (vgap.planetScreenOpen) {
        var planet = vgap.map.activePlanet;
        var ships = vgap.shipsAt(planet.x, planet.y);
        if (ships.length > 0) {
            vgap.map.selectShip(ships[ships.length-1].id);
        } else if (planet.isbase) {
            vgap.map.selectStarbase(planet.id);
        } // else only a planet
    } else if (vgap.starbaseScreenOpen) {
        var planet = vgap.map.activePlanet;
        vgap.map.selectPlanet(planet.id);
    } else if (vgap.shipScreenOpen) {
        var ship = vgap.map.activeShip;
        var ships = vgap.shipsAt(ship.x, ship.y)
        var index = ships.indexOf(ship);
        if (index == 0) {
            var planet = vgap.planetAt(ship.x, ship.y)
            if (planet) {
                if (planet.isbase) {
                    vgap.map.selectStarbase(planet.id);
                } else {
                    vgap.map.selectPlanet(planet.id);
                }
            } else {
                if (ships.length > 1) vgap.map.selectShip(ships[ships.length-1].id);
            }
        } else {
            vgap.map.selectShip(ships[index-1].id);
        }
    }
}