-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdebugmenu.lua
273 lines (209 loc) · 4.89 KB
/
debugmenu.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require "class"
local MenuOption = Class(function(self, str)
self.str = str
end)
function MenuOption:Left(menu)
end
function MenuOption:Right()
end
function MenuOption:Accept()
end
function MenuOption:Cancel(menu)
return menu:Pop()
end
function MenuOption:__tostring()
if type(self.str) == "string" then return self.str end
if type(self.str) == "function" then return self.str() end
return "???"
end
-----------------------------
local DoAction = Class(MenuOption, function(self, str, fn)
MenuOption._ctor(self, str)
self.fn = fn
end)
function DoAction:Accept(menu)
self.fn(menu)
end
-----------------------------
local Submenu = Class(MenuOption, function(self, str, options, name)
self.str = str
self.name = name
self.options = options
end)
function Submenu:Accept(menu)
local opts = {}
for k,v in pairs(self.options) do table.insert(opts, v) end
table.insert(opts, DoAction("Back", function(menu) menu:Pop() end))
menu:PushOptions(opts, self.name)
end
function Submenu:__tostring()
return self.str .. "..."
end
-----------------------------
local NumericToggle = Class(MenuOption, function(self, str, min, max, getfn, setfn, step)
MenuOption._ctor(self, str)
self.step = step or 1
self.min = min
self.max = max
self.setfn = setfn
self.getfn = getfn
end)
function NumericToggle:__tostring()
return self.str .. " [ " .. tostring(self.getfn()) .. " ]"
end
function NumericToggle:Left(menu)
local val = self.getfn()
if val > self.min then
self.setfn(math.max(self.min, val - self.step))
end
end
function NumericToggle:Right(menu)
local val = self.getfn()
if val < self.max then
self.setfn(math.min(self.max, val + self.step))
end
end
-----------------------------
local CheckBox = Class(MenuOption, function(self, str, getfn, setfn)
MenuOption._ctor(self, str)
self.setfn = setfn
self.getfn = getfn
end)
function CheckBox:__tostring()
return self.str .. " [" .. (self.getfn() and "true" or "false") .. "]"
end
function CheckBox:Left(menu)
self.setfn(false)
end
function CheckBox:Right(menu)
self.setfn(true)
end
-----------------------------
local TextMenu = Class(function(self, name)
self.title = name or "MENU"
self.optionsstack = {}
self.cursor_index_stack = {}
self.name_stack = {}
self.index = 1
end)
function TextMenu:Create()
end
function TextMenu:__tostring()
local t = {}
table.insert(t, "***** ")
table.insert(t, self.title)
table.insert(t, " *****\n")
if #self.optionsstack > 0 then
for k,v in pairs(self.optionsstack[#self.optionsstack]) do
local pre = " "
if k == self.index then
pre = " >"
end
table.insert(t, pre)
table.insert(t, tostring(v))
table.insert(t, "\n")
end
end
return table.concat(t)
end
function TextMenu:PushOptions(options, name)
table.insert(self.name_stack, self.name)
self.name = name or self.name
if #self.optionsstack > 0 then
table.insert(self.cursor_index_stack, self.index)
end
self.index = 1
table.insert(self.optionsstack, options)
end
function TextMenu:Right()
local option = self:GetOption()
if option then
option:Right(self)
end
end
function TextMenu:Left()
local option = self:GetOption()
if option then
option:Left(self)
end
end
function TextMenu:Cancel()
local option = self:GetOption()
if option then
return option:Cancel(self)
end
end
function TextMenu:Accept()
local option = self:GetOption()
if option then
option:Accept(self)
end
end
function TextMenu:GetOption()
if #self.optionsstack > 0 then
return self.optionsstack[#self.optionsstack][self.index]
end
end
function TextMenu:Up()
self.index = self.index - 1
if self.index == 0 then
if #self.optionsstack[#self.optionsstack] > 0 then
self.index = #self.optionsstack[#self.optionsstack]
else
self.index = 1
end
end
end
function TextMenu:Down()
if #self.optionsstack > 0 then
self.index = self.index + 1
if self.index > #self.optionsstack[#self.optionsstack] then
self.index = 1
end
end
end
function TextMenu:AtRoot()
return #self.optionsstack <= 1
end
function TextMenu:Pop()
if #self.optionsstack > 1 then
table.remove(self.optionsstack)
if #self.cursor_index_stack > 0 then
self.index = table.remove(self.cursor_index_stack)
end
if #self.name_stack > 0 then
self.name = table.remove(self.name_stack)
end
return true
end
end
--[[
local menu = TextMenu()
local num = 5
local godmode = true
quit = false
sub = {
NumericToggle("number", 0, 10, function() return num end, function(v) num = v end),
CheckBox("god mode", function() return godmode end, function(v) godmode = v end),
DoAction("Back", function(menu) menu:Pop() end),
}
root = {
Submenu("sub", sub, "SUBMENU"),
DoAction("Quit", function(menu) quit = true end),
}
menu:PushOptions(root)
menu:Right()
menu:Left()
menu:Left()
menu:Right()
menu:Down()
menu:Right()
print(menu)
--]]
return {
Submenu = Submenu,
DoAction = DoAction,
NumericToggle = NumericToggle,
CheckBox = CheckBox,
TextMenu = TextMenu
}