basic charlist

This commit is contained in:
Eduardo Bart 2011-08-26 15:00:22 -03:00
parent b58a1aa7d4
commit c60b677baa
13 changed files with 120 additions and 24 deletions

View File

@ -1,5 +1,5 @@
TextList < UIWidget TextList < UIWidget
size: 200 200 layout: verticalBox
border-image: border-image:
source: /core_ui/images/panel_flat.png source: /core_ui/images/panel_flat.png
border: 4 border: 4

View File

@ -1,3 +1,17 @@
local account
local password
function EnterGame_characterWindow_okClicked()
local charactersWindow = UI.root:getChildById('charactersWindow')
local selected = charactersWindow:getChildById('charactersList'):getFocusedChild()
if selected then
Game.loginWorld(account, password, selected.worldHost, selected.worldPort, selected.characterName)
charactersWindow:destroy()
mainMenu:hide()
end
end
function EnterGame_connectToLoginServer() function EnterGame_connectToLoginServer()
local protocolLogin = ProtocolLogin.create() local protocolLogin = ProtocolLogin.create()
@ -27,12 +41,31 @@ function EnterGame_connectToLoginServer()
displayInfoBox("Message of the day", motdText) displayInfoBox("Message of the day", motdText)
Configs.set("motd", motdNumber) Configs.set("motd", motdNumber)
end end
mainMenu:hide() end
protocolLogin.onCharacterList = function(protocol, characters, premDays)
loadBox:destroy()
local charactersWindow = UI.loadAndDisplayLocked('/mainmenu/ui/charlist.otui')
local charactersList = charactersWindow:getChildById('charactersList')
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()
charactersList:addChild(label)
label:setText(characterName .. ' (' .. worldName .. ')')
label:setStyle('CharactersListLabel')
label.characterName = characterName
label.worldHost = worldHost
label.worldPort = worldIp
end
end end
local enterGameWindow = UI.root:getChildById("enterGameWindow") local enterGameWindow = UI.root:getChildById("enterGameWindow")
local account = enterGameWindow:getChildById("accountNameLineEdit"):getText() account = enterGameWindow:getChildById("accountNameLineEdit"):getText()
local password = enterGameWindow:getChildById("accountPasswordLineEdit"):getText() password = enterGameWindow:getChildById("accountPasswordLineEdit"):getText()
protocolLogin:login(account, password) protocolLogin:login(account, password)
enterGameWindow:destroy() enterGameWindow:destroy()

View File

@ -1,3 +1,16 @@
CharactersListLabel < Label
image: /core_ui/images/empty_rect.png
font: helvetica-12px-bold
background-color: #00000000
focusable: true
margin.left: 1
margin.right: 1
margin.top: 1
state.focus:
background-color: #ffffff22
color: #ffffff
MainWindow MainWindow
id: charactersWindow id: charactersWindow
title: Charlist title: Charlist
@ -19,6 +32,7 @@ MainWindow
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.bottom: 16 margin.bottom: 16
margin.right: 16 margin.right: 16
onClick: EnterGame_characterWindow_okClicked()
Button Button
id: buttonCancel id: buttonCancel

View File

@ -42,6 +42,7 @@ void LuaInterface::registerFunctions()
g_lua.bindClassMemberFunction<UIWidget>("getChildById", &UIWidget::getChildById); g_lua.bindClassMemberFunction<UIWidget>("getChildById", &UIWidget::getChildById);
g_lua.bindClassMemberFunction<UIWidget>("getChildByIndex", &UIWidget::getChildByIndex); g_lua.bindClassMemberFunction<UIWidget>("getChildByIndex", &UIWidget::getChildByIndex);
g_lua.bindClassMemberFunction<UIWidget>("getChildCount", &UIWidget::getChildCount); g_lua.bindClassMemberFunction<UIWidget>("getChildCount", &UIWidget::getChildCount);
g_lua.bindClassMemberFunction<UIWidget>("getFocusedChild", &UIWidget::getFocusedChild);
g_lua.bindClassMemberFunction<UIWidget>("insertChild", &UIWidget::insertChild); g_lua.bindClassMemberFunction<UIWidget>("insertChild", &UIWidget::insertChild);
g_lua.bindClassMemberFunction<UIWidget>("removeChild", &UIWidget::removeChild); g_lua.bindClassMemberFunction<UIWidget>("removeChild", &UIWidget::removeChild);
g_lua.bindClassMemberFunction<UIWidget>("addChild", &UIWidget::addChild); g_lua.bindClassMemberFunction<UIWidget>("addChild", &UIWidget::addChild);

