gravity/options.js

91 lines
2.4 KiB
JavaScript

import {
DISPLAY_ACCELERATION_VECTORS,
DISPLAY_VELOCITY_VECTORS,
GRAVITATIONAL_CONSTANT,
MASS_CREATION_RATE,
MERGE_ON_COLLIDE,
MOTION_TIME_SCALE,
PATH_TRACES_DASHED,
PAUSE_DURING_CREATION,
PAUSE_DURING_SELECTION,
EVENT_OPTION_SET,
} from './config.js';
export const optionsLayout = {
pauseDuring: {
creation: ['Pause While Creating', 'boolean', PAUSE_DURING_CREATION],
creation2: ['Pause While Creating', 'boolean', PAUSE_DURING_CREATION],
selection: ['Pause While Selecting', 'boolean', PAUSE_DURING_SELECTION],
},
display: {
velocity: ['Velocity Vectors', 'boolean', DISPLAY_VELOCITY_VECTORS],
acceleration: ['Accel. Vectors', 'boolean', DISPLAY_ACCELERATION_VECTORS],
traces: ['Path Traces', 'boolean', DISPLAY_ACCELERATION_VECTORS],
dashedTraces: ['Dashed Traces', 'boolean', PATH_TRACES_DASHED],
},
collision: {
merge: ['Merge Masses<br>on Collision', 'boolean', MERGE_ON_COLLIDE, {wide: true}],
},
param: {
gravity: ['Gravity', 'number', GRAVITATIONAL_CONSTANT],
timeScale: ['Time Scale', 'number', MOTION_TIME_SCALE],
massCreationRate: ['Mass Creation Rate', 'number', MASS_CREATION_RATE],
}
};
export class Options {
sim = undefined;
values = {};
constructor(sim) {
this.sim = sim;
// Global methods to get/set current option values
this.sim.getOption = (path) => this.getOption(path);
this.sim.setOption = (path, value) => this.setOption(path, value);
this.sim.onOptionSet = (path, cb) => this.onOptionSet(path, cb);
}
getOption(path) {
const val = this.values[path];
return val;
}
setOption(path, value) {
this.values[path] = value;
const e = new CustomEvent(EVENT_OPTION_SET, {detail: {path, value}});
this.sim.div.dispatchEvent(e);
}
// cb: (value) => undefined
onOptionSet(path, cb) {
this.sim.div.addEventListener(EVENT_OPTION_SET, (e) => {
if (path === e.detail.path) {
cb(e.detail.value);
}
});
}
static getSection(layout, sectionName) {
const section = layout[sectionName];
const group = {
type: 'group',
name: sectionName,
title: section._title,
items: [],
};
for (const name in section) {
if (name.startsWith('_')) continue;
const [title, type, defaultValue, opts] = section[name];
group.items.push({
name,
type,
title,
default: defaultValue,
...opts
})
}
return group;
}
}