-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preliminary support for function in script route
- Loading branch information
1 parent
e34ec6e
commit 7cd0814
Showing
10 changed files
with
208 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
BTrain/Views/Scripts/RouteScript/RouteScriptFunctionsView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2021-22 Jean Bovet | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), | ||
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
import SwiftUI | ||
|
||
struct RouteScriptFunctionsView: View { | ||
|
||
let catalog: LocomotiveFunctionsCatalog | ||
@Binding var cmd: RouteScriptCommand | ||
@State private var editedCmd = RouteScriptCommand(action: .functions) | ||
|
||
@Environment(\.presentationMode) var presentationMode | ||
|
||
var body: some View { | ||
VStack { | ||
HStack { | ||
Text("Functions to execute:") | ||
Spacer() | ||
} | ||
if editedCmd.functions.isEmpty { | ||
Spacer() | ||
Button("Click To Add a Function") { | ||
addFunction() | ||
} | ||
.buttonStyle(.borderless) | ||
.padding() | ||
Spacer() | ||
} else { | ||
List { | ||
ForEach($editedCmd.functions, id:\.self) { function in | ||
HStack { | ||
Picker("State:", selection: function.enabled) { | ||
Text("Enable").tag(true) | ||
Text("Disable").tag(false) | ||
}.labelsHidden() | ||
|
||
Picker("Function:", selection: function.type) { | ||
ForEach(catalog.allTypes, id: \.self) { type in | ||
HStack { | ||
if let name = catalog.name(for: type) { | ||
Text("f\(type): \(name)") | ||
} else { | ||
Text("f\(type)") | ||
} | ||
if let image = catalog.image(for: type)?.copy(size: .init(width: 20, height: 20)) { | ||
Image(nsImage: image) | ||
.renderingMode(.template) | ||
} | ||
}.tag(type) | ||
} | ||
}.labelsHidden() | ||
|
||
Button("") { | ||
addFunction() | ||
}.buttonStyle(.borderless) | ||
|
||
Button("") { | ||
removeFunction(function: function.wrappedValue) | ||
}.buttonStyle(.borderless) | ||
} | ||
} | ||
} | ||
} | ||
Divider() | ||
HStack { | ||
Spacer() | ||
|
||
Button("Cancel") { | ||
presentationMode.wrappedValue.dismiss() | ||
} | ||
.keyboardShortcut(.cancelAction) | ||
|
||
Spacer().fixedSpace() | ||
|
||
Button("OK") { | ||
cmd = editedCmd | ||
presentationMode.wrappedValue.dismiss() | ||
} | ||
.keyboardShortcut(.defaultAction) | ||
}.padding() | ||
}.onAppear() { | ||
editedCmd = cmd | ||
} | ||
} | ||
|
||
func addFunction() { | ||
editedCmd.functions.append(.init(type: 0, enabled: false)) | ||
} | ||
|
||
func removeFunction(function: RouteScriptCommand.Function) { | ||
editedCmd.functions.removeAll(where: { $0.id == function.id }) | ||
} | ||
} | ||
|
||
struct RouteScriptFunctionsView_Previews: PreviewProvider { | ||
|
||
static let cmd = { | ||
var command = RouteScriptCommand(action: .functions) | ||
command.functions = [.init(type: 0, enabled: true)] | ||
return command | ||
}() | ||
|
||
static var previews: some View { | ||
Group { | ||
RouteScriptFunctionsView(catalog: LocomotiveFunctionsCatalog(), cmd: .constant(RouteScriptCommand(action: .functions))) | ||
}.previewDisplayName("Empty") | ||
Group { | ||
RouteScriptFunctionsView(catalog: LocomotiveFunctionsCatalog(), cmd: .constant(cmd)) | ||
}.previewDisplayName("Functions") | ||
} | ||
} |
Oops, something went wrong.