/
mainmenu.go
79 lines (71 loc) · 1.41 KB
/
mainmenu.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
package main
import (
"container/ring"
"fmt"
"github.com/acieroid/go-sfml"
"os"
"strings"
"time"
)
type MainMenu struct {
maps *ring.Ring
lastModif time.Time
w, h int
}
func NewMainMenu(mapDir string, w, h int) (m *MainMenu) {
file, err := os.Open(mapDir)
defer file.Close()
if err != nil {
panic(err)
}
fis, err := file.Readdir(0)
if err != nil {
panic(err)
}
var maps *ring.Ring
for _, fi := range fis {
if strings.HasSuffix(fi.Name(), ".txt") {
name := fi.Name()
mapName := name[:len(name)-4]
r := ring.New(1)
r.Value = mapName
if maps != nil {
maps.Link(r)
} else {
maps = r
}
}
}
return &MainMenu{maps, time.Now(), w, h}
}
func (m *MainMenu) Run(win sfml.RenderWindow) (string, int) {
e, b := win.PollEvent()
if b {
switch e.(type) {
case sfml.KeyEvent:
ev := e.(sfml.KeyEvent)
switch ev.Code() {
case sfml.KeyLeft:
if int(time.Since(m.lastModif)/1e6) > 250 {
m.maps = m.maps.Move(1)
m.lastModif = time.Now()
}
case sfml.KeyRight:
if int(time.Since(m.lastModif)/1e6) > 250 {
m.maps = m.maps.Move(-1)
m.lastModif = time.Now()
}
case sfml.KeyReturn:
return m.maps.Value.(string), GAME
case sfml.KeyEscape:
if int(time.Since(m.lastModif)/1e6) > 250 {
return "", QUIT
}
}
}
}
DrawTextBig(fmt.Sprintf("< Map: %s >", m.maps.Value.(string)),
m.w/2, m.h/2,
true, win)
return "", MENU
}