Add code components 366-385; ScrollArea Follow, editor sync
A terminal with history, a Go-colored code block, a log viewer that follows, a hex viewer, a memory map, a JSON tree, a line diff, a GAME OVER crash screen, an FPS meter, profiler bars, breakpoints, variables, command history, a shortcut list, a debug grid, a pixel editor that exports string art, a palette editor, a sprite sheet, a glyph map and a 6502 register view. layout.ScrollArea takes Follow, and ChatLog now uses it. data.TreeNode takes a Value drawn in its Slot. pixel gains Has, and Sprite.Art writes MustSprite art. pixel.Editor takes the caller's value at each input event, so a value changed between frames is never typed onto.
This commit is contained in:
42 files changed
+3415
-37
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package code
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||||
)
|
||||
|
||||
// Span is timed work, starting inside its parent's time.
|
||||
type Span struct {
|
||||
Name string
|
||||
Start, Length time.Duration
|
||||
Children []Span
|
||||
}
|
||||
|
||||
type ProfilerBarsOptions struct {
|
||||
// Width is in dots; zero means 240.
|
||||
Width int
|
||||
}
|
||||
|
||||
var depths = []pixel.Slot{pixel.Accent, pixel.Info, pixel.Good, pixel.Warn, pixel.Magic}
|
||||
|
||||
// ProfilerBars stacks spans under their parents, each bar its time.
|
||||
func ProfilerBars(c *ui.Context, text string, root Span, o ProfilerBarsOptions) *ui.Element {
|
||||
k := pixel.K(c)
|
||||
w := pixel.Or("ProfilerBars Width", o.Width, 240)
|
||||
if root.Length <= 0 {
|
||||
panic(fmt.Sprintf("code: ProfilerBars root %q lasts %v", root.Name, root.Length))
|
||||
}
|
||||
var said []string
|
||||
rows := check(root, &said)
|
||||
x := func(d time.Duration) int { return int(int64(w) * int64(d-root.Start) / int64(root.Length)) }
|
||||
return ui.Box(c).Size(k.D(w), k.D(rows*pixel.LineHeight)).Role(ui.RoleImage).Label(text).Description(strings.Join(said, "; ")).Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
g := k.Canvas(p, r)
|
||||
var bar func(s Span, depth int)
|
||||
bar = func(s Span, depth int) {
|
||||
x0, y := x(s.Start), depth*pixel.LineHeight
|
||||
bw := max(1, x(s.Start+s.Length)-x0)
|
||||
g.Rect(x0, y+1, bw, pixel.LineHeight-2, depths[depth%len(depths)])
|
||||
// Names show where they fit, times when they also fit.
|
||||
for _, s := range []string{fmt.Sprintf("%s %.1fms", s.Name, ms(s.Length)), s.Name} {
|
||||
if pixel.Measure(s)+4 <= bw {
|
||||
g.Text(x0+2, y, s, pixel.AccentInk, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, ch := range s.Children {
|
||||
bar(ch, depth+1)
|
||||
}
|
||||
}
|
||||
bar(root, 0)
|
||||
})
|
||||
}
|
||||
|
||||
// check counts rows; a child outside its parent panics.
|
||||
func check(s Span, said *[]string) int {
|
||||
*said = append(*said, fmt.Sprintf("%s %.1f ms", s.Name, ms(s.Length)))
|
||||
rows := 1
|
||||
for _, ch := range s.Children {
|
||||
if ch.Length < 0 || ch.Start < s.Start || ch.Start+ch.Length > s.Start+s.Length {
|
||||
panic(fmt.Sprintf("code: ProfilerBars span %q is outside %q", ch.Name, s.Name))
|
||||
}
|
||||
rows = max(rows, 1+check(ch, said))
|
||||
}
|
||||
return rows
|
||||
}
|
||||
Reference in new issue
Block a user