fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
8baa412
v2.1.1b
1local graphics =love . graphics 2local newImage =graphics . newImage 3local newFont =graphics . newFont 4local timer =love . timer 5local color =require ( "util.color" ) 6 7local log =require ( "log" ) 8 9local notification ={ 10messages ={} , 11font =newFont ( "fonts/melee-bold.otf" , 12 ), 12padding =8 , 13} 14 15local dummy =function ( height , fade ) end 16 17function notification . add ( height , length , fade , draw ) 18height =height or 16 19length =length or 3 20fade =fade or 0.75 21 22local now =timer . getTime () 23 24table . insert ( notification . messages , { 25time =now , 26length =length , 27fade =fade , 28finish =now + length + fade , 29 30height =height , 31 32draw =draw or dummy , 33 34lerp ={} , 35} ) 36end 37 38function notification . update ( x , y ) 39local now =timer . getTime () 40 41local prevh =0 42 43for k , message in pairs ( notification . messages ) do 44if ( message . finish >= now ) then 45x =x * 0.5 + ( message . lerp . x or x ) * 0.5 46y =y * 0.5 + ( message . lerp . y or y ) * 0.5 47y =y + prevh / 2 + notification . padding / 2 48 49message . lerp . x =x 50message . lerp . y =y 51 52prevh =message . height 53end 54end 55 56for k , message in pairs ( notification . messages ) do 57if ( message . finish >= now ) then 58return 59end 60end 61 62notification . messages ={} 63end 64 65function notification . draw () 66local now =timer . getTime () 67for k , message in pairs ( notification . messages ) do 68 69local fade =1 70 71if now >= message . time + message . length then 72fade =1 - (( now - message . time - message . length ) / message . fade ) 73end 74 75local x , y =message . lerp . x , message . lerp . y 76 77if x and y then 78graphics . push ( "transform" ) 79graphics . translate ( x , y ) 80graphics . setFont ( notification . font ) 81graphics . setColor ( 255 , 255 , 255 , 255 * fade ) 82local succ , ret =xpcall ( message . draw , debug . traceback , message . height , fade ) 83if not succ then 84log . error ( "notification error: %s" , ret ) 85end 86graphics . pop () 87end 88end 89end 90 91function notification . color ( color , text , ... ) 92if select ( "#" , ... ) > 0 then 93text =string . format ( text , ... ) 94end 95notification . add ( 14 , 5 , 0.75 , function ( height , fade ) 96graphics . setColor ( 0 , 0 , 0 , 255 * fade ) 97graphics . textOutline ( text , 1 , 1 , 1 ) 98graphics . setColor ( color . r , color . g , color . b , 255 * fade ) 99graphics . ( text , 0 , 0 ) 100end ) 101end 102 103function notification . error ( text , ... ) 104love . window . requestAttention () 105notification . color ( color ( 241 , 73 , 82 ), text , ... ) 106end 107 108function notification . warning ( text , ... ) 109notification . color ( color ( 255 , 242 , 0 ), text , ... ) 110end 111 112function notification . info ( text , ... ) 113notification . color ( color ( 79 , 202 , 255 ), text , ... ) 114end 115 116function notification . message ( text , ... ) 117notification . color ( color_white , text , ... ) 118end 119 120function notification . coloredMessage ( ... ) 121local args ={ ... } 122notification . add ( 14 , 5 , 0.75 , function ( height , fade ) 123graphics . setColor ( 0 , 0 , 0 , 255 * fade ) 124graphics . textOutline ( args , 1 , 1 , 1 ) 125graphics . setColor ( 255 , 255 , 255 , 255 * fade ) 126graphics . ( args , 0 , 0 ) 127end ) 128end 129 130return notification