Keeping state of a drag operation separately.

This commit is contained in:
Sander Schobers 2020-05-15 12:15:44 +02:00
parent c0c5235d5a
commit 5d297c98b8
2 changed files with 60 additions and 17 deletions

View File

@ -19,7 +19,7 @@ type ControlBase struct {
bounds geom.RectangleF32
offset geom.PointF32
parent Control
dragStart *geom.PointF32
drag Dragable
over bool
pressed bool
@ -62,18 +62,18 @@ func (c *ControlBase) Handle(ctx Context, e Event) {
case *MouseMoveEvent:
c.over = over(e.MouseEvent)
if c.pressed {
if c.dragStart == nil {
var start = c.ToControlPosition(e.Pos())
c.dragStart = &start
if c.onDragStart != nil {
c.onDragStart(start)
}
} else {
var start = *c.dragStart
if start, ok := c.drag.IsDragging(); ok {
var move = c.ToControlPosition(e.Pos())
c.drag.Move(move)
if c.onDragMove != nil {
c.onDragMove(start, move)
}
} else {
var start = c.ToControlPosition(e.Pos())
c.drag.Start(start)
if c.onDragStart != nil {
c.onDragStart(start)
}
}
}
case *MouseLeaveEvent:
@ -85,10 +85,9 @@ func (c *ControlBase) Handle(ctx Context, e Event) {
}
case *MouseButtonUpEvent:
if e.Button == MouseButtonLeft {
if c.dragStart != nil {
var start = *c.dragStart
if start, ok := c.drag.IsDragging(); ok {
var end = c.ToControlPosition(e.Pos())
c.dragStart = nil
c.drag.Cancel()
if c.onDragEnd != nil {
c.onDragEnd(start, end)
}

44
ui/dragable.go Normal file
View File

@ -0,0 +1,44 @@
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
}