ViewTexture.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // ViewTexture.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #include "WorldEditor.h"
  16. #include "ViewTexture.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. // CViewTexture
  25. CViewTexture::CViewTexture()
  26. {
  27. m_pViewPort = NULL;
  28. m_pDrawPort = NULL;
  29. }
  30. CViewTexture::~CViewTexture()
  31. {
  32. }
  33. BEGIN_MESSAGE_MAP(CViewTexture, CWnd)
  34. //{{AFX_MSG_MAP(CViewTexture)
  35. ON_WM_PAINT()
  36. ON_WM_LBUTTONDOWN()
  37. ON_WM_LBUTTONDBLCLK()
  38. ON_WM_CONTEXTMENU()
  39. ON_COMMAND(ID_RECREATE_TEXTURE, OnRecreateTexture)
  40. ON_WM_DESTROY()
  41. //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CViewTexture message handlers
  45. void CViewTexture::OnPaint()
  46. {
  47. {
  48. CPaintDC dc(this); // device context for painting
  49. }
  50. CWnd *pwndRect = GetParent()->GetDlgItem( IDC_PREVIEW_FRAME);
  51. if( pwndRect != NULL)
  52. {
  53. CRect rectBorder;
  54. pwndRect->GetWindowRect( rectBorder);
  55. GetParent()->ScreenToClient( rectBorder);
  56. MoveWindow( rectBorder);
  57. }
  58. if( (m_pViewPort == NULL) && (m_pDrawPort == NULL) )
  59. {
  60. // initialize canvas for active texture button
  61. _pGfx->CreateWindowCanvas( m_hWnd, &m_pViewPort, &m_pDrawPort);
  62. }
  63. // if there is a valid drawport, and the drawport can be locked
  64. if( (m_pDrawPort != NULL) && (m_pDrawPort->Lock()))
  65. {
  66. PIX pixWidth = m_pDrawPort->GetWidth();
  67. PIX pixHeight = m_pDrawPort->GetHeight();
  68. PIXaabbox2D rectPict;
  69. rectPict = PIXaabbox2D( PIX2D(0, 0),
  70. PIX2D(m_pDrawPort->GetWidth(), m_pDrawPort->GetHeight()));
  71. // clear texture area to black
  72. m_pDrawPort->Fill( C_BLACK | CT_OPAQUE);
  73. // erase z-buffer
  74. m_pDrawPort->FillZBuffer(ZBUF_BACK);
  75. CTextureObject toTexture;
  76. try
  77. {
  78. toTexture.SetData_t( m_strTexture);
  79. }
  80. catch( char *strError)
  81. {
  82. (void) strError;
  83. }
  84. if( toTexture.GetData() != NULL)
  85. {
  86. m_pDrawPort->PutTexture( &toTexture, rectPict);
  87. }
  88. else
  89. {
  90. // type text saying none selected
  91. m_pDrawPort->SetFont( theApp.m_pfntSystem);
  92. m_pDrawPort->SetTextAspect( 1.0f);
  93. m_pDrawPort->PutTextC( m_strTexture, pixWidth/2, pixHeight*1/3-10);
  94. }
  95. // unlock the drawport
  96. m_pDrawPort->Unlock();
  97. // if there is a valid viewport
  98. if (m_pViewPort!=NULL)
  99. {
  100. // swap it
  101. m_pViewPort->SwapBuffers();
  102. }
  103. }
  104. }
  105. void CViewTexture::OnLButtonDown(UINT nFlags, CPoint point)
  106. {
  107. if( !GetParent()->GetDlgItem( IDC_PREVIEW_FRAME)->IsWindowEnabled())
  108. {
  109. return;
  110. }
  111. HGLOBAL hglobal = CreateHDrop( m_strTexture);
  112. m_DataSource.CacheGlobalData( CF_HDROP, hglobal);
  113. m_DataSource.DoDragDrop( DROPEFFECT_COPY);
  114. CWnd::OnLButtonDown(nFlags, point);
  115. }
  116. void CViewTexture::OnLButtonDblClk(UINT nFlags, CPoint point)
  117. {
  118. OnRecreateTexture();
  119. CWnd::OnLButtonDblClk(nFlags, point);
  120. }
  121. void CViewTexture::OnContextMenu(CWnd* pWnd, CPoint point)
  122. {
  123. CMenu menu;
  124. if( menu.LoadMenu(IDR_THUMBNAIL_TEXTURE_POPUP))
  125. {
  126. CMenu* pPopup = menu.GetSubMenu(0);
  127. pPopup->TrackPopupMenu( TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_LEFTALIGN,
  128. point.x, point.y, this);
  129. }
  130. }
  131. void CViewTexture::OnRecreateTexture()
  132. {
  133. CTextureObject toTexture;
  134. try
  135. {
  136. toTexture.SetData_t( m_strTexture);
  137. }
  138. catch( char *strError)
  139. {
  140. (void) strError;
  141. }
  142. if( toTexture.GetData() != NULL)
  143. {
  144. _EngineGUI.CreateTexture( CTString(m_strTexture));
  145. CWorldEditorDoc *pDoc = theApp.GetDocument();
  146. if( pDoc != NULL)
  147. {
  148. pDoc->UpdateAllViews( NULL);
  149. }
  150. }
  151. }
  152. void CViewTexture::OnDestroy()
  153. {
  154. CWnd::OnDestroy();
  155. if( m_pViewPort != NULL)
  156. {
  157. _pGfx->DestroyWindowCanvas( m_pViewPort);
  158. m_pViewPort = NULL;
  159. }
  160. m_pViewPort = NULL;
  161. m_pDrawPort = NULL;
  162. }