aboutsummaryrefslogtreecommitdiff
path: root/gamemode/client/vgui/vgui_DMultiModelPanel.lua
blob: 37d08833057bd9a4383c417923aacdfd880d26a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
local PANEL = {}

AccessorFunc( PANEL, "m_fAnimSpeed",	"AnimSpeed" )
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.Entities = {}
	self.LastPaint = 0
	self.DirectionalLight = {}
	self.FarZ = 4096

	self:SetCamPos( Vector( 50, 50, 50 ) )
	self:SetLookAt( Vector( 0, 0, 40 ) )
	self:SetFOV( 70 )

	self:SetText( "" )
	self:SetAnimSpeed( 0.5 )
	self:SetAnimated( false )

	self:SetAmbientLight( Color( 50, 50, 50 ) )

	self:SetDirectionalLight( BOX_TOP, Color( 255, 255, 255 ) )
	self:SetDirectionalLight( BOX_FRONT, Color( 255, 255, 255 ) )

	self:SetColor( Color( 255, 255, 255, 255 ) )

end

function PANEL:SetDirectionalLight( iDirection, color )
	self.DirectionalLight[ iDirection ] = color
end

function PANEL:AddModel( strModelName )

	-- Note: Not in menu dll
	if ( !ClientsideModel ) then return end

    self.Entities[#self.Entities+1] = ClientsideModel(strModelName, RENDER_GROUP_OPAQUE_ENTITY)
	if ( !IsValid( self.Entities[#self.Entities] ) ) then return end

	self.Entities[#self.Entities]:SetNoDraw( true )
	self.Entities[#self.Entities]:SetIK( false )

end

function PANEL:DrawModel()

	local curparent = self
	local rightx = self:GetWide()
	local leftx = 0
	local topy = 0
	local bottomy = self:GetTall()
	local previous = curparent
	while( curparent:GetParent() != nil ) do
		curparent = curparent:GetParent()
		local x, y = previous:GetPos()
		topy = math.Max( y, topy + y )
		leftx = math.Max( x, leftx + x )
		bottomy = math.Min( y + previous:GetTall(), bottomy + y )
		rightx = math.Min( x + previous:GetWide(), rightx + x )
		previous = curparent
	end
	render.SetScissorRect( leftx, topy, rightx, bottomy, true )

    for k,v in pairs(self.Entities) do
    	local ret = self:PreDrawModel( v )
    	if ( ret != false ) then
    		v:DrawModel()
    		self:PostDrawModel( v )
    	end
    end

	render.SetScissorRect( 0, 0, 0, 0, false )

end

function PANEL:PreDrawModel( ent )
	return true
end

function PANEL:PostDrawModel( ent )

end

function PANEL:Paint( w, h )
    for k,v in pairs(self.Entities) do
    	if ( !IsValid( v ) ) then continue end

    	local x, y = self:LocalToScreen( 0, 0 )

    	self:LayoutEntity( v )

    	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( v: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 ) )

    	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
end

--[[
function PANEL:StartScene( name )

	if ( IsValid( self.Scene ) ) then
		self.Scene:Remove()
	end

	self.Scene = ClientsideScene( name, self.Entity )

end
]]

function PANEL:LayoutEntity( Entity )

	--
	-- This function is to be overriden
	--

	if ( self.bAnimated ) then
		self:RunAnimation()
	end
	Entity:SetAngles( Angle( 0, RealTime() * 10 % 360, 0 ) )

end

function PANEL:OnRemove()
	for k,v in pairs(self.Entities) do
        v:Remove()
        self.Entities[k] = nil
    end
end

derma.DefineControl( "DModelPanel", "A panel containing a model", PANEL, "DModelPanel" )