From 70aae360db4e71592c276a8dfedd50cd77e69fa2 Mon Sep 17 00:00:00 2001 From: esimov Date: Wed, 15 Dec 2021 15:33:11 +0200 Subject: [PATCH] gui: extended supported draw methods --- draw.go | 22 ++++++++++++++++++++++ gui.go | 39 ++++++++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/draw.go b/draw.go index e7e02cc..7ec7545 100644 --- a/draw.go +++ b/draw.go @@ -1,12 +1,15 @@ package caire import ( + "image" "image/color" "math" "gioui.org/f32" "gioui.org/op/clip" "gioui.org/op/paint" + "gioui.org/unit" + "gioui.org/widget" ) type shapeType int @@ -27,6 +30,25 @@ func (g *Gui) DrawSeam(shape shapeType, x, y, s float64) { } } +// EncodeSeamToImg draws the seams into an image widget. +func (g *Gui) EncodeSeamToImg() { + g.setFillColor(color.White) + + img := image.NewNRGBA(image.Rect(0, 0, int(g.cfg.window.w), int(g.cfg.window.h))) + for _, s := range g.proc.seams { + img.Set(s.X, s.Y, g.getFillColor()) + } + + src := paint.NewImageOp(img) + src.Add(g.ctx.Ops) + + widget.Image{ + Src: src, + Scale: 1 / float32(g.ctx.Px(unit.Dp(1))), + Fit: widget.Contain, + }.Layout(g.ctx) +} + // drawCircle draws a circle at the seam (x,y) coordinate with the provided size. func (g *Gui) drawCircle(x, y, s float64) { var ( diff --git a/gui.go b/gui.go index 0426864..613a79b 100644 --- a/gui.go +++ b/gui.go @@ -10,6 +10,7 @@ import ( "gioui.org/io/system" "gioui.org/layout" "gioui.org/op" + "gioui.org/op/clip" "gioui.org/op/paint" "gioui.org/unit" "gioui.org/widget" @@ -142,26 +143,38 @@ func (g *Gui) draw(win *app.Window, e system.FrameEvent) { g.ctx = layout.NewContext(g.ctx.Ops, e) win.Invalidate() - color := g.setColor(g.cfg.color.background) - paint.Fill(g.ctx.Ops, color) + c := g.setColor(g.cfg.color.background) + paint.Fill(g.ctx.Ops, c) if g.proc.img != nil { src := paint.NewImageOp(g.proc.img) src.Add(g.ctx.Ops) - imgWidget := widget.Image{ - Src: src, - Scale: 1 / float32(g.ctx.Px(unit.Dp(1))), - Fit: widget.Contain, - } + layout.Flex{ + Axis: layout.Horizontal, + }.Layout(g.ctx, + layout.Flexed(1, func(gtx layout.Context) layout.Dimensions { + paint.FillShape(gtx.Ops, c, + clip.Rect{Max: g.ctx.Constraints.Max}.Op(), + ) + return layout.UniformInset(unit.Px(0)).Layout(gtx, + func(gtx layout.Context) layout.Dimensions { + widget.Image{ + Src: src, + Scale: 1 / float32(g.ctx.Px(unit.Dp(1))), + Fit: widget.Contain, + }.Layout(gtx) - imgWidget.Layout(g.ctx) + if g.cp.Debug { + for _, s := range g.proc.seams { + g.DrawSeam(circle, float64(s.X), float64(s.Y), 2) + } + } + return layout.Dimensions{Size: gtx.Constraints.Max} + }) + }), + ) - if g.cp.Debug { - for _, s := range g.proc.seams { - g.DrawSeam(line, float64(s.X), float64(s.Y), 1) - } - } } e.Frame(g.ctx.Ops) }