-
Notifications
You must be signed in to change notification settings - Fork 0
/
gieditor.go
380 lines (345 loc) · 9.8 KB
/
gieditor.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
// Copyright (c) 2018, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package giv
import (
"fmt"
"github.com/goki/gi/gi"
"github.com/goki/gi/oswin"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
"github.com/goki/mat32"
)
// GiEditor represents a struct, creating a property editor of the fields --
// constructs Children widgets to show the field names and editor fields for
// each field, within an overall frame with an optional title, and a button
// box at the bottom where methods can be invoked
type GiEditor struct {
gi.Frame
KiRoot ki.Ki `desc:"root of tree being edited"`
Changed bool `desc:"has the root changed via gui actions? updated from treeview and structview for changes"`
Filename gi.FileName `desc:"current filename for saving / loading"`
}
var KiT_GiEditor = kit.Types.AddType(&GiEditor{}, GiEditorProps)
// AddNewGiEditor adds a new gieditor to given parent node, with given name.
func AddNewGiEditor(parent ki.Ki, name string) *GiEditor {
return parent.AddNewChild(KiT_GiEditor, name).(*GiEditor)
}
// Update updates the objects being edited (e.g., updating display changes)
func (ge *GiEditor) Update() {
if ge.KiRoot == nil {
return
}
ge.KiRoot.UpdateSig()
}
// Save saves tree to current filename, in a standard JSON-formatted file
func (ge *GiEditor) Save() {
if ge.KiRoot == nil {
return
}
if ge.Filename == "" {
return
}
ge.KiRoot.SaveJSON(string(ge.Filename))
ge.Changed = false
}
// SaveAs saves tree to given filename, in a standard JSON-formatted file
func (ge *GiEditor) SaveAs(filename gi.FileName) {
if ge.KiRoot == nil {
return
}
ge.KiRoot.SaveJSON(string(filename))
ge.Changed = false
ge.Filename = filename
ge.UpdateSig() // notify our editor
}
// Open opens tree from given filename, in a standard JSON-formatted file
func (ge *GiEditor) Open(filename gi.FileName) {
if ge.KiRoot == nil {
return
}
ge.KiRoot.OpenJSON(string(filename))
ge.Filename = filename
ge.SetFullReRender()
ge.UpdateSig() // notify our editor
}
// SetRoot sets the source root and ensures everything is configured
func (ge *GiEditor) SetRoot(root ki.Ki) {
updt := false
if ge.KiRoot != root {
updt = ge.UpdateStart()
ge.KiRoot = root
// ge.GetAllUpdates(root)
}
ge.Config()
ge.UpdateEnd(updt)
}
// // GetAllUpdates connects to all nodes in the tree to receive notification of changes
// func (ge *GiEditor) GetAllUpdates(root ki.Ki) {
// ge.KiRoot.FuncDownMeFirst(0, ge, func(k ki.Ki, level int, d interface{}) bool {
// k.NodeSignal().Connect(ge.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
// gee := recv.Embed(KiT_GiEditor).(*GiEditor)
// if !gee.Changed {
// fmt.Printf("GiEditor: Tree changed with signal: %v\n", ki.NodeSignals(sig))
// gee.Changed = true
// }
// })
// return true
// })
// }
// Config configures the widget
func (ge *GiEditor) Config() {
if ge.KiRoot == nil {
return
}
ge.Lay = gi.LayoutVert
ge.SetProp("spacing", gi.StdDialogVSpaceUnits)
config := kit.TypeAndNameList{}
config.Add(gi.KiT_Label, "title")
config.Add(gi.KiT_ToolBar, "toolbar")
config.Add(gi.KiT_SplitView, "splitview")
mods, updt := ge.ConfigChildren(config, ki.UniqueNames)
ge.SetTitle(fmt.Sprintf("GoGi Editor of Ki Node Tree: %v", ge.KiRoot.Name()))
ge.ConfigSplitView()
ge.ConfigToolbar()
if mods {
ge.UpdateEnd(updt)
}
return
}
// SetTitle sets the optional title and updates the Title label
func (ge *GiEditor) SetTitle(title string) {
lab := ge.TitleWidget()
lab.Text = title
}
// Title returns the title label widget, and its index, within frame
func (ge *GiEditor) TitleWidget() *gi.Label {
return ge.ChildByName("title", 0).(*gi.Label)
}
// SplitView returns the main SplitView
func (ge *GiEditor) SplitView() *gi.SplitView {
return ge.ChildByName("splitview", 2).(*gi.SplitView)
}
// TreeView returns the main TreeView
func (ge *GiEditor) TreeView() *TreeView {
return ge.SplitView().Child(0).Child(0).(*TreeView)
}
// StructView returns the main StructView
func (ge *GiEditor) StructView() *StructView {
return ge.SplitView().Child(1).(*StructView)
}
// ToolBar returns the toolbar widget
func (ge *GiEditor) ToolBar() *gi.ToolBar {
return ge.ChildByName("toolbar", 1).(*gi.ToolBar)
}
// ConfigToolbar adds a GiEditor toolbar.
func (ge *GiEditor) ConfigToolbar() {
tb := ge.ToolBar()
if tb != nil && tb.HasChildren() {
return
}
tb.SetStretchMaxWidth()
ToolBarView(ge, ge.Viewport, tb)
}
// ConfigSplitView configures the SplitView.
func (ge *GiEditor) ConfigSplitView() {
if ge.KiRoot == nil {
return
}
split := ge.SplitView()
// split.Dim = mat32.Y
split.Dim = mat32.X
if len(split.Kids) == 0 {
tvfr := gi.AddNewFrame(split, "tvfr", gi.LayoutHoriz)
tv := AddNewTreeView(tvfr, "tv")
sv := AddNewStructView(split, "sv")
tv.TreeViewSig.Connect(ge.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if data == nil {
return
}
gee, _ := recv.Embed(KiT_GiEditor).(*GiEditor)
svr := gee.StructView()
tvn, _ := data.(ki.Ki).Embed(KiT_TreeView).(*TreeView)
if sig == int64(TreeViewSelected) {
svr.SetStruct(tvn.SrcNode)
} else if sig == int64(TreeViewChanged) {
gee.SetChanged()
}
})
sv.ViewSig.Connect(ge.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
gee, _ := recv.Embed(KiT_GiEditor).(*GiEditor)
gee.SetChanged()
})
split.SetSplits(.3, .7)
}
tv := ge.TreeView()
tv.SetRootNode(ge.KiRoot)
sv := ge.StructView()
sv.SetStruct(ge.KiRoot)
}
func (ge *GiEditor) SetChanged() {
ge.Changed = true
ge.ToolBar().UpdateActions() // nil safe
}
func (ge *GiEditor) Render2D() {
ge.ToolBar().UpdateActions()
if win := ge.ParentWindow(); win != nil {
if !win.IsResizing() {
win.MainMenuUpdateActives()
}
}
ge.Frame.Render2D()
}
var GiEditorProps = ki.Props{
"EnumType:Flag": gi.KiT_NodeFlags,
"background-color": &gi.Prefs.Colors.Background,
"color": &gi.Prefs.Colors.Font,
"max-width": -1,
"max-height": -1,
"#title": ki.Props{
"max-width": -1,
"horizontal-align": gi.AlignCenter,
"vertical-align": gi.AlignTop,
},
"ToolBar": ki.PropSlice{
{"Update", ki.Props{
"icon": "update",
"updtfunc": ActionUpdateFunc(func(gei interface{}, act *gi.Action) {
ge := gei.(*GiEditor)
act.SetActiveStateUpdt(ge.Changed)
}),
}},
{"sep-file", ki.BlankProp{}},
{"Open", ki.Props{
"label": "Open",
"icon": "file-open",
"desc": "Open a json-formatted Ki tree structure",
"Args": ki.PropSlice{
{"File Name", ki.Props{
"default-field": "Filename",
"ext": ".json",
}},
},
}},
{"Save", ki.Props{
"icon": "file-save",
"desc": "Save json-formatted Ki tree structure to existing filename",
"updtfunc": ActionUpdateFunc(func(gei interface{}, act *gi.Action) {
ge := gei.(*GiEditor)
act.SetActiveStateUpdt(ge.Changed && ge.Filename != "")
}),
}},
{"SaveAs", ki.Props{
"label": "Save As...",
"icon": "file-save",
"desc": "Save as a json-formatted Ki tree structure",
"Args": ki.PropSlice{
{"File Name", ki.Props{
"default-field": "Filename",
"ext": ".json",
}},
},
}},
},
"MainMenu": ki.PropSlice{
{"AppMenu", ki.BlankProp{}},
{"File", ki.PropSlice{
{"Update", ki.Props{
"updtfunc": ActionUpdateFunc(func(gei interface{}, act *gi.Action) {
ge := gei.(*GiEditor)
act.SetActiveState(ge.Changed)
}),
}},
{"sep-file", ki.BlankProp{}},
{"Open", ki.Props{
"shortcut": gi.KeyFunMenuOpen,
"desc": "Open a json-formatted Ki tree structure",
"Args": ki.PropSlice{
{"File Name", ki.Props{
"default-field": "Filename",
"ext": ".json",
}},
},
}},
{"Save", ki.Props{
"shortcut": gi.KeyFunMenuSave,
"desc": "Save json-formatted Ki tree structure to existing filename",
"updtfunc": ActionUpdateFunc(func(gei interface{}, act *gi.Action) {
ge := gei.(*GiEditor)
act.SetActiveState(ge.Changed && ge.Filename != "")
}),
}},
{"SaveAs", ki.Props{
"shortcut": gi.KeyFunMenuSaveAs,
"label": "Save As...",
"desc": "Save as a json-formatted Ki tree structure",
"Args": ki.PropSlice{
{"File Name", ki.Props{
"default-field": "Filename",
"ext": ".json",
}},
},
}},
{"sep-close", ki.BlankProp{}},
{"Close Window", ki.BlankProp{}},
}},
{"Edit", "Copy Cut Paste Dupe"},
{"Window", "Windows"},
},
}
// GoGiEditorDialog opens an interactive editor of the given Ki tree, at its
// root, returns GiEditor and window
func GoGiEditorDialog(obj ki.Ki) *GiEditor {
width := 1280
height := 920
wnm := "gogi-editor"
wti := "GoGi Editor"
if obj != nil {
wnm += "-" + obj.Name()
wti += ": " + obj.Name()
}
win, recyc := gi.RecycleMainWindow(obj, wnm, wti, width, height)
if recyc {
mfr, err := win.MainFrame()
if err == nil {
return mfr.Child(0).(*GiEditor)
}
}
vp := win.WinViewport2D()
updt := vp.UpdateStart()
mfr := win.SetMainFrame()
mfr.Lay = gi.LayoutVert
ge := AddNewGiEditor(mfr, "editor")
ge.Viewport = vp
ge.SetRoot(obj)
mmen := win.MainMenu
MainMenuView(ge, win, mmen)
tb := ge.ToolBar()
tb.UpdateActions()
inClosePrompt := false
win.OSWin.SetCloseReqFunc(func(w oswin.Window) {
if !ge.Changed {
win.Close()
return
}
if inClosePrompt {
return
}
inClosePrompt = true
gi.ChoiceDialog(vp, gi.DlgOpts{Title: "Close Without Saving?",
Prompt: "Do you want to save your changes? If so, Cancel and then Save"},
[]string{"Close Without Saving", "Cancel"},
win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
switch sig {
case 0:
win.Close()
case 1:
// default is to do nothing, i.e., cancel
inClosePrompt = false
}
})
})
vp.UpdateEndNoSig(updt)
win.GoStartEventLoop() // in a separate goroutine
return ge
}