reorganize modules

This commit is contained in:
Eduardo Bart
2011-11-02 01:02:56 -02:00
parent 2304ff3529
commit 5ab0e6f2ac
59 changed files with 147 additions and 208 deletions

View File

@@ -0,0 +1,121 @@
CharacterList = { }
-- private variables
local charactersWindow
local loadBox
local characterList
-- private functions
local function onCharactersWindowKeyPress(self, keyCode, keyChar, keyboardModifiers)
if keyboardModifiers == KeyboardNoModifier then
if keyCode == KeyUp or keyCode == KeyTab then
characterList:focusPreviousChild(ActiveFocusReason)
elseif keyCode == KeyDown then
characterList:focusNextChild(ActiveFocusReason)
end
end
return false
end
local function tryLogin(charInfo, tries)
tries = tries or 1
if tries > 4 then
CharacterList.destroyLoadBox()
displayErrorBox('Error', 'Could not logout.')
return
end
if Game.isOnline() then
Game.logout(false)
if tries == 1 then
loadBox = displayCancelBox('Please wait', 'Loggin out...')
end
scheduleEvent(function() tryLogin(charInfo, tries+1) end, 250)
return
end
CharacterList.destroyLoadBox()
Game.loginWorld(EnterGame.account, EnterGame.password, charInfo.worldHost, charInfo.worldPort, charInfo.characterName)
loadBox = displayCancelBox('Please wait', 'Connecting to game server...')
function loadBox.onCancel()
loadBox = nil
Game.cancelLogin()
CharacterList.show()
end
-- save last used character
Configs.set('lastUsedCharacter', charInfo.characterName)
end
-- public functions
function CharacterList.create(characters, premDays)
if charactersWindow then
charactersWindow:destroy()
end
charactersWindow = UI.loadAndDisplay('/entergame/characterlist.otui')
characterList = charactersWindow:getChildById('characterList')
local accountStatusLabel = charactersWindow:getChildById('accountStatusLabel')
charactersWindow.onKeyPress = onCharactersWindowKeyPress
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 = UILabel.create()
characterList:addChild(label)
label:setText(characterName .. ' (' .. worldName .. ')')
label:setStyle('CharacterListLabel')
label.characterName = characterName
label.worldHost = worldHost
label.worldPort = worldIp
if i == 1 or Configs.get('lastUsedCharacter') == characterName then
characterList:focusChild(label, ActiveFocusReason)
end
end
if premDays > 0 then
accountStatusLabel:setText("Account Status:\nPremium Account (" .. premDays .. ' days left)')
end
end
function CharacterList.hide()
charactersWindow:hide()
end
function CharacterList.destroy()
CharacterList.hide()
EnterGame.show()
end
function CharacterList.show()
if not loadBox then
charactersWindow:show()
end
end
function CharacterList.doLogin()
local selected = charactersWindow:getChildById('characterList'):getFocusedChild()
if selected then
local charInfo = { worldHost = selected.worldHost,
worldPort = selected.worldPort,
characterName = selected.characterName }
CharacterList.hide()
tryLogin(charInfo)
else
displayErrorBox('Error', 'You must select a character to login!')
end
end
function CharacterList.destroyLoadBox()
if loadBox then
loadBox:destroy()
loadBox = nil
end
end

View File

@@ -0,0 +1,68 @@
CharacterListLabel < Label
image: /core_styles/images/empty_rect.png
font: verdana-11px-monochrome
background-color: #00000000
offset: 2 0
focusable: true
margin.left: 1
margin.right: 1
margin.top: 1
state.focus:
background-color: #ffffff22
color: #ffffff
MainWindow
id: charactersWindow
title: Character List
size: 250 250
onEnter: CharacterList.doLogin()
onEscape: CharacterList.destroy()
TextList
id: characterList
anchors.fill: parent
anchors.bottom: next.top
margin.top: 30
margin.bottom: 5
margin.left: 16
margin.right: 16
Label
id: accountStatusLabel
text: |-
Account Status:
Free Account
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: next.top
margin.left: 16
margin.bottom: 5
HorizontalSeparator
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: next.top
margin.left: 16
margin.right: 16
margin.bottom: 10
Button
id: buttonOk
text: Ok
width: 64
anchors.right: next.left
anchors.bottom: parent.bottom
margin.bottom: 16
margin.right: 16
onClick: CharacterList.doLogin()
Button
id: buttonCancel
text: Cancel
width: 64
anchors.right: parent.right
anchors.bottom: parent.bottom
margin.bottom: 16
margin.right: 16
onClick: CharacterList.destroy()

