Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathseedwatch.lua
More file actions
311 lines (281 loc) · 9.67 KB
/
Copy pathseedwatch.lua
File metadata and controls
311 lines (281 loc) · 9.67 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
local dlg = require('gui.dialogs')
local gui = require('gui')
local plugin = require('plugins.seedwatch')
local widgets = require('gui.widgets')
local CH_UP = string.char(30)
local CH_DN = string.char(31)
Seedwatch = defclass(Seedwatch, widgets.Window)
Seedwatch.ATTRS{
frame_title='Seedwatch',
frame={w=58, h=25},
frame_inset={t=1},
resizable=true,
}
local function sort_noop(a, b)
-- this function is used as a marker and never actually gets called
error('sort_noop should not be called')
end
local function sort_by_name_desc(a, b)
return a.data.name < b.data.name
end
local function sort_by_name_asc(a, b)
return a.data.name > b.data.name
end
local function sort_by_quantity_desc(a, b)
if a.data.quantity == b.data.quantity then
return sort_by_name_desc(a, b)
end
return a.data.quantity > b.data.quantity
end
local function sort_by_quantity_asc(a, b)
if a.data.quantity == b.data.quantity then
return sort_by_name_desc(a, b)
end
return a.data.quantity < b.data.quantity
end
local function sort_by_target_desc(a, b)
if a.data.target == b.data.target then
return sort_by_name_desc(a, b)
end
return a.data.target > b.data.target
end
local function sort_by_target_asc(a, b)
if a.data.target == b.data.target then
return sort_by_name_desc(a, b)
end
return a.data.target < b.data.target
end
function Seedwatch:init()
self:addviews{
widgets.CycleHotkeyLabel{
view_id='sort',
frame={l=1, t=0, w=31},
label='Sort by:',
key='CUSTOM_SHIFT_S',
options={
{label='Name'..CH_DN, value=sort_by_name_desc},
{label='Name'..CH_UP, value=sort_by_name_asc},
{label='Quantity'..CH_DN, value=sort_by_quantity_desc},
{label='Quantity'..CH_UP, value=sort_by_quantity_asc},
{label='Target'..CH_DN, value=sort_by_target_desc},
{label='Target'..CH_UP, value=sort_by_target_asc},
},
initial_option=sort_by_name_desc,
on_change=self:callback('refresh', 'sort'),
},
widgets.ToggleHotkeyLabel{
view_id='hide_nostock',
frame={t=0, l=24, w=31},
key='CUSTOM_CTRL_H',
label='Show only in stock:',
on_change=self:callback('refresh', 'sort'),
},
widgets.Panel{
view_id='list_panel',
frame={t=2, l=0, r=0, b=4},
frame_style=gui.FRAME_INTERIOR,
subviews={
widgets.CycleHotkeyLabel{
view_id='sort_name',
frame={t=0, l=0, w=5},
options={
{label='Name', value=sort_noop},
{label='Name'..CH_DN, value=sort_by_name_desc},
{label='Name'..CH_UP, value=sort_by_name_asc},
},
initial_option=sort_by_name_desc,
option_gap=0,
on_change=self:callback('refresh', 'sort_name'),
},
widgets.CycleHotkeyLabel{
view_id='sort_quantity',
frame={t=0, r=12, w=9},
options={
{label='Quantity', value=sort_noop},
{label='Quantity'..CH_DN, value=sort_by_quantity_desc},
{label='Quantity'..CH_UP, value=sort_by_quantity_asc},
},
option_gap=0,
on_change=self:callback('refresh', 'sort_quantity'),
},
widgets.CycleHotkeyLabel{
view_id='sort_target',
frame={t=0, r=3, w=7},
options={
{label='Target', value=sort_noop},
{label='Target'..CH_DN, value=sort_by_target_desc},
{label='Target'..CH_UP, value=sort_by_target_asc},
},
option_gap=0,
on_change=self:callback('refresh', 'sort_target'),
},
widgets.Label{
view_id='disabled_warning',
visible=function() return not plugin.isEnabled() end,
frame={t=3, h=1},
auto_width=true,
text={"Please enable seedwatch to change settings"},
text_pen=COLOR_YELLOW
},
widgets.List{
view_id='list',
frame={t=2, b=0},
visible=plugin.isEnabled,
on_double_click=self:callback('prompt_for_new_target'),
},
},
},
widgets.Panel{
view_id='footer',
frame={l=1, r=1, b=0, h=3},
subviews={
widgets.Label{
frame={t=0, l=0},
text={
'Double click on a row or hit ',
{text='Enter', pen=COLOR_LIGHTGREEN},
' to set the target.'
},
},
widgets.ToggleHotkeyLabel{
view_id='enable_toggle',
frame={t=2, l=0, w=29},
label='Seedwatch is',
key='CUSTOM_CTRL_E',
options={{value=true, label='Enabled', pen=COLOR_GREEN},
{value=false, label='Disabled', pen=COLOR_RED}},
on_change=function(val)
plugin.setEnabled(val)
self:refresh()
end,
},
widgets.HotkeyLabel{
frame={t=2, l=31},
label='Set all targets',
key='CUSTOM_CTRL_A',
auto_width=true,
on_activate=self:callback('prompt_for_all_targets'),
},
},
},
}
end
function Seedwatch:render(dc)
self.subviews.enable_toggle:setOption(plugin.isEnabled())
Seedwatch.super.render(self, dc)
end
function Seedwatch:onInput(keys)
if keys.SELECT then
self:prompt_for_new_target(self.subviews.list:getSelected())
end
return Seedwatch.super.onInput(self, keys)
end
function Seedwatch:postUpdateLayout()
self:refresh()
end
local SORT_WIDGETS = {
'sort',
'sort_name',
'sort_quantity',
'sort_target',
}
local function make_row_text(name, quantity, target, row_width)
return {
{text=name, width=row_width-22, pad_char=' '},
' ', {text=quantity, width=7, rjustify=true, pad_char=' '},
' ', {text=target, width=7, rjustify=true, pad_char=' '},
}
end
local plants_all = df.global.world.raws.plants.all
function Seedwatch:refresh(sort_widget, sort_fn)
sort_widget = sort_widget or 'sort'
sort_fn = sort_fn or self.subviews.sort:getOptionValue()
if sort_fn == sort_noop then
self.subviews[sort_widget]:cycle()
return
end
for _,widget_name in ipairs(SORT_WIDGETS) do
self.subviews[widget_name]:setOption(sort_fn)
end
local watch_map, seed_counts = plugin.seedwatch_getData()
local hide_nostock = self.subviews.hide_nostock:getOptionValue()
local list = self.subviews.list
local row_width = list.frame_body.width
local choices = {}
for idx,target in pairs(watch_map) do
if hide_nostock and not seed_counts[idx] then goto continue end
local name = plants_all[idx].seed_singular
local quantity = seed_counts[idx] or 0
table.insert(choices, {
text=make_row_text(name, quantity, target, row_width),
data={
id=plants_all[idx].id,
name=name,
quantity=quantity,
target=target,
},
})
::continue::
end
table.sort(choices, self.subviews.sort:getOptionValue())
local selected = list:getSelected()
list:setChoices(choices, selected)
end
local function check_number(target, text)
if not target then
dlg.showMessage('Invalid Number', 'This is not a number: '..text..NEWLINE..'(for zero enter a 0)', COLOR_LIGHTRED)
return false
end
if target < 0 then
dlg.showMessage('Invalid Number', 'Negative numbers make no sense!', COLOR_LIGHTRED)
return false
end
return true
end
function Seedwatch:prompt_for_new_target(_, choice)
dlg.showInputPrompt(
'Set target',
('Enter desired target for %s:'):format(choice.data.name),
COLOR_WHITE,
tostring(choice.data.target),
function(text)
local target = tonumber(text)
if check_number(target, text) then
plugin.seedwatch_setTarget(choice.data.id, target)
self:refresh()
end
end
)
end
function Seedwatch:prompt_for_all_targets()
dlg.showInputPrompt(
'Set all targets',
'Enter desired target for all seed types',
COLOR_WHITE,
'',
function(text)
local target = tonumber(text)
if check_number(target, text) then
plugin.seedwatch_setTarget('all', target)
self:refresh()
end
end
)
end
--
-- SeedwatchScreen
--
SeedwatchScreen = defclass(SeedwatchScreen, gui.ZScreen)
SeedwatchScreen.ATTRS{
focus_path='seedwatch',
}
function SeedwatchScreen:init()
self:addviews{Seedwatch{}}
end
function SeedwatchScreen:onDismiss()
view = nil
end
if not dfhack.isMapLoaded() or not dfhack.world.isFortressMode() then
qerror('seedwatch requires a fort map to be loaded')
end
view = view and view:raise() or SeedwatchScreen{}:show()
You can’t perform that action at this time.
