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

模拟服务器

开发平台:

C/C++

  1. // -------------------------------------------------------------------------
  2. // 文件名 : UiTrade.cpp
  3. // 创建者 : 吕桂华, Wooy(Wu yue)
  4. // 创建时间 : 2002-9-16 11:58:57
  5. // 功能描述 :
  6. // -------------------------------------------------------------------------
  7. #include "KWin32.h"
  8. #include "KIniFile.h"
  9. #include "../Elem/WndMessage.h"
  10. #include "../elem/wnds.h"
  11. #include "uitrade.h"
  12. #include "../UiSoundSetting.h"
  13. #include "../../../core/src/CoreObjGenreDef.h"
  14. #include "../../../core/src/coreshell.h"
  15. #include "../UiBase.h"
  16. #include "UiInformation.h"
  17. #include <crtdbg.h>
  18. extern iCoreShell* g_pCoreShell;
  19. #define SCHEME_INI_TRADE "玩家间交易.ini"
  20. #define STR_WAIT_TRADING "等待交易执行"
  21. #define STR_OTHER_OK "对方已锁定"
  22. #define STR_OTHER_NOT_OK "等待对方锁定"
  23. KUiTrade* KUiTrade::m_pSelf = NULL;
  24. #define SEND_MONEY_CHANGE_MSG_DELAY 16
  25. //--------------------------------------------------------------------------
  26. // 功能:打开窗口,返回唯一的一个类对象实例
  27. //--------------------------------------------------------------------------
  28. KUiTrade* KUiTrade::OpenWindow(KUiPlayerItem* pOther)
  29. {
  30. if (g_UiBase.GetStatus() != UIS_S_IDLE)
  31. return NULL;
  32. if (m_pSelf == NULL)
  33. {
  34. m_pSelf = new KUiTrade;
  35. if (m_pSelf)
  36. m_pSelf->Initialize();
  37. }
  38. if (m_pSelf)
  39. {
  40. UiSoundPlay(UI_SI_WND_OPENCLOSE);
  41. if (pOther)
  42. m_pSelf->m_OtherData = *pOther;
  43. m_pSelf->m_OtherName.SetText(pOther->Name);
  44. m_pSelf->UpdateTakewithItems();
  45. m_pSelf->UpdateOperData();
  46. m_pSelf->BringToTop();
  47. m_pSelf->Show();
  48. g_UiBase.SetStatus(UIS_S_TRADE_PLAYER);
  49. }
  50. return m_pSelf;
  51. }
  52. //--------------------------------------------------------------------------
  53. // 功能:如果窗口正被显示,则返回实例指针
  54. //--------------------------------------------------------------------------
  55. KUiTrade* KUiTrade::GetIfVisible()
  56. {
  57. return m_pSelf;
  58. }
  59. //--------------------------------------------------------------------------
  60. // 功能:关闭窗口,同时可以选择是否删除对象实例
  61. //--------------------------------------------------------------------------
  62. void KUiTrade::CloseWindow()
  63. {
  64. if (m_pSelf)
  65. {
  66. g_UiBase.SetStatus(UIS_S_IDLE);
  67. delete(m_pSelf);
  68. m_pSelf = NULL;
  69. }
  70. }
  71. KUiTrade::KUiTrade()
  72. {
  73. m_nSelfTradeMoney = 0;
  74. m_bAdjustingMoney = 0;
  75. m_nTakewithMoney  = 0;
  76. memset(&m_OtherData, 0, sizeof(KUiPlayerItem));
  77. m_uWaitTradeMsgColor = 0xffffff;
  78. m_uOtherLockMsgColor = 0xffffff;
  79. m_uOtherNotLockMsgColor =0xffffff;
  80. }
  81. KUiTrade::~KUiTrade()
  82. {
  83. }
  84. //初始化
  85. void KUiTrade::Initialize()
  86. {
  87. AddChild(&m_TakewithMoney);
  88. AddChild(&m_TakewithItemsBox);
  89. AddChild(&m_SelfItemsBox);
  90. AddChild(&m_SelfMoney);
  91. AddChild(&m_Ok);
  92. AddChild(&m_Cancel);
  93. AddChild(&m_Trade);
  94. AddChild(&m_AddMoney);
  95. AddChild(&m_ReduceMoney);
  96. AddChild(&m_OtherName);
  97. AddChild(&m_OtherItemsBox);
  98. AddChild(&m_OtherMoney);
  99. AddChild(&m_Info);
  100. m_TakewithItemsBox.SetContainerId((int)UOC_ITEM_TAKE_WITH);
  101. m_SelfItemsBox.SetContainerId((int)UOC_TO_BE_TRADE);
  102. m_OtherItemsBox.SetContainerId((int)UOC_OTHER_TO_BE_TRADE);
  103. m_SelfMoney.SetIntText(0);
  104. m_OtherMoney.SetIntText(0);
  105. m_TakewithMoney.SetIntText(0);
  106. m_Info.SetText("", 0);
  107. m_OtherName.SetText("", 0);
  108. char Scheme[256];
  109. g_UiBase.GetCurSchemePath(Scheme, 256);
  110. LoadScheme(Scheme);
  111. m_Trade.Enable(false);
  112. m_OtherItemsBox.EnablePickPut(false);
  113. Wnd_AddWindow(this);
  114. }
  115. //--------------------------------------------------------------------------
  116. // 功能:载入界面方案
  117. //--------------------------------------------------------------------------
  118. void KUiTrade::LoadScheme(const char* pScheme)
  119. {
  120. if (m_pSelf)
  121. {
  122. char Buff[128];
  123. KIniFile Ini;
  124. sprintf(Buff, "%s\"SCHEME_INI_TRADE, pScheme);
  125. if (Ini.Load(Buff))
  126. {
  127. m_pSelf->LoadScheme(&Ini);
  128. }
  129. }
  130. }
  131. //--------------------------------------------------------------------------
  132. // 功能:载入界面方案
  133. //--------------------------------------------------------------------------
  134. void KUiTrade::LoadScheme(KIniFile* pIni)
  135. {
  136. //_ASSERT(pIni);
  137. Init(pIni, "Main");
  138. m_TakewithItemsBox.Init(pIni, "TakewithItemsBox");
  139. m_TakewithMoney .Init(pIni, "TakewithMoney");
  140. m_SelfItemsBox  .Init(pIni, "SelfItemsBox");
  141. m_SelfMoney .Init(pIni, "SelfMoney");
  142. m_Ok .Init(pIni, "OkBtn");
  143. m_Cancel .Init(pIni, "CancelBtn");
  144. m_Trade .Init(pIni, "TradeBtn");
  145. m_AddMoney .Init(pIni, "AddMoney");
  146. m_ReduceMoney .Init(pIni, "ReduceMoney");
  147. m_OtherName .Init(pIni, "OtherName");
  148. m_OtherItemsBox .Init(pIni, "OtherItemsBox");
  149. m_OtherMoney .Init(pIni, "OtherMoney");
  150. m_Info .Init(pIni, "InfoText");
  151. m_OtherName .SetText(m_OtherData.Name);
  152. char szColor[16];
  153. pIni->GetString("InfoText", "WaitTradeMsgColor", "255,255,255", szColor, sizeof(szColor));
  154. m_uWaitTradeMsgColor = (::GetColor(szColor) & 0xffffff);
  155. pIni->GetString("InfoText", "LockMsgColor", "255,255,255", szColor, sizeof(szColor));
  156. m_uOtherLockMsgColor = (::GetColor(szColor) & 0xffffff);
  157. pIni->GetString("InfoText", "UnlockMsgColor", "255,255,255", szColor, sizeof(szColor));
  158. m_uOtherNotLockMsgColor = (::GetColor(szColor) & 0xffffff);
  159. m_SelfItemsBox.EnableTracePutPos(true);
  160. m_TakewithItemsBox.EnableTracePutPos(true);
  161. }
  162. void KUiTrade::UpdateTakewithItems()
  163. {
  164. m_TakewithItemsBox.Clear();
  165. m_nTakewithMoney = g_pCoreShell->GetGameData(GDI_PLAYER_HOLD_MONEY, 0, 0);
  166. m_TakewithMoney.SetIntText(m_nTakewithMoney);
  167. KUiObjAtRegion* pObjs = NULL;
  168. int nCount = g_pCoreShell->GetGameData(GDI_ITEM_TAKEN_WITH, 0, 0);
  169. if (nCount == 0)
  170. return;
  171. if (pObjs = (KUiObjAtRegion*)malloc(sizeof(KUiObjAtRegion) * nCount))
  172. {
  173. g_pCoreShell->GetGameData(GDI_ITEM_TAKEN_WITH, (unsigned int)pObjs, nCount);//单线程执行,nCount值不变
  174. for (int i = 0; i < nCount; i++)
  175. {
  176. KUiDraggedObject no;
  177. no.uGenre = pObjs[i].Obj.uGenre;
  178. no.uId = pObjs[i].Obj.uId;
  179. no.DataX = pObjs[i].Region.h;
  180. no.DataY = pObjs[i].Region.v;
  181. no.DataW = pObjs[i].Region.Width;
  182. no.DataH = pObjs[i].Region.Height;
  183. m_TakewithItemsBox.AddObject(&no, 1);
  184. }
  185. free(pObjs);
  186. pObjs = NULL;
  187. }
  188. }
  189. //对方变更随身物品
  190. void KUiTrade::OnChangedTakewithItem(KUiObjAtRegion* pObj, int bAdd)
  191. {
  192. if (pObj)
  193. {
  194. UiSoundPlay(UI_SI_PICKPUT_ITEM);
  195. KUiDraggedObject Obj;
  196. Obj.uGenre = pObj->Obj.uGenre;
  197. Obj.uId = pObj->Obj.uId;
  198. Obj.DataX = pObj->Region.h;
  199. Obj.DataY = pObj->Region.v;
  200. Obj.DataW = pObj->Region.Width;
  201. Obj.DataH = pObj->Region.Height;
  202. if (bAdd)
  203. m_TakewithItemsBox.AddObject(&Obj, 1);
  204. else
  205. m_TakewithItemsBox.RemoveObject(&Obj);
  206. }
  207. else
  208. UpdateTakewithItems();
  209. }
  210. //变更交易物品
  211. void KUiTrade::OnSelfChangedItem(KUiObjAtRegion* pObj, int bAdd)
  212. {
  213. _ASSERT(pObj);
  214. if (pObj->Obj.uGenre == CGOG_MONEY)
  215. {
  216. m_nSelfTradeMoney = pObj->Obj.uId;
  217. m_SelfMoney.SetIntText(pObj->Obj.uId);
  218. }
  219. else
  220. {
  221. UiSoundPlay(UI_SI_PICKPUT_ITEM);
  222. KUiDraggedObject obj;
  223. obj.uGenre = pObj->Obj.uGenre;
  224. obj.uId = pObj->Obj.uId;
  225. obj.DataX = pObj->Region.h;
  226. obj.DataY = pObj->Region.v;
  227. obj.DataW = pObj->Region.Width;
  228. obj.DataH = pObj->Region.Height;
  229. if (bAdd)
  230. m_SelfItemsBox.AddObject(&obj, 1);
  231. else
  232. m_SelfItemsBox.RemoveObject(&obj);
  233. }
  234. }
  235. // -------------------------------------------------------------------------
  236. // 功能 : 对方变更交易物品
  237. // -------------------------------------------------------------------------
  238. void KUiTrade::OnOppositeChangedItem(KUiObjAtRegion* pObj, int bAdd)
  239. {
  240. _ASSERT(pObj);
  241. if (pObj->Obj.uGenre == CGOG_MONEY)
  242. {
  243. m_OtherMoney.SetIntText(pObj->Obj.uId);
  244. }
  245. else
  246. {
  247. KUiDraggedObject obj;
  248. obj.uGenre = pObj->Obj.uGenre;
  249. obj.uId = pObj->Obj.uId;
  250. obj.DataX = pObj->Region.h;
  251. obj.DataY = pObj->Region.v;
  252. obj.DataW = pObj->Region.Width;
  253. obj.DataH = pObj->Region.Height;
  254. if (bAdd)
  255. m_OtherItemsBox.AddObject(&obj, 1);
  256. else
  257. m_OtherItemsBox.RemoveObject(&obj);
  258. }
  259. }
  260. // -------------------------------------------------------------------------
  261. // 功能 : 对方同意交易
  262. // -------------------------------------------------------------------------
  263. void KUiTrade::UpdateOperData()
  264. {
  265. int bLock = g_pCoreShell->GetGameData(GDI_TRADE_OPER_DATA, UTOD_IS_LOCKED, 0);
  266. m_Ok.CheckButton(bLock);
  267. if (g_pCoreShell->GetGameData(GDI_TRADE_OPER_DATA, UTOD_IS_TRADING, 0))
  268. {
  269. m_Trade.Enable(true);
  270. m_Trade.CheckButton(true);
  271. m_Info.SetText(STR_WAIT_TRADING, strlen(STR_WAIT_TRADING));
  272. m_Info.SetTextColor(m_uWaitTradeMsgColor);
  273. }
  274. else
  275. {
  276. int bOtherLock = g_pCoreShell->GetGameData(GDI_TRADE_OPER_DATA, UTOD_IS_OTHER_LOCKED, 0);
  277. m_Trade.CheckButton(false);
  278. m_Trade.Enable(bLock && bOtherLock);
  279. m_Info.SetText(bOtherLock ? STR_OTHER_OK : STR_OTHER_NOT_OK, -1);
  280. m_Info.SetTextColor(bOtherLock ? m_uOtherLockMsgColor : m_uOtherNotLockMsgColor);
  281. }
  282. m_SelfItemsBox.Enable(!bLock);
  283. m_SelfMoney.Enable(!bLock);
  284. m_TakewithItemsBox.Enable(!bLock);
  285. m_AddMoney.Enable(!bLock);
  286. m_ReduceMoney.Enable(!bLock);
  287. m_SelfMoney.Enable(!bLock);
  288. }
  289. // -------------------------------------------------------------------------
  290. // 功能 : 窗口函数
  291. // -------------------------------------------------------------------------
  292. int KUiTrade::WndProc(unsigned int uMsg, unsigned int uParam, int nParam)
  293. {
  294. switch(uMsg)
  295. {
  296. case WND_N_BUTTON_CLICK:
  297. if (uParam == (unsigned int)(KWndWindow*)&m_Cancel)
  298. OnCancelTrade();
  299. else if (uParam == (unsigned int)(KWndWindow*)&m_Ok)
  300. OnOk(nParam);
  301. else if (uParam == (unsigned int)(KWndWindow*)&m_Trade)
  302. OnTrade();
  303. else if (uParam == (unsigned int)(KWndWindow*)&m_AddMoney)
  304. {
  305. OnAdjustMoney(true);
  306. m_bAdjustingMoney = 1;
  307. }
  308. else if (uParam == (unsigned int)(KWndWindow*)&m_ReduceMoney)
  309. {
  310. OnAdjustMoney(false);
  311. m_bAdjustingMoney = 1;
  312. }
  313. break;
  314. case WND_N_BUTTON_HOLD:
  315. if (uParam == (unsigned int)(KWndWindow*)&m_AddMoney)
  316. {
  317. OnAdjustMoney(true);
  318. m_bAdjustingMoney = 1;
  319. }
  320. else if (uParam == (unsigned int)(KWndWindow*)&m_ReduceMoney)
  321. {
  322. OnAdjustMoney(false);
  323. m_bAdjustingMoney = 1;
  324. }
  325. break;
  326. case WND_N_EDIT_CHANGE:
  327. OnModififyMoney();
  328. break;
  329. case WND_N_ITEM_PICKDROP:
  330. OnItemPickDrop((ITEM_PICKDROP_PLACE*)uParam, (ITEM_PICKDROP_PLACE*)nParam);
  331. default:
  332. return KWndShowAnimate::WndProc(uMsg, uParam, nParam);
  333. }
  334. return 0;
  335. }
  336. void KUiTrade::Breathe()
  337. {
  338. if (m_bAdjustingMoney)
  339. {
  340. if ((++m_bAdjustingMoney) == SEND_MONEY_CHANGE_MSG_DELAY)
  341. OnAdjustedMoney();
  342. }
  343. }
  344. void KUiTrade::OnItemPickDrop(ITEM_PICKDROP_PLACE* pPickPos, ITEM_PICKDROP_PLACE* pDropPos)
  345. {
  346. KUiObjAtContRegion Pick, Drop;
  347. KUiDraggedObject Obj;
  348. if (pPickPos)
  349. {
  350. if (pPickPos->pWnd == (KWndWindow*)&m_SelfItemsBox)
  351. Pick.eContainer = UOC_TO_BE_TRADE;
  352. else if (pPickPos->pWnd == (KWndWindow*)&m_TakewithItemsBox)
  353. Pick.eContainer = UOC_ITEM_TAKE_WITH;
  354. else
  355. return;
  356. ((KWndObjectMatrix*)(pPickPos->pWnd))->GetObject(
  357. Obj, pPickPos->h, pPickPos->v);
  358. Pick.Obj.uGenre = Obj.uGenre;
  359. Pick.Obj.uId = Obj.uId;
  360. Pick.Region.Width = Obj.DataW;
  361. Pick.Region.Height = Obj.DataH;
  362. Pick.Region.h = Obj.DataX;
  363. Pick.Region.v = Obj.DataY;
  364. }
  365. if (pDropPos)
  366. {
  367. Wnd_GetDragObj(&Obj);
  368. Drop.Obj.uGenre = Obj.uGenre;
  369. Drop.Obj.uId = Obj.uId;
  370. Drop.Region.Width = Obj.DataW;
  371. Drop.Region.Height = Obj.DataH;
  372. Drop.Region.h = pDropPos->h;
  373. Drop.Region.v = pDropPos->v;
  374. if (pPickPos)
  375. Drop.eContainer = Pick.eContainer;
  376. else if (pDropPos->pWnd == (KWndWindow*)&m_SelfItemsBox)
  377. Drop.eContainer = UOC_TO_BE_TRADE;
  378. else if (pDropPos->pWnd == (KWndWindow*)&m_TakewithItemsBox)
  379. Drop.eContainer = UOC_ITEM_TAKE_WITH;
  380. else
  381. return;
  382. }
  383. g_pCoreShell->OperationRequest(GOI_SWITCH_OBJECT,
  384. pPickPos ? (unsigned int)&Pick : 0,
  385. pDropPos ? (int)&Drop : 0);
  386. }
  387. void KUiTrade::OnAdjustMoney(bool bAdd)
  388. {
  389. if (bAdd)
  390. {
  391. if (m_nTakewithMoney)
  392. {
  393. m_nTakewithMoney--;
  394. m_nSelfTradeMoney++;
  395. }
  396. }
  397. else if (m_nSelfTradeMoney)
  398. {
  399. m_nSelfTradeMoney--;
  400. m_nTakewithMoney++;
  401. }
  402. m_SelfMoney.SetIntText(m_nSelfTradeMoney);
  403. m_TakewithMoney.SetIntText(m_nTakewithMoney);
  404. }
  405. void KUiTrade::OnAdjustedMoney()
  406. {
  407. OnModififyMoney();
  408. m_bAdjustingMoney = 0;
  409. if (g_pCoreShell)
  410. {
  411. KUiObjAtRegion Obj = { 0 };
  412. Obj.Obj.uGenre = CGOG_MONEY;
  413. Obj.Obj.uId = m_nSelfTradeMoney;
  414. g_pCoreShell->OperationRequest(GOI_TRADE_DESIRE_ITEM,
  415. (unsigned int)&Obj, 0);
  416. }
  417. }
  418. void KUiTrade::OnModififyMoney()
  419. {
  420. m_bAdjustingMoney = 1;
  421. int nNewSelfTradeMoney = m_SelfMoney.GetIntNumber();
  422. if (nNewSelfTradeMoney < 0)
  423. nNewSelfTradeMoney = 0;
  424. if (nNewSelfTradeMoney > m_nTakewithMoney + m_nSelfTradeMoney)
  425. {
  426. nNewSelfTradeMoney = m_nTakewithMoney + m_nSelfTradeMoney;
  427. m_SelfMoney.SetIntText(nNewSelfTradeMoney);
  428. }
  429. m_nTakewithMoney -= nNewSelfTradeMoney - m_nSelfTradeMoney;
  430. m_nSelfTradeMoney = nNewSelfTradeMoney;
  431. m_TakewithMoney.SetIntText(m_nTakewithMoney);
  432. }
  433. //响应点击开始交易
  434. void KUiTrade::OnTrade()
  435. {
  436. g_pCoreShell->OperationRequest(GOI_TRADE, 0, 0);
  437. UpdateOperData();
  438. }
  439. //响应点击有交易意向
  440. void KUiTrade::OnOk(int bChecked)
  441. {
  442. OnAdjustedMoney();
  443. g_pCoreShell->OperationRequest(GOI_TRADE_LOCK, 0, !!bChecked);
  444. UpdateOperData();
  445. }
  446. //清除内容
  447. /*void KUiTrade::Clear()
  448. {
  449. m_SelfItemsBox.Clear();
  450. m_nTakewithMoney += m_nSelfTradeMoney;
  451. m_nSelfTradeMoney = 0;
  452. m_SelfMoney.SetText("0");
  453. m_TakewithMoney.SetIntText(m_nTakewithMoney);
  454. m_OtherItemsBox.Clear();
  455. m_OtherMoney.SetText("0");
  456. m_Info.SetText("");
  457. m_Trade.Enable(false);
  458. UpdateOperData();
  459. }*/
  460. void KUiTrade::OnCancelTrade()
  461. {
  462. g_pCoreShell->OperationRequest(GOI_TRADE_CANCEL, 0, 0);
  463. CloseWindow();
  464. }
  465. // -------------------------------------------------------------------------
  466. // 功能 : 交易结束
  467. // 参数 : bTraded表示是否进行了交易,如果取0值表示对方离开交易(交易被取消)
  468. // -------------------------------------------------------------------------
  469. void KUiTrade::UpdateTradeEnd(int bTraded)
  470. {
  471. CloseWindow();
  472. }