View File

@@ -0,0 +1,69 @@
EnterGame = { }
-- private variables
local loadBox
local enterGame
local motdNumber
local motdMessage
-- private functions
local function onError(protocol, error)
loadBox:destroy()
local errorBox = displayErrorBox('Login Error', error)
errorBox.onOk = EnterGame.create
end
local function onMotd(protocol, motd)
motdNumber = tonumber(string.sub(motd, 0, string.find(motd, "\n")))
motdMessage = string.sub(motd, string.find(motd, "\n") + 1, string.len(motd))
end
local function onCharacterList(protocol, characters, premDays)
loadBox:destroy()
CharacterList.create(characters, premDays)
local lastMotdNumber = tonumber(Configs.get("motd"))
if motdNumber and motdNumber ~= lastMotdNumber then
Configs.set("motd", motdNumber)
local motdBox = displayInfoBox("Message of the day", motdMessage)
motdBox.onOk = CharacterList.show
CharacterList.hide()
end
end
-- public functions
function EnterGame.create()
enterGame = UI.loadAndDisplay('/entergame/entergame.otui')
end
function EnterGame.destroy()
enterGame:destroy()
enterGame = nil
end
function EnterGame.show()
enterGame:show()
end
function EnterGame.hide()
enterGame:hide()
end
function EnterGame.doLogin()
EnterGame.account = enterGame:getChildById('accountNameLineEdit'):getText()
EnterGame.password = enterGame:getChildById('accountPasswordLineEdit'):getText()
EnterGame.hide()
local protocolLogin = ProtocolLogin.create()
protocolLogin.onError = onError
protocolLogin.onMotd = onMotd
protocolLogin.onCharacterList = onCharacterList
loadBox = displayCancelBox('Please wait', 'Connecting to login server...')
loadBox.onCancel = function(msgbox)
protocolLogin:cancelLogin()
EnterGame.show()
end
protocolLogin:login(EnterGame.account, EnterGame.password)
end

View File

@@ -0,0 +1,21 @@
Module
name: entergame
description: Manages enter game and character list windows
author: OTClient team
website: https://github.com/edubart/otclient
autoLoad: true
dependencies:
- core
- topmenu
- background
onLoad: |
require 'entergame'
require 'characterlist'
EnterGame.create()
return true
onUnload:
EnterGame.destroy()

View File

@@ -0,0 +1,57 @@
MainWindow
id: enterGame
title: Enter Game
size: 236 160
onEnter: EnterGame.doLogin()
onEscape: EnterGame.hide()
LargerLabel
text: Account name
anchors.left: parent.left
anchors.top: parent.top
margin.left: 18
margin.top: 28
LineEdit
id: accountNameLineEdit
text: otclient0
anchors.left: parent.left
anchors.right: parent.right
anchors.top: prev.bottom
margin.top: 2
margin.left: 18
margin.right: 18
LargerLabel
text: Password
anchors.left: prev.left
anchors.top: prev.bottom
margin.top: 8
PasswordLineEdit
id: accountPasswordLineEdit
text: 123456
anchors.left: parent.left
anchors.right: parent.right
anchors.top: prev.bottom
margin.top: 2
margin.left: 18
margin.right: 18
Button
text: Ok
width: 64
anchors.right: next.left
anchors.bottom: parent.bottom
margin.bottom: 16
margin.right: 16
onClick: EnterGame.doLogin()
Button
text: Cancel
width: 64
anchors.right: parent.right
anchors.bottom: parent.bottom
margin.bottom: 16
margin.right: 16
onClick: EnterGame.hide()