-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_run.go
More file actions
249 lines (206 loc) · 6.74 KB
/
handler_run.go
File metadata and controls
249 lines (206 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"bufio"
"fmt"
"image/color"
"log"
"os"
"strconv"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"github.com/josephus-git/TCAS-simulation-Fyne/graphics/ui"
"github.com/josephus-git/TCAS-simulation-Fyne/internal/aviation"
"github.com/josephus-git/TCAS-simulation-Fyne/internal/config"
"github.com/josephus-git/TCAS-simulation-Fyne/internal/util"
)
// a holds the main Fyne application instance for the program.
var a fyne.App
// inputWindow holds the Fyne GUI window used for user input and controls.
var inputWindow fyne.Window
// StartFyne initializes the Fyne GUI application, sets up the simulation input window with controls for configuration,
// and manages the lifecycle of both the input and simulation display windows.
func StartFyne(cfg *config.Config, simState *aviation.SimulationState) {
if cfg.FirstRun {
// Create a new Fyne application
a = app.NewWithID("tcas.app")
a.Settings().SetTheme(ui.CustomDarkTheme{})
// --- Initial Input Window ---
if inputWindow == nil {
inputWindow = a.NewWindow("TCAS Simulation Setup")
inputWindow.Resize(fyne.NewSize(400, 600)) // Smaller initial window
}
// A close interceptor for the main window
inputWindow.SetCloseIntercept(func() {
inputWindow.Hide()
cfg.FirstRun = false
})
title := canvas.NewText("TCAS Simulation Setup", color.White)
title.TextSize = 24
title.TextStyle.Bold = true
title.Alignment = fyne.TextAlignCenter
errorMessage := canvas.NewText("", color.RGBA{R: 255, A: 255}) // Red text for errors
errorMessage.Alignment = fyne.TextAlignCenter
errorMessage.TextStyle.Italic = true
information1 := canvas.NewText("Light grey is cruise altitude: 10000.0", color.RGBA{R: 200, G: 200, B: 200, A: 255})
information1.TextSize = 10
information2 := canvas.NewText("Light green is cruise altitude: 11000.0", color.RGBA{G: 255, A: 255})
information2.TextSize = 10
information3 := canvas.NewText("Light blue is cruise altitude: 12000.0", color.RGBA{B: 255, A: 255})
information3.TextSize = 10
// Input entry for Number of Planes
numPlanesEntry := widget.NewEntry()
numPlanesEntry.Validator = func(s string) error {
_, err := strconv.Atoi(s)
if err != nil {
if s == "" && cfg.FirstRun {
return nil
}
return fmt.Errorf("please input a valid integer")
}
return nil
}
numPlanesEntry.Hide()
numPlanesFormItem := widget.NewFormItem("Number of Planes:", numPlanesEntry)
// Input entry Duration of Simulation
durationEntry := widget.NewEntry()
durationEntry.SetPlaceHolder("Enter duration of simulation")
durationEntry.Validator = func(s string) error {
num, err := strconv.Atoi(s)
{
if err != nil {
return fmt.Errorf("please input a valid integer")
}
}
if num < 1 {
return fmt.Errorf("1 minute minimum")
}
return nil
}
durationFormItem := widget.NewFormItem("Duration (minutes):", durationEntry)
// checkbox for Varying Altitude
varyingAltitudeCheckbox := widget.NewCheck("Yes", func(b bool) {})
varyingAltitudeCheckbox.SetChecked(simState.DifferentAltitudes)
varyingAltitudeCheckbox.Hide()
// A form to group the input fields
inputForm := widget.NewForm(
numPlanesFormItem,
durationFormItem,
widget.NewFormItem("Varying Altitude:", varyingAltitudeCheckbox),
)
var simulationWindow fyne.Window
// The simulation button
startSimulationButton := widget.NewButton("Start Simulation", func() {
simulationWindow = a.NewWindow("Airport Simulation")
// update the form so the number of planes can be updated
simulationWindow.SetOnClosed(func() {
numPlanesEntry.Show()
numPlanesEntry.SetPlaceHolder("")
varyingAltitudeCheckbox.Show()
inputWindow.Show()
if simState.SimIsRunning {
aviation.EmergencyStop(simState)
}
aviation.CloseLogFiles(simState)
})
var numAirPlanes int
if !simState.SimWindowOpened {
numAirPlanes = cfg.NoOfAirplanes
} else {
numAirPlanesV, err := strconv.Atoi(numPlanesEntry.Text)
if err != nil || numAirPlanesV < 4 {
errorMessage.Text = "Please enter a valid number of airplanes (minimum 4)."
errorMessage.Refresh()
return
} else {
numAirPlanes = numAirPlanesV
}
}
durationOfSimulation, err := strconv.Atoi(durationEntry.Text)
if err != nil || durationOfSimulation < 1 {
errorMessage.Text = "Please enter a valid duration of simulation in minutes"
errorMessage.Refresh()
return
}
if !cfg.FirstRun && simState.SimWindowOpened {
cfg.DifferentAltitudes = varyingAltitudeCheckbox.Checked
}
if simState.SimIsRunning {
errorMessage.Text = "Please wait a few seconds before restarting the simulation"
errorMessage.Refresh()
return
}
errorMessage.Text = "" // Clear error message
errorMessage.Refresh()
simState.Airports = []*aviation.Airport{}
simState.PlanesInFlight = []*aviation.Plane{}
// Initialize the airports
cfg.NoOfAirplanes = numAirPlanes
aviation.InitializeAirports(cfg, simState)
// run the simulation
go aviation.StartSimulation(simState, time.Duration(durationOfSimulation))
// Create and show the simulation window
if !cfg.FirstRun {
ui.GraphicsSimulationInit(simState, simulationWindow, inputWindow)
}
simulationWindow.Show()
inputWindow.Hide()
aviation.OpenLogFiles(cfg, simState)
simState.SimWindowOpened = true
log.Printf("Starting simulation with %d airplanes.", numAirPlanes)
})
// Set content
background := canvas.NewRectangle(color.RGBA{})
inputContent := container.NewVBox(
layout.NewSpacer(), // Pushes content towards the center
title,
layout.NewSpacer(),
inputForm,
layout.NewSpacer(),
startSimulationButton,
layout.NewSpacer(),
information1,
information2,
information3,
layout.NewSpacer(),
errorMessage,
layout.NewSpacer(),
)
inputWindow.SetContent(container.NewStack(background, inputContent))
// Show input window
inputWindow.Show()
go startPartition(cfg, simState)
a.Run()
} else {
fyne.Do(func() { inputWindow.Show() })
}
}
// startPartition handles the command-line interface for the TCAS simulator, processing user input for various commands.
func startPartition(cfg *config.Config, simState *aviation.SimulationState) {
cfg.FirstRun = false
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("TCAS-simulator > ")
scanner.Scan()
input := util.CleanInput(scanner.Text())
argument2 := ""
if len(input) > 1 {
argument2 = input[1]
}
if len(input) == 0 {
fmt.Println("")
continue
}
cmd, ok := getCommand(cfg, simState, argument2)[input[0]]
if !ok {
fmt.Println("Unknown command, type <help> for usage")
continue
}
cmd.callback()
println("")
}
}