• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark-pixel / 13720364560

07 Mar 2025 12:09PM CUT coverage: 47.374% (+12.7%) from 34.646%
13720364560

push

github

web-flow
Add systems and performance monitor (#6)

162 of 190 new or added lines in 2 files covered. (85.26%)

451 of 952 relevant lines covered (47.37%)

94.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.13
/plot/systems.go
1
package plot
2

3
import (
4
        "fmt"
5
        "io"
6
        "reflect"
7

8
        px "github.com/gopxl/pixel/v2"
9
        "github.com/gopxl/pixel/v2/backends/opengl"
10
        "github.com/gopxl/pixel/v2/ext/text"
11
        "github.com/mlange-42/ark-tools/app"
12
        "github.com/mlange-42/ark/ecs"
13
)
14

15
// Systems drawer for inspecting ECS systems.
16
//
17
// Lists all systems and UI systems in their scheduling order,
18
// with their public fields.
19
//
20
// Details can be adjusted using the HideXxx fields.
21
// Further, keys U, F, T, V and N can be used to toggle details during a running simulation.
22
// The view can be scrolled using arrow keys or the mouse wheel.
23
type Systems struct {
24
        HideUISystems bool // Hides UI systems.
25
        HideFields    bool // Hides components fields.
26
        HideTypes     bool // Hides field types.
27
        HideValues    bool // Hides field values.
28
        HideNames     bool // Hide field names of nested structs.
29
        scroll        int
30
        systemsRes    ecs.Resource[app.Systems]
31
        text          *text.Text
32
        helpText      *text.Text
33
}
34

35
// Initialize the system
36
func (i *Systems) Initialize(w *ecs.World, win *opengl.Window) {
2✔
37
        i.systemsRes = ecs.NewResource[app.Systems](w)
2✔
38

2✔
39
        i.text = text.New(px.V(0, 0), defaultFont)
2✔
40
        i.helpText = text.New(px.V(0, 0), defaultFont)
2✔
41

2✔
42
        i.text.AlignedTo(px.BottomRight)
2✔
43
        i.helpText.AlignedTo(px.BottomRight)
2✔
44

2✔
45
        fmt.Fprint(i.helpText, "Toggle [u]i systems, [f]ields, [t]ypes, [v]alues or [n]ames, scroll with arrows or mouse wheel.")
2✔
46
}
2✔
47

48
// Update the drawer.
49
func (i *Systems) Update(w *ecs.World) {}
200✔
50

51
// UpdateInputs handles input events of the previous frame update.
52
func (i *Systems) UpdateInputs(w *ecs.World, win *opengl.Window) {
111✔
53
        if win.JustPressed(px.KeyF) {
111✔
NEW
54
                i.HideFields = !i.HideFields
×
NEW
55
                return
×
NEW
56
        }
×
57
        if win.JustPressed(px.KeyT) {
111✔
NEW
58
                i.HideTypes = !i.HideTypes
×
NEW
59
                return
×
NEW
60
        }
×
61
        if win.JustPressed(px.KeyV) {
111✔
NEW
62
                i.HideValues = !i.HideValues
×
NEW
63
                return
×
NEW
64
        }
×
65
        if win.JustPressed(px.KeyN) {
111✔
NEW
66
                i.HideNames = !i.HideNames
×
NEW
67
                return
×
NEW
68
        }
×
69
        if win.JustPressed(px.KeyU) {
111✔
NEW
70
                i.HideUISystems = !i.HideUISystems
×
NEW
71
                return
×
NEW
72
        }
×
73
        if win.JustPressed(px.KeyDown) {
111✔
NEW
74
                i.scroll++
×
NEW
75
                return
×
NEW
76
        }
×
77
        if win.JustPressed(px.KeyUp) {
111✔
NEW
78
                if i.scroll > 0 {
×
NEW
79
                        i.scroll--
×
NEW
80
                }
×
NEW
81
                return
×
82
        }
83
        scr := win.MouseScroll()
111✔
84
        if scr.Y != 0 {
111✔
NEW
85
                i.scroll -= int(scr.Y)
×
NEW
86
                if i.scroll < 0 {
×
NEW
87
                        i.scroll = 0
×
NEW
88
                }
×
89
        }
90
}
91

92
// Draw the system
93
func (i *Systems) Draw(w *ecs.World, win *opengl.Window) {
111✔
94
        i.helpText.Draw(win, px.IM.Moved(px.V(10, 20)))
111✔
95

111✔
96
        if !i.systemsRes.Has() {
111✔
NEW
97
                return
×
NEW
98
        }
×
99
        systems := i.systemsRes.Get()
111✔
100

111✔
101
        height := win.Canvas().Bounds().H()
111✔
102
        x0 := 10.0
111✔
103
        y0 := height - 10.0
111✔
104

111✔
105
        i.text.Clear()
111✔
106
        fmt.Fprint(i.text, "Systems\n\n")
111✔
107

111✔
108
        scroll := i.scroll
111✔
109

111✔
110
        for _, sys := range systems.Systems() {
333✔
111
                if i.HideUISystems {
244✔
112
                        if _, ok := sys.(app.UISystem); ok {
33✔
113
                                continue
11✔
114
                        }
115
                }
116

117
                val := reflect.ValueOf(sys).Elem()
211✔
118
                tp := val.Type()
211✔
119

211✔
120
                if scroll <= 0 {
422✔
121
                        fmt.Fprintf(i.text, "  %s\n", tp.Name())
211✔
122
                }
211✔
123
                scroll--
211✔
124

211✔
125
                if !i.HideFields {
422✔
126
                        for k := 0; k < val.NumField(); k++ {
1,344✔
127
                                field := tp.Field(k)
1,133✔
128
                                if field.IsExported() {
1,644✔
129
                                        if scroll <= 0 {
1,022✔
130
                                                i.printField(i.text, tp, field, val.Field(k))
511✔
131
                                        }
511✔
132
                                        scroll--
511✔
133
                                }
134
                        }
135
                        if scroll <= 0 {
422✔
136
                                fmt.Fprint(i.text, "\n")
211✔
137
                        }
211✔
138
                        scroll--
211✔
139
                }
140
        }
141

142
        if i.HideUISystems {
122✔
143
                i.text.Draw(win, px.IM.Moved(px.V(x0, y0)))
11✔
144
                return
11✔
145
        }
11✔
146

147
        fmt.Fprint(i.text, "\nUI Systems\n\n")
100✔
148
        for _, sys := range systems.UISystems() {
200✔
149
                val := reflect.ValueOf(sys).Elem()
100✔
150
                tp := val.Type()
100✔
151

100✔
152
                if scroll <= 0 {
200✔
153
                        fmt.Fprintf(i.text, "  %s\n", tp.Name())
100✔
154
                }
100✔
155
                scroll--
100✔
156

100✔
157
                if !i.HideFields {
200✔
158
                        for k := 0; k < val.NumField(); k++ {
900✔
159
                                field := tp.Field(k)
800✔
160
                                if field.IsExported() {
1,200✔
161
                                        if scroll <= 0 {
800✔
162
                                                i.printField(i.text, tp, field, val.Field(k))
400✔
163
                                        }
400✔
164
                                        scroll--
400✔
165
                                }
166
                        }
167
                        if scroll <= 0 {
200✔
168
                                fmt.Fprint(i.text, "\n")
100✔
169
                        }
100✔
170
                        scroll--
100✔
171
                }
172
        }
173

174
        i.text.Draw(win, px.IM.Moved(px.V(x0, y0)))
100✔
175
}
176

177
func (i *Systems) printField(w io.Writer, tp reflect.Type, field reflect.StructField, value reflect.Value) {
911✔
178
        fmt.Fprintf(w, "    %-20s ", field.Name)
911✔
179
        if !i.HideTypes {
1,822✔
180
                fmt.Fprintf(w, "    %-16s ", value.Type())
911✔
181
        }
911✔
182
        if !i.HideValues {
1,822✔
183
                if i.HideNames {
922✔
184
                        fmt.Fprintf(w, "= %v", value.Interface())
11✔
185
                } else {
911✔
186
                        fmt.Fprintf(w, "= %+v", value.Interface())
900✔
187
                }
900✔
188
        }
189
        fmt.Fprint(i.text, "\n")
911✔
190
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc