Skip to content

Commit

Permalink
Allow copy/paste in galaxy
Browse files Browse the repository at this point in the history
  • Loading branch information
andrews05 committed Sep 1, 2024
1 parent 9076c9d commit 6cfb53b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Plug-Ins/NovaTools/Galaxy Editor/GalaxyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class GalaxyView: NSView, CALayerDelegate, NSViewLayerContentScaleDelegate {

// MARK: - Selection

private var selectedSystems: [SystemView] = []
private(set) var selectedSystems: [SystemView] = []

override func selectAll(_ sender: Any?) {
controller.systemTable.selectAll(self)
Expand Down
59 changes: 42 additions & 17 deletions Plug-Ins/NovaTools/Galaxy Editor/GalaxyWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ extension GalaxyWindowController {
galaxyView.zoomOut(sender)
}

@IBAction func copy(_ sender: Any) {
let pb = NSPasteboard(name: .general)
pb.declareTypes([.RFResource], owner: nil)
pb.writeObjects(selectedSystems)
}

@IBAction func paste(_ sender: Any) {
// Forward to the document
manager.document?.perform(#selector(paste(_:)), with: sender)
}

func createSystem(position: NSPoint) {
manager.createResource(type: ResourceType("sÿst"), id: systemList.last?.id) { [weak self] system in
guard let self else { return }
Expand Down Expand Up @@ -156,6 +167,13 @@ extension GalaxyWindowController {
}
if shouldReload {
self.reload()
if notification.name == .DocumentDidAddResources {
// Select added systems
selectedSystems = resources.filter { $0.typeCode == "sÿst" }
if systemTable.selectedRow > 0 {
systemTable.scrollRowToVisible(systemTable.selectedRow)
}
}
}
}

Expand All @@ -174,8 +192,8 @@ extension GalaxyWindowController {
}
if resource.typeCode == "sÿst" {
systemViews[resource.id]?.updateFrame()
if let idx = systemList.firstIndex(of: resource) {
systemTable.reloadData(forRowIndexes: [idx + 1], columnIndexes: [1])
if let i = self.row(for: resource) {
systemTable.reloadData(forRowIndexes: [i], columnIndexes: [1])
}
} else if resource.typeCode == "nëbu" {
galaxyView.needsDisplay = true
Expand Down Expand Up @@ -252,30 +270,37 @@ extension GalaxyWindowController: NSTableViewDataSource, NSTableViewDelegate {
}

func syncSelectionFromView(clicked: SystemView? = nil) {
var scrollRow: Int?
var indexes = IndexSet()
for (i, system) in systemList.enumerated() {
if let view = systemViews[system.id], view.isHighlighted {
indexes.insert(i + 1)
if view == clicked {
scrollRow = i + 1
}
}
}
isSelectingSystems = true
systemTable.selectRowIndexes(indexes, byExtendingSelection: false)
selectedSystems = galaxyView.selectedSystems.map(\.resource)
isSelectingSystems = false
if let scrollRow {
systemTable.scrollRowToVisible(scrollRow)
if let clicked, let i = self.row(for: clicked.resource) {
systemTable.scrollRowToVisible(i)
}
}

@IBAction func doubleClickSystem(_ sender: Any) {
guard systemTable.clickedRow != 0 else {
return
}
for i in systemTable.selectedRowIndexes {
manager.open(resource: systemList[i - 1])
for system in selectedSystems {
manager.open(resource: system)
}
}

func row(for resource: Resource) -> Int? {
if let i = systemList.firstIndex(of: resource) {
return i + 1
}
return nil
}

var selectedSystems: [Resource] {
get {
systemTable.selectedRowIndexes.map { systemList[$0 - 1] }
}
set {
let indexes = IndexSet(newValue.compactMap(self.row(for:)))
systemTable.selectRowIndexes(indexes, byExtendingSelection: false)
}
}
}
4 changes: 2 additions & 2 deletions ResForge/Classes/ResourceDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -620,15 +620,15 @@ class ResourceDocument: NSDocument, NSWindowDelegate, NSDraggingDestination, NST
@IBAction func cut(_ sender: Any) {
let resources = dataSource.selectedResources(deep: true)
let pb = NSPasteboard.init(name: .general)
pb.declareTypes([.RFResource], owner: self)
pb.declareTypes([.RFResource], owner: nil)
pb.writeObjects(resources)
self.remove(resources: resources)
self.undoManager?.setActionName(NSLocalizedString(resources.count == 1 ? "Cut Resource" : "Cut Resources", comment: ""))
}

@IBAction func copy(_ sender: Any) {
let pb = NSPasteboard(name: .general)
pb.declareTypes([.RFResource], owner: self)
pb.declareTypes([.RFResource], owner: nil)
pb.writeObjects(dataSource.selectedResources(deep: true))
}

Expand Down

0 comments on commit 6cfb53b

Please sign in to comment.