/* 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 ENUMS ALIGN_LEFT = 0 ALIGN_CENTER = 1 ALIGN_RIGHT = 2 ALIGN_TOP = 3 ALIGN_BOTTOM = 4 ALIGN_NONE = 5 //POINT ENUMS //These are only defined in the rect class. POINT_TOP = 1 POINT_BOTTOM = 2 POINT_LEFT = 3 POINT_RIGHT = 4 POINT_BOTTOM_RIGHT = 5 POINT_BOTTOM_LEFT = 6 POINT_TOP_RIGHT = 7 POINT_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 end end function JHud() local o = {} for k,v in pairs(_JHud) do o[k]=v end o.elems = {} return o; end function _JHud:AttachNew(type) local o = _Element[type]:New() table.insert(self.elems,o) o.idx = table.getn( self.elems ) return o end function _JHud:Detach(elem) self.elems[elem.idx] = nil //stops it from rendering (I hope) elem = nil //better damn stop... end function _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 end end //Base Code _Element[HUD_BASE] = {} _Base = _Element[HUD_BASE] function _Base:SetPos(x,y) self.Pos = {x=x,y=y} end function _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]) end end function _Base:SetAlpha(a) local col = Color(self:GetColor()) col.a = a self:SetColor(unpack(col)) end function _Base:GetPos() if not self.Pos then return 0,0 end return self.Pos.x, self.Pos.y end function _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.a end //This just requires that all Drawing Classes have //UpdatePos in them. function _Base:SetAlign(alg) self.Align = alg self:UpdatePos() end function _Base:ShouldRender(...) if #arg > 0 then self.SRender = arg[1] else if self.SRender == nil then return true end return self.SRender end end function _Base:New() local o = {} for k,v in pairs(self) do o[k] = v end return o end //Rect Code _Element[HUD_RECT] = {} _Rect = _Element[HUD_RECT] function _Rect:SetSize(x,y) self.Size = {x=x,y=y} end function _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.y end function _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 end end function _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) end end function _Rect:SetAlign(alg) self.Align = alg self:UpdatePos() end function _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 o end //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) end function _TRect:GetTexture() return self.Texture or false end function _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) end function _TRect:New() local o = {} deriveInto(o,_Rect:New()) deriveInto(o,self) return o end //Rotatable Rect code. _Element[HUD_ROTATE_RECT] = {} _RRect = _Element[HUD_ROTATE_RECT] function _RRect:SetRotation(rot) self.rot = rot end function _RRect:GetRotation() return self.rot or 0 end function _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()) end function _RRect:New() local o = {} deriveInto(o,_TRect:New()) deriveInto(o,self) return o end //Smooth Rect "Class" _Element[HUD_SMOOTH_RECT] = {} _SRect = _Element[HUD_SMOOTH_RECT] function _SRect:SetSmooth(sm) self.Smooth = sm end function _SRect:GetSmooth() return self.Smooth or 0 end function _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) end function _SRect:New() local o = {} deriveInto(o,_Rect:New()) deriveInto(o,self) return o end //Text Class _Element[HUD_TEXT] = {} _Text = _Element[HUD_TEXT] function _Text:SetFont(font_name) self.Font = font_name self:UpdatePos() end function _Text:GetFont() return self.Font or "Default" end function _Text:SetText(text) self.Text = text self:UpdatePos() end function _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 end end function _Text:GetText() return self.Text or "" end function _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) end function _Text:New() local o = {} deriveInto(o,_Base:New()) deriveInto(o,self) return o end //Scrolling Text "class" _Element[HUD_SCROLL_TEXT] = {} _SText = _Element[HUD_SCROLL_TEXT] function _SText:SetSize(x) self.texsize = x end function _SText:GetSize() return self.texsize or ScrW() end function _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 end end function _SText:New() local o = {} deriveInto(o,_Text:New()) deriveInto(o,self) return o end //Outlined Text "Class" _Element[HUD_OUTLINE_TEXT] = {} _OText = _Element[HUD_OUTLINE_TEXT] function _OText:SetOutlineWidth(px) self.OutLine = px end function _OText:GetOutlineWidth() return self.OutLine or 1 end function _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]) end end function _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.a end function _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) end function _OText:New() local o = {} deriveInto(o,_Text:New()) deriveInto(o,self) return o end