This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
65 lines (58 loc) · 1.62 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ui
import (
"github.com/tanema/amore/gfx"
"github.com/tanema/amore/mouse"
)
// Button is a clickable rectangle
type Button struct {
x, y, w, h float32
image *gfx.Image
imageW, imageH float32
color *gfx.Color
mouseDown bool
clickDown bool
onClick func(*Button)
}
// NewButton builds and returns a new button
func NewButton(x, y, w, h float32, image *gfx.Image, color *gfx.Color, onClick func(*Button)) *Button {
button := &Button{
x: x, y: y, w: w, h: h,
color: color,
onClick: onClick,
}
button.SetImage(image)
return button
}
// SetImage sets the image for the button
func (button *Button) SetImage(image *gfx.Image) {
button.image = image
button.imageW = button.w / float32(image.GetWidth())
button.imageH = button.h / float32(image.GetHeight())
}
// Update checks for clicks
func (button *Button) Update(dt float32) {
x, y := mouse.GetPosition()
isDown := mouse.IsDown(mouse.LeftButton)
if isDown && !button.mouseDown && button.containsPoint(x, y) {
button.clickDown = true
} else if !isDown {
if button.mouseDown && button.clickDown && button.containsPoint(x, y) {
button.onClick(button)
}
button.clickDown = false
}
button.mouseDown = isDown
}
// Draw draws the button
func (button *Button) Draw() {
gfx.SetColorC(button.color)
gfx.Rect(gfx.LINE, button.x, button.y, button.w, button.h)
gfx.SetColor(255, 255, 255, 255)
gfx.Draw(button.image, button.x, button.y, 0, button.imageW, button.imageH)
}
func (button *Button) containsPoint(px, py float32) bool {
return button.x < px &&
button.x+button.w > px &&
button.y < py &&
button.y+button.h > py
}