ColorPaletteWnd.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. // ColorPaletteWnd.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #include "ColorPaletteWnd.h"
  16. #ifdef _DEBUG
  17. #undef new
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. // this ptr to color is used as result ptr meaning when one of colors from palette is clicked,
  23. // color that is pointed trough this ptr is filled with choosed color value
  24. COLOR *_pcolColorToSet;
  25. COLOR acol_ColorizePallete[] =
  26. {
  27. C_RED, C_dGREEN, C_BLUE, C_vdCYAN,
  28. C_MAGENTA, C_vdYELLOW, C_ORANGE, C_BROWN,
  29. C_PINK, C_dGRAY, C_GRAY, C_vdGRAY,
  30. C_dRED, C_lRED, C_vdGREEN, C_mdGREEN,
  31. C_dBLUE, C_lBLUE, C_dCYAN, C_vdBLUE,
  32. C_vdMAGENTA, C_dMAGENTA, C_dYELLOW, C_mdBROWN,
  33. C_vdORANGE, C_dORANGE, C_vdBROWN, C_BROWN,
  34. C_dPINK, C_lPINK, C_mdGRAY, C_vdRED,
  35. };
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CColorPaletteWnd
  38. CColorPaletteWnd::CColorPaletteWnd()
  39. {
  40. m_iSelectedColor = -1;
  41. _pcolColorToSet = NULL;
  42. m_pDrawPort = NULL;
  43. m_pViewPort = NULL;
  44. }
  45. CColorPaletteWnd::~CColorPaletteWnd()
  46. {
  47. if( m_pViewPort != NULL)
  48. {
  49. _pGfx->DestroyWindowCanvas( m_pViewPort);
  50. m_pViewPort = NULL;
  51. }
  52. }
  53. BEGIN_MESSAGE_MAP(CColorPaletteWnd, CWnd)
  54. //{{AFX_MSG_MAP(CColorPaletteWnd)
  55. ON_WM_PAINT()
  56. ON_WM_LBUTTONDOWN()
  57. ON_WM_KILLFOCUS()
  58. ON_WM_RBUTTONDOWN()
  59. //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CColorPaletteWnd message handlers
  63. // constants for defining palette window's looks
  64. #define COLORS_PER_X 4
  65. #define COLORS_PER_Y 8
  66. #define CLIENT_BORDER 6
  67. /*
  68. * Calculate given color's box in pixels
  69. */
  70. PIXaabbox2D CColorPaletteWnd::GetColorBBox( INDEX iColor)
  71. {
  72. CRect rectClient;
  73. // get window's client area
  74. GetClientRect( &rectClient);
  75. PIX DX = (rectClient.Width() - 2*CLIENT_BORDER)/COLORS_PER_X;
  76. PIX DY = (rectClient.Height() - 2*CLIENT_BORDER)/COLORS_PER_Y;
  77. // calculate starting pixel for current color
  78. PIX pixXS = CLIENT_BORDER + (iColor%COLORS_PER_X)*DX;
  79. PIX pixYS = CLIENT_BORDER + (iColor/COLORS_PER_X)*DY;
  80. // return calculated box
  81. return PIXaabbox2D( PIX2D(pixXS, pixYS), PIX2D(pixXS+DX, pixYS+DY) );
  82. }
  83. void CColorPaletteWnd::OnPaint()
  84. {
  85. {
  86. CPaintDC dc(this); // device context for painting
  87. }
  88. // if there is a valid drawport, and the drawport can be locked
  89. if( (m_pDrawPort != NULL) && (m_pDrawPort->Lock()) )
  90. {
  91. CWorldEditorView *pWorldEditorView = theApp.GetActiveView();
  92. ASSERT( pWorldEditorView != NULL);
  93. // clear background
  94. m_pDrawPort->Fill( C_BLACK | CT_OPAQUE);
  95. // erase z-buffer
  96. m_pDrawPort->FillZBuffer(ZBUF_BACK);
  97. // for all colors
  98. for( INDEX i=0; i<32; i++)
  99. {
  100. // get current color's box in pixels inside window
  101. PIXaabbox2D boxColor = GetColorBBox( i);
  102. // fill rectangle in current color
  103. m_pDrawPort->Fill( boxColor.Min()(1)+1, boxColor.Min()(2)+1,
  104. boxColor.Max()(1)-boxColor.Min()(1)-2, boxColor.Max()(2)-boxColor.Min()(2)-2,
  105. acol_ColorizePallete[ i] | CT_OPAQUE);
  106. // if we are drawing selected color
  107. if( i == m_iSelectedColor)
  108. {
  109. m_pDrawPort->DrawLine( boxColor.Min()(1), boxColor.Min()(2),
  110. boxColor.Min()(1), boxColor.Max()(2), C_WHITE|CT_OPAQUE, _POINT_);
  111. m_pDrawPort->DrawLine( boxColor.Min()(1), boxColor.Max()(2),
  112. boxColor.Max()(1), boxColor.Max()(2), C_WHITE|CT_OPAQUE, _POINT_);
  113. m_pDrawPort->DrawLine( boxColor.Max()(1), boxColor.Max()(2),
  114. boxColor.Max()(1), boxColor.Min()(2), C_WHITE|CT_OPAQUE, _POINT_);
  115. m_pDrawPort->DrawLine( boxColor.Max()(1), boxColor.Min()(2),
  116. boxColor.Min()(1), boxColor.Min()(2), C_WHITE|CT_OPAQUE, _POINT_);
  117. }
  118. }
  119. // unlock the drawport
  120. m_pDrawPort->Unlock();
  121. // if there is a valid viewport
  122. if (m_pViewPort!=NULL)
  123. {
  124. m_pViewPort->SwapBuffers();
  125. }
  126. }
  127. }
  128. void CColorPaletteWnd::OnLButtonDown(UINT nFlags, CPoint point)
  129. {
  130. PIXaabbox2D boxPoint( PIX2D( point.x, point.y), PIX2D(point.x, point.y) );
  131. // for all colors
  132. for( INDEX iSelectedColor=0; iSelectedColor<32; iSelectedColor++)
  133. {
  134. if( (GetColorBBox( iSelectedColor) & boxPoint) == boxPoint)
  135. {
  136. // obtain document
  137. CWorldEditorDoc *pDoc = theApp.GetDocument();
  138. // must not be null
  139. ASSERT( pDoc != NULL);
  140. // if polygon mode
  141. if( pDoc->m_iMode == POLYGON_MODE)
  142. {
  143. // polygon selection must contain selected polygons
  144. ASSERT(pDoc->m_selPolygonSelection.Count() != 0);
  145. // for each of the selected polygons
  146. FOREACHINDYNAMICCONTAINER(pDoc->m_selPolygonSelection, CBrushPolygon, itbpo)
  147. {
  148. // set new polygon's color
  149. itbpo->bpo_colColor = acol_ColorizePallete[ iSelectedColor];
  150. }
  151. }
  152. else if( pDoc->m_iMode == SECTOR_MODE)
  153. {
  154. // sector selection must contain selected sectors
  155. ASSERT(pDoc->m_selSectorSelection.Count() != 0);
  156. // for each of the selected sectors
  157. FOREACHINDYNAMICCONTAINER(pDoc->m_selSectorSelection, CBrushSector, itbsc)
  158. {
  159. // set new polygon's color
  160. itbsc->bsc_colColor = acol_ColorizePallete[ iSelectedColor];
  161. }
  162. }
  163. if( _pcolColorToSet != NULL)
  164. {
  165. // fill result
  166. *_pcolColorToSet = acol_ColorizePallete[ iSelectedColor];
  167. }
  168. // update all document's views
  169. pDoc->UpdateAllViews( NULL);
  170. break;
  171. }
  172. }
  173. // destroy color palette
  174. CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  175. pMainFrame->m_pColorPalette = NULL;
  176. // destroy color palette
  177. delete this;
  178. }
  179. static BOOL _bCanBeDestroyed = TRUE;
  180. void CColorPaletteWnd::OnKillFocus(CWnd* pNewWnd)
  181. {
  182. if( !_bCanBeDestroyed) return;
  183. // destroy color palette
  184. CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  185. pMainFrame->m_pColorPalette = NULL;
  186. delete this;
  187. }
  188. void CColorPaletteWnd::OnRButtonDown(UINT nFlags, CPoint point)
  189. {
  190. PIXaabbox2D boxPoint( PIX2D( point.x, point.y), PIX2D(point.x, point.y) );
  191. // for all colors
  192. for( INDEX iSelectedColor=0; iSelectedColor<32; iSelectedColor++)
  193. {
  194. if( (GetColorBBox( iSelectedColor) & boxPoint) == boxPoint)
  195. {
  196. COLORREF TmpColor = CLRF_CLR( acol_ColorizePallete[ iSelectedColor]);
  197. _bCanBeDestroyed = FALSE;
  198. if( MyChooseColor( TmpColor, *GetParent()))
  199. {
  200. // remember result
  201. acol_ColorizePallete[ iSelectedColor] = CLR_CLRF( TmpColor);
  202. Invalidate( FALSE);
  203. }
  204. _bCanBeDestroyed = TRUE;
  205. }
  206. }
  207. CWnd::OnRButtonDown(nFlags, point);
  208. // destroy color palette
  209. CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
  210. pMainFrame->m_pColorPalette = NULL;
  211. delete this;
  212. }