Skip to content

Commit

Permalink
Allow to iterate map notes on same tile by click
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-obrebski committed Sep 3, 2024
1 parent ea3b50e commit 50b53bc
Showing 1 changed file with 53 additions and 18 deletions.
71 changes: 53 additions & 18 deletions notes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,67 @@ NotesOverlay.ATTRS{

function NotesOverlay:init()
self.notes = {}
self.note_manager = nil
self.last_click_pos = {}
self:reloadVisibleNotes()
end

function NotesOverlay:overlay_onupdate()
self:reloadVisibleNotes()
end

function NotesOverlay:overlay_trigger()
print('called')
-- self:reloadVisibleNotes()
function NotesOverlay:overlay_trigger(args)
return self:showNoteManager()
end


function NotesOverlay:onInput(keys)
if keys._MOUSE_L then
local pos = dfhack.gui.getMousePos()

local notes = df.global.plotinfo.waypoints.points
for _, note in pairs(notes) do
if same_xyz(note.pos, pos) then
NoteManager{
note=note,
on_update=function() self:reloadVisibleNotes() end
}:show()
local note = self:clickedNote(pos)
if note ~= nil then
self:showNoteManager(note)
end
end
end

function NotesOverlay:clickedNote(click_pos)
local pos_curr_note = same_xyz(self.last_click_pos, click_pos)
and self.note_manager
and self.note_manager.note
or nil

self.last_click_pos = click_pos

local notes = df.global.plotinfo.waypoints.points

local last_note_on_pos = nil
local first_note_on_pos = nil
for _, note in pairs(notes) do
if same_xyz(note.pos, click_pos) then
if last_note_on_pos == pos_curr_note then
return note
end

first_note_on_pos = first_note_on_pos or note
last_note_on_pos = note
end
end

return first_note_on_pos
end

function NotesOverlay:showNoteManager(note)
if self.note_manager ~= nil then
self.note_manager:dismiss()
end

self.note_manager = NoteManager{
note=note,
on_update=function() self:reloadVisibleNotes() end
}

return self.note_manager:show()
end

function NotesOverlay:viewportChanged()
Expand All @@ -70,17 +103,16 @@ function NotesOverlay:onRenderFrame(dc)
local texpos = dfhack.textures.getTexposByHandle(green_pin[1])
dc:pen({fg=COLOR_BLACK, bg=COLOR_LIGHTCYAN, tile=texpos})

for _, point in pairs(self.notes) do
for _, note in pairs(self.notes) do
dc
:seek(point.pos.x, point.pos.y)
:seek(note.screen_pos.x, note.screen_pos.y)
:char('N')
end

dc:map(false)
end

function NotesOverlay:reloadVisibleNotes()
print('reloading notes')
self.notes = {}

local viewport = guidm.Viewport.get()
Expand All @@ -94,9 +126,8 @@ function NotesOverlay:reloadVisibleNotes()
if viewport:isVisible(point.pos) then
local pos = viewport:tileToScreen(point.pos)
table.insert(self.notes, {
name=point.name,
comment=point.comment,
pos=pos
point=point,
screen_pos=pos
})
end
end
Expand Down Expand Up @@ -261,6 +292,10 @@ function NoteManager:deleteNote()
self:dismiss()
end

function NoteManager:onDismiss()
self.note = nil
end

-- register widgets
OVERLAY_WIDGETS = {
map_notes=NotesOverlay
Expand All @@ -278,7 +313,7 @@ local function main(args)
return
end

return NoteManager{}:show()
return dfhack.internal.runCommand('overlay trigger notes.map_notes')
end
end

Expand Down

0 comments on commit 50b53bc

Please sign in to comment.