View File

@ -76,6 +76,13 @@ template<typename Ret, typename... Args>
typename std::enable_if<!std::is_void<Ret>::value, bool>::type typename std::enable_if<!std::is_void<Ret>::value, bool>::type
luavalue_cast(int index, std::function<Ret(Args...)>& func); luavalue_cast(int index, std::function<Ret(Args...)>& func);
// vector
template<typename T>
void push_luavalue(const std::vector<T>& vec);
// tuple
template<typename... Args>
void push_luavalue(const std::tuple<Args...>& tuple);
// start definitions // start definitions
@ -181,4 +188,38 @@ luavalue_cast(int index, std::function<Ret(Args...)>& func) {
return false; return false;
} }
template<typename T>
void push_luavalue(const std::vector<T>& vec) {
g_lua.newTable();
int i = 1;
for(const T& v : vec) {
push_luavalue(v);
g_lua.rawSeti(i);
i++;
}
}
template<int N>
struct push_tuple_luavalue {
template<typename Tuple>
static void call(const Tuple& tuple) {
push_luavalue(std::get<N-1>(tuple));
g_lua.rawSeti(N);
push_tuple_luavalue<N-1>::call(tuple);
}
};
template<>
struct push_tuple_luavalue<0> {
template<typename Tuple>
static void call(const Tuple& tuple) { }
};
template<typename... Args>
void push_luavalue(const std::tuple<Args...>& tuple) {
g_lua.newTable();
push_tuple_luavalue<sizeof...(Args)>::call(tuple);
}
#endif #endif

View File

