-
Notifications
You must be signed in to change notification settings - Fork 5
/
tool_sample.go
82 lines (68 loc) · 1.9 KB
/
tool_sample.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
package chunk
import (
"github.com/johanhenriksson/goworld/core/input/mouse"
"github.com/johanhenriksson/goworld/core/object"
"github.com/johanhenriksson/goworld/editor"
"github.com/johanhenriksson/goworld/geometry/lines"
"github.com/johanhenriksson/goworld/math/vec3"
"github.com/johanhenriksson/goworld/physics"
"github.com/johanhenriksson/goworld/render/color"
)
type SampleTool struct {
object.Object
Box object.Object
Reselect func()
}
var _ editor.Tool = &SampleTool{}
func NewSampleTool() *SampleTool {
padding := float32(0.05)
return object.New("Sample Tool", &SampleTool{
Box: object.Builder(object.Empty("Box")).
Attach(lines.NewBox(lines.BoxArgs{
Extents: vec3.One.Scaled(1 + padding),
Color: color.Purple,
})).
Position(vec3.New(-padding/2+0.5, -padding/2+0.5, -padding/2+0.5)).
Create(),
})
}
func (pt *SampleTool) Use(editor *Editor, position, normal vec3.T) {
target := position.Sub(normal.Scaled(0.5))
voxel := editor.GetVoxel(int(target.X), int(target.Y), int(target.Z))
editor.SelectColor(color.RGB8(voxel.R, voxel.G, voxel.B))
if pt.Reselect != nil {
pt.Reselect()
pt.Reselect = nil
}
}
func (pt *SampleTool) Hover(editor *Editor, position, normal vec3.T) {
p := position.Sub(normal.Scaled(0.5))
if editor.InBounds(p) {
object.Enable(pt.Box)
pt.Transform().SetPosition(p.Floor())
} else {
object.Disable(pt.Box)
}
}
func (pt *SampleTool) CanDeselect() bool {
return false
}
func (pt *SampleTool) ToolMouseEvent(ev mouse.Event, hover physics.RaycastHit) {
if hover.Shape == nil {
return
}
editor := object.GetInParents[*Editor](pt)
if editor == nil {
// hm?
return
}
pos := editor.Transform().Unproject(hover.Point)
norm := editor.Transform().UnprojectDir(hover.Normal)
if ev.Action() == mouse.Move {
pt.Hover(editor, pos, norm)
}
if ev.Action() == mouse.Press && ev.Button() == mouse.Button1 {
pt.Use(editor, pos, norm)
ev.Consume()
}
}