Skip to content

Commit ba1ae7e

Browse files
authored
Merge pull request #1409 from wiktor-obrebski/feat/adventurer-journal
Feat/adventurer journal
2 parents dc2bb03 + 1552b52 commit ba1ae7e

8 files changed

Lines changed: 285 additions & 77 deletions

File tree

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Template for new versions:
3434

3535
## New Features
3636
- `advtools`: new ``advtools.fastcombat`` overlay (enabled by default) allows you to skip combat animations and the announcement "More" button by mashing the movement keys
37+
- `gui/journal`: now working in adventure mode
3738

3839
## Fixes
3940
- `position`: support for adv mode look cursor

docs/gui/journal.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ gui/journal
33

44
.. dfhack-tool::
55
:summary: Fort journal with a multi-line text editor.
6-
:tags: fort interface
6+
:tags: adventure fort interface
77

88
The `gui/journal` interface makes it easy to take notes and document
9-
important details for the fortresses.
9+
important details for your fortresses and adventurers.
1010

1111
With this multi-line text editor,
12-
you can keep track of your fortress's background story, goals, notable events,
13-
and both short-term and long-term plans.
12+
you can keep track of your fortress's/adventurer's background story, goals,
13+
notable events, and both short-term and long-term plans.
1414

1515
This is particularly useful when you need to take a longer break from the game.
1616
Having detailed notes makes it much easier to resume your game after

gui/journal.lua

Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,15 @@ local utils = require 'utils'
77
local json = require 'json'
88
local shifter = reqscript('internal/journal/shifter')
99
local table_of_contents = reqscript('internal/journal/table_of_contents')
10+
local journal_context = reqscript('internal/journal/journal_context')
1011

1112
local RESIZE_MIN = {w=54, h=20}
1213
local TOC_RESIZE_MIN = {w=24}
1314

14-
local JOURNAL_PERSIST_KEY = 'journal'
15-
16-
local JOURNAL_WELCOME_COPY = [=[
17-
Welcome to gui/journal, the chronicler's tool for Dwarf Fortress!
18-
19-
Here, you can carve out notes, sketch your grand designs, or record the history of your fortress.
20-
The text you write here is saved together with your fort.
21-
22-
For guidance on navigation and hotkeys, tap the ? button in the upper right corner.
23-
Happy digging!
24-
]=]
25-
26-
local TOC_WELCOME_COPY = [=[
27-
Start a line with # symbols and a space to create a header. For example:
28-
29-
# My section heading
30-
31-
or
32-
33-
## My section subheading
34-
35-
Those headers will appear here, and you can click on them to jump to them in the text.]=]
36-
3715
journal_config = journal_config or json.open('dfhack-config/journal.json')
3816

17+
JOURNAL_CONTEXT_MODE = journal_context.JOURNAL_CONTEXT_MODE
18+
3919
JournalWindow = defclass(JournalWindow, widgets.Window)
4020
JournalWindow.ATTRS {
4121
frame_title='DF Journal',
@@ -47,6 +27,9 @@ JournalWindow.ATTRS {
4727
save_layout=true,
4828
show_tutorial=false,
4929

30+
toc_welcome_copy=DEFAULT_NIL,
31+
journal_welcome_copy=DEFAULT_NIL,
32+
5033
on_text_change=DEFAULT_NIL,
5134
on_cursor_change=DEFAULT_NIL,
5235
on_layout_change=DEFAULT_NIL
@@ -76,7 +59,7 @@ function JournalWindow:init()
7659
widgets.WrappedLabel{
7760
view_id='table_of_contents_tutorial',
7861
frame={l=0,t=0,r=0,b=3},
79-
text_to_wrap=TOC_WELCOME_COPY,
62+
text_to_wrap=self.toc_welcome_copy or '',
8063
visible=false
8164
}
8265
}
@@ -143,7 +126,7 @@ function JournalWindow:init()
143126
widgets.WrappedLabel{
144127
view_id='journal_tutorial',
145128
frame={l=0,t=1,r=0,b=0},
146-
text_to_wrap=JOURNAL_WELCOME_COPY
129+
text_to_wrap=self.journal_welcome_copy or ''
147130
}
148131
}
149132
end
@@ -285,13 +268,17 @@ end
285268
JournalScreen = defclass(JournalScreen, gui.ZScreen)
286269
JournalScreen.ATTRS {
287270
focus_path='journal',
288-
save_on_change=true,
271+
context_mode=DEFAULT_NIL,
289272
save_layout=true,
290273
save_prefix=''
291274
}
292275

