objective c - How to code drawing a straight line with two mouse clicks (OSX Mac App)? -


i'm trying make simple drawing app (osx mac app) , i'm trying figure out how user can draw line 2 mouse clicks, example, first mouse click (mousedown mouseup) mark origin point of line, , second mouse click (mousedown mouseup) mark end point of line. before user makes second click of end point, i'd line (before anchoring end point) shown live, kind of in photoshop. both objective-c , swift fine.

so far i've got...

var newlinear = nsbezierpath()  override func mousedown(theevent: nsevent) {         super.mousedown(theevent)         var lastpoint = theevent.locationinwindow         lastpoint.x -= frame.origin.x         lastpoint.y -= frame.origin.y         newlinear.movetopoint(lastpoint)     }  override func mouseup(theevent: nsevent) {         var newpoint = theevent.locationinwindow         newpoint.x -= frame.origin.x         newpoint.y -= frame.origin.y         newlinear.linetopoint(newpoint)         needsdisplay = true     } 

cheers!

enums associated values great application scales , possibly adds other tools , states.

enum state {     case normal     case drawingline(from: cgpoint, to: cgpoint) } var state = state.normal  override func mousedown(theevent: nsevent) {     super.mousedown(theevent)     var lastpoint = theevent.locationinwindow     lastpoint.x -= frame.origin.x     lastpoint.y -= frame.origin.y     state = .drawingline(from: lastpoint, to: lastpoint) }  override func mouseup(theevent: nsevent) {     if case .drawingline(let firstpoint, _) = state {         var newpoint = theevent.locationinwindow         newpoint.x -= frame.origin.x         newpoint.y -= frame.origin.y         //finalize line `firstpoint` `newpoint`     } }  override func mousemoved(theevent: nsevent) {     if case .drawingline(let firstpoint, _) = state {         needsdisplay = true         var newpoint = theevent.locationinwindow         newpoint.x -= frame.origin.x         newpoint.y -= frame.origin.y         state = .drawingline(from: firstpoint, to: newpoint)     } }  override func draw(_ dirtyrect: nsrect) {     if case .drawingline(let firstpoint, let secondpoint) = state {         //draw line `firstpoint` `secondpoint`     } } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -