BrushPaletteWnd.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. // BrushPaletteWnd.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #include "WorldEditor.h"
  16. #include "BrushPaletteWnd.h"
  17. #ifdef _DEBUG
  18. #undef new
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CBrushPaletteWnd
  25. CBrushPaletteWnd::CBrushPaletteWnd()
  26. {
  27. m_pDrawPort = NULL;
  28. m_pViewPort = NULL;
  29. // mark that timer is not yet started
  30. m_iTimerID = -1;
  31. }
  32. CBrushPaletteWnd::~CBrushPaletteWnd()
  33. {
  34. if( m_pViewPort != NULL)
  35. {
  36. _pGfx->DestroyWindowCanvas( m_pViewPort);
  37. m_pViewPort = NULL;
  38. }
  39. }
  40. BEGIN_MESSAGE_MAP(CBrushPaletteWnd, CWnd)
  41. //{{AFX_MSG_MAP(CBrushPaletteWnd)
  42. ON_WM_PAINT()
  43. ON_WM_KILLFOCUS()
  44. ON_WM_LBUTTONDOWN()
  45. ON_WM_RBUTTONDOWN()
  46. ON_WM_MOUSEMOVE()
  47. ON_WM_DESTROY()
  48. ON_WM_TIMER()
  49. ON_WM_LBUTTONUP()
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CBrushPaletteWnd message handlers
  54. #define BRUSHES_PER_X 4
  55. #define BRUSHES_PER_Y 8
  56. #define CLIENT_BORDER 2
  57. PIXaabbox2D CBrushPaletteWnd::GetBrushBBox( INDEX iBrush)
  58. {
  59. CRect rectClient;
  60. // get window's client area
  61. GetClientRect( &rectClient);
  62. PIX DX = (rectClient.Width()+1 - 2*CLIENT_BORDER)/BRUSHES_PER_X;
  63. PIX DY = (rectClient.Height()+1 - 2*CLIENT_BORDER)/BRUSHES_PER_Y;
  64. // calculate starting pixel for current Brush
  65. PIX pixXS = CLIENT_BORDER + (iBrush%BRUSHES_PER_X)*DX;
  66. PIX pixYS = CLIENT_BORDER + (iBrush/BRUSHES_PER_X)*DY;
  67. // return calculated box
  68. return PIXaabbox2D( PIX2D(pixXS, pixYS), PIX2D(pixXS+DX-1, pixYS+DY-1) );
  69. }
  70. void CBrushPaletteWnd::OnPaint()
  71. {
  72. {
  73. CPaintDC dc(this); // device context for painting
  74. }
  75. if( m_iTimerID == -1)
  76. {
  77. m_iTimerID = (int) SetTimer( 1, 10, NULL);
  78. }
  79. POINT ptMouse;
  80. GetCursorPos( &ptMouse);
  81. ScreenToClient( &ptMouse);
  82. // if there is a valid drawport, and the drawport can be locked
  83. if( (m_pDrawPort != NULL) && (m_pDrawPort->Lock()) )
  84. {
  85. CWorldEditorView *pWorldEditorView = theApp.GetActiveView();
  86. ASSERT( pWorldEditorView != NULL);
  87. // clear background
  88. m_pDrawPort->Fill( C_lGRAY|CT_OPAQUE);
  89. // erase z-buffer
  90. m_pDrawPort->FillZBuffer(ZBUF_BACK);
  91. // for all brushes
  92. for( INDEX iBrush=0; iBrush<CT_BRUSHES; iBrush++)
  93. {
  94. // get current brush's box in pixels inside window
  95. PIXaabbox2D boxBrush = GetBrushBBox( iBrush);
  96. RenderBrushShape( iBrush, boxBrush, m_pDrawPort);
  97. TIME tm=_pTimer->GetRealTimeTick();
  98. // if we are drawing selected brush
  99. if(iBrush==theApp.m_fCurrentTerrainBrush)
  100. {
  101. if(m_pDrawPort->Lock())
  102. {
  103. FLOAT fFactor=sin(tm*8)/2.0f+0.5f;
  104. COLOR colSelected=LerpColor(C_lGRAY,C_RED,fFactor);
  105. m_pDrawPort->DrawBorder(boxBrush.Min()(1)-1, boxBrush.Min()(2)-1,
  106. boxBrush.Max()(1)-boxBrush.Min()(1)+2, boxBrush.Max()(2)-boxBrush.Min()(2)+2,
  107. colSelected|CT_OPAQUE);
  108. m_pDrawPort->Unlock();
  109. }
  110. }
  111. PIXaabbox2D boxPoint( PIX2D( ptMouse.x, ptMouse.y), PIX2D(ptMouse.x, ptMouse.y) );
  112. if( (boxBrush & boxPoint) == boxPoint)
  113. {
  114. if(m_pDrawPort->Lock())
  115. {
  116. INDEX iRot=((ULONG)(tm*25.0f))&7;
  117. ULONG ulLineType=0x0f0f0f0f<<iRot;
  118. m_pDrawPort->DrawBorder(boxBrush.Min()(1)-1, boxBrush.Min()(2)-1,
  119. boxBrush.Max()(1)-boxBrush.Min()(1)+2, boxBrush.Max()(2)-boxBrush.Min()(2)+2,
  120. C_BLUE|CT_OPAQUE, ulLineType);
  121. m_pDrawPort->Unlock();
  122. }
  123. }
  124. }
  125. // unlock the drawport
  126. m_pDrawPort->Unlock();
  127. // if there is a valid viewport
  128. if (m_pViewPort!=NULL)
  129. {
  130. m_pViewPort->SwapBuffers();
  131. }
  132. }
  133. }
  134. void CBrushPaletteWnd::OnKillFocus(CWnd* pNewWnd)
  135. {
  136. CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  137. if( pNewWnd!=pMainFrame->m_pwndToolTip && pNewWnd!=this)
  138. {
  139. // destroy brush palette
  140. _pBrushPalette = NULL;
  141. delete this;
  142. }
  143. }
  144. void CBrushPaletteWnd::OnLButtonDown(UINT nFlags, CPoint point)
  145. {
  146. }
  147. void CBrushPaletteWnd::OnRButtonDown(UINT nFlags, CPoint point)
  148. {
  149. PIXaabbox2D boxPoint( PIX2D( point.x, point.y), PIX2D(point.x, point.y) );
  150. // for all brushes
  151. for( INDEX iBrush=0; iBrush<CT_BRUSHES; iBrush++)
  152. {
  153. if( (GetBrushBBox( iBrush) & boxPoint) == boxPoint)
  154. {
  155. // destroy brush palette
  156. _pBrushPalette = NULL;
  157. delete this;
  158. // invoke edit terrain dlg
  159. CDlgEditTerrainBrush dlg;
  160. dlg.m_iBrush=iBrush;
  161. dlg.DoModal();
  162. break;
  163. }
  164. }
  165. }
  166. void CBrushPaletteWnd::OnMouseMove(UINT nFlags, CPoint point)
  167. {
  168. Invalidate(FALSE);
  169. CWnd::OnMouseMove(nFlags, point);
  170. }
  171. void CBrushPaletteWnd::OnDestroy()
  172. {
  173. KillTimer( m_iTimerID);
  174. CWnd::OnDestroy();
  175. }
  176. void CBrushPaletteWnd::OnTimer(UINT nIDEvent)
  177. {
  178. POINT pt;
  179. GetCursorPos( &pt);
  180. CRect rectWnd;
  181. GetWindowRect(rectWnd);
  182. if(pt.x<rectWnd.left || pt.x>rectWnd.right ||
  183. pt.y<rectWnd.top || pt.y>rectWnd.bottom)
  184. {
  185. DestroyWindow();
  186. DeleteTempMap();
  187. return;
  188. }
  189. Invalidate(FALSE);
  190. CWnd::OnTimer(nIDEvent);
  191. }
  192. BOOL CBrushPaletteWnd::PreTranslateMessage(MSG* pMsg)
  193. {
  194. if( pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE)
  195. {
  196. DestroyWindow();
  197. DeleteTempMap();
  198. return TRUE;
  199. }
  200. return CWnd::PreTranslateMessage(pMsg);
  201. }
  202. void CBrushPaletteWnd::OnLButtonUp(UINT nFlags, CPoint point)
  203. {
  204. PIXaabbox2D boxPoint( PIX2D( point.x, point.y), PIX2D(point.x, point.y) );
  205. // for all brushes
  206. for( INDEX iBrush=0; iBrush<CT_BRUSHES; iBrush++)
  207. {
  208. if( (GetBrushBBox( iBrush) & boxPoint) == boxPoint)
  209. {
  210. theApp.m_fCurrentTerrainBrush=iBrush;
  211. break;
  212. }
  213. }
  214. // destroy brush palette
  215. _pBrushPalette = NULL;
  216. delete this;
  217. theApp.m_ctTerrainPageCanvas.MarkChanged();
  218. }