tibia-client/modules/client_entergame/characterlist.lua

186 lines
5.1 KiB
Lua
Raw Normal View History

CharacterList = { }
-- private variables
local charactersWindow
local loadBox
local characterList
local errorBox
-- private functions
local function onCharactersWindowKeyPress(self, keyCode, keyboardModifiers)
if keyboardModifiers == KeyboardNoModifier then
if keyCode == KeyUp then
characterList:focusPreviousChild(KeyboardFocusReason)
2012-01-11 00:13:38 +01:00
return true
elseif keyCode == KeyDown or keyCode == KeyTab then
characterList:focusNextChild(KeyboardFocusReason)
2012-01-11 00:13:38 +01:00
return true
end
end
return false
end
2011-08-30 01:20:30 +02:00
local function tryLogin(charInfo, tries)
tries = tries or 1
if tries > 4 then
CharacterList.destroyLoadBox()
displayErrorBox(tr('Error'), tr('Unable to logout.'))
2011-08-30 01:20:30 +02:00
return
end
2011-08-29 20:38:01 +02:00
2012-02-08 22:23:15 +01:00
if g_game.isOnline() then
g_game.safeLogout()
2011-08-30 01:20:30 +02:00
if tries == 1 then
loadBox = displayCancelBox(tr('Please wait'), tr('Loggin out...'))
2011-08-30 01:20:30 +02:00
end
scheduleEvent(function() tryLogin(charInfo, tries+1) end, 250)
2011-08-29 20:38:01 +02:00
return
end
2011-08-30 01:20:30 +02:00
CharacterList.destroyLoadBox()
g_game.loginWorld(EnterGame.account, EnterGame.password, charInfo.worldName, charInfo.worldHost, charInfo.worldPort, charInfo.characterName)
2011-08-29 20:38:01 +02:00
loadBox = displayCancelBox(tr('Please wait'), tr('Connecting to game server...'))
2012-02-06 20:19:47 +01:00
connect(loadBox, { onCancel = function()
loadBox = nil
2012-02-08 22:23:15 +01:00
g_game.cancelLogin()
2012-02-06 20:19:47 +01:00
CharacterList.show()
end })
2011-08-29 20:38:01 +02:00
-- save last used character
2012-01-06 10:35:48 +01:00
Settings.set('lastUsedCharacter', charInfo.characterName)
2011-08-29 20:38:01 +02:00
end
function onGameLoginError(message)
CharacterList.destroyLoadBox()
errorBox = displayErrorBox(tr("Login Error"), message)
errorBox.onOk = function()
errorBox = nil
CharacterList.showAgain()
end
end
function onGameConnectionError(message)
CharacterList.destroyLoadBox()
errorBox = displayErrorBox(tr("Login Error"), message)
errorBox.onOk = function()
errorBox = nil
CharacterList.showAgain()
end
end
-- public functions
function CharacterList.init()
charactersWindow = displayUI('characterlist.otui')
charactersWindow:hide()
characterList = charactersWindow:getChildById('characterList')
charactersWindow.onKeyPress = onCharactersWindowKeyPress
connect(g_game, { onLoginError = onGameLoginError })
connect(g_game, { onConnectionError = onGameConnectionError })
connect(g_game, { onGameStart = CharacterList.destroyLoadBox })
2012-03-18 21:59:00 +01:00
connect(g_game, { onGameEnd = CharacterList.showAgain })
end
function CharacterList.terminate()
disconnect(g_game, { onLoginError = onGameLoginError })
disconnect(g_game, { onConnectionError = onGameConnectionError })
disconnect(g_game, { onGameStart = CharacterList.destroyLoadBox })
2012-03-18 21:59:00 +01:00
disconnect(g_game, { onGameEnd = CharacterList.showAgain })
characterList = nil
charactersWindow:destroy()
charactersWindow = nil
if loadBox then
g_game.cancelLogin()
loadBox:destroy()
loadBox = nil
end
2012-02-07 01:41:53 +01:00
CharacterList = nil
end
function CharacterList.create(characters, premDays)
CharacterList.show()
characterList:destroyChildren()
local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel')
2011-12-09 16:01:04 +01:00
local focusLabel
for i,characterInfo in ipairs(characters) do
local characterName = characterInfo[1]
local worldName = characterInfo[2]
local worldHost = characterInfo[3]
local worldIp = characterInfo[4]
local label = createWidget('CharacterListLabel', characterList)
label:setText(characterName .. ' (' .. worldName .. ')')
label:setPhantom(false)
label.characterName = characterName
label.worldHost = worldHost
label.worldPort = worldIp
connect(label, { onDoubleClick = function () CharacterList.doLogin() return true end } )
2012-01-06 10:35:48 +01:00
if i == 1 or Settings.get('lastUsedCharacter') == characterName then
2011-12-09 16:01:04 +01:00
focusLabel = label
end
end
2011-12-09 16:01:04 +01:00
characterList:focusChild(focusLabel, ActiveFocusReason)
if premDays > 0 then
accountStatusLabel:setText(tr("Account Status:\nPremium Account (%s) days left", premDays))
end
end
function CharacterList.hide()
charactersWindow:hide()
end
2011-11-02 04:02:56 +01:00
function CharacterList.destroy()
2011-11-02 02:55:36 +01:00
CharacterList.hide()
2012-02-08 22:23:15 +01:00
if not g_game.isOnline() then
EnterGame.show()
end
2011-11-02 02:55:36 +01:00
end
function CharacterList.show()
if not loadBox and not errorBox then
2011-08-30 01:20:30 +02:00
charactersWindow:show()
charactersWindow:raise()
charactersWindow:focus()
2011-08-30 01:20:30 +02:00
end
end
2012-03-18 21:59:00 +01:00
function CharacterList.showAgain()
if characterList:hasChildren() then
CharacterList.show()
end
end
2012-01-07 01:08:08 +01:00
function CharacterList.isVisible()
if charactersWindow and charactersWindow:isVisible() then
return true
end
return false
end
function CharacterList.doLogin()
local selected = charactersWindow:getChildById('characterList'):getFocusedChild()
if selected then
2011-08-29 20:38:01 +02:00
local charInfo = { worldHost = selected.worldHost,
worldPort = selected.worldPort,
characterName = selected.characterName }
2011-08-30 01:20:30 +02:00
CharacterList.hide()
2011-08-29 20:38:01 +02:00
tryLogin(charInfo)
else
displayErrorBox(tr('Error'), tr('You must select a character to login!'))
end
end
function CharacterList.destroyLoadBox()
if loadBox then
loadBox:destroy()
loadBox = nil
end
end