-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.das
144 lines (103 loc) · 3.45 KB
/
ui.das
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
//options persistent_heap = true
//options gc
//module ui shared
require daslib/media
require levels
require game
struct Element
text:string
action:lambda<():void>
onCancel:lambda<():void>
children:array<Element?>
var ui_root: Element?
var curItem = 0
def make_main_menu : Element?
var continueCb <- @ <| ():void
ui_root = null
var miContinue <- new [[Element text="Continue", action <- continueCb]]
var chooseLevelCb <- @ <| ():void
ui_root = make_levels_list()
var miChooseLevel <- new [[Element text="Choose level", action <- chooseLevelCb]]
var exitCb <- @ <| ():void
schedule_quit_game()
var miExit <- new [[Element text="Exit", action <- exitCb]]
var closeMenu <- @ <| ():void
ui_root = null
var ui_root <- new [[Element
children <- to_array_move([[Element?
miContinue;
miChooseLevel;
miExit
]]),
onCancel <- closeMenu
]]
curItem = 0
return ui_root
def make_levels_list
var children:array<Element?>
children |> resize(length(levels_src))
for level, idx in levels_src, range(length(levels_src))
let solvedLabel = was_level_solved(idx) ? " (solved)" : ""
var action <- @ <| ():void
start_level(idx)
ui_root = null
children[idx] = new [[Element text="Level {idx+1}{solvedLabel}", action <- action]]
var backToMainMenu <- @ <| ():void
ui_root = make_main_menu()
var ui_root <- new [[Element
children <- children,
onCancel <- backToMainMenu
]]
curItem = cur_level
return ui_root
def on_key(pressed:bool; key:int)
if ui_root==null
if pressed && key == VK_ESCAPE
ui_root = make_main_menu()
return
if pressed
if key==VK_UP
curItem = (curItem - 1 + length(ui_root.children)) % length(ui_root.children)
elif key == VK_DOWN
curItem = (curItem + 1) % length(ui_root.children)
elif key == VK_PRIOR
curItem = max(curItem-10, 0)
elif key == VK_NEXT
curItem = min(curItem+10, length(ui_root.children)-1)
elif key == VK_RETURN
invoke(ui_root.children[curItem].action)
elif key == VK_ESCAPE
invoke(ui_root.onCancel)
let DASBOX_KB_RANGE_START = 1000
def send_kb_events
let gen <- generator<tuple<pressed:bool; key:int>>() <| $()
for key in range(DASBOX_KB_RANGE_START, DASBOX_KB_RANGE_START+128)
if get_key_down(key)
yield [[auto true, key]]
if get_key_up(key)
yield [[auto false, key]]
return false
for e in gen
on_key(e.pressed, e.key)
def act_ui(dt: float)
send_kb_events()
def draw_menu
if ui_root==null
return
let sw = get_screen_width()
let sh = get_screen_height()
let screensz = float2(sw, sh)
var y = sh/4
let fontSize = sh/20
let lineHeight = fontSize * 3 / 2
set_font_size(fontSize)
fill_rect(0, 0, sw, sh, 0xA05050A0)
let nItems = length(ui_root.children)
if nItems * lineHeight > sh/2
y = sh/2 - lineHeight - lineHeight * curItem
for item, idx in ui_root.children, range(nItems)
let isCurrent = idx == curItem
let bounds = get_text_size(item.text)
let color = isCurrent ? 0xFFFFFFFF : 0xA0A0A0A0
text_out((screensz[0]-bounds[0])/2.0f, float(y), item.text, color)
y += lineHeight