@ -20,9 +20,7 @@ void UILabel::setText(const std::string& text)
m_text = text; m_text = text;
// auto resize // auto resize
if(!m_fixedSize) if(!m_fixedSize && !m_rect.isValid()) {
resizeToText();
else if(!m_rect.isValid()) {
Size textSize = m_font->calculateTextRectSize(m_text); Size textSize = m_font->calculateTextRectSize(m_text);
if(m_rect.width() <= 0) if(m_rect.width() <= 0)
m_rect.setWidth(textSize.width()); m_rect.setWidth(textSize.width());

View File

@ -7,6 +7,7 @@
#include <sstream> #include <sstream>
#include <exception> #include <exception>
#include <cxxabi.h> #include <cxxabi.h>
#include "types.h"
namespace fw { namespace fw {
@ -227,6 +228,12 @@ inline unsigned int hex2dec(const std::string& str) {
return num; return num;
} }
inline std::string ip2str(uint32 ip) {
char host[16];
sprintf(host, "%d.%d.%d.%d", (uint8)ip, (uint8)(ip >> 8), (uint8)(ip >> 16), (uint8)(ip >> 24));
return std::string(host);
}
// an empty string to use anywhere needed // an empty string to use anywhere needed
const static std::string empty_string; const static std::string empty_string;

View File

@ -15,10 +15,10 @@ void Game::terminate()
logout(); logout();
} }
void Game::loginWorld(const std::string& account, const std::string& password, uint32 worldIp, uint16 worldPort, const std::string& characterName) void Game::loginWorld(const std::string& account, const std::string& password, const std::string& worldHost, int worldPort, const std::string& characterName)
{ {
m_protocolGame = ProtocolGamePtr(new ProtocolGame); m_protocolGame = ProtocolGamePtr(new ProtocolGame);
m_protocolGame->login(account, password, worldIp, worldPort, characterName); m_protocolGame->login(account, password, worldHost, (uint16)worldPort, characterName);
} }
void Game::logout() void Game::logout()

View File

@ -12,7 +12,7 @@ public:
void loginWorld(const std::string& account, void loginWorld(const std::string& account,
const std::string& password, const std::string& password,
uint32 worldIp, uint16 worldPort, const std::string& worldHost, int worldPort,
const std::string& characterName); const std::string& characterName);
void logout(); void logout();

View File

@ -6,7 +6,7 @@ ProtocolGame::ProtocolGame()
m_waitingLoginPacket = false; m_waitingLoginPacket = false;
} }
void ProtocolGame::login(const std::string& accountName, const std::string& accountPassword, uint32 ip, uint16 port, const std::string& characterName) void ProtocolGame::login(const std::string& accountName, const std::string& accountPassword, const std::string& host, uint16 port, const std::string& characterName)
{ {
if(accountName.empty() || accountPassword.empty()) { if(accountName.empty() || accountPassword.empty()) {
callLuaField("onError", "You must enter an account name and password."); callLuaField("onError", "You must enter an account name and password.");
@ -18,9 +18,6 @@ void ProtocolGame::login(const std::string& accountName, const std::string& acco
m_accountPassword = accountPassword; m_accountPassword = accountPassword;
m_characterName = characterName; m_characterName = characterName;
char host[16];
sprintf(host, "%d.%d.%d.%d", (uint8)ip, (uint8)(ip >> 8), (uint8)(ip >> 16), (uint8)(ip >> 24));
connect(host, port); connect(host, port);
} }

View File

@ -13,7 +13,7 @@ public:
ProtocolGame(); ProtocolGame();
public: public:
void login(const std::string& accountName, const std::string& accountPassword, uint32 ip, uint16 port, const std::string& characterName); void login(const std::string& accountName, const std::string& accountPassword, const std::string& host, uint16 port, const std::string& characterName);
void onConnect(); void onConnect();
void onRecv(InputMessage& inputMessage); void onRecv(InputMessage& inputMessage);

View File

@ -112,18 +112,19 @@ void ProtocolLogin::parseMOTD(InputMessage& inputMessage)
void ProtocolLogin::parseCharacterList(InputMessage& inputMessage) void ProtocolLogin::parseCharacterList(InputMessage& inputMessage)
{ {
uint8 characters = inputMessage.getU8(); typedef std::tuple<std::string, std::string, std::string, int> CharacterInfo;
for(int i = 0; i < characters; ++i) { typedef std::vector<CharacterInfo> CharaterList;
CharaterList charList;
int numCharacters = inputMessage.getU8();
for(int i = 0; i < numCharacters; ++i) {
std::string name = inputMessage.getString(); std::string name = inputMessage.getString();
std::string world = inputMessage.getString(); std::string world = inputMessage.getString();
uint32 ip = inputMessage.getU32(); uint32 ip = inputMessage.getU32();
uint16 port = inputMessage.getU16(); uint16 port = inputMessage.getU16();
charList.push_back(CharacterInfo(name, world, fw::ip2str(ip), port));
}
int premDays = inputMessage.getU16();
// TODO just test callLuaField("onCharacterList", charList, premDays);
if(i == 0) {
g_game.loginWorld(m_accountName, m_accountPassword, ip, port, name);
break;
}
}
/*uint16 premiumDays =*/ inputMessage.getU16();
} }

View File

@ -1,6 +1,7 @@
#include "otclient.h" #include "otclient.h"
#include <framework/luascript/luainterface.h> #include <framework/luascript/luainterface.h>
#include <otclient/core/game.h>
#include <otclient/core/datmanager.h> #include <otclient/core/datmanager.h>
#include <otclient/core/spritemanager.h> #include <otclient/core/spritemanager.h>
#include <otclient/net/protocollogin.h> #include <otclient/net/protocollogin.h>
@ -19,4 +20,7 @@ void OTClient::registerLuaFunctions()
g_lua.bindClassMemberFunction("cancelLogin", &ProtocolLogin::cancelLogin); g_lua.bindClassMemberFunction("cancelLogin", &ProtocolLogin::cancelLogin);
g_lua.registerClass<ProtocolGame, Protocol>(); g_lua.registerClass<ProtocolGame, Protocol>();
g_lua.registerClass<Game>();
g_lua.bindClassStaticFunction<Game>("loginWorld", std::bind(&Game::loginWorld, &g_game, _1, _2, _3, _4, _5));
} }