-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetraterm.go
1417 lines (1050 loc) · 33.7 KB
/
tetraterm.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package tetraterm
// TetraTerm is a terminal-based debugging solution for Tetra3D games and applications.
import (
"context"
"fmt"
"log"
"math/rand"
"regexp"
"sort"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/gdamore/tcell/v2"
"github.com/hajimehoshi/ebiten/v2"
p2p "github.com/leprosus/golang-p2p"
"github.com/rivo/tview"
"github.com/solarlune/tetra3d"
"github.com/solarlune/tetra3d/colors"
)
type ogLocalTransform struct {
Node tetra3d.INode
Parent tetra3d.INode
Position tetra3d.Vector
Scale tetra3d.Vector
Rotation tetra3d.Matrix4
}
func newOGTransform(node tetra3d.INode) ogLocalTransform {
return ogLocalTransform{
Node: node,
Parent: node.Parent(),
Position: node.LocalPosition(),
Scale: node.LocalScale(),
Rotation: node.LocalRotation(),
}
}
func (transform ogLocalTransform) Apply(parentingAsWell bool) {
if parentingAsWell && transform.Parent != transform.Node.Parent() {
transform.Node.Unparent()
if transform.Parent != nil {
transform.Parent.AddChildren(transform.Node)
}
}
transform.Node.SetLocalPositionVec(transform.Position)
transform.Node.SetLocalScaleVec(transform.Scale)
transform.Node.SetLocalRotation(transform.Rotation)
}
type emptyLogger struct{}
func (el emptyLogger) Info(msg string) {}
func (el emptyLogger) Warn(msg string) {}
func (el emptyLogger) Error(msg string) {}
type sceneNode struct {
Name string
NodeID uint64
Children []sceneNode
// Extra Stuff
parent *sceneNode
}
func (st sceneNode) ChildrenRecursive() []sceneNode {
out := []sceneNode{st}
for _, child := range st.Children {
out = append(out, child.ChildrenRecursive()...)
}
return out
}
func (st *sceneNode) Count() int {
count := 1
for _, child := range st.Children {
count += child.Count()
}
return count
}
func (st *sceneNode) ResetParenting() {
for c := range st.Children {
st.Children[c].parent = st
st.Children[c].ResetParenting()
}
}
func constructNodeTree(t3dNode tetra3d.INode) sceneNode {
sceneNode := sceneNode{
Name: t3dNode.Name(),
NodeID: t3dNode.ID(),
Children: []sceneNode{},
}
for _, child := range t3dNode.Children() {
newNode := constructNodeTree(child)
sceneNode.Children = append(sceneNode.Children, newNode)
}
return sceneNode
}
// ConnectionSettings represents the set of options to use for both the terminal display client as well
// as your server in your game. The host and port settings should match for the connection to take.
type ConnectionSettings struct {
Host string // What host name to use for connecting from the client to the server
Port string // What port to use for the connection
// Whether logging should be silent (default) or not; if not silent, the terminal client will spam messages
// indicating the messages received from the server, and vice-versa
SilentLogging bool
}
// NewDefaultConnectionSettings returns a new ConnectionSettings object filled out with the default connection
// setting values.
func NewDefaultConnectionSettings() *ConnectionSettings {
return &ConnectionSettings{
Host: "", // "" is local host
Port: "7979",
SilentLogging: true,
}
}
// Server represents the endpoint of your game that the debugging Terminal hooks into.
// Note that this handles and records various settings to enable debugging simply and easily,
// so it's best to not instantiate a Server when shipping a release version of your game.
type Server struct {
P2PServer *p2p.Server
activeLibrary *tetra3d.Library
activeScene *tetra3d.Scene
ogTransforms map[tetra3d.INode]ogLocalTransform
prevScene *tetra3d.Scene
t3dCamera *tetra3d.Camera
selectedNode tetra3d.INode
DebugDrawHierarchy bool
DebugDrawWireframe bool
DebugDrawBounds bool
}
// NewServer returns a new server, using the connection settings provided. If you pass nil,
// that is valid for a default set of connection settings.
func NewServer(settings *ConnectionSettings) *Server {
if settings == nil {
settings = NewDefaultConnectionSettings()
}
server := &Server{}
port := p2p.NewTCP(settings.Host, settings.Port)
s, err := p2p.NewServer(port)
if err != nil {
panic(err)
}
if settings.SilentLogging {
s.SetLogger(emptyLogger{})
}
server.P2PServer = s
s.SetHandle(ptNodeFollowCamera, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
if server.t3dCamera != nil {
if server.selectedNode != server.t3dCamera {
server.selectedNode.AddChildren(server.t3dCamera)
server.t3dCamera.ClearLocalTransform()
server.t3dCamera.Move(0, 0, 10)
}
}
return
})
s.SetHandle(ptToggleDebugDrawHierarchy, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
server.DebugDrawHierarchy = !server.DebugDrawHierarchy
packet := toggleDebugDrawHierarchy{}
packet.Decode(req)
packet.DebugDrawOn = server.DebugDrawHierarchy
res = packet.Encode()
return
})
s.SetHandle(ptToggleDebugDrawWireframe, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
server.DebugDrawWireframe = !server.DebugDrawWireframe
packet := toggleDebugDrawWireframe{}
packet.Decode(req)
packet.DebugDrawOn = server.DebugDrawWireframe
res = packet.Encode()
return
})
s.SetHandle(ptToggleDebugDrawBounds, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
server.DebugDrawBounds = !server.DebugDrawBounds
packet := toggleDebugDrawBounds{}
packet.Decode(req)
packet.DebugDrawOn = server.DebugDrawBounds
res = packet.Encode()
return
})
s.SetHandle(ptNodeSelect, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
packet := &nodeSelectPacket{}
err = packet.Decode(req)
if err != nil {
panic(err)
}
if server.activeScene != nil {
for _, node := range server.activeScene.Root.SearchTree().INodes() {
if node.ID() == packet.NodeID {
server.selectedNode = node
break
}
}
}
return
})
s.SetHandle(ptNodeMove, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
if server.selectedNode != nil {
packet := &nodeMovePacket{}
packet.Decode(req)
server.selectedNode.Move(packet.X, packet.Y, packet.Z)
}
return
})
s.SetHandle(ptNodeRotate, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
if server.selectedNode != nil {
packet := &nodeRotatePacket{}
packet.Decode(req)
server.selectedNode.Rotate(packet.X, packet.Y, packet.Z, packet.Angle)
}
return
})
s.SetHandle(ptNodeReset, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
server.resetSelectedNode()
return
})
s.SetHandle(ptNodeInfo, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
if server.selectedNode != nil {
packet := &nodeInfoPacket{}
packet.ID = server.selectedNode.ID()
packet.Position = server.selectedNode.LocalPosition()
packet.Scale = server.selectedNode.LocalScale()
packet.Rotation = matrix4ToMatrix3(server.selectedNode.LocalRotation())
packet.Visible = server.selectedNode.Visible()
packet.Type = server.selectedNode.Type()
res = packet.Encode()
}
return
})
s.SetHandle(ptGameInfo, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
// We don't need to decode this packet because the client is asking for this, and has nothing to
// give us.
packet := &gameInfoPacket{
FPS: float32(ebiten.ActualFPS()),
TPS: float32(ebiten.ActualTPS()),
// ModelCount: server.activeScene.Root.ChildrenRecursive().ByType(tetra3d.NodeTypeModel).,
}
if server.t3dCamera != nil {
packet.DebugInfo = server.t3dCamera.DebugInfo
packet.SectorRendering = server.t3dCamera.SectorRendering
sector := server.t3dCamera.CurrentSector()
if sector != nil {
packet.Sector = sector.Model.Name()
packet.SectorNeighbors = []string{}
for n := range sector.NeighborsWithinRange(server.t3dCamera.SectorRenderDepth) {
packet.SectorNeighbors = append(packet.SectorNeighbors, n.Model.Name())
}
}
}
res = packet.Encode()
return
})
s.SetHandle(ptNodeCreate, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
packet := &nodeCreatePacket{}
packet.Decode(req)
if packet.NodeToCreate == "" {
if server.activeLibrary != nil {
nodeNames := []string{}
for _, s := range server.activeLibrary.Scenes {
for _, child := range s.Root.SearchTree().INodes() {
candidate := child.Name()
exists := false
for _, n := range nodeNames {
if candidate == n {
exists = true
break
}
}
if !exists {
nodeNames = append(nodeNames, candidate)
}
}
}
packet.ViableNodes = nodeNames
res = packet.Encode()
return
}
} else {
var scenesToSearch []*tetra3d.Scene
if server.activeLibrary != nil {
scenesToSearch = server.activeLibrary.Scenes
} else {
scenesToSearch = []*tetra3d.Scene{server.activeScene}
}
for _, scene := range scenesToSearch {
for _, node := range scene.Root.SearchTree().INodes() {
if node.Name() == packet.NodeToCreate {
clone := node.Clone()
server.activeScene.Root.AddChildren(clone)
packet.NewSelectedNode = clone.ID()
server.selectedNode = clone
packet.SceneTree = constructNodeTree(server.activeScene.Root)
res = packet.Encode()
return
}
}
}
}
return
})
s.SetHandle(ptNodeDuplicate, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
packet := &nodeDuplicatePacket{}
packet.Decode(req)
if server.selectedNode != server.activeScene.Root {
clone := server.selectedNode.Clone()
replaceRandomNumber, _ := regexp.Compile("<[0123456789]{4}>")
name := clone.Name()
if match := replaceRandomNumber.FindStringIndex(name); match != nil {
name = name[:match[0]]
}
clone.SetName(name + "<" + strconv.Itoa(rand.Intn(9999)) + ">")
server.selectedNode.Parent().AddChildren(clone)
server.selectedNode.Parent().ReindexChild(clone, server.selectedNode.Index()+1)
packet.NewSelectedNode = clone.ID()
server.selectedNode = clone
packet.SceneTree = constructNodeTree(server.activeScene.Root)
}
res = packet.Encode()
return
})
s.SetHandle(ptNodeDelete, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
packet := &nodeDeletePacket{}
packet.Decode(req)
if server.selectedNode != server.activeScene.Root {
ogIndex := server.selectedNode.Index()
parent := server.selectedNode.Parent()
server.selectedNode.Unparent()
var newSelection tetra3d.INode
if len(parent.Children()) > 0 {
newSelection = parent.Children()[ogIndex-1]
} else {
newSelection = parent
}
packet.NewSelectedNode = newSelection.ID()
server.selectedNode = newSelection
packet.SceneTree = constructNodeTree(server.activeScene.Root)
}
res = packet.Encode()
return
})
s.SetHandle(ptNodeMoveInTree, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
packet := &nodeMoveInTreePacket{}
packet.Decode(req)
if server.selectedNode != server.activeScene.Root {
node := server.selectedNode
switch packet.MoveDir {
case mitMoveUp:
node.Parent().ReindexChild(node, node.Index()-1)
case mitMoveDown:
node.Parent().ReindexChild(node, node.Index()+1)
case mitIndent:
if node.Index() > 0 {
node.Parent().Children()[node.Index()-1].AddChildren(node)
}
case mitDeIndent:
if node.Parent() != node.Root() {
ogParentIndex := node.Parent().Index()
node.Parent().Parent().AddChildren(node)
node.Parent().ReindexChild(node, ogParentIndex+1)
}
}
packet.NewSelectedNode = node.ID()
packet.SceneTree = constructNodeTree(server.activeScene.Root)
}
res = packet.Encode()
return
})
s.SetHandle(ptSceneRefresh, func(ctx context.Context, req p2p.Data) (res p2p.Data, err error) {
packet := &sceneRefreshPacket{}
err = packet.Decode(req)
if err != nil {
log.Println(err)
} else {
packetChanged := false
if server.activeScene != nil {
tree := constructNodeTree(server.activeScene.Root)
packet.SceneTree = tree
packetChanged = true
} else {
log.Println("warning: no scene set for server")
}
// Give the response
if packetChanged {
res = packet.Encode()
}
}
return
})
go func() {
if err := server.P2PServer.Serve(); err != nil {
panic(err)
}
}()
return server
}
// Update updates the server as necessary. The scene should be the current game scene that you wish
// to send and visualize in the terminal client. This should be called every tick.
func (server *Server) Update(scene *tetra3d.Scene) {
server.activeScene = scene
server.activeLibrary = scene.Library()
if server.selectedNode == nil {
server.selectedNode = server.activeScene.Root
}
// Scene changed, so we can empty the og transforms list.
if server.activeScene != server.prevScene {
server.ogTransforms = map[tetra3d.INode]ogLocalTransform{}
}
for _, n := range scene.Root.SearchTree().INodes() {
server.recordOGTransforms(n)
}
server.prevScene = server.activeScene
}
// Draw handles any additional drawing from the terminal, drawing to the screen using the Tetra3D
// camera provided.
func (server *Server) Draw(screen *ebiten.Image, camera *tetra3d.Camera) {
server.t3dCamera = camera
if server.DebugDrawHierarchy {
camera.DrawDebugCenters(screen, server.selectedNode, colors.White())
draw := func(node tetra3d.INode) {
if node != camera {
nodePos := node.WorldPosition()
pos := camera.WorldToScreen(nodePos)
if (!node.Visible() || camera.WorldPosition().Distance(nodePos) > camera.Far()) && node != server.selectedNode {
return
}
color := colors.Gray()
if node == server.selectedNode {
color = colors.White()
}
camera.DebugDrawText(screen, node.Name(), pos.X, pos.Y, 1, color)
}
}
for _, n := range server.activeScene.Root.SearchTree().INodes() {
if n == server.selectedNode {
continue
}
draw(n)
}
draw(server.selectedNode) // Draw the node last so its name is visible
}
if server.DebugDrawBounds {
drawSettings := tetra3d.DefaultDrawDebugBoundsSettings()
drawSettings.RenderBroadphase = false
drawSettings.RenderTrianglesAABB = false
camera.DrawDebugBoundsColored(screen, server.activeScene.Root, drawSettings)
}
if server.DebugDrawWireframe {
camera.DrawDebugWireframe(screen, server.activeScene.Root, colors.LightGray())
}
}
func (server *Server) recordOGTransforms(node tetra3d.INode) {
if _, exists := server.ogTransforms[node]; !exists {
server.ogTransforms[node] = newOGTransform(node)
}
}
func (server *Server) resetSelectedNode() {
if server.selectedNode != nil {
server.ogTransforms[server.selectedNode].Apply(true)
for _, node := range server.selectedNode.SearchTree().INodes() {
server.ogTransforms[node].Apply(false)
}
}
}
// Display represents the terminal interface that hooks into your game through a TCP connection.
// By manipulating the Display in your terminal, you can view and modify your connected game scene
// while it's running. It will also automatically reconnect if the connection is lots.
type Display struct {
Client *p2p.Client
ClientSettings *ConnectionSettings
App *tview.Application
Root *tview.Pages
running atomic.Bool
currentSceneTree sceneNode
receivingData atomic.Bool
// Flexbox *tview.Flex
TreeNodeRoot *tview.TreeNode
TreeView *tview.TreeView
TreeViewScroll *Scrollbar
NodePropertyArea *tview.TextArea
GamePropertyArea *tview.TextArea
SearchBar *tview.InputField
SearchBarCloneMode bool
SearchBarCloneModeAutocompleteNames []string
// prevSceneData map[tetra3d.INode]string
// propertyText *tview.TextArea
SelectNextNode bool
SelectNextNodeIndex uint64
SceneNodesToTreeNodes map[uint64]*tview.TreeNode
// DebugDraw bool
// deleteNode bool
// duplicateNode bool
// stopped atomic.Bool
// forceSceneRebuild bool
// Handlers
// Scene *tetra3d.Scene
// Camera *tetra3d.Camera
}
// NewDisplay returns a new Display object with the ConnectionSettings provided. If nil is passed,
// the default connection settings set is used.
func NewDisplay(settings *ConnectionSettings) *Display {
if settings == nil {
settings = NewDefaultConnectionSettings()
}
app := &Display{
ClientSettings: settings,
App: tview.NewApplication(),
running: atomic.Bool{},
SceneNodesToTreeNodes: map[uint64]*tview.TreeNode{},
// Flexbox: tview.NewFlex(),
// prevSceneData: map[tetra3d.INode]string{},
// stopped: atomic.Bool{},
// ogTransforms: map[tetra3d.INode]ogLocalTransform{},
}
app.initClient()
/////////
app.Root = tview.NewPages()
app.Root.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Rune() == '1' {
app.sendRequest(newToggleDebugDrawHierarchy())
}
if event.Rune() == '2' {
app.sendRequest(newToggleDebugDrawWireframe())
}
if event.Rune() == '3' {
app.sendRequest(newToggleDebugDrawBounds())
}
if event.Key() == tcell.KeyCtrlQ {
app.App.Stop()
}
if event.Key() == tcell.KeyCtrlR {
app.App.Sync()
return nil
}
return event
})
app.App.SetRoot(app.Root, true)
app.TreeView = tview.NewTreeView()
app.TreeView.SetBorder(true)
app.TreeView.SetTitle("[ ◆ Node Tree ]")
// Title color's set later
// app.TreeView.SetTitleColor(tcell.ColorCornflowerBlue)
app.TreeView.SetGraphicsColor(tcell.ColorGreen)
app.TreeView.SetBackgroundColor(tcell.ColorDefault)
app.TreeView.SetSelectedFunc(func(node *tview.TreeNode) {
if len(node.GetChildren()) > 0 {
node.SetExpanded(!node.IsExpanded())
}
app.updateTreeNodeNames()
})
leftSide := tview.NewFlex()
leftSide.SetDirection(tview.FlexRow)
treePartial := tview.NewFlex()
treePartial.SetDirection(tview.FlexColumn)
treePartial.AddItem(app.TreeView, 0, 2, true)
app.TreeViewScroll = NewScrollbar(app.TreeView)
treePartial.AddItem(app.TreeViewScroll, 2, 0, false)
app.Root.SetMouseCapture(func(action tview.MouseAction, event *tcell.EventMouse) (tview.MouseAction, *tcell.EventMouse) {
return app.TreeViewScroll.HandleMouseInput(action, event)
})
leftSide.AddItem(treePartial, 0, 2, true)
app.SearchBar = tview.NewInputField()
app.SearchBar.SetAutocompleteFunc(func(currentText string) (entries []string) {
if currentText == "" {
return
}
var nodeNames []string
if app.SearchBarCloneMode {
nodeNames = app.SearchBarCloneModeAutocompleteNames
} else {
for _, n := range app.currentSceneTree.ChildrenRecursive() {
nodeNames = append(nodeNames, n.Name)
}
}
for _, n := range nodeNames {
if strings.Contains(strings.ToLower(n), strings.ToLower(currentText)) {
entries = append(entries, n)
}
}
return
})
app.SearchBar.SetLabel("Search Node: ")
app.SearchBar.SetLabelColor(tcell.ColorLightBlue)
app.SearchBar.SetFieldBackgroundColor(tcell.ColorDarkSlateGray)
app.SearchBar.SetFieldTextColor(tcell.ColorLightBlue)
app.SearchBar.SetChangedFunc(func(text string) {
if text != "" {
if app.SearchBarCloneMode {
res, err := app.sendRequest(newNodeCreatePacket())
if err != nil {
log.Println(err)
} else if res.(*nodeCreatePacket).ViableNodes != nil {
app.SearchBarCloneModeAutocompleteNames = res.(*nodeCreatePacket).ViableNodes
}
} else {
// Hacky fix for highlighting an element that's hidden by being under an unexpanded parent, I think?
app.TreeView.GetRoot().ExpandAll()
for _, node := range app.currentSceneTree.ChildrenRecursive() {
if strings.Contains(strings.ToLower(node.Name), strings.ToLower(text)) {
treeNode := app.SceneNodesToTreeNodes[node.NodeID]
// treeNode.ExpandAll() // Not necessary if we expand the root above
app.TreeView.SetCurrentNode(treeNode)
app.sendRequest(newNodeSelectPacket(node.NodeID))
app.TreeViewScroll.ScrollTo(app.TreeViewScroll.ChildIndexInTree(treeNode))
break
}
}
}
}
})
app.SearchBar.SetDoneFunc(func(key tcell.Key) {
if app.SearchBarCloneMode && key == tcell.KeyEnter {
np := newNodeCreatePacket()
np.NodeToCreate = app.SearchBar.GetText()
res, err := app.sendRequest(np)
if err != nil {
log.Println(err)
} else {
app.SelectNextNode = true
app.currentSceneTree = res.(*nodeCreatePacket).SceneTree
app.SelectNextNodeIndex = res.(*nodeCreatePacket).NewSelectedNode
}
}
app.SearchBar.SetBackgroundColor(tcell.ColorBlack)
app.SearchBar.SetLabelColor(tcell.ColorLightBlue)
app.App.SetFocus(app.TreeView)
})
leftSide.AddItem(app.SearchBar, 1, 0, false)
overallFlex := tview.NewFlex()
overallFlex.AddItem(leftSide, 0, 2, true)
//
helpText := combineStringsWithSpaces(
"Welcome to TetraTerm!",
"\n",
"\n",
"With TetraTerm, you can easily see and",
"modify game data while it is running, making",
"debugging and the flow of game development smoother.",
"\n",
"\n",
"By default, TetraTerm will connect on port 7979.",
"Just run your game after having created a TetraTerm.Server,",
"and the terminal should pick it up automatically.",
)
keyText := `KEYS
Arrow Keys: Select Node
Shift+Arrows: Change Order / Parent
WASD, QE: Move Node in-game
R: Reset Selected Node
Shift+D: Duplicate Node
Shift+X: Delete Node
F: Follow Node with Camera
Shift+F: Search Nodes
Shift+C: Clone Nodes
`
keyText2 := `KEYS - 2
1: Toggle Debug Hierarchy Drawing
2: Toggle Debug Wireframe Drawing
3: Toggle Debug Bounds Drawing
Ctrl+R: Force Terminal Refresh (if it gets corrupted)
Ctrl+Q : Quit (Ctrl+C also works)
`
nodesText := combineStringsWithSpaces(
"NODE TREE",
"\n",
"\n",
"On the left hand side of the main view, you'll have the",
"Node Tree. This displays the nodes in the scene. You can",
"scroll it using the colored bar to the right.",
"\n",
"\n",
"On the right-hand side is the Node Properties, which lists",
"the Node's various game properties, as well as the Game Properties,",
"which lists things like FPS, node count, and render frame-time.",
)
cloneText := combineStringsWithSpaces(
"CLONING",
"\n",
"\n",
"When you press Shift+C, you go into clone mode.",
"In Clone Mode, you can specify any Node by name and clone it,",
"reparenting it to the currently selected Node.",
"\n",
"\n",
"The Node to clone can come from any Scene in the current Library (collection",
"of Scenes from which the current Scene comes from).",
)
rightSide := tview.NewFlex()
rightSide.SetDirection(tview.FlexRow)
overallFlex.AddItem(rightSide, 0, 1, false)
keysExplanation := newMultipageModal(app, "key explanation", helpText, keyText, keyText2, nodesText, cloneText)
app.NodePropertyArea = tview.NewTextArea()
app.NodePropertyArea.SetBackgroundColor(tcell.ColorDefault)
style := tcell.Style{}.Background(tcell.ColorDefault)
app.NodePropertyArea.SetTextStyle(style)
app.NodePropertyArea.SetBorder(true)
app.NodePropertyArea.SetTitle("[ Node Properties ]")
rightSide.AddItem(app.NodePropertyArea, 0, 1, false)
app.GamePropertyArea = tview.NewTextArea()
app.GamePropertyArea.SetBackgroundColor(tcell.ColorDefault)
app.GamePropertyArea.SetTextStyle(style)
app.GamePropertyArea.SetBorder(true)
app.GamePropertyArea.SetTitle("[ Game Properties ]")
rightSide.AddItem(app.GamePropertyArea, 0, 1, false)
app.Root.AddAndSwitchToPage("Tree View", overallFlex, true)
app.Root.AddPage(keysExplanation.Name, keysExplanation, true, false)
go func() {
for {
time.Sleep(time.Millisecond * 250)
resp, err := app.sendRequest(newSceneRefreshPacket())
connected := false
if err != nil {
if err.Error() == "EOF" {
// Attempt to reopen connection
app.initClient()
}
} else {
app.receivingData.Store(true)
connected = true
app.App.QueueUpdate(func() {
app.currentSceneTree = resp.(*sceneRefreshPacket).SceneTree
app.currentSceneTree.ResetParenting()
for _, node := range app.currentSceneTree.ChildrenRecursive() {
existingNode, exists := app.SceneNodesToTreeNodes[node.NodeID]
if !exists {
tn := tview.NewTreeNode(node.Name)
tn.SetSelectable(true)
tn.SetReference(node)
app.SceneNodesToTreeNodes[node.NodeID] = tn
existingNode = tn
}
existingNode.ClearChildren()