Skip to content

Instantly share code, notes, and snippets.

@rbreslow
Created April 5, 2020 05:18
Show Gist options
  • Save rbreslow/e50756ad66176f5c278dc5255af4cca5 to your computer and use it in GitHub Desktop.
Save rbreslow/e50756ad66176f5c278dc5255af4cca5 to your computer and use it in GitHub Desktop.
local gridSize = nutsquare.gridSize
local ITEM = triplib.getClass( "BASE_ITEM" )
ITEM.w = 1
ITEM.h = 1
ITEM.modelIconAng = Angle( 0, 0, 0 )
ITEM.modelIconPos = Vector( 0, 0, 0 )
ITEM.modelIconFOV = 90
function ITEM:guessModelIconLayout()
local model = self.modelIcon or self.model
if not model then return end
local class = self:getClass()
local dummy = ClientsideModel( model, RENDERGROUP_OTHER )
local view = PositionSpawnIcon( dummy, vector_origin )
class.modelIconPos, class.modelIconAng = WorldToLocal( vector_origin, angle_zero, view.origin, view.angles )
class.modelIconFOV = view.fov * 1.05
dummy:Remove()
end
local colorMat = Material( "vgui/white" )
function ITEM:renderIcon( w, h )
render.Clear( 0, 0, 0, 0, true, true )
render.PushFilterMag( TEXFILTER.ANISOTROPIC )
render.PushFilterMin( TEXFILTER.ANISOTROPIC )
local model = self.modelIcon or self.model
if model then
if not self.modelIconPos then
self:guessModelIconLayout()
end
local csprop = ClientsideModel( model, RENDERGROUP_OTHER )
cam.Start3D( vector_origin, angle_zero, self.modelIconFOV, 0, 0, w, h )
render.SetWriteDepthToDestAlpha( false )
render.SuppressEngineLighting( true )
csprop:SetPos( self.modelIconPos )
csprop:SetAngles( self.modelIconAng )
csprop:DrawModel()
render.SuppressEngineLighting( false )
cam.End3D()
csprop:Remove()
else
surface.SetMaterial( colorMat )
surface.SetDrawColor( 100, 0, 100, 200 )
triplib.drawRoundedBoxUV( 2, 2, w - 4, h - 4, 12, 0, 0, w / gridSize, h / gridSize )
surface.SetDrawColor( 0, 100, 100, 200 )
triplib.drawRoundedBoxUV( 4, 4, w - 8, h - 8, 12, 0, 0, w / gridSize, h / gridSize )
draw.SimpleText( self.name, "TargetID", w / 2, h / 2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
end
render.PopFilterMag()
render.PopFilterMin()
end
local outlineThickness = 2
local outlineQuality = 3
function ITEM:renderOutline( w, h )
render.Clear( 255, 255, 255, 0, true, true )
render.PushFilterMag( TEXFILTER.NONE )
render.PushFilterMin( TEXFILTER.NONE )
render.OverrideColorWriteEnable( true, false )
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( self.iconCanvas.mat )
for ang = 0, 360, 90 / outlineQuality do
local rad = math.rad( ang )
surface.DrawTexturedRect( math.cos( rad ) * outlineThickness, math.cos( rad ) * outlineThickness, w, h )
end
render.OverrideColorWriteEnable( false )
render.PopFilterMag()
render.PopFilterMin()
end
function ITEM:init()
local class = self:getClass()
local canvas = rawget( class, "iconCanvas" )
if not canvas then
canvas = triplib.textureCanvas( class.w * gridSize, class.h * gridSize, "item_canvas-" .. class.className )
class.iconCanvas = canvas
canvas.Paint = function( cvs, w, h )
class:renderIcon( w, h )
end
canvas:Update( class.w * gridSize, class.h * gridSize )
end
local outlineCanvas = rawget( class, "iconOutlineCanvas" )
if not outlineCanvas then
outlineCanvas = triplib.textureCanvas( class.w * gridSize, class.h * gridSize, "item_outline_canvas-" .. class.className )
class.iconOutlineCanvas = outlineCanvas
outlineCanvas.Paint = function( cvs, w, h )
class:renderOutline( w, h )
end
outlineCanvas:Update( class.w * gridSize, class.h * gridSize )
end
end
function ITEM:paint( x, y )
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( self.iconCanvas.mat )
surface.DrawTexturedRect( x, y, self.w * gridSize, self.h * gridSize )
end
function ITEM:paintOutline( x, y )
surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( self.iconOutlineCanvas.mat )
surface.DrawTexturedRect( x, y, self.w * gridSize, self.h * gridSize )
end
function ITEM:paintToolTip( pnl, x, y )
y = y + 20
surface.SetFont( "HudHintTextLarge" )
local tw, th = surface.GetTextSize( self.name )
endX, endY = pnl:LocalToScreen( x + tw, y + th )
if endX > ScrW() then x = x - tw end
if endY > ScrH() then y = y - th - 24 end
nutsquare.paintTooltip( x, y, self.name )
end
function ITEM:click()
end
function ITEM:doubleClick()
self:use()
end
function ITEM:setIconModel( mdl )
self:GetClass().iconModel = mdl
end
function ITEM:getIconModel()
return self.iconModel or self.model
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment