From ffa6ca414b1c74f357d4ab760a65e50b825ff20d Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Sun, 3 Jul 2022 15:35:06 +0300 Subject: [PATCH] Added step by step simulation buttons --- lib/pages/design_component.dart | 100 ++++++++++++++++++++++++++++---- lib/utils/simulation.dart | 18 +++--- 2 files changed, 99 insertions(+), 19 deletions(-) diff --git a/lib/pages/design_component.dart b/lib/pages/design_component.dart index a4fe7a5..6bda709 100644 --- a/lib/pages/design_component.dart +++ b/lib/pages/design_component.dart @@ -363,17 +363,45 @@ class DesignComponentPage extends HookWidget { }, child: OrientationBuilder( builder: (context, orientation) { + final stackCanvas = StackCanvas( + key: canvasKey, + canvasController: canvasController, + animationDuration: const Duration(milliseconds: 50), + // disposeController: false, + backgroundColor: Theme.of(context).colorScheme.background, + ); + + final debuggingButtons = DebuggingButtons( + partialSimulation: simulatePartially.value, + onPartialSimulationToggle: () { + simulatePartially.value = !simulatePartially.value; + }, + onReset: simulatePartially.value ? () { + componentState.partialVisualSimulation!.restart(); + } : null, + onNextStep: simulatePartially.value && componentState.partialVisualSimulation!.nextToSimulate.isNotEmpty ? () { + componentState.partialVisualSimulation!.nextStep(); + } : null, + ); + if (orientation == Orientation.portrait) { return Column( mainAxisSize: MainAxisSize.max, children: [ Expanded( - child: StackCanvas( - key: canvasKey, - canvasController: canvasController, - animationDuration: const Duration(milliseconds: 50), - // disposeController: false, - backgroundColor: Theme.of(context).colorScheme.background, + child: Stack( + children: [ + stackCanvas, + if (isSimulating.value) + Positioned( + top: 8, + left: 0, + right: 0, + child: Center( + child: debuggingButtons, + ), + ), + ], ), ), ], @@ -384,12 +412,19 @@ class DesignComponentPage extends HookWidget { mainAxisSize: MainAxisSize.max, children: [ Expanded( - child: StackCanvas( - key: canvasKey, - canvasController: canvasController, - animationDuration: const Duration(milliseconds: 50), - // disposeController: false, - backgroundColor: Theme.of(context).colorScheme.background, + child: Stack( + children: [ + stackCanvas, + if (isSimulating.value) + Positioned( + top: 8, + left: 0, + right: 0, + child: Center( + child: debuggingButtons, + ), + ), + ], ), ), ], @@ -400,4 +435,45 @@ class DesignComponentPage extends HookWidget { ), ); } +} + +class DebuggingButtons extends StatelessWidget { + final bool partialSimulation; + + final void Function() onPartialSimulationToggle; + final void Function()? onReset; + final void Function()? onNextStep; + + const DebuggingButtons({super.key, required this.partialSimulation, required this.onPartialSimulationToggle, this.onReset, this.onNextStep}); + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + IconButton( + onPressed: onPartialSimulationToggle, + icon: Icon( + partialSimulation ? Icons.play_arrow : Icons.pause, + ), + tooltip: partialSimulation ? 'Simulate to end' : 'Simulate partially', + ), + IconButton( + onPressed: onNextStep, + icon: const Icon(Icons.navigate_next), + tooltip: 'Simulate next step', + ), + IconButton( + onPressed: onReset, + icon: const Icon(Icons.replay_outlined), + tooltip: 'Restart simulation', + ), + ], + ), + ), + ); + } } \ No newline at end of file diff --git a/lib/utils/simulation.dart b/lib/utils/simulation.dart index 173da94..6a36fb6 100644 --- a/lib/utils/simulation.dart +++ b/lib/utils/simulation.dart @@ -198,13 +198,7 @@ class PartialVisualSimulation with ChangeNotifier { Future modifyInput(String inputName, bool newValue) { _outputsValues['self/$inputName'] = newValue; - for (final key in _outputsValues.keys.toList()) { - if (!key.startsWith('self/')) { - _outputsValues.remove(key); - } - } - _alreadySimulated.clear(); - return reset(); + return restart(); } Future provideInputs(Map inputs) { @@ -216,6 +210,16 @@ class PartialVisualSimulation with ChangeNotifier { return reset(); } + Future restart() async { + for (final key in _outputsValues.keys.toList()) { + if (!key.startsWith('self/')) { + _outputsValues.remove(key); + } + } + _alreadySimulated.clear(); + return reset(); + } + Future reset() async { nextToSimulate.clear();