Skip to content

Commit c7642b0

Browse files
Add drag and drop support to the route box
1 parent e2a25c6 commit c7642b0

File tree

5 files changed

+88
-15
lines changed

5 files changed

+88
-15
lines changed

SwiftSplit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
CD7CDC03256F626D002AE46F /* LiveSplitServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD7CDC02256F626D002AE46F /* LiveSplitServer.swift */; };
1919
CD7CDC05256F7304002AE46F /* SimpleWebSocketServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD7CDC04256F7304002AE46F /* SimpleWebSocketServer.swift */; };
2020
CD7CDC2825717D2D002AE46F /* CelesteSplitter.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD7CDC2725717D2D002AE46F /* CelesteSplitter.swift */; };
21+
CDA94457257382D300DB2A85 /* RouteBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDA94456257382D300DB2A85 /* RouteBox.swift */; };
2122
DB56D9F05D0BC5B4DE4CECC8 /* Pods_SwiftSplit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CF09341B2ECF29501B823DEC /* Pods_SwiftSplit.framework */; };
2223
/* End PBXBuildFile section */
2324

@@ -40,6 +41,7 @@
4041
CD7CDC02256F626D002AE46F /* LiveSplitServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveSplitServer.swift; sourceTree = "<group>"; };
4142
CD7CDC04256F7304002AE46F /* SimpleWebSocketServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleWebSocketServer.swift; sourceTree = "<group>"; };
4243
CD7CDC2725717D2D002AE46F /* CelesteSplitter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CelesteSplitter.swift; sourceTree = "<group>"; };
44+
CDA94456257382D300DB2A85 /* RouteBox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouteBox.swift; sourceTree = "<group>"; };
4345
CF09341B2ECF29501B823DEC /* Pods_SwiftSplit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftSplit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4446
E1C15A185F4552F09EFE4D9D /* Pods-SwiftSplit.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftSplit.release.xcconfig"; path = "Target Support Files/Pods-SwiftSplit/Pods-SwiftSplit.release.xcconfig"; sourceTree = "<group>"; };
4547
/* End PBXFileReference section */
@@ -100,6 +102,7 @@
100102
CD7CDC2725717D2D002AE46F /* CelesteSplitter.swift */,
101103
CD7CDBE8256F5D0F002AE46F /* Assets.xcassets */,
102104
CD7CDBEA256F5D0F002AE46F /* Main.storyboard */,
105+
CDA94456257382D300DB2A85 /* RouteBox.swift */,
103106
CD7CDBED256F5D0F002AE46F /* Info.plist */,
104107
CD7CDBEE256F5D0F002AE46F /* SwiftSplit.entitlements */,
105108
CD7CDBF4256F5DDB002AE46F /* SwiftSplit-Bridging-Header.h */,
@@ -242,6 +245,7 @@
242245
CD7CDC00256F5DFC002AE46F /* memory_scanner.c in Sources */,
243246
CD7CDC01256F5DFC002AE46F /* CelesteEventGenerator.swift in Sources */,
244247
CD7CDBE7256F5D0D002AE46F /* ViewController.swift in Sources */,
248+
CDA94457257382D300DB2A85 /* RouteBox.swift in Sources */,
245249
CD7CDBFF256F5DFC002AE46F /* CelesteScanner.swift in Sources */,
246250
CD7CDC05256F7304002AE46F /* SimpleWebSocketServer.swift in Sources */,
247251
CD7CDBE5256F5D0D002AE46F /* AppDelegate.swift in Sources */,

SwiftSplit/Base.lproj/Main.storyboard

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@
871871
</constraints>
872872
</view>
873873
</box>
874-
<box title="Route" translatesAutoresizingMaskIntoConstraints="NO" id="neJ-0Z-w8w">
874+
<box title="Route" translatesAutoresizingMaskIntoConstraints="NO" id="neJ-0Z-w8w" customClass="RouteBox" customModule="SwiftSplit" customModuleProvider="target">
875875
<rect key="frame" x="17" y="118" width="503" height="98"/>
876876
<view key="contentView" id="2ja-yw-6wr">
877877
<rect key="frame" x="3" y="3" width="497" height="80"/>
@@ -1065,6 +1065,7 @@
10651065
<outlet property="livesplitClientsLabel" destination="QmT-1L-MUd" id="380-ot-XCf"/>
10661066
<outlet property="loadedRouteLabel" destination="KyE-c7-ajR" id="ffN-6y-9V1"/>
10671067
<outlet property="nextEventLabel" destination="RDG-cX-b88" id="BVL-GJ-0yk"/>
1068+
<outlet property="routeBox" destination="neJ-0Z-w8w" id="l6h-DE-9Q4"/>
10681069
<outlet property="routeDataLabel" destination="8ue-EI-oPc" id="BDr-aH-3zV"/>
10691070
</connections>
10701071
</viewController>

