From 97a7a7cd2afa18b73f5982ecb3ec08c0392b6d1e Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Fri, 10 May 2024 13:14:17 -0400 Subject: move output widget to separate file --- gui/widget/output.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 gui/widget/output.go (limited to 'gui/widget/output.go') diff --git a/gui/widget/output.go b/gui/widget/output.go new file mode 100644 index 0000000..0d03c0c --- /dev/null +++ b/gui/widget/output.go @@ -0,0 +1,42 @@ +package widget + +import ( + "fmt" + "sync" + + "image" + "image/draw" + + "volute/gui" + "volute/gui/text" + "volute/gui/win" +) + +func Output(val <-chan float64, r image.Rectangle, env gui.Env, wg *sync.WaitGroup) { + defer wg.Done() + defer close(env.Draw()) + + var v float64 = 0.0 + env.Draw() <- outputDraw(v, r) +Loop: + for { + select { + case v = <-val: + env.Draw() <- outputDraw(v, r) + case event, ok := <-env.Events(): + if !ok { // channel closed + break Loop + } + if event, ok := event.(win.WiFocus); ok && event.Focused { + env.Draw() <- outputDraw(v, r) + } + } + } +} + +func outputDraw(v float64, r image.Rectangle) func(draw.Image) image.Rectangle { + return func(drw draw.Image) image.Rectangle { + text.Draw([]byte(fmt.Sprintf("%.3f", v)), drw, r, BLACK, WHITE) + return r + } +} -- cgit v1.2.3