-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainMenu.adept
110 lines (86 loc) · 3.2 KB
/
MainMenu.adept
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
import 'main.adept'
MAIN_MENU_BUTTON_COUNT == 4
MAIN_MENU_BUTTON_WIDTH == 192.0f
MAIN_MENU_BUTTON_HEIGHT == 32.0f
struct MainMenu (buttons <String> List, actions <func() void> List, hovered_button int, title_y float) {
func setup {
this.buttons.add("online")
this.buttons.add("versus")
this.buttons.add("options")
this.buttons.add("quit")
this.actions.add(func &gotoOnlineMenu)
this.actions.add(func &gotoVersusMenu)
this.actions.add(func &gotoOptionsMenu)
this.actions.add(func &gotoQuit)
this.hovered_button = 0 // 0 == none
}
func enter {
this.title_y = -180.0f
gamedata.gamekind = GameKind::NONE
}
func exit {}
func __defer__ {
// Due to the compiler not yet automatically generating __defer__ functions
// via func& lookup, we have to manually ensure __defer__ is generated
}
func step {
this.hovered_button = 0
captMouseViewPosition(undef x float, undef y float)
repeat MAIN_MENU_BUTTON_COUNT {
if isMouseOverMainMenuButton(x, y, idx as int + 1) {
this.hovered_button = idx as int + 1
}
}
// Update title y position
target_title_y float = 64.0f
if fabs(this.title_y - target_title_y) < 0.01f, this.title_y = target_title_y
else this.title_y = (5.0f * this.title_y + target_title_y) / 6.0f
}
func click(x, y float, button int){
unless button == 1, return
repeat MAIN_MENU_BUTTON_COUNT {
if isMouseOverMainMenuButton(x, y, idx as int + 1) {
action func() void = this.actions.get(idx)
if action as ptr, action()
sfx.play(sfx.button)
return
}
}
}
func draw {
each button_text String in this.buttons {
getMainMenuButtonPosition(idx + 1, undef button_x float, undef button_y float)
drawButton(button_text, button_x, button_y, this.hovered_button == idx + 1)
}
captDrawTexture(textures.title, captViewWidth() / 2.0f - 160.0f, this.title_y, 320.0f, 180.0f)
}
}
func getMainMenuButtonPosition(button int, out x, y *float){
// NOTE: 'button' starts at 1
middle_x float = captViewWidth() / 2.0f
middle_y float = captViewHeight() / 2.0f
y_starting_offset float = 64.0f - 32.0f
y_run float = MAIN_MENU_BUTTON_HEIGHT + 16.0f
x_start float = middle_x - MAIN_MENU_BUTTON_WIDTH / 2.0f
y_start float = middle_y - y_starting_offset
y_start += y_run * (button - 1) as float
*x = x_start
*y = y_start
}
func isMouseOverMainMenuButton(mouse_x, mouse_y float, button int) bool {
getMainMenuButtonPosition(button, undef x float, undef y float)
return mouse_x > x && mouse_x < x + MAIN_MENU_BUTTON_WIDTH &&
mouse_y > y && mouse_y < y + MAIN_MENU_BUTTON_HEIGHT
}
func gotoOnlineMenu {
gamedata.setScene('onlinemenu')
}
func gotoVersusMenu {
gamedata.setScene('versusmenu')
}
func gotoOptionsMenu {
gamedata.setScene('optionsmenu')
}
func gotoQuit {
glfwSetWindowShouldClose(_captain_window, true)
}