Skip to content

Commit

Permalink
Merge pull request #592 from gucio321/canvas-export-drawlist
Browse files Browse the repository at this point in the history
Plot: add possibility to use PlotDrawList
  • Loading branch information
AllenDang authored Nov 28, 2022
2 parents 93764f0 + bfe66cd commit 105078d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
50 changes: 27 additions & 23 deletions Canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ import (
// conceptually you may see this as a dynamic "mesh" builder)
//
// for more details and use cases see examples/canvas.
// NOTE: GetCanvas() method returns a window-level canvas, however
// you can convert any imgui.DrawList into this type.
// The best example could be imgui.GetPlotDrawList()
// c := &Canvas{imgui.GetXXXDrawList()}
type Canvas struct {
drawlist imgui.DrawList
DrawList imgui.DrawList
}

// GetCanvas returns current draw list (for current window).
// it will fail if called out of window's layout.
func GetCanvas() *Canvas {
return &Canvas{
drawlist: imgui.GetWindowDrawList(),
DrawList: imgui.GetWindowDrawList(),
}
}

// AddLine draws a line (from p1 to p2).
func (c *Canvas) AddLine(p1, p2 image.Point, col color.Color, thickness float32) {
c.drawlist.AddLine(ToVec2(p1), ToVec2(p2), ToVec4Color(col), thickness)
c.DrawList.AddLine(ToVec2(p1), ToVec2(p2), ToVec4Color(col), thickness)
}

// DrawFlags represents imgui.DrawFlags.
Expand Down Expand Up @@ -66,92 +70,92 @@ const (

// AddRect draws a rectangle.
func (c *Canvas) AddRect(pMin, pMax image.Point, col color.Color, rounding float32, roundingCorners DrawFlags, thickness float32) {
c.drawlist.AddRect(ToVec2(pMin), ToVec2(pMax), ToVec4Color(col), rounding, int(roundingCorners), thickness)
c.DrawList.AddRect(ToVec2(pMin), ToVec2(pMax), ToVec4Color(col), rounding, int(roundingCorners), thickness)
}

// AddRectFilled draws a rectangle filled with `col`.
func (c *Canvas) AddRectFilled(pMin, pMax image.Point, col color.Color, rounding float32, roundingCorners DrawFlags) {
c.drawlist.AddRectFilled(ToVec2(pMin), ToVec2(pMax), ToVec4Color(col), rounding, int(roundingCorners))
c.DrawList.AddRectFilled(ToVec2(pMin), ToVec2(pMax), ToVec4Color(col), rounding, int(roundingCorners))
}

// AddText draws text.
func (c *Canvas) AddText(pos image.Point, col color.Color, text string) {
c.drawlist.AddText(ToVec2(pos), ToVec4Color(col), Context.FontAtlas.RegisterString(text))
c.DrawList.AddText(ToVec2(pos), ToVec4Color(col), Context.FontAtlas.RegisterString(text))
}

// AddBezierCubic draws bezier cubic.
func (c *Canvas) AddBezierCubic(pos0, cp0, cp1, pos1 image.Point, col color.Color, thickness float32, numSegments int) {
c.drawlist.AddBezierCubic(ToVec2(pos0), ToVec2(cp0), ToVec2(cp1), ToVec2(pos1), ToVec4Color(col), thickness, numSegments)
c.DrawList.AddBezierCubic(ToVec2(pos0), ToVec2(cp0), ToVec2(cp1), ToVec2(pos1), ToVec4Color(col), thickness, numSegments)
}

// AddTriangle draws a triangle.
func (c *Canvas) AddTriangle(p1, p2, p3 image.Point, col color.Color, thickness float32) {
c.drawlist.AddTriangle(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec4Color(col), thickness)
c.DrawList.AddTriangle(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec4Color(col), thickness)
}

// AddTriangleFilled draws a filled triangle.
func (c *Canvas) AddTriangleFilled(p1, p2, p3 image.Point, col color.Color) {
c.drawlist.AddTriangleFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec4Color(col))
c.DrawList.AddTriangleFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec4Color(col))
}

// AddCircle draws a circle.
func (c *Canvas) AddCircle(center image.Point, radius float32, col color.Color, segments int, thickness float32) {
c.drawlist.AddCircle(ToVec2(center), radius, ToVec4Color(col), segments, thickness)
c.DrawList.AddCircle(ToVec2(center), radius, ToVec4Color(col), segments, thickness)
}

// AddCircleFilled draws a filled circle.
func (c *Canvas) AddCircleFilled(center image.Point, radius float32, col color.Color) {
c.drawlist.AddCircleFilled(ToVec2(center), radius, ToVec4Color(col))
c.DrawList.AddCircleFilled(ToVec2(center), radius, ToVec4Color(col))
}

// AddQuad draws a quad.
func (c *Canvas) AddQuad(p1, p2, p3, p4 image.Point, col color.Color, thickness float32) {
c.drawlist.AddQuad(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ToVec4Color(col), thickness)
c.DrawList.AddQuad(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ToVec4Color(col), thickness)
}

// AddQuadFilled draws a filled quad.
func (c *Canvas) AddQuadFilled(p1, p2, p3, p4 image.Point, col color.Color) {
c.drawlist.AddQuadFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ToVec4Color(col))
c.DrawList.AddQuadFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ToVec4Color(col))
}

// Stateful path API, add points then finish with PathFillConvex() or PathStroke().

