-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.go
129 lines (113 loc) · 3.14 KB
/
components.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
package gogpui
import (
"image/color"
rl "github.com/gen2brain/raylib-go/raylib"
)
func (g *GPUI) Text(area Rectangle, frameEvents []Event, TextSize int, Color color.RGBA, String string) {
g.DrawTextAt(String, TextSize, Vector2{X: area.X, Y: area.Y}, Color)
}
func (g *GPUI) Button(
area Rectangle,
frameEvents []Event,
TextSize int,
FillColor color.RGBA,
TextColor color.RGBA,
Label string) bool {
pressed := false
for _, evt := range frameEvents {
if evt.Type == EventType_MouseClick {
mouseClick := evt.Data.(MouseClickEvent)
if mouseClick.Position.X >= area.X && mouseClick.Position.Y >= area.Y &&
mouseClick.Position.X <= area.X+area.Width && mouseClick.Position.Y <= area.Y+area.Height {
pressed = true
}
}
}
if pressed {
g.DrawFilledRectangle(area, FillColor)
} else {
g.DrawRectangle(area, 2, FillColor)
}
g.DrawTextAt(Label, TextSize, Vector2{
X: area.X + area.Width*2/3,
Y: area.Y + area.Height/2,
}, TextColor)
return pressed
}
func (g *GPUI) Border(screenArea Rectangle) Rectangle {
return rl.Rectangle{
X: screenArea.X + 1,
Y: screenArea.Y + 1,
Width: screenArea.Width - 1,
Height: screenArea.Height - 1,
}
}
func (g *GPUI) TextBox(
id string,
screanArea rl.Rectangle,
frameEvents []Event,
TextColor color.RGBA,
textPointer *string) {
text := *textPointer
for _, evt := range frameEvents {
if evt.Type == EventType_MouseClick {
mouseClick := evt.Data.(MouseClickEvent)
if mouseClick.Position.X >= screanArea.X && mouseClick.Position.Y >= screanArea.Y &&
mouseClick.Position.X <= screanArea.X+screanArea.Width && mouseClick.Position.Y <= screanArea.Y+screanArea.Height {
g.activeTextBoxID = id
}
}
}
if g.activeTextBoxID == id {
for _, evt := range frameEvents {
if evt.Type == EventType_KeyPress {
char := evt.Data.(KeyPressEvent).GetAsciiChar()
if char != 0 {
text += string(char)
}
}
}
*textPointer = text
}
border := rl.Red
if g.activeTextBoxID == id {
border = rl.Blue
}
g.DrawRectangle(screanArea, 1, border)
g.DrawTextAt(*textPointer, 40, Vector2{X: screanArea.X, Y: screanArea.Y}, rl.White)
}
func ColumnarAreas(screenArea Rectangle, columnCount int) []Rectangle {
columnWidth := screenArea.Width / float32(columnCount)
var columns []rl.Rectangle
for i := 0; i < columnCount; i++ {
columns = append(columns, Rectangle{
X: float32(i)*columnWidth + screenArea.X,
Width: columnWidth,
Y: screenArea.Y,
Height: screenArea.Height,
})
}
return columns
}
func ListAreas(screenArea Rectangle, listItemCount int) []Rectangle {
itemHeight := screenArea.Height / float32(listItemCount)
var items []Rectangle
for i := 0; i < listItemCount; i++ {
items = append(items, Rectangle{
X: screenArea.X,
Width: screenArea.Width,
Y: float32(i)*itemHeight + screenArea.Y,
Height: itemHeight,
})
}
return items
}
func GridAreas(screenArea Rectangle, rowsCount int, columnsCount int) []Rectangle {
var items []rl.Rectangle
columns := ColumnarAreas(screenArea, columnsCount)
for _, col := range columns {
listAreas := ListAreas(col, rowsCount)
items = append(items, listAreas...)
}
return items
}