1windows = {}
2
3function init()
4 g_ui.importStyle('textwindow')
5
6 connect(g_game, { onEditText = onGameEditText,
7 onEditList = onGameEditList,
8 onGameEnd = destroyWindows })
9end
10
11function terminate()
12 disconnect(g_game, { onEditText = onGameEditText,
13 onEditList = onGameEditList,
14 onGameEnd = destroyWindows })
15
16 destroyWindows()
17end
18
19function destroyWindows()
20 for _,window in pairs(windows) do
21 window:destroy()
22 end
23 windows = {}
24end
25
26function onGameEditText(id, itemId, maxLength, text, writer, time)
27 local textWindow = g_ui.createWidget('TextWindow', rootWidget)
28
29 local writeable = #text < maxLength and maxLength > 0
30 local textItem = textWindow:getChildById('textItem')
31 local description = textWindow:getChildById('description')
32 local textEdit = textWindow:getChildById('text')
33 local okButton = textWindow:getChildById('okButton')
34 local cancelButton = textWindow:getChildById('cancelButton')
35
36 local textScroll = textWindow:getChildById('textScroll')
37
38 if textItem:isHidden() then
39 textItem:show()
40 end
41
42 textItem:setItemId(itemId)
43 textEdit:setMaxLength(maxLength)
44 textEdit:setText(text)
45 textEdit:setEditable(writeable)
46 textEdit:setCursorVisible(writeable)
47
48 local desc = ''
49 if #writer > 0 then
50 desc = tr('You read the following, written by \n%s\n', writer)
51 if #time > 0 then
52 desc = desc .. tr('on %s.\n', time)
53 end
54 elseif #time > 0 then
55 desc = tr('You read the following, written on \n%s.\n', time)
56 end
57
58 if #text == 0 and not writeable then
59 desc = tr("It is empty.")
60 elseif writeable then
61 desc = desc .. tr('You can enter new text.')
62 end
63
64 local lines = #{string.find(desc, '\n')}
65 if lines < 2 then desc = desc .. '\n' end
66
67 description:setText(desc)
68
69 if not writeable then
70 textWindow:setText(tr('Show Text'))
71 cancelButton:hide()
72 cancelButton:setWidth(0)
73 okButton:setMarginRight(0)
74 else
75 textWindow:setText(tr('Edit Text'))
76 end
77
78 if description:getHeight() < 64 then
79 description:setHeight(64)
80 end
81
82 local function destroy()
83 textWindow:destroy()
84 table.removevalue(windows, textWindow)
85 end
86
87 local doneFunc = function()
88 if writeable then
89 g_game.editText(id, textEdit:getText())
90 end
91 destroy()
92 end
93
94 okButton.onClick = doneFunc
95 cancelButton.onClick = destroy
96
97 if not writeable then
98 textWindow.onEnter = doneFunc
99 end
100
101 textWindow.onEscape = destroy
102
103 table.insert(windows, textWindow)
104end
105
106function onGameEditList(id, doorId, text)
107 local textWindow = g_ui.createWidget('TextWindow', rootWidget)
108
109 local textEdit = textWindow:getChildById('text')
110 local description = textWindow:getChildById('description')
111 local okButton = textWindow:getChildById('okButton')
112 local cancelButton = textWindow:getChildById('cancelButton')
113
114 local textItem = textWindow:getChildById('textItem')
115 if textItem and not textItem:isHidden() then
116 textItem:hide()
117 end
118
119 textEdit:setMaxLength(8192)
120 textEdit:setText(text)
121 textEdit:setEditable(true)
122 description:setText(tr('Enter one name per line.'))
123 textWindow:setText(tr('Edit List'))
124
125 if description:getHeight() < 64 then
126 description:setHeight(64)
127 end
128
129 local function destroy()
130 textWindow:destroy()
131 table.removevalue(windows, textWindow)
132 end
133
134 local doneFunc = function()
135 g_game.editList(id, doorId, textEdit:getText())
136 destroy()
137 end
138
139 okButton.onClick = doneFunc
140 cancelButton.onClick = destroy
141 textWindow.onEscape = destroy
142
143 table.insert(windows, textWindow)
144end
145