Canvas.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package giu
  2. import (
  3. "image"
  4. "image/color"
  5. "github.com/AllenDang/imgui-go"
  6. )
  7. // Canvas represents imgui.DrawList
  8. // from imgui.h:
  9. // A single draw command list (generally one per window,
  10. // conceptually you may see this as a dynamic "mesh" builder)
  11. //
  12. // for more details and use cases see examples/canvas.
  13. type Canvas struct {
  14. drawlist imgui.DrawList
  15. }
  16. // GetCanvas returns current draw list (for current window).
  17. // it will fail if called out of window's layout.
  18. func GetCanvas() *Canvas {
  19. return &Canvas{
  20. drawlist: imgui.GetWindowDrawList(),
  21. }
  22. }
  23. // AddLine draws a line (from p1 to p2).
  24. func (c *Canvas) AddLine(p1, p2 image.Point, col color.Color, thickness float32) {
  25. c.drawlist.AddLine(ToVec2(p1), ToVec2(p2), ToVec4Color(col), thickness)
  26. }
  27. // DrawFlags represents imgui.DrawFlags.
  28. type DrawFlags int
  29. // draw flags enum:.
  30. const (
  31. DrawFlagsNone DrawFlags = 0
  32. // PathStroke(), AddPolyline(): specify that shape should be closed (portant: this is always == 1 for legacy reason).
  33. DrawFlagsClosed DrawFlags = 1 << 0
  34. // AddRect(), AddRectFilled(), PathRect(): enable rounding top-left corner only (when rounding > 0.0f, we default to all corners).
  35. // Was 0x01.
  36. DrawFlagsRoundCornersTopLeft DrawFlags = 1 << 4
  37. // AddRect(), AddRectFilled(), PathRect(): enable rounding top-right corner only (when rounding > 0.0f, we default to all corners).
  38. // Was 0x02.
  39. DrawFlagsRoundCornersTopRight DrawFlags = 1 << 5
  40. // AddRect(), AddRectFilled(), PathRect(): enable rounding bottom-left corner only (when rounding > 0.0f, we default to all corners).
  41. // Was 0x04.
  42. DrawFlagsRoundCornersBottomLeft DrawFlags = 1 << 6
  43. // AddRect(), AddRectFilled(), PathRect(): enable rounding bottom-right corner only (when rounding > 0.0f,
  44. // we default to all corners). Wax 0x08.
  45. DrawFlagsRoundCornersBottomRight DrawFlags = 1 << 7
  46. // AddRect(), AddRectFilled(), PathRect(): disable rounding on all corners (when rounding > 0.0f). This is NOT zero, NOT an implicit flag!
  47. DrawFlagsRoundCornersNone DrawFlags = 1 << 8
  48. DrawFlagsRoundCornersTop DrawFlags = DrawFlagsRoundCornersTopLeft | DrawFlagsRoundCornersTopRight
  49. DrawFlagsRoundCornersBottom DrawFlags = DrawFlagsRoundCornersBottomLeft | DrawFlagsRoundCornersBottomRight
  50. DrawFlagsRoundCornersLeft DrawFlags = DrawFlagsRoundCornersBottomLeft | DrawFlagsRoundCornersTopLeft
  51. DrawFlagsRoundCornersRight DrawFlags = DrawFlagsRoundCornersBottomRight | DrawFlagsRoundCornersTopRight
  52. DrawFlagsRoundCornersAll DrawFlags = DrawFlagsRoundCornersTopLeft | DrawFlagsRoundCornersTopRight |
  53. DrawFlagsRoundCornersBottomLeft | DrawFlagsRoundCornersBottomRight
  54. // Default to ALL corners if none of the RoundCornersXX flags are specified.
  55. DrawFlagsRoundCornersDefault DrawFlags = DrawFlagsRoundCornersAll
  56. DrawFlagsRoundCornersMask DrawFlags = DrawFlagsRoundCornersAll | DrawFlagsRoundCornersNone
  57. )
  58. // AddRect draws a rectangle.
  59. func (c *Canvas) AddRect(pMin, pMax image.Point, col color.Color, rounding float32, roundingCorners DrawFlags, thickness float32) {
  60. c.drawlist.AddRect(ToVec2(pMin), ToVec2(pMax), ToVec4Color(col), rounding, int(roundingCorners), thickness)
  61. }
  62. // AddRectFilled draws a rectangle filled with `col`.
  63. func (c *Canvas) AddRectFilled(pMin, pMax image.Point, col color.Color, rounding float32, roundingCorners DrawFlags) {
  64. c.drawlist.AddRectFilled(ToVec2(pMin), ToVec2(pMax), ToVec4Color(col), rounding, int(roundingCorners))
  65. }
  66. // AddText draws text.
  67. func (c *Canvas) AddText(pos image.Point, col color.Color, text string) {
  68. c.drawlist.AddText(ToVec2(pos), ToVec4Color(col), tStr(text))
  69. }
  70. // AddBezierCubic draws bezier cubic.
  71. func (c *Canvas) AddBezierCubic(pos0, cp0, cp1, pos1 image.Point, col color.Color, thickness float32, numSegments int) {
  72. c.drawlist.AddBezierCubic(ToVec2(pos0), ToVec2(cp0), ToVec2(cp1), ToVec2(pos1), ToVec4Color(col), thickness, numSegments)
  73. }
  74. // AddTriangle draws a triangle.
  75. func (c *Canvas) AddTriangle(p1, p2, p3 image.Point, col color.Color, thickness float32) {
  76. c.drawlist.AddTriangle(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec4Color(col), thickness)
  77. }
  78. // AddTriangleFilled draws a filled triangle.
  79. func (c *Canvas) AddTriangleFilled(p1, p2, p3 image.Point, col color.Color) {
  80. c.drawlist.AddTriangleFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec4Color(col))
  81. }
  82. // AddCircle draws a circle.
  83. func (c *Canvas) AddCircle(center image.Point, radius float32, col color.Color, segments int, thickness float32) {
  84. c.drawlist.AddCircle(ToVec2(center), radius, ToVec4Color(col), segments, thickness)
  85. }
  86. // AddCircleFilled draws a filled circle.
  87. func (c *Canvas) AddCircleFilled(center image.Point, radius float32, col color.Color) {
  88. c.drawlist.AddCircleFilled(ToVec2(center), radius, ToVec4Color(col))
  89. }
  90. // AddQuad draws a quad.
  91. func (c *Canvas) AddQuad(p1, p2, p3, p4 image.Point, col color.Color, thickness float32) {
  92. c.drawlist.AddQuad(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ToVec4Color(col), thickness)
  93. }
  94. // AddQuadFilled draws a filled quad.
  95. func (c *Canvas) AddQuadFilled(p1, p2, p3, p4 image.Point, col color.Color) {
  96. c.drawlist.AddQuadFilled(ToVec2(p1), ToVec2(p2), ToVec2(p3), ToVec2(p4), ToVec4Color(col))
  97. }
  98. // Stateful path API, add points then finish with PathFillConvex() or PathStroke().
  99. func (c *Canvas) PathClear() {
  100. c.drawlist.PathClear()
  101. }
  102. func (c *Canvas) PathLineTo(pos image.Point) {
  103. c.drawlist.PathLineTo(ToVec2(pos))
  104. }
  105. func (c *Canvas) PathLineToMergeDuplicate(pos image.Point) {
  106. c.drawlist.PathLineToMergeDuplicate(ToVec2(pos))
  107. }
  108. func (c *Canvas) PathFillConvex(col color.Color) {
  109. c.drawlist.PathFillConvex(ToVec4Color(col))
  110. }
  111. func (c *Canvas) PathStroke(col color.Color, closed bool, thickness float32) {
  112. c.drawlist.PathStroke(ToVec4Color(col), closed, thickness)
  113. }
  114. func (c *Canvas) PathArcTo(center image.Point, radius, min, max float32, numSegments int) {
  115. c.drawlist.PathArcTo(ToVec2(center), radius, min, max, numSegments)
  116. }
  117. func (c *Canvas) PathArcToFast(center image.Point, radius float32, min12, max12 int) {
  118. c.drawlist.PathArcToFast(ToVec2(center), radius, min12, max12)
  119. }
  120. func (c *Canvas) PathBezierCubicCurveTo(p1, p2, p3 image.Point, numSegments int) {
  121. c.drawlist.PathBezierCubicCurveTo(ToVec2(p1), ToVec2(p2), ToVec2(p3), numSegments)
  122. }
  123. func (c *Canvas) AddImage(texture *Texture, pMin, pMax image.Point) {
  124. c.drawlist.AddImage(texture.id, ToVec2(pMin), ToVec2(pMax))
  125. }
  126. func (c *Canvas) AddImageV(texture *Texture, pMin, pMax, uvMin, uvMax image.Point, col color.Color) {
  127. c.drawlist.AddImageV(texture.id, ToVec2(pMin), ToVec2(pMax), ToVec2(uvMin), ToVec2(uvMax), ToVec4Color(col))
  128. }