summaryrefslogtreecommitdiff
path: root/engine/UI/Button.go
blob: f865c5fe28029ec7e4db58d2ecc6aafa9b353e36 (plain)
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
package ui

import (
	rl "github.com/gen2brain/raylib-go/raylib"
)

type DetectionType uint8

const (
	OnClick DetectionType = iota
	OnHold
)

type Button struct {
	WidthWeight float32
	DisplayElement UIElement
	EventType DetectionType
	ButtonType rl.MouseButton
	OnClick func()

	clicked func(rl.MouseButton) bool
}

func (base *Button) Init(parent *Menu) {
	base.DisplayElement.Init(parent)
	if base.WidthWeight == 0 {
		base.WidthWeight = 1
	}

	switch base.EventType {
	case OnClick:
		base.clicked = rl.IsMouseButtonReleased
	case OnHold:
		base.clicked = rl.IsMouseButtonDown
	}
}

func (base *Button) Destroy() {
}

func (base *Button) GetOccupationWeight() float32 {
	return base.WidthWeight
}

func (base *Button) Draw(position *rl.Rectangle) {
	base.DisplayElement.Draw(position)
	if base.OnClick != nil && base.clicked(base.ButtonType) {
		var mousePos = rl.GetMousePosition()
		if rl.CheckCollisionPointRec(mousePos, *position) {
			base.OnClick()
		}
	}
}