ed-safari/src/renderer.js

136 lines
4.3 KiB
JavaScript
Raw Normal View History

import 'bootstrap/dist/css/bootstrap.css';
import './assets/index.css';
import './icons/flaticon.css';
import './assets/ldom.min';
const { app, ipcRenderer } = require('electron');
import { Safari } from './models/Safari';
import { Settings } from './models/Settings';
import { UI } from './models/UI';
import { Body } from './models/Body';
import { EDSM } from './models/EDSM';
import { blend } from './assets/blend';
2023-05-08 12:25:13 -07:00
2023-05-08 17:42:09 -07:00
// Grab app.isPackaged from main process
let isPackaged = false;
2023-05-08 17:42:09 -07:00
window.process.argv.forEach((item) => {
if (item.includes('EDS-ENV')) {
isPackaged = (item.split('=').pop() === 'true');
2023-05-08 17:42:09 -07:00
}
})
/* ------------------------------------------------------------------------------- app setup ---- */
2023-05-08 12:25:13 -07:00
const safari = Safari.start(isPackaged);
const settings = Settings.get(isPackaged);
const journal = safari.journal;
const edsm = EDSM.connect();
2023-05-08 13:10:38 -07:00
if (!journal) {
// TODO handle error
}
2023-05-08 12:25:13 -07:00
safari.watchJournalDir();
journal.watch();
2023-05-13 12:50:39 -07:00
/* -------------------------------------------------------------------- close window handler ---- */
$('#closeBtn').on('click', () => {
ipcRenderer.send('CLOSE_WINDOW');
2023-05-13 17:32:43 -07:00
});
2023-05-13 12:50:39 -07:00
2023-05-13 13:11:57 -07:00
/* ----------------------------------------------------------------- settings button handler ---- */
$('#settingsBtn').on('click', () => {
ipcRenderer.send('LOAD_SETTINGS');
2023-05-13 17:32:43 -07:00
});
2023-05-13 13:11:57 -07:00
2023-05-11 21:02:23 -07:00
/* ------------------------------------------------------------------------- build body list ---- */
2023-05-11 21:02:23 -07:00
journal.once('BUILD_BODY_LIST', () => {
if (journal.location?.bodies?.length > 0) {
journal.location.sortBodies();
journal.location.bodies.forEach((body) => {
const row = UI.createBodyRow(body);
$('#scans').appendChild(row);
2023-05-13 17:32:43 -07:00
});
}
2023-05-13 17:32:43 -07:00
});
2023-05-09 13:40:32 -07:00
2023-05-11 21:02:23 -07:00
/* ----------------------------------------------------------------- started hyperspace jump ---- */
journal.on('ENTERING_WITCH_SPACE', () => UI.enterWitchSpace());
2023-05-11 21:02:23 -07:00
2023-05-09 13:40:32 -07:00
/* ---------------------------------------------------------------------- entered new system ---- */
journal.on('ENTERED_NEW_SYSTEM', () => {
UI.setCurrentSystem(journal.location);
2023-05-11 21:02:23 -07:00
$('#navRoute').children().filter(`#${CSS.escape(journal.location.SystemAddress)}`).remove();
2023-05-09 13:40:32 -07:00
2023-05-11 21:02:23 -07:00
// verify that the internal navRoute matches the UI navRoute, and rebuild it if not
if ($('#navRoute').children().length !== journal.navRoute.length) {
journal.emit('SET_NAV_ROUTE');
2023-05-11 21:02:23 -07:00
}
2023-05-13 17:32:43 -07:00
});
/* ---------------------------------------------------------------------- body scan detected ---- */
journal.on('BODY_SCANNED', (body, DSS) => {
journal.location.sortBodies();
// If this is a DSS scan, it's very likely that the body already exists in our list so we just
// need to remove the highlighting if applicable
if (DSS) {
const bodyRow = $(`#${body.BodyID}`);
if (bodyRow.length > 0) { // check just in case body was missed in earlier scans
bodyRow.removeClass('highlighted uncharted').addClass('charted');
} else {
const row = UI.createBodyRow(body);
$('#scans').appendChild(row);
}
} else { // else it's an FSS/auto scan and won't be in the list yet
const row = UI.createBodyRow(body);
$('#scans').appendChild(row);
}
2023-05-13 17:32:43 -07:00
});
2023-05-11 01:54:03 -07:00
/* --------------------------------------------------------------------------- nav route set ---- */
journal.on('SET_NAV_ROUTE', () => {
// clear previous nav route, if any
$('#navRoute').children().remove();
2023-05-11 02:45:02 -07:00
2023-05-11 21:02:23 -07:00
if (journal.navRoute.length > 0) {
2023-05-11 02:45:02 -07:00
journal.navRoute.forEach((system) => {
// duplicate check
// CSS.escape is needed since CSS technically doesn't allow numeric IDs
const systemRow = $(`#${CSS.escape(system.SystemAddress)}`);
2023-05-11 02:45:02 -07:00
if (systemRow.length === 0) {
const row = UI.createSystemRow(system);
$('#navRoute').appendChild(row);
2023-05-11 02:45:02 -07:00
}
2023-05-13 17:32:43 -07:00
});
2023-05-11 02:45:02 -07:00
}
2023-05-13 17:32:43 -07:00
});
2023-05-12 03:00:15 -07:00
/* ------------------------------------------------------------------------ system value set ---- */
edsm.on('SYSTEM_APPRAISED', (system) => {
const systemRow = $(`#${CSS.escape(system.SystemAddress)}`);
2023-05-12 03:00:15 -07:00
if (systemRow.length > 0) {
UI.setValue(systemRow, system.estimatedValueMapped);
2023-05-12 03:00:15 -07:00
}
});
// const color = blend([245, 168, 4], [0, 0, 0], [0.4, 0.6]);
// const rgb = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
// $('body').css('--secondary-dark', rgb);