Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Early implementation of serializable_models and user settings. #31

Closed
wants to merge 10 commits into from
6 changes: 6 additions & 0 deletions public/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,9 @@ input {
#toolbar button {
margin-right: 5px;
}

fieldset input {
width: auto;
height: auto;
float: left;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the float if it's in a group, which is display:flex? I feel like it might work without it

}
21 changes: 21 additions & 0 deletions public/views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,26 @@ html
div.input
button#delete(disabled) Delete this guide

.row
.column
h1 Settings
hr
h3 Theme
fieldset(id="theme")

h3 Keybindings
fieldset#editor.column
.group
input(type="radio", id="normal", name="editor", value="normal")
label(for="normal") Normal
.group
input(type="radio", id="emacs", name="editor", value="emacs")
label(for="emacs") Emacs
.group
input(type="radio", id="vim", name="editor", value="vim")
label(for="vim") Vim

button#saveSettings Save Settings

script#code(type='x-code')!= source
script(src='js/client.js')
36 changes: 18 additions & 18 deletions src/frontend/costFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { List } from 'immutable';
// tslint:disable-next-line:import-name
import Bezier = require('bezier-js');

import { commit, onChange, setState, state } from './state';
import { commit, onChange, state } from './state';
import { addModel } from './model';
import { renderer } from './renderer';

const costControls = <HTMLDivElement>document.getElementById('cost');
const distanceMultiplierControls = range(3)
.map((i) => <HTMLInputElement>document.getElementById(`distance${i+1}`));
.map((i) => <HTMLInputElement>document.getElementById(`distance${i + 1}`));
const alignmentOffsetControl = <HTMLInputElement>document.getElementById('alignmentOffset');
const alignmentMultiplierControl = <HTMLInputElement>document.getElementById('alignmentMultiplier');
const deleteBtn = <HTMLButtonElement>document.getElementById('delete');
Expand Down Expand Up @@ -45,22 +45,22 @@ const initialCostFnParams = [
]

export const addCostFn = () => {
let { costFnParams } = state;
let { costFnParams } = state.asBakedType();
if (!costFnParams) {
costFnParams = List(initialCostFnParams);
}
const costFn = calder.CostFunction.guidingVectors(costFnParams.toJS(), );

setState({ costFnParams, costFn });
state.setState({ costFnParams, costFn });
};

