/
embed2d.go
483 lines (431 loc) · 13.3 KB
/
embed2d.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright (c) 2019, 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
/*
// Embed2D embeds a 2D Viewport on a vertically-oriented plane, using a texture.
// The embedded viewport contains its own 2D scenegraph and receives events, with
// mouse coordinates translated into the 3D plane space. The full range of Cogent Core
// 2D elements can be embedded.
type Embed2D struct {
Solid
// the embedded viewport to display
Viewport *EmbedViewport
// overall scaling factor relative to an arbitrary but sensible default scale based on size of viewport -- increase to increase size of view
Zoom float32
// if true, will be resized to fit its contents during initialization (though it will never get smaller than original size specified at creation) -- this requires having a core.Layout element (or derivative, such as core.Frame) as the first and only child of the Viewport
FitContent bool
// original standardized 96 DPI size -- the original size specified on creation -- actual size is affected by device pixel ratio and resizing due to FitContent
StdSize image.Point
// original size scaled according to logical dpi
DPISize image.Point
}
const (
// FitContent is used as arg for NewEmbed2D to specify that plane should be resized
// to fit content.
FitContent = true
// FixesSize is used as arg for NewEmbed2D to specify that plane should remain a
// specified fixed size (using )
FixedSize = false
)
// NewEmbed2D adds a new embedded 2D viewport of given name and nominal size
// according to the standard 96 dpi resolution (i.e., actual size is adjusted relative
// to that using window's current Logical DPI scaling). If fitContent is true and
// first and only element in Viewport is a core.Layout, then it will be resized
// to fit content size (though no smaller than given size).
func NewEmbed2D(sc *Scene, parent tree.Node, name string, width, height int, fitContent bool) *Embed2D {
em := parent.NewChild(TypeEmbed2D, name).(*Embed2D)
em.Defaults(sc)
em.StdSize = image.Point{width, height}
em.Viewport = NewEmbedViewport(sc, em, name, width, height)
em.Viewport.Fill = true
em.FitContent = fitContent
return em
}
func (em *Embed2D) Defaults(sc *Scene) {
tm := sc.PlaneMesh2D()
em.SetMesh(sc, tm)
em.Solid.Defaults()
em.Zoom = 1
em.Mat.Bright = 2 // this is key for making e.g., a white background show up as white..
}
// ResizeToFit resizes viewport and texture to fit the content
func (em *Embed2D) ResizeToFit() error {
initSz := em.Viewport.Scene.Viewport.LayState.Alloc.Size.ToPoint()
vpsz := em.Viewport.PrefSize(initSz)
vpsz.X = ints.MaxInt(em.DPISize.X, vpsz.X)
vpsz.Y = ints.MaxInt(em.DPISize.Y, vpsz.Y)
em.Viewport.Resize(vpsz)
em.Viewport.FullRender2DTree()
em.UploadViewTex(em.Viewport.Scene)
return nil
}
// Resize resizes viewport and texture to given standardized 96 DPI size,
// which becomes the specified new size.
func (em *Embed2D) Resize(width, height int) {
em.StdSize = image.Point{width, height}
em.SetDPISize()
em.Viewport.Resize(em.DPISize)
em.Viewport.FullRender2DTree()
em.UploadViewTex(em.Viewport.Scene)
}
// SetDPISize sets the DPI-adjusted size using LogicalDPI from window.
// Window must be non-nil. Als
func (em *Embed2D) SetDPISize() {
if em.Viewport.Win == nil {
return
}
ldpi := em.Viewport.Win.LogicalDPI()
scr := ldpi / 96.0
em.Zoom = 1.0 / scr
// fmt.Printf("init ldpi: %v scr: %v\n", ldpi, scr)
sz := em.StdSize
sz.X = int(float32(sz.X) * scr)
sz.Y = int(float32(sz.Y) * scr)
em.DPISize = sz
}
func (em *Embed2D) Config(sc *Scene) {
if sc.Win != nil && em.Viewport.Win == nil {
em.Viewport.Win = sc.Win
em.SetDPISize()
if em.FitContent {
em.ResizeToFit()
} else {
em.Viewport.Resize(em.DPISize)
em.Viewport.FullRender2DTree()
em.UploadViewTex(sc)
}
} else {
em.Viewport.FullRender2DTree()
em.UploadViewTex(sc)
}
err := em.Validate(sc)
if err != nil {
em.SetInvisible()
}
em.NodeBase.Config(sc)
}
// UploadViewTex uploads the viewport image to the texture
func (em *Embed2D) UploadViewTex(sc *Scene) {
img := em.Viewport.Pixels
var tx Texture
var err error
if em.Mat.TexPtr == nil {
txname := "__Embed2D: " + em.Nm
tx, err = sc.TextureByNameTry(txname)
if err != nil {
tx = &TextureBase{Nm: txname}
sc.AddTexture(tx)
tx.SetImage(img)
em.Mat.SetTexture(sc, tx)
} else {
fmt.Printf("xyz.Embed2D: error: texture name conflict: %s\n", txname)
em.Mat.SetTexture(sc, tx)
}
} else {
tx = em.Mat.TexPtr
tx.SetImage(img)
sc.Phong.UpdateTextureName(tx.Name())
}
}
// Validate checks that text has valid mesh and texture settings, etc
func (em *Embed2D) Validate() error {
// todo: validate more stuff here
return em.Solid.Validate(sc)
}
func (em *Embed2D) UpdateWorldMatrix(parWorld *math32.Matrix4) {
em.PoseMu.Lock()
defer em.PoseMu.Unlock()
if em.Viewport != nil {
sz := em.Viewport.Geom.Size
sc := math32.Vec3(.006 * em.Zoom * float32(sz.X), .006 * em.Zoom * float32(sz.Y), em.Pose.Scale.Z)
em.Pose.Matrix.SetTransform(em.Pose.Pos, em.Pose.Quat, sc)
} else {
em.Pose.UpdateMatrix()
}
em.Pose.UpdateWorldMatrix(parWorld)
em.SetFlag(int(WorldMatrixUpdated))
}
func (em *Embed2D) UpdateBBox2D(size math32.Vector2, sc *Scene) {
em.Solid.UpdateBBox2D(size, sc)
em.Viewport.BBoxMu.Lock()
em.BBoxMu.Lock()
em.Viewport.Geom.Pos = em.WinBBox.Min
em.Viewport.WinBBox.Min = em.WinBBox.Min
em.Viewport.WinBBox.Max = em.WinBBox.Min.Add(em.Viewport.Geom.Size)
em.BBoxMu.Unlock()
em.Viewport.BBoxMu.Unlock()
em.Viewport.FuncDownMeFirst(0, em.Viewport.This(), func(k tree.Node, level int, d any) bool {
if k == em.Viewport.This() {
return tree.Continue
}
_, ni := core.KiToNode2D(k)
if ni == nil {
return tree.Break // going into a different type of thing, bail
}
if ni.IsUpdating() {
return tree.Break
}
ni.SetWinBBox()
return tree.Continue
})
}
func (em *Embed2D) RenderClass() RenderClasses {
return RClassOpaqueTexture
}
var Embed2DProperties = tree.Properties{
tree.EnumTypeFlag: core.TypeNodeFlags,
}
func (em *Embed2D) Project2D(sc *Scene, pt image.Point) (image.Point, bool) {
var ppt image.Point
if em.Viewport == nil || em.Viewport.Pixels == nil {
return ppt, false
}
if em.IsUpdating() {
return ppt, false
}
em.Viewport.BBoxMu.RLock()
em.BBoxMu.RLock()
defer func() {
em.BBoxMu.RUnlock()
em.Viewport.BBoxMu.RUnlock()
}()
sz := em.Viewport.Geom.Size
relpos := pt.Sub(sc.ObjBBox.Min)
ray := em.RayPick(relpos, sc)
// is in XY plane with norm pointing up in Z axis
plane := math32.Plane{Norm: math32.Vec3(0, 0, 1), Off: 0}
ispt, ok := ray.IntersectPlane(plane)
if !ok || ispt.Z > 1.0e-5 { // Z > 0 means clicked "in front" of plane -- with tolerance
fmt.Printf("in front: ok: %v ispt: %v\n", ok, ispt)
return ppt, false
}
ppt.X = int((ispt.X + 0.5) * float32(sz.X))
ppt.Y = int((ispt.Y + 0.5) * float32(sz.Y))
if ppt.X < 0 || ppt.Y < 0 {
return ppt, false
}
ppt.Y = sz.Y - ppt.Y // top-zero
// fmt.Printf("ppt: %v\n", ppt)
ppt = ppt.Add(em.Viewport.Geom.Pos)
return ppt, true
}
func (em *Embed2D) ConnectEvents3D(sc *Scene) {
em.ConnectEvent(sc.Win, system.MouseEvent, core.RegPri, func(recv, send tree.Node, sig int64, d any) {
emm := recv.Embed(TypeEmbed2D).(*Embed2D)
ssc := emm.Viewport.Scene
if !ssc.IsVisible() {
return
}
cpop := ssc.Win.CurPopup()
if cpop != nil && !ssc.Win.CurPopupIsTooltip() {
return // let window handle popups
}
me := d.(*mouse.Event)
ppt, ok := emm.Project2D(ssc, me.Where)
if !ok {
return
}
if !ssc.HasFocus2D() {
ssc.GrabFocus()
}
md := &mouse.Event{}
*md = *me
md.Where = ppt
emm.Viewport.EventMgr.MouseEvents(md)
emm.Viewport.EventMgr.SendEventSignal(md, false)
emm.Viewport.EventMgr.MouseEventReset(md)
if !md.IsProcessed() {
ni := em.This().(Node)
if ssc.CurSel != ni {
ssc.SetSel(ni)
}
}
me.SetProcessed() // must always
})
em.ConnectEvent(sc.Win, system.MouseMoveEvent, core.RegPri, func(recv, send tree.Node, sig int64, d any) {
emm := recv.Embed(TypeEmbed2D).(*Embed2D)
ssc := emm.Viewport.Scene
if !ssc.IsVisible() {
return
}
cpop := ssc.Win.CurPopup()
if cpop != nil && !ssc.Win.CurPopupIsTooltip() {
return // let window handle popups
}
me := d.(*mouse.MoveEvent)
ppt, ok := emm.Project2D(ssc, me.Where)
if !ok {
return
}
md := &mouse.MoveEvent{}
*md = *me
del := ppt.Sub(me.Where)
md.Where = ppt
md.From = md.From.Add(del)
emm.Viewport.EventMgr.MouseEvents(md)
emm.Viewport.EventMgr.SendEventSignal(md, false)
emm.Viewport.EventMgr.GenMouseFocusEvents(md, false)
emm.Viewport.EventMgr.MouseEventReset(md)
me.SetProcessed() // must always
})
em.ConnectEvent(sc.Win, system.MouseDragEvent, core.RegPri, func(recv, send tree.Node, sig int64, d any) {
emm := recv.Embed(TypeEmbed2D).(*Embed2D)
ssc := emm.Viewport.Scene
if !ssc.IsVisible() {
return
}
cpop := ssc.Win.CurPopup()
if cpop != nil && !ssc.Win.CurPopupIsTooltip() {
return // let window handle popups
}
me := d.(*mouse.DragEvent)
ppt, ok := emm.Project2D(ssc, me.Where)
if !ok {
return
}
md := &mouse.DragEvent{}
*md = *me
del := ppt.Sub(me.Where)
md.Where = ppt
md.From = md.From.Add(del)
emm.Viewport.EventMgr.MouseEvents(md)
emm.Viewport.EventMgr.SendEventSignal(md, false)
emm.Viewport.EventMgr.MouseEventReset(md)
me.SetProcessed() // must always
})
em.ConnectEvent(sc.Win, system.KeyChordEvent, core.HiPri, func(recv, send tree.Node, sig int64, d any) {
// note: restylesering HiPri -- we are outside 2D focus system, and get *all* keyboard events
emm := recv.Embed(TypeEmbed2D).(*Embed2D)
ssc := emm.Viewport.Scene
if !ssc.IsVisible() || !ssc.HasFocus2D() {
return
}
cpop := ssc.Win.CurPopup()
if cpop != nil && !ssc.Win.CurPopupIsTooltip() {
return // let window handle popups
}
kt := d.(*key.ChordEvent)
// fmt.Printf("key event: %v\n", kt.String())
emm.Viewport.EventMgr.MouseEvents(kt) // also handles key..
emm.Viewport.EventMgr.SendEventSignal(kt, false)
if !kt.IsProcessed() {
emm.Viewport.EventMgr.ManagerKeyChordEvents(kt)
}
emm.Viewport.EventMgr.MouseEventReset(kt)
})
}
///////////////////////////////////////////////////////////////////
// EmbedViewport
// EmbedViewport is an embedded viewport with its own event manager to handle
// events instead of using the Window.
type EmbedViewport struct {
core.Viewport2D
// event manager that handles dispersing events to nodes
EventMgr core.EventMgr `json:"-" xml:"-"`
// parent scene -- trigger updates
Scene *Scene `json:"-" xml:"-"`
// parent Embed2D -- render updates
EmbedPar *Embed2D `json:"-" xml:"-"`
// update flag for top-level updates
TopUpdated bool `json:"-" xml:"-"`
}
// NewEmbedViewport creates a new Pixels Image with the specified width and height,
// and initializes the renderer etc
func NewEmbedViewport(sc *Scene, em *Embed2D, name string, width, height int) *EmbedViewport {
sz := image.Point{width, height}
vp := &EmbedViewport{}
vp.Geom = core.Geom2DInt{Size: sz}
vp.Pixels = image.NewRGBA(image.Rectangle{Max: sz})
vp.Render.Init(width, height, vp.Pixels)
vp.InitName(vp, name)
vp.Scene = sc
vp.EmbedPar = em
vp.Win = vp.Scene.Win
vp.EventMgr.Master = vp
return vp
}
func (vp *EmbedViewport) VpTop() core.Viewport {
return vp.This().(core.Viewport)
}
func (vp *EmbedViewport) VpTopNode() core.Node {
return vp.This().(core.Node)
}
func (vp *EmbedViewport) VpTopUpdateStart() bool {
if vp.TopUpdated {
return false
}
vp.TopUpdated = true
return true
}
func (vp *EmbedViewport) VpTopUpdateEnd(updt bool) {
if !updt {
return
}
vp.VpUploadAll()
vp.TopUpdated = false
}
func (vp *EmbedViewport) VpEventMgr() *core.EventMgr {
return &vp.EventMgr
}
func (vp *EmbedViewport) VpIsVisible() bool {
if vp.Scene == nil || vp.EmbedPar == nil {
return false
}
return vp.Scene.IsVisible()
}
func (vp *EmbedViewport) VpUploadAll() {
if !vp.This().(core.Viewport).VpIsVisible() {
return
}
// fmt.Printf("embed vp upload all\n")
updt := vp.Scene.UpdateStart()
if updt {
ssc := vp.EmbedPar.Viewport.Scene
if !ssc.IsRendering() {
vp.EmbedPar.UploadViewTex(vp.Scene)
}
}
vp.Scene.UpdateEnd(updt)
}
// VpUploadVp uploads our viewport image into the parent window -- e.g., called
// by popups when updating separately
func (vp *EmbedViewport) VpUploadVp() {
vp.VpUploadAll()
}
// VpUploadRegion uploads node region of our viewport image
func (vp *EmbedViewport) VpUploadRegion(vpBBox, winBBox image.Rectangle) {
vp.VpUploadAll()
}
///////////////////////////////////////
// EventMaster API
func (vp *EmbedViewport) EventTopNode() tree.Node {
return vp
}
func (vp *EmbedViewport) FocusTopNode() tree.Node {
return vp
}
func (vp *EmbedViewport) EventTopUpdateStart() bool {
return vp.VpTopUpdateStart()
}
func (vp *EmbedViewport) EventTopUpdateEnd(updt bool) {
vp.VpTopUpdateEnd(updt)
}
// IsInScope returns whether given node is in scope for receiving events
func (vp *EmbedViewport) IsInScope(node tree.Node, popup bool) bool {
return true // no popups for embedded
}
func (vp *EmbedViewport) CurPopupIsTooltip() bool {
return false
}
// DeleteTooltip deletes any tooltip popup (called when hover ends)
func (vp *EmbedViewport) DeleteTooltip() {
}
// IsFocusActive returns true if focus is active in this master
func (vp *EmbedViewport) IsFocusActive() bool {
return true
}
// SetFocusActiveState sets focus active state
func (vp *EmbedViewport) SetFocusActiveState(active bool) {
}
*/