mirror of
https://github.com/dancojocaru2000/logic-circuits-simulator.git
synced 2025-06-19 10:32:28 +03:00
Compare commits
3 commits
2a19505ed3
...
0ab607ae3d
Author | SHA1 | Date | |
---|---|---|---|
0ab607ae3d | |||
ce97a691a0 | |||
19ddc565ed |
4 changed files with 241 additions and 48 deletions
|
@ -27,6 +27,16 @@ class NewProjectDialog extends HookWidget {
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
|
Align(
|
||||||
|
alignment: Alignment.centerRight,
|
||||||
|
child: IconButton(
|
||||||
|
icon: const Icon(Icons.close),
|
||||||
|
tooltip: 'Close',
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
Center(
|
Center(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||||
import 'package:logic_circuits_simulator/models/project.dart';
|
import 'package:logic_circuits_simulator/models/project.dart';
|
||||||
|
@ -18,18 +19,50 @@ class EditComponentPage extends HookWidget {
|
||||||
final anySave = useState(false);
|
final anySave = useState(false);
|
||||||
final projectState = useProvider<ProjectState>();
|
final projectState = useProvider<ProjectState>();
|
||||||
final ce = projectState.index.components.where((c) => c.componentId == component.componentId).first;
|
final ce = projectState.index.components.where((c) => c.componentId == component.componentId).first;
|
||||||
|
final truthTable = useState(ce.truthTable?.toList());
|
||||||
|
final inputs = useState(ce.inputs.toList());
|
||||||
|
final outputs = useState(ce.outputs.toList());
|
||||||
final componentNameEditingController = useTextEditingController(text: ce.componentName);
|
final componentNameEditingController = useTextEditingController(text: ce.componentName);
|
||||||
useValueListenable(componentNameEditingController);
|
useValueListenable(componentNameEditingController);
|
||||||
final dirty = useMemoized(() {
|
final dirty = useMemoized(
|
||||||
|
() {
|
||||||
if (componentNameEditingController.text.isEmpty) {
|
if (componentNameEditingController.text.isEmpty) {
|
||||||
// Don't allow saving empty name
|
// Don't allow saving empty name
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (inputs.value.isEmpty) {
|
||||||
|
// Don't allow saving empty inputs
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (outputs.value.isEmpty) {
|
||||||
|
// Don't allow saving empty outputs
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (componentNameEditingController.text != ce.componentName) {
|
if (componentNameEditingController.text != ce.componentName) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (!const ListEquality().equals(inputs.value, ce.inputs)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!const ListEquality().equals(outputs.value, ce.outputs)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!const ListEquality().equals(truthTable.value, ce.truthTable)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}, [componentNameEditingController.text, ce.componentName]);
|
},
|
||||||
|
[
|
||||||
|
componentNameEditingController.text,
|
||||||
|
ce.componentName,
|
||||||
|
inputs.value,
|
||||||
|
ce.inputs,
|
||||||
|
outputs.value,
|
||||||
|
ce.outputs,
|
||||||
|
truthTable.value,
|
||||||
|
ce.truthTable,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
return WillPopScope(
|
return WillPopScope(
|
||||||
onWillPop: () async {
|
onWillPop: () async {
|
||||||
|
@ -91,38 +124,154 @@ class EditComponentPage extends HookWidget {
|
||||||
SliverToBoxAdapter(
|
SliverToBoxAdapter(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
child: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
'Inputs',
|
'Inputs',
|
||||||
style: Theme.of(context).textTheme.headline5,
|
style: Theme.of(context).textTheme.headline5,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
tooltip: 'Add new input',
|
||||||
|
onPressed: () {
|
||||||
|
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
SliverList(
|
SliverList(
|
||||||
delegate: SliverChildBuilderDelegate(
|
delegate: SliverChildBuilderDelegate(
|
||||||
(context, i) => ListTile(
|
(context, idx) => ListTile(
|
||||||
title: Text(ce.inputs[i]),
|
leading: inputs.value.length > 1 ? IconButton(
|
||||||
|
icon: const Icon(Icons.remove_circle),
|
||||||
|
color: Colors.red,
|
||||||
|
tooltip: 'Remove input ${inputs.value[idx]}',
|
||||||
|
onPressed: () async {
|
||||||
|
final shouldRemove = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: Text('Remove Input ${inputs.value[idx]}?'),
|
||||||
|
content: const Text('Are you sure you want to remove the input?'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(false),
|
||||||
|
child: const Text('Cancel'),
|
||||||
),
|
),
|
||||||
childCount: ce.inputs.length,
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop(true);
|
||||||
|
},
|
||||||
|
style: ButtonStyle(
|
||||||
|
backgroundColor: MaterialStateProperty.all(Colors.red),
|
||||||
|
),
|
||||||
|
child: const Text('Remove'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (shouldRemove == true) {
|
||||||
|
if (truthTable.value != null) {
|
||||||
|
final tt = truthTable.value!.toList();
|
||||||
|
final shiftIndex = inputs.value.length - 1 - idx;
|
||||||
|
final shifted = 1 << shiftIndex;
|
||||||
|
final indecesToRemove = [];
|
||||||
|
for (var i = tt.length - 1; i >= 0; i--) {
|
||||||
|
if (i & shifted == 0) {
|
||||||
|
indecesToRemove.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (final i in indecesToRemove) {
|
||||||
|
tt.removeAt(i);
|
||||||
|
}
|
||||||
|
truthTable.value = tt;
|
||||||
|
}
|
||||||
|
inputs.value = inputs.value.toList()..removeRange(idx, idx+1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) : null,
|
||||||
|
title: Text(inputs.value[idx]),
|
||||||
|
),
|
||||||
|
childCount: inputs.value.length,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SliverToBoxAdapter(
|
SliverToBoxAdapter(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
child: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
'Outputs',
|
'Outputs',
|
||||||
style: Theme.of(context).textTheme.headline5,
|
style: Theme.of(context).textTheme.headline5,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
tooltip: 'Add new output',
|
||||||
|
onPressed: () {
|
||||||
|
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
SliverList(
|
SliverList(
|
||||||
delegate: SliverChildBuilderDelegate(
|
delegate: SliverChildBuilderDelegate(
|
||||||
(context, i) => ListTile(
|
(context, idx) => ListTile(
|
||||||
title: Text(ce.outputs[i]),
|
leading: outputs.value.length > 1 ? IconButton(
|
||||||
|
icon: const Icon(Icons.remove_circle),
|
||||||
|
color: Colors.red,
|
||||||
|
tooltip: 'Remove output ${outputs.value[idx]}',
|
||||||
|
onPressed: () async {
|
||||||
|
final shouldRemove = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: Text('Remove Output ${outputs.value[idx]}?'),
|
||||||
|
content: const Text('Are you sure you want to remove the output?'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(false),
|
||||||
|
child: const Text('Cancel'),
|
||||||
),
|
),
|
||||||
childCount: ce.outputs.length,
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.of(context).pop(true);
|
||||||
|
},
|
||||||
|
style: ButtonStyle(
|
||||||
|
backgroundColor: MaterialStateProperty.all(Colors.red),
|
||||||
|
),
|
||||||
|
child: const Text('Remove'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (shouldRemove == true) {
|
||||||
|
if (truthTable.value != null) {
|
||||||
|
for (var i = 0; i < truthTable.value!.length; i++) {
|
||||||
|
truthTable.value!.replaceRange(i, i+1, [truthTable.value![i].replaceRange(idx, idx+1, "")]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outputs.value = outputs.value.toList()..removeRange(idx, idx+1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) : null,
|
||||||
|
title: Text(outputs.value[idx]),
|
||||||
|
),
|
||||||
|
childCount: outputs.value.length,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (ce.truthTable != null) ...[
|
if (truthTable.value != null) ...[
|
||||||
SliverToBoxAdapter(
|
SliverToBoxAdapter(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
@ -136,9 +285,12 @@ class EditComponentPage extends HookWidget {
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
child: TruthTableEditor(
|
child: TruthTableEditor(
|
||||||
truthTable: ce.truthTable!,
|
truthTable: truthTable.value!,
|
||||||
inputs: ce.inputs,
|
inputs: inputs.value,
|
||||||
outputs: ce.outputs,
|
outputs: outputs.value,
|
||||||
|
onUpdateTable: (idx, newValue) {
|
||||||
|
truthTable.value = truthTable.value?.toList()?..replaceRange(idx, idx+1, [newValue]);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -150,6 +302,11 @@ class EditComponentPage extends HookWidget {
|
||||||
if (componentNameEditingController.text.isNotEmpty) {
|
if (componentNameEditingController.text.isNotEmpty) {
|
||||||
await projectState.editComponent(component.copyWith(componentName: componentNameEditingController.text));
|
await projectState.editComponent(component.copyWith(componentName: componentNameEditingController.text));
|
||||||
}
|
}
|
||||||
|
await projectState.editComponent(ce.copyWith(
|
||||||
|
inputs: inputs.value,
|
||||||
|
outputs: outputs.value,
|
||||||
|
truthTable: truthTable.value,
|
||||||
|
));
|
||||||
anySave.value = true;
|
anySave.value = true;
|
||||||
// TODO: Implement saving
|
// TODO: Implement saving
|
||||||
},
|
},
|
||||||
|
@ -187,8 +344,9 @@ class TruthTableHeaderText extends StatelessWidget {
|
||||||
|
|
||||||
class TruthTableTrue extends StatelessWidget {
|
class TruthTableTrue extends StatelessWidget {
|
||||||
final BoxBorder? border;
|
final BoxBorder? border;
|
||||||
|
final void Function()? onTap;
|
||||||
|
|
||||||
const TruthTableTrue({super.key, this.border});
|
const TruthTableTrue({super.key, this.border, this.onTap});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -196,6 +354,8 @@ class TruthTableTrue extends StatelessWidget {
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
border: border,
|
border: border,
|
||||||
),
|
),
|
||||||
|
child: InkWell(
|
||||||
|
onTap: onTap,
|
||||||
child: Text(
|
child: Text(
|
||||||
'T',
|
'T',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
|
@ -204,14 +364,16 @@ class TruthTableTrue extends StatelessWidget {
|
||||||
fontSize: 20,
|
fontSize: 20,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TruthTableFalse extends StatelessWidget {
|
class TruthTableFalse extends StatelessWidget {
|
||||||
final BoxBorder? border;
|
final BoxBorder? border;
|
||||||
|
final void Function()? onTap;
|
||||||
|
|
||||||
const TruthTableFalse({super.key, this.border});
|
const TruthTableFalse({super.key, this.border, this.onTap});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -219,6 +381,8 @@ class TruthTableFalse extends StatelessWidget {
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
border: border,
|
border: border,
|
||||||
),
|
),
|
||||||
|
child: InkWell(
|
||||||
|
onTap: onTap,
|
||||||
child: Text(
|
child: Text(
|
||||||
'F',
|
'F',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
|
@ -227,6 +391,7 @@ class TruthTableFalse extends StatelessWidget {
|
||||||
fontSize: 20,
|
fontSize: 20,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -236,7 +401,9 @@ class TruthTableEditor extends StatelessWidget {
|
||||||
final List<String> outputs;
|
final List<String> outputs;
|
||||||
final List<String> truthTable;
|
final List<String> truthTable;
|
||||||
|
|
||||||
const TruthTableEditor({Key? key, required this.inputs, required this.outputs, required this.truthTable}) : super(key: key);
|
final void Function(int, String) onUpdateTable;
|
||||||
|
|
||||||
|
const TruthTableEditor({Key? key, required this.inputs, required this.outputs, required this.truthTable, required this.onUpdateTable}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -268,18 +435,33 @@ class TruthTableEditor extends StatelessWidget {
|
||||||
final inputBinary = (index - 1).toRadixString(2).padLeft(inputs.length, '0');
|
final inputBinary = (index - 1).toRadixString(2).padLeft(inputs.length, '0');
|
||||||
final outputBinary = truthTable[index - 1];
|
final outputBinary = truthTable[index - 1];
|
||||||
|
|
||||||
Widget runeToWidget(int rune, {BoxBorder? border}) {
|
Widget runeToWidget({required int rune, void Function()? onTap, BoxBorder? border}) {
|
||||||
return int.parse(String.fromCharCode(rune)) != 0 ? TruthTableTrue(border: border) : TruthTableFalse(border: border);
|
return int.parse(String.fromCharCode(rune)) != 0
|
||||||
|
? TruthTableTrue(
|
||||||
|
border: border,
|
||||||
|
onTap: onTap,
|
||||||
|
)
|
||||||
|
: TruthTableFalse(
|
||||||
|
border: border,
|
||||||
|
onTap: onTap,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return TableRow(
|
return TableRow(
|
||||||
children: inputBinary.runes.indexedMap(
|
children: inputBinary.runes.indexedMap(
|
||||||
(index, r) => runeToWidget(
|
(i, r) => runeToWidget(
|
||||||
r,
|
rune: r,
|
||||||
border: index == inputBinary.runes.length - 1 ? const Border(right: BorderSide(width: 2)) : null,
|
border: i == inputBinary.runes.length - 1 ? const Border(right: BorderSide(width: 2)) : null,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.followedBy(outputBinary.runes.map((r) => runeToWidget(r)))
|
.followedBy(outputBinary.runes.indexedMap(
|
||||||
|
(i, r) => runeToWidget(
|
||||||
|
rune: r,
|
||||||
|
onTap: () {
|
||||||
|
onUpdateTable(index - 1, outputBinary.replaceRange(i, i+1, (outputBinary[i] == "1") ? "0" : "1"));
|
||||||
|
},
|
||||||
|
),
|
||||||
|
))
|
||||||
.toList(),
|
.toList(),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
|
@ -28,7 +28,7 @@ packages:
|
||||||
name: async
|
name: async
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.9.0"
|
version: "2.8.2"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -128,7 +128,7 @@ packages:
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.1.0"
|
version: "4.1.0"
|
||||||
collection:
|
collection:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: collection
|
name: collection
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
|
|
|
@ -34,6 +34,7 @@ dependencies:
|
||||||
intl: ^0.17.0
|
intl: ^0.17.0
|
||||||
flutter_hooks: ^0.18.3
|
flutter_hooks: ^0.18.3
|
||||||
uuid: ^3.0.6
|
uuid: ^3.0.6
|
||||||
|
collection: ^1.16.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Add table
Reference in a new issue