@@ -39,12 +39,7 @@ struct ContentView: View {
3939 @ State private var showInfo = false
4040 @ State private var showCodeGeneratorPaywall = false
4141 @ State private var showCodeGeneratorPlaceholder = false
42- @ State private var isSimulationMode = false
43- @ State private var simulationEngine : VisuStructSimulationEngine
44- @ State private var simulationPlayTask : Task < Void , Never > ? = nil
45- @ State private var isSimulationPlaying = false
46- @ State private var simulationWasPlayingBeforeInput = false
47- @ State private var simulationInputText = " "
42+ @ State private var simulation : SimulationController
4843 @ ObservedObject private var recentFilesStore : RecentFilesStore
4944 /// Erhöhen bei jedem Tipp auf „Code erzeugen“, damit die Anzeige neu aufgebaut wird (auch wenn der Dialog schon offen ist).
5045 @ State private var codeGeneratorRegenerationToken : Int = 0
@@ -74,7 +69,6 @@ struct ContentView: View {
7469 @ AppStorage ( VisuStructUIScaleStep . appStorageKeyCanvas ) private var canvasScaleRaw : String = VisuStructUIScaleStep . small . rawValue
7570 @ AppStorage ( VisuStructUIScaleStep . appStorageKeyInspector ) private var inspectorScaleRaw : String = VisuStructUIScaleStep . small . rawValue
7671 @ State private var editorFocusToken = UUID ( )
77- @ AppStorage ( SimulationStepDelayOption . appStorageKey ) private var simulationStepDelayRaw : String = SimulationStepDelayOption . defaultRawValue
7872 @ AppStorage ( SimulationHighlightMode . appStorageKey ) private var simulationHighlightModeRaw : String = SimulationHighlightMode . defaultRawValue
7973
8074 private var canvasScaleStep : VisuStructUIScaleStep {
@@ -89,22 +83,14 @@ struct ContentView: View {
8983 ExportFilenameStyle . fromStored ( exportFilenameStyleRaw )
9084 }
9185
92- private var simulationStepDelay : SimulationStepDelayOption {
93- SimulationStepDelayOption . fromStored ( simulationStepDelayRaw )
94- }
95-
96- private var simulationHighlightMode : SimulationHighlightMode {
97- SimulationHighlightMode . fromStored ( simulationHighlightModeRaw )
98- }
99-
10086 init ( ) {
10187 let startupLang = AppLanguage . currentFromDefaults ( )
10288 let model = DiagramSheetTabsModel . makeInitial ( language : startupLang )
10389 let recentStore = RecentFilesStore . shared
10490 _tabsModel = State ( initialValue : model )
10591 _recentFilesStore = ObservedObject ( wrappedValue : recentStore )
10692 _documentServices = State ( initialValue : DiagramEditorDocumentServices ( tabsModel : model , recentFilesStore : recentStore ) )
107- _simulationEngine = State ( initialValue : VisuStructSimulationEngine ( document : model . tabs [ 0 ] . document ) )
93+ _simulation = State ( initialValue : SimulationController ( tabsModel : model ) )
10894 }
10995
11096 // MARK: - Tab-Titel & Dokument öffnen
@@ -770,15 +756,15 @@ struct ContentView: View {
770756 simulationCanvasBlock
771757 . layoutPriority ( 1 )
772758 SimulationStatePanel (
773- engine : simulationEngine ,
774- isPlaying : isSimulationPlaying ,
775- inputText : $simulationInputText ,
759+ engine : simulation . engine ,
760+ isPlaying : simulation . isPlaying ,
761+ inputText : $simulation . inputText ,
776762 highlightModeRaw : $simulationHighlightModeRaw ,
777- onPlay : playSimulation ,
778- onPause : pauseSimulation ,
779- onStep : stepSimulation ,
780- onSubmitInput : submitSimulationInput ,
781- onStop : resetSimulation
763+ onPlay : { simulation . play ( ) } ,
764+ onPause : { simulation . pause ( ) } ,
765+ onStep : { simulation . step ( ) } ,
766+ onSubmitInput : { simulation . submitInput ( ) } ,
767+ onStop : { simulation . reset ( ) }
782768 )
783769 . frame ( width : panelWidth )
784770 . frame ( maxHeight : . infinity , alignment : . top )
@@ -789,15 +775,15 @@ struct ContentView: View {
789775 simulationCanvasBlock
790776 . frame ( height : max ( 340 , geo . size . height * 0.55 ) )
791777 SimulationStatePanel (
792- engine : simulationEngine ,
793- isPlaying : isSimulationPlaying ,
794- inputText : $simulationInputText ,
778+ engine : simulation . engine ,
779+ isPlaying : simulation . isPlaying ,
780+ inputText : $simulation . inputText ,
795781 highlightModeRaw : $simulationHighlightModeRaw ,
796- onPlay : playSimulation ,
797- onPause : pauseSimulation ,
798- onStep : stepSimulation ,
799- onSubmitInput : submitSimulationInput ,
800- onStop : resetSimulation
782+ onPlay : { simulation . play ( ) } ,
783+ onPause : { simulation . pause ( ) } ,
784+ onStep : { simulation . step ( ) } ,
785+ onSubmitInput : { simulation . submitInput ( ) } ,
786+ onStop : { simulation . reset ( ) }
801787 )
802788 }
803789 . frame ( maxWidth : . infinity , alignment : . leading )
@@ -926,104 +912,13 @@ struct ContentView: View {
926912 )
927913 }
928914
929- // MARK: - Simulation (Modus & Steuerung)
930-
931- private func beginSimulationMode ( ) {
932- pauseSimulation ( )
933- var engine = VisuStructSimulationEngine ( document : document )
934- engine . reset ( )
935- simulationEngine = engine
936- simulationInputText = " "
937- replaceSelectedPathOnCurrentTab ( engine . currentStepPath )
938- withAnimation ( . easeInOut ( duration : 0.18 ) ) {
939- isSimulationMode = true
940- }
941- }
942-
943- private func endSimulationMode ( ) {
944- pauseSimulation ( )
945- withAnimation ( . easeInOut ( duration : 0.18 ) ) {
946- isSimulationMode = false
947- }
948- if tabsModel . currentTab . selectedPath == nil , ! document . elements . isEmpty {
949- replaceSelectedPathOnCurrentTab ( [ 0 ] )
950- }
951- }
952-
953- private func stepSimulation ( ) {
954- guard simulationEngine . canStep else {
955- pauseSimulation ( )
956- return
957- }
958- let wasPlaying = isSimulationPlaying
959- let lastExecutedPath = simulationEngine . currentStepPath
960- var engine = simulationEngine
961- engine . step ( )
962- simulationEngine = engine
963- if simulationHighlightMode == . nextToExecute , engine . canStep {
964- replaceSelectedPathOnCurrentTab ( engine . currentStepPath )
965- } else {
966- // lastExecuted (oder Ende/Input): letztes ausgeführtes Element bleibt markiert.
967- replaceSelectedPathOnCurrentTab ( lastExecutedPath ?? engine . currentStepPath )
968- }
969- // Wenn ein Input als Ergebnis des Steps angefordert wurde, Play pausieren,
970- // aber merken, dass wir nach OK automatisch weiterlaufen sollen.
971- if engine . state . inputRequest != nil , wasPlaying {
972- simulationWasPlayingBeforeInput = true
973- }
974- if ! engine . canStep {
975- pauseSimulation ( )
976- }
977- }
978-
979- private func resetSimulation ( ) {
980- pauseSimulation ( )
981- var engine = VisuStructSimulationEngine ( document : document )
982- engine . reset ( )
983- simulationEngine = engine
984- simulationInputText = " "
985- replaceSelectedPathOnCurrentTab ( engine . currentStepPath )
986- }
915+ // MARK: - Simulation (Modus & Steuerung) → `SimulationController`
987916
988- private func submitSimulationInput ( ) {
989- let shouldResume = isSimulationPlaying || simulationWasPlayingBeforeInput
990- pauseSimulation ( )
991- var engine = simulationEngine
992- engine . provideInput ( simulationInputText )
993- simulationEngine = engine
994- if engine . state . inputRequest == nil {
995- simulationInputText = " "
996- }
997- replaceSelectedPathOnCurrentTab ( engine . currentStepPath )
917+ private var isSimulationMode : Bool { simulation . isActive }
998918
999- if shouldResume , isSimulationMode , engine . canStep {
1000- simulationWasPlayingBeforeInput = false
1001- playSimulation ( )
1002- }
1003- }
919+ private func beginSimulationMode ( ) { simulation . begin ( ) }
1004920
1005- private func playSimulation ( ) {
1006- guard ! isSimulationPlaying , simulationEngine . canStep else { return }
1007- isSimulationPlaying = true
1008- simulationPlayTask ? . cancel ( )
1009- simulationPlayTask = Task { @MainActor in
1010- while ! Task . isCancelled && isSimulationMode && simulationEngine . canStep {
1011- stepSimulation ( )
1012- guard ! Task . isCancelled , isSimulationMode , simulationEngine . canStep else { break }
1013- try? await Task . sleep ( nanoseconds : simulationStepDelay . nanoseconds )
1014- }
1015- if ! Task . isCancelled {
1016- isSimulationPlaying = false
1017- simulationPlayTask = nil
1018- }
1019- }
1020- }
1021-
1022- private func pauseSimulation ( ) {
1023- simulationPlayTask ? . cancel ( )
1024- simulationPlayTask = nil
1025- isSimulationPlaying = false
1026- }
921+ private func endSimulationMode ( ) { simulation . end ( ) }
1027922
1028923 // MARK: - Tab-Titel (Platzhalter)
1029924
0 commit comments