func (c *Canvas) PathClear() {
c.drawlist.PathClear()
c.DrawList.PathClear()
}

func (c *Canvas) PathLineTo(pos image.Point) {
c.drawlist.PathLineTo(ToVec2(pos))
c.DrawList.PathLineTo(ToVec2(pos))
}

func (c *Canvas) PathLineToMergeDuplicate(pos image.Point) {
c.drawlist.PathLineToMergeDuplicate(ToVec2(pos))
c.DrawList.PathLineToMergeDuplicate(ToVec2(pos))
}

func (c *Canvas) PathFillConvex(col color.Color) {
c.drawlist.PathFillConvex(ToVec4Color(col))
c.DrawList.PathFillConvex(ToVec4Color(col))
}

func (c *Canvas) PathStroke(col color.Color, closed bool, thickness float32) {
c.drawlist.PathStroke(ToVec4Color(col), closed, thickness)
c.DrawList.PathStroke(ToVec4Color(col), closed, thickness)
}

func (c *Canvas) PathArcTo(center image.Point, radius, min, max float32, numSegments int) {
c.drawlist.PathArcTo(ToVec2(center), radius, min, max, numSegments)
c.DrawList.PathArcTo(ToVec2(center), radius, min, max, numSegments)
}

func (c *Canvas) PathArcToFast(center image.Point, radius float32, min12, max12 int) {
c.drawlist.PathArcToFast(ToVec2(center), radius, min12, max12)
c.DrawList.PathArcToFast(ToVec2(center), radius, min12, max12)
}

func (c *Canvas) PathBezierCubicCurveTo(p1, p2, p3 image.Point, numSegments int) {
c.drawlist.PathBezierCubicCurveTo(ToVec2(p1), ToVec2(p2), ToVec2(p3), numSegments)
c.DrawList.PathBezierCubicCurveTo(ToVec2(p1), ToVec2(p2), ToVec2(p3), numSegments)
}

func (c *Canvas) AddImage(texture *Texture, pMin, pMax image.Point) {
c.drawlist.AddImage(texture.id, ToVec2(pMin), ToVec2(pMax))
c.DrawList.AddImage(texture.id, ToVec2(pMin), ToVec2(pMax))
}

func (c *Canvas) AddImageV(texture *Texture, pMin, pMax, uvMin, uvMax image.Point, col color.Color) {
c.drawlist.AddImageV(texture.id, ToVec2(pMin), ToVec2(pMax), ToVec2(uvMin), ToVec2(uvMax), ToVec4Color(col))
c.DrawList.AddImageV(texture.id, ToVec2(pMin), ToVec2(pMax), ToVec2(uvMin), ToVec2(uvMax), ToVec4Color(col))
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.18

require (
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8
github.com/AllenDang/imgui-go v1.12.1-0.20220322114136-499bbf6a42ad
github.com/AllenDang/imgui-go v1.12.1-0.20221124025851-59b862ca5a0c
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220806181222-55e207c401ad
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b
github.com/mazznoer/csscolorparser v0.1.3
github.com/napsy/go-css v0.0.0-20221107082635-4ed403047a64
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8 h1:dKZMqib/yUDoCFigmz2agG8geZ/e3iRq304/KJXqKyw=
github.com/AllenDang/go-findfont v0.0.0-20200702051237-9f180485aeb8/go.mod h1:b4uuDd0s6KRIPa84cEEchdQ9ICh7K0OryZHbSzMca9k=
github.com/AllenDang/imgui-go v1.12.1-0.20220322114136-499bbf6a42ad h1:Kr961C2uEEAklK+jBRiZVnQH0AgS7o6pXrIgUTUUGiM=
github.com/AllenDang/imgui-go v1.12.1-0.20220322114136-499bbf6a42ad/go.mod h1:kuPs9RWleaUuK7D49bE6HPxyRA36Lp4ICKGp+5OnnbY=
github.com/AllenDang/imgui-go v1.12.1-0.20221124025851-59b862ca5a0c h1:kiXjH0n0KzOpvhgy3nDFkPmKfg4A+QWsEOwxwWy6yuI=
github.com/AllenDang/imgui-go v1.12.1-0.20221124025851-59b862ca5a0c/go.mod h1:kuPs9RWleaUuK7D49bE6HPxyRA36Lp4ICKGp+5OnnbY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -10,10 +10,8 @@ github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3/go.mod h1:VEPNJ
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 h1:zDw5v7qm4yH7N8C8uWd+8Ii9rROdgWxQuGoJ9WDXxfk=
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211213063430-748e38ca8aec/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220806181222-55e207c401ad h1:kX51IjbsJPCvzV9jUoVQG9GEUqIq5hjfYzXTqQ52Rh8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220806181222-55e207c401ad/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/gucio321/go-css v0.0.0-20220927183634-22e6096f036e h1:5FrujxF+Hl8A1vKMb91jyswjspgiDVnkkyZ0xY/cQvY=
github.com/gucio321/go-css v0.0.0-20220927183634-22e6096f036e/go.mod h1:HqZYcKcNnv50fgOTdGUn9YbJa2qC9oJ3kLnyrwwVzUI=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mazznoer/csscolorparser v0.1.3 h1:vug4zh6loQxAUxfU1DZEu70gTPufDPspamZlHAkKcxE=
Expand Down

0 comments on commit 105078d

Please sign in to comment.