forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.go
263 lines (219 loc) · 6.67 KB
/
Utils.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
255
256
257
258
259
260
261
262
263
package giu
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
"path/filepath"
imgui "github.com/AllenDang/cimgui-go"
"github.com/pkg/browser"
)
// ID is an alias type. If some method uses ID it means, that this string will NOT be proceeded anyhow and will be passed
// as-is to cimgui-go. This also means that it deffinitly needs to be
// unique because else it will cause strange UI behavior.
// see: https://github.com/AllenDang/giu/wiki#id
type ID string
func (i ID) String() string {
return string(i)
}
// LoadImage loads image from file and returns *image.RGBA.
func LoadImage(imgPath string) (*image.RGBA, error) {
imgFile, err := os.Open(filepath.Clean(imgPath))
if err != nil {
return nil, fmt.Errorf("LoadImage: error opening image file %s: %w", imgPath, err)
}
defer func() {
if err := imgFile.Close(); err != nil {
panic(fmt.Sprintf("error closing image file: %s", imgPath))
}
}()
img, err := png.Decode(imgFile)
if err != nil {
return nil, fmt.Errorf("LoadImage: error decoding png image: %w", err)
}
return ImageToRgba(img), nil
}
// ImageToRgba converts image.Image to *image.RGBA.
func ImageToRgba(img image.Image) *image.RGBA {
switch trueImg := img.(type) {
case *image.RGBA:
return trueImg
default:
rgba := image.NewRGBA(trueImg.Bounds())
draw.Draw(rgba, trueImg.Bounds(), trueImg, image.Pt(0, 0), draw.Src)
return rgba
}
}
// ToVec4Color converts rgba color to imgui.Vec4.
func ToVec4Color(col color.Color) imgui.Vec4 {
const mask = 0xffff
r, g, b, a := col.RGBA()
return imgui.Vec4{
X: float32(r) / mask,
Y: float32(g) / mask,
Z: float32(b) / mask,
W: float32(a) / mask,
}
}
// ToVec2 converts image.Point to imgui.Vec2.
func ToVec2(pt image.Point) imgui.Vec2 {
return imgui.Vec2{
X: float32(pt.X),
Y: float32(pt.Y),
}
}
// Vec4ToRGBA converts imgui's Vec4 to golang rgba color.
func Vec4ToRGBA(vec4 imgui.Vec4) color.RGBA {
return color.RGBA{
R: uint8(vec4.X * 255),
G: uint8(vec4.Y * 255),
B: uint8(vec4.Z * 255),
A: uint8(vec4.W * 255),
}
}
// Update updates giu app
// it is done by default after each frame.
// However because frames stops rendering, when no user
// action is done, it may be necessary to
// Update ui manually at some point.
func Update() {
Context.m.Lock()
defer Context.m.Unlock()
if Context.isAlive {
Context.backend.Refresh()
}
}
// GetCursorScreenPos returns imgui drawing cursor on the screen.
func GetCursorScreenPos() image.Point {
pos := imgui.CursorScreenPos()
return image.Pt(int(pos.X), int(pos.Y))
}
// SetCursorScreenPos sets imgui drawing cursor on the screen.
func SetCursorScreenPos(pos image.Point) {
imgui.SetCursorScreenPos(imgui.Vec2{X: float32(pos.X), Y: float32(pos.Y)})
}
// GetCursorPos gets imgui drawing cursor inside of current window.
func GetCursorPos() image.Point {
pos := imgui.CursorPos()
return image.Pt(int(pos.X), int(pos.Y))
}
// SetCursorPos sets imgui drawing cursor inside of current window.
func SetCursorPos(pos image.Point) {
imgui.SetCursorPos(imgui.Vec2{X: float32(pos.X), Y: float32(pos.Y)})
}
// GetMousePos returns mouse position.
func GetMousePos() image.Point {
pos := imgui.MousePos()
return image.Pt(int(pos.X), int(pos.Y))
}
// GetAvailableRegion returns region available for rendering.
// it is always WindowSize-WindowPadding*2.
func GetAvailableRegion() (width, height float32) {
region := imgui.ContentRegionAvail()
return region.X, region.Y
}
// CalcTextSize calls CalcTextSizeV(text, false, -1).
func CalcTextSize(text string) (width, height float32) {
return CalcTextSizeV(text, false, -1)
}
// CalcTextSizeV calculates text dimensions.
func CalcTextSizeV(text string, hideAfterDoubleHash bool, wrapWidth float32) (w, h float32) {
size := imgui.CalcTextSizeV(
text,
hideAfterDoubleHash,
wrapWidth,
)
return size.X, size.Y
}
// SetNextWindowSize sets size of the next window.
func SetNextWindowSize(width, height float32) {
imgui.SetNextWindowSize(imgui.Vec2{X: width, Y: height})
}
// ExecCondition represents imgui.Cond.
type ExecCondition imgui.Cond
// imgui conditions.
const (
ConditionAlways ExecCondition = ExecCondition(imgui.CondAlways)
ConditionOnce ExecCondition = ExecCondition(imgui.CondOnce)
ConditionFirstUseEver ExecCondition = ExecCondition(imgui.CondFirstUseEver)
ConditionAppearing ExecCondition = ExecCondition(imgui.CondAppearing)
)
// SetNextWindowPos sets position of next window.
func SetNextWindowPos(x, y float32) {
imgui.SetNextWindowPos(imgui.Vec2{X: x, Y: y})
}
// SetNextWindowSizeV does similar to SetNextWIndowSize but allows to specify imgui.Cond.
func SetNextWindowSizeV(width, height float32, condition ExecCondition) {
imgui.SetNextWindowSizeV(
imgui.Vec2{
X: width,
Y: height,
},
imgui.Cond(condition),
)
}
// SetItemDefaultFocus set the item focused by default.
func SetItemDefaultFocus() {
imgui.SetItemDefaultFocus()
}
// SetKeyboardFocusHere sets keyboard focus at *NEXT* widget.
func SetKeyboardFocusHere() {
SetKeyboardFocusHereV(0)
}
// SetKeyboardFocusHereV sets keyboard on the next widget. Use positive 'offset' to access sub components of a multiple component widget. Use -1 to access previous widget.
func SetKeyboardFocusHereV(i int) {
imgui.SetKeyboardFocusHereV(int32(i))
}
// PushClipRect pushes a clipping rectangle for both ImGui logic (hit-testing etc.) and low-level ImDrawList rendering.
func PushClipRect(clipRectMin, clipRectMax image.Point, intersectWithClipRect bool) {
imgui.PushClipRect(ToVec2(clipRectMin), ToVec2(clipRectMax), intersectWithClipRect)
}
// PopClipRect should be called to end PushClipRect.
func PopClipRect() {
imgui.PopClipRect()
}
// Assert checks if cond. If not cond, it alls golang panic.
func Assert(cond bool, t, method, msg string, args ...any) {
if !cond {
fatal(t, method, msg, args...)
}
}
func fatal(widgetName, method, message string, args ...any) {
if widgetName != "" {
widgetName = fmt.Sprintf("(*%s)", widgetName)
}
log.Panicf("giu: %s.%s: %s", widgetName, method, fmt.Sprintf(message, args...))
}
// OpenURL opens `url` in default browser.
func OpenURL(url string) {
if err := browser.OpenURL(url); err != nil {
log.Printf("Error opening %s: %v", url, err)
}
}
// ColorToUint converts GO color into Uint32 color
// it is 0xRRGGBBAA.
func ColorToUint(col color.Color) uint32 {
r, g, b, a := col.RGBA()
mask := uint32(0xff)
return r&mask +
g&mask<<8 +
b&mask<<16 +
a&mask<<24
}
// UintToColor converts uint32 of form 0xRRGGBB into color.RGBA.
func UintToColor(col uint32) *color.RGBA {
mask := 0xff
r := byte(col >> 24 & uint32(mask))
g := byte(col >> 16 & uint32(mask))
b := byte(col >> 8 & uint32(mask))
a := byte(col >> 0 & uint32(mask))
return &color.RGBA{
R: r,
G: g,
B: b,
A: a,
}
}