SwiftSplit/CelesteSplitter.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class CelesteSplitter {
3737
private var gameTimeRunning = false
3838
private(set) var routeIndex = 0
3939

40+
func reset() {
41+
server.reset()
42+
routeIndex = 0
43+
}
44+
4045
func update() throws -> [String] {
4146
guard let info = try scanner.getInfo() else {
4247
autoSplitterInfo = AutoSplitterInfo()

SwiftSplit/RouteBox.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// RouteBox.swift
3+
// SwiftSplit
4+
//
5+
// Created by Pierce Corcoran on 11/28/20.
6+
// Copyright © 2020 Pierce Corcoran. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import Cocoa
11+
12+
protocol RouteBoxDelegate: class {
13+
func openRouteFile(url: URL)
14+
}
15+
16+
class RouteBox: NSBox {
17+
weak var delegate: RouteBoxDelegate?
18+
19+
override init(frame frameRect: NSRect) {
20+
super.init(frame: frameRect)
21+
self.registerMyTypes()
22+
}
23+
24+
required init?(coder: NSCoder) {
25+
super.init(coder: coder)
26+
self.registerMyTypes()
27+
}
28+
29+
final private func registerMyTypes() {
30+
registerForDraggedTypes(
31+
[NSPasteboard.PasteboardType.URL,
32+
NSPasteboard.PasteboardType.fileURL,
33+
NSPasteboard.PasteboardType.fileNameType(forPathExtension: "json")
34+
])
35+
}
36+
37+
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
38+
return NSDragOperation.copy
39+
}
40+
41+
override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
42+
return true
43+
}
44+
45+
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
46+
print("performDragOperation")
47+
48+
if let urls = sender.draggingPasteboard.readObjects(forClasses: [NSURL.self]) as? [URL] {
49+
if !urls.isEmpty {
50+
delegate?.openRouteFile(url: urls[0])
51+
}
52+
return true
53+
}
54+
return false
55+
}
56+
}

SwiftSplit/ViewController.swift

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import Cocoa
1010
import Carbon
1111

12-
class ViewController: NSViewController {
12+
class ViewController: NSViewController, RouteBoxDelegate {
1313
var splitter: CelesteSplitter? = nil
1414

1515
@IBOutlet weak var gameTimeLabel: NSTextField!
1616
@IBOutlet weak var nextEventLabel: NSTextField!
1717
@IBOutlet weak var livesplitClientsLabel: NSTextField!
1818
@IBOutlet weak var eventStreamLabel: NSTextField!
1919

20+
@IBOutlet weak var routeBox: RouteBox!
2021
@IBOutlet weak var loadedRouteLabel: NSTextField!
2122
@IBOutlet weak var routeDataLabel: NSTextField!
2223

@@ -36,6 +37,7 @@ class ViewController: NSViewController {
3637
var routeConfig = RouteConfig() {
3738
didSet {
3839
splitter?.routeConfig = routeConfig
40+
splitter?.reset()
3941
}
4042
}
4143

@@ -56,6 +58,7 @@ class ViewController: NSViewController {
5658

5759
eventStreamLabel.stringValue = eventStream.joined(separator: "\n")
5860
server = try? LiveSplitServer(host: "localhost", port: 8777)
61+
routeBox.delegate = self
5962
}
6063

6164
func update() {
@@ -216,27 +219,31 @@ class ViewController: NSViewController {
216219
nextEventLabel.stringValue = splitter.routeIndex < routeConfig.route.count ? "\"\(routeConfig.route[splitter.routeIndex])\"" : "<none>"
217220
}
218221

222+
func openRouteFile(url: URL) {
223+
guard let data = try? Data(contentsOf: url),
224+
let dataValues = try? JSONSerialization.jsonObject(with: data, options: .init()) as? [String: Any],
225+
let config = RouteConfig(json: dataValues)
226+
else {
227+
return
228+
}
229+
hasRouteConfig = true
230+
routeConfig = config
231+
232+
loadedRouteLabel.stringValue = "\(url.lastPathComponent)"
233+
234+
updateInfoViews()
235+
}
236+
219237
@IBAction func loadRoute(_ sender: Any) {
220238
let dialog = NSOpenPanel();
221239

222240
dialog.title = "Choose a route .json file";
223241
dialog.allowedFileTypes = ["json"];
224242

225243
if (dialog.runModal() == .OK) {
226-
guard let fileUrl = dialog.url,
227-
let data = try? Data(contentsOf: fileUrl),
228-
let dataValues = try? JSONSerialization.jsonObject(with: data, options: .init()) as? [String: Any],
229-
let config = RouteConfig(json: dataValues)
230-
else {
231-
return
244+
if let fileUrl = dialog.url {
245+
openRouteFile(url: fileUrl)
232246
}
233-
hasRouteConfig = true
234-
routeConfig = config
235-
236-
loadedRouteLabel.stringValue = "\(fileUrl.lastPathComponent)"
237-
238-
updateInfoViews()
239-
240247
} else {
241248
// User clicked on "Cancel"
242249
return

0 commit comments

Comments
 (0)