/
buttons.go
201 lines (164 loc) · 3.53 KB
/
buttons.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
buttons
gloo 9.8.2017
create
draw
click
return codes
*/
package main
import (
"fmt"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"image/color"
"log"
"path/filepath"
)
type buttonobjectData struct {
name string
code int // return this code if clicked
x, y, w, h float64
active bool
image *ebiten.Image
clicked bool
count int // count frames
}
var (
aButtonData buttonobjectData
buttonArr []buttonobjectData
buttonRectImage *ebiten.Image
buttonOOP buttonobjectData
)
const (
noButtonPressed = 0
)
func (b *buttonobjectData) init() {
fmt.Print("button init test \n")
//b.name = "George"
fmt.Print("button string test ", b.name, " is very good \n")
}
func Button_Init() {
buttonRectImage, _ = ebiten.NewImage(100, 100, ebiten.FilterNearest)
}
func Button_Create(n string,
code int,
x float64,
y float64,
w float64,
h float64,
a bool) {
fmt.Print("button create\n")
buttonArr = append(buttonArr, aButtonData)
i := len(buttonArr)
i = i - 1 // zero index
buttonArr[i].name = n
buttonArr[i].code = code
buttonArr[i].x = x
buttonArr[i].y = y
buttonArr[i].w = w
buttonArr[i].h = h
buttonArr[i].active = a
buttonArr[i].clicked = false
buttonArr[i].count = 0
var err error
var fname string
fname = filepath.Join("data", n)
img, _, err := ebitenutil.NewImageFromFile(
fname,
ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
buttonArr[i].image = img
}
/*
check if same function name can exist in the same project
*/
func Haha() {
fmt.Print("Ha ha buttons\n")
}
func Button_Draw(screen *ebiten.Image) {
//fmt.Print("button draw\n")
// Create an empty option struct
opts := &ebiten.DrawImageOptions{}
m := len(buttonArr)
blue := color.NRGBA{0x00, 0x00, 0xff, 0xff}
for i := 0; i < m; i++ {
buttonRectImage.Fill(blue)
opts.GeoM.Reset()
//opts.GeoM.Scale( buttonArr[i].w, buttonArr[i].h )
if buttonArr[i].clicked {
opts.GeoM.Translate(buttonArr[i].x+5, buttonArr[i].y+5)
buttonArr[i].count += 1
if buttonArr[i].count > 30 {
buttonArr[i].clicked = false
buttonArr[i].count = 0
}
} else {
opts.GeoM.Translate(buttonArr[i].x, buttonArr[i].y)
}
//screen.DrawImage(buttonRectImage, opts)
screen.DrawImage(buttonArr[i].image, opts)
//ebitenutil.DebugPrint(buttonRectImage, buttonArr[i].name)
//ebitenutil.DebugPrint(screen, buttonArr[i].name)
}
}
/*
start timer
how long
what to do when timer runs out
reset timer
button timer
*/
//const kButtonStart = 2222
type buttonTimerStruct struct {
counter int
numFrames int
code int
kButtonStart int
}
func (bt *buttonTimerStruct) set(codef, numF int) {
fmt.Print("buttontimer set! \n")
bt.code = codef
bt.numFrames = numF
}
func (bt *buttonTimerStruct) inc() {
if bt.code == bt.kButtonStart {
//fmt.Print("inc\n")
bt.counter++
}
}
func (bt *buttonTimerStruct) check() bool {
if bt.counter > bt.numFrames {
return true
}
return false
}
func (bt *buttonTimerStruct) reset() {
fmt.Print("reset buttonTimer \n")
bt.counter = 0
bt.code = 0
bt.numFrames = 0
bt.kButtonStart = 2222
}
func Button_Click(mx, my int) int {
//fmt.Print("button click\n")
var x, y, w, h, x1, y1 int
m := len(buttonArr)
for i := 0; i < m; i++ {
x = int(buttonArr[i].x)
y = int(buttonArr[i].y)
w = int(buttonArr[i].w)
h = int(buttonArr[i].h)
x1 = x + w
y1 = y + h
code := buttonArr[i].code
if mx > x && mx < x1 && my > y && my < y1 {
buttonArr[i].clicked = true
buttonArr[i].count = 0
return code
}
}
return noButtonPressed
}