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

模拟服务器

开发平台:

C/C++

  1. /*****************************************************************************************
  2. // 界面--储物箱界面
  3. // Copyright : Kingsoft 2003
  4. // Author :   Wooy(Wu yue)
  5. // CreateTime: 2003-4-21
  6. *****************************************************************************************/
  7. #include "KWin32.h"
  8. #include "KIniFile.h"
  9. #include "../elem/wnds.h"
  10. #include "UiStoreBox.h"
  11. #include "UiGetMoney.h"
  12. #include "UiItem.h"
  13. #include "../../../core/src/coreshell.h"
  14. #include "../../../core/src/GameDataDef.h"
  15. #include "../UiSoundSetting.h"
  16. #include "../UiBase.h"
  17. #include <crtdbg.h>
  18. extern iCoreShell* g_pCoreShell;
  19. #define SCHEME_INI_ITEM "储物箱.ini"
  20. KUiStoreBox* KUiStoreBox::m_pSelf = NULL;
  21. enum WAIT_OTHER_WND_OPER_PARAM
  22. {
  23. UISTOREBOX_WAIT_GETMONEY,
  24. };
  25. //--------------------------------------------------------------------------
  26. // 功能:如果窗口正被显示,则返回实例指针
  27. //--------------------------------------------------------------------------
  28. KUiStoreBox* KUiStoreBox::GetIfVisible()
  29. {
  30. if (m_pSelf && m_pSelf->IsVisible())
  31. return m_pSelf;
  32. return NULL;
  33. }
  34. //--------------------------------------------------------------------------
  35. // 功能:打开窗口,返回唯一的一个类对象实例
  36. //--------------------------------------------------------------------------
  37. KUiStoreBox* KUiStoreBox::OpenWindow()
  38. {
  39. if (m_pSelf == NULL)
  40. {
  41. m_pSelf = new KUiStoreBox;
  42. if (m_pSelf)
  43. m_pSelf->Initialize();
  44. }
  45. if (m_pSelf)
  46. {
  47. if (KUiItem::GetIfVisible() == NULL)
  48. KUiItem::OpenWindow();
  49. else
  50. UiSoundPlay(UI_SI_WND_OPENCLOSE);
  51. m_pSelf->UpdateData();
  52. m_pSelf->BringToTop();
  53. m_pSelf->Show();
  54. Wnd_GameSpaceHandleInput(false);
  55. }
  56. return m_pSelf;
  57. }
  58. //--------------------------------------------------------------------------
  59. // 功能:关闭窗口
  60. //--------------------------------------------------------------------------
  61. void KUiStoreBox::CloseWindow()
  62. {
  63. if (m_pSelf)
  64. {
  65. Wnd_GameSpaceHandleInput(true);
  66. m_pSelf->Destroy();
  67. m_pSelf = NULL;
  68. }
  69. }
  70. // -------------------------------------------------------------------------
  71. // 功能 : 初始化
  72. // -------------------------------------------------------------------------
  73. void KUiStoreBox::Initialize()
  74. {
  75. AddChild(&m_Money);
  76. AddChild(&m_GetMoneyBtn);
  77. AddChild(&m_CloseBtn);
  78. AddChild(&m_ItemBox);
  79. m_ItemBox.SetContainerId((int)UOC_STORE_BOX);
  80. char Scheme[256];
  81. g_UiBase.GetCurSchemePath(Scheme, 256);
  82. LoadScheme(Scheme);
  83. m_nMoney = 0;
  84. Wnd_AddWindow(this);
  85. }
  86. //--------------------------------------------------------------------------
  87. // 功能:构造函数
  88. //--------------------------------------------------------------------------
  89. void KUiStoreBox::UpdateData()
  90. {
  91. m_ItemBox.Clear();
  92. m_nMoney = 0;
  93. m_Money.SetText("0");
  94. KUiObjAtRegion* pObjs = NULL;
  95. int nCount = g_pCoreShell->GetGameData(GDI_ITEM_IN_STORE_BOX, 0, 0);
  96. if (nCount == 0)
  97. return;
  98. if (pObjs = (KUiObjAtRegion*)malloc(sizeof(KUiObjAtRegion) * nCount))
  99. {
  100. g_pCoreShell->GetGameData(GDI_ITEM_IN_STORE_BOX, (unsigned int)pObjs, nCount);//单线程执行,nCount值不变
  101. for (int i = 0; i < nCount; i++)
  102. UpdateItem(&pObjs[i], true);
  103. free(pObjs);
  104. pObjs = NULL;
  105. }
  106. }
  107. // -------------------------------------------------------------------------
  108. // 功能 : 物品变化更新
  109. // -------------------------------------------------------------------------
  110. void KUiStoreBox::UpdateItem(KUiObjAtRegion* pItem, int bAdd)
  111. {
  112. if (pItem)
  113. {
  114. UiSoundPlay(UI_SI_PICKPUT_ITEM);
  115. if (pItem->Obj.uGenre != CGOG_MONEY)
  116. {
  117. KUiDraggedObject Obj;
  118. Obj.uGenre = pItem->Obj.uGenre;
  119. Obj.uId = pItem->Obj.uId;
  120. Obj.DataX = pItem->Region.h;
  121. Obj.DataY = pItem->Region.v;
  122. Obj.DataW = pItem->Region.Width;
  123. Obj.DataH = pItem->Region.Height;
  124. if (bAdd)
  125. m_ItemBox.AddObject(&Obj, 1);
  126. else
  127. m_ItemBox.RemoveObject(&Obj);
  128. }
  129. else
  130. {
  131. m_nMoney = pItem->Obj.uId;
  132. m_Money.SetIntText(m_nMoney);
  133. }
  134. }
  135. else
  136. UpdateData();
  137. }
  138. // -------------------------------------------------------------------------
  139. // 功能 : 载入界面方案
  140. // -------------------------------------------------------------------------
  141. void KUiStoreBox::LoadScheme(const char* pScheme)
  142. {
  143. char Buff[128];
  144. KIniFile Ini;
  145. sprintf(Buff, "%s\%s", pScheme, SCHEME_INI_ITEM);
  146. if (m_pSelf && Ini.Load(Buff))
  147. {
  148. m_pSelf->Init(&Ini, "Main");
  149. m_pSelf->m_Money.Init(&Ini, "Money");
  150. m_pSelf->m_GetMoneyBtn.Init(&Ini, "GetMoneyBtn");
  151. m_pSelf->m_CloseBtn.Init(&Ini, "CloseBtn");
  152. m_pSelf->m_ItemBox.Init(&Ini, "ItemBox");
  153. m_pSelf->m_ItemBox.EnableTracePutPos(true);
  154. }
  155. }
  156. // -------------------------------------------------------------------------
  157. // 功能 : 窗口函数
  158. // -------------------------------------------------------------------------
  159. int KUiStoreBox::WndProc(unsigned int uMsg, unsigned int uParam, int nParam)
  160. {
  161. switch(uMsg)
  162. {
  163. case WND_N_ITEM_PICKDROP:
  164. OnItemPickDrop((ITEM_PICKDROP_PLACE*)uParam, (ITEM_PICKDROP_PLACE*)nParam);
  165. break;
  166. case WND_N_BUTTON_CLICK:
  167. if (uParam == (unsigned int)(KWndWindow*)&m_CloseBtn)
  168. CloseWindow();
  169. else if (uParam == (unsigned int)(KWndWindow*)&m_GetMoneyBtn)
  170. {
  171. KUiGetMoney::OpenWindow(0, m_nMoney, this, UISTOREBOX_WAIT_GETMONEY, &m_Money);
  172. }
  173. break;
  174. case WND_M_OTHER_WORK_RESULT:
  175. if (uParam == UISTOREBOX_WAIT_GETMONEY)
  176. OnGetMoney(nParam);
  177. break;
  178. default:
  179. return KWndShowAnimate::WndProc(uMsg, uParam, nParam);
  180. }
  181. return 0;
  182. }
  183. void KUiStoreBox::OnGetMoney(int nMoney)
  184. {
  185. if (nMoney > 0)
  186. {
  187. g_pCoreShell->OperationRequest(GOI_MONEY_INOUT_STORE_BOX,
  188. false, nMoney);
  189. }
  190. }
  191. void KUiStoreBox::OnItemPickDrop(ITEM_PICKDROP_PLACE* pPickPos, ITEM_PICKDROP_PLACE* pDropPos)
  192. {
  193. if (g_UiBase.GetStatus() != UIS_S_IDLE)
  194. return;
  195. KUiObjAtContRegion Pick, Drop;
  196. KUiDraggedObject Obj;
  197. if (pPickPos)
  198. {
  199. _ASSERT(pPickPos->pWnd);
  200. ((KWndObjectMatrix*)(pPickPos->pWnd))->GetObject(
  201. Obj, pPickPos->h, pPickPos->v);
  202. Pick.Obj.uGenre = Obj.uGenre;
  203. Pick.Obj.uId = Obj.uId;
  204. Pick.Region.Width = Obj.DataW;
  205. Pick.Region.Height = Obj.DataH;
  206. Pick.Region.h = Obj.DataX;
  207. Pick.Region.v = Obj.DataY;
  208. Pick.eContainer = UOC_STORE_BOX;
  209. }
  210. if (pDropPos)
  211. {
  212. Wnd_GetDragObj(&Obj);
  213. Drop.Obj.uGenre = Obj.uGenre;
  214. Drop.Obj.uId = Obj.uId;
  215. Drop.Region.Width = Obj.DataW;
  216. Drop.Region.Height = Obj.DataH;
  217. Drop.Region.h = pDropPos->h;
  218. Drop.Region.v = pDropPos->v;
  219. Drop.eContainer = UOC_STORE_BOX;
  220. }
  221. g_pCoreShell->OperationRequest(GOI_SWITCH_OBJECT,
  222. pPickPos ? (unsigned int)&Pick : 0,
  223. pDropPos ? (int)&Drop : 0);
  224. }