export const addCostFunctionViz = () => {
const { costFn, costFnParams, selectedCurve } = state;
const { costFn, costFnParams, selectedCurve } = state.asBakedType();
if (!costFn || !costFnParams) {
return;
}

const vectorField = costFn.generateVectorField(3, 0.5, {x: 0, y: 2, z: 0});
const vectorField = costFn.generateVectorField(3, 0.5, { x: 0, y: 2, z: 0 });
const guidingCurves = List(costFn.generateGuidingCurve().map((path: [number, number, number][], index: number) => {
return {
path,
Expand All @@ -69,11 +69,11 @@ export const addCostFunctionViz = () => {
};
}));

setState({ vectorField, guidingCurves });
state.setState({ vectorField, guidingCurves });
};

export const addNewCurve = (polyline: {x: number; y: number}[]) => {
const [ bezier ] = fitCurve(polyline.map((point) => [point.x, point.y]), 100, undefined, true);
export const addNewCurve = (polyline: { x: number; y: number }[]) => {
const [bezier] = fitCurve(polyline.map((point) => [point.x, point.y]), 100, undefined, true);
const depth = vec3.distance(renderer.camera.position, renderer.camera.target);

const transform = mat4.invert(mat4.create(), renderer.camera.getTransform());
Expand All @@ -97,20 +97,20 @@ export const addNewCurve = (polyline: {x: number; y: number}[]) => {
const lastCurve = state.costFnParams && state.costFnParams[state.costFnParams.size - 1];

const curve = {
bezier: new Bezier(bezier3D.map(([x, y, z]) => { return {x, y, z}; })),
bezier: new Bezier(bezier3D.map(([x, y, z]) => { return { x, y, z }; })),
distanceMultiplier: lastCurve ? [...lastCurve.distanceMultiplier] : [...scale],
alignmentMultiplier: lastCurve ? lastCurve.alignmentMultiplier : 500,
alignmentOffset: lastCurve ? lastCurve.alignmentOffset : 0.7
};

let { costFnParams = List([]) } = state;
let { costFnParams = List([]) } = state.asBakedType();
costFnParams = costFnParams.push(curve);

setState({ costFnParams });
state.setState({ costFnParams });
}

onChange('selectedCurve', () => {
const { guidingCurves, costFnParams, selectedCurve } = state;
const { guidingCurves, costFnParams, selectedCurve } = state.asBakedType();
if (!costFnParams) {
return;
}
Expand All @@ -120,7 +120,7 @@ onChange('selectedCurve', () => {
curve.selected = index === selectedCurve;
});

setState({ guidingCurves });
state.setState({ guidingCurves });
}

if (selectedCurve !== null && selectedCurve !== undefined) {
Expand All @@ -144,7 +144,7 @@ onChange('selectedCurve', () => {
});

const updateCostFnParams = throttle(() => {
let { costFnParams, selectedCurve } = state;
let { costFnParams, selectedCurve } = state.asBakedType();
if (!costFnParams || selectedCurve === undefined || selectedCurve === null) {
return;
}
Expand All @@ -166,14 +166,14 @@ const updateCostFnParams = throttle(() => {

costFnParams = costFnParams.set(selectedCurve, updatedParams);

setState({ costFnParams });
state.setState({ costFnParams });
addCostFn();
addModel();
commit();
}, 100, { trailing: true });

const deleteCurve = () => {
let { guidingCurves, costFnParams, selectedCurve } = state;
let { guidingCurves, costFnParams, selectedCurve } = state.asBakedType();
if (!guidingCurves || !costFnParams || selectedCurve === undefined || selectedCurve === null) {
return;
}
Expand All @@ -186,7 +186,7 @@ const deleteCurve = () => {
guidingCurves = guidingCurves.delete(selectedCurve);
costFnParams = costFnParams.delete(selectedCurve);

setState({ guidingCurves, costFnParams, selectedCurve: null });
state.setState({ guidingCurves, costFnParams, selectedCurve: null });
addCostFn();
addCostFunctionViz();
addModel();
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'brace/ext/language_tools';
import { Completion } from './Completion';
import { onChange, state } from './state';

const codeElement = <HTMLScriptElement> document.getElementById('code');
const codeElement = <HTMLScriptElement>document.getElementById('code');
export const defaultSource = codeElement.innerText;

export const editor = ace.edit('source');
Expand All @@ -18,7 +18,7 @@ editor.setOptions({

onChange('source', () => {
if (editor.getSession().getValue() === state.source) {
return;
return;
}
editor.getSession().setValue(state.source || '');
});
6 changes: 3 additions & 3 deletions src/frontend/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as calder from 'calder-gl';
import { transform } from '@babel/standalone';

import { setState, state } from './state';
import { state } from './state';
import { editor } from './editor';
import { renderer } from './renderer';

Expand All @@ -22,10 +22,10 @@ export const addGenerator = () => {
const generator = calder.Armature.generator();
setup(generator);

setState({ source, generator });
state.setState({ source, generator });
} catch (e) {
console.log(e);

setState({ source, generator: undefined });
state.setState({ source, generator: undefined });
}
};
28 changes: 14 additions & 14 deletions src/frontend/interactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Camera, vec3ToVector } from 'calder-gl';
import { renderer } from './renderer';
import { commit, setState, state } from './state'
import { commit, state } from './state'
import { addCostFn, addCostFunctionViz, addNewCurve } from './costFn';
import { addModel } from './model';
import { mat4, quat, vec3, vec4 } from 'gl-matrix';
Expand Down Expand Up @@ -41,7 +41,7 @@ function handleMouseDown(event: MouseEvent) {

controlState.dragged = false;

const button = <MouseButton> event.button;
const button = <MouseButton>event.button;

if (button === MouseButton.LEFT) {
if (event.shiftKey) {
Expand All @@ -53,7 +53,7 @@ function handleMouseDown(event: MouseEvent) {
const x = event.clientX - bounds.left;
const y = event.clientY - bounds.top;

setState({ pencilLine: [{x, y}] });
state.setState({ pencilLine: [{ x, y }] });
} else {
controlState.selectedHandle = null;
let closestDistance = Infinity;
Expand Down Expand Up @@ -100,7 +100,7 @@ function handleMouseUp(event: MouseEvent) {
y: event.clientY - boundingRect.top
});

setState({ selectedCurve: selectedIndex < guidingCurves.size ? selectedIndex : null });
state.setState({ selectedCurve: selectedIndex < guidingCurves.size ? selectedIndex : null });
commit();
}

Expand All @@ -116,12 +116,12 @@ function handleMouseUp(event: MouseEvent) {
addCostFn();
addCostFunctionViz();
addModel();
setState({ pencilLine: undefined });
state.setState({ pencilLine: undefined });

const { guidingCurves } = state;
if (guidingCurves) {
const selectedCurve = guidingCurves.size - 1;
setState({ selectedCurve, guidingCurves });
state.setState({ selectedCurve, guidingCurves });
commit();
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ function handleMouseMove(event: MouseEvent) {
const y = event.clientY - bounds.top;

pencilLine.push({ x, y });
setState({ pencilLine });
state.setState({ pencilLine });
}

} else if (controlState.mode === ControlMode.DRAG_CURVE) {
Expand All @@ -165,7 +165,7 @@ function handleMouseMove(event: MouseEvent) {
// Bring the transform into world coordinates by applying the inverse camera transform
const inverseTransform = mat4.invert(tmpMat4, renderer.camera.getTransform());
if (inverseTransform && state.guidingCurves && state.costFnParams &&
state.selectedCurve !== undefined && state.selectedCurve !== null && controlState.selectedHandle !== null) {
state.selectedCurve !== undefined && state.selectedCurve !== null && controlState.selectedHandle !== null) {
const points = state.costFnParams.get(state.selectedCurve).bezier.points;

// We need to scale the direction vector so that when the control point moves, it moves the right
Expand Down Expand Up @@ -213,7 +213,7 @@ function handleMouseMove(event: MouseEvent) {
};
const guidingCurves = state.guidingCurves.set(state.selectedCurve, newGuidingCurve);

setState({ costFnParams, guidingCurves });
state.setState({ costFnParams, guidingCurves });
}

} else if (controlState.mode === ControlMode.CAMERA_MOVE) {
Expand Down Expand Up @@ -259,11 +259,11 @@ function handleMouseMove(event: MouseEvent) {
Camera.up,
direction);
vec3.normalize(axis, axis);
renderer.camera.rotateAboutTarget(quat.setAxisAngle(
tmpQuat,
axis,
event.movementY / 100
));
renderer.camera.rotateAboutTarget(quat.setAxisAngle(
tmpQuat,
axis,
event.movementY / 100
));
}
}
}
Expand Down
Loading