-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.go
189 lines (158 loc) · 3.54 KB
/
shapes.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package engoutil
import (
"strings"
"github.com/EngoEngine/ecs"
"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/common"
)
type ShapeKind uint16
const (
SHAPE_KIND_LINE ShapeKind = 1 << iota
SHAPE_KIND_STIPPLE_LINE
SHAPE_KIND_RECT
SHAPE_KIND_STIPPLE_RECT
SHAPE_KIND_CIRCLE
SHAPE_KIND_POLYGON
SHAPE_KIND_CURVE
SHAPE_KIND_TEXT
SHAPE_KIND_IMAGE
)
var shapeKindName = [9]string{"Line", "StippleLine", "Rect", "StippleRect", "Circle", "Polygon", "Curve", "Text", "Image"}
func (kind ShapeKind) String() string {
var s []string
for i, name := range shapeKindName {
if kind&(1<<i) > 0 {
s = append(s, name)
}
}
if len(s) > 0 {
return strings.Join(s, "|")
}
return "invalid shape"
}
type MouseAction uint8
const (
MOUSE_NONE MouseAction = iota
MOUSE_CLICKED
MOUSE_DRAGGED
)
type Point [2]float32
type Points []Point
func (p Points) Points() (points []engo.Point) {
points = make([]engo.Point, len(p))
for i, v := range p {
points[i] = engo.Point{X: v[0], Y: v[1]}
}
return
}
func (p Points) Equal(points []engo.Point) bool {
if len(p) != len(points) {
return true
}
for i, v := range points {
if v.X != p[i][0] || v.Y != p[i][1] {
return true
}
}
return false
}
type Shapes []*Shape
// 基础形状
type Shape struct {
kind ShapeKind
Entity *ecs.BasicEntity
Render *common.RenderComponent
Space *common.SpaceComponent
// Mouseable
mouseAction MouseAction
Mouse *common.MouseComponent
// attribute 0:x, 1:y
// Line 2:offsetX, 3:offsetY, 4:sin, 5:cos
// Rect 2:w, 3:h
// Circle 2:radius, 3:arc
// Polygon 2:w, 3:h
// Curve 2:w, 3:h
// Text 2:ax, 3:ay, 4:scale
attr [6]float32
onUpdate func(*Shape, float32)
onHover [2]func(*Shape)
onClick func(*Shape)
onDrag func(*Shape, float32, float32)
// SHAPE_KIND_STIPPLE_LINE, SHAPE_KIND_STIPPLE_RECT
stipple *Stipple
}
// implementation of common.BasicFace
func (s *Shape) ID() uint64 {
return s.Entity.ID()
}
// implementation of common.BasicFace
func (s *Shape) GetBasicEntity() *ecs.BasicEntity {
return s.Entity
}
// implementation of common.RenderFace
func (s *Shape) GetRenderComponent() *common.RenderComponent {
return s.Render
}
// implementation of common.SpaceFace
func (s *Shape) GetSpaceComponent() *common.SpaceComponent {
return s.Space
}
// implementation of common.Mouseable
func (s *Shape) GetMouseComponent() *common.MouseComponent {
return s.Mouse
}
// (*Shape) Hidden
func (s *Shape) Hidden() {
s.Render.Hidden = true
}
// (*Shape) Show
func (s *Shape) Show() {
s.Render.Hidden = false
}
// (*Shape) Visible
func (s *Shape) Visible() bool {
return !s.Render.Hidden
}
// (*Shape) OnUpdate
func (s *Shape) OnUpdate(fn func(*Shape, float32)) {
s.onUpdate = fn
}
// (*Shape) OnHover
func (s *Shape) OnHover(enter, leave func(*Shape)) {
if s.Mouse == nil {
s.Mouse = &common.MouseComponent{}
}
s.onHover[0] = enter
s.onHover[1] = leave
}
// (*Shape) OnClick
func (s *Shape) OnClick(fn func(*Shape)) {
if s.Mouse == nil {
s.Mouse = &common.MouseComponent{}
}
s.onClick = fn
}
// (*Shape) OnDrag
func (s *Shape) OnDrag(fn func(*Shape, float32, float32)) {
if s.Mouse == nil {
s.Mouse = &common.MouseComponent{}
}
s.onDrag = fn
}
func (s *Shape) requireKind(kind ShapeKind, method string) bool {
if kind&s.kind != s.kind {
warning("(Shape) %s(), %s expected, got %s", method, kind, s.kind)
return false
}
return true
}
func newShape(kind ShapeKind) (s *Shape) {
entity := ecs.NewBasic()
s = &Shape{
kind: kind,
Entity: &entity,
Render: &common.RenderComponent{},
Space: &common.SpaceComponent{},
}
return
}