DrawList.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package imgui
  2. // #include "DrawListWrapper.h"
  3. import "C"
  4. import (
  5. "unsafe"
  6. )
  7. // DrawList is a draw-command list.
  8. // This is the low-level list of polygons that ImGui functions are filling.
  9. // At the end of the frame, all command lists are passed to your render function for rendering.
  10. //
  11. // Each ImGui window contains its own DrawList. You can use GetWindowDrawList() to access
  12. // the current window draw list and draw custom primitives.
  13. //
  14. // You can interleave normal ImGui calls and adding primitives to the current draw list.
  15. //
  16. // All positions are generally in pixel coordinates (top-left at (0,0), bottom-right at io.DisplaySize),
  17. // however you are totally free to apply whatever transformation matrix to want to the data
  18. // (if you apply such transformation you'll want to apply it to ClipRect as well)
  19. //
  20. // Important: Primitives are always added to the list and not culled (culling is done at
  21. // higher-level by ImGui functions), if you use this API a lot consider coarse culling your drawn objects.
  22. type DrawList uintptr
  23. func (list DrawList) handle() C.IggDrawList {
  24. return C.IggDrawList(list)
  25. }
  26. // Commands returns the list of draw commands.
  27. // Typically 1 command = 1 GPU draw call, unless the command is a callback.
  28. func (list DrawList) Commands() []DrawCommand {
  29. count := int(C.iggDrawListGetCommandCount(list.handle()))
  30. commands := make([]DrawCommand, count)
  31. for i := 0; i < count; i++ {
  32. commands[i] = DrawCommand(C.iggDrawListGetCommand(list.handle(), C.int(i)))
  33. }
  34. return commands
  35. }
  36. // VertexBufferLayout returns the byte sizes necessary to select fields in a vertex buffer of a DrawList.
  37. func VertexBufferLayout() (entrySize int, posOffset int, uvOffset int, colOffset int) {
  38. var entrySizeArg C.size_t
  39. var posOffsetArg C.size_t
  40. var uvOffsetArg C.size_t
  41. var colOffsetArg C.size_t
  42. C.iggGetVertexBufferLayout(&entrySizeArg, &posOffsetArg, &uvOffsetArg, &colOffsetArg)
  43. entrySize = int(entrySizeArg)
  44. posOffset = int(posOffsetArg)
  45. uvOffset = int(uvOffsetArg)
  46. colOffset = int(colOffsetArg)
  47. return
  48. }
  49. // VertexBuffer returns the handle information of the whole vertex buffer.
  50. // Returned are the handle pointer and the total byte size.
  51. // The buffer is a packed array of vertex entries, each consisting of a 2D position vector, a 2D UV vector,
  52. // and a 4-byte color value. To determine the byte size and offset values, call VertexBufferLayout.
  53. func (list DrawList) VertexBuffer() (unsafe.Pointer, int) {
  54. var data unsafe.Pointer
  55. var size C.int
  56. C.iggDrawListGetRawVertexBuffer(list.handle(), &data, &size)
  57. return data, int(size)
  58. }
  59. // IndexBufferLayout returns the byte size necessary to select fields in an index buffer of DrawList.
  60. func IndexBufferLayout() (entrySize int) {
  61. var entrySizeArg C.size_t
  62. C.iggGetIndexBufferLayout(&entrySizeArg)
  63. entrySize = int(entrySizeArg)
  64. return
  65. }
  66. // IndexBuffer returns the handle information of the whole index buffer.
  67. // Returned are the handle pointer and the total byte size.
  68. // The buffer is a packed array of index entries, each consisting of an integer offset.
  69. // To determine the byte size, call IndexBufferLayout.
  70. func (list DrawList) IndexBuffer() (unsafe.Pointer, int) {
  71. var data unsafe.Pointer
  72. var size C.int
  73. C.iggDrawListGetRawIndexBuffer(list.handle(), &data, &size)
  74. return data, int(size)
  75. }
  76. func (list DrawList) AddLine(p1, p2 Vec2, col Vec4, thickness float32) {
  77. c := GetColorU32(col)
  78. p1Arg, _ := p1.wrapped()
  79. p2Arg, _ := p2.wrapped()
  80. C.iggDrawListAddLine(list.handle(), p1Arg, p2Arg, C.uint(c), C.float(thickness))
  81. }
  82. func (list DrawList) AddRect(pMin, pMax Vec2, col Vec4, rounding float32, rounding_corners int, thickness float32) {
  83. c := GetColorU32(col)
  84. pMinArg, _ := pMin.wrapped()
  85. pMaxArg, _ := pMax.wrapped()
  86. C.iggDrawListAddRect(list.handle(), pMinArg, pMaxArg, C.uint(c), C.float(rounding), C.int(rounding_corners), C.float(thickness))
  87. }
  88. func (list DrawList) AddRectFilled(pMin, pMax Vec2, col Vec4, rounding float32, rounding_corners int) {
  89. c := GetColorU32(col)
  90. pMinArg, _ := pMin.wrapped()
  91. pMaxArg, _ := pMax.wrapped()
  92. C.iggDrawListAddRectFilled(list.handle(), pMinArg, pMaxArg, C.uint(c), C.float(rounding), C.int(rounding_corners))
  93. }
  94. func (list DrawList) AddText(pos Vec2, col Vec4, text string) {
  95. c := GetColorU32(col)
  96. posArg, _ := pos.wrapped()
  97. textArg, textFin := wrapString(text)
  98. defer textFin()
  99. C.iggDrawListAddText(list.handle(), posArg, C.uint(c), textArg)
  100. }
  101. func (list DrawList) AddBezierCubic(pos0, cp0, cp1, pos1 Vec2, col Vec4, thickness float32, num_segments int) {
  102. c := GetColorU32(col)
  103. pos0Arg, _ := pos0.wrapped()
  104. cp0Arg, _ := cp0.wrapped()
  105. cp1Arg, _ := cp1.wrapped()
  106. pos1Arg, _ := pos1.wrapped()
  107. C.iggDrawListAddBezierCubic(list.handle(), pos0Arg, cp0Arg, cp1Arg, pos1Arg, C.uint(c), C.float(thickness), C.int(num_segments))
  108. }
  109. func (list DrawList) AddTriangle(p1, p2, p3 Vec2, col Vec4, thickness float32) {
  110. c := GetColorU32(col)
  111. p1Arg, _ := p1.wrapped()
  112. p2Arg, _ := p2.wrapped()
  113. p3Arg, _ := p3.wrapped()
  114. C.iggDrawListAddTriangle(list.handle(), p1Arg, p2Arg, p3Arg, C.uint(c), C.float(thickness))
  115. }
  116. func (list DrawList) AddTriangleFilled(p1, p2, p3 Vec2, col Vec4) {
  117. c := GetColorU32(col)
  118. p1Arg, _ := p1.wrapped()
  119. p2Arg, _ := p2.wrapped()
  120. p3Arg, _ := p3.wrapped()
  121. C.iggDrawListAddTriangleFilled(list.handle(), p1Arg, p2Arg, p3Arg, C.uint(c))
  122. }
  123. func (list DrawList) AddCircle(center Vec2, radius float32, col Vec4, segments int, thickness float32) {
  124. c := GetColorU32(col)
  125. centerArg, _ := center.wrapped()
  126. C.iggDrawListAddCircle(list.handle(), centerArg, C.float(radius), C.uint(c), C.int(segments), C.float(thickness))
  127. }
  128. func (list DrawList) AddCircleFilled(center Vec2, radius float32, col Vec4) {
  129. c := GetColorU32(col)
  130. centerArg, _ := center.wrapped()
  131. C.iggDrawListAddCircleFilled(list.handle(), centerArg, C.float(radius), C.uint(c), 0)
  132. }
  133. func (list DrawList) AddQuad(p1, p2, p3, p4 Vec2, col Vec4, thickness float32) {
  134. c := GetColorU32(col)
  135. p1Arg, _ := p1.wrapped()
  136. p2Arg, _ := p2.wrapped()
  137. p3Arg, _ := p3.wrapped()
  138. p4Arg, _ := p4.wrapped()
  139. C.iggDrawListAddQuad(list.handle(), p1Arg, p2Arg, p3Arg, p4Arg, C.uint(c), C.float(thickness))
  140. }
  141. func (list DrawList) AddQuadFilled(p1, p2, p3, p4 Vec2, col Vec4) {
  142. c := GetColorU32(col)
  143. p1Arg, _ := p1.wrapped()
  144. p2Arg, _ := p2.wrapped()
  145. p3Arg, _ := p3.wrapped()
  146. p4Arg, _ := p4.wrapped()
  147. C.iggDrawListAddQuadFilled(list.handle(), p1Arg, p2Arg, p3Arg, p4Arg, C.uint(c))
  148. }
  149. // Stateful path API, add points then finish with PathFillConvex() or PathStroke()
  150. func (list DrawList) PathClear() {
  151. C.iggDrawListPathClear(list.handle())
  152. }
  153. func (list DrawList) PathLineTo(pos Vec2) {
  154. posArg, _ := pos.wrapped()
  155. C.iggDrawListPathLineTo(list.handle(), posArg)
  156. }
  157. func (list DrawList) PathLineToMergeDuplicate(pos Vec2) {
  158. posArg, _ := pos.wrapped()
  159. C.iggDrawListPathLineToMergeDuplicate(list.handle(), posArg)
  160. }
  161. func (list DrawList) PathFillConvex(col Vec4) {
  162. C.iggDrawListPathFillConvex(list.handle(), C.uint(GetColorU32(col)))
  163. }
  164. func (list DrawList) PathStroke(col Vec4, closed bool, thickness float32) {
  165. C.iggDrawListPathStroke(list.handle(), C.uint(GetColorU32(col)), castBool(closed), C.float(thickness))
  166. }
  167. func (list DrawList) PathArcTo(center Vec2, radius, a_min, a_max float32, num_segments int) {
  168. centerArg, _ := center.wrapped()
  169. C.iggDrawListPathArcTo(list.handle(), centerArg, C.float(radius), C.float(a_min), C.float(a_max), C.int(num_segments))
  170. }
  171. func (list DrawList) PathArcToFast(center Vec2, radius float32, a_min_of_12, a_max_of_12 int) {
  172. centerArg, _ := center.wrapped()
  173. C.iggDrawListPathArcToFast(list.handle(), centerArg, C.float(radius), C.int(a_min_of_12), C.int(a_max_of_12))
  174. }
  175. func (list DrawList) PathBezierCubicCurveTo(p1, p2, p3 Vec2, num_segments int) {
  176. p1Arg, _ := p1.wrapped()
  177. p2Arg, _ := p2.wrapped()
  178. p3Arg, _ := p3.wrapped()
  179. C.iggDrawListPathBezierCubicCurveTo(list.handle(), p1Arg, p2Arg, p3Arg, C.int(num_segments))
  180. }
  181. func (list DrawList) AddImage(textureId TextureID, pMin, pMax Vec2) {
  182. pMinArg, _ := pMin.wrapped()
  183. pMaxArg, _ := pMax.wrapped()
  184. C.iggDrawListAddImage(list.handle(), C.IggTextureID(textureId), pMinArg, pMaxArg)
  185. }
  186. func (list DrawList) AddImageV(textureId TextureID, pMin, pMax Vec2, uvMin, uvMax Vec2, col Vec4) {
  187. c := GetColorU32(col)
  188. pMinArg, _ := pMin.wrapped()
  189. pMaxArg, _ := pMax.wrapped()
  190. uvMinArg, _ := uvMin.wrapped()
  191. uvMaxArg, _ := uvMax.wrapped()
  192. C.iggDrawListAddImageV(list.handle(), C.IggTextureID(textureId), pMinArg, pMaxArg, uvMinArg, uvMaxArg, C.uint(c))
  193. }