Skip to content

Commit 0383916

Browse files
Add create new note gui/notes test
1 parent 2269cbe commit 0383916

2 files changed

Lines changed: 76 additions & 12 deletions

File tree

gui/notes.lua

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local overlay = require 'plugins.overlay'
99
local utils = require 'utils'
1010

1111
local note_manager = reqscript('internal/notes/note_manager')
12+
local notes_textures = reqscript('notes').textures
1213

1314
local map_points = df.global.plotinfo.waypoints.points
1415

@@ -17,13 +18,6 @@ local RESIZE_MIN = {w=65, h=30}
1718
local NOTE_SEARCH_BATCH_SIZE = 25
1819
local OVERLAY_NAME = 'notes.map_notes'
1920

20-
local green_pin = dfhack.textures.loadTileset(
21-
'hack/data/art/note_green_pin_map.png',
22-
32,
23-
32,
24-
true
25-
)
26-
2721
NotesWindow = defclass(NotesWindow, widgets.Window)
2822
NotesWindow.ATTRS {
2923
frame_title='DF Notes',
@@ -253,6 +247,7 @@ NotesScreen = defclass(NotesScreen, gui.ZScreen)
253247
NotesScreen.ATTRS {
254248
focus_path='gui/notes',
255249
pass_movement_keys=true,
250+
enable_selector_blink = true,
256251
}
257252

258253
function NotesScreen:init()
@@ -283,7 +278,7 @@ function NotesScreen:onInput(keys)
283278
if (keys.SELECT or keys._MOUSE_L) then
284279
self.adding_note_pos = dfhack.gui.getMousePos()
285280

286-
local manager = note_manager.NoteManager{
281+
local note_manager = note_manager.NoteManager{
287282
note=nil,
288283
on_update=function()
289284
dfhack.run_command_silent('overlay trigger notes.map_notes')
@@ -294,7 +289,8 @@ function NotesScreen:onInput(keys)
294289
self:stopNoteAdd()
295290
end
296291
}:show()
297-
manager:setNotePos(self.adding_note_pos)
292+
note_manager:setNotePos(self.adding_note_pos)
293+
self.subviews.notes_window.note_manager = note_manager
298294

299295
return true
300296
elseif (keys.LEAVESCREEN or keys._MOUSE_R)then
@@ -309,7 +305,7 @@ end
309305
function NotesScreen:onRenderFrame(dc, rect)
310306
NotesScreen.super.onRenderFrame(self, dc, rect)
311307

312-
if not dfhack.screen.inGraphicsMode() and not gui.blink_visible(500) then
308+
if self.enable_selector_blink and not gui.blink_visible(500) then
313309
return
314310
end
315311

@@ -321,7 +317,9 @@ function NotesScreen:onRenderFrame(dc, rect)
321317

322318
local function get_overlay_pen(pos)
323319
if same_xy(curr_pos, pos) then
324-
local texpos = dfhack.textures.getTexposByHandle(green_pin[1])
320+
local texpos = dfhack.textures.getTexposByHandle(
321+
notes_textures.green_pin[1]
322+
)
325323
return dfhack.pen.parse{
326324
ch='X',
327325
fg=COLOR_BLUE,

test/gui/notes.lua

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local gui_notes = reqscript('gui/notes')
33
local utils = require('utils')
44
local guidm = require('gui.dwarfmode')
55
local overlay = require('plugins.overlay')
6+
local notes_textures = reqscript('notes').textures
67

78
config = {
89
target = 'gui/notes',
@@ -43,6 +44,7 @@ local function arrange_gui_notes(options)
4344
gui_notes.main()
4445

4546
local gui_notes = gui_notes.view
47+
gui_notes.enable_selector_blink = false
4648

4749
gui_notes:updateLayout()
4850
gui_notes:onRender()
@@ -238,7 +240,6 @@ function test.edit_note()
238240
note_manager.subviews.name:setText('updated green note 2')
239241
note_manager.subviews.comment:setText('updated comment 2')
240242
local screen = dfhack.gui.getCurViewscreen(true)
241-
printall(screen.widgets)
242243
gui.simulateInput(dfhack.gui.getCurViewscreen(true), 'CUSTOM_CTRL_ENTER')
243244

244245
local note_list = gui_notes.subviews.note_list:getChoices()
@@ -281,3 +282,68 @@ function test.delete_note()
281282

282283
cleanup(gui_notes)
283284
end
285+
286+
function test.create_new_note()
287+
local notes = {
288+
{name='green note 1', comment='comment 1', pos={x=1, y=1, z=1}},
289+
}
290+
291+
local gui_notes, gui_notes_window = arrange_gui_notes({ notes=notes })
292+
293+
local note_list = gui_notes.subviews.note_list:getChoices()
294+
expect.eq(#note_list, 1)
295+
296+
gui.simulateInput(dfhack.gui.getCurViewscreen(true), 'CUSTOM_CTRL_N')
297+
298+
local viewport = guidm.Viewport.get()
299+
300+
local half_x = math.floor((viewport.x1 + viewport.x2) / 2)
301+
local half_y = math.floor((viewport.y1 + viewport.y2) / 2)
302+
303+
local pos = {x=half_x, y=half_y, z=viewport.z}
304+
local screen_pos = viewport:tileToScreen(pos)
305+
df.global.cursor = pos
306+
307+
-- should not be a test function to map screen tile to mouse pos?
308+
df.global.gps.precise_mouse_x = screen_pos.x * df.global.gps.viewport_zoom_factor / 4
309+
df.global.gps.precise_mouse_y = screen_pos.y * df.global.gps.viewport_zoom_factor / 4
310+
311+
df.global.gps.mouse_x = screen_pos.x
312+
df.global.gps.mouse_y = screen_pos.y
313+
314+
gui_notes:render(gui.Painter.new())
315+
316+
local pen = dfhack.screen.readTile(screen_pos.x, screen_pos.y, true)
317+
318+
if dfhack.screen.inGraphicsMode() then
319+
local pin_textpos = dfhack.textures.getTexposByHandle(
320+
notes_textures.green_pin[1]
321+
)
322+
expect.eq(pen and pen.tile, pin_textpos)
323+
else
324+
expect.eq(pen and pen.ch, string.byte('X'))
325+
end
326+
327+
gui.simulateInput(dfhack.gui.getCurViewscreen(true), '_MOUSE_L')
328+
329+
local note_manager = gui_notes_window.note_manager
330+
331+
expect.ne(note_manager, nil)
332+
expect.eq(note_manager.visible, true)
333+
334+
note_manager.subviews.name:setText('note 2')
335+
note_manager.subviews.comment:setText('new note')
336+
337+
gui.simulateInput(dfhack.gui.getCurViewscreen(true), 'CUSTOM_CTRL_ENTER')
338+
339+
local note_list = gui_notes.subviews.note_list:getChoices()
340+
expect.eq(#note_list, 2)
341+
342+
local gui_note = note_list[2]
343+
expect.eq(gui_note.text, 'note 2')
344+
expect.eq(gui_note.point.comment, 'new note')
345+
expect.table_eq(gui_note.point.pos, pos)
346+
347+
cleanup(gui_notes)
348+
end
349+

0 commit comments

Comments
 (0)