|
| 1 | +local gui = require('gui') |
| 2 | +local overlay = require('plugins.overlay') |
| 3 | +local guidm = require('gui.dwarfmode') |
| 4 | +local utils = require('utils') |
| 5 | +local notes_textures = reqscript('notes').textures |
| 6 | + |
| 7 | +local waypoints = df.global.plotinfo.waypoints |
| 8 | +local map_points = df.global.plotinfo.waypoints.points |
| 9 | + |
| 10 | +config = { |
| 11 | + target = 'notes', |
| 12 | + mode = 'fortress' |
| 13 | +} |
| 14 | + |
| 15 | +local map_points_backup = nil |
| 16 | + |
| 17 | +local function install_notes_overlay(options) |
| 18 | + options = options or {} |
| 19 | + |
| 20 | + map_points_backup = utils.clone(map_points) |
| 21 | + map_points:resize(0) |
| 22 | + |
| 23 | + overlay.rescan() |
| 24 | + overlay.overlay_command({'enable', 'notes.map_notes'}) |
| 25 | + -- if overlay |
| 26 | + local overlay_state = overlay.get_state() |
| 27 | + if not overlay_state.config['notes.map_notes'].enabled then |
| 28 | + qerror('can not enable notes.map_notes overlay') |
| 29 | + end |
| 30 | + |
| 31 | + return overlay_state.db['notes.map_notes'].widget |
| 32 | +end |
| 33 | + |
| 34 | +local function reload_notes() |
| 35 | + overlay.overlay_command({'trigger', 'notes.map_notes'}) |
| 36 | +end |
| 37 | + |
| 38 | +local function cleanup(notes_overlay) |
| 39 | + if notes_overlay.note_manager then |
| 40 | + notes_overlay.note_manager:dismiss() |
| 41 | + end |
| 42 | + |
| 43 | + df.global.plotinfo.waypoints.points:resize(#map_points_backup) |
| 44 | + for ind, map_point in ipairs(map_points_backup) do |
| 45 | + df.global.plotinfo.waypoints.points[ind - 1] = map_point |
| 46 | + end |
| 47 | + map_points_backup = nil |
| 48 | + |
| 49 | + reload_notes() |
| 50 | +end |
| 51 | + |
| 52 | +local function add_note(notes_overlay, pos, name, comment) |
| 53 | + df.global.cursor = copyall(pos) |
| 54 | + |
| 55 | + local cmd_result = overlay.overlay_command({ |
| 56 | + 'trigger', 'notes.map_notes', 'add' |
| 57 | + }) |
| 58 | + |
| 59 | + notes_overlay.note_manager.subviews.name:setText(name) |
| 60 | + notes_overlay.note_manager.subviews.comment:setText(comment) |
| 61 | + |
| 62 | + gui.simulateInput(dfhack.gui.getCurViewscreen(true), 'CUSTOM_CTRL_ENTER') |
| 63 | +end |
| 64 | + |
| 65 | + |
| 66 | +function test.load_notes_overlay() |
| 67 | + local notes_overlay = install_notes_overlay() |
| 68 | + expect.ne(notes_overlay, nil) |
| 69 | + cleanup(notes_overlay) |
| 70 | +end |
| 71 | + |
| 72 | +function test.trigger_add_new_note_modal() |
| 73 | + local notes_overlay = install_notes_overlay() |
| 74 | + |
| 75 | + local cmd_result = overlay.overlay_command({ |
| 76 | + 'trigger', 'notes.map_notes', 'add' |
| 77 | + }) |
| 78 | + |
| 79 | + expect.eq(cmd_result, true) |
| 80 | + expect.ne(notes_overlay.note_manager, nil) |
| 81 | + expect.eq(notes_overlay.note_manager.visible, true) |
| 82 | + |
| 83 | + cleanup(notes_overlay) |
| 84 | +end |
| 85 | + |
| 86 | +function test.render_existing_notes() |
| 87 | + local notes_overlay = install_notes_overlay() |
| 88 | + |
| 89 | + local pos_1 = {x=10, y=20, z=0} |
| 90 | + local pos_2 = {x=10, y=20, z=0} |
| 91 | + local pos_3 = {x=10, y=20, z=0} |
| 92 | + |
| 93 | + add_note(notes_overlay, pos_1, 'note 1', 'first note') |
| 94 | + add_note(notes_overlay, pos_2, 'note 2', 'second note') |
| 95 | + add_note(notes_overlay, pos_3, 'note 3', 'last note') |
| 96 | + |
| 97 | + reload_notes() |
| 98 | + |
| 99 | + local viewport = guidm.Viewport.get() |
| 100 | + |
| 101 | + local pin_textpos = dfhack.textures.getTexposByHandle( |
| 102 | + notes_textures.green_pin[1] |
| 103 | + ) |
| 104 | + |
| 105 | + for _, pos in ipairs({pos_1, pos_2, pos_3}) do |
| 106 | + dfhack.gui.revealInDwarfmodeMap(pos) |
| 107 | + |
| 108 | + -- TODO: find better way to wait for overlay re-render |
| 109 | + delay(10) |
| 110 | + |
| 111 | + local screen_pos = viewport:tileToScreen(pos) |
| 112 | + local pen = dfhack.screen.readTile(screen_pos.x, screen_pos.y, true) |
| 113 | + expect.eq(pen and pen.tile, pin_textpos) |
| 114 | + end |
| 115 | + |
| 116 | + cleanup(notes_overlay) |
| 117 | +end |
| 118 | + |
| 119 | +function test.edit_clicked_note() |
| 120 | + local notes_overlay = install_notes_overlay() |
| 121 | + |
| 122 | + local pos = {x=10, y=20, z=0} |
| 123 | + add_note(notes_overlay, pos, 'note 1', 'note to edit') |
| 124 | + add_note(notes_overlay, {x=20, y=10, z=2}, 'note 2', 'other note') |
| 125 | + add_note(notes_overlay, {x=0, y=10, z=5}, 'note 3', 'another note') |
| 126 | + |
| 127 | + reload_notes() |
| 128 | + dfhack.screen.invalidate() |
| 129 | + dfhack.gui.revealInDwarfmodeMap(pos) |
| 130 | + |
| 131 | + -- TODO: find better way to wait for overlay re-render |
| 132 | + delay(10) |
| 133 | + |
| 134 | + notes_overlay:updateLayout() |
| 135 | + |
| 136 | + local viewport = guidm.Viewport.get() |
| 137 | + local screen_pos = viewport:tileToScreen(pos) |
| 138 | + |
| 139 | + local rect = gui.ViewRect{rect=notes_overlay.frame_rect} |
| 140 | + |
| 141 | + -- should not be a test function to map screen tile to mouse pos? |
| 142 | + df.global.gps.precise_mouse_x = screen_pos.x * df.global.gps.viewport_zoom_factor / 4 |
| 143 | + df.global.gps.precise_mouse_y = screen_pos.y * df.global.gps.viewport_zoom_factor / 4 |
| 144 | + |
| 145 | + local screen = dfhack.gui.getCurViewscreen(true) |
| 146 | + gui.simulateInput(screen, { |
| 147 | + _MOUSE_L=true, |
| 148 | + }) |
| 149 | + |
| 150 | + local note_manager = notes_overlay.note_manager |
| 151 | + expect.ne(note_manager, nil) |
| 152 | + |
| 153 | + expect.eq(note_manager.subviews.name:getText(), 'note 1') |
| 154 | + expect.eq(note_manager.subviews.comment:getText(), 'note to edit') |
| 155 | + |
| 156 | + note_manager.subviews.name:setText('edited note 1') |
| 157 | + note_manager.subviews.comment:setText('edited comment') |
| 158 | + |
| 159 | + gui.simulateInput(dfhack.gui.getCurViewscreen(true), 'CUSTOM_CTRL_ENTER') |
| 160 | + |
| 161 | + expect.eq(map_points[0].name, 'edited note 1') |
| 162 | + expect.eq(map_points[0].comment, 'edited comment') |
| 163 | + |
| 164 | + cleanup(notes_overlay) |
| 165 | +end |
| 166 | + |
| 167 | +function test.delete_clicked_note() |
| 168 | + local notes_overlay = install_notes_overlay() |
| 169 | + |
| 170 | + local pos = {x=10, y=20, z=0} |
| 171 | + add_note(notes_overlay, {x=20, y=10, z=2}, 'note 1', 'note to edit') |
| 172 | + add_note(notes_overlay, pos, 'note 2', 'other note') |
| 173 | + add_note(notes_overlay, {x=0, y=10, z=5}, 'note 3', 'another note') |
| 174 | + |
| 175 | + reload_notes() |
| 176 | + dfhack.screen.invalidate() |
| 177 | + dfhack.gui.revealInDwarfmodeMap(pos) |
| 178 | + |
| 179 | + notes_overlay:updateLayout() |
| 180 | + |
| 181 | + local viewport = guidm.Viewport.get() |
| 182 | + local screen_pos = viewport:tileToScreen(pos) |
| 183 | + |
| 184 | + local rect = gui.ViewRect{rect=notes_overlay.frame_rect} |
| 185 | + |
| 186 | + -- should not be a test function to map screen tile to mouse pos? |
| 187 | + df.global.gps.precise_mouse_x = screen_pos.x * df.global.gps.viewport_zoom_factor / 4 |
| 188 | + df.global.gps.precise_mouse_y = screen_pos.y * df.global.gps.viewport_zoom_factor / 4 |
| 189 | + |
| 190 | + local screen = dfhack.gui.getCurViewscreen(true) |
| 191 | + gui.simulateInput(screen, { |
| 192 | + _MOUSE_L=true, |
| 193 | + }) |
| 194 | + |
| 195 | + expect.eq(#map_points, 3) |
| 196 | + |
| 197 | + gui.simulateInput(dfhack.gui.getCurViewscreen(true), 'CUSTOM_CTRL_D') |
| 198 | + |
| 199 | + expect.eq(#map_points, 2) |
| 200 | + expect.eq(map_points[0].name, 'note 1') |
| 201 | + expect.eq(map_points[1].name, 'note 3') |
| 202 | + |
| 203 | + cleanup(notes_overlay) |
| 204 | +end |
0 commit comments