IO.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package imgui
  2. // #include "IOWrapper.h"
  3. import "C"
  4. // IO is where your app communicate with ImGui. Access via CurrentIO().
  5. // Read 'Programmer guide' section in imgui.cpp file for general usage.
  6. type IO struct {
  7. handle C.IggIO
  8. }
  9. // WantCaptureMouse returns true if imgui will use the mouse inputs.
  10. // Do not dispatch them to your main game/application in this case.
  11. // In either case, always pass on mouse inputs to imgui.
  12. // (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.)
  13. func (io IO) WantCaptureMouse() bool {
  14. return C.iggWantCaptureMouse(io.handle) != 0
  15. }
  16. // WantCaptureKeyboard returns true if imgui will use the keyboard inputs.
  17. // Do not dispatch them to your main game/application (in both cases, always pass keyboard inputs to imgui).
  18. // (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.).
  19. func (io IO) WantCaptureKeyboard() bool {
  20. return C.iggWantCaptureKeyboard(io.handle) != 0
  21. }
  22. // WantTextInput is true, you may display an on-screen keyboard.
  23. // This is set by ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active).
  24. func (io IO) WantTextInput() bool {
  25. return C.iggWantTextInput(io.handle) != 0
  26. }
  27. // SetDisplaySize sets the size in pixels.
  28. func (io IO) SetDisplaySize(value Vec2) {
  29. out, _ := value.wrapped()
  30. C.iggIoSetDisplaySize(io.handle, out)
  31. }
  32. func (io IO) GetMouseDrawCursor() bool {
  33. return C.iggIoGetMouseDrawCursor(io.handle) != 0
  34. }
  35. func (io IO) SetMouseDrawCursor(b bool) {
  36. C.iggIoSetMouseDrawCursor(io.handle, castBool(b))
  37. }
  38. // Fonts returns the font atlas to load and assemble one or more fonts into a single tightly packed texture.
  39. func (io IO) Fonts() FontAtlas {
  40. return FontAtlas(C.iggIoGetFonts(io.handle))
  41. }
  42. // SetMousePosition sets the mouse position, in pixels.
  43. // Set to Vec2(-math.MaxFloat32,-mathMaxFloat32) if mouse is unavailable (on another screen, etc.)
  44. func (io IO) SetMousePosition(value Vec2) {
  45. posArg, _ := value.wrapped()
  46. C.iggIoSetMousePosition(io.handle, posArg)
  47. }
  48. // SetMouseButtonDown sets whether a specific mouse button is currently pressed.
  49. // Mouse buttons: left, right, middle + extras.
  50. // ImGui itself mostly only uses left button (BeginPopupContext** are using right button).
  51. // Other buttons allows us to track if the mouse is being used by your application +
  52. // available to user as a convenience via IsMouse** API.
  53. func (io IO) SetMouseButtonDown(index int, down bool) {
  54. var downArg C.IggBool
  55. if down {
  56. downArg = 1
  57. }
  58. C.iggIoSetMouseButtonDown(io.handle, C.int(index), downArg)
  59. }
  60. // AddMouseWheelDelta adds the given offsets to the current mouse wheel values.
  61. // 1 vertical unit scrolls about 5 lines text.
  62. // Most users don't have a mouse with an horizontal wheel, may not be provided by all back-ends.
  63. func (io IO) AddMouseWheelDelta(horizontal, vertical float32) {
  64. C.iggIoAddMouseWheelDelta(io.handle, C.float(horizontal), C.float(vertical))
  65. }
  66. // GetMouseWheelDelta returns the vertical delta of the mouse wheel.
  67. func (io IO) GetMouseWheelDelta() float32 {
  68. var delta float32
  69. deltaArg, deltaFin := wrapFloat(&delta)
  70. C.iggIoGetMouseWheelDelta(io.handle, deltaArg)
  71. deltaFin()
  72. return delta
  73. }
  74. // GetMouseWheelHDelta returns the horizontal delta of the mouse wheel.
  75. func (io IO) GetMouseWheelHDelta() float32 {
  76. var delta float32
  77. deltaArg, deltaFin := wrapFloat(&delta)
  78. C.iggIoGetMouseWheelHDelta(io.handle, deltaArg)
  79. deltaFin()
  80. return delta
  81. }
  82. func (io IO) GetMouseDelta() Vec2 {
  83. var delta Vec2
  84. deltaArg, deltaFin := delta.wrapped()
  85. C.iggIoGetMouseDelta(io.handle, deltaArg)
  86. deltaFin()
  87. return delta
  88. }
  89. // SetDeltaTime sets the time elapsed since last frame, in seconds.
  90. func (io IO) SetDeltaTime(value float32) {
  91. C.iggIoSetDeltaTime(io.handle, C.float(value))
  92. }
  93. // SetFontGlobalScale sets the global scaling factor for all fonts.
  94. func (io IO) SetFontGlobalScale(value float32) {
  95. C.iggIoSetFontGlobalScale(io.handle, C.float(value))
  96. }
  97. // KeyPress sets the KeysDown flag.
  98. func (io IO) KeyPress(key int) {
  99. C.iggIoKeyPress(io.handle, C.int(key))
  100. }
  101. // KeyRelease clears the KeysDown flag.
  102. func (io IO) KeyRelease(key int) {
  103. C.iggIoKeyRelease(io.handle, C.int(key))
  104. }
  105. // KeyMap maps a key into the KeysDown array which represents your "native" keyboard state.
  106. func (io IO) KeyMap(imguiKey int, nativeKey int) {
  107. C.iggIoKeyMap(io.handle, C.int(imguiKey), C.int(nativeKey))
  108. }
  109. // KeyCtrl sets the keyboard modifier control pressed.
  110. func (io IO) KeyCtrl(leftCtrl int, rightCtrl int) {
  111. C.iggIoKeyCtrl(io.handle, C.int(leftCtrl), C.int(rightCtrl))
  112. }
  113. // KeyShift sets the keyboard modifier shift pressed.
  114. func (io IO) KeyShift(leftShift int, rightShift int) {
  115. C.iggIoKeyShift(io.handle, C.int(leftShift), C.int(rightShift))
  116. }
  117. // KeyAlt sets the keyboard modifier alt pressed.
  118. func (io IO) KeyAlt(leftAlt int, rightAlt int) {
  119. C.iggIoKeyAlt(io.handle, C.int(leftAlt), C.int(rightAlt))
  120. }
  121. // KeySuper sets the keyboard modifier super pressed.
  122. func (io IO) KeySuper(leftSuper int, rightSuper int) {
  123. C.iggIoKeySuper(io.handle, C.int(leftSuper), C.int(rightSuper))
  124. }
  125. // AddInputCharacters adds a new character into InputCharacters[].
  126. func (io IO) AddInputCharacters(chars string) {
  127. textArg, textFin := wrapString(chars)
  128. defer textFin()
  129. C.iggIoAddInputCharactersUTF8(io.handle, textArg)
  130. }
  131. // SetIniFilename changes the filename for the settings. Default: "imgui.ini".
  132. // Use an empty string to disable the ini from being used.
  133. func (io IO) SetIniFilename(value string) {
  134. valueArg, valueFin := wrapString(value)
  135. defer valueFin()
  136. C.iggIoSetIniFilename(io.handle, valueArg)
  137. }
  138. // SetConfigFlags sets the gamepad/keyboard navigation options, etc.
  139. func (io IO) SetConfigFlags(flags int) {
  140. C.iggIoSetConfigFlags(io.handle, C.int(flags))
  141. }
  142. func (io IO) GetConfigFlags() int {
  143. return int(C.iggIoGetConfigFlags(io.handle))
  144. }
  145. // SetBackendFlags sets back-end capabilities.
  146. func (io IO) SetBackendFlags(flags int) {
  147. C.iggIoSetBackendFlags(io.handle, C.int(flags))
  148. }
  149. func (io IO) GetFrameCountSinceLastInput() int {
  150. return int(C.iggGetFrameCountSinceLastInput(io.handle))
  151. }
  152. func (io IO) SetFrameCountSinceLastInput(count int) {
  153. C.iggSetFrameCountSinceLastInput(io.handle, C.int(count))
  154. }
  155. func (io IO) AddFocusEvent(focused bool) {
  156. C.iggIoAddFocusEvent(io.handle, castBool(focused))
  157. }
  158. // Clipboard describes the access to the text clipboard of the window manager.
  159. type Clipboard interface {
  160. // Text returns the current text from the clipboard, if available.
  161. Text() (string, error)
  162. // SetText sets the text as the current text on the clipboard.
  163. SetText(value string)
  164. }
  165. var clipboards = map[C.IggIO]Clipboard{}
  166. var dropLastClipboardText = func() {}
  167. // SetClipboard registers a clipboard for text copy/paste actions.
  168. // If no clipboard is set, then a fallback implementation may be used, if available for the OS.
  169. // To disable clipboard handling overall, pass nil as the Clipboard.
  170. //
  171. // Since ImGui queries the clipboard text via a return value, the wrapper has to hold the
  172. // current clipboard text as a copy in memory. This memory will be freed at the next clipboard operation.
  173. func (io IO) SetClipboard(board Clipboard) {
  174. dropLastClipboardText()
  175. if board != nil {
  176. clipboards[io.handle] = board
  177. C.iggIoRegisterClipboardFunctions(io.handle)
  178. } else {
  179. C.iggIoClearClipboardFunctions(io.handle)
  180. delete(clipboards, io.handle)
  181. }
  182. }
  183. //export iggIoGetClipboardText
  184. func iggIoGetClipboardText(handle C.IggIO) *C.char {
  185. dropLastClipboardText()
  186. board := clipboards[handle]
  187. if board == nil {
  188. return nil
  189. }
  190. text, err := board.Text()
  191. if err != nil {
  192. return nil
  193. }
  194. textPtr, textFin := wrapString(text)
  195. dropLastClipboardText = func() {
  196. dropLastClipboardText = func() {}
  197. textFin()
  198. }
  199. return textPtr
  200. }
  201. //export iggIoSetClipboardText
  202. func iggIoSetClipboardText(handle C.IggIO, text *C.char) {
  203. dropLastClipboardText()
  204. board := clipboards[handle]
  205. if board == nil {
  206. return
  207. }
  208. board.SetText(C.GoString(text))
  209. }