Context.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package imgui
  2. import (
  3. "errors"
  4. )
  5. // #include "imguiWrapper.h"
  6. import "C"
  7. // Context specifies a scope of ImGui.
  8. //
  9. // All contexts share a same FontAtlas by default.
  10. // If you want different font atlas, you can create them and overwrite the CurrentIO.Fonts of an ImGui context.
  11. type Context struct {
  12. handle C.IggContext
  13. }
  14. // CreateContext produces a new internal state scope.
  15. // Passing nil for the fontAtlas creates a default font.
  16. func CreateContext(fontAtlas *FontAtlas) *Context {
  17. var fontAtlasPtr C.IggFontAtlas
  18. if fontAtlas != nil {
  19. fontAtlasPtr = fontAtlas.handle()
  20. }
  21. return &Context{handle: C.iggCreateContext(fontAtlasPtr)}
  22. }
  23. func (c *Context) GetHandle() C.IggContext {
  24. return c.handle
  25. }
  26. // ErrNoContext is used when no context is current.
  27. var ErrNoContext = errors.New("no current context")
  28. // CurrentContext returns the currently active state scope.
  29. // Returns ErrNoContext if no context is available.
  30. func CurrentContext() (*Context, error) {
  31. raw := C.iggGetCurrentContext()
  32. if raw == nil {
  33. return nil, ErrNoContext
  34. }
  35. return &Context{handle: raw}, nil
  36. }
  37. // Destroy removes the internal state scope.
  38. // Trying to destroy an already destroyed context does nothing.
  39. func (context *Context) Destroy() {
  40. if context.handle != nil {
  41. C.iggDestroyContext(context.handle)
  42. context.handle = nil
  43. }
  44. }
  45. // ErrContextDestroyed is returned when trying to use an already destroyed context.
  46. var ErrContextDestroyed = errors.New("context is destroyed")
  47. // SetCurrent activates this context as the currently active state scope.
  48. func (context Context) SetCurrent() error {
  49. if context.handle == nil {
  50. return ErrContextDestroyed
  51. }
  52. C.iggSetCurrentContext(context.handle)
  53. return nil
  54. }
  55. func SetMaxWaitBeforeNextFrame(time float32) {
  56. C.iggSetMaxWaitBeforeNextFrame(C.double(time))
  57. }