forked from AllenDang/cimgui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra_types.go
254 lines (210 loc) · 5.21 KB
/
extra_types.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package imgui
// #include "cimgui_wrapper.h"
// #include "cimplot_wrapper.h"
// #include "extra_types.h"
import "C"
import (
"image/color"
"runtime"
"time"
)
type (
Wchar C.uint
ID C.ImGuiID
TextureID C.ImTextureID
DrawIdx C.ImDrawIdx
TableColumnIdx C.ImGuiTableColumnIdx
TableDrawChannelIdx C.ImGuiTableDrawChannelIdx
KeyChord C.ImGuiKeyChord
)
var _ wrappableType[C.ImVec2, *Vec2] = &Vec2{}
type Vec2 struct {
X float32
Y float32
}
func NewVec2(x, y float32) Vec2 {
return Vec2{X: x, Y: y}
}
func (i *Vec2) fromC(vec2 C.ImVec2) *Vec2 {
*i = NewVec2(float32(vec2.x), float32(vec2.y))
return i
}
func (i Vec2) toC() C.ImVec2 {
return C.ImVec2{x: C.float(i.X), y: C.float(i.Y)}
}
var _ wrappableType[C.ImVec4, *Vec4] = &Vec4{}
type Vec4 struct {
X float32
Y float32
Z float32
W float32
}
func NewVec4(r, g, b, a float32) Vec4 {
return Vec4{
X: r,
Y: g,
Z: b,
W: a,
}
}
func (i *Vec4) fromC(vec4 C.ImVec4) *Vec4 {
*i = NewVec4(float32(vec4.x), float32(vec4.y), float32(vec4.z), float32(vec4.w))
return i
}
func (i Vec4) toC() C.ImVec4 {
return C.ImVec4{x: C.float(i.X), y: C.float(i.Y), z: C.float(i.Z), w: C.float(i.W)}
}
var _ wrappableType[C.ImColor, *Color] = &Color{}
type Color struct {
FieldValue Vec4
}
func NewColor(r, g, b, a float32) Color {
return Color{
FieldValue: Vec4{
X: r,
Y: g,
Z: b,
W: a,
},
}
}
func NewColorFromPacked(v uint32) Color {
return NewColor(
float32(0xff&(v>>0))/255,
float32(0xff&(v>>8))/255,
float32(0xff&(v>>16))/255,
float32(0xff&(v>>24))/255,
)
}
func NewColorFromColor(c color.Color) Color {
nc := color.NRGBAModel.Convert(c).(color.NRGBA)
return NewColor(
float32(nc.R)/0xffff,
float32(nc.G)/0xffff,
float32(nc.B)/0xffff,
float32(nc.A)/0xffff,
)
}
func (i *Color) fromC(col C.ImColor) *Color {
*i = NewColor(float32(col.Value.x), float32(col.Value.y), float32(col.Value.z), float32(col.Value.w))
return i
}
func (i Color) toC() C.ImColor {
return C.ImColor{Value: i.FieldValue.toC()}
}
func colorComponent(v float32) uint8 {
r := int(v*255 + 0.5)
if r < 0 {
return 0
}
if r > 255 {
return 255
}
return uint8(r)
}
func (i Color) Pack() uint32 {
return uint32(colorComponent(i.FieldValue.X))<<0 |
uint32(colorComponent(i.FieldValue.Y))<<8 |
uint32(colorComponent(i.FieldValue.Z))<<16 |
uint32(colorComponent(i.FieldValue.W))<<24
}
func (i Color) Color() color.Color {
return color.NRGBA{
R: colorComponent(i.FieldValue.X),
G: colorComponent(i.FieldValue.Y),
B: colorComponent(i.FieldValue.Z),
A: colorComponent(i.FieldValue.W),
}
}
var _ wrappableType[C.ImRect, *Rect] = &Rect{}
type Rect struct {
Min Vec2
Max Vec2
}
func (i *Rect) fromC(rect C.ImRect) *Rect {
out := &Vec2{}
out.fromC(rect.Min)
i.Min = *out
out = &Vec2{}
out.fromC(rect.Max)
i.Max = *out
return i
}
func (r *Rect) toC() C.ImRect {
return C.ImRect{Min: r.Min.toC(), Max: r.Max.toC()}
}
var _ wrappableType[C.ImPlotPoint, *PlotPoint] = &PlotPoint{}
type PlotPoint struct {
X float64
Y float64
}
func NewPlotPoint(x, y float64) PlotPoint {
return PlotPoint{X: x, Y: y}
}
func (i *PlotPoint) fromC(p C.ImPlotPoint) *PlotPoint {
*i = NewPlotPoint(float64(p.x), float64(p.y))
return i
}
func (p PlotPoint) toC() C.ImPlotPoint {
return C.ImPlotPoint{x: C.double(p.X), y: C.double(p.Y)}
}
type PlotTime struct {
S int // second part
FieldUs int // microsecond part
}
func NewPlotTime(t time.Time) PlotTime {
ts := t.UnixMicro()
return PlotTime{
S: int(ts / 1e6),
FieldUs: int(ts % 1e6),
}
}
func (i PlotTime) Time() time.Time {
return time.Unix(int64(i.S), int64(i.FieldUs)*int64(time.Microsecond))
}
func (i *PlotTime) fromC(p C.ImPlotTime) *PlotTime {
*i = PlotTime{int(p.S), int(p.Us)}
return i
}
func (p PlotTime) toC() C.ImPlotTime {
return C.ImPlotTime{S: C.xlong(p.S), Us: C.int(p.FieldUs)}
}
type Vector[T any] struct {
Size int
Capacity int
Data T
pinner *runtime.Pinner
}
func newVectorFromC[T any](size, capacity C.int, data T) Vector[T] {
return Vector[T]{Size: int(size), Capacity: int(capacity), Data: data, pinner: &runtime.Pinner{}}
}
// wrappableType represents a GO type that can be converted into a C value
// and back - into a GO value.
// CTYPE represents the equivalent C type of self.
// self is the type wrappableType applies to - TODO - figure out if it can be ommited :-)
// intentional values:
// - CTYPE is e.g. C.ImVec2, C.ImColor e.t.c.
// - self is a pointer type (e.g. *Vec2, Color)
type wrappableType[CTYPE any, self any] interface {
// toC converts self into CTYPE
toC() CTYPE
// fromC converts takes CTYPE, converts it into self,
// applies to receiver and returns it.
fromC(CTYPE) self
}
// wrap takes a variable of one of the types defined in this file
// and returns a pointer to its C equivalend as well as a "finisher" func.
// This finisher func should be called after doing any operations
// on C pointer in order to apply the changes back to the original GO variable.
func wrap[CTYPE any, self any](in wrappableType[CTYPE, self]) (cPtr *CTYPE, finisher func()) {
if in != nil {
c := in.toC()
cPtr = &c
finisher = func() {
in.fromC(*cPtr)
}
} else {
finisher = func() {}
}
return cPtr, finisher
}