func drawScissor(ctx *nanovgo.Context, x, y, t float32) { ctx.Save() // Draw first rect and set scissor to it's area. ctx.Translate(x, y) ctx.Rotate(nanovgo.DegToRad(5)) ctx.BeginPath() ctx.Rect(-20, -20, 60, 40) ctx.SetFillColor(nanovgo.RGBA(255, 0, 0, 255)) ctx.Fill() ctx.Scissor(-20, -20, 60, 40) // Draw second rectangle with offset and rotation. ctx.Translate(40, 0) ctx.Rotate(t) // Draw the intended second rectangle without any scissoring. ctx.Save() ctx.ResetScissor() ctx.BeginPath() ctx.Rect(-20, -10, 60, 30) ctx.SetFillColor(nanovgo.RGBA(255, 128, 0, 64)) ctx.Fill() ctx.Restore() // Draw second rectangle with combined scissoring. ctx.IntersectScissor(-20, -10, 60, 30) ctx.BeginPath() ctx.Rect(-20, -10, 60, 30) ctx.SetFillColor(nanovgo.RGBA(255, 128, 0, 255)) ctx.Fill() ctx.Restore() }
func (v *VScrollPanel) Draw(self Widget, ctx *nanovgo.Context) { if len(v.children) == 0 { return } x := float32(v.x) y := float32(v.y) w := float32(v.w) h := float32(v.h) child := v.children[0] layout := self.Layout() if layout != nil { _, v.childPreferredHeight = layout.PreferredSize(self, ctx) } else { _, v.childPreferredHeight = child.PreferredSize(child, ctx) } ctx.Save() ctx.Translate(x, y) ctx.Scissor(0, 0, w, h) ctx.Translate(0, -v.scroll*(float32(v.childPreferredHeight)-h)) if child.Visible() { child.Draw(child, ctx) } ctx.Restore() if v.childPreferredHeight > v.h { scrollH := h * minF(1.0, h/float32(v.childPreferredHeight)) scrollH = minF(maxF(20.0, scrollH), h) paint := nanovgo.BoxGradient(x+w-12+1, y+4+1, 8, h-8, 3, 4, nanovgo.MONO(0, 32), nanovgo.MONO(0, 92)) ctx.BeginPath() ctx.RoundedRect(x+w-12, y+4, 8, h-8, 3) ctx.SetFillPaint(paint) ctx.Fill() barPaint := nanovgo.BoxGradient(x+y-12-1, y+4+1+(h-8-scrollH)*v.scroll-1, 8, scrollH, 3, 4, nanovgo.MONO(220, 100), nanovgo.MONO(128, 100)) ctx.BeginPath() ctx.RoundedRect(x+w-12+1, y+4+1+(h-8-scrollH)*v.scroll, 8-2, scrollH-2, 2) ctx.SetFillPaint(barPaint) ctx.Fill() } }
// Draw() draws the widget (and all child widgets) func (w *WidgetImplement) Draw(self Widget, ctx *nanovgo.Context) { if debugFlag { ctx.SetStrokeWidth(1.0) ctx.BeginPath() ctx.Rect(float32(w.x)-0.5, float32(w.y)-0.5, float32(w.w)+1.0, float32(w.h)+1.0) ctx.SetStrokeColor(nanovgo.RGBA(255, 0, 0, 255)) ctx.Stroke() } if len(w.children) == 0 { return } ctx.Translate(float32(w.x), float32(w.y)) // draw depth 0 items var drawLater widgetsAsc = make([]Widget, 0, len(w.children)) for _, child := range w.children { if child.Visible() { depth := child.Depth() if depth == 0 { cx, cy := child.Position() cw, ch := child.Size() if !self.IsClipped(cx, cy, cw, ch) { child.Draw(child, ctx) } } else { drawLater = append(drawLater, child) } } } // draw by depth order sort.Sort(drawLater) for _, child := range drawLater { cx, cy := child.Position() cw, ch := child.Size() if !self.IsClipped(cx, cy, cw, ch) { child.Draw(child, ctx) } } ctx.Translate(-float32(w.x), -float32(w.y)) }
func (c *ColorWheel) Draw(self Widget, ctx *nanovgo.Context) { c.WidgetImplement.Draw(self, ctx) if !c.visible { return } x := float32(c.x) y := float32(c.y) w := float32(c.w) h := float32(c.h) ctx.Save() defer ctx.Restore() cx := x + w*0.5 cy := y + h*0.5 r1 := toF(w < h, w, h)*0.5 - 5.0 r0 := r1 * 0.75 aeps := 0.7 / r1 // half a pixel arc length in radians (2pi cancels out). for i := 0; i < 6; i++ { a0 := float32(i)/6.0*nanovgo.PI*2.0 - aeps a1 := float32(i+1)/6.0*nanovgo.PI*2.0 + aeps ctx.BeginPath() ctx.Arc(cx, cy, r0, a0, a1, nanovgo.Clockwise) ctx.Arc(cx, cy, r1, a1, a0, nanovgo.CounterClockwise) ctx.ClosePath() sin1, cos1 := sinCosF(a0) sin2, cos2 := sinCosF(a1) ax := cx + cos1*(r0+r1)*0.5 ay := cy + sin1*(r0+r1)*0.5 bx := cx + cos2*(r0+r1)*0.5 by := cy + sin2*(r0+r1)*0.5 color1 := nanovgo.HSLA(a0/(nanovgo.PI*2), 1.0, 0.55, 255) color2 := nanovgo.HSLA(a1/(nanovgo.PI*2), 1.0, 0.55, 255) paint := nanovgo.LinearGradient(ax, ay, bx, by, color1, color2) ctx.SetFillPaint(paint) ctx.Fill() } ctx.BeginPath() ctx.Circle(cx, cy, r0-0.5) ctx.Circle(cx, cy, r1+0.5) ctx.SetStrokeColor(nanovgo.MONO(0, 64)) ctx.Stroke() // Selector ctx.Save() defer ctx.Restore() ctx.Translate(cx, cy) ctx.Rotate(c.hue * nanovgo.PI * 2) // Marker on u := clampF(r1/50, 1.5, 4.0) ctx.SetStrokeWidth(u) ctx.BeginPath() ctx.Rect(r0-1, -2*u, r1-r0+2, 4*u) ctx.SetStrokeColor(nanovgo.MONO(255, 192)) ctx.Stroke() paint := nanovgo.BoxGradient(r0-3, -5, r1-r0+6, 10, 2, 4, nanovgo.MONO(0, 128), nanovgo.MONO(0, 0)) ctx.BeginPath() ctx.Rect(r0-2-10, -4-10, r1-r0+4+20, 8+20) ctx.Rect(r0-2, -4, r1-r0+4, 8) ctx.PathWinding(nanovgo.Hole) ctx.SetFillPaint(paint) ctx.Fill() // Center triangle r := r0 - 6 sin1, cos1 := sinCosF(120.0 / 180.0 * nanovgo.PI) sin2, cos2 := sinCosF(-120.0 / 180.0 * nanovgo.PI) ax := cos1 * r ay := sin1 * r bx := cos2 * r by := sin2 * r ctx.BeginPath() ctx.MoveTo(r, 0) ctx.LineTo(ax, ay) ctx.LineTo(bx, by) ctx.ClosePath() triPaint1 := nanovgo.LinearGradient(r, 0, ax, ay, nanovgo.HSL(c.hue, 1.0, 0.5), nanovgo.MONO(255, 255)) ctx.SetFillPaint(triPaint1) ctx.Fill() triPaint2 := nanovgo.LinearGradient((r+ax)*0.5, ay*0.5, bx, by, nanovgo.MONO(0, 0), nanovgo.MONO(0, 255)) ctx.SetFillPaint(triPaint2) ctx.Fill() // selector circle on triangle px, py := c.calculatePosition() ctx.SetStrokeWidth(u) ctx.BeginPath() ctx.Circle(px, py, 2*u) ctx.SetStrokeColor(nanovgo.MONO(255, 192)) ctx.Stroke() }
func drawColorWheel(ctx *nanovgo.Context, x, y, w, h, t float32) { var r0, r1, ax, ay, bx, by, aeps, r float32 hue := sinF(t * 0.12) ctx.Save() defer ctx.Restore() /* ctx.BeginPath() ctx.Rect(x,y,w,h) ctx.FillColor(nanovgo.RGBA(255,0,0,128)) ctx.Fill()*/ cx := x + w*0.5 cy := y + h*0.5 if w < h { r1 = w*0.5 - 5.0 } else { r1 = h*0.5 - 5.0 } r0 = r1 - 20.0 aeps = 0.5 / r1 // half a pixel arc length in radians (2pi cancels out). for i := 0; i < 6; i++ { a0 := float32(i)/6.0*nanovgo.PI*2.0 - aeps a1 := float32(i+1.0)/6.0*nanovgo.PI*2.0 + aeps ctx.BeginPath() ctx.Arc(cx, cy, r0, a0, a1, nanovgo.Clockwise) ctx.Arc(cx, cy, r1, a1, a0, nanovgo.CounterClockwise) ctx.ClosePath() ax = cx + cosF(a0)*(r0+r1)*0.5 ay = cy + sinF(a0)*(r0+r1)*0.5 bx = cx + cosF(a1)*(r0+r1)*0.5 by = cy + sinF(a1)*(r0+r1)*0.5 paint := nanovgo.LinearGradient(ax, ay, bx, by, nanovgo.HSLA(a0/(nanovgo.PI*2), 1.0, 0.55, 255), nanovgo.HSLA(a1/(nanovgo.PI*2), 1.0, 0.55, 255)) ctx.SetFillPaint(paint) ctx.Fill() } ctx.BeginPath() ctx.Circle(cx, cy, r0-0.5) ctx.Circle(cx, cy, r1+0.5) ctx.SetStrokeColor(nanovgo.RGBA(0, 0, 0, 64)) ctx.SetStrokeWidth(1.0) ctx.Stroke() // Selector ctx.Translate(cx, cy) ctx.Rotate(hue * nanovgo.PI * 2) // Marker on ctx.SetStrokeWidth(2.0) ctx.BeginPath() ctx.Rect(r0-1, -3, r1-r0+2, 6) ctx.SetStrokeColor(nanovgo.RGBA(255, 255, 255, 192)) ctx.Stroke() paint := nanovgo.BoxGradient(r0-3, -5, r1-r0+6, 10, 2, 4, nanovgo.RGBA(0, 0, 0, 128), nanovgo.RGBA(0, 0, 0, 0)) ctx.BeginPath() ctx.Rect(r0-2-10, -4-10, r1-r0+4+20, 8+20) ctx.Rect(r0-2, -4, r1-r0+4, 8) ctx.PathWinding(nanovgo.Hole) ctx.SetFillPaint(paint) ctx.Fill() // Center triangle r = r0 - 6 ax = cosF(120.0/180.0*nanovgo.PI) * r ay = sinF(120.0/180.0*nanovgo.PI) * r bx = cosF(-120.0/180.0*nanovgo.PI) * r by = sinF(-120.0/180.0*nanovgo.PI) * r ctx.BeginPath() ctx.MoveTo(r, 0) ctx.LineTo(ax, ay) ctx.LineTo(bx, by) ctx.ClosePath() paint = nanovgo.LinearGradient(r, 0, ax, ay, nanovgo.HSLA(hue, 1.0, 0.5, 255), nanovgo.RGBA(255, 255, 255, 255)) ctx.SetFillPaint(paint) ctx.Fill() paint = nanovgo.LinearGradient((r+ax)*0.5, (0+ay)*0.5, bx, by, nanovgo.RGBA(0, 0, 0, 0), nanovgo.RGBA(0, 0, 0, 255)) ctx.SetFillPaint(paint) ctx.Fill() ctx.SetStrokeColor(nanovgo.RGBA(0, 0, 0, 64)) ctx.Stroke() // Select circle on triangle ax = cosF(120.0/180.0*nanovgo.PI) * r * 0.3 ay = sinF(120.0/180.0*nanovgo.PI) * r * 0.4 ctx.SetStrokeWidth(2.0) ctx.BeginPath() ctx.Circle(ax, ay, 5) ctx.SetStrokeColor(nanovgo.RGBA(255, 255, 255, 192)) ctx.Stroke() paint = nanovgo.RadialGradient(ax, ay, 7, 9, nanovgo.RGBA(0, 0, 0, 64), nanovgo.RGBA(0, 0, 0, 0)) ctx.BeginPath() ctx.Rect(ax-20, ay-20, 40, 40) ctx.Circle(ax, ay, 7) ctx.PathWinding(nanovgo.Hole) ctx.SetFillPaint(paint) ctx.Fill() }
func drawThumbnails(ctx *nanovgo.Context, x, y, w, h float32, images []int, t float32) { var cornerRadius float32 = 3.0 var thumb float32 = 60.0 var arry float32 = 30.5 stackh := float32(len(images)/2)*(thumb+10) + 10 u := (1 + cosF(t*0.5)) * 0.5 u2 := (1 - cosF(t*0.2)) * 0.5 ctx.Save() defer ctx.Restore() // Drop shadow shadowPaint := nanovgo.BoxGradient(x, y+4, w, h, cornerRadius*2, 20, nanovgo.RGBA(0, 0, 0, 128), nanovgo.RGBA(0, 0, 0, 0)) ctx.BeginPath() ctx.Rect(x-10, y-10, w+20, h+30) ctx.RoundedRect(x, y, w, h, cornerRadius) ctx.PathWinding(nanovgo.Hole) ctx.SetFillPaint(shadowPaint) ctx.Fill() // Window ctx.BeginPath() ctx.RoundedRect(x, y, w, h, cornerRadius) ctx.MoveTo(x-10, y+arry) ctx.LineTo(x+1, y+arry-11) ctx.LineTo(x+1, y+arry+11) ctx.SetFillColor(nanovgo.RGBA(200, 200, 200, 255)) ctx.Fill() ctx.Block(func() { ctx.Scissor(x, y, w, h) ctx.Translate(0, -(stackh-h)*u) dv := 1.0 / float32(len(images)-1) for i, imageID := range images { tx := x + 10.0 ty := y + 10.0 tx += float32(i%2) * (thumb + 10.0) ty += float32(i/2) * (thumb + 10.0) imgW, imgH, _ := ctx.ImageSize(imageID) var iw, ih, ix, iy float32 if imgW < imgH { iw = thumb ih = iw * float32(imgH) / float32(imgW) ix = 0 iy = -(ih - thumb) * 0.5 } else { ih = thumb iw = ih * float32(imgW) / float32(imgH) ix = -(iw - thumb) * 0.5 iy = 0 } v := float32(i) * dv a := clampF((u2-v)/dv, 0, 1) if a < 1.0 { drawSpinner(ctx, tx+thumb/2, ty+thumb/2, thumb*0.25, t) } imgPaint := nanovgo.ImagePattern(tx+ix, ty+iy, iw, ih, 0.0/180.0*nanovgo.PI, imageID, a) ctx.BeginPath() ctx.RoundedRect(tx, ty, thumb, thumb, 5) ctx.SetFillPaint(imgPaint) ctx.Fill() shadowPaint := nanovgo.BoxGradient(tx-1, ty, thumb+2, thumb+2, 5, 3, nanovgo.RGBA(0, 0, 0, 128), nanovgo.RGBA(0, 0, 0, 0)) ctx.BeginPath() ctx.Rect(tx-5, ty-5, thumb+10, thumb+10) ctx.RoundedRect(tx, ty, thumb, thumb, 6) ctx.PathWinding(nanovgo.Hole) ctx.SetFillPaint(shadowPaint) ctx.Fill() ctx.BeginPath() ctx.RoundedRect(tx+0.5, ty+0.5, thumb-1, thumb-1, 4-0.5) ctx.SetStrokeWidth(1.0) ctx.SetStrokeColor(nanovgo.RGBA(255, 255, 255, 192)) ctx.Stroke() } }) // Hide fades fadePaint := nanovgo.LinearGradient(x, y, x, y+6, nanovgo.RGBA(200, 200, 200, 255), nanovgo.RGBA(200, 200, 200, 0)) ctx.BeginPath() ctx.Rect(x+4, y, w-8, 6) ctx.SetFillPaint(fadePaint) ctx.Fill() fadePaint = nanovgo.LinearGradient(x, y+h, x, y+h-6, nanovgo.RGBA(200, 200, 200, 255), nanovgo.RGBA(200, 200, 200, 0)) ctx.BeginPath() ctx.Rect(x+4, y+h-6, w-8, 6) ctx.SetFillPaint(fadePaint) ctx.Fill() // Scroll bar shadowPaint = nanovgo.BoxGradient(x+w-12+1, y+4+1, 8, h-8, 3, 4, nanovgo.RGBA(0, 0, 0, 32), nanovgo.RGBA(0, 0, 0, 92)) ctx.BeginPath() ctx.RoundedRect(x+w-12, y+4, 8, h-8, 3) ctx.SetFillPaint(shadowPaint) // ctx.FillColor(ctx.RGBA(255,0,0,128)); ctx.Fill() scrollH := (h / stackh) * (h - 8) shadowPaint = nanovgo.BoxGradient(x+w-12-1, y+4+(h-8-scrollH)*u-1, 8, scrollH, 3, 4, nanovgo.RGBA(220, 220, 220, 255), nanovgo.RGBA(128, 128, 128, 255)) ctx.BeginPath() ctx.RoundedRect(x+w-12+1, y+4+1+(h-8-scrollH)*u, 8-2, scrollH-2, 2) ctx.SetFillPaint(shadowPaint) // ctx.FillColor(ctx.RGBA(0,0,0,128)); ctx.Fill() }