summaryrefslogtreecommitdiff
path: root/gamemode/vgui/vgui_goodmodelpanel.lua
blob: b61c96f61254328e343b2a6c9db4ad3812f418d6 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*   _                                
    ( )
   _| |   __   _ __   ___ ___     _ _
 /'_` | /'__`\( '__)/' _ ` _ `\ /'_` )
( (_| |(  ___/| |   | ( ) ( ) |( (_| |
`\__,_)`\____)(_)   (_) (_) (_)`\__,_) sucks shit

*/
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, "colAmbientLight", "AmbientLight" )
AccessorFunc( PANEL, "colColor", 		"Color" )
AccessorFunc( PANEL, "bAnimated", 		"Animated" )

function PANEL:Init()

	self.Entity = nil
	self.NextModel = ""
	self.StopRender = false
	self.NextSetModel = nil
	self.LastPaint = 0
	self.DirectionalLight = {}

	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:SetModel( model )

	if self.NextModel == model then return end

	self.NextModel = model
	self.StopRender = true
	self.NextSetModel = CurTime() + 0.01

end

function PANEL:Think()

	if self.NextSetModel and self.NextSetModel < CurTime() then

		self:SpecialSetModel( self.NextModel )
		self.StopRender = false
		self.NextSetModel = nil

	end

end

function PANEL:SpecialSetModel( model )

	if IsValid( self.Entity ) then

		self.Entity:Remove()
		self.Entity = nil

	end

	if not ClientsideModel then return end

	self.LastModel = model
	self.Entity = ClientsideModel( model, RENDERGROUP_OPAQUE )

	if not IsValid( self.Entity ) then return end

	self.Entity:SetNoDraw( true )

	local seq = self:ChooseSequence( model )

	if seq > 0 then self.Entity:ResetSequence( seq ) end

end

function PANEL:GetSequenceList()

	return { "walk_all", "WalkUnarmed_all", "walk_all_moderate", "idle" }

end

function PANEL:ExcludedModels()

	return { "player", "models/human", "eli", "police" }

end

function PANEL:ChooseSequence( model )

	local seq = 0

	for k,v in pairs( self:ExcludedModels() ) do

		if string.find( model, v ) then

			return self.Entity:LookupSequence( "idle" )

		end

	end

	for k,v in pairs( self:GetSequenceList() ) do

		if seq <= 0 then seq = self.Entity:LookupSequence( v ) end

	end

	return seq

end

function PANEL:Paint()

	if ( !IsValid( self.Entity ) ) then return end
	if self.StopRender then return end

	local x, y = self:LocalToScreen( 0, 0 )
	local w, h = self:GetSize()

    local sl, st, sr, sb = x, y, x + w, y + h

    local p = self

    while p:GetParent() do

        p = p:GetParent()

        local pl, pt = p:LocalToScreen( 0, 0 )
        local pr, pb = pl + p:GetWide(), pt + p:GetTall()

        sl = sl < pl and pl or sl
        st = st < pt and pt or st
        sr = sr > pr and pr or sr
        sb = sb > pb and pb or sb

    end

	render.SetScissorRect( sl, st, sr, sb, true )

	self:LayoutEntity( self.Entity )

	cam.Start3D( self.vCamPos, (self.vLookatPos-self.vCamPos):Angle(), self.fFOV, x, y, self:GetWide(), self:GetTall() )
	cam.IgnoreZ( true )

	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.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

	if IsValid( self.Entity ) and not GetGlobalBool( "GameOver", false ) then

		self.Entity:DrawModel()

	end

	render.SuppressEngineLighting( false )
	cam.IgnoreZ( false )
	cam.End3D()

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

	self.LastPaint = RealTime()

end

function PANEL:LayoutEntity( Entity )

	if self.StopRender then return end

	if ( self.bAnimated ) then
		self:RunAnimation()
	end

	Entity:SetAngles( Angle( 0, RealTime() * 10,  0) )

end

derma.DefineControl( "GoodModelPanel", "A panel containing a model that isn't broken and gay", PANEL, "DModelPanel" )