hgegui.cpp
上传用户:maxiaolivb
上传日期:2022-06-07
资源大小:915k
文件大小:6k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.5
  3. ** Copyright (C) 2003-2004, Relish Games
  4. ** hge.relishgames.com
  5. **
  6. ** hgeGUI helper class implementation
  7. */
  8. #include "....includehgegui.h"
  9. HGE *hgeGUI::hge=0;
  10. HGE *hgeGUIObject::hge=0;
  11. hgeGUI::hgeGUI()
  12. {
  13. hge=hgeCreate(HGE_VERSION);
  14. ctrls=0;
  15. ctrlLock=0;
  16. ctrlFocus=0;
  17. ctrlOver=0;
  18. navmode=HGEGUI_NONAVKEYS;
  19. bLPressed=bLLastPressed=false;
  20. bRPressed=bRLastPressed=false;
  21. nWheel=0;
  22. mx=my=0.0f;
  23. nEnterLeave=0;
  24. sprCursor=0;
  25. }
  26. hgeGUI::~hgeGUI()
  27. {
  28. hgeGUIObject *ctrl=ctrls, *nextctrl;
  29. while(ctrl)
  30. {
  31. nextctrl=ctrl->next;
  32. delete ctrl;
  33. ctrl=nextctrl;
  34. }
  35. hge->Release();
  36. }
  37. void hgeGUI::AddCtrl(hgeGUIObject *ctrl)
  38. {
  39. hgeGUIObject *last=ctrls;
  40. ctrl->gui=this;
  41. if(!ctrls)
  42. {
  43. ctrls=ctrl;
  44. ctrl->prev=0;
  45. ctrl->next=0;
  46. }
  47. else
  48. {
  49. while(last->next) last=last->next;
  50. last->next=ctrl;
  51. ctrl->prev=last;
  52. ctrl->next=0;
  53. }
  54. }
  55. void hgeGUI::DelCtrl(int id)
  56. {
  57. hgeGUIObject *ctrl=ctrls;
  58. while(ctrl)
  59. {
  60. if(ctrl->id == id)
  61. {
  62. if(ctrl->prev) ctrl->prev->next = ctrl->next;
  63. else ctrls = ctrl->next;
  64. if(ctrl->next) ctrl->next->prev = ctrl->prev;
  65. delete ctrl;
  66. return;
  67. }
  68. ctrl=ctrl->next;
  69. }
  70. }
  71. hgeGUIObject* hgeGUI::GetCtrl(int id) const
  72. {
  73. hgeGUIObject *ctrl=ctrls;
  74. while(ctrl)
  75. {
  76. if(ctrl->id == id) return ctrl;
  77. ctrl=ctrl->next;
  78. }
  79. return NULL;
  80. }
  81. void hgeGUI::MoveCtrl(int id, float x, float y)
  82. {
  83. hgeGUIObject *ctrl=GetCtrl(id);
  84. ctrl->rect.x2=x + (ctrl->rect.x2 - ctrl->rect.x1);
  85. ctrl->rect.y2=y + (ctrl->rect.y2 - ctrl->rect.y1);
  86. ctrl->rect.x1=x;
  87. ctrl->rect.y1=y;
  88. }
  89. void hgeGUI::ShowCtrl(int id, bool bVisible)
  90. {
  91. GetCtrl(id)->bVisible=bVisible;
  92. }
  93. void hgeGUI::EnableCtrl(int id, bool bEnabled)
  94. {
  95. GetCtrl(id)->bEnabled=bEnabled;
  96. }
  97. void hgeGUI::SetNavMode(int mode)
  98. {
  99. navmode=mode;
  100. }
  101. void hgeGUI::SetCursor(hgeSprite *spr)
  102. {
  103. sprCursor=spr;
  104. }
  105. void hgeGUI::SetFocus(int id)
  106. {
  107. hgeGUIObject *ctrlNewFocus=GetCtrl(id);
  108. if(ctrlNewFocus==ctrlFocus) return;
  109. if(!ctrlNewFocus)
  110. {
  111. if(ctrlFocus) ctrlFocus->Focus(false);
  112. ctrlFocus=0;
  113. }
  114. else if(!ctrlNewFocus->bStatic && ctrlNewFocus->bVisible && ctrlNewFocus->bEnabled)
  115. {
  116. if(ctrlFocus) ctrlFocus->Focus(false);
  117. if(ctrlNewFocus) ctrlNewFocus->Focus(true);
  118. ctrlFocus=ctrlNewFocus;
  119. }
  120. }
  121. int hgeGUI::GetFocus() const
  122. {
  123. if(ctrlFocus) return ctrlFocus->id;
  124. else return 0;
  125. }
  126. void hgeGUI::Enter()
  127. {
  128. hgeGUIObject *ctrl=ctrls;
  129. while(ctrl)
  130. {
  131. ctrl->Enter();
  132. ctrl=ctrl->next;
  133. }
  134. nEnterLeave=2;
  135. }
  136. void hgeGUI::Leave()
  137. {
  138. hgeGUIObject *ctrl=ctrls;
  139. while(ctrl)
  140. {
  141. ctrl->Leave();
  142. ctrl=ctrl->next;
  143. }
  144. ctrlFocus=0;
  145. ctrlOver=0;
  146. ctrlLock=0;
  147. nEnterLeave=1;
  148. }
  149. void hgeGUI::Render()
  150. {
  151. hgeGUIObject *ctrl=ctrls;
  152. while(ctrl)
  153. {
  154. if(ctrl->bVisible) ctrl->Render();
  155. ctrl=ctrl->next;
  156. }
  157. if(hge->Input_IsMouseOver() && sprCursor) sprCursor->Render(mx,my);
  158. }
  159. int hgeGUI::Update(float dt)
  160. {
  161. bool bDone;
  162. int key;
  163. hgeGUIObject *ctrl;
  164. // Update the mouse variables
  165. hge->Input_GetMousePos(&mx, &my);
  166. bLLastPressed=bLPressed;
  167. bLPressed=hge->Input_GetKeyState(VK_LBUTTON);
  168. bRLastPressed=bRPressed;
  169. bRPressed=hge->Input_GetKeyState(VK_RBUTTON);
  170. nWheel=hge->Input_GetMouseWheel();
  171. // Update all controls
  172. ctrl=ctrls;
  173. while(ctrl)
  174. {
  175. ctrl->Update(dt);
  176. ctrl=ctrl->next;
  177. }
  178. // Handle Enter/Leave
  179. if(nEnterLeave)
  180. {
  181. ctrl=ctrls; bDone=true;
  182. while(ctrl)
  183. {
  184. if(!ctrl->IsDone()) { bDone=false; break; }
  185. ctrl=ctrl->next;
  186. }
  187. if(!bDone) return 0;
  188. else
  189. {
  190. if(nEnterLeave==1) return -1;
  191. else nEnterLeave=0;
  192. }
  193. }
  194. // Handle keys
  195. key=hge->Input_GetKey();
  196. if(((navmode & HGEGUI_LEFTRIGHT) && key==HGEK_LEFT) ||
  197. ((navmode & HGEGUI_UPDOWN) && key==HGEK_UP))
  198. {
  199. ctrl=ctrlFocus;
  200. if(!ctrl)
  201. {
  202. ctrl=ctrls;
  203. if(!ctrl) return 0;
  204. }
  205. do {
  206. ctrl=ctrl->prev;
  207. if(!ctrl && ((navmode & HGEGUI_CYCLED) || !ctrlFocus))
  208. {
  209. ctrl=ctrls;
  210. while(ctrl->next) ctrl=ctrl->next;
  211. }
  212. if(!ctrl || ctrl==ctrlFocus) break;
  213. } while(ctrl->bStatic==true || ctrl->bVisible==false || ctrl->bEnabled==false);
  214. if(ctrl && ctrl!=ctrlFocus)
  215. {
  216. if(ctrlFocus) ctrlFocus->Focus(false);
  217. if(ctrl) ctrl->Focus(true);
  218. ctrlFocus=ctrl;
  219. }
  220. }
  221. else if(((navmode & HGEGUI_LEFTRIGHT) && key==HGEK_RIGHT) ||
  222. ((navmode & HGEGUI_UPDOWN) && key==HGEK_DOWN))
  223. {
  224. ctrl=ctrlFocus;
  225. if(!ctrl)
  226. {
  227. ctrl=ctrls;
  228. if(!ctrl) return 0;
  229. while(ctrl->next) ctrl=ctrl->next;
  230. }
  231. do {
  232. ctrl=ctrl->next;
  233. if(!ctrl && ((navmode & HGEGUI_CYCLED) || !ctrlFocus)) ctrl=ctrls;
  234. if(!ctrl || ctrl==ctrlFocus) break;
  235. } while(ctrl->bStatic==true || ctrl->bVisible==false || ctrl->bEnabled==false);
  236. if(ctrl && ctrl!=ctrlFocus)
  237. {
  238. if(ctrlFocus) ctrlFocus->Focus(false);
  239. if(ctrl) ctrl->Focus(true);
  240. ctrlFocus=ctrl;
  241. }
  242. }
  243. else if(ctrlFocus && key && key!=HGEK_LBUTTON && key!=HGEK_RBUTTON)
  244. {
  245. if(ctrlFocus->KeyClick(key, hge->Input_GetChar())) return ctrlFocus->id;
  246. }
  247. // Handle mouse
  248. if(ctrlLock)
  249. {
  250. ctrl=ctrlLock;
  251. if(!bLPressed && !bRPressed) ctrlLock=0;
  252. if(ProcessCtrl(ctrl)) return ctrl->id;
  253. }
  254. else
  255. {
  256. // Find last (topmost) control
  257. ctrl=ctrls;
  258. if(ctrl)
  259. while(ctrl->next) ctrl=ctrl->next;
  260. while(ctrl)
  261. {
  262. if(ctrl->rect.TestPoint(mx,my) && ctrl->bEnabled)
  263. {
  264. if(ctrlOver != ctrl)
  265. {
  266. if(ctrlOver) ctrlOver->MouseOver(false);
  267. ctrl->MouseOver(true);
  268. ctrlOver=ctrl;
  269. }
  270. if(ProcessCtrl(ctrl)) return ctrl->id;
  271. else return 0;
  272. }
  273. ctrl=ctrl->prev;
  274. }
  275. if(ctrlOver) {ctrlOver->MouseOver(false); ctrlOver=0;}
  276. }
  277. return 0;
  278. }
  279. bool hgeGUI::ProcessCtrl(hgeGUIObject *ctrl)
  280. {
  281. bool bResult=false;
  282. if(!bLLastPressed && bLPressed) { ctrlLock=ctrl;SetFocus(ctrl->id);bResult=bResult || ctrl->MouseLButton(true); }
  283. if(!bRLastPressed && bRPressed) { ctrlLock=ctrl;SetFocus(ctrl->id);bResult=bResult || ctrl->MouseRButton(true); }
  284. if(bLLastPressed && !bLPressed) { bResult=bResult || ctrl->MouseLButton(false); }
  285. if(bRLastPressed && !bRPressed) { bResult=bResult || ctrl->MouseRButton(false); }
  286. if(nWheel) bResult=bResult || ctrl->MouseWheel(nWheel);
  287. bResult=bResult || ctrl->MouseMove(mx-ctrl->rect.x1,my-ctrl->rect.y1);
  288. return bResult;
  289. }