Skip to content

Commit

Permalink
Merge pull request #69 from thkruz/more-interfaces
Browse files Browse the repository at this point in the history
More interfaces and fix linter errors
  • Loading branch information
ajmas authored Jan 9, 2024
2 parents 3ce3e18 + 065116b commit f420d32
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 46 deletions.
21 changes: 12 additions & 9 deletions src/hud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ function showAttribution (visible: true) {
if (attributionElem) {
if (visible) {
const satelliteStore = viewer.getSatelliteStore();
if (satelliteStore && satelliteStore.getAttribution()) {
const attribution = satelliteStore.getAttribution() || {};
if (satelliteStore?.getAttribution()) {
const attribution = satelliteStore.getAttribution();
const updatedDate = satelliteStore.getUpdatedDate();
attributionElem.innerHTML = `Orbital object data from <a href="${attribution.url}">${attribution.name}</a> <span class="updated">(updated ${updatedDate})</span>`;

if (attribution) {
attributionElem.innerHTML = `Orbital object data from <a href="${attribution.url}">${attribution.name}</a> <span class="updated">(updated ${updatedDate})</span>`;
attributionElem.classList.remove('hidden');
}
}
attributionElem.classList.remove('hidden');
} else {
attributionElem.classList.add('hidden');
}
Expand Down Expand Up @@ -274,12 +277,12 @@ function getSupportedEvents () {
}

function initMenus () {
const elements = document.querySelectorAll('.menu-item');
for (let i = 0; i < elements.length; i++) {
const element = elements[i] as HTMLElement;
const elements = Array.from(document.querySelectorAll('.menu-item'));

for (const element of elements) {
element.addEventListener('click', () => {
const action = element.dataset.action;
if (action && action.startsWith('open:')) {
const action = (element as HTMLElement).dataset.action;
if (action?.startsWith('open:')) {
const parts = action.split(':');
windowManager.openWindow(parts[1]);
}
Expand Down
11 changes: 8 additions & 3 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
const defaultLogLevel = 'debug';
const logLevels = ['error', 'warn', 'info', 'debug'];

let allOutputs: Record<string, any> = {};
let allOutputs = {
error: (..._args: any) => {},
warn: (..._args: any) => {},
info: (..._args: any) => {},
debug: (..._args: any) => {}
};
let globalLogger = new Proxy({
logLevel: defaultLogLevel,
enabledOutputs: {} as Record<string, any>,
Expand Down Expand Up @@ -41,8 +46,8 @@ function init () {

const enabledOutputs = scope.enabledOutputs;

for (let i = 0; i < logLevels.length; i++) {
enabledOutputs[logLevels[i]] = true;
for (const logLevel of logLevels) {
enabledOutputs[logLevel] = true;
}

allOutputs = {
Expand Down
7 changes: 5 additions & 2 deletions src/viewer/Earth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ShaderMaterial, UniformsUtils, Texture } from 'three';
import { Color, TextureLoader, MeshPhongMaterial, SphereGeometry, Mesh, Group, BackSide, AdditiveBlending } from '../utils/three';
import SceneComponent from './interfaces/SceneComponent';
import SatelliteOrbitScene from './SatelliteOrbitScene';
import { ViewerContext } from './interfaces/ViewerContext';

class Earth implements SceneComponent {
baseUrl = '';
Expand Down Expand Up @@ -91,7 +92,7 @@ class Earth implements SceneComponent {
group.add(mesh);
}

async init (scene: SatelliteOrbitScene, context: Record<string, any>) {
async init (scene: SatelliteOrbitScene, context: ViewerContext) {
if (context.config) {
this.baseUrl = context.config.baseUrl;
}
Expand Down Expand Up @@ -122,7 +123,9 @@ class Earth implements SceneComponent {
this.group.add(this.sphere);

if (this.addClouds) {
this.initClouds(scene, this.group);
this.initClouds(scene, this.group).catch(error => {
console.error('Error loading clouds', error);
});
}

if (this.addAtmosphere) {
Expand Down
5 changes: 3 additions & 2 deletions src/viewer/Orbits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SatelliteOrbitScene from './SatelliteOrbitScene';
import logger from '../utils/logger';
import SatelliteGroups from './SatelliteGroups';
import SelectableSatellite from './interfaces/SelectableSatellite';
import { ViewerContext } from './interfaces/ViewerContext';

class Orbits implements SceneComponent, SelectableSatellite {
config: Record<string, any> = {};
Expand Down Expand Up @@ -221,12 +222,12 @@ class Orbits implements SceneComponent, SelectableSatellite {
// calculate tracks
if (this.satelliteGroup) {
const satellites = this.satelliteGroup.sats;
const satelliteIds = satellites.map((entry: Record<string, any>) => entry.satId as number);
const satelliteIds = satellites.map((entry) => entry.satId as number);
this.calculateOrbits(satelliteIds);
}
}

init (scene: SatelliteOrbitScene, context: Record<string, any>) {
init (scene: SatelliteOrbitScene, context: ViewerContext) {
this.config = context.config;
this.scene = scene;
this.orbitWorker = new OrbitCalculationWorker();
Expand Down
30 changes: 15 additions & 15 deletions src/viewer/SatelliteGroups.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import SatGroup from './SatelliteGroup';
import logger from '../utils/logger';
import SatelliteStore from './SatelliteStore';
import type SatelliteGroup from './SatelliteGroup';

class SatelliteGroups {
groups: Record<string, SatGroup> = {};
selectedGroup?: SatGroup;
sats: any[] = [];
satelliteStore: SatelliteStore;

constructor (satelliteGroups: Record<string, any>[], satelliteStore: SatelliteStore) {
constructor (satelliteGroups: SatelliteGroup[], satelliteStore: SatelliteStore) {
if (!satelliteStore) {
throw new Error('satelliteStore is required');
}
Expand All @@ -26,13 +27,12 @@ class SatelliteGroups {
this.selectedGroup = group;
if (!group) {
this.clearSelect();
return;
}
}

forEach (callback: (satId: number) => void) {
for (let i = 0; i < this.sats.length; i++) {
callback(this.sats[i].satId);
for (const sat of this.sats) {
callback(sat.satId);
}
}

Expand All @@ -54,21 +54,21 @@ class SatelliteGroups {

reloadGroups () {
const keys = Object.keys(this.groups);
for (let i = 0; i < keys.length; i++) {
this.groups[keys[i]].reload();
for (const key of keys) {
this.groups[key].reload();
}
}

resetConfig (satelliteGroups: Record<string, any>[]) {
resetConfig (satelliteGroups: SatelliteGroup[]) {
const groupConfigs = satelliteGroups;
for (let i = 0; i < groupConfigs.length; i++) {
logger.debug(`registering satellite group ${groupConfigs[i].name} (id: ${groupConfigs[i].id})`);
this.groups[groupConfigs[i].id.toLowerCase()] = new SatGroup(
groupConfigs[i].id.toLowerCase(),
groupConfigs[i].name,
groupConfigs[i].groupType,
groupConfigs[i].data,
this.satelliteStore as SatelliteStore
for (const groupConfig of groupConfigs) {
logger.debug(`registering satellite group ${groupConfig.name} (id: ${groupConfig.id})`);
this.groups[groupConfig.id.toLowerCase()] = new SatGroup(
groupConfig.id.toLowerCase(),
groupConfig.name,
groupConfig.groupType,
groupConfig.data,
this.satelliteStore
);
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/viewer/SatelliteStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class SatelliteStore {
tleUrl = `${config.baseUrl}/data/attributed-TLE.json`;
eventManager: EventManager;
satData: SatelliteObject[] = [];
attribution?: Record<string, any>;
attribution?: {
name: string;
url: string;
};
updateDate?: Date;
satelliteVelocities: Float32Array = new Float32Array();
satellitePositions: Float32Array = new Float32Array();
Expand Down Expand Up @@ -65,7 +68,10 @@ class SatelliteStore {
}
}

getAttribution (): Record<string, any> | undefined {
getAttribution (): {
name: string;
url: string;
} | undefined {
return this.attribution;
}

Expand Down
6 changes: 4 additions & 2 deletions src/viewer/Satellites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import DefaultColorScheme from './color-schemes/DefaultColorScheme';
import SelectableSatellite from './interfaces/SelectableSatellite';
import ShaderStore from './ShaderStore';
import GroupColorScheme from './color-schemes/GroupColorScheme';
import { SatelliteObject } from './interfaces/SatelliteObject';
import { ViewerContext } from './interfaces/ViewerContext';

class Satellites implements SceneComponent, SelectableSatellite {
baseUrl = '';
Expand Down Expand Up @@ -94,7 +96,7 @@ class Satellites implements SceneComponent, SelectableSatellite {
/**
* update point colours
*/
private updateSatellitesMaterial (satCount: number, satellites: Record<string, any>[]) {
private updateSatellitesMaterial (satCount: number, satellites: SatelliteObject[]) {
if (this.geometry?.attributes.color && this.currentColorScheme && this.satelliteStore) {
// Adjust if the satellite count adjusts
if (this.satelliteColors.length === 0 || (satCount * 4 !== this.satelliteColors.length)) {
Expand Down Expand Up @@ -332,7 +334,7 @@ class Satellites implements SceneComponent, SelectableSatellite {
}));
}

async init (scene: SatelliteOrbitScene, context: Record<string, any>) {
async init (scene: SatelliteOrbitScene, context: ViewerContext) {
this.satelliteStore = context.satelliteStore;
this.shaderStore = context.shaderStore;
this.scene = scene;
Expand Down
3 changes: 2 additions & 1 deletion src/viewer/Universe.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ViewerContext } from './interfaces/ViewerContext';
import { TextureLoader } from '../utils/three';
import SceneComponent from './interfaces/SceneComponent';
import SatelliteOrbitScene from './SatelliteOrbitScene';

class Universe implements SceneComponent {
init (scene: SatelliteOrbitScene, context: Record<string, any>) {
init (scene: SatelliteOrbitScene, context: ViewerContext) {
const baseUrl = context.config.baseUrl;
const texture = new TextureLoader().load(`${baseUrl}textures/example_render.jpg`);

Expand Down
7 changes: 4 additions & 3 deletions src/viewer/color-schemes/ColorScheme.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import SatelliteGroup from '../SatelliteGroup';
import { SatelliteObject } from '../interfaces/SatelliteObject';

class ColorScheme {
name: string;
colorizer: (satellite: Record<string, any>, group?: SatelliteGroup) => { color: number[], pickable: boolean };
colorizer: (satellite: SatelliteObject, group?: SatelliteGroup) => { color: number[], pickable: boolean };

constructor (name: string, colorizer: (satellite: Record<string, any>) => { color: number[], pickable: boolean }) {
constructor (name: string, colorizer: (satellite: SatelliteObject) => { color: number[], pickable: boolean }) {
this.name = name;
this.colorizer = colorizer;
}

getSatelliteColor (satellite: Record<string, any>, group?: SatelliteGroup): { color: number[], pickable: boolean } {
getSatelliteColor (satellite: SatelliteObject, group?: SatelliteGroup): { color: number[], pickable: boolean } {
return this.colorizer(satellite, group);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/viewer/color-schemes/DefaultColorScheme.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { SatelliteObject } from '../interfaces/SatelliteObject';
import ColorScheme from './ColorScheme';

class DefaultColorScheme extends ColorScheme {
constructor () {
super ('Default color scheme', (satellite: Record<string, any>) => {
super ('Default color scheme', (satellite: SatelliteObject) => {
let color = [1.0, 1.0, 0.0, 1.0];
let pickable = false;

Expand Down
5 changes: 3 additions & 2 deletions src/viewer/color-schemes/GroupColorScheme.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import SatelliteGroup from '@satellite-viewer/SatelliteGroup';
import ColorScheme from './ColorScheme';
import { SatelliteObject } from '../interfaces/SatelliteObject';

class GroupColorScheme extends ColorScheme {
constructor () {
super ('Group color scheme', (satellite: Record<string, any>, group?: SatelliteGroup) => {
super ('Group color scheme', (satellite: SatelliteObject, group?: SatelliteGroup) => {
if (satellite) {
if (group && group.hasSat(satellite.id)) {
if (group?.hasSat(satellite.id)) {
return {
color: [1.0, 0.2, 0.0, 1.0],
pickable: true
Expand Down
13 changes: 10 additions & 3 deletions src/viewer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import SatelliteGroup from './SatelliteGroup';
import ShaderStore from './ShaderStore';
import logger from '@/utils/logger';
import { ArrowHelper, Raycaster, Vector2, Vector3 } from 'three';
import { SatelliteObject } from './interfaces/SatelliteObject';
import { ViewerContext } from './interfaces/ViewerContext';

class Viewer {
config: Record<string, any> = {
Expand All @@ -28,7 +30,12 @@ class Viewer {
camera?: PerspectiveCamera;
controls?: OrbitControls;
renderer?: WebGLRenderer;
context: Record<string, any> = {};
context: ViewerContext = {
satelliteGroups: null as unknown as SatelliteGroups,
config: null as unknown as Record<string, any>,
satelliteStore: null as unknown as SatelliteStore,
shaderStore: null as unknown as ShaderStore
};
satelliteGroups?: SatelliteGroups;
satelliteStore?: SatelliteStore;
shaderStore?: ShaderStore;
Expand Down Expand Up @@ -82,7 +89,7 @@ class Viewer {
}
}

private onSatDataLoaded (satData: Record<string, any>) {
private onSatDataLoaded (satData: SatelliteObject[]) {
this.eventManager.fireEvent('satdataloaded', satData);
this.ready = true;
}
Expand Down Expand Up @@ -404,7 +411,7 @@ class Viewer {
this.satellites?.setSatelliteGroup(satelliteGroup);
}

getSelectedSatellite (): Record<string, any> | undefined {
getSelectedSatellite (): SatelliteObject | undefined {
if (this.satelliteStore) {
return this.satelliteStore.getSatellite(this.selectedSatelliteIdx);
}
Expand Down
3 changes: 2 additions & 1 deletion src/viewer/interfaces/SceneComponent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ViewerContext } from './ViewerContext';
import SatelliteOrbitScene from '../SatelliteOrbitScene';

interface SceneComponent {
init (scene: SatelliteOrbitScene, context: Record<string, any>): void | Promise<void>;
init (scene: SatelliteOrbitScene, context: ViewerContext): void | Promise<void>;
update (scene?: SatelliteOrbitScene): void;
}

Expand Down
11 changes: 11 additions & 0 deletions src/viewer/interfaces/ViewerContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type SatelliteGroups from '../SatelliteGroups';
import type SatelliteStore from '../SatelliteStore';
import type ShaderStore from '../ShaderStore';


export interface ViewerContext {
satelliteGroups: SatelliteGroups;
config: Record<string, any>;
satelliteStore: SatelliteStore;
shaderStore: ShaderStore;
}

0 comments on commit f420d32

Please sign in to comment.