forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.lua
More file actions
369 lines (320 loc) · 10.5 KB
/
notes.lua
File metadata and controls
369 lines (320 loc) · 10.5 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
-- Map notes
--@ module = true
local gui = require 'gui'
local widgets = require 'gui.widgets'
local guidm = require 'gui.dwarfmode'
local script = require 'gui.script'
local overlay = require 'plugins.overlay'
local utils = require 'utils'
local note_manager = reqscript('internal/notes/note_manager')
local notes_textures = reqscript('notes').textures
local map_points = df.global.plotinfo.waypoints.points
local NOTE_LIST_RESIZE_MIN = {w=26}
local RESIZE_MIN = {w=65, h=30}
local NOTE_SEARCH_BATCH_SIZE = 25
local OVERLAY_NAME = 'notes.map_notes'
NotesWindow = defclass(NotesWindow, widgets.Window)
NotesWindow.ATTRS {
frame_title='DF Notes',
resizable=true,
resize_min=RESIZE_MIN,
frame_inset={l=0,r=0,t=0,b=0},
on_note_add=DEFAULT_NIL
}
function NotesWindow:init()
self.selected_note = nil
self.note_manager = nil
self.curr_search_phrase = nil
local left_panel_content = {
widgets.Panel{
frame={l=0,h=3},
frame_style=gui.FRAME_INTERIOR,
subviews={
widgets.EditField{
view_id='search',
on_change=self:callback('loadFilteredNotes'),
on_submit=function()
self.subviews.note_list:submit()
end
},
}
},
widgets.List{
view_id='note_list',
frame={l=0,b=2},
frame_inset={t=1},
row_height=1,
on_select=function (ind, note)
self:loadNote(note)
end,
on_submit=function (ind, note)
self:loadNote(note)
dfhack.gui.pauseRecenter(note.point.pos)
end
},
}
self:addviews{
widgets.Panel{
view_id='note_list_panel',
frame={l=0, w=NOTE_LIST_RESIZE_MIN.w, t=0, b=1},
visible=true,
frame_inset={l=1,t=1,b=1,r=1},
autoarrange_subviews=true,
subviews=left_panel_content,
},
widgets.HotkeyLabel{
view_id='create',
frame={l=1,b=1,h=1},
auto_width=true,
label='New note',
key='CUSTOM_CTRL_N',
visible=true,
on_activate=function()
if self.on_note_add then
self:on_note_add()
end
end,
},
widgets.Divider{
frame={l=NOTE_LIST_RESIZE_MIN.w,t=0,b=0,w=1},
interior_b=false,
frame_style_t=false,
frame_style_b=false,
},
widgets.Panel{
view_id='note_details',
frame={l=NOTE_LIST_RESIZE_MIN.w + 1,t=0,b=0},
frame_inset=1,
subviews={
widgets.Panel{
view_id="name_panel",
frame_title='Name',
frame_style=gui.FRAME_INTERIOR,
frame={l=0,r=0,t=0,h=4},
frame_inset={l=1,r=1},
subviews={
widgets.WrappedLabel{
view_id='name',
auto_height=false,
frame={l=0,r=0,t=0,b=0},
},
},
},
widgets.Panel{
view_id="comment_panel",
frame_title='Comment',
frame_style=gui.FRAME_INTERIOR,
frame={l=0,r=0,t=4,b=2},
frame_inset={l=1,r=1,t=1},
subviews={
widgets.WrappedLabel{
view_id='comment',
auto_height=false,
frame={l=0,r=0,t=0,b=0},
},
}
},
widgets.Panel{
frame={l=0,r=0,b=0,h=2},
frame_inset={l=1,r=1,t=1},
subviews={
widgets.HotkeyLabel{
view_id='edit',
frame={l=0,t=0,h=1},
auto_width=true,
label='Edit',
key='CUSTOM_CTRL_E',
on_activate=function() self:showNoteManager(self.selected_note) end,
},
widgets.HotkeyLabel{
view_id='delete',
frame={r=0,t=0,h=1},
auto_width=true,
label='Delete',
key='CUSTOM_CTRL_D',
on_activate=function() self:deleteNote(self.selected_note) end,
},
}
}
}
}
}
end
function NotesWindow:postUpdateLayout()
if self.curr_search_phrase == nil then
self:loadFilteredNotes('', true)
end
end
function NotesWindow:showNoteManager(note)
if self.note_manager ~= nil then
self.note_manager:dismiss()
end
self.note_manager = note_manager.NoteManager{
note=note,
on_update=function()
self:reloadFilteredNotes()
dfhack.run_command_silent('overlay trigger notes.map_notes')
end,
on_dismiss=function() self.visible = true end
}
self.visible = false
return self.note_manager:show():raise()
end
function NotesWindow:deleteNote(note)
for ind, map_point in pairs(map_points) do
if map_point.id == note.point.id then
map_points:erase(ind)
break
end
end
self:reloadFilteredNotes()
end
function NotesWindow:loadNote(note)
self.selected_note = note
if note == nil then
return
end
self.subviews.name.text_to_wrap = self.selected_note.point.name
self.subviews.comment.text_to_wrap = self.selected_note.point.comment
self.subviews.note_details:updateLayout()
end
function NotesWindow:reloadFilteredNotes()
self:loadFilteredNotes(self.curr_search_phrase, true)
end
function NotesWindow:loadFilteredNotes(search_phrase, force)
local full_list_loaded = self.curr_search_phrase == ''
search_phrase = search_phrase:lower()
self.curr_search_phrase = search_phrase
script.start(function ()
if #search_phrase == 0 and full_list_loaded and not force then
return
end
local choices = {}
for ind, map_point in ipairs(map_points) do
if ind > 0 and ind % NOTE_SEARCH_BATCH_SIZE == 0 then
script.sleep(1, 'frames')
end
if self.curr_search_phrase ~= search_phrase then
-- stop the work if user provided new search phrase
return
end
if (
#map_point.name > 0 and
utils.search_text(map_point.name, search_phrase)
) then
table.insert(choices, {
text=map_point.name,
point=map_point
})
end
end
self.subviews.note_list:setChoices(choices)
local sel_ind, sel_note = self.subviews.note_list:getSelected()
self:loadNote(sel_note)
end)
end
NotesScreen = defclass(NotesScreen, gui.ZScreen)
NotesScreen.ATTRS {
focus_path='gui/notes',
pass_movement_keys=true,
enable_selector_blink = true,
}
function NotesScreen:init()
self.is_adding_note = false
self.adding_note_pos = nil
self:addviews{
NotesWindow{
view_id='notes_window',
frame={w=RESIZE_MIN.w, h=35},
on_note_add=self:callback('startNoteAdd')
},
}
end
function NotesScreen:startNoteAdd()
self.adding_note_pos = nil
self.subviews.notes_window.visible = false
self.is_adding_note = true
end
function NotesScreen:stopNoteAdd()
self.subviews.notes_window.visible = true
self.is_adding_note = false
end
function NotesScreen:onInput(keys)
if self.is_adding_note then
if (keys.SELECT or keys._MOUSE_L) then
self.adding_note_pos = dfhack.gui.getMousePos()
local note_manager = note_manager.NoteManager{
note=nil,
on_update=function()
dfhack.run_command_silent('overlay trigger notes.map_notes')
self.subviews.notes_window:reloadFilteredNotes()
self:stopNoteAdd()
end,
on_dismiss=function()
self:stopNoteAdd()
end
}:show()
note_manager:setNotePos(self.adding_note_pos)
self.subviews.notes_window.note_manager = note_manager
return true
elseif (keys.LEAVESCREEN or keys._MOUSE_R)then
self:stopNoteAdd()
return true
end
end
return NotesScreen.super.onInput(self, keys)
end
function NotesScreen:onRenderFrame(dc, rect)
NotesScreen.super.onRenderFrame(self, dc, rect)
if self.enable_selector_blink and not gui.blink_visible(500) then
return
end
if self.is_adding_note then
local curr_pos = self.adding_note_pos or dfhack.gui.getMousePos()
if not curr_pos then
return
end
local function get_overlay_pen(pos)
if same_xy(curr_pos, pos) then
local texpos = dfhack.textures.getTexposByHandle(
notes_textures.green_pin[1]
)
return dfhack.pen.parse{
ch='X',
fg=COLOR_BLUE,
tile=texpos
}
end
end
guidm.renderMapOverlay(get_overlay_pen, {
x1=curr_pos.x,
y1=curr_pos.y,
x2=curr_pos.x,
y2=curr_pos.y,
})
end
end
function NotesScreen:onAboutToShow()
if not overlay.isOverlayEnabled(OVERLAY_NAME) then
self.should_disable_overlay = true
overlay.overlay_command({'enable', 'notes.map_notes'})
end
end
function NotesScreen:onDismiss()
if self.should_disable_overlay then
overlay.overlay_command({'disable', 'notes.map_notes'})
end
if self.subviews.notes_window.note_manager then
self.subviews.notes_window.note_manager:dismiss()
end
view = nil
end
function main()
if not dfhack.isMapLoaded() or not dfhack.world.isFortressMode() then
qerror('notes requires a fortress map to be loaded')
end
view = view and view:raise() or NotesScreen{}:show()
end
if not dfhack_flags.module then
main()
end