forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 4
/
names.lua
92 lines (83 loc) · 2.82 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
--rename items or units with the native interface
--[====[
names
=====
Rename units or items. Usage:
:-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
]====]
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
adv_screen = adv_screen or df.viewscreen_setupadventurest:new()
adv_screen.page = df.viewscreen_setupadventurest.T_page.Background
namescr = defclass(namescr, gui.Screen)
namescr.focus_path = 'names'
function namescr:init()
local parent = dfhack.gui.getCurViewscreen()
local trg = dfhack.gui.getAnyUnit(parent)
if trg then
-- ok
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
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
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
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
adv_screen.adventurer.name:assign(trg.name)
gui.simulateInput(adv_screen, 'A_CUST_NAME')
end
function namescr:setName()
self.trg.name:assign(self._native.parent.name) --hint:df.viewscreen_layer_choose_language_namest
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