forked from inkyblackness/imgui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContext.go
67 lines (58 loc) · 1.85 KB
/
Context.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
65
66
67
package imgui
import (
"errors"
)
// #include "wrapper/Context.h"
// #include "plot_wrapper/Contexts.h"
import "C"
// Context specifies a scope of ImGui/ImPlot.
//
// All contexts share a same FontAtlas by default.
// If you want different font atlas, you can create them and overwrite the CurrentIO.Fonts of an ImGui context.
type Context struct {
handle C.IggContext
plot C.igpContext
}
// CreateContext produces a new internal state scope.
// Passing nil for the fontAtlas creates a default font.
func CreateContext(fontAtlas *FontAtlas) *Context {
var fontAtlasPtr C.IggFontAtlas
if fontAtlas != nil {
fontAtlasPtr = fontAtlas.handle()
}
return &Context{handle: C.iggCreateContext(fontAtlasPtr), plot: C.igpCreateContext()}
}
// ErrNoContext is used when no context is current.
var ErrNoContext = errors.New("no current context")
// CurrentContext returns the currently active state scope.
// Returns ErrNoContext if no context is available.
func CurrentContext() (*Context, error) {
raw := C.iggGetCurrentContext()
if raw == nil {
return nil, ErrNoContext
}
return &Context{handle: raw, plot: C.igpCurrentContext()}, nil
}
// Destroy removes the internal state scope.
// Trying to destroy an already destroyed context does nothing.
func (context *Context) Destroy() {
if context.handle != nil {
C.iggDestroyContext(context.handle)
context.handle = nil
}
if context.plot != nil {
C.igpDestroyContext(context.plot)
context.plot = nil
}
}
// ErrContextDestroyed is returned when trying to use an already destroyed context.
var ErrContextDestroyed = errors.New("context is destroyed")
// SetCurrent activates this context as the currently active state scope.
func (context Context) SetCurrent() error {
if context.handle == nil {
return ErrContextDestroyed
}
C.iggSetCurrentContext(context.handle)
C.igpSetCurrentContext(context.plot)
return nil
}