234 lines
5.7 KiB
Lua
234 lines
5.7 KiB
Lua
local PANEL = {}
|
|
|
|
AccessorFunc( PANEL, "m_fAnimSpeed", "AnimSpeed" )
|
|
AccessorFunc( PANEL, "Entity", "Entity" )
|
|
AccessorFunc( PANEL, "vCamPos", "CamPos" )
|
|
AccessorFunc( PANEL, "fFOV", "FOV" )
|
|
AccessorFunc( PANEL, "vLookatPos", "LookAt" )
|
|
AccessorFunc( PANEL, "aLookAngle", "LookAng" )
|
|
AccessorFunc( PANEL, "colAmbientLight", "AmbientLight" )
|
|
AccessorFunc( PANEL, "colColor", "Color" )
|
|
AccessorFunc( PANEL, "bAnimated", "Animated" )
|
|
|
|
function PANEL:Init()
|
|
|
|
self.Entity = nil
|
|
self.SubEntitys = {}
|
|
self.LastPaint = 0
|
|
self.DirectionalLight = {}
|
|
self.FarZ = 4096
|
|
self.Angles = Angle(0, 0, 0)
|
|
|
|
self:SetCamPos( Vector(0, 0, 0) )
|
|
self:SetLookAt( Vector(-100, 0, -22) )
|
|
self:SetFOV( 50 )
|
|
|
|
self:SetText( "" )
|
|
self:SetAnimSpeed( 0.5 )
|
|
self:SetAnimated( true )
|
|
|
|
self:SetAmbientLight( Color( 50, 50, 50 ) )
|
|
|
|
self:SetDirectionalLight( BOX_TOP, Color( 255, 255, 255 ) )
|
|
self:SetDirectionalLight( BOX_FRONT, Color( 255, 255, 255 ) )
|
|
|
|
self:SetDirectionalLight(BOX_RIGHT, Color(255, 160, 80, 255))
|
|
self:SetDirectionalLight(BOX_LEFT, Color(80, 160, 255, 255))
|
|
|
|
self:SetColor( Color( 255, 255, 255, 255 ) )
|
|
|
|
end
|
|
|
|
function PANEL:SetDirectionalLight( iDirection, color )
|
|
self.DirectionalLight[ iDirection ] = color
|
|
end
|
|
|
|
function PANEL:SetModel( strModelName )
|
|
if ( IsValid( self.Entity ) ) then
|
|
self.Entity:Remove()
|
|
self.Entity = nil
|
|
end
|
|
|
|
if ( !ClientsideModel ) then return end
|
|
|
|
self.Entity = ClientsideModel( strModelName, RENDERGROUP_OTHER )
|
|
if ( !IsValid( self.Entity ) ) then return end
|
|
|
|
self.Entity:SetNoDraw( true )
|
|
self.Entity:SetIK( false )
|
|
|
|
local iSeq = self.Entity:LookupSequence( "walk_all" )
|
|
if ( iSeq <= 0 ) then iSeq = self.Entity:LookupSequence( "WalkUnarmed_all" ) end
|
|
if ( iSeq <= 0 ) then iSeq = self.Entity:LookupSequence( "walk_all_moderate" ) end
|
|
|
|
if ( iSeq > 0 ) then self.Entity:ResetSequence( iSeq ) end
|
|
|
|
end
|
|
|
|
function PANEL:GetModel()
|
|
|
|
if ( !IsValid( self.Entity ) ) then return end
|
|
|
|
return self.Entity:GetModel()
|
|
|
|
end
|
|
|
|
function PANEL:DrawModel()
|
|
|
|
local curparent = self
|
|
local leftx, topy = self:LocalToScreen( 0, 0 )
|
|
local rightx, bottomy = self:LocalToScreen( self:GetWide(), self:GetTall() )
|
|
while ( curparent:GetParent() != nil ) do
|
|
curparent = curparent:GetParent()
|
|
|
|
local x1, y1 = curparent:LocalToScreen( 0, 0 )
|
|
local x2, y2 = curparent:LocalToScreen( curparent:GetWide(), curparent:GetTall() )
|
|
|
|
leftx = math.max( leftx, x1 )
|
|
topy = math.max( topy, y1 )
|
|
rightx = math.min( rightx, x2 )
|
|
bottomy = math.min( bottomy, y2 )
|
|
previous = curparent
|
|
end
|
|
|
|
render.SetScissorRect( leftx, topy, rightx, bottomy, true )
|
|
|
|
local ret = self:PreDrawModel( self.Entity )
|
|
if ( ret != false ) then
|
|
self.Entity:DrawModel()
|
|
|
|
for k,v in pairs(self.SubEntitys) do
|
|
if IsValid(v) then
|
|
v:DrawModel()
|
|
if v.bone and isnumber(v.bone) then
|
|
local pos, ang = MCS.GetBoneOrientation(self.Entity, v.bone, v.pos, v.ang)
|
|
ang:RotateAroundAxis(ang:Up(), v.ang.y)
|
|
ang:RotateAroundAxis(ang:Right(), v.ang.p)
|
|
ang:RotateAroundAxis(ang:Forward(), v.ang.r)
|
|
v:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z)
|
|
v:SetAngles(ang)
|
|
end
|
|
end
|
|
end
|
|
|
|
self:PostDrawModel( self.Entity )
|
|
end
|
|
|
|
render.SetScissorRect( 0, 0, 0, 0, false )
|
|
|
|
end
|
|
|
|
function PANEL:PreDrawModel( ent )
|
|
return true
|
|
end
|
|
|
|
function PANEL:PostDrawModel( ent )
|
|
|
|
end
|
|
|
|
function PANEL:DragMousePress()
|
|
self.PressX, self.PressY = gui.MousePos()
|
|
self.Pressed = true
|
|
end
|
|
|
|
function PANEL:DoDoubleClick()
|
|
if self:GetFOV() < 10 then
|
|
self:SetFOV(50)
|
|
else
|
|
self:SetFOV(self:GetFOV() - 5)
|
|
end
|
|
end
|
|
|
|
function PANEL:DragMouseRelease()
|
|
self.Pressed = false
|
|
end
|
|
|
|
function PANEL:Paint( w, h )
|
|
|
|
if ( !IsValid( self.Entity ) ) then return end
|
|
|
|
local x, y = self:LocalToScreen( 0, 0 )
|
|
|
|
self:LayoutEntity( self.Entity )
|
|
|
|
local ang = self.aLookAngle
|
|
if ( !ang ) then
|
|
ang = ( self.vLookatPos - self.vCamPos ):Angle()
|
|
end
|
|
|
|
cam.Start3D( self.vCamPos, ang, self.fFOV, x, y, w, h, 5, self.FarZ )
|
|
|
|
render.SuppressEngineLighting( true )
|
|
render.SetLightingOrigin( self.Entity:GetPos() )
|
|
render.ResetModelLighting( self.colAmbientLight.r / 255, self.colAmbientLight.g / 255, self.colAmbientLight.b / 255 )
|
|
render.SetColorModulation( self.colColor.r / 255, self.colColor.g / 255, self.colColor.b / 255 )
|
|
render.SetBlend( ( self:GetAlpha() / 255 ) * ( self.colColor.a / 255 ) ) -- * surface.GetAlphaMultiplier()
|
|
|
|
for i = 0, 6 do
|
|
local col = self.DirectionalLight[ i ]
|
|
if ( col ) then
|
|
render.SetModelLighting( i, col.r / 255, col.g / 255, col.b / 255 )
|
|
end
|
|
end
|
|
|
|
self:DrawModel()
|
|
|
|
render.SuppressEngineLighting( false )
|
|
cam.End3D()
|
|
|
|
self.LastPaint = RealTime()
|
|
|
|
end
|
|
|
|
function PANEL:RunAnimation()
|
|
self.Entity:FrameAdvance( ( RealTime() - self.LastPaint ) * self.m_fAnimSpeed )
|
|
end
|
|
|
|
function PANEL:StartScene( name )
|
|
|
|
if ( IsValid( self.Scene ) ) then
|
|
self.Scene:Remove()
|
|
end
|
|
|
|
self.Scene = ClientsideScene( name, self.Entity )
|
|
end
|
|
|
|
function PANEL:LayoutEntity(ent)
|
|
if (self.bAnimated) then
|
|
self:RunAnimation()
|
|
end
|
|
|
|
if (self.Pressed) then
|
|
local mx = gui.MousePos()
|
|
self.Angles = self.Angles - Angle(0, (self.PressX or mx) - mx, 0)
|
|
self.PressX, self.PressY = gui.MousePos()
|
|
end
|
|
|
|
ent:SetAngles(self.Angles)
|
|
end
|
|
|
|
function PANEL:OnRemove()
|
|
if ( IsValid( self.Entity ) ) then
|
|
self.Entity:Remove()
|
|
end
|
|
|
|
for k,v in pairs(self.SubEntitys) do
|
|
if IsValid(v) then
|
|
v:Remove()
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )
|
|
|
|
local ctrl = vgui.Create( ClassName )
|
|
ctrl:SetSize( 300, 300 )
|
|
ctrl:SetModel( "models/props_junk/PlasticCrate01a.mdl" )
|
|
ctrl:GetEntity():SetSkin( 2 )
|
|
|
|
PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )
|
|
|
|
end
|
|
|
|
derma.DefineControl( "MSDModelPanel", "A panel containing a model but more epic", PANEL, "DButton" ) |