Clone of Akilla's ac2d @ https://github.com/deregtd/AC2D

cMovableWindow.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #pragma once
  2. #include "WindowLib/cWindow.h"
  3. #pragma warning(disable:4244)
  4. class cTitleBar : public CWindow, private RenderEventAbstractor< cTitleBar >, private MouseEventsAbstractor< cTitleBar >
  5. {
  6. public:
  7. cTitleBar()
  8. {
  9. m_bMoving = false;
  10. m_stCaption.SetAnchorRight(true);
  11. m_stCaption.SetSize(GetWidth()-32, 16);
  12. m_stCaption.SetPosition(16,0);
  13. m_stCaption.SetText("Untitled Window");
  14. m_stCaption.SetTextColor(0xFFFFFF);
  15. m_stCaption.SetVisible(true);
  16. AddChild(m_stCaption);
  17. m_pbIcon.SetPosition(0,0);
  18. m_pbIcon.SetSize(16,16);
  19. m_pbIcon.SetVisible(true);
  20. AddChild(m_pbIcon);
  21. m_mbMinimize.SetVisible(true);
  22. m_mbMinimize.SetPosition(GetWidth()-16,0);
  23. AddChild(m_mbMinimize);
  24. AddRenderEventHandler( *(RenderEventAbstractor< cTitleBar > *)this );
  25. AddMouseEventHandler( *(MouseEventsAbstractor< cTitleBar > *)this );
  26. m_pbIcon.AddMouseEventHandler( *(MouseEventsAbstractor< cTitleBar > *)this );
  27. m_stCaption.AddMouseEventHandler( *(MouseEventsAbstractor< cTitleBar > *)this );
  28. }
  29. ~cTitleBar()
  30. {
  31. }
  32. void SetIcon(DWORD dwIcon)
  33. {
  34. m_pbIcon.SetPicture(dwIcon);
  35. }
  36. DWORD GetIcon() const
  37. {
  38. return m_pbIcon.GetPicture();
  39. }
  40. void SetCaption(std::string sCaption)
  41. {
  42. m_stCaption.SetText(sCaption);
  43. }
  44. std::string GetCaption() const
  45. {
  46. return m_stCaption.GetText();
  47. }
  48. void SetTransparency( float fTrans )
  49. {
  50. m_fTrans = fTrans;
  51. }
  52. private:
  53. bool MouseEventsAbstractor< cTitleBar >::OnMouseDown( IWindow & Window, float X, float Y, unsigned long Button )
  54. {
  55. if (Button & 1)
  56. {
  57. //captionbar
  58. m_bMoving = true;
  59. m_fLastX = GetAbsoluteLeft() + X;
  60. m_fLastY = GetAbsoluteTop() + Y;
  61. }
  62. return true;
  63. }
  64. bool MouseEventsAbstractor< cTitleBar >::OnMouseMove( IWindow & Window, float X, float Y, unsigned long Button )
  65. {
  66. if (m_bMoving)
  67. {
  68. float fX = GetAbsoluteLeft() + X, fY = GetAbsoluteTop() + Y;
  69. GetParent()->SetPosition(GetParent()->GetLeft()+((X+GetAbsoluteLeft())-m_fLastX), GetParent()->GetTop()+((Y+GetAbsoluteTop())-m_fLastY));
  70. m_fLastX = fX;
  71. m_fLastY = fY;
  72. }
  73. return true;
  74. }
  75. bool MouseEventsAbstractor< cTitleBar >::OnMouseUp( IWindow & Window, float X, float Y, unsigned long Button )
  76. {
  77. if ((Button & 1) && (m_bMoving))
  78. {
  79. m_bMoving = false;
  80. }
  81. return true;
  82. }
  83. bool MouseEventsAbstractor< cTitleBar >::OnClick( IWindow &Window, float X, float Y, unsigned long Button )
  84. {
  85. return false;
  86. }
  87. bool MouseEventsAbstractor< cTitleBar >::OnDoubleClick( IWindow &Window, float X, float Y, unsigned long Button )
  88. {
  89. return false;
  90. }
  91. bool MouseEventsAbstractor< cTitleBar >::OnMouseWheel( IWindow &Window, float X, float Y, unsigned long Button )
  92. {
  93. return false;
  94. }
  95. bool MouseEventsAbstractor< cTitleBar >::OnMouseEnter( IWindow &Window, float X, float Y, unsigned long Button )
  96. {
  97. return false;
  98. }
  99. bool MouseEventsAbstractor< cTitleBar >::OnMouseExit( IWindow &Window, float X, float Y, unsigned long Button )
  100. {
  101. return false;
  102. }
  103. bool RenderEventAbstractor< cTitleBar >::OnRender( IWindow & Window, double TimeSlice )
  104. {
  105. float iLeft = GetAbsoluteLeft(),
  106. iRight = iLeft + GetWidth(),
  107. iTop = GetAbsoluteTop(),
  108. iBottom = iTop + GetHeight();
  109. //titlebar
  110. glBindTexture(GL_TEXTURE_2D, m_Portal->FindGraphic(0x06001125));
  111. POINTf tpp = m_Portal->GetGraphicEdges(0x06001125);
  112. glBegin(GL_QUADS);
  113. glColor4f(1,1,1,m_fTrans);
  114. glTexCoord2f(0,0);
  115. glVertex2f(iLeft,iTop);
  116. glTexCoord2f(0.53f,0);
  117. glVertex2f(iRight,iTop);
  118. glTexCoord2f(0.53f,tpp.y);
  119. glVertex2f(iRight,iTop+16);
  120. glTexCoord2f(0,tpp.y);
  121. glVertex2f(iLeft,iTop+16);
  122. glEnd();
  123. return true;
  124. }
  125. private:
  126. cStaticText m_stCaption;
  127. cPictureBox m_pbIcon;
  128. class cMinimizeBox : public cPictureBox, private MouseEventsAbstractor< cMinimizeBox > {
  129. public:
  130. cMinimizeBox()
  131. {
  132. SetPicture(0x06001283);
  133. SetAnchorLeft(false);
  134. SetAnchorRight(true);
  135. SetSize(16,16);
  136. AddMouseEventHandler( *(MouseEventsAbstractor< cMinimizeBox > *)this );
  137. }
  138. private:
  139. bool MouseEventsAbstractor< cMinimizeBox >::OnMouseDown( IWindow & Window, float X, float Y, unsigned long Button )
  140. {
  141. GetParent()->GetParent()->SetVisible(false);
  142. return true;
  143. }
  144. bool MouseEventsAbstractor< cMinimizeBox >::OnClick( IWindow &Window, float X, float Y, unsigned long Button )
  145. {
  146. return false;
  147. }
  148. bool MouseEventsAbstractor< cMinimizeBox >::OnDoubleClick( IWindow &Window, float X, float Y, unsigned long Button )
  149. {
  150. return false;
  151. }
  152. bool MouseEventsAbstractor< cMinimizeBox >::OnMouseWheel( IWindow &Window, float X, float Y, unsigned long Button )
  153. {
  154. return false;
  155. }
  156. bool MouseEventsAbstractor< cMinimizeBox >::OnMouseUp( IWindow &Window, float X, float Y, unsigned long Button )
  157. {
  158. return false;
  159. }
  160. bool MouseEventsAbstractor< cMinimizeBox >::OnMouseMove( IWindow &Window, float X, float Y, unsigned long Button )
  161. {
  162. return false;
  163. }
  164. bool MouseEventsAbstractor< cMinimizeBox >::OnMouseEnter( IWindow &Window, float X, float Y, unsigned long Button )
  165. {
  166. return false;
  167. }
  168. bool MouseEventsAbstractor< cMinimizeBox >::OnMouseExit( IWindow &Window, float X, float Y, unsigned long Button )
  169. {
  170. return false;
  171. }
  172. } m_mbMinimize;
  173. float m_fLastX, m_fLastY;
  174. bool m_bMoving;
  175. float m_fTrans;
  176. };
  177. class cMovableWindow : public CWindow, private MouseEventsAbstractor< cMovableWindow >, private RenderEventAbstractor< cMovableWindow > {
  178. public:
  179. cMovableWindow()
  180. {
  181. m_fTrans = 1.0f;
  182. m_TitleBar.SetAnchorRight(true);
  183. m_TitleBar.SetSize(GetWidth(), 16);
  184. m_TitleBar.SetPosition(0,0);
  185. m_TitleBar.SetVisible(true);
  186. AddChild(m_TitleBar);
  187. m_wClientArea.SetPosition(4, 16);
  188. m_wClientArea.SetSize(GetWidth()-8,GetHeight()-20);
  189. m_wClientArea.SetAnchorBottom(true);
  190. m_wClientArea.SetAnchorRight(true);
  191. m_wClientArea.SetVisible(true);
  192. AddChild(m_wClientArea);
  193. m_bDraggingL = false;
  194. m_bDraggingR = false;
  195. m_bSetCursor = false;
  196. AddRenderEventHandler( *(RenderEventAbstractor< cMovableWindow > *)this );
  197. AddMouseEventHandler( *(MouseEventsAbstractor< cMovableWindow > *)this );
  198. }
  199. ~cMovableWindow()
  200. {
  201. }
  202. private:
  203. bool RenderEventAbstractor< cMovableWindow >::OnRender( IWindow & Window, double TimeSlice )
  204. {
  205. float iLeft = GetAbsoluteLeft(),
  206. iRight = iLeft + GetWidth(),
  207. iTop = GetAbsoluteTop(),
  208. iBottom = iTop + GetHeight();
  209. //background
  210. glBindTexture(GL_TEXTURE_2D, m_Portal->FindGraphic(0x0600129C));
  211. glBegin(GL_QUADS);
  212. glColor4f(1,1,1,m_fTrans);
  213. glTexCoord2f(0,0);
  214. glVertex2f(iLeft,iTop+16);
  215. glTexCoord2f(1,0);
  216. glVertex2f(iRight,iTop+16);
  217. glTexCoord2f(1,1);
  218. glVertex2f(iRight,iBottom);
  219. glTexCoord2f(0,1);
  220. glVertex2f(iLeft,iBottom);
  221. glEnd();
  222. //left edge
  223. glBindTexture(GL_TEXTURE_2D, m_Portal->FindGraphic(0x0600114C));
  224. POINTf tpp = m_Portal->GetGraphicEdges(0x0600114C);
  225. glBegin(GL_QUADS);
  226. glColor4f(1,1,1,m_fTrans);
  227. glTexCoord2f(0.1f*tpp.x,0);
  228. glVertex2f(iLeft,iTop+16);
  229. glTexCoord2f(0.7f*tpp.x,0);
  230. glVertex2f(iLeft+4,iTop+16);
  231. glTexCoord2f(0.7f*tpp.x,tpp.y);
  232. glVertex2f(iLeft+4,iBottom);
  233. glTexCoord2f(0.1f*tpp.x,tpp.y);
  234. glVertex2f(iLeft,iBottom);
  235. glEnd();
  236. //right edge
  237. glBegin(GL_QUADS);
  238. glColor4f(1,1,1,m_fTrans);
  239. glTexCoord2f(0.1f*tpp.x,0);
  240. glVertex2f(iRight,iTop+16);
  241. glTexCoord2f(0.7f*tpp.x,0);
  242. glVertex2f(iRight-4,iTop+16);
  243. glTexCoord2f(0.7f*tpp.x,tpp.y);
  244. glVertex2f(iRight-4,iBottom);
  245. glTexCoord2f(0.1f*tpp.x,tpp.y);
  246. glVertex2f(iRight,iBottom);
  247. glEnd();
  248. //bottom
  249. glBindTexture(GL_TEXTURE_2D, m_Portal->FindGraphic(0x06001125));
  250. tpp = m_Portal->GetGraphicEdges(0x06001125);
  251. glBegin(GL_QUADS);
  252. glColor4f(1,1,1,m_fTrans);
  253. glTexCoord2f(0,0.1f*tpp.y);
  254. glVertex2f(iLeft,iBottom);
  255. glTexCoord2f(tpp.x,0.1f*tpp.y);
  256. glVertex2f(iRight,iBottom);
  257. glTexCoord2f(tpp.x,0.7f*tpp.y);
  258. glVertex2f(iRight,iBottom-4);
  259. glTexCoord2f(0,0.7f*tpp.y);
  260. glVertex2f(iLeft,iBottom-4);
  261. glEnd();
  262. return true;
  263. }
  264. public:
  265. void SetTransparency( float fTrans )
  266. {
  267. m_fTrans = fTrans;
  268. m_TitleBar.SetTransparency(fTrans);
  269. }
  270. void SetTitle(std::string sTitle)
  271. {
  272. m_TitleBar.SetCaption(sTitle);
  273. }
  274. std::string GetTitle() const
  275. {
  276. return m_TitleBar.GetCaption();
  277. }
  278. void SetIcon(DWORD dwIcon)
  279. {
  280. m_TitleBar.SetIcon(dwIcon);
  281. }
  282. DWORD GetIcon() const
  283. {
  284. return m_TitleBar.GetIcon();
  285. }
  286. void AddClientChild(IWindow &Child)
  287. {
  288. m_wClientArea.AddChild(Child);
  289. }
  290. void RemoveClientChild(IWindow &Child)
  291. {
  292. m_wClientArea.RemoveChild(Child);
  293. }
  294. private:
  295. bool MouseEventsAbstractor< cMovableWindow >::OnMouseDown( IWindow & Window, float X, float Y, unsigned long Button )
  296. {
  297. if (Button & 1)
  298. {
  299. m_fStartX = GetAbsoluteLeft() + X;
  300. m_fStartY = GetAbsoluteTop() + Y;
  301. if (((X <= 4) && (Y >= GetHeight() - 8)) ||
  302. ((X <= 8) && (Y >= GetHeight() - 4)))
  303. m_bDraggingL = true;
  304. if (((X >= GetWidth() - 4) && (Y >= GetHeight() - 8)) ||
  305. ((X >= GetWidth() - 8) && (Y >= GetHeight() - 4)))
  306. m_bDraggingR = true;
  307. }
  308. return true;
  309. }
  310. bool MouseEventsAbstractor< cMovableWindow >::OnMouseMove( IWindow & Window, float X, float Y, unsigned long Button )
  311. {
  312. if (m_bDraggingL)
  313. {
  314. float fX = GetAbsoluteLeft() + X;
  315. float fY = GetAbsoluteTop() + Y;
  316. SetSize(GetWidth() - (fX - m_fStartX), GetHeight() + (fY - m_fStartY));
  317. SetLeft(GetLeft() + (fX - m_fStartX));
  318. m_fStartX = fX;
  319. m_fStartY = fY;
  320. }
  321. else if (m_bDraggingR)
  322. {
  323. float fX = GetAbsoluteLeft() + X;
  324. float fY = GetAbsoluteTop() + Y;
  325. SetSize(GetWidth() + (fX - m_fStartX), GetHeight() + (fY - m_fStartY));
  326. m_fStartX = fX;
  327. m_fStartY = fY;
  328. }
  329. else
  330. {
  331. if (((X >= GetWidth() - 4) && (Y >= GetHeight() - 8)) ||
  332. ((X >= GetWidth() - 8) && (Y >= GetHeight() - 4)))
  333. {
  334. if (!m_bSetCursor)
  335. {
  336. SetClassLongPtr(GetForegroundWindow(), GCLP_HCURSOR, (LONG_PTR) LoadCursor(NULL, IDC_SIZENWSE));
  337. m_bSetCursor = true;
  338. }
  339. }
  340. else if (((X <= 4) && (Y >= GetHeight() - 8)) ||
  341. ((X <= 8) && (Y >= GetHeight() - 4)))
  342. {
  343. if (!m_bSetCursor)
  344. {
  345. SetClassLongPtr(GetForegroundWindow(), GCLP_HCURSOR, (LONG_PTR) LoadCursor(NULL, IDC_SIZENESW));
  346. m_bSetCursor = true;
  347. }
  348. }
  349. else
  350. {
  351. if (m_bSetCursor)
  352. {
  353. SetClassLongPtr(GetForegroundWindow(), GCLP_HCURSOR, (LONG_PTR) LoadCursor(NULL, IDC_ARROW));
  354. m_bSetCursor = false;
  355. }
  356. }
  357. }
  358. return true;
  359. }
  360. bool MouseEventsAbstractor< cMovableWindow >::OnMouseExit( IWindow & Window, float X, float Y, unsigned long Button )
  361. {
  362. SetClassLongPtr(GetForegroundWindow(), GCLP_HCURSOR, (LONG_PTR) LoadCursor(NULL, IDC_ARROW));
  363. m_bSetCursor = false;
  364. return true;
  365. }
  366. bool MouseEventsAbstractor< cMovableWindow >::OnMouseUp( IWindow & Window, float X, float Y, unsigned long Button )
  367. {
  368. if (m_bDraggingL)
  369. m_bDraggingL = false;
  370. if (m_bDraggingR)
  371. m_bDraggingR = false;
  372. return true;
  373. }
  374. bool MouseEventsAbstractor< cMovableWindow >::OnClick( IWindow &Window, float X, float Y, unsigned long Button )
  375. {
  376. return false;
  377. }
  378. bool MouseEventsAbstractor< cMovableWindow >::OnDoubleClick( IWindow &Window, float X, float Y, unsigned long Button )
  379. {
  380. return false;
  381. }
  382. bool MouseEventsAbstractor< cMovableWindow >::OnMouseWheel( IWindow &Window, float X, float Y, unsigned long Button )
  383. {
  384. return false;
  385. }
  386. bool MouseEventsAbstractor< cMovableWindow >::OnMouseEnter( IWindow &Window, float X, float Y, unsigned long Button )
  387. {
  388. return false;
  389. }
  390. protected:
  391. float m_fTrans;
  392. cTitleBar m_TitleBar;
  393. CWindow m_wClientArea;
  394. private:
  395. bool m_bDraggingR, m_bDraggingL, m_bSetCursor;
  396. float m_fStartX, m_fStartY;
  397. };