/
drag.go
74 lines (64 loc) · 1.64 KB
/
drag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package draw
import (
"image"
"github.com/codeation/lineation/draw/box"
)
type dragState struct {
catchPoint image.Point
oldParent, newParent *box.Box
beforeBox *box.Box
IsGridCleared bool
}
func (state *dragState) IsDroppable() bool {
return state.newParent != nil
}
func (v *View) Catch(eventPoint image.Point) (*dragState, bool) {
if v.activeBox == nil || v.activeBox == v.rootBox {
return nil, false
}
pt := eventPoint.Sub(v.offset)
if !v.activeBox.In(pt) {
return nil, false
}
return &dragState{
catchPoint: eventPoint,
}, true
}
func (v *View) Drag(state *dragState, eventPoint image.Point) {
plus := eventPoint.Sub(state.catchPoint.Sub(v.offset))
state.newParent, state.beforeBox = v.rootBox.FindOther(
image.Pt(eventPoint.X, v.activeBox.Point().Y+plus.Y).Sub(v.offset), v.activeBox)
v.activeBox.Drag(plus)
}
func (v *View) DrawDrag(state *dragState) {
if !state.IsGridCleared {
v.activeBox.Raise()
v.w.Clear()
v.rootBox.DrawGrid(v.w, v.offset, v.activeBox)
v.w.Show()
state.IsGridCleared = true
}
if state.newParent != state.oldParent {
if state.oldParent != nil {
state.oldParent.DeEmphasize()
}
if state.newParent != nil {
state.newParent.Emphasize()
}
state.oldParent = state.newParent
}
}
func (v *View) DrawRemain(state *dragState) {
v.w.Clear()
v.rootBox.DrawGrid(v.w, v.offset, nil)
v.w.Show()
if state.newParent != nil {
state.newParent.DeEmphasize()
}
v.activeBox.Drag(image.Pt(0, 0).Add(v.offset))
}
func (v *View) Drop(state *dragState, eventPoint image.Point) {
state.newParent.DeEmphasize()
state.newParent.Adopt(v.activeBox, state.beforeBox)
v.QueueDraw()
}