forked from AllenDang/cimgui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_text.go
110 lines (87 loc) · 2.54 KB
/
input_text.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package imgui
// #include <memory.h>
// #include <stdlib.h>
// #include "extra_types.h"
// #include "cimgui_wrapper.h"
// #include "cimgui_structs_accessor.h"
// extern int generalInputTextCallback(ImGuiInputTextCallbackData* data);
import "C"
import (
"runtime/cgo"
"unsafe"
)
type InputTextCallback func(data InputTextCallbackData) int
type inputTextInternalState struct {
buf *StringBuffer
callback InputTextCallback
}
func (state *inputTextInternalState) release() {
state.buf.Free()
}
//export generalInputTextCallback
func generalInputTextCallback(cbData *C.ImGuiInputTextCallbackData) C.int {
data := newInputTextCallbackDataFromC(cbData)
bufHandle := (*cgo.Handle)(cbData.UserData)
statePtr := bufHandle.Value().(*inputTextInternalState)
if data.EventFlag() == InputTextFlagsCallbackResize {
statePtr.buf.ResizeTo(int(data.BufSize()))
C.wrap_ImGuiInputTextCallbackData_SetBuf(cbData, (*C.char)(statePtr.buf.ptr))
data.SetBufSize(int32(statePtr.buf.size))
data.SetBufTextLen(int32(data.BufTextLen()))
data.SetBufDirty(true)
}
if statePtr.callback != nil {
return C.int(statePtr.callback(*data))
}
return 0
}
func InputTextWithHint(label, hint string, buf *string, flags InputTextFlags, callback InputTextCallback) bool {
labelArg, labelFin := WrapString(label)
defer labelFin()
hintArg, hintFin := WrapString(hint)
defer hintFin()
state := &inputTextInternalState{
buf: NewStringBuffer(*buf),
callback: callback,
}
defer func() {
*buf = state.buf.ToGo()
state.release()
}()
stateHandle := cgo.NewHandle(state)
defer stateHandle.Delete()
flags |= InputTextFlagsCallbackResize
return C.igInputTextWithHint(
labelArg,
hintArg,
(*C.char)(state.buf.ptr),
C.xulong(len(*buf)+1),
C.ImGuiInputTextFlags(flags),
C.ImGuiInputTextCallback(C.generalInputTextCallback),
unsafe.Pointer(&stateHandle),
) == C.bool(true)
}
func InputTextMultiline(label string, buf *string, size Vec2, flags InputTextFlags, callback InputTextCallback) bool {
labelArg, labelFin := WrapString(label)
defer labelFin()
state := &inputTextInternalState{
buf: NewStringBuffer(*buf),
callback: callback,
}
defer func() {
*buf = state.buf.ToGo()
state.release()
}()
stateHandle := cgo.NewHandle(state)
defer stateHandle.Delete()
flags |= InputTextFlagsCallbackResize
return C.igInputTextMultiline(
labelArg,
(*C.char)(state.buf.ptr),
C.xulong(len(*buf)+1),
size.toC(),
C.ImGuiInputTextFlags(flags),
C.ImGuiInputTextCallback(C.generalInputTextCallback),
unsafe.Pointer(&stateHandle),
) == C.bool(true)
}