forked from MidnightCommander/mc
/
dialog.lua
153 lines (124 loc) · 3.09 KB
/
dialog.lua
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
--[[
The UI of the game.
]]
local board = require('samples.games.blocks.board')
local setup_menu = {
{T"&Classic", value="classic"},
{T"Classic (&wide)", value="classicwide"},
{T"&Advanced", value="advanced"},
}
local current_setup = "classic"
local function run_dialog()
board.reset_board()
board.new_piece()
local ui_score = {
shapes = ui.Label(),
lines = ui.Label(),
points = ui.Label(),
}
local dlg = ui.Dialog(T"Game of Blocks")
local view = ui.Custom()
view.cols = board.get_wd()*2
view.rows = board.get_ht()
local itrvl
view.on_draw = function()
board.draw_board(view:get_canvas())
end
local function rjust(n)
n = require('utils.text').format_size(n, 6, true)
return ("%6s"):format(n)
end
local function update_score_display()
local score = board.get_score()
ui_score.shapes.text = rjust(score.shapes)
ui_score.lines.text = rjust(score.lines)
ui_score.points.text = rjust(score.points)
end
update_score_display()
local function go_down()
if not board.go_down() then
if not board.next_piece() then
itrvl:stop()
alert(T"Game over!")
end
update_score_display()
end
end
local K = utils.magic.memoize(tty.keyname_to_keycode)
view.on_hotkey = function(self, kcode)
if kcode == K'left' then
board.go_left()
elseif kcode == K'right' then
board.go_right()
elseif kcode == K'up' then
board.rotate_current_piece()
elseif kcode == K'down' then
go_down()
else
return false
end
view:redraw()
return true
end
local function tick()
go_down()
view:redraw()
dlg:refresh()
end
itrvl = timer.set_interval(tick, 1000)
local scores = ui.Groupbox(T"Stats"):add(
ui.HBox():add(
ui.VBox():add(ui.Label(T"Shapes:"), ui.Label(T"Rows:"), ui.Label(T"Points:")),
ui.VBox():add(ui_score.shapes, ui_score.lines, ui_score.points)
)
)
local btn_pause = ui.Button(T"&Pause")
local btn_resume = ui.Button{T"&Resume", enabled=false}
btn_pause.on_click = function()
itrvl:stop()
btn_pause.enabled = false
btn_resume.enabled = true
end
btn_resume.on_click = function()
itrvl:resume()
btn_pause.enabled = true
btn_resume.enabled = false
end
local function change_setup()
itrvl:stop()
local pref = ui.Dialog(T"Setup")
local lst = ui.Radios{ items = setup_menu }
lst.value = current_setup
pref:add(lst)
pref:add(ui.DefaultButtons())
if pref:run() then
current_setup = lst.value
board.setup(lst.value)
dlg.result = "restart"
dlg:close()
end
itrvl:resume()
end
dlg:add(ui.HBox():add(
view,
ui.VBox():add(
scores,
ui.Space(),
ui.HBox():add(ui.Space(), ui.VBox{gap=1}:add(
ui.Button{T"&New", result="restart"},
ui.Button{T"&Setup...", on_click=change_setup},
btn_pause,
btn_resume,
ui.Button({T"E&xit", result=false})
))
)
))
local result = dlg:run()
itrvl:stop()
if result == "restart" then
run_dialog()
end
end
return {
run_dialog = run_dialog,
}