forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.lua
127 lines (106 loc) · 3.57 KB
/
rename.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
-- Rename various objects via gui.
--[====[
gui/rename
==========
Backed by `rename`, this script allows entering the desired name
via a simple dialog in the game ui.
* ``gui/rename [building]`` in :kbd:`q` mode changes the name of a building.
.. image:: /docs/images/rename-bld.png
The selected building must be one of stockpile, workshop, furnace, trap, or siege engine.
It is also possible to rename zones from the :kbd:`i` menu.
* ``gui/rename [unit]`` with a unit selected changes the nickname.
Unlike the built-in interface, this works even on enemies and animals.
* ``gui/rename unit-profession`` changes the selected unit's custom profession name.
.. image:: /docs/images/rename-prof.png
Likewise, this can be applied to any unit, and when used on animals it overrides
their species string.
The ``building`` or ``unit`` options are automatically assumed when in relevant UI state.
]====]
local gui = require 'gui'
local dlg = require 'gui.dialogs'
local widgets = require 'gui.widgets'
local plugin = require 'plugins.rename'
local mode = ...
local focus = dfhack.gui.getCurFocus()
RenameDialog = defclass(RenameDialog, dlg.InputBox)
function RenameDialog:init(info)
self:addviews{
widgets.Label{
view_id = 'controls',
text = {
{key = 'CUSTOM_ALT_C', text = ': Clear, ',
on_activate = function()
self.subviews.edit.text = ''
end},
{key = 'CUSTOM_ALT_S', text = ': Special chars',
on_activate = curry(dfhack.run_script, 'gui/cp437-table')},
},
frame = {b = 0, l = 0, r = 0, w = 70},
}
}
-- calculate text_width once
self.subviews.controls:getTextWidth()
end
function RenameDialog:getWantedFrameSize()
local x, y = self.super.getWantedFrameSize(self)
x = math.max(x, self.subviews.controls.text_width)
return x, y + 2
end
function showRenameDialog(title, text, input, on_input)
RenameDialog{
frame_title = title,
text = text,
text_pen = COLOR_GREEN,
input = input,
on_input = on_input,
}:show()
end
local function verify_mode(expected)
if mode ~= nil and mode ~= expected then
qerror('Invalid UI state for mode '..mode)
end
end
local unit = dfhack.gui.getSelectedUnit(true)
local building = dfhack.gui.getSelectedBuilding(true)
if building and (not unit or mode == 'building') then
verify_mode('building')
if plugin.canRenameBuilding(building) then
showRenameDialog(
'Rename Building',
'Enter a new name for the building:',
building.name,
curry(plugin.renameBuilding, building)
)
else
dlg.showMessage(
'Rename Building',
'Cannot rename this type of building.', COLOR_LIGHTRED
)
end
elseif unit then
if mode == 'unit-profession' then
showRenameDialog(
'Rename Unit',
'Enter a new profession for the unit:',
unit.custom_profession,
function(newval)
unit.custom_profession = newval
end
)
else
verify_mode('unit')
local vname = dfhack.units.getVisibleName(unit)
local vnick = ''
if vname and vname.has_name then
vnick = vname.nickname
end
showRenameDialog(
'Rename Unit',
'Enter a new nickname for the unit:',
vnick,
curry(dfhack.units.setNickname, unit)
)
end
elseif mode then
verify_mode(nil)
end