-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
42 lines (33 loc) · 1.03 KB
/
button.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
package rlnicex
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
type Button struct {
Label Label
Pos rl.Rectangle
}
func NewButton(label Label, x, y, w, h float64) Button {
return Button{
Label: label,
Pos: rl.NewRectangle(float32(x), float32(y), float32(w), float32(h)),
}
}
func (b Button) IsHovered(r Offset) bool {
final := getFinal(b.Pos, r)
return rl.CheckCollisionPointRec(rl.GetMousePosition(), final.ToFloat32())
}
func (b Button) IsClicked(r Offset) bool {
return b.IsHovered(r) && rl.IsMouseButtonReleased(rl.MouseLeftButton)
}
func (b Button) IsHeld(r Offset) bool {
return b.IsHovered(r) && rl.IsMouseButtonDown(rl.MouseLeftButton)
}
func (b Button) Render(r Offset) {
final := getFinal(b.Pos, r)
style := getUsedStyle(b, r)
// Draw the background
rl.DrawRectangle(final.X, final.Y, final.Width, final.Height, style.BackgroundColor)
// Render the label
b.Label.RenderWithStyle(r.Sub(float64(b.Pos.X)+float64(b.Pos.Width)/2, float64(b.Pos.Y)+float64(b.Pos.Height)/2), style)
DrawBorder(final, style)
}