forked from HACKERALERT/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.go
185 lines (156 loc) · 4.79 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
package giu
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"path/filepath"
"github.com/Picocrypt/imgui-go"
)
// 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, err
}
defer func() {
// nolint:govet // we want to reuse this err variable here
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, 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.RGBA) imgui.Vec4 {
return imgui.Vec4{
X: float32(col.R) / 255,
Y: float32(col.G) / 255,
Z: float32(col.B) / 255,
W: float32(col.A) / 255,
}
}
// 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.
// Hoeever because frames stops rendering, when no user
// action is done, it may be necessary to
// Update ui manually at some point.
func Update() {
if Context.isAlive {
Context.platform.Update()
Context.IO().SetFrameCountSinceLastInput(0)
}
}
// 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))
}
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.CalcTextSize(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 * Context.platform.GetContentScale(), Y: height * Context.platform.GetContentScale()})
}
// ExecCondition represents imgui.Condition
type ExecCondition imgui.Condition
// imgui conditions
const (
ConditionAlways ExecCondition = ExecCondition(imgui.ConditionAlways)
ConditionOnce ExecCondition = ExecCondition(imgui.ConditionOnce)
ConditionFirstUseEver ExecCondition = ExecCondition(imgui.ConditionFirstUseEver)
ConditionAppearing ExecCondition = ExecCondition(imgui.ConditionAppearing)
)
// 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.Condition
func SetNextWindowSizeV(width, height float32, condition ExecCondition) {
imgui.SetNextWindowSizeV(
imgui.Vec2{
X: width * Context.platform.GetContentScale(),
Y: height * Context.platform.GetContentScale(),
},
imgui.Condition(condition),
)
}
// SetItemDefaultFocus set the item focused by default
func SetItemDefaultFocus() {
imgui.SetItemDefaultFocus()
}
// SetKeyboardFocusHere sets keyboard focus at the widget
func SetKeyboardFocusHere() {
SetKeyboardFocusHereV(0)
}
func SetKeyboardFocusHereV(i int) {
imgui.SetKeyboardFocusHereV(i)
}
func PushClipRect(clipRectMin, clipRectMax image.Point, intersectWithClipRect bool) {
imgui.PushClipRect(ToVec2(clipRectMin), ToVec2(clipRectMax), intersectWithClipRect)
}
func PopClipRect() {
imgui.PopClipRect()
}