Skip to content

Commit

Permalink
Add note add feature to gui/notes tool
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-obrebski committed Sep 8, 2024
1 parent 166675b commit 83b4da0
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 22 deletions.
142 changes: 126 additions & 16 deletions gui/notes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

local gui = require 'gui'
local widgets = require 'gui.widgets'
local guidm = require('gui.dwarfmode')
local script = require 'gui.script'
local text_editor = reqscript('internal/journal/text_editor')
local note_manager = reqscript('internal/notes/note_manager')
Expand All @@ -13,12 +14,20 @@ local NOTE_LIST_RESIZE_MIN = {w=26}
local RESIZE_MIN = {w=65, h=30}
local NOTE_SEARCH_BATCH_SIZE = 25

local green_pin = dfhack.textures.loadTileset(
'hack/data/art/note_green_pin_map.png',
32,
32,
true
)

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()
Expand All @@ -30,7 +39,7 @@ function NotesWindow:init()
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},
frame_inset={l=1,t=1,b=1,r=1},
autoarrange_subviews=true,
subviews={
widgets.HotkeyLabel {
Expand Down Expand Up @@ -67,19 +76,30 @@ function NotesWindow:init()
},
widgets.List{
view_id='note_list',
frame={l=0},
frame={l=0,b=2},
frame_inset={t=0},
row_height=1,
on_submit=function (ind, note)
self:loadNote(note)
dfhack.gui.pauseRecenter(note.point.pos)
end
},
}
},
},
widgets.HotkeyLabel{
view_id='create',
frame={l=1,b=1,h=1},
auto_width=true,
label='New note',
key='CUSTOM_ALT_N',
visible=edit_mode,
on_activate=function()
if self.on_note_add then
self:on_note_add()
end
end,
},
widgets.Divider{
view_id='note_list_divider',

frame={l=NOTE_LIST_RESIZE_MIN.w,t=0,b=0,w=1},

interior_b=false,
Expand Down Expand Up @@ -155,6 +175,10 @@ function NotesWindow:showNoteManager(note)

self.note_manager = note_manager.NoteManager{
note=note,
on_update=function()
self:reloadFilteredNotes()
dfhack.internal.runCommand('overlay trigger notes.map_notes')
end,
on_dismiss=function() self.visible = true end
}

Expand All @@ -170,7 +194,7 @@ function NotesWindow:deleteNote(note)
end
end

self:loadFilteredNotes(self.curr_search_phrase, true)
self:reloadFilteredNotes()
end

function NotesWindow:loadNote(note)
Expand All @@ -180,16 +204,26 @@ function NotesWindow:loadNote(note)
return
end

local note_details_frame = self.subviews.name_panel.frame_body
if note_details_frame ~= nil then
local note_width = self.subviews.name_panel.frame_body.width
local wrapped_name = note.point.name:wrap(note_width)
local wrapped_comment = note.point.comment:wrap(note_width)

self.subviews.name:setText(wrapped_name)
self.subviews.comment:setText(wrapped_comment)
self.subviews.note_details:updateLayout()
-- self.note_width_calculated = false
end

function NotesWindow:postUpdateLayout()
if self.selected_note == nil then
return
end
local note_details_frame = self.subviews.name_panel.frame_body

local note_width = self.subviews.name_panel.frame_body.width
local wrapped_name = self.selected_note.point.name:wrap(note_width)
local wrapped_comment = self.selected_note.point.comment:wrap(note_width)

self.subviews.name:setText(wrapped_name)
self.subviews.comment:setText(wrapped_comment)
self.subviews.note_details:updateLayout()
end

function NotesWindow:reloadFilteredNotes()
self:loadFilteredNotes(self.curr_search_phrase, true)
end

function NotesWindow:loadFilteredNotes(search_phrase, force)
Expand Down Expand Up @@ -234,24 +268,100 @@ function NotesWindow:loadFilteredNotes(search_phrase, force)

local sel_ind, sel_note = self.subviews.note_list:getSelected()
self:loadNote(sel_note)
self:updateLayout()
end)
end


NotesScreen = defclass(NotesScreen, gui.ZScreen)
NotesScreen.ATTRS {
focus_path='gui/notes',
pass_movement_keys=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 manager = note_manager.NoteManager{
note=nil,
on_update=function()
self.subviews.notes_window:reloadFilteredNotes()
dfhack.internal.runCommand('overlay trigger notes.map_notes')
self:dismiss()
end,
on_dismiss=function()
self:stopNoteAdd()
end
}:show()
manager:setNotePos(self.adding_note_pos)

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 not dfhack.screen.inGraphicsMode() 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(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:onDismiss()
view = nil
end
Expand Down
12 changes: 8 additions & 4 deletions internal/journal/text_editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,18 @@ function TextEditor:renderSubviews(dc)
end

function TextEditor:onInput(keys)
if (self.subviews.scrollbar.is_dragging) then
return self.subviews.scrollbar:onInput(keys)
end

if keys._MOUSE_L and self:getMousePos() then
self:setFocus(true)
end

if not self.focus then
return false
end

if (self.subviews.scrollbar.is_dragging) then
return self.subviews.scrollbar:onInput(keys)
end

return TextEditor.super.onInput(self, keys)
end

Expand Down
10 changes: 9 additions & 1 deletion internal/notes/note_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ NoteManager.ATTRS{
}

function NoteManager:init()
self.note_pos = nil
local edit_mode = self.note ~= nil

self:addviews{
Expand Down Expand Up @@ -95,8 +96,12 @@ function NoteManager:init()
}
end

function NoteManager:setNotePos(note_pos)
self.notes_pos = note_pos
end

function NoteManager:createNote()
local cursor_pos = guidm.getCursorPos()
local cursor_pos = self.notes_pos or guidm.getCursorPos()
if cursor_pos == nil then
dfhack.printerr('Enable keyboard cursor to add a note.')
return
Expand Down Expand Up @@ -145,6 +150,9 @@ function NoteManager:saveNote()

self.note.point.name = name
self.note.point.comment = comment
if self.notes_pos then
self.note.pos=self.notes_pos
end

if self.on_update then
self.on_update()
Expand Down
2 changes: 1 addition & 1 deletion notes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function NotesOverlay:overlay_onupdate()
end

function NotesOverlay:overlay_trigger(args)
return self:showNoteManager()
self:reloadVisibleNotes()
end

function NotesOverlay:onInput(keys)
Expand Down

0 comments on commit 83b4da0

Please sign in to comment.