-
Notifications
You must be signed in to change notification settings - Fork 71
/
values.go
160 lines (140 loc) · 3.61 KB
/
values.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
// 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
import (
"log/slog"
"reflect"
"sort"
"cogentcore.org/core/base/reflectx"
"cogentcore.org/core/core"
"cogentcore.org/core/icons"
"cogentcore.org/core/tree"
"cogentcore.org/core/views"
"cogentcore.org/core/xyz"
)
func init() {
views.AddValue(xyz.MeshName(""), func() views.Value { return &MeshValue{} })
}
// MeshValue represents an [xyz.MeshName] with a button.
type MeshValue struct {
views.ValueBase[*core.Button]
}
func (v *MeshValue) Config() {
v.Widget.SetType(core.ButtonTonal).SetIcon(icons.DeployedCode)
views.ConfigDialogWidget(v, false)
}
func (v *MeshValue) Update() {
txt := reflectx.ToString(v.Value.Interface())
if txt == "" {
txt = "(none, click to select)"
}
v.Widget.SetText(txt).Update()
}
func (v *MeshValue) ConfigDialog(d *core.Body) (bool, func()) {
d.SetTitle("Select a mesh")
if v.OwnKind != reflect.Struct {
return false, nil
}
ndi, ok := v.Owner.(xyz.Node)
if !ok {
return false, nil
}
sci := ndi.ParentByType(xyz.SceneType, tree.Embeds)
if sci == nil {
slog.Error("missing parent scene for node", "node", ndi)
return false, nil
}
sc := sci.(*xyz.Scene)
sl := sc.MeshList()
sort.Strings(sl)
si := 0
cur := reflectx.ToString(v.Value.Interface())
views.NewSliceView(d).SetSlice(&sl).SetSelectedValue(cur).BindSelect(&si)
return true, func() {
if si >= 0 {
ms := sl[si]
v.SetValue(ms)
v.Update()
}
}
}
/*
TODO: This doesn't work because texture is on Material which doesn't have a pointer to the Scene!
// Value restylesers TexValue as the viewer of TexName
func (mn TexName) Value() views.Value {
vv := TexValue{}
vv.Init(&vv)
return &vv
}
// TexValue presents an action for displaying a TexName and selecting
// textures from a ChooserDialog
type TexValue struct {
views.ValueBase
}
func (vv *TexValue) WidgetType() reflect.Type {
vv.WidgetTyp = core.TypeAction
return vv.WidgetTyp
}
func (vv *TexValue) UpdateWidget() {
if vv.Widget == nil {
return
}
ac := vv.Widget.(*core.Button)
txt := reflectx.ToString(vv.Value.Interface())
if txt == "" {
txt = "(none, click to select)"
}
ac.SetText(txt)
}
func (vv *TexValue) Config(widg core.Node2D) {
vv.Widget = widg
ac := vv.Widget.(*core.Button)
ac.SetProp("border-radius", units.NewPx(4))
ac.ActionSig.ConnectOnly(vv.This(), func(recv, send tree.Node, sig int64, data any) {
vvv, _ := recv.Embed(TypeTexValue).(*TexValue)
ac := vvv.Widget.(*core.Button)
vvv.Activate(ac.ViewportSafe(), nil, nil)
})
vv.UpdateWidget()
}
func (vv *TexValue) HasAction() bool {
return true
}
func (vv *TexValue) Activate(vp *core.Viewport2D, dlgRecv tree.Node, dlgFunc tree.RecvFunc) {
if vv.IsInactive() {
return
}
if vv.OwnKind != reflect.Struct {
return
}
mati, ok := vv.Owner.(*Material)
if !ok {
return
}
sci, err := ndi.ParentByTypeTry(TypeScene, tree.Embeds)
if err != nil {
log.Println(err)
return
}
sc := sci.Embed(TypeScene).(*Scene)
sl := sc.TextureList()
sort.Strings(sl)
cur := reflectx.ToString(vv.Value.Interface())
desc, _ := vv.Tag("desc")
views.SliceViewSelectDialog(vp, &sl, cur, views.DlgOpts{Title: "Select a Texture", Prompt: desc}, nil,
vv.This(), func(recv, send tree.Node, sig int64, data any) {
if sig == int64(core.DialogAccepted) {
ddlg := send.Embed(core.TypeDialog).(*core.Dialog)
si := views.SliceViewSelectDialogValue(ddlg)
if si >= 0 {
vv.SetValue(sl[si])
vv.UpdateWidget()
}
}
if dlgRecv != nil && dlgFunc != nil {
dlgFunc(dlgRecv, send, sig, data)
}
})
}
*/