Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
8.4 KiB

  1. //===== Copyright � 2005-2006, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: build a sheet data file and a large image out of multiple images
  4. //
  5. //===========================================================================//
  6. #include "materialobjects/dmedemo3.h"
  7. #include "tier1/strtools.h"
  8. #include "datamodel/dmelementfactoryhelper.h"
  9. #include "tier0/dbg.h"
  10. //-----------------------------------------------------------------------------
  11. // Dme version of a quad
  12. //-----------------------------------------------------------------------------
  13. IMPLEMENT_ELEMENT_FACTORY( DmeQuadV3, CDmeQuadV3 );
  14. void CDmeQuadV3::OnConstruction()
  15. {
  16. m_X0.Init( this, "x0" );
  17. m_Y0.Init( this, "y0" );
  18. m_X1.Init( this, "x1" );
  19. m_Y1.Init( this, "y1" );
  20. m_Color.InitAndSet( this, "color", Color( 255, 255, 255, 255 ) );
  21. }
  22. void CDmeQuadV3::OnDestruction()
  23. {
  24. }
  25. //-----------------------------------------------------------------------------
  26. // Dme version of a list of quads
  27. //-----------------------------------------------------------------------------
  28. IMPLEMENT_ELEMENT_FACTORY( DmeQuadListV3, CDmeQuadListV3 );
  29. void CDmeQuadListV3::OnConstruction()
  30. {
  31. m_Quads.Init( this, "quads" );
  32. }
  33. void CDmeQuadListV3::OnDestruction()
  34. {
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Iteration necessary to render
  38. //-----------------------------------------------------------------------------
  39. int CDmeQuadListV3::GetQuadCount() const
  40. {
  41. return m_Quads.Count();
  42. }
  43. CDmeQuadV3 *CDmeQuadListV3::GetQuad( int i )
  44. {
  45. return m_Quads[ i ];
  46. }
  47. //-----------------------------------------------------------------------------
  48. // List management
  49. //-----------------------------------------------------------------------------
  50. void CDmeQuadListV3::AddQuad( CDmeQuadV3 *pQuad )
  51. {
  52. m_Quads.InsertBefore( 0, pQuad );
  53. }
  54. CDmeQuadV3 *CDmeQuadListV3::FindQuadByName( const char *pName )
  55. {
  56. for ( int i = 0; i < m_Quads.Count(); ++i )
  57. {
  58. if ( !Q_stricmp( pName, m_Quads[i]->GetName() ) )
  59. return m_Quads[i];
  60. }
  61. return NULL;
  62. }
  63. void CDmeQuadListV3::RemoveQuad( CDmeQuadV3 *pQuad )
  64. {
  65. int nIndex = m_Quads.Find( pQuad );
  66. if ( nIndex != m_Quads.InvalidIndex() )
  67. {
  68. m_Quads.Remove( nIndex );
  69. }
  70. }
  71. void CDmeQuadListV3::RemoveAllQuads()
  72. {
  73. m_Quads.RemoveAll();
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Render order management
  77. //-----------------------------------------------------------------------------
  78. void CDmeQuadListV3::MoveToFront( CDmeQuadV3 *pQuad )
  79. {
  80. int nIndex = m_Quads.Find( pQuad );
  81. if ( nIndex != m_Quads.InvalidIndex() )
  82. {
  83. m_Quads.Remove( nIndex );
  84. m_Quads.AddToTail( pQuad );
  85. }
  86. }
  87. void CDmeQuadListV3::MoveToBack( CDmeQuadV3 *pQuad )
  88. {
  89. int nIndex = m_Quads.Find( pQuad );
  90. if ( nIndex != m_Quads.InvalidIndex() )
  91. {
  92. m_Quads.Remove( nIndex );
  93. m_Quads.InsertBefore( 0, pQuad );
  94. }
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Dme version of a the editor 'document'
  98. //-----------------------------------------------------------------------------
  99. IMPLEMENT_ELEMENT_FACTORY( DmeQuadDocV3, CDmeQuadDocV3 );
  100. void CDmeQuadDocV3::OnConstruction()
  101. {
  102. m_QuadList.InitAndCreate( this, "quadList" );
  103. m_SelectedQuads.Init( this, "selectedQuads", FATTRIB_DONTSAVE );
  104. }
  105. void CDmeQuadDocV3::OnDestruction()
  106. {
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Iteration necessary to render
  110. //-----------------------------------------------------------------------------
  111. int CDmeQuadDocV3::GetQuadCount() const
  112. {
  113. return m_QuadList->GetQuadCount();
  114. }
  115. CDmeQuadV3 *CDmeQuadDocV3::GetQuad( int i )
  116. {
  117. return m_QuadList->GetQuad( i );
  118. }
  119. int CDmeQuadDocV3::GetSelectedQuadCount() const
  120. {
  121. return m_SelectedQuads.Count();
  122. }
  123. CDmeQuadV3 *CDmeQuadDocV3::GetSelectedQuad( int i )
  124. {
  125. return m_SelectedQuads[i];
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Adds quad, resets selection to new quad
  129. //-----------------------------------------------------------------------------
  130. void CDmeQuadDocV3::AddQuad( const char *pName, int x0, int y0, int x1, int y1 )
  131. {
  132. CDmeQuadV3 *pQuadV3 = CreateElement< CDmeQuadV3 >( pName, GetFileId() );
  133. pQuadV3->m_X0 = x0;
  134. pQuadV3->m_X1 = x1;
  135. pQuadV3->m_Y0 = y0;
  136. pQuadV3->m_Y1 = y1;
  137. m_QuadList->AddQuad( pQuadV3 );
  138. ClearSelection();
  139. AddQuadToSelection( pName );
  140. }
  141. //-----------------------------------------------------------------------------
  142. // Clears selection
  143. //-----------------------------------------------------------------------------
  144. void CDmeQuadDocV3::ClearSelection()
  145. {
  146. m_SelectedQuads.RemoveAll();
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Adds quad to selection
  150. //-----------------------------------------------------------------------------
  151. void CDmeQuadDocV3::AddQuadToSelection( const char *pName )
  152. {
  153. CDmeQuadV3 *pQuad = m_QuadList->FindQuadByName( pName );
  154. if ( pQuad )
  155. {
  156. if ( m_SelectedQuads.Find( pQuad ) == m_SelectedQuads.InvalidIndex() )
  157. {
  158. m_SelectedQuads.AddToTail( pQuad );
  159. }
  160. }
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Add quads in rect to selection
  164. //-----------------------------------------------------------------------------
  165. void CDmeQuadDocV3::AddQuadsInRectToSelection( int x0, int y0, int x1, int y1 )
  166. {
  167. if ( x0 > x1 )
  168. {
  169. swap( x0, x1 );
  170. }
  171. if ( y0 > y1 )
  172. {
  173. swap( y0, y1 );
  174. }
  175. int nCount = m_QuadList->GetQuadCount();
  176. for ( int i = 0; i < nCount; ++i )
  177. {
  178. CDmeQuadV3 *pQuad = m_QuadList->GetQuad( i );
  179. if ( x0 < pQuad->MaxX() && x1 > pQuad->MinX() && y0 < pQuad->MaxY() && y1 > pQuad->MinY() )
  180. {
  181. if ( m_SelectedQuads.Find( pQuad ) == m_SelectedQuads.InvalidIndex() )
  182. {
  183. m_SelectedQuads.AddToTail( pQuad );
  184. }
  185. }
  186. }
  187. }
  188. //-----------------------------------------------------------------------------
  189. // Is point in selected quad?
  190. //-----------------------------------------------------------------------------
  191. bool CDmeQuadDocV3::IsPointInSelectedQuad( int x, int y ) const
  192. {
  193. int nCount = m_SelectedQuads.Count();
  194. for ( int i = 0; i < nCount; ++i )
  195. {
  196. CDmeQuadV3 *pQuad = m_SelectedQuads[i];
  197. if ( x >= pQuad->MinX() && x <= pQuad->MaxX() && y >= pQuad->MinY() && y <= pQuad->MaxY() )
  198. return true;
  199. }
  200. return false;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Deletes selected quads
  204. //-----------------------------------------------------------------------------
  205. void CDmeQuadDocV3::DeleteSelectedQuads()
  206. {
  207. int nCount = m_SelectedQuads.Count();
  208. for ( int i = nCount; --i >= 0; )
  209. {
  210. CDmeQuadV3 *pQuad = m_SelectedQuads[i];
  211. m_SelectedQuads.FastRemove( i );
  212. DestroyElement( pQuad );
  213. }
  214. }
  215. //-----------------------------------------------------------------------------
  216. // Changes quad color
  217. //-----------------------------------------------------------------------------
  218. void CDmeQuadDocV3::SetSelectedQuadColor( int r, int g, int b, int a )
  219. {
  220. Color c( r, g, b, a );
  221. int nCount = m_SelectedQuads.Count();
  222. for ( int i = 0; i < nCount; ++i )
  223. {
  224. CDmeQuadV3 *pQuad = m_SelectedQuads[i];
  225. pQuad->m_Color.Set( c );
  226. }
  227. }
  228. //-----------------------------------------------------------------------------
  229. // Moves quads
  230. //-----------------------------------------------------------------------------
  231. void CDmeQuadDocV3::MoveSelectedQuads( int dx, int dy )
  232. {
  233. int nCount = m_SelectedQuads.Count();
  234. for ( int i = 0; i < nCount; ++i )
  235. {
  236. CDmeQuadV3 *pQuad = m_SelectedQuads[i];
  237. pQuad->m_X0 += dx;
  238. pQuad->m_X1 += dx;
  239. pQuad->m_Y0 += dy;
  240. pQuad->m_Y1 += dy;
  241. }
  242. }
  243. //-----------------------------------------------------------------------------
  244. // Resizes selected quad (works only when 1 quad is selected)
  245. //-----------------------------------------------------------------------------
  246. void CDmeQuadDocV3::ResizeSelectedQuad( int nWidth, int nHeight )
  247. {
  248. if ( m_SelectedQuads.Count() != 1 )
  249. return;
  250. CDmeQuadV3 *pQuad = m_SelectedQuads[0];
  251. pQuad->m_X1 = pQuad->m_X0 + nWidth;
  252. pQuad->m_Y1 = pQuad->m_Y0 + nHeight;
  253. }
  254. //-----------------------------------------------------------------------------
  255. // Moves selected quad to front/back (works only when 1 quad is selected)
  256. //-----------------------------------------------------------------------------
  257. void CDmeQuadDocV3::MoveSelectedToFront()
  258. {
  259. if ( m_SelectedQuads.Count() != 1 )
  260. return;
  261. m_QuadList->MoveToFront( m_SelectedQuads[0] );
  262. }
  263. void CDmeQuadDocV3::MoveSelectedToBack()
  264. {
  265. if ( m_SelectedQuads.Count() != 1 )
  266. return;
  267. m_QuadList->MoveToBack( m_SelectedQuads[0] );
  268. }