| Scripts/mfdfastconfiguration.js |
"use strict";
this.name = "MFDFastConfiguration";
this.author = "phkb";
this.copyright = "2016 phkb";
this.description = "Primable equipment that allows different sets of MFD's to be quickly shown.";
this.licence = "CC BY-NC-SA 3.0";
this._sets = []; // our data array of different MFD sets
this._startpoint = 0; // starting point for the mode, which will either be 0 or the altstart point
this._curr_mode = -1; // current mode of the primable equipment
this._altstart = 21; // alternate starting point when "Hide all setting functions" is enabled
this._default = ""; // default MFD set, to be activated when docking so HUD Selector picks up the right MFD's
this._hudSelectorInstalled = false;
this._currentMFDSet = [];
this._activeMode = true;
this._currentSetName = "";
this._lastSetName = "";
this._playerIsDocking = false; // flag to indicate the player has requested docking clearance
this._dockTimer = null; // timer used when docking to determine if the player has exceeded the alotted time
this._trueValues = ["yes", "1", 1, "true", true];
this._changing = [];
this._hairSpace = String.fromCharCode(31);
this._hairSpaceLength = defaultFont.measureString(this._hairSpace);
this._ellip = "…";
this._menu = 0;
this._lastChoice = "";
this._lastMenu = "";
this._selectedIndex = 0;
this._setArray = [];
this._changes = false;
// list of different modes
this._modes = expandMissionText("mfd_modes").split("|");
//-------------------------------------------------------------------------------------------------------------
this.activated = function () {
var w = worldScripts.MFDFastConfiguration;
switch (w._curr_mode) {
case 0:
w.$storeMFDSet("set1", false);
w._currentSetName = "set1";
player.consoleMessage(expandMissionText("mfd_current_saved", { set: expandMissionText("mfd_" + w._currentSetName) }));
break;
case 1:
w.$storeMFDSet("set2", false);
w._currentSetName = "set2";
player.consoleMessage(expandMissionText("mfd_current_saved", { set: expandMissionText("mfd_" + w._currentSetName) }));
break;
case 2:
w.$storeMFDSet("set3", false);
w._currentSetName = "set3";
player.consoleMessage(expandMissionText("mfd_current_saved", { set: expandMissionText("mfd_" + w._currentSetName) }));
break;
case 3:
w.$storeMFDSet("setGreen", false);
w._currentSetName = "setGreen";
player.consoleMessage(expandMissionText("mfd_current_saved", { set: expandMissionText("mfd_" + w._currentSetName) }));
break;
case 4:
w.$storeMFDSet("setYellow", false);
w._currentSetName = "setYellow";
player.consoleMessage(expandMissionText("mfd_current_saved", { set: expandMissionText("mfd_" + w._currentSetName) }));
break;
case 5:
w.$storeMFDSet("setRed", false);
w._currentSetName = "setRed";
player.consoleMessage(expandMissionText("mfd_current_saved", { set: expandMissionText("mfd_" + w._currentSetName) }));
break;
case 6:
w.$storeMFDSet("setDocking", false);
w._currentSetName = "setDocking";
player.consoleMessage(expandMissionText("mfd_current_saved", { set: expandMissionText("mfd_" + w._currentSetName) }));
break;
case 7:
w._default = "set1";
player.consoleMessage(expandMissionText("mfd_default", { set: expandMissionText("mfd_" + w._default) }));
break;
case 8:
w._default = "set2";
player.consoleMessage(expandMissionText("mfd_default", { set: expandMissionText("mfd_" + w._default) }));
break;
case 9:
w._default = "set3";
player.consoleMessage(expandMissionText("mfd_default", { set: expandMissionText("mfd_" + w._default) }));
break;
case 10:
w._default = "setGreen";
player.consoleMessage(expandMissionText("mfd_default", { set: expandMissionText("mfd_" + w._default) }));
break;
case 11:
w._default = "setYellow";
player.consoleMessage(expandMissionText("mfd_default", { set: expandMissionText("mfd_" + w._default) }));
break;
case 12:
w._default = "setRed";
player.consoleMessage(expandMissionText("mfd_default", { set: expandMissionText("mfd_" + w._default) }));
break;
case 13:
w.$deleteMFDSet("set1");
break;
case 14:
w.$deleteMFDSet("set2");
break;
case 15:
w.$deleteMFDSet("set3");
break;
case 16:
w.$deleteMFDSet("setGreen");
break;
case 17:
w.$deleteMFDSet("setYellow");
break;
case 18:
w.$deleteMFDSet("setRed");
break;
case 19:
w.$deleteMFDSet("setDocking");
break;
case 20:
w._startpoint = this._altstart;
player.consoleMessage(expandMissionText("mfd_settings_hidden"));
break;
case 21:
if (w._default != "") w.$restoreMFDSet(w._default, false);
break;
case 22:
//this.$updateCurrentSet();
w.$restoreMFDSet("set1", false);
break;
case 23:
this.$updateCurrentSet();
w.$restoreMFDSet("set2", false);
break;
case 24:
this.$updateCurrentSet();
w.$restoreMFDSet("set3", false);
break;
case 25:
this.$updateCurrentSet();
w.$restoreMFDSet("setGreen", false);
break;
case 26:
this.$updateCurrentSet();
w.$restoreMFDSet("setYellow", false);
break;
case 27:
this.$updateCurrentSet();
w.$restoreMFDSet("setRed", false);
break;
case 28:
this.$updateCurrentSet();
w.$restoreMFDSet("setDocking", false);
break;
case 29:
w._activeMode = !w._activeMode;
if (w._activeMode === true) {
w._modes[29] = expandMissionText("mfd_disable_auto");
player.consoleMessage(expandMissionText("mfd_enable_auto_notify"));
}
if (w._activeMode === false) {
w._modes[29] = expandMissionText("mfd_enable_auto");
player.consoleMessage(expandMissionText("mfd_disable_auto_notify"));
}
break;
case 30:
w._startpoint = 0;
w._curr_mode = -1;
player.consoleMessage(expandMissionText("mfd_settings_restored"));
break;
}
}
//-------------------------------------------------------------------------------------------------------------
this.mode = function () {
var w = worldScripts.MFDFastConfiguration;
w._curr_mode += 1;
// skip the last one (restore functions) if they're in the list already.
if (w._curr_mode == w._modes.length - 1 && w._startpoint === 0) w._curr_mode += 1;
if (w._curr_mode == w._modes.length) w._curr_mode = w._startpoint;
// tell the player what mode they're in
player.consoleMessage(w._modes[w._curr_mode]);
}
//-------------------------------------------------------------------------------------------------------------
this.$deleteMFDSet = function (setName) {
// see if the set is stored
var idx = this.$setNameIndex(setName);
if (idx >= 0) {
// found it! delete it and notify the player
this._sets.splice(idx, 1);
player.consoleMessage(expandMissionText("mfd_set_deleted"));
} else {
// notify the player that the set wasn't there to be deleted
player.consoleMessage(expandMissionText("mfd_set_not_found"));
}
if (this._currentSetName === setName) this._currentSetName = "";
}
//-------------------------------------------------------------------------------------------------------------
this.$storeMFDSet = function (setName, suppressMessages, autoSave) {
var p = player.ship;
var config = {};
// put all the settings in a dictionary object
if (p && p.multiFunctionDisplayList) {
for (var i = 0; i < p.multiFunctionDisplayList.length; i++) {
var noadd = false;
if (autoSave === true) {
// these two MFDs autohide themselves, so don't add them to the config
if (p.multiFunctionDisplayList[i] === "DamageReportMFD" && worldScripts.DamageReportMFD && worldScripts.DamageReportMFD._mode === 1) noadd = true;
if (p.multiFunctionDisplayList[i] === "BountySystem_WarrantScanner" && worldScripts.BountySystem_WarrantScanner) noadd = true;
}
if (noadd === false) config["MFD_" + (i < 10 ? "0" : "") + i] = p.multiFunctionDisplayList[i];
}
}
// see if this set is already stored (meaning we're doing an update)
var idx = this.$setNameIndex(setName);
if (idx >= 0) {
// ok, found it! reset the data here
this._sets[idx].data = JSON.stringify(config);
} else {
// this must be a new one, so store a new set
this._sets.push({ name: setName, data: JSON.stringify(config) });
}
// notify the player
if (suppressMessages == false) player.consoleMessage(expandMissionText("mfd_set_stored"));
}
//-------------------------------------------------------------------------------------------------------------
this.$restoreMFDSet = function (setName, suppressMessages) {
var config = {};
// see if we've got this set stored
var idx = this.$setNameIndex(setName);
if (idx >= 0) {
config = JSON.parse(this._sets[idx].data);
} else {
// report back if not
if (suppressMessages == false) {
player.consoleMessage(expandMissionText("mfd_set_not_defined"));
}
return;
}
// restore the set
var p = player.ship;
if (p && p.isValid && p.multiFunctionDisplayList) {
// check for the autohiding SDC MFD
var bSDC = false;
if (worldScripts.StationDockControl_MFD) {
if (worldScripts.StationDockControl_MFD._mfdID >= 0) {
bSDC = true;
worldScripts.StationDockControl_MFD._mfdID = -1;
}
}
for (var i = 0; i < p.multiFunctionDisplayList.length; i++) {
var itm = config["MFD_" + (i < 10 ? "0" : "") + i];
try {
p.setMultiFunctionDisplay(i, itm);
if (itm === "LaunchQueueMFD" && bSDC === true) {
//log(this.name, "setting SDC " + i);
worldScripts.StationDockControl_MFD._mfdID = i;
}
}
catch (err) {
log(this.name, "!!ERROR: Unable to set MFD " + (i + 1) + " to " + itm);
}
}
if (worldScripts.DockMonitor_MFD) {
if (worldScripts.DockMonitor_MFD._mfdID >= 0) {
bSDC = true;
worldScripts.DockMonitor_MFD._mfdID = -1;
}
}
for (var i = 0; i < p.multiFunctionDisplayList.length; i++) {
var itm = config["MFD_" + (i < 10 ? "0" : "") + i];
try {
p.setMultiFunctionDisplay(i, itm);
if (itm === "DockMonitor_MFD" && bSDC === true) {
//log(this.name, "setting SDC " + i);
worldScripts.StationDockControl_MFD._mfdID = i;
}
}
catch (err) {
log(this.name, "!!ERROR: Unable to set MFD " + (i + 1) + " to " + itm);
}
}
// notify the player
if (suppressMessages == false) {
player.consoleMessage(expandMissionText("mfd_set_activated"));
}
this._currentSetName = setName;
// if the player has made changes outside of active mode, make sure they stay
// only applies to automatic changes based on condition
if (this._activeMode === false) {
for (var i = 0; i < this._changing.length; i++) {
if (this._changing[i] != "" && this._changing[i] != undefined) {
p.setMultiFunctionDisplay(i, this._changing[i]);
}
}
}
}
p = null;
}
//-------------------------------------------------------------------------------------------------------------
this.$setNameIndex = function (setName) {
for (var i = 0; i < this._sets.length; i++) {
if (this._sets[i].name == setName) return i;
}
return -1;
}
//-------------------------------------------------------------------------------------------------------------
this.startUpComplete = function () {
if (missionVariables.MFDFastConfig_Data) {
this._sets = JSON.parse(missionVariables.MFDFastConfig_Data);
delete missionVariables.MFDFastConfig_Data;
}
if (missionVariables.MFDFastConfig_Default) {
this._default = missionVariables.MFDFastConfig_Default;
delete missionVariables.MFDFastConfig_Default;
}
if (missionVariables.MFDFastConfig_HideSettings) {
if (missionVariables.MFDFastConfig_HideSettings == false) {
this._startpoint = 0;
} else {
this._startpoint = this._altstart;
this._curr_mode = this._startpoint;
}
delete missionVariables.MFDFastConfig_HideSettings;
}
if (missionVariables.MFDFastConfig_AutoUpdate) {
this._activeMode = (this._trueValues.indexOf(missionVariables.MFDFastConfig_AutoUpdate) === -1 ? false : true);
if (this._activeMode === true) this._modes[29] = expandMissionText("mfd_disable_auto");
if (this._activeMode === false) this._modes[29] = expandMissionText("mfd_enable_auto");
}
if (worldScripts.hudselector) this._hudSelectorInstalled = true;
this.$initInterface(player.ship.dockedStation);
}
//-------------------------------------------------------------------------------------------------------------
this.alertConditionChanged = function (newCondition, oldCondition) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
// note: I'm spoofing an alert condition of "4", meaning "player is docking"
if (newCondition === 0) return;
var idx = 0;
var idxGr = this.$setNameIndex("setGreen");
var idxYw = this.$setNameIndex("setYellow");
var idxRd = this.$setNameIndex("setRed");
var idxDk = this.$setNameIndex("setDocking");
if (["set1", "set2", "set3", "setGreen", "setYellow", "setRed", "setDocking"].indexOf(this._currentSetName) >= 0) {
this._lastSetName = this._currentSetName;
}
// auto store the old state before we switch to any alert-condition set (if there are any)
// but only if we have an alert condition set to switch to
if (this._activeMode === true && ((newCondition === 1 && idxGr >= 0) || (newCondition === 2 && idxYw >= 0) || (newCondition === 3 && idxRd >= 0) || (newCondition === 4 && idxDk >= 0))) {
this.$updateCurrentSet();
}
if (newCondition === 1) {
if (idxGr >= 0) this.$restoreMFDSet("setGreen", false);
}
if (newCondition === 2) {
if (idxYw >= 0) this.$restoreMFDSet("setYellow", false);
}
if (newCondition === 3) {
if (idxRd >= 0) this.$restoreMFDSet("setRed", false);
}
if (newCondition === 4) {
if (idxDk >= 0) this.$restoreMFDSet("setDocking", false);
}
}
//-------------------------------------------------------------------------------------------------------------
this.$updateCurrentSet = function () {
// auto store the old state before we switch to any alert-condition set (if there are any)
if (this._activeMode === true) {
// check for currently active set and update
if (["set1", "set2", "set3", "setGreen", "setYellow", "setRed", "setDocking"].indexOf(this._currentSetName) >= 0) {
var idx = this.$setNameIndex(this._currentSetName);
if (idx >= 0) this.$storeMFDSet(this._currentSetName, true, true);
}
}
}
//-------------------------------------------------------------------------------------------------------------
this.playerWillSaveGame = function () {
// store all our data
if (this._sets.length > 0) {
missionVariables.MFDFastConfig_Data = JSON.stringify(this._sets);
}
missionVariables.MFDFastConfig_Default = this._default;
if (this._startpoint != 0) {
missionVariables.MFDFastConfig_HideSettings = true;
} else {
missionVariables.MFDFastConfig_HideSettings = false;
}
missionVariables.MFDFastConfig_AutoUpdate = this._activeMode;
}
//-------------------------------------------------------------------------------------------------------------
this.shipWillDockWithStation = function (station) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
// auto store the old state before we switch to any alert-condition set (if there are any)
this.$updateCurrentSet();
this._changing.length = 0;
// if we have a docking set, and there's a previous set in play, and we don't have a default
// switch to the previous set we had so it can be made the default
// this is to avoid having the docking set pulled up on launch
if (this._currentSetName === "setDocking" && this._lastSetName !== "" && this._default === "") {
this.$restoreMFDSet(this._lastSetName, false);
}
if (this._hudSelectorInstalled == false && this._default == "") {
// save the current config so we can put it back on launch
this.$storeMFDSet("current", true, true);
}
if (this._default !== "") {
// restore the default so HUD Selector picks up the correct MFD's
this.$restoreMFDSet(this._default, true);
}
this.$stopTimers();
}
//-------------------------------------------------------------------------------------------------------------
this.shipLaunchedFromStation = function (station) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
this._playerIsDocking = false;
// if HUD selector isn't installed, lets restore either the default set (if defined) or what we had when we docked.
// if HUD selector is installed, it will handle restoring the MFD config for us.
if (this._hudSelectorInstalled == false) {
if (this._default != "") {
// restore the default MFD set
this.$restoreMFDSet(this._default, true);
} else {
this.$restoreMFDSet("current", true);
}
}
}
//-------------------------------------------------------------------------------------------------------------
this.playerBoughtEquipment = function (equipmentKey) {
if (equipmentKey == "EQ_MFD_FAST_CONFIG_REMOVAL") {
// give the player their money back
player.credits += 600;
var p = player.ship;
p.removeEquipment(equipmentKey);
p.removeEquipment("EQ_MFD_FAST_CONFIG");
this._sets = [];
this._default = "";
this.startpoint = 0;
player.consoleMessage(expandMissionText("mfd_config_removed"));
}
if (equipmentKey == "EQ_MFD_FAST_CONFIG") {
this.$initInterface(player.ship.dockedStation);
}
}
//-------------------------------------------------------------------------------------------------------------
this.mfdKeyChanged = function (index, key) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
// are we in auto mode? don't do anything if we are
if (this._activeMode === true) return;
// if not, make a note of what we're changing
if (this._changing.length === 0) this._changing = new Array(player.ship.multiFunctionDisplayList.length);
this._changing[index] = player.ship.multiFunctionDisplayList[index];
}
//-------------------------------------------------------------------------------------------------------------
this.shipDied = function (whom, why) {
this.$stopTimers();
}
//-------------------------------------------------------------------------------------------------------------
this.playerRequestedDockingClearance = function (message) {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
if (message === "DOCKING_CLEARANCE_GRANTED" || message === "DOCKING_CLEARANCE_EXTENDED" || message === "DOCKING_CLEARANCE_DENIED_TRAFFIC_INBOUND" || message === "DOCKING_CLEARANCE_DENIED_TRAFFIC_OUTBOUND") {
this._playerIsDocking = true;
this.alertConditionChanged(4, player.alertCondition);
if (message === "DOCKING_CLEARANCE_GRANTED" || message === "DOCKING_CLEARANCE_EXTENDED") {
// start a timer so we can track when the clearance expires
if (this._dockTimer && this._dockTimer.isRunning) this._dockTimer.stop();
this._dockTimer = new Timer(this, this.$dockingExpired, 121, 0);
}
} else {
this.alertConditionChanged(player.alertCondition, 4);
this._playerIsDocking = false;
if (this._dockTimer && this._dockTimer.isRunning) this._dockTimer.stop();
}
}
//-------------------------------------------------------------------------------------------------------------
this.playerDockingClearanceCancelled = function () {
if (player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") != "EQUIPMENT_OK") return;
this.alertConditionChanged(player.alertCondition, 4);
this._playerIsDocking = false;
if (this._dockTimer && this._dockTimer.isRunning) this._dockTimer.stop();
}
//-------------------------------------------------------------------------------------------------------------
// stops the dock timer
this.$stopTimers = function () {
// stop the dock timer check timer if it's running
if (this._dockTimer && this._dockTimer.isRunning) this._dockTimer.stop();
}
//-------------------------------------------------------------------------------------------------------------
// checks for an expired docking request
this.$dockingExpired = function $dockingExpired() {
if (this._playerIsDocking === true) {
this.alertConditionChanged(player.alertCondition, 4);
this._playerIsDocking = false;
}
}
//-------------------------------------------------------------------------------------------------------------
this.shipDockedWithStation = function (station) {
this.$initInterface(station);
}
//-------------------------------------------------------------------------------------------------------------
this.missionScreenEnded = function () {
this._menu = 0;
this._changes = false;
this._lastMenu = "";
}
//-------------------------------------------------------------------------------------------------------------
this.$initInterface = function $initInterface(station) {
if (this._hudSelectorInstalled && player.ship.equipmentStatus("EQ_MFD_FAST_CONFIG") == "EQUIPMENT_OK") {
station.setInterface(this.name, {
title: expandMissionText("mfd_title"),
category: expandDescription("[interfaces-category-ship-systems]"),
summary: expandMissionText("mfd_summary"),
callback: this.$configSetSelection.bind(this)
});
}
}
//---------------------------------------------------------------------------------
this.$configSetSelection = function () {
var sets = ["set1", "set2", "set3", "setGreen", "setYellow", "setRed", "setDocking"];
var curChoices = {};
var msg = "";
var initKey = "99_EXIT";
if (this._menu == 0) {
for (var i = 0; i < sets.length; i++) {
var chk = this.$setNameIndex(sets[i]);
var nm = expandMissionText("mfd_" + sets[i]);
curChoices["01_EDIT_" + i + "_" + sets[i]] = {
text: expandMissionText("mfd_edit", { name: nm }) + (chk >= 0 ? expandMissionText("mfd_is_set") : expandMissionText("mfd_not_set")),
color: "yellowColor"
};
}
curChoices["02_DEFAULT"] = { text: expandMissionText("mfd_default_menu", { currentdefault: expandMissionText("mfd_set_" + this._default) }), color: "yellowColor" };
curChoices["03_SETTINGS"] = { text: (this._startpoint == 0 ? expandMissionText("mfd_settings_shown") : expandMissionText("mfd_settings_hide")), color: "orangeColor" };
curChoices["04_AUTO"] = { text: (this._activeMode ? expandMissionText("mfd_enable_auto_notify") : expandMissionText("mfd_disable_auto_notify")), color: "orangeColor" };
curChoices["99_EXIT"] = {
text: expandMissionText("mfd_exit"),
color: "orangeColor"
};
msg = expandMissionText("mfd_config_main");
}
if (this._menu == 1) {
for (var i = 0; i < sets.length; i++) {
var chk = this.$setNameIndex(sets[i]);
var nm = expandMissionText("mfd_" + sets[i]);
curChoices["01_DEFAULT_" + i + "_" + sets[i]] = {
text: expandMissionText("mfd_set_default", { name: nm }) + (sets[i] == this._default ? expandMissionText("mfd_default_current") : ""),
color: (chk >= 0 ? "yellowColor" : "grayColor"),
unselectable: (chk == -1)
};
}
curChoices["98_EXIT"] = {
text: expandMissionText("mfd_return"),
color: "orangeColor"
};
msg = expandMissionText("mfd_config_defaults");
initKey = "98_EXIT";
}
if (this._lastMenu) initKey = this._lastMenu;
var opts = {
titleKey: "mfd_title",
screenID: "oolite-mfd-config-map",
allowInterrupt: true,
exitScreen: "GUI_SCREEN_INTERFACES",
choices: curChoices,
overlay: { name: "mfd_settings.png", height: 546 },
initialChoicesKey: initKey,
message: msg
};
mission.runScreen(opts, this.$configSetHandler, this);
}
//---------------------------------------------------------------------------------
this.$configSetHandler = function (choice) {
var p = player.ship;
this._lastMenu = choice;
if (choice.indexOf("01_EDIT") >= 0) {
this._selectedSet = choice.split("_")[3];
var idx = this.$setNameIndex(this._selectedSet);
if (idx >= 0) {
var config = JSON.parse(this._sets[idx].data);
this._setArray.length = 0;
for (var i = 0; i < p.multiFunctionDisplays; i++) {
this._setArray.push(config["MFD_" + (i < 10 ? "0" : "") + i]);
}
}
this.$updateInterfaceMFD();
return;
}
if (choice.indexOf("01_DEFAULT") >= 0) {
this._default = choice.split("_")[3];
player.consoleMessage(expandMissionText("mfd_default", { set: expandMissionText("mfd_" + this._default) }));
}
if (choice == "02_DEFAULT") {
this._menu = 1;
this._lastMenu = "";
}
if (choice == "03_SETTINGS") {
if (this._startpoint == 0) {
this._startpoint = this._altstart
} else {
this._startpoint = 0;
}
}
if (choice == "04_AUTO") {
this._activeMode = !this._activeMode;
}
if (choice == "98_EXIT") {
this._menu = 0;
this._lastMenu = "";
}
if (choice != "99_EXIT") this.$configSetSelection();
}
//---------------------------------------------------------------------------------
this.$updateInterfaceMFD = function () {
var hs = worldScripts.hudselector;
var p = player.ship;
var len = 40;
if (p && p.isValid) len = p.multiFunctionDisplays;
var maxc = 20; //max.22 lines in total with done and exit
var dialno = this._selectedIndex;
if (dialno >= len) dialno = 0; //go back to the start
var index = 10;
var ch = {};
for (var j = dialno; j < dialno + maxc && j < len; j++) {
var z = hs.$lookupMFDDisplayName(this._setArray[j]);
if (!z) z = "undefined";
ch["_" + index.toString()] = this.$padText(expandMissionText("mfd_slot", { index: (j + 1) }), 6) + this.$padText(z, 17);
index += 1;
}
// only put the "more" item on if there are more to show
if (len > maxc) ch["_M"] = { text: expandMissionText("mfd_more"), color: "orangeColor" };
ch["_MS"] = { text: (this._changes ? "* " : "") + expandMissionText("mfd_save") + (this._changes ? " *" : ""), color: (this._changes ? "whiteColor" : "orangeColor") };
ch["_MX"] = { text: expandMissionText("mfd_return"), color: "orangeColor" };
ch["_MD"] = { text: expandMissionText("mfd_delete_set"), color: "orangeColor" };
var ic = "_MX";
if (this._lastChoice) ic = this._lastChoice;
// checking for the presence of a new function in the global object that indicates the version of Oolite is sufficient for the extra keys to work
mission.runScreen({
titleKey: "mfd_selector",
screenID: "oolite-mfd-config-map",
message: (global.getColorSaturation ? expandMissionText("mfd_selectortextnew", { setname: expandMissionText("mfd_" + this._selectedSet) }) : expandMissionText("mfd_selectortext_nrml", { setname: expandMissionText("mfd_" + this._selectedSet) })),
registerKeys: { "left": [{ key: "arrowLeft" }], "right": [{ key: "arrowRight" }], "esc": [{ key: "esc" }] },
allowInterrupt: true,
initialChoicesKey: ic,
overlay: { name: "mfd_settings.png", height: 546 },
exitScreen: "GUI_SCREEN_INTERFACES",
choices: ch
},
this.$mfdSelector_Choice, this
);
}
//-------------------------------------------------------------------------------------------------------------
this.$mfdSelector_Choice = function (choice, key) {
var hs = worldScripts.hudselector
var p = player.ship;
var maxc = 20; //max.22 lines in total with done and exit
this._lastChoice = choice;
if (((!key || key == null || key == "enter") && choice == "_MX") || key == "esc") {
this._changes = false;
this.$configSetSelection();
return;
}
if ((key == null || key == "enter") && choice == "_M") {
this._lastChoice = null;
this._selectedIndex += maxc;
} else if ((key == null || key == "enter") && choice == "_MS") {
var config = {};
var undef = 0;
for (var i = 0; i < p.multiFunctionDisplays; i++) {
if (this._setArray[i] == "undefined" || this._setArray[i] == null) {
undef += 1;
} else {
config["MFD_" + (i < 10 ? "0" : "") + i] = this._setArray[i];
}
}
var idx = this.$setNameIndex(this._selectedSet);
if (idx >= 0) {
// ok, found it! reset the data here
if (undef == p.multiFunctionDisplays) {
this._sets.splice(idx, 1);
} else {
this._sets[idx].data = JSON.stringify(config);
}
} else {
// this must be a new one, so store a new set
if (undef < p.multiFunctionDisplays) {
this._sets.push({ name: this._selectedSet, data: JSON.stringify(config) });
}
}
player.consoleMessage(expandMissionText("mfd_set_stored"));
this._changes = false;
} else if ((key == null || key == "enter") && choice == "_MD") {
this.$deleteMFDSet(this._selectedSet);
this._setArray = [];
for (var i = 0; i < p.multiFunctionDisplays; i++) {
this._setArray.push("undefined");
}
this._changes = false;
} else {
var t = -1;
if (choice) t = choice.substr(1) - 10; //cut starting "_"
if (t >= 0) {
this._changes = true;
t += this._selectedIndex;
var m = this._setArray[t];
var mfd = -1;
if (m && m.length > 0 && m != "undefined") {
for (var i = 0; i < hs.$HUDSelectorMFDs.length; i++) {
if (hs.$HUDSelectorMFDs[i] &&
(m == hs.$HUDSelectorMFDs[i][0] || m == hs.$HUDSelectorMFDs[i][1]))
mfd = i + (key == "left" ? -1 : 1); //step to the next MFD
}
} else {
if (key == "right" && m == "undefined") mfd = 0;
if (key == "left" && m == "undefined") mfd = hs.$HUDSelectorMFDs.length - 1;
}
if (mfd >= hs.$HUDSelectorMFDs.length || mfd < -1) {
if (mfd == -2) mfd = hs.$HUDSelectorMFDs.length - 1; else mfd = -1;
}
if (mfd == -1) {
this._setArray[t] = "undefined";
} else {
var f = hs.$HUDSelectorMFDs[mfd][1]; //mfdname
if (!f) f = hs.$HUDSelectorMFDs[mfd][0]; //worldscriptname
this._setArray[t] = f;
}
}
}
this.$updateInterfaceMFD();
}
//-------------------------------------------------------------------------------------------------------------
// appends space to currentText to the specified length in 'em'
this.$padText = function $padText(currentText, desiredLength, leftSwitch, centreSwitch) {
var that = $padText;
var hairSpace = (that.hairSpace = that.hairSpace || this._hairSpace);
var ellip = (that.ellip = that.ellip || this._ellip);
var hairSpaceLength = (that.hairSpaceLength = that.hairSpaceLength || this._hairSpaceLength);
var measure = (that.measure = that.measure || defaultFont.measureString);
if (currentText == null) currentText = "";
var currentLength = measure(currentText.replace(/%%/g, "%"));
// calculate number needed to fill remaining length
var padsNeeded = Math.floor((desiredLength - currentLength) / hairSpaceLength);
if (padsNeeded < 1) {
// text is too long for column, so start pulling characters off
var tmp = currentText;
do {
tmp = tmp.substring(0, tmp.length - 2) + ellip;
if (tmp === ellip) break;
} while (measure(tmp.replace(/%%/g, "%")) > desiredLength);
currentLength = measure(tmp.replace(/%%/g, "%"));
padsNeeded = Math.floor((desiredLength - currentLength) / hairSpaceLength);
currentText = tmp;
}
// quick way of generating a repeated string of that number
if (!leftSwitch || leftSwitch === false) {
if (!centreSwitch || centreSwitch === false) {
return currentText + new Array(padsNeeded).join(hairSpace);
} else {
return currentText + new Array(parseInt(padsNeeded / 2)).join(hairSpace);
}
} else {
if (!centreSwitch || centreSwitch === false) {
return new Array(padsNeeded).join(hairSpace) + currentText;
} else {
return new Array(parseInt(padsNeeded / 2)).join(hairSpace) + currentText;
}
}
}
|