-
Notifications
You must be signed in to change notification settings - Fork 7
/
ui.go
236 lines (193 loc) · 6.15 KB
/
ui.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
package pixelui
import (
"C"
"github.com/gopxl/pixel/v2"
"github.com/inkyblackness/imgui-go/v4"
)
import (
"image/color"
"runtime"
"time"
"unsafe"
"github.com/gopxl/mainthread/v2"
"github.com/gopxl/pixel/v2/backends/opengl"
"github.com/gopxl/pixel/v2/ext/atlas"
)
const uiShader = `
#version 330 core
in vec4 vColor;
in vec2 vTexCoords;
in float vIntensity;
in vec4 vClipRect;
out vec4 fragColor;
uniform vec4 uColorMask;
uniform vec4 uTexBounds;
uniform sampler2D uTexture;
uniform vec4 uClipRect;
void main() {
if ((vClipRect != vec4(0,0,0,0)) && (gl_FragCoord.x < vClipRect.x || gl_FragCoord.y < vClipRect.y || gl_FragCoord.x > vClipRect.z || gl_FragCoord.y > vClipRect.w))
discard;
fragColor = vColor;
if (vIntensity == 0) {
fragColor *= vColor * texture(uTexture, vTexCoords).a;
fragColor *= uColorMask;
} else {
fragColor *= vColor * texture(uTexture, vTexCoords);
fragColor *= uColorMask;
}
}
`
// UI Stores the state of the pixelui UI
type UI struct {
win *opengl.Window
context *imgui.Context
io imgui.IO
fonts imgui.FontAtlas
timer time.Time
shader *opengl.GLShader
matrix pixel.Matrix
shaderTris *opengl.GLTriangles
atlas *atlas.Atlas
group atlas.Group
font atlas.TextureId
cursors map[imgui.MouseCursorID]*opengl.Cursor
}
var CurrentUI *UI
// pixelui.NewUI flags:
//
// NO_DEFAULT_FONT: Do not load the default font during New.
const (
NO_DEFAULT_FONT uint8 = 1 << iota
)
// New Creates the UI and setups up its internal structures
func New(win *opengl.Window, atlas *atlas.Atlas, flags uint8) *UI {
var context *imgui.Context
mainthread.Call(func() {
context = imgui.CreateContext(nil)
})
ui := &UI{
win: win,
context: context,
atlas: atlas,
group: atlas.MakeGroup(),
cursors: make(map[imgui.MouseCursorID]*opengl.Cursor),
}
CurrentUI = ui
ui.io = imgui.CurrentIO()
ui.initIO()
ui.fonts = ui.io.Fonts()
ui.shader = opengl.NewGLShader(uiShader)
ui.shaderTris = opengl.NewGLTriangles(ui.shader, pixel.MakeTrianglesData(0))
if flags&NO_DEFAULT_FONT == 0 {
ui.loadDefaultFont()
}
runtime.SetFinalizer(ui, (*UI).destroy)
return ui
}
// Destroy cleans up the imgui context
func (ui *UI) destroy() {
ui.context.Destroy()
}
// NewFrame Call this at the beginning of the frame to tell the UI that the frame has started
func (ui *UI) NewFrame() {
if !ui.timer.IsZero() {
ui.io.SetDeltaTime(float32(time.Since(ui.timer).Seconds()))
}
ui.timer = time.Now()
// imgui requires that io be set before calling NewFrame
ui.prepareIO()
imgui.NewFrame()
}
// update Handles general update type things and handle inputs. Called from ui.Draw.
func (ui *UI) update() {
}
func (ui *UI) updateMatrix() {
ui.matrix = pixel.IM.ScaledXY(ui.win.Bounds().Center(), pixel.V(1, -1))
}
// Draw Draws the imgui UI to the Pixel Window
func (ui *UI) Draw(win *opengl.Window) {
ui.updateMatrix()
win.SetComposeMethod(pixel.ComposeOver)
win.SetMatrix(ui.matrix)
// imgui draws things from top-left as 0,0 where Pixel draws from bottom-left as 0,0,
// for drawing and handling inputs, we need to "flip" imgui.
ui.update()
// Tell imgui to render and get the resulting draw data
imgui.Render()
data := imgui.RenderedDrawData()
// Since we have to redraw all of the triangles every frame,
// only resize the triangles list when we need to, and truncate
// it right before we draw (to get rid of any extra triangles).
totalTris := 0
// In each command, there is a vertex buffer that holds all of the vertices to draw;
// there's also an index buffer which stores the indices into the vertex buffer that should
// be draw together. The vertex buffer is shared between multiple commands.
vertexSize, posOffset, uvOffset, colOffset := imgui.VertexBufferLayout()
indexSize := imgui.IndexBufferLayout()
for _, cmds := range data.CommandLists() {
var indexBufferOffset uintptr
start, _ := cmds.VertexBuffer()
idxStart, _ := cmds.IndexBuffer()
for _, cmd := range cmds.Commands() {
if cmd.HasUserCallback() {
cmd.CallUserCallback(cmds)
} else {
count := cmd.ElementCount()
iStart := totalTris
totalTris += count
if ui.shaderTris.Len() < totalTris {
ui.shaderTris.SetLen(totalTris)
}
clipRect := imguiRectToPixelRect(cmd.ClipRect()).Norm()
clipRect.Min = ui.matrix.Project(clipRect.Min)
clipRect.Max = ui.matrix.Project(clipRect.Max)
clipRect = clipRect.Norm()
id := uint32(cmd.TextureID())
spr := ui.atlas.Get(id)
texRect := spr.Frame()
intensity := 0.0
if id != ui.font.ID() {
intensity = 1.0
}
for i := 0; i < count; i++ {
idx := unsafe.Pointer(uintptr(idxStart) + indexBufferOffset)
index := *(*uint16)(idx)
ptr := unsafe.Pointer(uintptr(start) + (uintptr(int(index) * vertexSize)))
pos := *(*imgui.Vec2)(unsafe.Pointer(uintptr(ptr) + uintptr(posOffset)))
uv := *(*imgui.Vec2)(unsafe.Pointer(uintptr(ptr) + uintptr(uvOffset)))
col := *(*uint32)(unsafe.Pointer(uintptr(ptr) + uintptr(colOffset)))
position := PV(pos)
color := imguiColorToPixelColor(col)
uuvv := ui.calcData(texRect, PV(uv))
ui.shaderTris.SetPosition(iStart+i, position)
ui.shaderTris.SetPicture(iStart+i, uuvv, intensity)
ui.shaderTris.SetColor(iStart+i, pixel.ToRGBA(color))
ui.shaderTris.SetClipRect(iStart+i, clipRect)
indexBufferOffset += uintptr(indexSize)
}
}
}
}
ui.shaderTris.SetLen(totalTris)
ui.shaderTris.CopyVertices()
win.MakePicture(ui.atlas.Textures()[0]).Draw(win.MakeTriangles(ui.shaderTris))
win.SetMatrix(pixel.IM)
}
// recip returns the reciprocal of the given number.
func recip(m float64) float64 {
return 1 / m
}
// calcData scales the incoming sprite uv to the proper sub-sprite in the packed atlas.
func (ui *UI) calcData(frame pixel.Rect, uuvv pixel.Vec) (pic pixel.Vec) {
return uuvv.ScaledXY(frame.Size()).Add(frame.Min).ScaledXY(ui.atlas.Textures()[0].Bounds().Size().Map(recip))
}
// imguiColorToPixelColor Converts the imgui color to a Pixel color.
func imguiColorToPixelColor(c uint32) color.RGBA {
// ABGR -> RGBA
return color.RGBA{
A: uint8((c >> 24) & 0xFF),
B: uint8((c >> 16) & 0xFF),
G: uint8((c >> 8) & 0xFF),
R: uint8(c & 0xFF),
}
}