/
scene.go
163 lines (140 loc) · 3.77 KB
/
scene.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
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package xyzview provides a GUI view for a 3D xyz scene.
package xyzview
//go:generate core generate
import (
"image"
"image/draw"
"cogentcore.org/core/abilities"
"cogentcore.org/core/core"
"cogentcore.org/core/events"
"cogentcore.org/core/styles"
"cogentcore.org/core/system"
"cogentcore.org/core/units"
"cogentcore.org/core/vgpu"
"cogentcore.org/core/xyz"
)
// Scene is a core.Widget that manages a xyz.Scene,
// providing the basic rendering logic for the 3D scene
// in the 2D core GUI context.
type Scene struct {
core.WidgetBase
// XYZ is the 3D xyz.Scene
XYZ *xyz.Scene `set:"-"`
// how to deal with selection / manipulation events
SelectionMode SelectionModes
// currently selected node
CurrentSelected xyz.Node `copier:"-" json:"-" xml:"-" view:"-"`
// currently selected manipulation control point
CurrentManipPoint *ManipPoint `copier:"-" json:"-" xml:"-" view:"-"`
// parameters for selection / manipulation box
SelectionParams SelectionParams `view:"inline"`
}
func (sw *Scene) OnInit() {
sw.XYZ = xyz.NewScene("Scene")
sw.XYZ.Defaults()
sw.SelectionParams.Defaults()
sw.WidgetBase.OnInit()
sw.HandleEvents()
sw.SetStyles()
}
func (sw *Scene) OnAdd() {
sw.WidgetBase.OnAdd()
sw.Scene.AddDirectRender(sw)
}
func (sw *Scene) Destroy() {
sw.Scene.DeleteDirectRender(sw)
sw.XYZ.Destroy()
sw.WidgetBase.Destroy()
}
// SceneXYZ returns the xyz.Scene
func (sw *Scene) SceneXYZ() *xyz.Scene {
return sw.XYZ
}
func (sw *Scene) SetStyles() {
sw.Style(func(s *styles.Style) {
s.SetAbilities(true, abilities.Clickable, abilities.Focusable, abilities.Activatable, abilities.Slideable, abilities.LongHoverable, abilities.DoubleClickable)
s.Grow.Set(1, 1)
s.Min.Set(units.Em(20))
})
}
func (sw *Scene) HandleEvents() {
sw.On(events.Scroll, func(e events.Event) {
// pos := sw.Geom.ContentBBox.Min
// fmt.Println("loc off:", e.LocalOff(), "pos:", pos, "e pos:", e.WindowPos())
// e.SetLocalOff(e.LocalOff().Add(pos))
sw.XYZ.MouseScrollEvent(e.(*events.MouseScroll))
sw.NeedsRender()
})
sw.On(events.KeyChord, func(e events.Event) {
sw.XYZ.KeyChordEvent(e)
sw.NeedsRender()
})
sw.HandleSlideEvents()
sw.HandleSelectEvents()
}
func (sw *Scene) Config() {
sw.ConfigFrame()
}
// ConfigFrame configures the framebuffer for GPU rendering,
// using the RenderWin GPU and Device.
func (sw *Scene) ConfigFrame() {
zp := image.Point{}
sz := sw.Geom.Size.Actual.Content.ToPointFloor()
if sz == zp {
return
}
sw.XYZ.Geom.Size = sz
doConfig := false
if sw.XYZ.Frame != nil {
cursz := sw.XYZ.Frame.Format.Size
if cursz == sz {
return
}
} else {
doConfig = true
}
win := sw.WidgetBase.Scene.EventMgr.RenderWin()
if win == nil {
return
}
drw := win.SystemWindow.Drawer()
system.TheApp.RunOnMain(func() {
sw.XYZ.ConfigFrameFromSurface(drw.Surface().(*vgpu.Surface))
if doConfig {
sw.XYZ.Config()
}
})
sw.XYZ.SetFlag(true, xyz.ScNeedsRender)
sw.NeedsRender()
}
func (sw *Scene) Render() {
sw.ConfigFrame() // no-op if all good
if sw.XYZ.Frame == nil {
return
}
if sw.XYZ.Is(xyz.ScNeedsConfig) {
system.TheApp.RunOnMain(func() {
sw.XYZ.Config()
})
}
sw.XYZ.DoUpdate()
}
// DirectRenderImage uploads framebuffer image
func (sw *Scene) DirectRenderImage(drw system.Drawer, idx int) {
if sw.XYZ.Frame == nil || !sw.IsVisible() {
return
}
drw.SetFrameImage(idx, sw.XYZ.Frame.Frames[0])
}
// DirectRenderDraw draws the current image to RenderWin drawer
func (sw *Scene) DirectRenderDraw(drw system.Drawer, idx int, flipY bool) {
if !sw.IsVisible() {
return
}
bb := sw.Geom.TotalBBox
ibb := image.Rectangle{Max: bb.Size()}
drw.Copy(idx, 0, bb.Min, ibb, draw.Src, flipY)
}