-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.go
112 lines (100 loc) · 2.29 KB
/
canvas.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package engoutil
import (
"github.com/EngoEngine/ecs"
"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/common"
)
var _ ecs.System = (*Canvas)(nil)
var _ ecs.Initializer = (*Canvas)(nil)
func NewCanvas() *Canvas {
return &Canvas{}
}
// Using Canvas instead of RenderSystem
type Canvas struct {
render *common.RenderSystem
mouse *common.MouseSystem
objects Shapes
ready Shapes
ids map[uint64]struct{}
refresh bool
}
func (c *Canvas) Remove(basic ecs.BasicEntity) {
delete(c.ids, basic.ID())
c.render.Remove(basic)
c.mouse.Remove(basic)
}
func (c *Canvas) New(w *ecs.World) {
c.render = &common.RenderSystem{}
c.mouse = &common.MouseSystem{}
c.objects = make(Shapes, 0, 512)
c.ids = make(map[uint64]struct{})
w.AddSystem(c.render)
w.AddSystem(c.mouse)
}
func (c *Canvas) Update(dt float32) {
if c.refresh {
n := len(c.objects)
for i, v := range c.ready {
c.ids[v.Entity.ID()] = struct{}{}
v.Render.SetZIndex(v.Render.StartZIndex + float32(i+n))
c.render.AddByInterface(v)
if v.onClick != nil || v.onHover[0] != nil || v.onDrag != nil {
c.mouse.AddByInterface(v)
}
c.objects = append(c.objects, v)
}
c.ready = make(Shapes, 256)
c.refresh = false
}
for _, v := range c.objects {
if v.onUpdate != nil {
v.onUpdate(v, dt)
}
if v.Render.Hidden {
continue
}
if v.onHover[0] != nil {
if v.Mouse.Hovered {
v.onHover[0](v)
} else if v.Mouse.Leave {
v.onHover[1](v)
}
}
if v.onClick != nil || v.onDrag != nil {
if v.Mouse.Clicked {
v.mouseAction = MOUSE_CLICKED
} else {
if v.Mouse.Dragged {
v.mouseAction = MOUSE_DRAGGED
if v.onDrag != nil {
v.onDrag(v, engo.Input.Mouse.X-v.Space.Position.X, engo.Input.Mouse.Y-v.Space.Position.Y)
}
} else if v.Mouse.Released {
if v.mouseAction != MOUSE_DRAGGED && v.onClick != nil {
v.onClick(v)
}
v.mouseAction = MOUSE_NONE
}
}
}
}
}
func (c *Canvas) Push(shapes ...*Shape) {
for _, s := range shapes {
if s == nil {
continue
}
if _, ok := c.ids[s.Entity.ID()]; !ok {
c.ready = append(c.ready, s)
}
}
}
func (c *Canvas) GroupPush(groups ...Shapes) {
for _, g := range groups {
c.Push(g...)
}
}
// After that, the contents of push will be rendered in the next frame
func (c *Canvas) Draw() {
c.refresh = true
}