forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
names.lua
106 lines (97 loc) · 3.27 KB
/
names.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
--rename items or units with the native interface
--[====[
names
=====
Rename units or items (including artifacts) with the native interface.
If a first name is desired, press ``f``. To clear the current first name,
leave this blank.
]====]
local gui = require 'gui'
local dlg = require 'gui.dialogs'
local widgets = require 'gui.widgets'
local utils = require 'utils'
local validArgs = utils.invert({
'help',
})
local args = utils.processArgs({...}, validArgs)
if args.help then
print(
[[names.lua
arguments:
-help
print this help message
if a first name is desired press f, leave blank to clear current first name
if viewing an artifact you can rename it
if viewing a unit you can rename them
]])
return
end
setup_screen = setup_screen or df.viewscreen_setupdwarfgamest:new()
setup_screen.show_play_now = 0
namescr = defclass(namescr, gui.Screen)
namescr.focus_path = 'names'
function namescr:init()
local parent = dfhack.gui.getCurViewscreen()
local nameType
local trg = dfhack.gui.getAnyUnit(parent)
if trg then
nameType = df.language_name_type.Figure
elseif df.viewscreen_itemst:is_instance(parent) then
local fact = dfhack.items.getGeneralRef(parent.item, df.general_ref_type.IS_ARTIFACT) --hint:df.viewscreen_itemst
if fact then
local artifact = df.artifact_record.find(fact.artifact_id) --hint:df.general_ref_is_artifactst
trg = artifact --luacheck: retype
nameType = df.language_name_type.Artifact
end
elseif df.viewscreen_dungeon_monsterstatusst:is_instance(parent) then
local uid = parent.unit.id --hint:df.viewscreen_dungeon_monsterstatusst
trg = df.unit.find(uid) --luacheck: retype
nameType = df.language_name_type.Figure
elseif df.global.ui_advmode.menu == df.ui_advmode_menu.Look then
local t_look = df.global.ui_look_list.items[df.global.ui_look_cursor]
if t_look.type == df.ui_look_list.T_items.T_type.Unit then
trg = t_look.data.Unit --luacheck: retype
nameType = df.language_name_type.Figure
end
else
qerror('Could not find valid target')
end
if trg.name.language == -1 then
qerror("Target's name does not have a language")
end
self.trg = trg
setup_screen.fort_name:assign(trg.name)
gui.simulateInput(setup_screen, 'SETUP_NAME_FORT')
dfhack.gui.getCurViewscreen().type = nameType
end
function namescr:setName()
local name = setup_screen.fort_name
self.trg.name:assign(name)
if self.trg._type == df.unit then
local unit = self.trg
if unit.status and unit.status.current_soul then
unit.status.current_soul.name:assign(name)
end
local hf = df.historical_figure.find(unit.hist_figure_id)
if hf then
hf.name:assign(name)
end
end
end
function namescr:onRenderBody(dc)
self._native.parent:render()
end
function namescr:onInput(keys)
if keys.LEAVESCREEN then
self:setName()
self:dismiss()
dfhack.screen.dismiss(self._native.parent)
return
end
return self:sendInputToParent(keys)
end
if dfhack.gui.getViewscreenByType(df.viewscreen_layer_choose_language_namest, 0) then
qerror('names screen already shown')
else
namescr():show()
end