mirror of
https://github.com/dancojocaru2000/logic-circuits-simulator.git
synced 2025-02-22 17:19:36 +02:00
Implemented removing stuff when dropping on bin
This commit is contained in:
parent
a86a5e6aec
commit
fc361b04d3
1 changed files with 99 additions and 3 deletions
|
@ -47,6 +47,99 @@ class DesignComponentPage extends HookWidget {
|
||||||
final wireToDelete = useState<String?>(null);
|
final wireToDelete = useState<String?>(null);
|
||||||
final sourceToConnect = useState<String?>(null);
|
final sourceToConnect = useState<String?>(null);
|
||||||
final hoveredIO = useState<String?>(null);
|
final hoveredIO = useState<String?>(null);
|
||||||
|
final deleteMovingWidget = useMemoized(
|
||||||
|
() => () async {
|
||||||
|
if (!deleteOnDrop.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final w = movingWidget.value;
|
||||||
|
final cs = componentState;
|
||||||
|
// First remove all connected wires
|
||||||
|
if (w is DesignComponent) {
|
||||||
|
final wires = cs.wiringDraft.wires
|
||||||
|
.where(
|
||||||
|
(wire) => wire.input.startsWith('${w.instanceId}/') || wire.output.startsWith('${w.instanceId}/')
|
||||||
|
)
|
||||||
|
.map((wire) => wire.wireId)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
await cs.updateDesign(cs.designDraft.copyWith(
|
||||||
|
wires: cs.designDraft.wires
|
||||||
|
.where((wire) => !wires.contains(wire.wireId))
|
||||||
|
.toList(),
|
||||||
|
), commit: false);
|
||||||
|
await cs.updateWiring(cs.wiringDraft.copyWith(
|
||||||
|
wires: cs.wiringDraft.wires
|
||||||
|
.where((wire) => !wires.contains(wire.wireId))
|
||||||
|
.toList(),
|
||||||
|
), commit: false);
|
||||||
|
await cs.updateDesign(cs.designDraft.copyWith(
|
||||||
|
components: cs.designDraft.components
|
||||||
|
.where((comp) => comp.instanceId != w.instanceId)
|
||||||
|
.toList(),
|
||||||
|
));
|
||||||
|
await cs.updateWiring(cs.wiringDraft.copyWith(
|
||||||
|
instances: cs.wiringDraft.instances
|
||||||
|
.where((comp) => comp.instanceId != w.instanceId)
|
||||||
|
.toList(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
else if (w is DesignInput) {
|
||||||
|
final wires = cs.wiringDraft.wires
|
||||||
|
.where(
|
||||||
|
(wire) => wire.output == 'self/${w.name}',
|
||||||
|
)
|
||||||
|
.map((wire) => wire.wireId)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
await cs.updateDesign(cs.designDraft.copyWith(
|
||||||
|
wires: cs.designDraft.wires
|
||||||
|
.where((wire) => !wires.contains(wire.wireId))
|
||||||
|
.toList(),
|
||||||
|
), commit: false);
|
||||||
|
await cs.updateWiring(cs.wiringDraft.copyWith(
|
||||||
|
wires: cs.wiringDraft.wires
|
||||||
|
.where((wire) => !wires.contains(wire.wireId))
|
||||||
|
.toList(),
|
||||||
|
));
|
||||||
|
await cs.updateDesign(cs.designDraft.copyWith(
|
||||||
|
inputs: cs.designDraft.inputs
|
||||||
|
.where((input) => input.name != w.name)
|
||||||
|
.toList(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
else if (w is DesignOutput) {
|
||||||
|
final wires = cs.wiringDraft.wires
|
||||||
|
.where(
|
||||||
|
(wire) => wire.input == 'self/${w.name}',
|
||||||
|
)
|
||||||
|
.map((wire) => wire.wireId)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
await cs.updateDesign(cs.designDraft.copyWith(
|
||||||
|
wires: cs.designDraft.wires
|
||||||
|
.where((wire) => !wires.contains(wire.wireId))
|
||||||
|
.toList(),
|
||||||
|
), commit: false);
|
||||||
|
await cs.updateWiring(cs.wiringDraft.copyWith(
|
||||||
|
wires: cs.wiringDraft.wires
|
||||||
|
.where((wire) => !wires.contains(wire.wireId))
|
||||||
|
.toList(),
|
||||||
|
));
|
||||||
|
await cs.updateDesign(cs.designDraft.copyWith(
|
||||||
|
outputs: cs.designDraft.outputs
|
||||||
|
.where((output) => output.name != w.name)
|
||||||
|
.toList(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[
|
||||||
|
movingWidget.value,
|
||||||
|
deleteOnDrop.value,
|
||||||
|
componentState.wiringDraft,
|
||||||
|
componentState.designDraft,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
final widgets = useMemoized(() => [
|
final widgets = useMemoized(() => [
|
||||||
for (final subcomponent in componentState.designDraft.components)
|
for (final subcomponent in componentState.designDraft.components)
|
||||||
|
@ -95,6 +188,7 @@ class DesignComponentPage extends HookWidget {
|
||||||
movingWidget.value = subcomponent;
|
movingWidget.value = subcomponent;
|
||||||
},
|
},
|
||||||
onPointerUp: (event) {
|
onPointerUp: (event) {
|
||||||
|
deleteMovingWidget();
|
||||||
componentState.updateDesign(componentState.designDraft);
|
componentState.updateDesign(componentState.designDraft);
|
||||||
movingWidgetUpdater.value = null;
|
movingWidgetUpdater.value = null;
|
||||||
movingWidget.value = null;
|
movingWidget.value = null;
|
||||||
|
@ -168,6 +262,7 @@ class DesignComponentPage extends HookWidget {
|
||||||
movingWidget.value = input;
|
movingWidget.value = input;
|
||||||
},
|
},
|
||||||
onPointerUp: (event) {
|
onPointerUp: (event) {
|
||||||
|
deleteMovingWidget();
|
||||||
componentState.updateDesign(componentState.designDraft);
|
componentState.updateDesign(componentState.designDraft);
|
||||||
movingWidgetUpdater.value = null;
|
movingWidgetUpdater.value = null;
|
||||||
movingWidget.value = null;
|
movingWidget.value = null;
|
||||||
|
@ -226,6 +321,7 @@ class DesignComponentPage extends HookWidget {
|
||||||
movingWidget.value = output;
|
movingWidget.value = output;
|
||||||
},
|
},
|
||||||
onPointerUp: (event) {
|
onPointerUp: (event) {
|
||||||
|
deleteMovingWidget();
|
||||||
componentState.updateDesign(componentState.designDraft);
|
componentState.updateDesign(componentState.designDraft);
|
||||||
movingWidgetUpdater.value = null;
|
movingWidgetUpdater.value = null;
|
||||||
movingWidget.value = null;
|
movingWidget.value = null;
|
||||||
|
@ -249,7 +345,7 @@ class DesignComponentPage extends HookWidget {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
for (final wire in componentState.wiring.wires)
|
for (final wire in componentState.wiringDraft.wires)
|
||||||
(() {
|
(() {
|
||||||
const ioCircleDiameter = 20;
|
const ioCircleDiameter = 20;
|
||||||
|
|
||||||
|
@ -391,7 +487,7 @@ class DesignComponentPage extends HookWidget {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
})(),
|
})(),
|
||||||
], [componentState.designDraft, isSimulating.value, componentState.partialVisualSimulation!.outputsValues, designSelection.value, sourceToConnect.value]);
|
], [componentState.wiringDraft, componentState.designDraft, isSimulating.value, componentState.partialVisualSimulation!.outputsValues, designSelection.value, sourceToConnect.value]);
|
||||||
useEffect(() {
|
useEffect(() {
|
||||||
final wList = widgets;
|
final wList = widgets;
|
||||||
canvasController.addCanvasObjects(wList);
|
canvasController.addCanvasObjects(wList);
|
||||||
|
|
Loading…
Add table
Reference in a new issue