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

模拟服务器

开发平台:

C/C++

  1. /*****************************************************************************************
  2. // 界面--选择附近的某个玩家
  3. // Copyright : Kingsoft 2003
  4. // Author :   Wooy(Wu yue)
  5. // CreateTime: 2003-2-26
  6. *****************************************************************************************/
  7. #include "KWin32.h"
  8. #include "KIniFile.h"
  9. #include "../elem/wnds.h"
  10. #include "../Elem/WndMessage.h"
  11. #include "../UiBase.h"
  12. #include "UiSelPlayerNearby.h"
  13. #include <crtdbg.h>
  14. #include "../../../core/src/coreshell.h"
  15. #include "../../../core/src/gamedatadef.h"
  16. #include "../UiSoundSetting.h"
  17. #include "UiGame.h"
  18. extern iCoreShell* g_pCoreShell;
  19. #define SCHEME_INI "选择附近玩家.ini"
  20. KUiSelPlayerNearby* KUiSelPlayerNearby::m_pSelf = NULL;
  21. //--------------------------------------------------------------------------
  22. // 功能:打开窗口,返回唯一的一个类对象实例
  23. //--------------------------------------------------------------------------
  24. KUiSelPlayerNearby* KUiSelPlayerNearby::OpenWindow()
  25. {
  26. if (m_pSelf == NULL)
  27. {
  28. m_pSelf = new KUiSelPlayerNearby;
  29. if (m_pSelf)
  30. m_pSelf->Initialize();
  31. }
  32. if (m_pSelf)
  33. {
  34. UiSoundPlay(UI_SI_WND_OPENCLOSE);
  35. m_pSelf->BringToTop();
  36. m_pSelf->UpdateData();
  37. m_pSelf->Show();
  38. }
  39. return m_pSelf;
  40. }
  41. KUiSelPlayerNearby::KUiSelPlayerNearby()
  42. {
  43. m_pDataList = NULL;
  44. m_nPlayerCount = 0;
  45. m_nActionCount = 0;
  46. }
  47. int KUiSelPlayerNearby::DoesHaveVisibleWnd()
  48. {
  49. if (m_pSelf && m_pSelf->IsVisible())
  50. return true;
  51. return false;
  52. }
  53. //初始化
  54. void KUiSelPlayerNearby::Initialize()
  55. {
  56. AddChild(&m_PlayerList);
  57. AddChild(&m_ListScroll);
  58. m_PlayerList.SetScrollbar(&m_ListScroll);
  59. AddChild(&m_InputEdit);
  60. AddChild(&m_RefreshBtn);
  61. AddChild(&m_CancelBtn);
  62. Wnd_AddWindow(this);
  63. char Buffer[256];
  64. g_UiBase.GetCurSchemePath(Buffer, sizeof(Buffer));
  65. strcat(Buffer, "\"SCHEME_INI);
  66. KIniFile Ini;
  67. if (Ini.Load(Buffer))
  68. {
  69. LoadScheme(&Ini);
  70. }
  71. }
  72. void KUiSelPlayerNearby::LoadActionList(KIniFile* pSetting)
  73. {
  74. m_nActionCount = 0;
  75. int i;
  76. if (pSetting)
  77. {
  78. pSetting->GetInteger("InterPlayerAction", "Count", 0, &m_nActionCount);
  79. if (m_nActionCount > 0)
  80. {
  81. if (m_nActionCount > MAX_NUM_BUTTON)
  82. m_nActionCount = MAX_NUM_BUTTON;
  83. char szKey[16];
  84. for (i = 0; i < m_nActionCount; i++)
  85. {
  86. sprintf(szKey, "%d_Name", i);
  87. pSetting->GetString("InterPlayerAction", szKey, "",
  88. m_ActionList[i].szActionName,
  89. sizeof(m_ActionList[i].szActionName));
  90. sprintf(szKey, "%d_Id", i);
  91. pSetting->GetInteger("InterPlayerAction", szKey, -1,
  92. &m_ActionList[i].nId);
  93. m_ActionBtns[i].SetText(m_ActionList[i].szActionName);
  94. }
  95. }
  96. }
  97. for (i = 0;i < m_nActionCount; i++)
  98. AddChild(&m_ActionBtns[i]);
  99. for (i = m_nActionCount; i < MAX_NUM_BUTTON; i++)
  100. m_ActionBtns[i].LeaveAlone();
  101. }
  102. //--------------------------------------------------------------------------
  103. // 功能:关闭窗口,同时可以选则是否删除对象实例
  104. //--------------------------------------------------------------------------
  105. void KUiSelPlayerNearby::CloseWindow(bool bDestroy)
  106. {
  107. if (m_pSelf)
  108. {
  109. m_pSelf->Clear();
  110. if (bDestroy == false)
  111. {
  112. m_pSelf->Hide();
  113. }
  114. else
  115. {
  116. m_pSelf->Destroy();
  117. m_pSelf = NULL;
  118. }
  119. }
  120. }
  121. void KUiSelPlayerNearby::LoadScheme(const char* pScheme)
  122. {
  123. if (m_pSelf)
  124. {
  125. char Buff[128];
  126. KIniFile Ini;
  127. sprintf(Buff, "%s\%s", pScheme, SCHEME_INI);
  128. if (Ini.Load(Buff))
  129. {
  130. m_pSelf->LoadScheme(&Ini);
  131. }
  132. }
  133. }
  134. void KUiSelPlayerNearby::LoadScheme(KIniFile* pIni)
  135. {
  136. if (pIni)
  137. {
  138. Init(pIni, "Main");
  139. m_PlayerList.Init(pIni, "PlayerList");
  140. m_ListScroll.Init(pIni, "Scroll");
  141. m_CancelBtn .Init(pIni, "Close");
  142. m_RefreshBtn.Init(pIni, "Refresh");
  143. m_InputEdit.Init(pIni, "InputEdit");
  144. char szSection[16];
  145. for (int i = 0; i < MAX_NUM_BUTTON; i++)
  146. {
  147. sprintf(szSection, "ActionBtn_%d", i);
  148. m_ActionBtns[i].Init(pIni, szSection);
  149. }
  150. LoadActionList(pIni);
  151. }
  152. }
  153. //窗口函数
  154. int KUiSelPlayerNearby::WndProc(unsigned int uMsg, unsigned int uParam, int nParam)
  155. {
  156. int nRet = 0;
  157. switch(uMsg)
  158. {
  159. case WND_N_BUTTON_CLICK:
  160. if (uParam == (unsigned int)(KWndWindow*)&m_CancelBtn)
  161. CloseWindow(false);
  162. else if (uParam == (unsigned int)(KWndWindow*)&m_RefreshBtn)
  163. UpdateData();
  164. else
  165. {
  166. for (int i = 0; i < m_nActionCount; i++)
  167. {
  168. if (uParam == (unsigned int)(KWndWindow*)&m_ActionBtns[i])
  169. {
  170. OnClickXButton(i);
  171. break;
  172. }
  173. }
  174. }
  175. break;
  176. case WND_N_SCORLLBAR_POS_CHANGED:
  177. if (uParam == (unsigned int)(KWndWindow*)&m_ListScroll)
  178. m_PlayerList.SetTopItemIndex(nParam);
  179. break;
  180. case WND_N_LIST_ITEM_SEL:
  181. if (uParam == (unsigned int)(KWndWindow*)&m_PlayerList)
  182. {
  183. char szCurText[256];
  184. szCurText[0] = 0;
  185. m_PlayerList.GetString(nParam, szCurText);
  186. m_InputEdit.SetText(szCurText);
  187. }
  188. break;
  189. default:
  190. nRet = KWndShowAnimate::WndProc(uMsg, uParam, nParam);
  191. }
  192. return nRet;
  193. }
  194. void KUiSelPlayerNearby::OnClickXButton(int nAction)
  195. {
  196. char Buffer[64];
  197. KUiMsgParam Param;
  198. Param.nMsgLength = m_InputEdit.GetText(Buffer, sizeof(Buffer), true);
  199. if (Param.nMsgLength && nAction >= 0 && nAction < m_nActionCount)
  200. {
  201. if (m_ActionList[nAction].nId >= 0)
  202. {
  203. KUiPlayerItem player;
  204. player.Name[0] = 0;
  205. for (int i = 0; i < m_nPlayerCount; i++)
  206. {
  207. if (strcmp(Buffer, m_pDataList[i].Name) == 0)
  208. {
  209. player = m_pDataList[i];
  210. break;
  211. }
  212. }
  213. if (player.Name[0] == 0)
  214. {
  215. strcpy(player.Name, Buffer);
  216. player.nIndex = -1;
  217. player.uId = 0;
  218. player.nData = 0;
  219. }
  220. ProcessPeople(&player, m_ActionList[nAction].nId);
  221. UpdateData();
  222. }
  223. }
  224. }
  225. void KUiSelPlayerNearby::UpdateData()
  226. {
  227. Clear();
  228. int nCount = g_pCoreShell->GetGameData(GDI_NEARBY_PLAYER_LIST, 0, 0);
  229. if (nCount)
  230. {
  231. m_pDataList = (KUiPlayerItem*)malloc(sizeof(KUiPlayerItem) * nCount);
  232. if (m_pDataList)
  233. {
  234. KUiPlayerItem Item;
  235. m_nPlayerCount = g_pCoreShell->GetGameData(GDI_NEARBY_PLAYER_LIST, (unsigned int)m_pDataList, nCount);
  236. _ASSERT(m_nPlayerCount == nCount);
  237. m_PlayerList.SetContent((unsigned char*)m_pDataList, m_nPlayerCount,
  238. sizeof(KUiPlayerItem), (char*)(&Item.Name) - (char*)&Item);
  239. }
  240. }
  241. }
  242. void KUiSelPlayerNearby::Clear()
  243. {
  244. if (m_pDataList)
  245. {
  246. m_PlayerList.SetContent(0, 0, 0, 0);
  247. free(m_pDataList);
  248. m_pDataList = NULL;
  249. m_nPlayerCount = 0;
  250. }
  251. }