-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathsetup-editor.lua
More file actions
282 lines (242 loc) · 7.91 KB
/
setup-editor.lua
File metadata and controls
282 lines (242 loc) · 7.91 KB
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
273
274
275
276
277
278
279
280
281
282
-- gui/setup_editor.lua
local gui = require('gui')
local widgets = require('gui.widgets')
local dialog = require('gui.dialogs')
---------------------------
-- Utility
---------------------------
local function getStringValue(val)
return tostring(val)
end
local function make_editable_list(rows, get_tab_index, flip_tabs)
local function build_choices()
local choices = {}
for _, row in ipairs(rows) do
local val
local ok, result = pcall(row.get)
if ok then val = getStringValue(result) else val = '(error)' end
table.insert(choices, {
label = row.label,
get = row.get,
set = row.set,
text = row.label .. ': ' .. val,
})
end
return choices
end
local list = widgets.List{
frame = {t=0, l=0, r=0, b=0},
choices = build_choices(),
on_submit = function(idx, choice)
local current_val = choice.get() or ''
dialog.showInputPrompt(
choice.label,
"Enter new value:",
COLOR_WHITE,
tostring(current_val),
function(new_val)
local num = tonumber(new_val)
if num ~= nil then
choice.set(num)
local current_tab = get_tab_index()
local other_tab = current_tab == 1 and 2 or 1
flip_tabs(other_tab)
flip_tabs(current_tab)
end
end
)
end
}
list.refreshDisplay = function()
local idx = list:getSelected()
list:setChoices(build_choices())
list:setSelected(idx)
end
return list
end
---------------------------
-- Variable Wrappers
---------------------------
local function try_points_remaining(screen)
local ok = pcall(function() return screen.points_remaining end)
if not ok then return nil end
return {
label = 'Embark points',
get = function() return screen.points_remaining end,
set = function(v) screen.points_remaining = v end
}
end
local function try_dwarf_skill_picks(screen, i)
local ok = pcall(function() return screen.dwarf_info[i].skill_picks_left end)
if not ok then return nil end
return {
label = ('Dwarf %d skill picks'):format(i+1),
get = function() return screen.dwarf_info[i].skill_picks_left end,
set = function(v) screen.dwarf_info[i].skill_picks_left = v end
}
end
local function try_char_skill_picks(screen, i)
local ok = pcall(function() return screen.csheet[i].skill_picks_left end)
if not ok then return nil end
return {
label = ('Char %d skill picks'):format(i+1),
get = function() return screen.csheet[i].skill_picks_left end,
set = function(v) screen.csheet[i].skill_picks_left = v end
}
end
local function try_char_att_points(screen, i)
local ok = pcall(function() return screen.csheet[i].att_points end)
if not ok then return nil end
return {
label = ('Char %d attribute pts'):format(i+1),
get = function() return screen.csheet[i].att_points end,
set = function(v) screen.csheet[i].att_points = v end
}
end
local function try_char_ip(screen, i)
local ok = pcall(function() return screen.csheet[i].ip end)
if not ok then return nil end
return {
label = ('Char %d IP'):format(i+1),
get = function() return screen.csheet[i].ip end,
set = function(v) screen.csheet[i].ip = v end
}
end
local function try_char_eqpet_points(screen, i)
local ok = pcall(function() return screen.csheet[i].eqpet_points end)
if not ok then return nil end
return {
label = ('Char %d eq/pet pts'):format(i+1),
get = function() return screen.csheet[i].eqpet_points end,
set = function(v) screen.csheet[i].eqpet_points = v end
}
end
---------------------------
-- Page Builders
---------------------------
local function make_fortress_page(screen, get_tab_index, flip_tabs)
local rows = {}
local points = try_points_remaining(screen)
if points then table.insert(rows, points) end
for i = 0, 6 do
local row = try_dwarf_skill_picks(screen, i)
if row then table.insert(rows, row) end
end
if #rows == 0 then
table.insert(rows, {
label = '(Not on fortress setup screen)',
get = function() return '' end,
set = function() end
})
end
return make_editable_list(rows, get_tab_index, flip_tabs)
end
local function make_adventure_page(screen, get_tab_index, flip_tabs)
local rows = {}
for i = 0, 1 do
local funcs = {
try_char_skill_picks,
try_char_att_points,
try_char_ip,
try_char_eqpet_points,
}
for _, f in ipairs(funcs) do
local row = f(screen, i)
if row then table.insert(rows, row) end
end
end
if #rows == 0 then
table.insert(rows, {
label = '(Not on adventure setup screen)',
get = function() return '' end,
set = function() end
})
end
return make_editable_list(rows, get_tab_index, flip_tabs)
end
---------------------------
-- UI Class
---------------------------
TabbedEditor = defclass(TabbedEditor, widgets.Window)
TabbedEditor.ATTRS{
frame_title = 'Mode Setup Editor',
frame = {w=80, h=25, r=2, t=2},
frame_style = gui.FRAME_WINDOW,
resizable = true,
}
function TabbedEditor:init()
local ok, vscreen = pcall(dfhack.gui.getCurViewscreen, true)
local screen = ok and vscreen or {}
local initial_tab = 1
if try_points_remaining(screen) then
initial_tab = 1
elseif try_char_skill_picks(screen, 0) then
initial_tab = 2
end
local tab_pages = widgets.Pages{
view_id='pages',
frame={t=2, l=0, r=0, b=0},
subviews={},
}
local get_tab_index = function()
return tab_pages:getSelected()
end
local flip_tabs = function(to_idx)
tab_pages:setSelected(to_idx)
self:refreshCurrentPage()
end
self.page_refs = {nil, nil}
local function ensure_page(tab_idx)
if not self.page_refs[tab_idx] then
if tab_idx == 1 then
self.page_refs[1] = make_fortress_page(screen, get_tab_index, flip_tabs)
elseif tab_idx == 2 then
self.page_refs[2] = make_adventure_page(screen, get_tab_index, flip_tabs)
end
tab_pages.subviews[tab_idx] = self.page_refs[tab_idx]
if tab_idx == tab_pages:getSelected() and self.page_refs[tab_idx].refreshDisplay then
self.page_refs[tab_idx]:refreshDisplay()
end
end
end
local tab_bar = widgets.TabBar{
view_id = 'tab_bar',
frame={t=0, l=0},
labels={'Fortress', 'Adventure'},
on_select = function(idx)
ensure_page(idx)
tab_pages:setSelected(idx)
self:refreshCurrentPage()
end,
get_cur_page = function()
return tab_pages:getSelected()
end,
}
self:addviews{
tab_bar,
tab_pages,
}
ensure_page(1)
ensure_page(2)
local other_tab = initial_tab == 1 and 2 or 1
tab_bar.on_select(other_tab)
tab_bar.on_select(initial_tab)
end
function TabbedEditor:refreshCurrentPage()
local idx = self.subviews.pages:getSelected()
local current_page = self.page_refs[idx]
if current_page and current_page.refreshDisplay then
current_page:refreshDisplay()
end
end
TabbedEditorScreen = defclass(TabbedEditorScreen, gui.ZScreen)
TabbedEditorScreen.ATTRS{
focus_path = 'setup_editor',
}
function TabbedEditorScreen:init()
self:addviews{TabbedEditor{}}
end
function TabbedEditorScreen:onDismiss()
view = nil
end
view = view and view:raise() or TabbedEditorScreen{}:show()