All pastes #442045 Raw Edit

Untitled

public lua v1 · immutable
·published 2007-04-16 00:12 UTC
rendered paste body
/*  JHud  	  jlulian38's generic HUD/Clientside drawererer*/HUD_BASE = 0    //Don't use this unless you're deriving your own                        //Render type from my code.HUD_RECT = 1    //Generic Untextured rectangle.HUD_TEXTURE_RECT = 2 //Texturable Rectangle.HUD_ROTATE_RECT = 3  //Rotateable Rectangle.HUD_SMOOTH_RECT = 4     //Smoothed (Rounded Corners) Rectangle.HUD_TEXT = 5            //Text...HUD_SCROLL_TEXT = 6     //Scrolling text.HUD_OUTLINE_TEXT = 7    //Outlined text.//ALIGN ENUMSALIGN_LEFT = 0ALIGN_CENTER = 1ALIGN_RIGHT = 2ALIGN_TOP = 3ALIGN_BOTTOM = 4ALIGN_NONE = 5//POINT ENUMS//These are only defined in the rect class.POINT_TOP = 1POINT_BOTTOM = 2POINT_LEFT = 3POINT_RIGHT = 4POINT_BOTTOM_RIGHT = 5POINT_BOTTOM_LEFT = 6POINT_TOP_RIGHT = 7POINT_TOP_LEFT = 8////////////////////////////////The terible terrible code.////                          //// If you bork this, it's   ////      not my fault        ////////////////////////////////local _JHud = {} //This is our Method table.local _Element = {} //This is the Element metatable.//Here's that really terrible and dreaded function.function deriveInto(org,from)		 for k,v in pairs(from) do org[k] = v endendfunction JHud()		 local o = {}		 for k,v in pairs(_JHud) do o[k]=v end		 o.elems = {}		 return o;endfunction _JHud:AttachNew(type)		 local o = _Element[type]:New()		 table.insert(self.elems,o)		 o.idx = table.getn( self.elems )		 return oendfunction _JHud:Detach(elem)	self.elems[elem.idx] = nil //stops it from rendering (I hope)	elem = nil //better damn stop...endfunction _JHud:Render()		 for k,v in pairs(self.elems) do		  	 if v.Think then		  	 	v:Think() //Think... Always.		  	 end		 	 if v.Render then		 	    if v:ShouldRender() then		 	 	   v:Render()		 	 	end	 	 	 else	 	 	 	 Msg("Something is very wrong with your code...\n")		 	 end		 endend//Base Code_Element[HUD_BASE] = {}_Base = _Element[HUD_BASE]function _Base:SetPos(x,y)		 self.Pos = {x=x,y=y}endfunction _Base:SetColor(...)		 if #arg == 1 then   		 	self.Color = Color(arg[1],arg[1],arg[1],255)	 	 elseif #arg == 2 then	 		self.Color = Color(arg[1],arg[1],arg[1],arg[2]) 		 elseif #arg == 3 then            self.Color = Color(arg[1],arg[2],arg[3],255)         else            self.Color = Color(arg[1],arg[2],arg[3],arg[4])		 endendfunction _Base:SetAlpha(a)		 local col = Color(self:GetColor())		 col.a = a		 self:SetColor(unpack(col))endfunction _Base:GetPos()		 if not self.Pos then		 	return 0,0	 	 end	 	 return self.Pos.x, self.Pos.yendfunction _Base:GetColor()		 if not self.Color then		 	return 255,255,255,255 //BLINDING WHITE	 	 end 	     return self.Color.r,self.Color.g,self.Color.b,self.Color.aend//This just requires that all Drawing Classes have//UpdatePos in them.function _Base:SetAlign(alg)		self.Align = alg		self:UpdatePos()endfunction _Base:ShouldRender(...)		 if #arg > 0 then		 	self.SRender = arg[1]	 	 else	 	    if self.SRender == nil then return true end	        return self.SRender         endendfunction _Base:New()		 local o = {}		 for k,v in pairs(self) do o[k] = v end		 return oend//Rect Code_Element[HUD_RECT] = {}_Rect = _Element[HUD_RECT]function _Rect:SetSize(x,y)		self.Size = {x=x,y=y}endfunction _Rect:GetSize()		 if not self.Size then		 	return 0,0		 end		 //So we return like x,y rather than a table.		 return self.Size.x,self.Size.yendfunction _Rect:UpdatePos()		 if not self.Pos then self:SetPos(0,0) end		if (self.Align == ALIGN_CENTER) then			local hH = ScrH()			local hW = ScrW()			local w,h = self:GetSize()			w = w / hW			h = h / hH			self:SetPos((hW * .5) - (hW * (w / 2)), (hH * .5) - (hH * (h / 2)))		elseif (self.Align == ALIGN_NONE) then			return		endendfunction _Rect:GetPoint(PNT)	if (PNT == POINT_TOP) then		return self.Pos.x	elseif (PNT == POINT_BOTTOM) then		local hH = self:GetPoint(POINT_TOP)		local _,h = self:GetSize()		return hH + h    elseif (PNT == POINT_LEFT) then		return self.Pos.y	elseif (PNT == POINT_RIGHT) then		local hW = self:GetPoint(POINT_LEFT)		local w = self:GetSize()		return hW + w	elseif (PNT == POINT_BOTTOM_LEFT) then		return self:GetPoint(POINT_LEFT), self:GetPoint(POINT_BOTTOM)	elseif (PNT == POINT_BOTTOM_RIGHT) then		return self:GetPoint(POINT_RIGHT), self:GetPoint(POINT_BOTTOM)	elseif (PNT == POINT_TOP_LEFT) then		return self:GetPoint(POINT_LEFT), self:GetPoint(POINT_TOP)	elseif (PNT == POINT_TOP_RIGHT) then		return self:GetPoint(POINT_RIGHT), self:GetPoint(POINT_TOP)	endendfunction _Rect:SetAlign(alg)		self.Align = alg		self:UpdatePos()endfunction _Rect:New(origin)		 local o = {}		 deriveInto(o,_Base:New()) // copy from _Base, into this local object.		 deriveInto(o,self) //copy from _Rect, into this local object.		 return oend//Here's the uber fun bit.function _Rect:Render()		 //Not...		 surface.SetDrawColor(self:GetColor())		 local x,y = self:GetPos()		 local w,h = self:GetSize()		 surface.DrawRect(x,y,w,h)end//Textured Rect code._Element[HUD_TEXTURE_RECT] = {}_TRect = _Element[HUD_TEXTURE_RECT]function _TRect:SetTexture(tex)	self.Texture = surface.GetTextureID(tex)endfunction _TRect:GetTexture()	return self.Texture or falseendfunction _TRect:Render()		 if self:GetTexture() then surface.SetTexture(self:GetTexture()) end     	 surface.SetDrawColor(self:GetColor())		 local x,y = self:GetPos()		 local w,h = self:GetSize()		 surface.DrawTexturedRect(x,y,w,h)endfunction _TRect:New()		local o = {}		deriveInto(o,_Rect:New())		deriveInto(o,self)		return oend//Rotatable Rect code._Element[HUD_ROTATE_RECT] = {}_RRect = _Element[HUD_ROTATE_RECT]function _RRect:SetRotation(rot)		self.rot = rotendfunction _RRect:GetRotation()		return self.rot or 0endfunction _RRect:Render()		 if self:GetTexture() then surface.SetTexture(self:GetTexture()) end     	 surface.SetDrawColor(self:GetColor())		 local x,y = self:GetPos()		 local w,h = self:GetSize()		 surface.DrawTexturedRectRotated(x,y,w,h,self:GetRotation())endfunction _RRect:New()		local o = {}		deriveInto(o,_TRect:New())		deriveInto(o,self)		return oend//Smooth Rect "Class"_Element[HUD_SMOOTH_RECT] = {}_SRect = _Element[HUD_SMOOTH_RECT]function _SRect:SetSmooth(sm)		 self.Smooth = smendfunction _SRect:GetSmooth()		 return self.Smooth or 0endfunction _SRect:Render()		 local col = Color(self:GetColor())		 local smooth = self:GetSmooth()		 local x,y = self:GetPos()		 local w,h = self:GetSize()		 draw.RoundedBox(smooth,x,y,w,h,col)endfunction _SRect:New()		 local o = {}         deriveInto(o,_Rect:New())         deriveInto(o,self)		 return oend//Text Class_Element[HUD_TEXT] = {}_Text = _Element[HUD_TEXT]function _Text:SetFont(font_name)		 self.Font = font_name		 self:UpdatePos()endfunction _Text:GetFont()		 return self.Font or "Default"endfunction _Text:SetText(text)		 self.Text = text		 self:UpdatePos()endfunction _Text:UpdatePos()		 if (self.Align == ALIGN_CENTER) then		    local x,y		 	surface.SetFont(self:GetFont())		 	local xS,yS = surface.GetTextSize(self:GetText())	        x = ScrW() * .5	        y = ScrH() * .5	        x = x - (xS * .5)	        y = y - (yS * .5)		 	self:SetPos(x,y)		 elseif (self.Align == ALIGN_NONE) then			return		 endendfunction _Text:GetText()		 return self.Text or ""endfunction _Text:Render()		 local txt = self:GetText()  //This is actually		 local fnt = self:GetFont()  //More complicated than		 local col = Color(self:GetColor()) //Rect drawing :(		 local x,y = self:GetPos()		 draw.DrawText(txt,fnt,x,y,col)endfunction _Text:New()		 local o = {}   		 deriveInto(o,_Base:New())   		 deriveInto(o,self)   		 return oend//Scrolling Text "class"_Element[HUD_SCROLL_TEXT] = {}_SText = _Element[HUD_SCROLL_TEXT]function _SText:SetSize(x)		 self.texsize = xendfunction _SText:GetSize()		 return self.texsize or ScrW()endfunction _SText:Render()        local mX,y = self:GetPos()        local texwide = self:GetSize()        local text = self:GetText()        surface.SetFont( self:GetFont() )		local w, h = surface.GetTextSize( text )		w = w + 64		local x = math.fmod( CurTime()*150, w ) * -1;		while ( x < texwide ) do        	local oldX = x			x = x + mX			surface.SetTextColor( self:GetColor() )			surface.SetTextPos( x + 3, y + 3 )			surface.DrawText( text )			surface.SetTextColor( self:GetColor() )			surface.SetTextPos( x, y )			surface.DrawText( text )			x = oldX			x = x + w		endendfunction _SText:New()		 local o = {}   		 deriveInto(o,_Text:New())   		 deriveInto(o,self)   		 return oend//Outlined Text "Class"_Element[HUD_OUTLINE_TEXT] = {}_OText = _Element[HUD_OUTLINE_TEXT]function _OText:SetOutlineWidth(px)		 self.OutLine = pxendfunction _OText:GetOutlineWidth()		 return self.OutLine or 1endfunction _OText:SetOutlineColor(...)		 if #arg == 1 then   		 	self.OutColor = Color(arg[1],arg[1],arg[1],255)	 	 elseif #arg == 2 then	 		self.OutColor = Color(arg[1],arg[1],arg[1],arg[2]) 		 elseif #arg == 3 then            self.OutColor = Color(arg[1],arg[2],arg[3],255)         else            self.OutColor = Color(arg[1],arg[2],arg[3],arg[4])		 endendfunction _OText:GetOutlineColor()		 if not self.OutColor then		 	return 255,255,255,255 //BLINDING WHITE	 	 end 	     return self.OutColor.r,self.OutColor.g,self.OutColor.b,self.OutColor.aendfunction _OText:Render()		 local text = self:GetText()		 local font = self:GetFont()		 local outlinewidth = self:GetOutlineWidth()		 local x,y = self:GetPos()		 for _x=-1, 1 do		 	 for _y=-1, 1 do		 	 	 surface.SetTextColor(self:GetOutlineColor())		 	 	 surface.SetFont(font)		 	 	 surface.SetTextPos( x + (_x*outlinewidth), y + (_y*outlinewidth))		 	 	 surface.DrawText(text)	 		 end		 end		 surface.SetTextColor(self:GetColor()) 	 	 surface.SetFont(font) 	 	 surface.SetTextPos( x, y) 	 	 surface.DrawText(text)endfunction _OText:New()		 local o = {}		 deriveInto(o,_Text:New())		 deriveInto(o,self)	  	 return oend