• 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

74.19
/plot/resources.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/ecs"
12
)
13

14
// Resources drawer for inspecting ECS resources.
15
//
16
// Lists all resources with their public fields.
17
//
18
// Details can be adjusted using the HideXxx fields.
19
// Further, keys F, T, V and N can be used to toggle details during a running simulation.
20
// The view can be scrolled using arrow keys or the mouse wheel.
21
type Resources struct {
22
        HideFields bool // Hides components fields.
23
        HideTypes  bool // Hides field types.
24
        HideValues bool // Hides field values.
25
        HideNames  bool // Hide field names of nested structs.
26
        scroll     int
27
        text       *text.Text
28
        helpText   *text.Text
29
}
30

31
// Initialize the system
32
func (i *Resources) Initialize(w *ecs.World, win *opengl.Window) {
2✔
33
        i.text = text.New(px.V(0, 0), defaultFont)
2✔
34
        i.helpText = text.New(px.V(0, 0), defaultFont)
2✔
35

2✔
36
        i.text.AlignedTo(px.BottomRight)
2✔
37
        i.helpText.AlignedTo(px.BottomRight)
2✔
38

2✔
39
        fmt.Fprint(i.helpText, "Toggle [f]ields, [t]ypes, [v]alues or [n]ames, scroll with arrows or mouse wheel.")
2✔
40
}
2✔
41

42
// Update the drawer.
43
func (i *Resources) Update(w *ecs.World) {}
110✔
44

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

82
// Draw the system
83
func (i *Resources) Draw(w *ecs.World, win *opengl.Window) {
110✔
84
        i.helpText.Draw(win, px.IM.Moved(px.V(10, 20)))
110✔
85

110✔
86
        height := win.Canvas().Bounds().H()
110✔
87
        x0 := 10.0
110✔
88
        y0 := height - 10.0
110✔
89

110✔
90
        i.text.Clear()
110✔
91
        fmt.Fprint(i.text, "Resources\n\n")
110✔
92

110✔
93
        scroll := i.scroll
110✔
94

110✔
95
        res := w.Resources()
110✔
96
        allRes := ecs.ResourceIDs(w)
110✔
97
        for _, id := range allRes {
570✔
98
                if !res.Has(id) {
470✔
99
                        continue
10✔
100
                }
101
                ptr := res.Get(id)
450✔
102
                val := reflect.ValueOf(ptr).Elem()
450✔
103
                tp := val.Type()
450✔
104

450✔
105
                if scroll <= 0 {
900✔
106
                        fmt.Fprintf(i.text, "  %s\n", tp.Name())
450✔
107
                }
450✔
108
                scroll--
450✔
109

450✔
110
                if !i.HideFields {
900✔
111
                        for k := 0; k < val.NumField(); k++ {
2,340✔
112
                                field := tp.Field(k)
1,890✔
113
                                if field.IsExported() {
2,570✔
114
                                        if scroll <= 0 {
1,360✔
115
                                                i.printField(i.text, tp, field, val.Field(k))
680✔
116
                                        }
680✔
117
                                        scroll--
680✔
118
                                }
119
                        }
120
                        if scroll <= 0 {
900✔
121
                                fmt.Fprint(i.text, "\n")
450✔
122
                        }
450✔
123
                        scroll--
450✔
124
                }
125
        }
126

127
        i.text.Draw(win, px.IM.Moved(px.V(x0, y0)))
110✔
128
}
129

130
func (i *Resources) printField(w io.Writer, tp reflect.Type, field reflect.StructField, value reflect.Value) {
680✔
131
        fmt.Fprintf(w, "    %-20s ", field.Name)
680✔
132
        if !i.HideTypes {
1,360✔
133
                fmt.Fprintf(w, "    %-16s ", value.Type())
680✔
134
        }
680✔
135
        if !i.HideValues {
1,360✔
136
                if i.HideNames {
680✔
137
                        fmt.Fprintf(w, "= %v", value.Interface())
×
138
                } else {
680✔
139
                        fmt.Fprintf(w, "= %+v", value.Interface())
680✔
140
                }
680✔
141
        }
142
        fmt.Fprint(i.text, "\n")
680✔
143
}
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