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

mlange-42 / arche-pixel / 9206262010

23 May 2024 10:17AM CUT coverage: 88.275% (+0.03%) from 88.246%
9206262010

push

github

web-flow
Add optional property `XLim` to `Lines` plot (#54)

4 of 4 new or added lines in 1 file covered. (100.0%)

1438 of 1629 relevant lines covered (88.28%)

222101.43 hits per line

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

75.23
/plot/inspector.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/arche-model/resource"
12
        "github.com/mlange-42/arche/ecs"
13
        "github.com/mlange-42/arche/generic"
14
)
15

16
// Inspector drawer for inspecting entities.
17
//
18
// Shows information of the entity indicated by the SelectedEntity resource ([github.com/mlange-42/arche-model/resource.SelectedEntity]).
19
// Entity selection is to be done by another system, e.g. by user input.
20
//
21
// Details can be adjusted using the HideXxx fields.
22
// Further, keys F, T, V and N can be used to toggle details during a running simulation.
23
// The view can be scrolled using arrow keys or the mouse wheel.
24
type Inspector struct {
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
        selectedRes generic.Resource[resource.SelectedEntity]
31
        text        *text.Text
32
        helpText    *text.Text
33
}
34

35
// Initialize the system
36
func (i *Inspector) Initialize(w *ecs.World, win *opengl.Window) {
3✔
37
        i.selectedRes = generic.NewResource[resource.SelectedEntity](w)
3✔
38

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

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

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

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

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

88
// Draw the system
89
func (i *Inspector) Draw(w *ecs.World, win *opengl.Window) {
122✔
90
        i.helpText.Draw(win, px.IM.Moved(px.V(10, 20)))
122✔
91

122✔
92
        if !i.selectedRes.Has() {
122✔
93
                return
×
94
        }
×
95
        sel := i.selectedRes.Get().Selected
122✔
96
        if sel.IsZero() {
122✔
97
                return
×
98
        }
×
99

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

122✔
104
        i.text.Clear()
122✔
105
        fmt.Fprintf(i.text, "Entity %+v\n\n", sel)
122✔
106

122✔
107
        if !w.Alive(sel) {
133✔
108
                fmt.Fprint(i.text, "  dead entity")
11✔
109
                i.text.Draw(win, px.IM.Moved(px.V(x0, y0)))
11✔
110
                return
11✔
111
        }
11✔
112

113
        mask := w.Mask(sel)
111✔
114
        bits := mask.TotalBitsSet()
111✔
115

111✔
116
        scroll := i.scroll
111✔
117

111✔
118
        allComp := ecs.ComponentIDs(w)
111✔
119
        for _, id := range allComp {
333✔
120
                if mask.Get(id) {
444✔
121
                        tp, _ := ecs.ComponentInfo(w, id)
222✔
122
                        ptr := w.Get(sel, id)
222✔
123
                        val := reflect.NewAt(tp.Type, ptr).Elem()
222✔
124

222✔
125
                        if scroll <= 0 {
444✔
126
                                fmt.Fprintf(i.text, "  %s\n", tp.Type.Name())
222✔
127
                        }
222✔
128
                        scroll--
222✔
129

222✔
130
                        if !i.HideFields {
444✔
131
                                for k := 0; k < val.NumField(); k++ {
666✔
132
                                        field := tp.Type.Field(k)
444✔
133
                                        if field.IsExported() {
888✔
134
                                                if scroll <= 0 {
888✔
135
                                                        i.printField(i.text, tp.Type, field, val.Field(k))
444✔
136
                                                }
444✔
137
                                                scroll--
444✔
138
                                        }
139
                                }
140
                                if scroll <= 0 {
444✔
141
                                        fmt.Fprint(i.text, "\n")
222✔
142
                                }
222✔
143
                                scroll--
222✔
144
                        }
145
                        bits--
222✔
146
                }
147
        }
148

149
        i.text.Draw(win, px.IM.Moved(px.V(x0, y0)))
111✔
150
}
151

152
func (i *Inspector) printField(w io.Writer, tp reflect.Type, field reflect.StructField, value reflect.Value) {
444✔
153
        fmt.Fprintf(w, "    %-20s ", field.Name)
444✔
154
        if !i.HideTypes {
888✔
155
                fmt.Fprintf(w, "    %-16s ", value.Type())
444✔
156
        }
444✔
157
        if !i.HideValues {
888✔
158
                if i.HideNames {
488✔
159
                        fmt.Fprintf(w, "= %v", value.Interface())
44✔
160
                } else {
444✔
161
                        fmt.Fprintf(w, "= %+v", value.Interface())
400✔
162
                }
400✔
163
        }
164
        fmt.Fprint(i.text, "\n")
444✔
165
}
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