fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a8034e9
main
1local PANEL =class . create ( "Label" , "Panel" ) 2 3PANEL : ACCESSOR ( "Text" , "m_sText" , "Label" ) 4PANEL : ACCESSOR ( "TextColor" , "m_cTextColor" , color_black ) 5PANEL : ACCESSOR ( "FontFile" , "m_sFontFile" ) 6PANEL : ACCESSOR ( "FontSize" , "m_iFontSize" ) 7PANEL : ACCESSOR ( "FontHint" , "m_iFontHint" ) 8PANEL : ACCESSOR ( "ShadowDistance" , "m_iShadowDistance" , 1 ) 9PANEL : ACCESSOR ( "ShadowColor" , "m_cShadowColor" , color_lightgrey ) 10PANEL : ACCESSOR ( "OutlineThickness" , "m_iOutlineThickness" ) 11PANEL : ACCESSOR ( "OutlineColor" , "m_cOutlineColor" , color_black ) 12PANEL : ACCESSOR ( "Wrapped" , "m_bWrapped" , false ) 13PANEL : ACCESSOR ( "TextAlignmentX" , "m_sAlignmentX" , "left" ) 14PANEL : ACCESSOR ( "TextAlignmentY" , "m_sAlignmentY" , "center" ) 15PANEL : ACCESSOR ( "TextAlignmentWrap" , "m_sTextAlignmentWrap" , "left" ) 16PANEL : ACCESSOR ( "DrawLabel" , "m_bDrawLabel" , true ) 17 18function PANEL : Label () 19self : super () -- Initialize our baseclass 20 21self . m_tTextMargins ={ left =0 , top =0 , right =0 , bottom =0 } 22 23self : SetDrawPanel ( false ) 24self : SetFocusable ( false ) 25 26self . m_pFont =graphics . newFont ( "fonts/melee.otf" , 12 ) 27 28self : SetLineHeight ( 0.55 ) 29 30self : SetText ( self . m_sText ) 31end 32 33function PANEL : Skin () 34self : super ( "Skin" ) 35gui . skinHook ( "Init" , "Label" , self ) 36end 37 38function PANEL : TextMargin ( left , top , right , bottom ) 39self . m_tTextMargins . left =left 40self . m_tTextMargins . top =top 41self . m_tTextMargins . right =right 42self . m_tTextMargins . bottom =bottom 43end 44 45function PANEL : GetTextMargin () 46return self . m_tTextMargins 47end 48 49function PANEL : Think ( dt ) 50self : super ( "Think" , dt ) 51if not self . m_pFont and self . m_sFontFile then 52self . m_pFont =graphics . newFont ( self . m_sFontFile , self . m_iFontSize , self . m_iFontHint ) 53end 54end 55 56function PANEL : SetText ( text ) 57self . m_sText =text 58if self . m_bWrapped then 59self . m_iTextWidth , self . m_tTextWrap =self . m_pFont : getWrap ( self . m_sText , self : GetWidth () - self . m_tTextMargins . left - self . m_tTextMargins . right ) 60else 61self . m_iTextWidth , self . m_tTextWrap =self . m_pFont : getWidth ( self . m_sText ), { self . m_sText } 62end 63end 64 65function PANEL : PerformLayout () 66end 67 68function PANEL : SetLineHeight ( height ) 69self . m_pFont : setLineHeight ( height ) 70end 71 72function PANEL : GetLineHeight () 73return self . m_pFont : getLineHeight () 74end 75 76function PANEL : SetFont ( filename , size , hinting ) 77self . m_pFont =graphics . newFont ( filename , size , hinting ) 78self . m_sFontFile =filename 79self . m_iFontSize =size 80self . m_iFontHint =hinting 81end 82 83local floor =math . floor 84 85function PANEL : PaintLabel ( w , h ) 86if not self . m_bDrawLabel then return end 87 88graphics . setColor ( self . m_cTextColor ) 89graphics . setFont ( self . m_pFont ) 90 91local x , y =self . m_tTextMargins . left , self . m_tTextMargins . top 92 93w =w - self . m_tTextMargins . right - self . m_tTextMargins . left 94h =h - self . m_tTextMargins . top - self . m_tTextMargins . bottom 95 96local tw , th =self . m_pFont : getWidth ( self . m_sText ), self . m_pFont : getAscent () - self . m_pFont : getDescent () 97 98if self . m_bWrapped then 99local sd =tonumber ( self . m_iShadowDistance ) 100 101if sd and sd > 0 then 102graphics . setColor ( self . m_cShadowColor ) 103graphics . printf ( self . m_sText , x + sd , y + sd , w , self . m_sTextAlignmentWrap ) 104end 105 106graphics . setColor ( self . m_cTextColor ) 107graphics . printf ( self . m_sText , x , y , w , self . m_sTextAlignmentWrap ) 108else 109--print(self.m_pFont:getHeight(), self.m_pFont:getLineHeight(), self.m_pFont:getDescent(), self.m_pFont:getAscent(), self.m_pFont:getBaseline()) 110 111-- Set alignment for non-wrapped text 112if self . m_sAlignmentX == "center" then 113x =x + floor ( w / 2 - ( tw / 2 )) 114elseif self . m_sAlignmentX == "right" then 115x =x + w - tw 116end 117 118-- Set alignment for non-wrapped text 119if self . m_sAlignmentY == "center" then 120y =y + floor ( h / 2 - ( th / 2 )) 121elseif self . m_sAlignmentY == "bottom" then 122y =y + h - th 123end 124 125local ol =tonumber ( self . m_iOutlineThickness ) 126 127if ol and ol > 0 then 128graphics . setColor ( self . m_cOutlineColor ) 129graphics . textOutline ( self . m_sText , ol , x , y ) 130end 131 132local sd =tonumber ( self . m_iShadowDistance ) 133 134if sd and sd > 0 then 135graphics . setColor ( self . m_cShadowColor ) 136graphics . ( self . m_sText , x + sd , y + sd ) 137end 138 139graphics . setColor ( self . m_cTextColor ) 140graphics . ( self . m_sText , x , y ) 141end 142end 143 144function PANEL : Paint ( w , h ) 145self : PaintLabel ( w , h ) 146end 147 148function PANEL : SizeToText () 149self : WidthToText () 150self : HeightToText () 151end 152 153function PANEL : WidthToText () 154self : SetWidth ( self . m_pFont : getWidth ( self . m_sText )) 155end 156 157function PANEL : HeightToText () 158self : SetHeight ( self . m_pFont : getHeight () * self : GetLineHeight () * # self . m_tTextWrap ) 159end