UiLogin.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:11k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /*****************************************************************************************
  2. // 界面--login窗口
  3. // Copyright : Kingsoft 2002
  4. // Author :   Wooy(Wu yue)
  5. // CreateTime: 2002-8-12
  6. *****************************************************************************************/
  7. #include "KWin32.h"
  8. #include "KIniFile.h"
  9. #include "../Elem/WndMessage.h"
  10. #include "../Elem/Wnds.h"
  11. #include "../UiBase.h"
  12. #include "UiInformation.h"
  13. #include "UiLogin.h"
  14. #include "UiSelServer.h"
  15. #include "UiLoginBg.h"
  16. #include "UiConnectInfo.h"
  17. #include "../../Login/Login.h"
  18. #include "../UiSoundSetting.h"
  19. #include <crtdbg.h>
  20. #include "../../ENGINE/Src/KSG_MD5_String.h"
  21. #define SCHEME_INI_LOGIN "登陆.ini"
  22. KUiLogin* KUiLogin::m_pSelf = NULL;
  23. //--------------------------------------------------------------------------
  24. // 功能:构造函数
  25. //--------------------------------------------------------------------------
  26. KUiLogin::KUiLogin()
  27. {
  28. m_szLoginBg[0] = 0;
  29. }
  30. //--------------------------------------------------------------------------
  31. // 功能:析构函数
  32. //--------------------------------------------------------------------------
  33. KUiLogin::~KUiLogin()
  34. {
  35. }
  36. //--------------------------------------------------------------------------
  37. // 功能:打开窗口,返回唯一的一个类对象实例
  38. //--------------------------------------------------------------------------
  39. KUiLogin* KUiLogin::OpenWindow()
  40. {
  41. if (m_pSelf == NULL)
  42. {
  43. m_pSelf = new KUiLogin;
  44. if (m_pSelf)
  45. m_pSelf->Initialize();
  46. }
  47. if (m_pSelf)
  48. {
  49. UiSoundPlay(UI_SI_POPUP_OUTGAME_WND);
  50.         m_pSelf->Show();
  51. }
  52.     return m_pSelf;
  53. }
  54. //--------------------------------------------------------------------------
  55. // 功能:关闭窗口,同时可以选则是否删除对象实例
  56. //--------------------------------------------------------------------------
  57. void KUiLogin::CloseWindow(bool bDestroy)
  58. {
  59. if (m_pSelf)
  60. {
  61. g_LoginLogic.SetRememberAccountFlag(m_pSelf->m_RememberAccount.IsButtonChecked() != 0);
  62. if (bDestroy)
  63. {
  64. m_pSelf->Destroy();
  65. m_pSelf = NULL;
  66. }
  67. else
  68. {
  69. m_pSelf->m_PassWord.ClearText(true);
  70. m_pSelf->Hide();
  71. }
  72. }
  73. }
  74. void KUiLogin::Show()
  75. {
  76. KWndShowAnimate::Show();
  77. KUiLoginBackGround::SetConfig(m_szLoginBg);
  78. char szAccount[32];
  79. m_RememberAccount.CheckButton(g_LoginLogic.GetLoginAccount(szAccount));
  80. m_Account.SetText(szAccount);
  81. if (szAccount[0])
  82. Wnd_SetFocusWnd(&m_PassWord);
  83. else
  84. Wnd_SetFocusWnd(&m_Account);
  85. }
  86. //--------------------------------------------------------------------------
  87. // 功能:初始化
  88. //--------------------------------------------------------------------------
  89. void KUiLogin::Initialize()
  90. {
  91. AddChild(&m_Account);
  92. AddChild(&m_PassWord);
  93. AddChild(&m_Login);
  94. AddChild(&m_Cancel);
  95. AddChild(&m_RememberAccount);
  96. char Scheme[256];
  97. g_UiBase.GetCurSchemePath(Scheme, 256);
  98. LoadScheme(Scheme);
  99. Wnd_AddWindow(this, WL_TOPMOST);
  100. }
  101. //--------------------------------------------------------------------------
  102. // 功能:载入窗口的界面方案
  103. //--------------------------------------------------------------------------
  104. void KUiLogin::LoadScheme(const char* pScheme)
  105. {
  106. char Buff[128];
  107. KIniFile Ini;
  108. sprintf(Buff, "%s\%s", pScheme, SCHEME_INI_LOGIN);
  109. if (Ini.Load(Buff))
  110. {
  111. KWndShowAnimate::Init(&Ini, "Main");
  112. m_Account .Init(&Ini, "Account");
  113. m_PassWord.Init(&Ini, "Password");
  114. m_Login   .Init(&Ini, "Login");
  115. m_Cancel  .Init(&Ini, "Cancel");
  116. m_RememberAccount.Init(&Ini, "Remember");
  117. Ini.GetString("Main", "LoginBg", "", m_szLoginBg, sizeof(m_szLoginBg));
  118. }
  119. }
  120. //--------------------------------------------------------------------------
  121. // 功能:窗口函数
  122. //--------------------------------------------------------------------------
  123. int KUiLogin::WndProc(unsigned int uMsg, unsigned int uParam, int nParam)
  124. {
  125. int nRet = 0;
  126. switch(uMsg)
  127. {
  128. case WND_N_BUTTON_CLICK:
  129. if (uParam == (unsigned int)(KWndWindow*)&m_Login)
  130. OnLogin();
  131. else if (uParam == (unsigned int)(KWndWindow*)&m_Cancel)
  132. OnCancel();
  133. break;
  134. case WND_N_EDIT_SPECIAL_KEY_DOWN:
  135. if (nParam == VK_TAB)
  136. {
  137.             if ((GetKeyState(VK_SHIFT) & 0x8000) == 0)
  138.             {
  139.                 // 如果没有按住Shift
  140.     if (uParam == (unsigned int)(KWndWindow*)&m_Account)
  141.     {
  142.     int x, y, cx;
  143.     m_PassWord.GetAbsolutePos(&x, &y);
  144.     m_PassWord.GetSize(&cx, NULL);
  145.     Wnd_SetCursorPos(x + cx, y);
  146.     Wnd_SetFocusWnd(&m_PassWord);
  147.     nRet = 1;
  148.     }
  149.     else if (uParam == (unsigned int)(KWndWindow*)&m_PassWord)
  150.     {
  151.     Wnd_SetFocusWnd(NULL);
  152.     m_Login.SetCursorAbove();
  153.     nRet = 1;
  154.     }
  155.             }
  156.             else
  157.             {   
  158.                 // 如果按住Shift
  159.     if (uParam == (unsigned int)(KWndWindow*)&m_Account)
  160.     {
  161.       Wnd_SetFocusWnd(NULL);
  162.            m_Cancel.SetCursorAbove();
  163.     nRet = 1;
  164.     }
  165.     else if (uParam == (unsigned int)(KWndWindow*)&m_PassWord)
  166.     {
  167.         int x, y, cx;
  168.         m_Account.GetAbsolutePos(&x, &y);
  169.         m_Account.GetSize(&cx, NULL);
  170.         Wnd_SetCursorPos(x + cx, y);
  171.         Wnd_SetFocusWnd(&m_Account);
  172.     nRet = 1;
  173.     }
  174.             }
  175. }
  176. else if (nParam == VK_RETURN)
  177. {
  178. if (uParam == (unsigned int)(KWndWindow*)&m_Account)
  179. {
  180. Wnd_SetFocusWnd(&m_PassWord);
  181. nRet = 1;
  182. }
  183. else if (uParam == (unsigned int)(KWndWindow*)&m_PassWord)
  184. {
  185. OnLogin();
  186. nRet = 1;
  187. }
  188. }
  189. else if (nParam == VK_ESCAPE)
  190. {
  191. OnCancel();
  192. nRet = 1;
  193. }
  194. break;
  195. case WM_KEYDOWN:
  196. OnKeyDown(uParam);
  197. nRet = 1;
  198. break;
  199. case WM_SYSKEYDOWN:
  200. if (uParam == 'A')
  201. OnEnableAutoLogin();
  202. break;
  203. default:
  204. nRet = KWndShowAnimate::WndProc(uMsg, uParam, nParam);
  205. break;
  206. }
  207. return nRet;
  208. }
  209. void KUiLogin::OnEnableAutoLogin()
  210. {
  211. KIniFile* pSetting = g_UiBase.GetCommConfigFile();
  212. int nAutoLogin = false;
  213. if (pSetting)
  214. {
  215. pSetting->GetInteger("Main", "AutoLogin", 0, &nAutoLogin);
  216. g_UiBase.CloseCommConfigFile();
  217. }
  218. if (nAutoLogin == 6323)
  219. {
  220. g_LoginLogic.SetRememberAllFlag(true);
  221. m_RememberAccount.CheckButton(true);
  222. }
  223. }
  224. void KUiLogin::OnKeyDown(unsigned int uKey)
  225. {
  226. if (uKey == VK_RETURN)
  227. {
  228. if (m_Cancel.IsButtonActive())
  229. OnCancel();
  230. else
  231. OnLogin();
  232. }
  233. else if (uKey == VK_TAB)
  234. {
  235.         if ((GetKeyState(VK_SHIFT) & 0x8000) == 0)
  236.         {
  237.             // 如果没有按住Shift
  238.      if (m_Login.IsButtonActive())
  239.     m_RememberAccount.SetCursorAbove();
  240.     else if (m_RememberAccount.IsButtonActive())
  241.     m_Cancel.SetCursorAbove();
  242.     else
  243.     {
  244.     int x, y, cx;
  245.     m_Account.GetAbsolutePos(&x, &y);
  246.     m_Account.GetSize(&cx, NULL);
  247.     Wnd_SetCursorPos(x + cx, y);
  248.     Wnd_SetFocusWnd(&m_Account);
  249.     }
  250.         }
  251.         else
  252.         {   
  253.             // 如果按住Shift
  254.      if (m_Login.IsButtonActive())
  255.             {
  256. int x, y, cx;
  257. m_PassWord.GetAbsolutePos(&x, &y);
  258. m_PassWord.GetSize(&cx, NULL);
  259. Wnd_SetCursorPos(x + cx, y);
  260. Wnd_SetFocusWnd(&m_PassWord);
  261.             }
  262.     else if (m_RememberAccount.IsButtonActive())
  263.             {
  264.          m_Login.SetCursorAbove();
  265.     }
  266.             else   // if m_Cancel.IsButtonActive() 
  267.     {
  268.                 m_RememberAccount.SetCursorAbove();
  269.     }
  270.             
  271.         }
  272. }
  273. else if (uKey == VK_SPACE)
  274. {
  275. if (m_Login.IsButtonActive())
  276. OnLogin();
  277. else if (m_Cancel.IsButtonActive())
  278. OnCancel();
  279. else if (m_RememberAccount.IsButtonActive())
  280. m_RememberAccount.CheckButton(!m_RememberAccount.IsButtonChecked());
  281. }
  282. else if (uKey == VK_LEFT)
  283. m_Login.SetCursorAbove();
  284. else if (uKey == VK_RIGHT)
  285. m_Cancel.SetCursorAbove();
  286. else if (uKey == VK_ESCAPE)
  287. OnCancel();
  288. }
  289. //--------------------------------------------------------------------------
  290. // 功能:登录
  291. //--------------------------------------------------------------------------
  292. void KUiLogin::OnLogin()
  293. {
  294. char szAccount[32];
  295.     char      szPassword[KSG_PASSWORD_MAX_SIZE];
  296. KSG_PASSWORD Password;
  297. if (GetInputInfo(szAccount, szPassword))
  298. {
  299.         #ifdef SWORDONLINE_USE_MD5_PASSWORD
  300.         KSG_StringToMD5String(Password.szPassword, szPassword);
  301.         #else
  302.         #pragma message (KSG_ATTENTION("Add Password to MD5 string"))
  303.         strncpy(Password.szPassword, szPassword, sizeof(Password.szPassword));
  304.         Password.szPassword[sizeof(Password.szPassword) - 1] = '';
  305.         #endif
  306. g_LoginLogic.AccountLogin(szAccount, Password);
  307. KUiConnectInfo::OpenWindow(CI_MI_CONNECTING, LL_S_ROLE_LIST_READY);
  308. memset(&szPassword, 0, sizeof(szPassword));
  309. memset(&Password, 0, sizeof(Password));
  310. CloseWindow(false);
  311. }
  312. }
  313. //--------------------------------------------------------------------------
  314. // 功能:取消
  315. //--------------------------------------------------------------------------
  316. void KUiLogin::OnCancel()
  317. {
  318. CloseWindow(false);
  319. g_LoginLogic.ReturnToIdle();
  320. KUiSelServer::OpenWindow(); //点击『取消』按钮返回『初始界面』;
  321. }
  322. //--------------------------------------------------------------------------
  323. // 功能:获取输入的账号密码信息
  324. //--------------------------------------------------------------------------
  325. int KUiLogin::GetInputInfo(char* pszAccount, char* pszPassword)
  326. {
  327. _ASSERT(pszAccount && pszPassword);
  328. if (m_Account.GetText(pszAccount, 32, false) &&
  329. m_PassWord.GetText(pszPassword, 32, false))
  330. {
  331. return true;
  332. }
  333. CloseWindow(false);
  334. KUiConnectInfo::OpenWindow(CI_MI_ERROR_LOGIN_INPUT, CI_NS_LOGIN_WND);
  335. return false;
  336. /* int nLen = m_Account.GetText(m_szAccount, 32, false);
  337. unsigned char cCode;
  338. if (nLen >= LOGIN_ACCOUNT_MIN_LEN && nLen <= LOGIN_ACCOUNT_MAX_LEN)
  339. {
  340. for (int i = 0; i < nLen;)
  341. {
  342. cCode = (unsigned char)m_szAccount[i];
  343. if (i + 2 <= nLen && cCode >= 0x81 && cCode <= 0xfe &&
  344. (cCode <= 0xa0 || cCode >= 0xaa))
  345. {
  346. i += 2;
  347. }
  348. else if ((cCode >= 0x30 && cCode <= 0x39) ||
  349. (cCode >= 0x41 && cCode <= 0x5a) ||
  350. (cCode >= 0x61 && cCode <= 0x7a))
  351. {
  352. i++;
  353. }
  354. else
  355. break;
  356. }
  357. if (i == nLen)
  358. {
  359. nLen = m_PassWord.GetText(m_szPassword, 32, false);
  360. if (IsValidPwd(m_szPassword, nLen))
  361. return true;
  362. }
  363. }
  364. KUiConnectInfo::OpenWindow(CI_MI_ACCOUNT_PWD_ERROR, CI_AE_BACK_TO_LOGIN);
  365. CloseWindow(false);
  366. return false;*/
  367. }
  368. bool KUiLogin::IsValidPassword(const char* pszPassword, int nLen)
  369. {
  370. return true; //to be del this line;
  371. if (pszPassword)
  372. {
  373. if (nLen < 0)
  374. nLen = strlen(pszPassword);
  375. if (nLen >= LOGIN_PASSWORD_MIN_LEN && nLen <= LOGIN_PASSWORD_MAX_LEN)
  376. {
  377. for (int i = 0; i < nLen; i++)
  378. {
  379. unsigned char cCode = (unsigned char)pszPassword[i];
  380. if ((cCode < 0x30 || cCode > 0x39) &&
  381. (cCode < 0x41 || cCode > 0x5a) &&
  382. (cCode < 0x61 || cCode > 0x7a))
  383. {
  384. break;
  385. }
  386. if (i == nLen)
  387. return true;
  388. }
  389. }
  390. }
  391. return false;
  392. }