Advertisement
Guest User

Untitled

a guest
Sep 17th, 2020 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BaseInstrument extends TemplateElement {
  2.     constructor() {
  3.         super();
  4.         this.urlConfig = new URLConfig();
  5.         this.xmlConfigLoading = false;
  6.         this.frameCount = 0;
  7.         this._lastTime = 0;
  8.         this._isConnected = false;
  9.         this._isInitialized = false;
  10.         this._quality = Quality.high;
  11.         this._gameState = GameState.ingame;
  12.         this._deltaTime = 0;
  13.         this._alwaysUpdate = false;
  14.         this.highlightList = [];
  15.         this.backgroundList = [];
  16.         this.pendingCalls = [];
  17.         this.dataMetaManager = new DataReadMetaManager();
  18.     }
  19.     get initialized() { return this._isInitialized; }
  20.     get instrumentIdentifier() { return this._instrumentId; }
  21.     get instrumentIndex() { return (this.urlConfig.index != null) ? this.urlConfig.index : 1; }
  22.     get isInteractive() { return false; }
  23.     get IsGlassCockpit() { return false; }
  24.     get isPrimary() { return (this.urlConfig.index == null || this.urlConfig.index == 1); }
  25.     get deltaTime() { return this._deltaTime; }
  26.     connectedCallback() {
  27.         super.connectedCallback();
  28.         this.electricity = this.getChildById("Electricity");
  29.         this.highlightSvg = this.getChildById("highlight");
  30.         this.loadDocumentAttributes();
  31.         this.loadURLAttributes();
  32.         this.loadXMLConfig();
  33.         window.document.addEventListener("OnVCockpitPanelAttributesChanged", this.loadDocumentAttributes.bind(this));
  34.         this.startTime = Date.now();
  35.         if (this.getGameState() != GameState.mainmenu) {
  36.             this.createMainLoop();
  37.         }
  38.     }
  39.     disconnectedCallback() {
  40.         super.disconnectedCallback();
  41.         this._isConnected = false;
  42.     }
  43.     Init() {
  44.         this._isInitialized = true;
  45.         this.initTransponder();
  46.     }
  47.     setInstrumentIdentifier(_identifier) {
  48.         if (_identifier && _identifier != "" && _identifier != this.instrumentIdentifier) {
  49.             this._instrumentId = _identifier;
  50.             var guid = this.getAttribute("Guid");
  51.             if (guid != undefined) {
  52.                 LaunchFlowEvent("ON_VCOCKPIT_INSTRUMENT_INITIALIZED", guid, this.instrumentIdentifier, this.isInteractive, this.IsGlassCockpit);
  53.             }
  54.         }
  55.     }
  56.     setConfigFile(_path) {
  57.         this._xmlConfigPath = _path;
  58.     }
  59.     getChildById(_selector) {
  60.         if (_selector == "")
  61.             return null;
  62.         if (!_selector.startsWith("#") && !_selector.startsWith("."))
  63.             _selector = "#" + _selector;
  64.         var child = this.querySelector(_selector.toString());
  65.         return child;
  66.     }
  67.     getChildrenById(_selector) {
  68.         if (_selector == "")
  69.             return null;
  70.         if (!_selector.startsWith("#") && !_selector.startsWith("."))
  71.             _selector = "#" + _selector;
  72.         var children = this.querySelectorAll(_selector.toString());
  73.         return children;
  74.     }
  75.     getChildrenByClassName(_selector) {
  76.         return this.getElementsByClassName(_selector);
  77.     }
  78.     startHighlight(_id) {
  79.         let elem = this.getChildById(_id);
  80.         if (elem) {
  81.             let highlight = new HighlightedElement();
  82.             highlight.elem = elem;
  83.             highlight.style = elem.style.cssText;
  84.             this.highlightList.push(highlight);
  85.         }
  86.         let elems = this.getChildrenByClassName(_id);
  87.         for (let i = 0; i < elems.length; i++) {
  88.             let highlight = new HighlightedElement();
  89.             highlight.elem = elems[i];
  90.             highlight.style = elems[i].style.cssText;
  91.             this.highlightList.push(highlight);
  92.         }
  93.         this.updateHighlightElements();
  94.     }
  95.     stopHighlight(_id) {
  96.         let elem = this.getChildById(_id);
  97.         if (elem) {
  98.             for (let i = 0; i < this.highlightList.length; i++) {
  99.                 if (this.highlightList[i].elem == elem) {
  100.                     elem.style.cssText = this.highlightList[i].style;
  101.                     this.highlightList.splice(i, 1);
  102.                 }
  103.             }
  104.         }
  105.         let elems = this.getChildrenByClassName(_id);
  106.         for (let i = 0; i < elems.length; i++) {
  107.             for (let j = 0; j < this.highlightList.length; j++) {
  108.                 if (this.highlightList[j].elem == elems[i]) {
  109.                     elems[i].style.cssText = this.highlightList[j].style;
  110.                     this.highlightList.splice(j, 1);
  111.                 }
  112.             }
  113.         }
  114.         this.updateHighlightElements();
  115.     }
  116.     clearHighlights() {
  117.         this.highlightList = [];
  118.         this.updateHighlightElements();
  119.     }
  120.     updateHighlightElements() {
  121.         for (let i = 0; i < this.backgroundList.length; i++) {
  122.             this.backgroundList[i].remove();
  123.         }
  124.         this.backgroundList = [];
  125.         if (this.highlightList.length > 0) {
  126.             this.highlightSvg.setAttribute("active", "true");
  127.             let elems = "";
  128.             for (let i = 0; i < this.highlightList.length; i++) {
  129.                 let rect = this.highlightList[i].elem.getBoundingClientRect();
  130.                 if (this.highlightList[i] instanceof HTMLElement) {
  131.                     let bg = document.createElement("div");
  132.                     bg.style.backgroundColor = "rgba(0,0,0,0.9)";
  133.                     bg.style.zIndex = "-1";
  134.                     bg.style.left = this.highlightList[i].elem.offsetLeft.toString() + "px";
  135.                     bg.style.top = this.highlightList[i].elem.offsetTop.toString() + "px";
  136.                     bg.style.width = rect.width.toString() + "px";
  137.                     bg.style.height = rect.height.toString() + "px";
  138.                     bg.style.position = "absolute";
  139.                     this.highlightList[i].elem.parentElement.appendChild(bg);
  140.                     this.backgroundList.push(bg);
  141.                 }
  142.                 if (i > 0) {
  143.                     elems += ";";
  144.                 }
  145.                 elems += rect.left + " ";
  146.                 elems += rect.top + " ";
  147.                 elems += rect.right + " ";
  148.                 elems += rect.bottom;
  149.             }
  150.             this.highlightSvg.setAttribute("elements", elems);
  151.         }
  152.         else {
  153.             this.highlightSvg.setAttribute("active", "false");
  154.         }
  155.     }
  156.     onInteractionEvent(_args) {
  157.     }
  158.     onSoundEnd(_event) {
  159.     }
  160.     getQuality() {
  161.         if (this._alwaysUpdate && this._quality != Quality.disabled) {
  162.             return Quality.high;
  163.         }
  164.         return this._quality;
  165.     }
  166.     getGameState() {
  167.         return this._gameState;
  168.     }
  169.     reboot() {
  170.         console.log("Rebooting Instrument...");
  171.         this.initTransponder();
  172.     }
  173.     onFlightStart() {
  174.         console.log("Flight Starting...");
  175.         SimVar.SetSimVarValue("L:HUD_AP_SELECTED_SPEED", "Number", 0);
  176.         SimVar.SetSimVarValue("L:HUD_AP_SELECTED_ALTITUDE", "Number", 0);
  177.         this.dispatchEvent(new Event('FlightStart'));
  178.     }
  179.     onQualityChanged(_quality) {
  180.         this._quality = _quality;
  181.     }
  182.     onGameStateChanged(_oldState, _newState) {
  183.         if (_newState != GameState.mainmenu) {
  184.             this.createMainLoop();
  185.             if (_oldState == GameState.loading && (_newState == GameState.ingame || _newState == GameState.briefing)) {
  186.                 this.reboot();
  187.             }
  188.             else if (_oldState == GameState.briefing && _newState == GameState.ingame) {
  189.                 this.onFlightStart();
  190.             }
  191.         }
  192.         else {
  193.             this.killMainLoop();
  194.         }
  195.         this._gameState = _newState;
  196.     }
  197.     loadDocumentAttributes() {
  198.         var attr = undefined;
  199.         if (document.body.hasAttribute("quality"))
  200.             attr = document.body.getAttribute("quality");
  201.         else if (window.parent && window.parent.document.body.hasAttribute("quality"))
  202.             attr = window.parent.document.body.getAttribute("quality");
  203.         if (attr != undefined) {
  204.             var quality = Quality[attr];
  205.             if (quality != undefined && this._quality != quality) {
  206.                 this.onQualityChanged(quality);
  207.             }
  208.         }
  209.         if (document.body.hasAttribute("gamestate"))
  210.             attr = document.body.getAttribute("gamestate");
  211.         else if (window.parent && window.parent.document.body.hasAttribute("gamestate"))
  212.             attr = window.parent.document.body.getAttribute("gamestate");
  213.         if (attr != undefined) {
  214.             var state = GameState[attr];
  215.             if (state != undefined && this._gameState != state) {
  216.                 this.onGameStateChanged(this._gameState, state);
  217.             }
  218.         }
  219.     }
  220.     parseXMLConfig() {
  221.         if (this.instrumentXmlConfig) {
  222.             let electric = this.instrumentXmlConfig.getElementsByTagName("Electric");
  223.             if (electric.length > 0) {
  224.                 this.electricalLogic = new CompositeLogicXMLElement(this, electric[0]);
  225.             }
  226.             let alwaysUpdate = this.instrumentXmlConfig.getElementsByTagName("AlwaysUpdate");
  227.             if (alwaysUpdate.length > 0) {
  228.                 if (alwaysUpdate[0].textContent.toLowerCase() == "true") {
  229.                     this._alwaysUpdate = true;
  230.                 }
  231.             }
  232.         }
  233.     }
  234.     parseURLAttributes() {
  235.         var instrumentID = this.templateID;
  236.         if (this.urlConfig.index)
  237.             instrumentID += "_" + this.urlConfig.index;
  238.         this.setInstrumentIdentifier(instrumentID);
  239.         if (this.urlConfig.style)
  240.             this.setAttribute("instrumentstyle", this.urlConfig.style);
  241.     }
  242.     requestCall(_func) {
  243.         this.pendingCalls.push(_func);
  244.     }
  245.     beforeUpdate() {
  246.         {
  247.             var curTime = Date.now();
  248.             this._deltaTime = curTime - this._lastTime;
  249.             this._lastTime = curTime;
  250.         }
  251.         {
  252.             let length = this.pendingCalls.length;
  253.             if (length > 10)
  254.                 console.warn("Many pending calls this frame (" + length + ")");
  255.             for (let i = 0; i < length; i++) {
  256.                 this.pendingCalls[i]();
  257.             }
  258.             this.pendingCalls.splice(0, length);
  259.         }
  260.     }
  261.     Update() {
  262.         this.dataMetaManager.UpdateAll();
  263.         this.updateHighlight();
  264.     }
  265.     afterUpdate() {
  266.         this.frameCount++;
  267.     }
  268.     CanUpdate() {
  269.         var quality = this.getQuality();
  270.         if (quality == Quality.ultra) {
  271.             return true;
  272.         }
  273.         else if ((quality == Quality.high) || (quality == Quality.medium) || (quality == Quality.low))  {
  274.            
  275.             var frameCountResult = (this.frameCount % 2) != 0;
  276.             if(this.instrumentIdentifier.startsWith("Aera"))
  277.             {
  278.                 frameCountResult = (this.frameCount % 2) != 0;
  279.             }
  280.             else if(this.instrumentIdentifier.startsWith("AS3X"))
  281.             {
  282.                 if(this.instrumentIdentifier.includes("MFD"))
  283.                 {
  284.                     frameCountResult = (this.frameCount % 6) != 0;
  285.                 }
  286.                 else if(this.instrumentIdentifier.includes("PFD"))
  287.                 {
  288.                     frameCountResult = (this.frameCount % 2) != 0;
  289.                 }
  290.             }
  291.             else if(this.instrumentIdentifier.startsWith("AS1000"))
  292.             {
  293.                 if(this.instrumentIdentifier.includes("MFD"))
  294.                 {
  295.                     frameCountResult = (this.frameCount % 6) != 0;
  296.                 }
  297.                 else if(this.instrumentIdentifier.includes("PFD"))
  298.                 {
  299.                     frameCountResult = (this.frameCount % 2) != 0;
  300.                 }
  301.             }
  302.             else if(this.instrumentIdentifier.startsWith("AS3000"))
  303.             {
  304.                 if(this.instrumentIdentifier.includes("MFD"))
  305.                 {
  306.                     frameCountResult = (this.frameCount % 6) != 0;
  307.                 }
  308.                 else if(this.instrumentIdentifier.includes("PFD"))
  309.                 {
  310.                     frameCountResult = (this.frameCount % 2) != 0;
  311.                 }
  312.             }
  313.             else if(this.instrumentIdentifier.startsWith("A320") || this.instrumentIdentifier.startsWith("B747") || this.instrumentIdentifier.startsWith("B787"))
  314.             {
  315.                 frameCountResult = (this.frameCount % 4) != 0;
  316.                 if(this.instrumentIdentifier.includes("Com") || this.instrumentIdentifier.includes("CDU") || this.instrumentIdentifier.includes("FDW") || this.instrumentIdentifier.includes("FMC"))
  317.                 {
  318.                     frameCountResult = (this.frameCount % 7) != 0;
  319.                 }
  320.                 else if(this.instrumentIdentifier.includes("ALT") || this.instrumentIdentifier.includes("EICAS") || this.instrumentIdentifier.includes("FCU") || this.instrumentIdentifier.includes("HUD") || this.instrumentIdentifier.includes("IAS") || this.instrumentIdentifier.includes("PFD") || this.instrumentIdentifier.includes("SAI"))
  321.                 {
  322.                     frameCountResult = (this.frameCount % 3) != 0;
  323.                 }
  324.                 else if (this.instrumentIdentifier.includes("MFD"))
  325.                 {
  326.                     frameCountResult = (this.frameCount % 6) != 0;
  327.                 }
  328.             }
  329.            
  330.             if (frameCountResult) {
  331.                 return false;
  332.             }
  333.  
  334.         }
  335.         else if (quality == Quality.hidden) {
  336.             if ((this.frameCount % 128) != 0) {
  337.                 return false;
  338.             }
  339.         }
  340.         else if (quality == Quality.disabled) {
  341.             return false;
  342.         }
  343.         return true;
  344.     }
  345.     updateElectricity() {
  346.         let powerOn = this.isElectricityAvailable();
  347.         if (this.electricity) {
  348.             if (powerOn)
  349.                 this.electricity.setAttribute("state", "on");
  350.             else
  351.                 this.electricity.setAttribute("state", "off");
  352.         }
  353.         return powerOn;
  354.     }
  355.     isElectricityAvailable() {
  356.         if (this.electricalLogic) {
  357.             return this.electricalLogic.getValue() != 0;
  358.         }
  359.         return SimVar.GetSimVarValue("CIRCUIT AVIONICS ON", "Bool");
  360.     }
  361.     playInstrumentSound(soundId) {
  362.         if (this.isElectricityAvailable()) {
  363.             Coherent.call("PLAY_INSTRUMENT_SOUND", soundId);
  364.             return true;
  365.         }
  366.         return false;
  367.     }
  368.     createMainLoop() {
  369.         if (this._isConnected)
  370.             return;
  371.         this._lastTime = Date.now();
  372.         let updateLoop = () => {
  373.             if (!this._isConnected) {
  374.                 console.log("Exiting MainLoop...");
  375.                 return;
  376.             }
  377.             try {
  378.                 if (BaseInstrument.allInstrumentsLoaded && !this.xmlConfigLoading && SimVar.IsReady()) {
  379.                     if (!this._isInitialized)
  380.                         this.Init();
  381.                     this.beforeUpdate();
  382.                     if (this.CanUpdate())
  383.                         this.Update();
  384.                     this.afterUpdate();
  385.                 }
  386.             }
  387.             catch (Error) {
  388.                 console.error(this.instrumentIdentifier + " : " + Error, Error.stack);
  389.             }
  390.             requestAnimationFrame(updateLoop);
  391.         };
  392.         this._isConnected = true;
  393.         console.log("MainLoop created");
  394.         requestAnimationFrame(updateLoop);
  395.     }
  396.     killMainLoop() {
  397.         this._isConnected = false;
  398.     }
  399.     loadXMLConfig() {
  400.         var xmlPath;
  401.         if (this.urlConfig.config) {
  402.             xmlPath = "/Pages/VCockpit/Instruments/Shared/Configs/" + this.urlConfig.config + ".xml";
  403.         }
  404.         else if (this._xmlConfigPath) {
  405.             xmlPath = "/VFS/" + this._xmlConfigPath.replace(/\\/g, "/");
  406.         }
  407.         if (xmlPath) {
  408.             this.xmlConfigLoading = true;
  409.             var xmlRequest = new XMLHttpRequest();
  410.             xmlRequest.onreadystatechange = function (_NavSystem) {
  411.                 if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
  412.                     _NavSystem.onXMLConfigLoaded(this);
  413.                 }
  414.             }.bind(xmlRequest, this);
  415.             xmlRequest.open("GET", xmlPath, true);
  416.             xmlRequest.send();
  417.         }
  418.     }
  419.     onXMLConfigLoaded(_xml) {
  420.         this.xmlConfig = _xml.responseXML;
  421.         if (this.xmlConfig) {
  422.             let instruments = this.xmlConfig.getElementsByTagName("Instrument");
  423.             for (let i = 0; i < instruments.length; i++) {
  424.                 let name = instruments[i].getElementsByTagName("Name")[0].textContent;
  425.                 if (name == this.instrumentIdentifier) {
  426.                     this.instrumentXmlConfig = instruments[i];
  427.                 }
  428.             }
  429.             this.parseXMLConfig();
  430.         }
  431.         else {
  432.             console.error("XML Config file is not well-formatted");
  433.         }
  434.         this.xmlConfigLoading = false;
  435.     }
  436.     loadURLAttributes() {
  437.         var parsedUrl = new URL(this.getAttribute("Url").toLowerCase());
  438.         this.urlConfig.style = parsedUrl.searchParams.get("style");
  439.         this.urlConfig.config = parsedUrl.searchParams.get("config");
  440.         let index = parsedUrl.searchParams.get("index");
  441.         this.urlConfig.index = index == null ? null : parseInt(index);
  442.         this.urlConfig.wasmModule = parsedUrl.searchParams.get("wasm_module");
  443.         this.urlConfig.wasmGauge = parsedUrl.searchParams.get("wasm_gauge");
  444.         this.parseURLAttributes();
  445.     }
  446.     getTimeSinceStart() {
  447.         return Date.now() - this.startTime;
  448.     }
  449.     getAspectRatio() {
  450.         var vpRect = this.getBoundingClientRect();
  451.         if (vpRect) {
  452.             var vpWidth = vpRect.width;
  453.             var vpHeight = vpRect.height;
  454.             var aspectRatio = vpWidth / vpHeight;
  455.             return aspectRatio;
  456.         }
  457.         return 1.0;
  458.     }
  459.     updateHighlight() {
  460.     }
  461.     highlightGetState(_valueMin, _valueMax, _period) {
  462.         let time = new Date().getTime();
  463.         let size = _valueMax - _valueMin;
  464.         let middle = _valueMin + size / 2;
  465.         return middle + (Math.sin((time % _period / _period * Math.PI * 2)) * (size / 2));
  466.     }
  467.     wasTurnedOff() {
  468.         return false;
  469.     }
  470.     initTransponder() {
  471.         let transponderCode = ("0000" + SimVar.GetSimVarValue("TRANSPONDER CODE:1", "number")).slice(-4);
  472.         if (transponderCode) {
  473.             let currentCode = parseInt(transponderCode);
  474.             if (currentCode == 0) {
  475.                 Simplane.setTransponderToRegion();
  476.             }
  477.         }
  478.     }
  479. }
  480. BaseInstrument.allInstrumentsLoaded = false;
  481. class URLConfig {
  482. }
  483. var Quality;
  484. (function (Quality) {
  485.     Quality[Quality["ultra"] = 0] = "ultra";
  486.     Quality[Quality["high"] = 1] = "high";
  487.     Quality[Quality["medium"] = 2] = "medium";
  488.     Quality[Quality["low"] = 3] = "low";
  489.     Quality[Quality["hidden"] = 4] = "hidden";
  490.     Quality[Quality["disabled"] = 5] = "disabled";
  491. })(Quality || (Quality = {}));
  492. var GameState;
  493. (function (GameState) {
  494.     GameState[GameState["mainmenu"] = 0] = "mainmenu";
  495.     GameState[GameState["loading"] = 1] = "loading";
  496.     GameState[GameState["briefing"] = 2] = "briefing";
  497.     GameState[GameState["ingame"] = 3] = "ingame";
  498. })(GameState || (GameState = {}));
  499. class HighlightedElement {
  500. }
  501. customElements.define("base-instrument", BaseInstrument);
  502. checkAutoload();
  503. //# sourceMappingURL=BaseInstrument.js.map
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement