zntg/ui/dragable.go

45 lines
1.1 KiB
Go

package ui
import "opslag.de/schobers/geom"
// Dragable keeps track of the mouse position during a drag operation.
type Dragable struct {
start *geom.PointF32
current *geom.PointF32
}
// Cancel cancels the drag operation and returns the start and end position.
func (d *Dragable) Cancel() (geom.PointF32, geom.PointF32) {
if d.start == nil {
return geom.ZeroPtF32, geom.ZeroPtF32
}
start, end := *d.start, *d.current
d.start = nil
d.current = nil
return start, end
}
// IsDragging returns if the drag operation is in progress and the start location if so.
func (d *Dragable) IsDragging() (geom.PointF32, bool) {
if d.start != nil {
return *d.start, true
}
return geom.ZeroPtF32, false
}
// Move calculates the delta between the start point and the current position.
func (d *Dragable) Move(p geom.PointF32) (geom.PointF32, bool) {
if d.start == nil {
return geom.ZeroPtF32, false
}
delta := p.Sub(*d.current)
d.current = &p
return delta, true
}
// Start set the start point of the drag operation.
func (d *Dragable) Start(p geom.PointF32) {
d.start = &p
d.current = &p
}