Skip to content

Commit

Permalink
Add functional patch saving plus validation
Browse files Browse the repository at this point in the history
  • Loading branch information
oamaok committed Oct 13, 2021
1 parent a7b87dc commit e34b807
Show file tree
Hide file tree
Showing 21 changed files with 266 additions and 138 deletions.
6 changes: 6 additions & 0 deletions client/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Patch } from '../../common/types'

type Method = 'GET' | 'POST'
type Options = {
body?: any
Expand Down Expand Up @@ -29,3 +31,7 @@ const post = (endpoint: string, options?: Options) => {
export const getIdentity = () => {
return get('/api/identity')
}

export const saveNewPatch = (patch: Patch) => {
return post('/api/patch', { body: patch })
}
6 changes: 3 additions & 3 deletions client/src/components/ActiveCable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const ActiveCable = () => {

const candidateSocket = getCableConnectionCandidate()

const a = getSocketPosition(state.activeCable.from.socket)
const a = getSocketPosition(state.activeCable.from)
const b = candidateSocket ? getSocketPosition(candidateSocket) : state.cursor

const from = state.activeCable.from.socket.type === 'output' ? a : b
const to = state.activeCable.from.socket.type === 'output' ? b : a
const from = state.activeCable.from.type === 'output' ? a : b
const to = state.activeCable.from.type === 'output' ? b : a

return <CablePath from={from} to={to} />
}
Expand Down
14 changes: 14 additions & 0 deletions client/src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { h } from 'kaiku'

const App = () => {
return (
<div className="">
<div className="header">
<h2>modulate</h2>
<div className="current-patch">
<input type="text" />
</div>
</div>
</div>
)
}
2 changes: 1 addition & 1 deletion client/src/components/Cable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { h, useEffect } from 'kaiku'
import { getRegisteredSocket, getSockets } from '../sockets'
import { getSocketPosition } from '../state'
import { ConnectedSocket } from '../types'
import { ConnectedSocket } from '../../../common/types'
import CablePath from './CablePath'

type Props = {
Expand Down
8 changes: 4 additions & 4 deletions client/src/components/Cables.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { h } from 'kaiku'
import state from '../state'
import { viewport, patch } from '../state'

import ActiveCable from './ActiveCable'
import Cable from './Cable'

const Cables = () => (
<div className="cables">
<svg viewBox={`0 0 ${state.viewport.width} ${state.viewport.height}`}>
{state.cables.map(({ id, from, to }) => (
<Cable key={id} from={from.socket} to={to.socket} />
<svg viewBox={`0 0 ${viewport.width} ${viewport.height}`}>
{patch.cables.map(({ id, from, to }) => (
<Cable key={id} from={from} to={to} />
))}
<ActiveCable />
</svg>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Knob.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h, useState, useEffect } from 'kaiku'
import state, { getKnobValue, setKnobValue } from '../state'
import { Id, Vec2 } from '../types'
import { Id, Vec2 } from '../../../common/types'

type Props = {
moduleId: Id
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Module.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { h, useState, useEffect, FC } from 'kaiku'
import state, { getModulePosition } from '../state'
import { Id, Vec2 } from '../types'
import { Id, Vec2 } from '../../../common/types'

type Props = {
id: Id
Expand Down
7 changes: 3 additions & 4 deletions client/src/components/Modules.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { h, Fragment } from 'kaiku'
import state from '../state'
import { Id } from '../types'
import { patch } from '../state'
import { moduleMap } from '../moduleMap'

const Modules = () => {
return (
<>
{Object.keys(state.modules).map((id: string) => {
{Object.keys(patch.modules).map((id: string) => {
const Component: any =
moduleMap[state.modules[id as Id].name as keyof typeof moduleMap]
moduleMap[patch.modules[id].name as keyof typeof moduleMap]
return <Component id={id} />
})}
</>
Expand Down
5 changes: 3 additions & 2 deletions client/src/components/Socket.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { h, Fragment, useEffect, useRef } from 'kaiku'
import { getModulePosition, plugActiveCable, setSocketPosition } from '../state'
import { plugActiveCable, setSocketPosition } from '../state'
import { RegisteredSocket, registerSocket, unregisterSocket } from '../sockets'
import { Vec2 } from '../types'
import { Vec2 } from '../../../common/types'

type Props = RegisteredSocket

Expand Down Expand Up @@ -43,6 +43,7 @@ const Socket = ({ moduleId, type, name, node }: Props) => {
if (ref.current) {
const offset = getSocketOffset(ref.current)
const { width, height } = ref.current.getBoundingClientRect()

setSocketPosition(moduleId, name, {
x: offset.x + width / 2,
y: offset.y + height / 2,
Expand Down
9 changes: 2 additions & 7 deletions client/src/generated/worklets.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
// THIS IS A GENERATED FILE, DO NOT EDIT MANUALLY
Expand All @@ -8,13 +9,7 @@ import Clock from '../../worklets/Clock'
import Gain from '../../worklets/Gain'
import ModulationHelper from '../../worklets/ModulationHelper'
import Sequencer from '../../worklets/Sequencer'
export const workletNames = [
'ADSR',
'Clock',
'Gain',
'ModulationHelper',
'Sequencer',
] as const
export const workletNames = ["ADSR","Clock","Gain","ModulationHelper","Sequencer"] as const
export type Worklets = {
ADSR: typeof ADSR
Clock: typeof Clock
Expand Down
27 changes: 9 additions & 18 deletions client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { initializeAudio } from './audio'
import Cables from './components/Cables'
import Modules from './components/Modules'
import ModuleSelector from './components/ModuleSelector'
import state from './state'
import state, { patch } from './state'
import * as api from './api'

const initialize = async () => {
Expand All @@ -14,14 +14,13 @@ const initialize = async () => {
if (rawSaveState) {
const { currentId, modules, knobs, sockets, cables } =
JSON.parse(rawSaveState)
state.knobs = knobs
state.currentId = currentId
state.modules = modules
state.sockets = sockets
patch.knobs = knobs
patch.currentId = currentId
patch.modules = modules

state.initialized = true
await new Promise((resolve) => requestAnimationFrame(resolve))
state.cables = cables
patch.cables = cables
} else {
state.initialized = true
}
Expand All @@ -36,18 +35,7 @@ const App = () => {
useEffect(() => {
if (!state.initialized) return

const { currentId, modules, knobs, sockets, cables } = state

localStorage.setItem(
'savestate',
JSON.stringify({
currentId,
modules,
knobs,
sockets,
cables,
})
)
localStorage.setItem('savestate', JSON.stringify(patch))
})

if (!state.initialized) {
Expand All @@ -63,6 +51,9 @@ const App = () => {
<ModuleSelector />
<Modules />
<Cables />
<div className="patch-controls">
<button onClick={() => api.saveNewPatch(patch)}>Save</button>
</div>
</>
)
}
Expand Down
Loading

0 comments on commit e34b807

Please sign in to comment.