I have an array of Rectangles (tbl) and I need to be able to drag them around smoothly
This code (Swift) however, is jerky, and sometimes undoes what it just did
Functionaly this should be nearly identical syntax to Xojo (kinda)
public func tbl_mouseDown (_ index:Int,_ x:Int,_ y:Int,_ rightButton:Bool=false) -> Bool {
if rightButton == false {
// remember the coor of where the mouse was clicked
last_x = CGFloat(x)
last_y = CGFloat(y)
return true
} else {
return false // not dealing with righbutton yet
}
}
public func tbl_mouseDrag (_ index:Int,_ x:Int,_ y:Int,_ rightButton:Bool=false) {
let fX = CGFloat(x) // Swift is strict types, so convert to float
let fY = CGFloat(y)
let deltaX = last_x-fX // calc the delta between last mouse position and current
let deltaY = last_y-fY
if (deltaX != 0) || (deltaY != 0) {
last_x = fX // remember new position
last_y = fY
tbl[index].frame.origin.x -= deltaX // move rectangle by delta
tbl[index].frame.origin.y -= deltaY
print("VERIFY : tbl_mouseDrag \(tbl[index].frame) : \(deltaX),\(deltaY)")
}
}
example : note how the delta keeps flipping
VERIFY : tbl_mouseDrag (7.0, 5.0, 315.0, 195.0) : -5.0,-5.0
VERIFY : tbl_mouseDrag (2.0, 0.0, 315.0, 195.0) : 5.0,5.0
VERIFY : tbl_mouseDrag (7.0, 5.0, 315.0, 195.0) : -5.0,-5.0
VERIFY : tbl_mouseDrag (3.0, 0.0, 315.0, 195.0) : 4.0,5.0
VERIFY : tbl_mouseDrag (7.0, 5.0, 315.0, 195.0) : -4.0,-5.0
VERIFY : tbl_mouseDrag (3.0, 0.0, 315.0, 195.0) : 4.0,5.0
VERIFY : tbl_mouseDrag (7.0, 5.0, 315.0, 195.0) : -4.0,-5.0
any idea what Iām doing wrong?