293276
function JournalScreen:init()
294-
local context = self:loadContext()
277+
self.journal_context = journal_context.journal_context_factory(
278+
self.context_mode,
279+
self.save_prefix
280+
)
281+
local content = self.journal_context:load_content()
295282

296283
self:addviews{
297284
JournalWindow{
@@ -300,62 +287,45 @@ function JournalScreen:init()
300287

301288
save_layout=self.save_layout,
302289

303-
init_text=context.text[1],
304-
init_cursor=context.cursor[1],
305-
show_tutorial=context.show_tutorial or false,
290+
init_text=content.text[1],
291+
init_cursor=content.cursor[1],
292+
show_tutorial=content.show_tutorial or false,
293+
294+
toc_welcome_copy=self.journal_context:tocWelcomeCopy(),
295+
journal_welcome_copy=self.journal_context:welcomeCopy(),
306296

307-
on_text_change=self:callback('saveContext'),
308-
on_cursor_change=self:callback('saveContext')
297+
on_text_change=self:callback('onTextChange'),
298+
on_cursor_change=self:callback('onTextChange')
309299
},
310300
}
311301
end
312302

313-
function JournalScreen:loadContext()
314-
local site_data = self.save_on_change and dfhack.persistent.getSiteData(
315-
self.save_prefix .. JOURNAL_PERSIST_KEY
316-
) or {}
303+
function JournalScreen:onTextChange()
304+
local text = self.subviews.journal_editor:getText()
305+
local cursor = self.subviews.journal_editor:getCursor()
317306

