/
world_ed.go
266 lines (238 loc) · 6.57 KB
/
world_ed.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
package editors
import (
"fmt"
"image/color"
"reflect"
"strings"
"github.com/bradbev/flatland/src/asset"
"github.com/bradbev/flatland/src/editor"
"github.com/bradbev/flatland/src/editor/edgui"
"github.com/bradbev/flatland/src/flat"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/inkyblackness/imgui-go/v4"
"golang.org/x/exp/slices"
)
func worldEd(context *editor.TypeEditContext, value reflect.Value) error {
world := value.Addr().Interface().(*flat.World)
c, firstTime := editor.GetContext[worldEdContext](context, value)
_ = c
if firstTime {
c.world = world
c.buildWorldTree()
c.addDialog = &addDialog{Title: "Add Actor##uniqueID"}
c.addDialog.Context = c
}
flags := imgui.TableFlagsSizingStretchSame |
imgui.TableFlagsResizable |
imgui.TableFlagsBordersOuter |
imgui.TableFlagsBordersV |
imgui.TableFlagsContextMenuInBody
if imgui.BeginTableV(context.ID("WorldEd##"), 2, flags, imgui.Vec2{}, 0) {
defer imgui.EndTable()
imgui.TableNextRow()
imgui.TableSetColumnIndex(0)
c.renderOutliner(context, value)
imgui.TableSetColumnIndex(1)
c.renderWorld(context, value)
}
return nil
}
type worldEdContext struct {
world *flat.World
root *worldTreeNode
addDialog *addDialog
valueToEdit reflect.Value
actorToEdit flat.Actor
}
func (w *worldEdContext) renderWorld(context *editor.TypeEditContext, value reflect.Value) {
width := imgui.ColumnWidth()
id, img := context.Ed.GetImguiTexture(value, width, width)
img.Fill(color.Black)
w.world.Draw(img)
for i, a := range w.world.PersistentActors {
if a == nil {
continue
}
g := ebiten.GeoM{}
flat.ApplyTransform(a.GetTransform(), &g)
x, y := g.Apply(0, 0)
s := fmt.Sprintf("%v", i)
ebitenutil.DebugPrintAt(img, s, int(x), int(y))
}
imgui.Image(id, imgui.Vec2{X: f32(width), Y: f32(width)})
}
type worldTreeHandler struct {
context *worldEdContext
root *worldTreeNode
}
func (wt *worldTreeHandler) Clicked(node edgui.TreeNode) {
n := node.(*worldTreeNode)
if n.name != "Root" {
toEdit := n.actor
edgui.WalkTree(wt.root, nil, func(node edgui.TreeNode, context any) {
if node != nil {
node.(*worldTreeNode).selected = false
}
})
n.selected = true
wt.context.actorToEdit = toEdit
wt.context.valueToEdit = reflect.ValueOf(toEdit).Elem()
}
}
func (w *worldEdContext) renderOutliner(context *editor.TypeEditContext, value reflect.Value) {
imgui.Text("Outliner")
if imgui.Button("Add") {
w.addDialog.Open()
}
if w.addDialog.Draw() {
w.buildWorldTree()
context.SetChanged()
}
edgui.WithDisabled(w.actorToEdit == nil, func() {
imgui.SameLine()
if imgui.Button("Remove") {
w.world.PersistentActors = slices.DeleteFunc(w.world.PersistentActors, func(a flat.Actor) bool {
return a == w.actorToEdit
})
w.world.RemoveFromWorld(w.actorToEdit)
w.actorToEdit = nil
w.valueToEdit = reflect.Value{}
w.buildWorldTree()
context.SetChanged()
}
})
edgui.DrawTree(w.root, &worldTreeHandler{
context: w,
root: w.root,
})
imgui.Separator()
if w.valueToEdit.IsValid() {
w.actorInlineEd(context, w.valueToEdit)
}
}
func (w *worldEdContext) actorInlineEd(context *editor.TypeEditContext, value reflect.Value) {
flat.WalkComponents(w.actorToEdit, func(target, parent flat.Component) {
if target == nil {
return
}
editor.StructEd(context, reflect.ValueOf(target).Elem())
imgui.Separator()
})
}
func (w *worldEdContext) buildWorldTree() {
w.root = &worldTreeNode{
name: "Root",
}
for _, actor := range w.world.PersistentActors {
nodeName := ""
if name, _ := asset.GetLoadPathForAsset(actor); name != "" {
nodeName = string(name)
}
if nodeName == "" {
if parentPath := asset.GetParent(actor); parentPath != "" {
nodeName = string(parentPath)
}
}
if nodeName == "" {
nodeName, _ = asset.ObjectTypeName(actor)
}
w.root.children = append(w.root.children,
&worldTreeNode{
name: nodeName,
actor: actor,
})
}
}
type worldTreeNode struct {
name string
children []edgui.TreeNode
selected bool
actor flat.Actor
}
var _ edgui.TreeNode = (*worldTreeNode)(nil)
func (c *worldTreeNode) Name() string {
return c.name
}
func (c *worldTreeNode) Children() []edgui.TreeNode { return c.children }
func (c *worldTreeNode) Leaf() bool { return len(c.children) == 0 }
func (c *worldTreeNode) Expanded() bool { return true }
func (c *worldTreeNode) Selected() bool { return c.selected }
type addDialog struct {
Title string
Context *worldEdContext
open bool
selectedItem *addAssetItem
list edgui.FilteredList[*addAssetItem]
wasDoubleClicked bool
}
func (a *addDialog) Clicked(node edgui.ListNode, index int) {
assetItem := node.(*addAssetItem)
if assetItem.selected {
assetItem.selected = false
a.selectedItem = nil
return
}
for _, item := range a.list.List {
item.selected = false
}
assetItem.selected = true
a.selectedItem = assetItem
}
func (a *addDialog) DoubleClicked(node edgui.ListNode, index int) {
assetItem := node.(*addAssetItem)
a.selectedItem = assetItem
a.wasDoubleClicked = true
}
type addAssetItem struct {
assetPath string
selected bool
}
func (a *addAssetItem) Name() string {
return a.assetPath
}
func (a *addAssetItem) Selected() bool {
return a.selected
}
func (a *addDialog) Open() {
a.open = true
a.wasDoubleClicked = false
imgui.OpenPopup(a.Title)
var items []*addAssetItem
paths, _ := asset.FilterFilesByReflectType(reflect.TypeOf(new(flat.Actor)))
for _, p := range paths {
items = append(items, &addAssetItem{
assetPath: p,
})
}
slices.SortFunc(items, func(a, b *addAssetItem) int {
return strings.Compare(a.Name(), b.Name())
})
a.list = edgui.FilteredList[*addAssetItem]{List: items}
}
func (a *addDialog) Draw() bool {
result := false
if imgui.BeginPopupModalV(a.Title, &a.open, imgui.WindowFlagsNone) {
defer imgui.EndPopup()
edgui.WithDisabled(a.selectedItem == nil, func() {
if imgui.Button("Add") || a.wasDoubleClicked {
var actorToAdd flat.Actor
// an existing actor was selected. Load it and set it as the parent
parent, err := asset.Load(asset.Path(a.selectedItem.assetPath))
flat.Check(err)
instance, err := asset.NewInstance(parent)
flat.Check(err)
asset.SetParent(instance, parent)
actorToAdd = instance.(flat.Actor)
// Add the new actor to the world
world := a.Context.world
world.AddToWorld(actorToAdd)
world.PersistentActors = append(world.PersistentActors, actorToAdd)
imgui.CloseCurrentPopup()
result = true
}
})
a.list.Draw("AddActorList", a)
}
return result
}