forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.go
139 lines (113 loc) · 3.14 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
package giu
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
"github.com/AllenDang/imgui-go"
)
func LoadImage(imgPath string) (*image.RGBA, error) {
imgFile, err := os.Open(imgPath)
if err != nil {
return nil, err
}
defer imgFile.Close()
img, err := png.Decode(imgFile)
if err != nil {
return nil, err
}
switch trueImg := img.(type) {
case *image.RGBA:
return trueImg, nil
default:
rgba := image.NewRGBA(trueImg.Bounds())
draw.Draw(rgba, trueImg.Bounds(), trueImg, image.Pt(0, 0), draw.Src)
return rgba, nil
}
}
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,
}
}
func ToVec2(pt image.Point) imgui.Vec2 {
return imgui.Vec2{
X: float32(pt.X),
Y: float32(pt.Y),
}
}
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),
}
}
func Update() {
if Context.isAlive {
Context.platform.Update()
Context.IO().SetFrameCountSinceLastInput(0)
}
}
func GetCursorScreenPos() image.Point {
pos := imgui.CursorScreenPos()
return image.Pt(int(pos.X), int(pos.Y))
}
func SetCursorScreenPos(pos image.Point) {
imgui.SetCursorScreenPos(imgui.Vec2{X: float32(pos.X), Y: float32(pos.Y)})
}
func GetCursorPos() image.Point {
pos := imgui.CursorPos()
return image.Pt(int(pos.X), int(pos.Y))
}
func SetCursorPos(pos image.Point) {
imgui.SetCursorPos(imgui.Vec2{X: float32(pos.X), Y: float32(pos.Y)})
}
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
}
func CalcTextSize(text string) (width, height float32) {
size := imgui.CalcTextSize(text, false, 0)
return size.X, size.Y
}
func SetNextWindowSize(width, height float32) {
imgui.SetNextWindowSize(imgui.Vec2{X: width * Context.platform.GetContentScale(), Y: height * Context.platform.GetContentScale()})
}
type ExecCondition imgui.Condition
const (
ConditionAlways ExecCondition = ExecCondition(imgui.ConditionAlways)
ConditionOnce ExecCondition = ExecCondition(imgui.ConditionOnce)
ConditionFirstUseEver ExecCondition = ExecCondition(imgui.ConditionFirstUseEver)
ConditionAppearing ExecCondition = ExecCondition(imgui.ConditionAppearing)
)
func SetNextWindowPos(x, y float32) {
imgui.SetNextWindowPos(imgui.Vec2{X: x, Y: y})
}
func SetNextWindowSizeV(width, height float32, condition ExecCondition) {
imgui.SetNextWindowSizeV(imgui.Vec2{X: width * Context.platform.GetContentScale(), Y: height * Context.platform.GetContentScale()}, imgui.Condition(condition))
}
func SetItemDefaultFocus() {
imgui.SetItemDefaultFocus()
}
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()
}