Team Fortress 2 Source Code as on 22/4/2020
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.

95 lines
2.9 KiB

  1. #include "cbase.h"
  2. #include "softline.h"
  3. #include <KeyValues.h>
  4. // memdbgon must be the last include file in a .cpp file!!!
  5. #include <tier0/memdbgon.h>
  6. SoftLine::SoftLine(vgui::Panel *parent, const char *panelName, Color col) :
  7. vgui::Panel(parent, panelName)
  8. {
  9. m_Color = col;
  10. m_iCornerType = 0;
  11. }
  12. void SoftLine::Paint()
  13. {
  14. if (m_iCornerType == 1)
  15. DrawSoftLine(1,GetTall() - 2, GetWide() - 2, 1, m_Color);
  16. else
  17. DrawSoftLine(1,1, GetWide() - 2, GetTall() - 2, m_Color);
  18. }
  19. int SoftLine::s_nWhiteTexture = -1;
  20. void SoftLine::DrawSoftLine(float x, float y, float x2, float y2, Color c)
  21. {
  22. vgui::Vertex_t start, end;
  23. if (s_nWhiteTexture == -1)
  24. {
  25. s_nWhiteTexture = vgui::surface()->CreateNewTextureID();
  26. vgui::surface()->DrawSetTextureFile( s_nWhiteTexture, "vgui/white" , true, false);
  27. if (s_nWhiteTexture == -1)
  28. return;
  29. return;
  30. }
  31. // draw main line
  32. vgui::surface()->DrawSetTexture(s_nWhiteTexture);
  33. vgui::surface()->DrawSetColor(c);
  34. //vgui::surface()->DrawLine(x,y,x2,y2);
  35. start.Init(Vector2D(x,y), Vector2D(0,0));
  36. end.Init(Vector2D(x2,y2), Vector2D(1,1));
  37. DrawPolygonLine(start, end);
  38. // draw translucent ones around it to give it some softness
  39. vgui::surface()->DrawSetColor(c);
  40. start.Init(Vector2D(x - 0.50f,y - 0.50f), Vector2D(0,0));
  41. end.Init(Vector2D(x2 - 0.50f,y2 - 0.50f), Vector2D(1,1));
  42. DrawPolygonLine(start, end);
  43. start.Init(Vector2D(x + 0.50f,y - 0.50f), Vector2D(0,0));
  44. end.Init(Vector2D(x2 + 0.50f,y2 - 0.50f), Vector2D(1,1));
  45. DrawPolygonLine(start, end);
  46. start.Init(Vector2D(x - 0.50f,y + 0.50f), Vector2D(0,0));
  47. end.Init(Vector2D(x2 - 0.50f,y2 + 0.50f), Vector2D(1,1));
  48. DrawPolygonLine(start, end);
  49. start.Init(Vector2D(x + 0.50f,y + 0.50f), Vector2D(0,0));
  50. end.Init(Vector2D(x2 + 0.50f,y2 + 0.50f), Vector2D(1,1));
  51. DrawPolygonLine(start, end);
  52. }
  53. // draws a line using polygon calls
  54. void SoftLine::DrawPolygonLine(vgui::Vertex_t start, vgui::Vertex_t end, float width)
  55. {
  56. DrawPolygonLine(start.m_Position.x, start.m_Position.y, end.m_Position.x, end.m_Position.y, width);
  57. }
  58. void SoftLine::DrawPolygonLine(float x, float y, float x2, float y2, float width)
  59. {
  60. // find long edge
  61. Vector2D start(x, y);
  62. Vector2D end(x2, y2);
  63. Vector2D long_edge = end - start;
  64. // normalize and rotate 90 degrees to get our short edge
  65. Vector2D short_edge = long_edge;
  66. short_edge.NormalizeInPlace();
  67. float newx = cos(1.5708f) * short_edge.x - sin(1.5708f) * short_edge.y;
  68. float newy = sin(1.5708f) * short_edge.x - cos(1.5708f) * short_edge.y;
  69. short_edge.x = newx;
  70. short_edge.y = newy;
  71. short_edge *= width;
  72. vgui::Vertex_t points[4] =
  73. {
  74. vgui::Vertex_t( Vector2D(x, y) - short_edge * 0.5f, Vector2D(0,0) ),
  75. vgui::Vertex_t( Vector2D(x, y) - short_edge * 0.5f + long_edge, Vector2D(1,0) ),
  76. vgui::Vertex_t( Vector2D(x, y) + short_edge * 0.5f + long_edge, Vector2D(1,1) ),
  77. vgui::Vertex_t( Vector2D(x, y) + short_edge * 0.5f, Vector2D(0,1) )
  78. };
  79. vgui::surface()->DrawTexturedPolygon(4, points);
  80. }