Guest User

Untitled

a guest
Nov 1st, 2016
12
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local PANEL = {}
  2.  
  3. function PANEL:DrawHexagon(w, h)
  4.   local tbl = {};
  5.  
  6.   if (self.avatar.direction) then
  7.     tbl = {
  8.       { x = w/2, y = 0 },
  9.       { x = w, y = h/2 - h/4 },
  10.       { x = w, y = h/2 + h/4 },
  11.       { x = w/2, y = h },
  12.       { x = 0, y = h/2 + h/4 },
  13.       { x = 0, y = h/2 - h/4 }
  14.     }
  15.   else
  16.     tbl = {
  17.       { x = w/2 - w/4, y = 0 },
  18.       { x = w/2 + w/4, y = 0 },
  19.       { x = w, y = h/2 },
  20.       { x = w/2 + w/4, y = h },
  21.       { x = w/2 - w/4, y = h },
  22.       { x = 0, y = h/2 }
  23.     }
  24.   end
  25.  
  26.   surface.DrawPoly(tbl);
  27. end
  28.  
  29. function PANEL:DrawTriangle(w, h)
  30.   local tbl = {}
  31.  
  32.   if (self.avatar.direction) then
  33.     tbl = {
  34.       { x = 0, y = 0 },
  35.       { x = w, y = 0 },
  36.       { x = w/2, y = h }
  37.     }
  38.   else
  39.     tbl = {
  40.       { x = w/2, y = 0 },
  41.       { x = w, y = h },
  42.       { x = 0, y = h }
  43.     }
  44.   end
  45.  
  46.   surface.DrawPoly(tbl)
  47. end
  48.  
  49. function PANEL:DrawCircle(w, h)
  50.   local x = w/2;
  51.   local y = h/2;
  52.   local radius = h/2;
  53.   local seg = 360;
  54.  
  55.   local cir = {}
  56.     table.insert( cir, { x = x, y = y } )
  57.     for i = 0, seg do
  58.         local a = math.rad( ( i / seg ) * -360 )
  59.         table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius } )
  60.     end
  61.     local a = math.rad( 0 )
  62.     table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius } )
  63.     surface.DrawPoly( cir )
  64. end
  65.  
  66. function PANEL:DrawSquare(w, h)
  67.   local tbl = {
  68.     { x = 0, y = 0 },
  69.     { x = w, y = 0 },
  70.     { x = w, y = h },
  71.     { x = 0, y = h }
  72.   }
  73.  
  74.   surface.DrawPoly(tbl);
  75. end
  76.  
  77. function PANEL:DrawPentagon(w, h)
  78.   local tbl = {
  79.     { x = w/2, y = 0 },
  80.     { x = w, y = h * 0.4 },
  81.     { x = w - w/5, y = h },
  82.     { x = 0 + w/5, y = h },
  83.     { x = 0, y = h * 0.4 }
  84.   }
  85.  
  86.   surface.DrawPoly(tbl);
  87. end
  88.  
  89.  
  90. function PANEL:Init()
  91.   self.avatar = vgui.Create( "AvatarImage", self )
  92.   self.avatar:SetPaintedManually( true )
  93.   self.avatar.type = 4; -- default to square
  94.   self.avatar.direction = false;
  95. end
  96.  
  97. function PANEL:PerformLayout()
  98.   self.avatar:SetSize( self:GetWide(), self:GetTall() )
  99. end
  100.  
  101. function PANEL:SetPlayer( ply, size )
  102.   self.avatar:SetPlayer( ply, size )
  103. end
  104.  
  105. function PANEL:SetType(type, direction)
  106.   self.avatar.type = type;
  107.   self.avatar.direction = direction;
  108. end
  109.  
  110. function PANEL:GetType()
  111.   local type = self.avatar.type;
  112.   local direction = self.avatar.direction;
  113.  
  114.   return type, direction;
  115. end
  116.  
  117. function PANEL:DrawPolygon(w, h)
  118.   local type, direction = self:GetType();
  119.  
  120.   if (type == 1) then self:DrawHexagon(w, h); end
  121.   if (type == 2) then self:DrawTriangle(w, h); end
  122.   if (type == 3) then self:DrawCircle(w, h); end
  123.   if (type == 4) then self:DrawSquare(w, h); end
  124.   if (type == 5) then self:DrawPentagon(w, h); end
  125. end
  126.  
  127. function PANEL:Paint( w, h )
  128.   render.ClearStencil()
  129.   render.SetStencilEnable( true )
  130.  
  131.   render.SetStencilWriteMask( 1 )
  132.   render.SetStencilTestMask( 1 )
  133.  
  134.   render.SetStencilFailOperation( STENCILOPERATION_REPLACE )
  135.   render.SetStencilPassOperation( STENCILOPERATION_ZERO )
  136.   render.SetStencilZFailOperation( STENCILOPERATION_ZERO )
  137.   render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_NEVER )
  138.   render.SetStencilReferenceValue( 1 )
  139.  
  140.   draw.NoTexture()
  141.   surface.SetDrawColor(color_white)
  142.   self:DrawPolygon(w, h);
  143.  
  144.   render.SetStencilFailOperation( STENCILOPERATION_ZERO )
  145.   render.SetStencilPassOperation( STENCILOPERATION_REPLACE )
  146.   render.SetStencilZFailOperation( STENCILOPERATION_ZERO )
  147.   render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_EQUAL )
  148.   render.SetStencilReferenceValue( 1 )
  149.  
  150.   self.avatar:PaintManual()
  151.  
  152.   render.SetStencilEnable( false )
  153.   render.ClearStencil()
  154. end
  155. vgui.Register( "EnhancedAvatarImage", PANEL )
RAW Paste Data