Created
February 6, 2015 21:32
-
-
Save chrisschreiner/8929dbb04a2e69ab608e to your computer and use it in GitHub Desktop.
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
class ST_Initial: ST_Root { | |
var selection = NodeSelection() | |
override var stateName:String {return "Singleselection"} | |
init(context: PStateMachineContext) { | |
super.init(context) | |
} | |
override func perform(f: FunctionEnum) { | |
switch f { | |
case let .mouseDown(event): | |
if let node = nodeAtLocation(event) { | |
selection.toggleSingle(node) | |
} | |
case let .keyModFlags(m): | |
if (m & NSEventModifierFlags.ShiftKeyMask) == NSEventModifierFlags.ShiftKeyMask { | |
//Note: When the selection in this class changes, the selection in the parent state stays unchanged, this is why it is passed as an "inout" | |
pushState(ST_MultiSelection(context: context, selection:&selection)) | |
} | |
default: | |
return | |
} | |
} | |
private func nodeAtLocation(e:NSEvent) -> HexNode? { | |
if let (node,location,offset) = getNodeAtEvent(e) { | |
if let node = node { | |
return node | |
} | |
} | |
return nil | |
} | |
} | |
class ST_MultiSelection: ST_Initial { | |
override var stateName:String {return "Multiselection"} | |
init(context: PStateMachineContext, inout selection:NodeSelection) { | |
super.init(context: context) | |
self.selection = selection | |
} | |
override func perform(f: FunctionEnum) { | |
switch f { | |
case let .mouseDown(event): | |
if let node = nodeAtLocation(event) { | |
selection.toggleMulti(node) | |
} | |
case let .keyModFlags(m): | |
if !(m & NSEventModifierFlags.ShiftKeyMask == NSEventModifierFlags.ShiftKeyMask) { | |
popState() | |
} | |
default: | |
return | |
} | |
} | |
} | |
class NodeSelection { | |
var list = [HexNode]() | |
func toggleSingle(entry:HexNode) { | |
clear() | |
if !entry.selected { | |
entry.selected = true | |
assert(list.count == 0) | |
list.append(entry) | |
} | |
} | |
func toggleMulti(entry:HexNode) { | |
if entry.selected { | |
for (i,n) in enumerate(list) { | |
if n === entry { | |
list.removeAtIndex(i) | |
n.selected = false | |
break | |
} | |
} | |
} else { | |
entry.selected = true | |
list.append(entry) | |
} | |
} | |
func set(entry:HexNode) { | |
list.each{$0.selected = false} | |
clear() | |
list.insert(entry, atIndex: 0) | |
entry.selected = true | |
} | |
func clear() { | |
list.each{$0.selected = false} | |
list.removeAll(keepCapacity: false) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment