2012-07-24 07:30:08 +02:00
|
|
|
function init()
|
|
|
|
g_ui.importStyle('textwindow.otui')
|
|
|
|
|
|
|
|
connect(g_game, { onEditText = onGameEditText,
|
|
|
|
onEditList = onGameEditList,
|
|
|
|
onGameEnd = destroy })
|
|
|
|
end
|
|
|
|
|
|
|
|
function terminate()
|
|
|
|
disconnect(g_game, { onEditText = onGameEditText,
|
|
|
|
onEditList = onGameEditList,
|
|
|
|
onGameEnd = destroy })
|
2012-05-01 02:20:27 +02:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
destroy()
|
|
|
|
end
|
2012-07-14 12:59:32 +02:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function destroy()
|
2012-07-15 13:49:28 +02:00
|
|
|
if textWindow then
|
2012-07-24 07:30:08 +02:00
|
|
|
textWindow:destroy()
|
|
|
|
textWindow = nil
|
2012-07-14 12:59:32 +02:00
|
|
|
end
|
2012-07-24 07:30:08 +02:00
|
|
|
end
|
|
|
|
|
2012-09-30 05:16:57 +02:00
|
|
|
function getCursorPosByNewLine(str, count)
|
2012-10-03 17:51:07 +02:00
|
|
|
if count <= 1 then return 0 end
|
|
|
|
|
2012-09-30 05:16:57 +02:00
|
|
|
local i = 0
|
|
|
|
for n = 1, count-1 do
|
|
|
|
local tPos = string.find(str, '\n', i)
|
|
|
|
if tPos then
|
|
|
|
i = tPos+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return i - 1
|
|
|
|
end
|
|
|
|
|
2012-10-03 17:51:07 +02:00
|
|
|
function getLineByCursorPos(str, pos, maxLine)
|
|
|
|
for i = 1, maxLine do
|
|
|
|
if pos <= getCursorPosByNewLine(str, i) then
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return maxLine + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function getLineSizeByCursorPos(str, pos, maxLine)
|
|
|
|
for i = 1, maxLine + 1 do
|
|
|
|
if pos < getCursorPosByNewLine(str, i) then
|
|
|
|
return {minPos = getCursorPosByNewLine(str, i-1), maxPos = (getCursorPosByNewLine(str, i) - 1)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return {minPos = getCursorPosByNewLine(str, maxLine + 1), maxPos = str:len()}
|
|
|
|
end
|
|
|
|
|
|
|
|
function string.count(str, pattern)
|
|
|
|
local _, _count = string.gsub(str, pattern, pattern)
|
|
|
|
return _count
|
|
|
|
end
|
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onGameEditText(id, itemId, maxLength, text, writter, time)
|
|
|
|
if textWindow then return end
|
2012-07-14 12:59:32 +02:00
|
|
|
textWindow = g_ui.createWidget('TextWindow', rootWidget)
|
2012-05-01 02:20:27 +02:00
|
|
|
|
2012-05-01 02:53:02 +02:00
|
|
|
local writeable = (maxLength ~= #text) and maxLength > 0
|
2012-05-01 02:20:27 +02:00
|
|
|
local textItem = textWindow:getChildById('textItem')
|
|
|
|
local description = textWindow:getChildById('description')
|
|
|
|
local textEdit = textWindow:getChildById('text')
|
|
|
|
local okButton = textWindow:getChildById('okButton')
|
|
|
|
local cancelButton = textWindow:getChildById('cancelButton')
|
|
|
|
|
2012-09-30 05:16:57 +02:00
|
|
|
local textScroll = textWindow:getChildById('textScroll')
|
|
|
|
|
2012-05-01 02:20:27 +02:00
|
|
|
textItem:setItemId(itemId)
|
|
|
|
textEdit:setMaxLength(maxLength)
|
|
|
|
textEdit:setText(text)
|
|
|
|
textEdit:setEnabled(writeable)
|
2012-09-30 05:16:57 +02:00
|
|
|
|
2012-05-01 15:06:38 +02:00
|
|
|
local desc = ''
|
2012-05-01 02:20:27 +02:00
|
|
|
if #writter > 0 then
|
2012-05-01 15:06:38 +02:00
|
|
|
desc = tr('You read the following, written by \n%s\n', writter)
|
2012-05-01 02:20:27 +02:00
|
|
|
if #time > 0 then
|
|
|
|
desc = desc .. tr('on %s.\n', time)
|
|
|
|
end
|
|
|
|
elseif #time > 0 then
|
2012-09-06 02:37:52 +02:00
|
|
|
desc = tr('You read the following, written on \n%s.\n', time)
|
2012-05-01 02:20:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if #text == 0 and not writeable then
|
2012-09-06 02:37:52 +02:00
|
|
|
desc = tr("It is empty.")
|
2012-05-01 02:20:27 +02:00
|
|
|
elseif writeable then
|
|
|
|
desc = desc .. tr('You can enter new text.')
|
|
|
|
end
|
|
|
|
|
2012-09-06 02:37:52 +02:00
|
|
|
local lines = #{string.find(desc, '\n')}
|
|
|
|
if lines < 2 then desc = desc .. '\n' end
|
|
|
|
|
2012-05-01 02:20:27 +02:00
|
|
|
description:setText(desc)
|
2012-05-01 02:53:02 +02:00
|
|
|
|
|
|
|
if not writeable then
|
|
|
|
textWindow:setText(tr('Show Text'))
|
2012-09-06 02:37:52 +02:00
|
|
|
--textEdit:wrapText()
|
2012-05-01 02:53:02 +02:00
|
|
|
cancelButton:hide()
|
2012-09-06 02:37:52 +02:00
|
|
|
cancelButton:setWidth(0)
|
|
|
|
okButton:setMarginRight(0)
|
2012-05-01 02:53:02 +02:00
|
|
|
else
|
|
|
|
textWindow:setText(tr('Edit Text'))
|
|
|
|
end
|
|
|
|
|
2012-09-06 02:37:52 +02:00
|
|
|
local doneFunc = function()
|
2012-05-01 02:53:02 +02:00
|
|
|
if writeable then
|
|
|
|
g_game.editText(id, textEdit:getText())
|
|
|
|
end
|
2012-09-06 02:37:52 +02:00
|
|
|
destroy()
|
|
|
|
end
|
2012-09-30 05:16:57 +02:00
|
|
|
|
2012-10-03 17:51:07 +02:00
|
|
|
local newLineCount = string.count(textEdit:getText(), '\n')
|
2012-09-30 05:16:57 +02:00
|
|
|
if(newLineCount >= 9) then
|
|
|
|
textScroll:setMaximum(newLineCount-9)
|
|
|
|
end
|
|
|
|
|
2012-10-03 17:51:07 +02:00
|
|
|
local _prev, _next = 0, 11
|
|
|
|
local scrollOnValueChange = function(widget, value, delta)
|
|
|
|
local line = getLineByCursorPos(textEdit:getText(), textEdit:getCursorPos(), newLineCount)
|
|
|
|
if(delta > 0) then
|
|
|
|
textEdit:setCursorPos(getCursorPosByNewLine(textEdit:getText(), _next + delta - 1))
|
|
|
|
if writeable then textEdit:setCursorPos(getCursorPosByNewLine(textEdit:getText(), line + delta)) end
|
|
|
|
else
|
2012-10-03 18:03:56 +02:00
|
|
|
textEdit:setCursorPos(getCursorPosByNewLine(textEdit:getText(), _prev + delta + 1) - 1)
|
2012-10-03 17:51:07 +02:00
|
|
|
if writeable then textEdit:setCursorPos(getCursorPosByNewLine(textEdit:getText(), line + delta)) end
|
|
|
|
end
|
2012-09-30 05:16:57 +02:00
|
|
|
|
2012-10-03 17:51:07 +02:00
|
|
|
_next = _next + delta
|
|
|
|
_prev = _prev + delta
|
2012-09-30 05:16:57 +02:00
|
|
|
end
|
|
|
|
|
2012-10-03 17:51:07 +02:00
|
|
|
textScroll.onValueChange = scrollOnValueChange
|
|
|
|
|
|
|
|
local navigateVertical = function(up) -- Pressing Up / Down when scrollbar is at min / max value
|
|
|
|
local line = getLineByCursorPos(textEdit:getText(), textEdit:getCursorPos(), newLineCount)
|
|
|
|
if up and textScroll:getValue() == textScroll:getMinimum() then
|
|
|
|
textEdit:setCursorPos(getCursorPosByNewLine(textEdit:getText(), line - 1))
|
|
|
|
elseif not up and textScroll:getValue() == textScroll:getMaximum() then
|
|
|
|
textEdit:setCursorPos(getCursorPosByNewLine(textEdit:getText(), line + 1))
|
|
|
|
end
|
2012-09-30 05:16:57 +02:00
|
|
|
end
|
|
|
|
|
2012-10-03 17:51:07 +02:00
|
|
|
local navigateHorizontal = function(right) -- Pressing Left / Right to navigate in a line
|
|
|
|
local currentCursor = textEdit:getCursorPos()
|
|
|
|
local lineSize = getLineSizeByCursorPos(textEdit:getText(), currentCursor, newLineCount)
|
|
|
|
if right and currentCursor < lineSize.maxPos then
|
|
|
|
textEdit:setCursorPos(currentCursor+1)
|
|
|
|
elseif not right and currentCursor > lineSize.minPos then
|
|
|
|
textEdit:setCursorPos(currentCursor-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local onKeyPress = function(widget, keyCode, keyModifiers)
|
|
|
|
if keyModifiers ~= 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if keyCode == 16 or keyCode == 17 then -- Left / Right
|
|
|
|
navigateHorizontal((keyCode == 17))
|
|
|
|
return true
|
|
|
|
elseif keyCode == 14 or keyCode == 15 then -- Up / Down
|
|
|
|
local up = (keyCode == 14)
|
|
|
|
navigateVertical(up)
|
|
|
|
|
|
|
|
if up then
|
|
|
|
textScroll:setValue(textScroll:getValue() - 1)
|
|
|
|
else
|
|
|
|
textScroll:setValue(textScroll:getValue() + 1)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
2012-09-30 05:16:57 +02:00
|
|
|
|
|
|
|
if(not writeable) then
|
|
|
|
textEdit:setCursorPos(0)
|
2012-10-03 17:51:07 +02:00
|
|
|
textWindow.onKeyPress = onKeyPress -- textEdit won't receive focus
|
2012-09-30 05:16:57 +02:00
|
|
|
else
|
|
|
|
textScroll:setValue(textScroll:getMaximum())
|
|
|
|
textEdit:setCursorPos(text:len())
|
2012-10-03 17:51:07 +02:00
|
|
|
textEdit.onKeyPress = onKeyPress
|
2012-09-30 05:16:57 +02:00
|
|
|
end
|
|
|
|
|
2012-09-06 02:37:52 +02:00
|
|
|
okButton.onClick = doneFunc
|
|
|
|
cancelButton.onClick = destroy
|
|
|
|
--textWindow.onEnter = doneFunc -- this should be '\n'
|
|
|
|
textWindow.onEscape = destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
function onGameEditList(id, doorId, text)
|
|
|
|
if textWindow then return end
|
|
|
|
textWindow = g_ui.createWidget('TextWindow', rootWidget)
|
|
|
|
|
|
|
|
local textEdit = textWindow:getChildById('text')
|
|
|
|
local description = textWindow:getChildById('description')
|
|
|
|
local okButton = textWindow:getChildById('okButton')
|
|
|
|
local cancelButton = textWindow:getChildById('cancelButton')
|
|
|
|
|
|
|
|
textEdit:setMaxLength(8192)
|
|
|
|
textEdit:setText(text)
|
|
|
|
textEdit:setEnabled(true)
|
|
|
|
description:setText(tr('Enter one name per line.'))
|
|
|
|
textWindow:setText(tr('Edit List'))
|
|
|
|
|
|
|
|
doneFunc = function()
|
|
|
|
g_game.editList(id, doorId, textEdit:getText())
|
2012-07-24 07:30:08 +02:00
|
|
|
destroy()
|
2012-05-01 02:20:27 +02:00
|
|
|
end
|
2012-07-24 07:30:08 +02:00
|
|
|
|
2012-07-14 12:59:32 +02:00
|
|
|
okButton.onClick = doneFunc
|
|
|
|
textWindow.onEnter = doneFunc
|
2012-07-24 07:30:08 +02:00
|
|
|
textWindow.onEscape = destroy
|
2012-05-01 02:20:27 +02:00
|
|
|
end
|
|
|
|
|
2012-09-06 02:37:52 +02:00
|
|
|
|
2012-07-24 07:30:08 +02:00
|
|
|
function onGameEditList(id, doorId, text)
|
|
|
|
if textWindow then return end
|
2012-07-14 12:59:32 +02:00
|
|
|
textWindow = g_ui.createWidget('TextWindow', rootWidget)
|
2012-05-01 02:53:02 +02:00
|
|
|
|
|
|
|
local textEdit = textWindow:getChildById('text')
|
|
|
|
local description = textWindow:getChildById('description')
|
|
|
|
local okButton = textWindow:getChildById('okButton')
|
|
|
|
local cancelButton = textWindow:getChildById('cancelButton')
|
|
|
|
|
|
|
|
textEdit:setMaxLength(8192)
|
|
|
|
textEdit:setText(text)
|
|
|
|
textEdit:setEnabled(true)
|
2012-05-01 04:00:07 +02:00
|
|
|
description:setText(tr('Enter one name per line.'))
|
2012-05-01 02:53:02 +02:00
|
|
|
textWindow:setText(tr('Edit List'))
|
|
|
|
|
2012-07-14 12:59:32 +02:00
|
|
|
doneFunc = function()
|
2012-05-01 02:53:02 +02:00
|
|
|
g_game.editList(id, doorId, textEdit:getText())
|
2012-07-24 07:30:08 +02:00
|
|
|
destroy()
|
2012-05-01 02:53:02 +02:00
|
|
|
end
|
2012-07-24 07:30:08 +02:00
|
|
|
|
2012-07-14 12:59:32 +02:00
|
|
|
okButton.onClick = doneFunc
|
|
|
|
textWindow.onEnter = doneFunc
|
2012-07-24 07:30:08 +02:00
|
|
|
textWindow.onEscape = destroy
|
2012-10-03 17:51:07 +02:00
|
|
|
end
|