fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
be5d99b
settings-overhaul
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 ( "TextAlignment" , "m_sAlignment" , "left" ) 14 15function PANEL : Label () 16self : super () -- Initialize our baseclass 17 18self . m_tTextMargins ={ left =0 , top =0 , right =0 , bottom =0 } 19 20self : SetDrawPanel ( false ) 21self : SetFocusable ( false ) 22 23self . m_pFont =graphics . newFont ( "fonts/melee.otf" , 12 ) 24 25self : SetLineHeight ( 0.55 ) 26 27self : SetText ( self . m_sText ) 28end 29 30function PANEL : TextMargin ( left , top , right , bottom ) 31self . m_tTextMargins . left =left 32self . m_tTextMargins . top =top 33self . m_tTextMargins . right =right 34self . m_tTextMargins . bottom =bottom 35end 36 37function PANEL : GetTextMargin () 38return self . m_tTextMargins 39end 40 41function PANEL : Think ( dt ) 42self : super ( "Think" , dt ) 43if not self . m_pFont and self . m_sFontFile then 44self . m_pFont =graphics . newFont ( self . m_sFontFile , self . m_iFontSize , self . m_iFontHint ) 45end 46end 47 48function PANEL : SetText ( text ) 49self . m_sText =text 50if self . m_bWrapped then 51self . m_iTextWidth , self . m_tTextWrap =self . m_pFont : getWrap ( self . m_sText , self : GetWidth ()) 52else 53self . m_iTextWidth , self . m_tTextWrap =self . m_pFont : getWidth ( self . m_sText ), { self . m_sText } 54end 55end 56 57function PANEL : PerformLayout () 58end 59 60function PANEL : SetLineHeight ( height ) 61self . m_pFont : setLineHeight ( height ) 62end 63 64function PANEL : GetLineHeight () 65return self . m_pFont : getLineHeight () 66end 67 68function PANEL : SetFont ( filename , size , hinting ) 69self . m_pFont =graphics . newFont ( filename , size , hinting ) 70self . m_sFontFile =filename 71self . m_iFontSize =size 72self . m_iFontHint =hinting 73end 74 75local floor =math . floor 76 77function PANEL : Paint ( w , h ) 78self : super ( "Paint" , w , h ) 79 80graphics . setColor ( self . m_cTextColor ) 81graphics . setFont ( self . m_pFont ) 82 83local x , y =self . m_tTextMargins . left , self . m_tTextMargins . top 84 85w =w - self . m_tTextMargins . right - self . m_tTextMargins . left 86h =h - self . m_tTextMargins . top - self . m_tTextMargins . bottom 87 88local tw , th =self . m_pFont : getWidth ( self . m_sText ), self . m_pFont : getAscent () - self . m_pFont : getDescent () 89 90if self . m_bWrapped then 91local sd =tonumber ( self . m_iShadowDistance ) 92 93if sd and sd > 0 then 94graphics . setColor ( self . m_cShadowColor ) 95graphics . printf ( self . m_sText , x + sd , y + sd , self : GetWidth (), self . m_sAlignment ) 96end 97 98graphics . setColor ( self . m_cTextColor ) 99graphics . printf ( self . m_sText , x , y , self : GetWidth (), self . m_sAlignment ) 100else 101--print(self.m_pFont:getHeight(), self.m_pFont:getLineHeight(), self.m_pFont:getDescent(), self.m_pFont:getAscent(), self.m_pFont:getBaseline()) 102 103-- Set alignment for non-wrapped text 104if self . m_sAlignment == "center" then 105x , y =x + floor ( w / 2 - ( tw / 2 )), y + floor ( h / 2 - ( th / 2 )) 106elseif self . m_sAlignment == "right" then 107x , y =x + w - tw , y + floor ( h / 2 - ( th / 2 )) 108else -- Assume left 109y =y + floor ( h / 2 - ( th / 2 )) 110end 111 112local ol =tonumber ( self . m_iOutlineThickness ) 113 114if ol and ol > 0 then 115graphics . setColor ( self . m_cOutlineColor ) 116graphics . textOutline ( self . m_sText , ol , x , y ) 117end 118 119local sd =tonumber ( self . m_iShadowDistance ) 120 121if sd and sd > 0 then 122graphics . setColor ( self . m_cShadowColor ) 123graphics . ( self . m_sText , x + sd , y + sd ) 124end 125 126graphics . setColor ( self . m_cTextColor ) 127graphics . ( self . m_sText , x , y ) 128end 129end 130 131function PANEL : SizeToText () 132self : SetSize ( self . m_iTextWidth , self . m_pFont : getHeight () * self : GetLineHeight () * # self . m_tTextWrap ) 133end 134 135function PANEL : WidthToText () 136self : SetWidth ( self . m_pFont : getWidth ( self . m_sText )) 137end 138 139function PANEL : HeightToText () 140self : SetHeight ( self . m_pFont : getHeight () * self : GetLineHeight () * # self . m_tTextWrap ) 141end