-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contexts.go
64 lines (54 loc) · 1.69 KB
/
Contexts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package implot
// #include "wrapper/Contexts.h"
import "C"
import (
"errors"
"unsafe"
"github.com/inkyblackness/imgui-go/v4"
)
// Context specifies a scope; a global state for ImPlot.
type Context struct {
handle C.igpContext
}
// CreateContext creates a new ImPlot context.
// It should be called right after imgui.CreateContext().
func CreateContext() *Context {
return &Context{handle: C.igpCreateContext()}
}
// ErrNoContext is used when no context is current.
// It should not be the same with imgui.ErrNoContext.
var ErrNoContext = errors.New("no current context")
// CurrentContext returns the currently active context.
// Returns ErrNoContext if no context is available.
func CurrentContext() (*Context, error) {
raw := C.igpCurrentContext()
if raw == nil {
return nil, ErrNoContext
}
return &Context{handle: raw}, nil
}
// ErrContextDestroyed is returned when trying to use an already destroyed context.
// It should not be the same with imgui.ErrContextDestroyed.
var ErrContextDestroyed = errors.New("context is destroyed")
// Destroy deletes the context.
// Destroying an already destroyed context does nothing.
func (c *Context) Destroy() {
if c.handle != nil {
C.igpDestroyContext(c.handle)
c.handle = nil
}
}
// SetCurrent activates this context as the current active one.
func (c *Context) SetCurrent() error {
if c.handle == nil {
return ErrContextDestroyed
}
C.igpSetCurrentContext(c.handle)
return nil
}
// SetImGUIContext sets the current ImGUI context for ImPlot.
//
// It should not be used, at least with the current way Go links libraries.
func SetImGUIContext(ig *imgui.Context) {
C.igpSetImGUIContext(C.iggContext(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(ig)))))
}