fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a7d6bcc
gui
1local graphics =love . graphics 2 3local setColor =graphics . setColor 4 5-- Love2D changed color values to be 0-1 6-- Allow 0-255 values again.. 7function graphics . setColor ( r , g , b , a ) 8if type ( r ) == "table" then 9r , g , b , a =r . r , r . g , r . b , r . a 10else 11r , g , b , a =r or 255 , g or 255 , b or 255 , a or 255 12end 13setColor ( r / 255 , g / 255 , b / 255 , a / 255 ) 14end 15 16-- Draw an image using width and height in pixels 17-- OriginX and OriginY should be between 0 and 1 18-- An origin of 0.5, 0.5 would be the center 19function graphics . easyDraw ( obj , x , y , rotation , width , height , originX , originY , ... ) 20if not obj then return end 21 22local objW , objH =obj : getWidth (), obj : getHeight () 23 24rotation =rotation or 0 25width =width or objW 26height =height or objH 27originX =originX or 0 28originY =originY or 0 29 30local scaledW =width / objW 31local scaledH =height / objH 32graphics . draw ( obj , x , y , rotation , scaledW , scaledH , objW * originX , objH * originY , ... ) 33end 34 35--applies a transformation that maps 36-- 0,0 => ox, oy 37-- 1,0 => xx, xy 38-- 0,1 => yx, yy 39-- via love.graphics.translate, .rotate and .scale 40 41local sqrt =math . sqrt 42local atan2 =math . atan2 43local acos =math . acos 44local tan =math . tan 45local cos =math . cos 46local sin =math . sin 47 48function graphics . transform ( ox , oy , xx , xy , yx , yy ) 49 50local ex , ey , fx , fy =xx - ox , xy - oy , yx - ox , yy - oy 51if ex * fy < ey * fx then ex , ey , fx , fy =fx , fy , ex , ey end 52local e , f =math . sqrt ( ex * ex + ey * ey ), math . sqrt ( fx * fx + fy * fy ) 53 54ex , ey =ex / e , ey / e 55fx , fy =fx / f , fy / f 56 57local desiredOrientation =math . atan2 ( ey + fy , ex + fx ) 58local desiredAngle =math . acos ( ex * fx + ey * fy ) / 2 59local z =math . tan ( desiredAngle ) 60local distortion =math . sqrt (( 1 + z * z ) / 2 ) 61 62graphics . translate ( ox , oy ) 63graphics . rotate ( desiredOrientation ) 64graphics . scale ( 1 , z ) 65graphics . rotate ( - math . pi / 4 ) 66graphics . scale ( e / distortion , f / distortion ) 67 68end 69 70function graphics . innerRectangle ( x , y , w , h , ... ) 71local lw =graphics . getLineWidth () 72 73x =x + math . ceil ( lw / 2 ) 74y =y + math . floor ( lw / 2 ) 75w =w - lw 76h =h - lw 77 78graphics . rectangle ( "line" , x , y , w , h , ... ) 79end 80 81-- Draw a rectangle to fit AROUND the width and height 82function graphics . outlineRectangle ( x , y , w , h , ... ) 83local lw =graphics . getLineWidth () 84 85x =x + math . ceil ( lw / 2 ) - lw 86y =y + math . floor ( lw / 2 ) - lw 87w =w + lw * 2 88h =h + lw * 2 89 90graphics . rectangle ( "line" , x , y , w , h , ... ) 91end 92 93function graphics . textOutlinef ( text , width , x , y , ... ) 94local steps =width * 2 / 3 95if steps < 1 then steps =1 end 96 97for _x =- width , width , steps do 98for _y =- width , width , steps do 99graphics . printf ( text , x + _x , y + _y , ... ) 100end 101end 102end 103 104function graphics . textOutline ( text , width , x , y , ... ) 105local steps =width * 2 / 3 106if steps < 1 then steps =1 end 107 108for _x =- width , width , steps do 109for _y =- width , width , steps do 110graphics . ( text , x + _x , y + _y , ... ) 111end 112end 113end