Skip to content

Commit 89c24bb

Browse files
committed
refactor: split node i/o into separate components
1 parent 62628e9 commit 89c24bb

File tree

4 files changed

+238
-125
lines changed

4 files changed

+238
-125
lines changed

src/svelte/editor/NodeInputs.svelte

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!--
2+
3+
Copyright (C) 2024 Daniel Fernandes
4+
5+
This file is part of MIDI-FX.
6+
7+
MIDI-FX is free software: you can redistribute it and/or modify it
8+
under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
MIDI-FX is distributed in the hope that it will be useful, but
13+
WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with MIDI-FX. If not, see <https://www.gnu.org/licenses/>.
19+
20+
-->
21+
<script lang="ts">
22+
import Port, { PortType } from "./Port.svelte";
23+
24+
export let id: string;
25+
export let inputs: string[] = [];
26+
export let handles = {};
27+
</script>
28+
29+
<div class="node-inputs">
30+
{#each inputs as name}
31+
<Port type={PortType.Input} {name} {id} bind:handle={handles[name]}/>
32+
{/each}
33+
</div>
34+
35+
<style>
36+
.node-inputs {
37+
width: 24px;
38+
display: flex;
39+
flex-direction: column;
40+
align-items: center;
41+
justify-content: space-evenly;
42+
}
43+
</style>

src/svelte/editor/NodeOutputs.svelte

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!--
2+
3+
Copyright (C) 2024 Daniel Fernandes
4+
5+
This file is part of MIDI-FX.
6+
7+
MIDI-FX is free software: you can redistribute it and/or modify it
8+
under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
MIDI-FX is distributed in the hope that it will be useful, but
13+
WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with MIDI-FX. If not, see <https://www.gnu.org/licenses/>.
19+
20+
-->
21+
<script lang="ts">
22+
import Port, { PortType } from "./Port.svelte";
23+
24+
export let id: string;
25+
export let inputs: string[] = [];
26+
export let handles = {};
27+
</script>
28+
29+
<div class="node-outputs">
30+
{#each inputs as name}
31+
<Port type={PortType.Output} {name} {id} bind:handle={handles[name]}/>
32+
{/each}
33+
</div>
34+
35+
<style>
36+
.node-outputs {
37+
width: 24px;
38+
display: flex;
39+
flex-direction: column;
40+
align-items: center;
41+
justify-content: space-evenly;
42+
}
43+
</style>

src/svelte/editor/NodeWrapper.svelte

Lines changed: 5 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import ContextMenu from "./ContextMenu.svelte";
5858
import { showInfo } from "./InfoModal.svelte";
5959
import interact from "interactjs";
60+
import NodeInputsUI from "./NodeInputs.svelte";
61+
import NodeOutputsUI from "./NodeOutputs.svelte";
6062
6163
export let id: string;
6264
export let type: string;
@@ -108,6 +110,7 @@
108110
109111
function updateIOCoords(elements: IOElements, io: { [key: string]: LiveIO }) {
110112
for (const [name, elm] of Object.entries(elements)) {
113+
console.log(name)
111114
const displayCoords = getCenterCoords(elm);
112115
const { x, y } = displayToEditorCoords(displayCoords);
113116
io[name].x = x;
@@ -145,66 +148,6 @@
145148
updateCoords();
146149
});
147150
148-
function onOutputPointerdown(e: PointerEvent, name: string) {
149-
if (e.pointerType !== "mouse" || e.buttons === 1) {
150-
e.stopPropagation();
151-
152-
pseudoConnection.update((val) => {
153-
val.from = [id, name];
154-
return val;
155-
});
156-
}
157-
}
158-
159-
function onInputPointerup(e: Event, name: string) {
160-
pseudoConnection.update((val) => {
161-
if (val.from) {
162-
createConnection(val.from[0], val.from[1], [id, name]);
163-
val.from = undefined;
164-
}
165-
return val;
166-
});
167-
}
168-
169-
function onInputPointerdown(e: PointerEvent, name: string) {
170-
if (e.pointerType !== "mouse" || e.buttons === 1) {
171-
e.stopPropagation();
172-
173-
function onInputPointerleave(e: PointerEvent) {
174-
if (e.pointerType !== "mouse" || e.buttons === 1) {
175-
e.stopPropagation();
176-
let outputNode, outputName;
177-
instances.update((val: LiveInstances) => {
178-
for (const node of Object.entries(val)) {
179-
for (const output of Object.entries(node[1].outputs)) {
180-
for (const connection of output[1].connections) {
181-
if (connection[0] === id && connection[1] === name) {
182-
outputNode = node[0];
183-
outputName = output[0];
184-
}
185-
}
186-
}
187-
}
188-
return val;
189-
});
190-
if (outputNode) {
191-
destroyConnection(outputNode, outputName, [id, name]);
192-
pseudoConnection.update((val) => {
193-
val.from = [outputNode, outputName];
194-
return val;
195-
});
196-
}
197-
}
198-
}
199-
200-
e.target.addEventListener("pointerleave", onInputPointerleave);
201-
202-
window.addEventListener("pointerup", () => {
203-
e.target.removeEventListener("pointerleave", onInputPointerleave);
204-
});
205-
}
206-
}
207-
208151
let showContextMenu: boolean = false;
209152
let contextMenuCoords: Point;
210153
@@ -250,20 +193,7 @@
250193
bind:this={node}
251194
>
252195
<div class="inputs">
253-
{#each Object.entries(liveInputs) as [name]}
254-
<div class="input">
255-
<div
256-
class={name}
257-
bind:this={inputElements[name]}
258-
on:pointerup={(e) => {
259-
onInputPointerup(e, name);
260-
}}
261-
on:pointerdown|stopPropagation={(e) => onInputPointerdown(e, name)}
262-
>
263-
<span>{name}</span>
264-
</div>
265-
</div>
266-
{/each}
196+
<NodeInputsUI {id} inputs={Object.keys(liveInputs)} bind:handles={inputElements}/>
267197
</div>
268198

269199
<div class="body">
@@ -283,19 +213,7 @@
283213
</div>
284214

285215
<div class="outputs">
286-
{#each Object.entries(liveOutputs) as [name]}
287-
<div class="output">
288-
<div
289-
class={name}
290-
bind:this={outputElements[name]}
291-
on:pointerdown={(e) => {
292-
onOutputPointerdown(e, name);
293-
}}
294-
>
295-
<span>{name}</span>
296-
</div>
297-
</div>
298-
{/each}
216+
<NodeOutputsUI {id} inputs={Object.keys(liveOutputs)} bind:handles={outputElements}/>
299217
</div>
300218
</div>
301219

@@ -336,42 +254,4 @@
336254
padding: 4px 12px;
337255
}
338256
339-
/* text */
340-
341-
:is(div.output, div.input) {
342-
writing-mode: vertical-lr;
343-
color: black;
344-
font-size: 12px;
345-
margin: 4px 0px;
346-
padding: 0px;
347-
width: 24px;
348-
}
349-
350-
/* socket */
351-
352-
:is(div.output, div.input) > div {
353-
padding: 6px 4px;
354-
background: white;
355-
}
356-
357-
div.input > div {
358-
border-radius: 6px 0 0 6px;
359-
}
360-
361-
div.output > div {
362-
border-radius: 0 6px 6px 0;
363-
}
364-
365-
/* hover effect */
366-
367-
:is(div.input, div.output) > div:hover {
368-
background: hsl(0 0% 10%) !important;
369-
color: white;
370-
}
371-
372-
/* special colors */
373-
374-
:is(div.output, div.input) > div.MIDI {
375-
background: hsl(160 90% 60%);
376-
}
377257
</style>

0 commit comments

Comments
 (0)