318-
if not site_data.text then
319-
site_data.text={''}
320-
site_data.show_tutorial = true
321-
end
322-
site_data.cursor = site_data.cursor or {#site_data.text[1] + 1}
323-
324-
return site_data
325-
end
326-
327-
function JournalScreen:onTextChange(text)
328-
self:saveContext(text)
329-
end
330-
331-
function JournalScreen:saveContext()
332-
if self.save_on_change and dfhack.isWorldLoaded() then
333-
local text = self.subviews.journal_editor:getText()
334-
local cursor = self.subviews.journal_editor:getCursor()
335-
336-
dfhack.persistent.saveSiteData(
337-
self.save_prefix .. JOURNAL_PERSIST_KEY,
338-
{text={text}, cursor={cursor}}
339-
)
340-
end
307+
self.journal_context:save_content(text, cursor)
341308
end
342309

343310
function JournalScreen:onDismiss()
344311
view = nil
345312
end
346313

347314
function main(options)
348-
if not dfhack.isMapLoaded() or not dfhack.world.isFortressMode() then
349-
qerror('journal requires a fortress map to be loaded')
315+
if not dfhack.isMapLoaded() or (not dfhack.world.isFortressMode()
316+
and not dfhack.world.isAdventureMode()) then
317+
qerror('journal requires a fortress/adventure map to be loaded')
350318
end
351319

352320
local save_layout = options and options.save_layout
353-
local save_on_change = options and options.save_on_change
321+
local overrided_context_mode = options and options.context_mode
322+
local context_mode = overrided_context_mode == nil and
323+
journal_context.detect_journal_context_mode() or overrided_context_mode
354324

355325
view = view and view:raise() or JournalScreen{
356326
save_prefix=options and options.save_prefix or '',
357327
save_layout=save_layout == nil and true or save_layout,
358-
save_on_change=save_on_change == nil and true or save_on_change,
328+
context_mode=context_mode,
359329
}:show()
360330
end
361331

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--@ module = true
2+
3+
local JOURNAL_WELCOME_COPY = [=[
4+
Welcome to gui/journal, the adventurer's notebook for Dwarf Fortress!
5+
6+
Here, you can jot down your travels, keep track of important places, or note anything worth remembering.
7+
The text you write here is saved together with your adventurer.
8+
9+
For guidance on navigation and hotkeys, tap the ? button in the upper right corner.
10+
Safe travels!
11+
]=]
12+
13+
local TOC_WELCOME_COPY = [=[
14+
Start a line with # symbols and a space to create a header. For example:
15+
16+
# My section heading
17+
18+
or
19+
20+
## My section subheading
21+
22+
Those headers will appear here, and you can click on them to jump to them in the text.]=]
23+
24+
25+
AdventurerJournalContext = defclass(AdventurerJournalContext)
26+
AdventurerJournalContext.ATTRS{
27+
save_prefix='',
28+
adventurer_id=DEFAULT_NIL
29+
}
30+
31+
function get_adventurer_context_key(prefix, adventurer_id)
32+
return string.format(
33+
'%sjournal:adventurer:%s',
34+
prefix,
35+
adventurer_id
36+
)
37+
end
38+
39+
function AdventurerJournalContext:save_content(text, cursor)
40+
if dfhack.isWorldLoaded() then
41+
dfhack.persistent.saveWorldData(
42+
get_adventurer_context_key(self.save_prefix, self.adventurer_id),
43+
{text={text}, cursor={cursor}}
44+
)
45+
end
46+
end
47+
48+
function AdventurerJournalContext:load_content()
49+
if dfhack.isWorldLoaded() then
50+
local world_data = dfhack.persistent.getWorldData(
51+
get_adventurer_context_key(self.save_prefix, self.adventurer_id)
52+
) or {}
53+
54+
if not world_data.text then
55+
world_data.text={''}
56+
world_data.show_tutorial = true
57+
end
58+
world_data.cursor = world_data.cursor or {#world_data.text[1] + 1}
59+
return world_data
60+
end
61+
end
62+
63+
function AdventurerJournalContext:delete_content()
64+
if dfhack.isWorldLoaded() then
65+
dfhack.persistent.deleteWorldData(
66+
get_adventurer_context_key(self.save_prefix, self.adventurer_id)
67+
)
68+
end
69+
end
70+
71+
function AdventurerJournalContext:welcomeCopy()
72+
return JOURNAL_WELCOME_COPY
73+
end
74+
75+
function AdventurerJournalContext:tocWelcomeCopy()
76+
return TOC_WELCOME_COPY
77+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--@ module = true
2+
3+
-- Dummy Context, no storage --
4+
5+
DummyJournalContext = defclass(DummyJournalContext)
6+
7+
function DummyJournalContext:save_content(text, cursor)
8+
end
9+
10+
function DummyJournalContext:load_content()
11+
return {text={''}, cursor={1}, show_tutorial=true}
12+
end
13+
14+
function DummyJournalContext:delete_content()
15+
end
16+
17+
function DummyJournalContext:welcomeCopy()
18+
return ''
19+
end
20+
21+
function DummyJournalContext:tocWelcomeCopy()
22+
return ''
23+
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--@ module = true
2+
3+
local JOURNAL_WELCOME_COPY = [=[
4+
Welcome to gui/journal, the chronicler's tool for Dwarf Fortress!
5+
6+
Here, you can carve out notes, sketch your grand designs, or record the history of your fortress.
7+
The text you write here is saved together with your fort.
8+
9+
For guidance on navigation and hotkeys, tap the ? button in the upper right corner.
10+
Happy digging!
11+
]=]
12+
13+
local TOC_WELCOME_COPY = [=[
14+
Start a line with # symbols and a space to create a header. For example:
15+
16+
# My section heading
17+
18+
or
19+
20+
## My section subheading
21+
22+
Those headers will appear here, and you can click on them to jump to them in the text.]=]
23+
24+
25+
FortressJournalContext = defclass(FortressJournalContext)
26+
FortressJournalContext.ATTRS{
27+
save_prefix=''
28+
}
29+
30+
function get_fort_context_key(prefix)
31+
return prefix .. 'journal'
32+
end
33+
34+
function FortressJournalContext:save_content(text, cursor)
35+
if dfhack.isWorldLoaded() then
36+
dfhack.persistent.saveSiteData(
37+
get_fort_context_key(self.save_prefix),
38+
{text={text}, cursor={cursor}}
39+
)
40+
end
41+
end
42+
43+
function FortressJournalContext:load_content()
44+
if dfhack.isWorldLoaded() then
45+
local site_data = dfhack.persistent.getSiteData(
46+
get_fort_context_key(self.save_prefix)
47+
) or {}
48+
49+
if not site_data.text then
50+
site_data.text={''}
51+
site_data.show_tutorial = true
52+
end
53+
site_data.cursor = site_data.cursor or {#site_data.text[1] + 1}
54+
return site_data
55+
end
56+
end
57+
58+
function FortressJournalContext:delete_content()
59+
if dfhack.isWorldLoaded() then
60+
dfhack.persistent.deleteSiteData(
61+
get_fort_context_key(self.save_prefix)
62+
)
63+
end
64+
end
65+
66+
function FortressJournalContext:welcomeCopy()
67+
return JOURNAL_WELCOME_COPY
68+
end
69+
70+
function FortressJournalContext:tocWelcomeCopy()
71+
return TOC_WELCOME_COPY
72+
end

0 commit comments

Comments
 (0)