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.

475 lines
13 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdio.h>
  8. #include <vgui/IBorder.h>
  9. #include <vgui/ISurface.h>
  10. #include <vgui/IScheme.h>
  11. #include <vgui/IBorder.h>
  12. #include <keyvalues.h>
  13. #include <vgui_controls/ImagePanel.h>
  14. #include <vgui_controls/Image.h>
  15. #include <vgui_controls/Controls.h>
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include <tier0/memdbgon.h>
  18. using namespace vgui;
  19. DECLARE_BUILD_FACTORY( ImagePanel );
  20. //-----------------------------------------------------------------------------
  21. // Purpose:
  22. //-----------------------------------------------------------------------------
  23. ImagePanel::ImagePanel(Panel *parent, const char *name) : Panel(parent, name)
  24. {
  25. m_pImage = NULL;
  26. m_pszImageName = NULL;
  27. m_pszFillColorName = NULL;
  28. m_pszDrawColorName = NULL;
  29. m_bScaleImage = false;
  30. m_bTileImage = false;
  31. m_bTileHorizontally = false;
  32. m_bTileVertically = false;
  33. m_iTileHorizontalOverride = 0;
  34. m_iTileVerticalOverride = 0;
  35. m_flTileHorizontalOverride = 0;
  36. m_flTileVerticalOverride = 0;
  37. m_fScaleAmount = 0.0f;
  38. m_FillColor = Color(0, 0, 0, 0);
  39. m_DrawColor = Color(255,255,255,255);
  40. m_iRotation = ROTATED_UNROTATED;
  41. m_bFrameImage = false;
  42. m_iFrame = 0;
  43. SetImage( m_pImage );
  44. REGISTER_COLOR_AS_OVERRIDABLE( m_FillColor, "fillcolor_override" );
  45. REGISTER_COLOR_AS_OVERRIDABLE( m_DrawColor, "drawcolor_override" );
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose:
  49. //-----------------------------------------------------------------------------
  50. ImagePanel::~ImagePanel()
  51. {
  52. delete [] m_pszImageName;
  53. delete [] m_pszFillColorName;
  54. delete [] m_pszDrawColorName;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose: handles size changing
  58. //-----------------------------------------------------------------------------
  59. void ImagePanel::OnSizeChanged(int newWide, int newTall)
  60. {
  61. BaseClass::OnSizeChanged(newWide, newTall);
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose:
  65. //-----------------------------------------------------------------------------
  66. void ImagePanel::SetImage(IImage *image)
  67. {
  68. m_pImage = image;
  69. Repaint();
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose: sets an image by file name
  73. //-----------------------------------------------------------------------------
  74. void ImagePanel::SetImage(const char *imageName)
  75. {
  76. if ( imageName && m_pszImageName && V_stricmp( imageName, m_pszImageName ) == 0 )
  77. return;
  78. int len = Q_strlen(imageName) + 1;
  79. delete [] m_pszImageName;
  80. m_pszImageName = new char[ len ];
  81. Q_strncpy(m_pszImageName, imageName, len );
  82. InvalidateLayout(false, true); // forrce applyschemesettings to run
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose:
  86. //-----------------------------------------------------------------------------
  87. IImage *ImagePanel::GetImage()
  88. {
  89. return m_pImage;
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Purpose:
  93. //-----------------------------------------------------------------------------
  94. Color ImagePanel::GetDrawColor( void )
  95. {
  96. return m_DrawColor;
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose:
  100. //-----------------------------------------------------------------------------
  101. void ImagePanel::SetDrawColor( Color drawColor )
  102. {
  103. m_DrawColor = drawColor;
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Purpose:
  107. //-----------------------------------------------------------------------------
  108. void ImagePanel::PaintBackground()
  109. {
  110. if (m_FillColor[3] > 0)
  111. {
  112. // draw the specified fill color
  113. int wide, tall;
  114. GetSize(wide, tall);
  115. surface()->DrawSetColor(m_FillColor);
  116. surface()->DrawFilledRect(0, 0, wide, tall);
  117. }
  118. if (m_pImage)
  119. {
  120. m_pImage->SetColor( GetDrawColor() );
  121. m_pImage->SetPos(0, 0);
  122. m_pImage->SetRotation( m_iRotation );
  123. if ( m_bFrameImage )
  124. m_pImage->SetFrame( m_iFrame );
  125. if (m_bScaleImage)
  126. {
  127. // Image size is stored in the bitmap, so temporarily set its size
  128. // to our panel size and then restore after we draw it.
  129. int imageWide, imageTall;
  130. m_pImage->GetSize( imageWide, imageTall );
  131. if ( m_fScaleAmount > 0.0f )
  132. {
  133. float wide, tall;
  134. wide = static_cast<float>(imageWide) * m_fScaleAmount;
  135. tall = static_cast<float>(imageTall) * m_fScaleAmount;
  136. m_pImage->SetSize( static_cast<int>(wide), static_cast<int>(tall) );
  137. }
  138. else
  139. {
  140. int wide, tall;
  141. GetSize( wide, tall );
  142. m_pImage->SetSize( wide, tall );
  143. }
  144. m_pImage->Paint();
  145. m_pImage->SetSize( imageWide, imageTall );
  146. }
  147. else if ( m_bTileImage || m_bTileHorizontally || m_bTileVertically )
  148. {
  149. int wide, tall;
  150. GetSize(wide, tall);
  151. int imageWide, imageTall;
  152. m_pImage->GetSize( imageWide, imageTall );
  153. bool bUseAbsolute = false;
  154. if ( m_flTileHorizontalOverride || m_flTileVerticalOverride )
  155. {
  156. bUseAbsolute = true;
  157. m_pImage->SetSize( m_flTileHorizontalOverride ? m_flTileHorizontalOverride : imageWide, m_flTileVerticalOverride ? m_flTileVerticalOverride : imageTall );
  158. }
  159. int y = 0;
  160. int iStepY = 0;
  161. while ( y < tall )
  162. {
  163. int x = 0;
  164. int iStepX = 0;
  165. while (x < wide)
  166. {
  167. if ( bUseAbsolute )
  168. {
  169. if ( m_flTileHorizontalOverride )
  170. {
  171. x = (m_flTileHorizontalOverride * iStepX);
  172. }
  173. if ( m_flTileVerticalOverride )
  174. {
  175. y = (m_flTileVerticalOverride * iStepY);
  176. }
  177. iStepX++;
  178. }
  179. m_pImage->SetPos(x,y);
  180. m_pImage->Paint();
  181. x += imageWide;
  182. if ( !m_bTileHorizontally )
  183. break;
  184. }
  185. y += imageTall;
  186. iStepY++;
  187. if ( !m_bTileVertically )
  188. break;
  189. }
  190. m_pImage->SetPos(0, 0);
  191. }
  192. else
  193. {
  194. m_pImage->SetColor( GetDrawColor() );
  195. m_pImage->Paint();
  196. }
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Purpose: Gets control settings for editing
  201. //-----------------------------------------------------------------------------
  202. void ImagePanel::GetSettings(KeyValues *outResourceData)
  203. {
  204. BaseClass::GetSettings(outResourceData);
  205. if (m_pszImageName)
  206. {
  207. outResourceData->SetString("image", m_pszImageName);
  208. }
  209. if (m_pszFillColorName)
  210. {
  211. outResourceData->SetString("fillcolor", m_pszFillColorName);
  212. }
  213. if (m_pszDrawColorName)
  214. {
  215. outResourceData->SetString("drawcolor", m_pszDrawColorName);
  216. }
  217. if (GetBorder())
  218. {
  219. outResourceData->SetString("border", GetBorder()->GetName());
  220. }
  221. outResourceData->SetBool("scaleImage", m_bScaleImage);
  222. outResourceData->SetFloat("scaleAmount", m_fScaleAmount);
  223. outResourceData->SetBool("tileImage", m_bTileImage);
  224. outResourceData->SetBool("tileHorizontally", m_bTileHorizontally);
  225. outResourceData->SetBool("tileVertically", m_bTileVertically);
  226. outResourceData->SetInt("tileHorizontalOverride", m_iTileHorizontalOverride);
  227. outResourceData->SetInt("tileVerticalOverride", m_iTileVerticalOverride);
  228. }
  229. //-----------------------------------------------------------------------------
  230. // Purpose: Applies designer settings from res file
  231. //-----------------------------------------------------------------------------
  232. void ImagePanel::ApplySettings(KeyValues *inResourceData)
  233. {
  234. delete [] m_pszImageName;
  235. delete [] m_pszFillColorName;
  236. delete [] m_pszDrawColorName;
  237. m_pszImageName = NULL;
  238. m_pszFillColorName = NULL;
  239. m_pszDrawColorName = NULL;
  240. m_bScaleImage = inResourceData->GetBool("scaleImage", false);
  241. m_fScaleAmount = inResourceData->GetFloat("scaleAmount", 0.0f);
  242. m_bTileImage = inResourceData->GetBool("tileImage", false);
  243. m_bTileHorizontally = inResourceData->GetBool("tileHorizontally", m_bTileImage);
  244. m_bTileVertically = inResourceData->GetBool("tileVertically", m_bTileImage);
  245. m_flTileHorizontalOverride = m_iTileHorizontalOverride = inResourceData->GetInt("tileHorizontalOverride", 0);
  246. m_flTileVerticalOverride = m_iTileVerticalOverride = inResourceData->GetInt("tileVerticalOverride", 0);
  247. if ( IsProportional() )
  248. {
  249. int screenwide, screentall;
  250. vgui::surface()->GetScreenSize( screenwide, screentall );
  251. int proH, proW;
  252. vgui::surface()->GetProportionalBase( proW, proH );
  253. double scale = (double)screentall / (double)proH;
  254. m_flTileHorizontalOverride = m_iTileHorizontalOverride * scale;
  255. m_flTileVerticalOverride = m_iTileVerticalOverride * scale;
  256. }
  257. const char *imageName = inResourceData->GetString("image", "");
  258. if ( *imageName )
  259. {
  260. SetImage( imageName );
  261. }
  262. if ( KeyValues *pKeyFrame = inResourceData->FindKey( "frame" ) )
  263. {
  264. m_iFrame = inResourceData->GetInt( "frame", m_iFrame );
  265. m_bFrameImage = true;
  266. }
  267. const char *pszFillColor = inResourceData->GetString("fillcolor", "");
  268. if (*pszFillColor)
  269. {
  270. int r = 0, g = 0, b = 0, a = 255;
  271. int len = Q_strlen(pszFillColor) + 1;
  272. m_pszFillColorName = new char[ len ];
  273. Q_strncpy( m_pszFillColorName, pszFillColor, len );
  274. if (sscanf(pszFillColor, "%d %d %d %d", &r, &g, &b, &a) >= 3)
  275. {
  276. // it's a direct color
  277. m_FillColor = Color(r, g, b, a);
  278. }
  279. else
  280. {
  281. IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
  282. m_FillColor = pScheme->GetColor(pszFillColor, Color(0, 0, 0, 0));
  283. }
  284. }
  285. const char *pszDrawColor = inResourceData->GetString("drawcolor", "");
  286. if (*pszDrawColor)
  287. {
  288. int r = 0, g = 0, b = 0, a = 255;
  289. int len = Q_strlen(pszDrawColor) + 1;
  290. m_pszDrawColorName = new char[ len ];
  291. Q_strncpy( m_pszDrawColorName, pszDrawColor, len );
  292. if (sscanf(pszDrawColor, "%d %d %d %d", &r, &g, &b, &a) >= 3)
  293. {
  294. // it's a direct color
  295. m_DrawColor = Color(r, g, b, a);
  296. }
  297. else
  298. {
  299. IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
  300. m_DrawColor = pScheme->GetColor(pszDrawColor, Color(0, 0, 0, 0));
  301. }
  302. }
  303. const char *pszBorder = inResourceData->GetString("border", "");
  304. if (*pszBorder)
  305. {
  306. IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
  307. SetBorder(pScheme->GetBorder(pszBorder));
  308. }
  309. BaseClass::ApplySettings(inResourceData);
  310. }
  311. //-----------------------------------------------------------------------------
  312. // Purpose: load the image, this is done just before this control is displayed
  313. //-----------------------------------------------------------------------------
  314. void ImagePanel::ApplySchemeSettings( IScheme *pScheme )
  315. {
  316. BaseClass::ApplySchemeSettings(pScheme);
  317. if ( m_pszImageName && strlen( m_pszImageName ) > 0 )
  318. {
  319. SetImage(scheme()->GetImage(m_pszImageName, m_bScaleImage));
  320. }
  321. }
  322. //-----------------------------------------------------------------------------
  323. // Purpose: Describes editing details
  324. //-----------------------------------------------------------------------------
  325. const char *ImagePanel::GetDescription()
  326. {
  327. static char buf[1024];
  328. _snprintf(buf, sizeof(buf), "%s, string image, string border, string fillcolor, bool scaleImage", BaseClass::GetDescription());
  329. return buf;
  330. }
  331. //-----------------------------------------------------------------------------
  332. // Purpose: sets whether or not the image should scale to fit the size of the ImagePanel (defaults to false)
  333. //-----------------------------------------------------------------------------
  334. void ImagePanel::SetShouldScaleImage( bool state )
  335. {
  336. m_bScaleImage = state;
  337. }
  338. //-----------------------------------------------------------------------------
  339. // Purpose: gets whether or not the image should be scaled to fit the size of the ImagePanel
  340. //-----------------------------------------------------------------------------
  341. bool ImagePanel::GetShouldScaleImage()
  342. {
  343. return m_bScaleImage;
  344. }
  345. //-----------------------------------------------------------------------------
  346. // Purpose: used in conjunction with setting that the image should scale and defines an absolute scale amount
  347. //-----------------------------------------------------------------------------
  348. void ImagePanel::SetScaleAmount( float scale )
  349. {
  350. m_fScaleAmount = scale;
  351. }
  352. float ImagePanel::GetScaleAmount( void )
  353. {
  354. return m_fScaleAmount;
  355. }
  356. //-----------------------------------------------------------------------------
  357. // Purpose: set the color to fill with, if no Image is specified
  358. //-----------------------------------------------------------------------------
  359. void ImagePanel::SetFillColor( Color col )
  360. {
  361. m_FillColor = col;
  362. }
  363. //-----------------------------------------------------------------------------
  364. // Purpose: data accessor
  365. //-----------------------------------------------------------------------------
  366. Color ImagePanel::GetFillColor()
  367. {
  368. return m_FillColor;
  369. }
  370. char *ImagePanel::GetImageName()
  371. {
  372. return m_pszImageName;
  373. }
  374. bool ImagePanel::EvictImage()
  375. {
  376. if ( !m_pImage )
  377. {
  378. // nothing to do
  379. return false;
  380. }
  381. if ( !scheme()->DeleteImage( m_pszImageName ) )
  382. {
  383. // no eviction occured, could have an outstanding reference
  384. return false;
  385. }
  386. // clear out our cached concept of it
  387. // as it may change
  388. // the next SetImage() will re-establish
  389. m_pImage = NULL;
  390. delete [] m_pszImageName;
  391. m_pszImageName = NULL;
  392. return true;
  393. }
  394. int ImagePanel::GetNumFrames()
  395. {
  396. if ( !m_pImage )
  397. {
  398. return 0;
  399. }
  400. return m_pImage->GetNumFrames();
  401. }
  402. void ImagePanel::SetFrame( int nFrame )
  403. {
  404. m_bFrameImage = true;
  405. m_iFrame = nFrame;
  406. }
  407. int ImagePanel::GetFrame() const
  408. {
  409. return m_iFrame;
  410. }