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

模拟服务器

开发平台:

C/C++

  1. /*****************************************************************************************
  2. // 界面--快速输入消息界面
  3. // Copyright : Kingsoft 2002
  4. // Author :   Wooy(Wu yue)
  5. // CreateTime: 2002-12-11
  6. *****************************************************************************************/
  7. #include "KWin32.h"
  8. #include "KIniFile.h"
  9. #include "../elem/wnds.h"
  10. #include "../Elem/WndMessage.h"
  11. #include "UiFastInputMsg.h"
  12. #include "UiMsgCentrePad.h"
  13. #include "../../TextCtrlCmd/TextCtrlCmd.h"
  14. #include "../UiBase.h"
  15. #include "../../../core/src/gamedatadef.h"
  16. #include "../../../core/src/coreshell.h"
  17. #include "../../../core/src/MsgGenreDef.h"
  18. #include <crtdbg.h>
  19. #include "../../../Represent/iRepresent/iRepresentShell.h"
  20. #include "../../../Engine/src/Text.h"
  21. extern iRepresentShell* g_pRepresentShell;
  22. extern iCoreShell* g_pCoreShell;
  23. #define SCHEME_INI "快速输入消息界面.ini"
  24. #define SWITCH_OPEN_CLOSE 1
  25. #define SEL_COLOR_MENU 1
  26. #define SEL_CHANNEL_MENU 2
  27. KUiFastInputMsg* KUiFastInputMsg::m_pSelf = NULL;
  28. KUiFastInputMsg::KUiFastInputMsg()
  29. {
  30. m_pMenuData    = NULL;
  31. m_pChannelData = NULL;
  32. m_bChannelMenu =  false;
  33. m_bColorMenu   = false;
  34. m_bLocked = false;
  35. }
  36. //--------------------------------------------------------------------------
  37. // 功能:如果窗口正被显示,则返回实例指针
  38. //--------------------------------------------------------------------------
  39. KUiFastInputMsg* KUiFastInputMsg::GetIfVisible()
  40. {
  41. if (m_pSelf && m_pSelf->IsVisible())
  42. return m_pSelf;
  43. return NULL;
  44. }
  45. KUiFastInputMsg::~KUiFastInputMsg()
  46. {
  47. Hide();
  48. if (m_bLocked == false)
  49. Wnd_UnRegisterHotKey(this, SWITCH_OPEN_CLOSE);
  50. }
  51. //--------------------------------------------------------------------------
  52. // 功能:打开窗口,返回唯一的一个类对象实例
  53. //--------------------------------------------------------------------------
  54. KUiFastInputMsg* KUiFastInputMsg::OpenWindow()
  55. {
  56. if (m_pSelf)
  57. m_pSelf->Show();
  58. else
  59. {
  60. m_pSelf = new KUiFastInputMsg;
  61. if (m_pSelf)
  62. m_pSelf->Initialize();
  63. }
  64. return m_pSelf;
  65. }
  66. //--------------------------------------------------------------------------
  67. // 功能:关闭窗口,同时可以选则是否删除对象实例
  68. //--------------------------------------------------------------------------
  69. void KUiFastInputMsg::CloseWindow(bool bDestroy)
  70. {
  71. if (m_pSelf)
  72. {
  73. if (bDestroy == false)
  74. m_pSelf->Hide();
  75. else
  76. {
  77. KIniFile* pSetting = g_UiBase.GetPrivateSettingFile();
  78. if (pSetting)
  79. {
  80. char Buffer[16];
  81. sprintf(Buffer, "%d,%d,%d", m_pSelf->m_CurColor.Color_b.r,
  82. m_pSelf->m_CurColor.Color_b.g, m_pSelf->m_CurColor.Color_b.b);
  83. pSetting->WriteString("UiConfig", "InputMsgColor", Buffer);
  84. g_UiBase.ClosePrivateSettingFile(true);
  85. }
  86. m_pSelf->Destroy();
  87. m_pSelf = NULL;
  88. }
  89. }
  90. }
  91. //显示窗口
  92. void KUiFastInputMsg::Show()
  93. {
  94. m_bChannelMenu =false;
  95. m_bColorMenu = false;
  96. Wnd_SetFocusWnd(&m_InputEdit);
  97. UpdateCurrentChannel();
  98. KWndImage::Show();
  99. }
  100. void KUiFastInputMsg::UpdateCurrentChannel()
  101. {
  102. if (m_pSelf)
  103. {
  104. KUiChatChannel Channel;
  105. memset(&Channel, 0, sizeof(KUiChatChannel));
  106. g_pCoreShell->GetGameData(GDI_CHAT_CURRENT_SEND_CHANNEL, 
  107. (unsigned int)&Channel, 0);
  108. m_pSelf->m_ChannelName.SetText(Channel.cTitle);
  109. }
  110. }
  111. //隐藏窗口
  112. void KUiFastInputMsg::Hide()
  113. {
  114. CancelMenu();
  115. KWndImage::Hide();
  116. }
  117. //初始化
  118. void KUiFastInputMsg::Initialize()
  119. {
  120. AddChild(&m_InputEdit);
  121. AddChild(&m_ChannelBtn);
  122. AddChild(&m_ColorBtn);
  123. AddChild(&m_SendBtn);
  124. AddChild(&m_ChannelName);
  125. char Buffer[256];
  126. g_UiBase.GetCurSchemePath(Buffer, 256);
  127. LoadScheme(Buffer);
  128. m_Style &= ~WND_S_VISIBLE;
  129. Wnd_AddWindow(this, WL_TOPMOST);
  130. KIniFile* pSetting = g_UiBase.GetPrivateSettingFile();
  131. if (pSetting)
  132. {
  133. pSetting->GetString("UiConfig", "InputMsgColor", "244,230,197", Buffer, sizeof(Buffer));
  134. m_CurColor.Color_dw = GetColor(Buffer);
  135. g_UiBase.ClosePrivateSettingFile(false);
  136. }
  137. else
  138. {
  139. m_CurColor.Color_b.r = 244;
  140. m_CurColor.Color_b.g = 230;
  141. m_CurColor.Color_b.b = 197;
  142. }
  143. m_CurColor.Color_b.a = 0;
  144. m_bLocked = false;
  145. Wnd_RegisterHotKey(this, SWITCH_OPEN_CLOSE, VK_RETURN, 0);
  146. }
  147. //载入界面方案
  148. void KUiFastInputMsg::LoadScheme(const char* pScheme)
  149. {
  150. if (m_pSelf)
  151. {
  152. char Buff[128];
  153. KIniFile Ini;
  154. sprintf(Buff, "%s\%s", pScheme, SCHEME_INI);
  155. if (Ini.Load(Buff))
  156. m_pSelf->LoadScheme(&Ini);
  157. }
  158. }
  159. void KUiFastInputMsg::LoadScheme(class KIniFile* pIni)
  160. {
  161. _ASSERT(pIni);
  162. Init(pIni, "Main");
  163. m_InputEdit .Init(pIni, "InputEdit");
  164. m_ChannelBtn .Init(pIni, "ChannelBtn");
  165. m_SendBtn .Init(pIni, "SendBtn");
  166. m_ColorBtn .Init(pIni, "ColorBtn");
  167. m_ChannelName.Init(pIni, "ChannelName");
  168. int i;
  169. m_cNumColor = 0;
  170. char Buffer[16], Value[4];
  171. for (i = 0; i < UI_INPUT_MSG_MAX_COLOR; i++)
  172. {
  173. itoa(i, Value, 10);
  174. if (pIni->GetString("ColorList", Value, "", Buffer, sizeof(Buffer)))
  175. {
  176. m_ColorList[m_cNumColor] = GetColor(Buffer);
  177. m_ColorList[m_cNumColor] = (m_ColorList[m_cNumColor] & 0xFFFFFF);
  178. m_cNumColor ++;
  179. }
  180. else
  181. break;
  182. }
  183. }
  184. //窗口函数
  185. int KUiFastInputMsg::WndProc(unsigned int uMsg, unsigned int uParam, int nParam)
  186. {
  187. switch(uMsg)
  188. {
  189. case WND_N_BUTTON_CLICK:
  190. if (uParam == (unsigned int)(KWndWindow*)&m_SendBtn)
  191. OnSend();
  192. else if (uParam == (unsigned int)(KWndWindow*)&m_ChannelBtn)
  193. {
  194. if (m_bChannelMenu)
  195. CancelMenu();
  196. else
  197. PopupChannelMenu();
  198. }
  199. else if (uParam == (unsigned int)(KWndWindow*)&m_ColorBtn)
  200. {
  201. if (m_bColorMenu)
  202. CancelMenu();
  203. else
  204. PopupColorMenu();
  205. }
  206. break;
  207. case WND_N_EDIT_SPECIAL_KEY_DOWN:
  208. if (m_bLocked && nParam == VK_RETURN)
  209. OnSend();
  210. else if (nParam == VK_UP || nParam == VK_DOWN)
  211. InputRecentMsg(nParam == VK_UP);
  212. case WND_M_HOTKEY:
  213. if (uParam == SWITCH_OPEN_CLOSE)
  214. {
  215. if (IsVisible())
  216. OnSend();
  217. else
  218. Show();
  219. }
  220. break;
  221. case WND_N_EDIT_CHANGE:
  222. m_cPreMsgCounter = 0;
  223. break;
  224. case WND_M_MENUITEM_SELECTED:
  225. if (uParam == SEL_COLOR_MENU)
  226. {
  227. if (nParam >= 0 && nParam < m_cNumColor)
  228. m_CurColor.Color_dw = m_ColorList[nParam];
  229. }
  230. else if (uParam == SEL_CHANNEL_MENU && m_pChannelData && nParam >= 0)
  231. {
  232. g_pCoreShell->OperationRequest(GOI_SET_SEND_CHAT_CHANNEL,
  233. (unsigned int)&m_pChannelData[nParam], 0);
  234. }
  235. CancelMenu();
  236. break;
  237. case WM_SYSKEYDOWN:
  238. if (uParam == 'L')
  239. {
  240. SwitchLockMode();
  241. return 1;
  242. }
  243. break;
  244. default:
  245. return KWndImage::WndProc(uMsg, uParam, nParam);
  246. }
  247. return 0;
  248. }
  249. void KUiFastInputMsg::PaintWindow()
  250. {
  251. KWndImage::PaintWindow();
  252. int nWidth, nHeight;
  253. KRUShadow Shadow;
  254. m_ColorBtn.GetSize(&nWidth, &nHeight);
  255. m_ColorBtn.GetAbsolutePos(&Shadow.oPosition.nX, &Shadow.oPosition.nY);
  256. Shadow.oEndPos.nX = Shadow.oPosition.nX + nWidth;
  257. Shadow.oEndPos.nY = Shadow.oPosition.nY + nHeight;
  258. Shadow.Color.Color_dw = m_CurColor.Color_dw;
  259. g_pRepresentShell->DrawPrimitives(1, &Shadow, RU_T_SHADOW, true);
  260. }
  261. void KUiFastInputMsg::PopupColorMenu()
  262. {
  263. CancelMenu();
  264. m_pMenuData = (KPopupMenuData*)malloc(MENU_DATA_SIZE(UI_INPUT_MSG_MAX_COLOR));
  265. if (m_pMenuData == NULL)
  266. return;
  267. int i, j;
  268. m_pMenuData->bHaveHeadTailImg = false;
  269. m_ColorBtn.GetAbsolutePos(&i, &j);
  270. m_pMenuData->nX = i;
  271. m_pMenuData->nY = j;
  272. m_ColorBtn.GetSize(&i, &j);
  273. m_pMenuData->nItemWidth  = i;
  274. m_pMenuData->nItemHeight = j;
  275. m_pMenuData->bZoomSelectedItem = true;
  276. m_pMenuData->nFontSize = 12;
  277. m_pMenuData->nItemTitleIndent = 0;
  278. m_pMenuData->byItemTitleUpSpace = 0;
  279. m_pMenuData->nNumItem = UI_INPUT_MSG_MAX_COLOR;
  280. m_pMenuData->nSelectedItem = -1;
  281. m_pMenuData->uBorderLineColor = 0xff000000;
  282. m_pMenuData->uSelItemBgColor = 0;
  283. m_pMenuData->uSelTextColor = 0;
  284. m_pMenuData->uTextColor = 0;
  285. for (i = 0; i < UI_INPUT_MSG_MAX_COLOR; i++)
  286. {
  287. m_pMenuData->Items[i].szTitle[0] = 0;
  288. m_pMenuData->Items[i].uBgColor = m_ColorList[i];
  289. }
  290. KPopupMenu::Popup(m_pMenuData, (KWndWindow*)this, SEL_COLOR_MENU);
  291. m_bColorMenu = true;
  292. }
  293. void KUiFastInputMsg::PopupChannelMenu()
  294. {
  295. CancelMenu();
  296. int nCount = g_pCoreShell->GetGameData(GDI_CHAT_SEND_CHANNEL_LIST, 0, 0);
  297. if (nCount <= 0)
  298. return;
  299. m_pChannelData = (KUiChatChannel*)malloc(sizeof(KUiChatChannel) * nCount);
  300. if (m_pChannelData == NULL)
  301. return;
  302. nCount = g_pCoreShell->GetGameData(GDI_CHAT_SEND_CHANNEL_LIST,
  303. (unsigned int)m_pChannelData, nCount);
  304. _ASSERT(nCount > 0);
  305. m_pMenuData = (KPopupMenuData*)malloc(MENU_DATA_SIZE(nCount));
  306. if (m_pMenuData == NULL)
  307. {
  308. free(m_pChannelData);
  309. m_pChannelData = NULL;
  310. return;
  311. }
  312. KPopupMenu::InitMenuData(m_pMenuData, nCount);
  313. int i, j;
  314. m_ChannelBtn.GetAbsolutePos(&i, &j);
  315. m_pMenuData->nX = i;
  316. m_pMenuData->nY = j;
  317. for (i = 0; i < nCount; i++)
  318. strcpy(m_pMenuData->Items[i].szTitle, m_pChannelData[i].cTitle);
  319. KPopupMenu::Popup(m_pMenuData, (KWndWindow*)this, SEL_CHANNEL_MENU);
  320. m_bChannelMenu = true;
  321. }
  322. void KUiFastInputMsg::CancelMenu()
  323. {
  324. KPopupMenu::Cancel();
  325. if (m_pMenuData)
  326. {
  327. free(m_pMenuData);
  328. m_pMenuData = NULL;
  329. }
  330. m_bChannelMenu = false;
  331. m_bColorMenu = false;
  332. if (m_pChannelData)
  333. {
  334. free(m_pChannelData);
  335. m_pChannelData = NULL;
  336. }
  337. }
  338. void KUiFastInputMsg::OnSend()
  339. {
  340. if (g_pCoreShell)
  341. {
  342. char Buffer[512];
  343. KUiMsgParam Param;
  344. Param.nMsgLength = m_InputEdit.GetText(Buffer, sizeof(Buffer));
  345. if (Param.nMsgLength)
  346. {
  347. memcpy(m_RecentMsg[m_cLatestMsgIndex], Buffer, Param.nMsgLength);
  348. m_RecentMsg[m_cLatestMsgIndex][Param.nMsgLength] = 0;
  349. m_cLatestMsgIndex = (m_cLatestMsgIndex + 1) % MAX_RECENT_MSG_COUNT;
  350. m_cPreMsgCounter = 0;
  351. if (TextMsgFilter(Buffer, Param.nMsgLength, Param.eGenre) == false)
  352. {
  353. strcpy(Param.szName, "自己");
  354. Param.cChatPrefixLen = 4;
  355. Param.cChatPrefix[0] = KTC_COLOR;
  356. Param.cChatPrefix[1] = m_CurColor.Color_b.r;
  357. Param.cChatPrefix[2] = m_CurColor.Color_b.g;
  358. Param.cChatPrefix[3] = m_CurColor.Color_b.b;
  359. if (Param.eGenre == MSG_G_CHAT)
  360. {
  361. Param.nMsgLength = TEncodeText(Buffer, Param.nMsgLength);
  362. KUiMsgCentrePad::MessageArrival((const char*)Buffer, &Param);
  363. }
  364. g_pCoreShell->OperationRequest(GOI_SEND_MSG, (unsigned int)Buffer, (int)&Param);
  365. }
  366. }
  367. }
  368. m_InputEdit.SetText("");
  369. if (m_bLocked == false)
  370. Hide();
  371. }
  372. void KUiFastInputMsg::SwitchLockMode()
  373. {
  374. m_bLocked = !m_bLocked;
  375. if (m_bLocked)
  376. Wnd_UnRegisterHotKey(this, SWITCH_OPEN_CLOSE);
  377. else
  378. Wnd_RegisterHotKey(this, SWITCH_OPEN_CLOSE, VK_RETURN, 0);
  379. }
  380. void KUiFastInputMsg::InputRecentMsg(bool bPrior)
  381. {
  382. int nCounter;
  383. if (bPrior)
  384. nCounter = m_cPreMsgCounter - 1;
  385. else
  386. nCounter = m_cPreMsgCounter + 1;
  387. if (nCounter < 0 && nCounter >= - MAX_RECENT_MSG_COUNT)
  388. {
  389. int nIndex = m_cLatestMsgIndex + nCounter;
  390. if (nIndex < 0)
  391. nIndex += 8;
  392. if (m_RecentMsg[nIndex][0])
  393. {
  394. m_InputEdit.SetText(m_RecentMsg[nIndex]);
  395. m_cPreMsgCounter = nCounter;
  396. }
  397. }
  398. else if (nCounter >= 0)
  399. {
  400. m_InputEdit.SetText("");
  401. m_cPreMsgCounter = 0;
  402. }
  403. }
  404. void KUiFastInputMsg::Clear()
  405. {
  406. if (m_pSelf)
  407. {
  408. m_pSelf->m_cLatestMsgIndex = 0;
  409. m_pSelf->m_cPreMsgCounter = 0;
  410. for(int i = 0; i < MAX_RECENT_MSG_COUNT; i++)
  411. m_pSelf->m_RecentMsg[i][0] = 0;
  412. }
  413. }