fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
29daa42
GUI
1local PANEL ={} 2 3local watcher =require ( "memory.watcher" ) 4local perspective =require ( "perspective" ) 5local state =require ( "smash.states" ) 6 7require ( "extensions.math" ) 8 9local BUTTONS ={ 10NONE =0x0000 , 11DPAD_LEFT =0x0001 , 12DPAD_RIGHT =0x0002 , 13DPAD_DOWN =0x0004 , 14DPAD_UP =0x0008 , 15Z =0x0010 , 16R =0x0020 , 17L =0x0040 , 18A =0x0100 , 19B =0x0200 , 20X =0x0400 , 21Y =0x0800 , 22START =0x1000 , 23} 24 25local timer =love . timer 26local graphics =love . graphics 27local newImage =graphics . newImage 28local newFont =graphics . newFont 29local format =string . format 30 31local DISCONNECTED =newImage ( "textures/buttons/disconnected.png" ) 32 33local BUTTON_TEXTURES ={ 34DPAD ={ 35BACKGROUND =newImage ( "textures/buttons/d-pad-gate.png" ), 36POSITION ={ 37x =100 , 38y =128 , 39} , 40} , 41 42DPAD_LEFT ={ 43PRESSED =newImage ( "textures/buttons/d-pad-pressed-left.png" ), 44POSITION ={ 45x =108 , 46y =144 , 47} , 48} , 49 50DPAD_RIGHT ={ 51PRESSED =newImage ( "textures/buttons/d-pad-pressed-right.png" ), 52POSITION ={ 53x =108 , 54y =144 , 55} , 56} , 57 58DPAD_UP ={ 59PRESSED =newImage ( "textures/buttons/d-pad-pressed-up.png" ), 60POSITION ={ 61x =108 , 62y =144 , 63} , 64} , 65 66DPAD_DOWN ={ 67PRESSED =newImage ( "textures/buttons/d-pad-pressed-down.png" ), 68POSITION ={ 69x =108 , 70y =144 , 71} , 72} , 73 74JOYSTICK ={ 75BACKGROUND =newImage ( "textures/buttons/joystick-gate.png" ), 76MASK =newImage ( "textures/buttons/joystick-mask.png" ), 77STICK =newImage ( "textures/buttons/joystick-outline.png" ), 78} , 79CSTICK ={ 80BACKGROUND =newImage ( "textures/buttons/c-stick-gate.png" ), 81STICK =newImage ( "textures/buttons/c-stick.png" ), 82} , 83A ={ 84OUTLINE =newImage ( "textures/buttons/a-outline.png" ), 85PRESSED =newImage ( "textures/buttons/a-pressed.png" ), 86COLOR =color ( 0 , 225 , 150 , 255 ), 87POSITION ={ 88x =12 + 64 + 256 , 89y =64 90} 91} , 92B ={ 93OUTLINE =newImage ( "textures/buttons/b-outline.png" ), 94PRESSED =newImage ( "textures/buttons/b-pressed.png" ), 95COLOR =color ( 230 , 0 , 0 , 255 ), 96POSITION ={ 97x =16 + 256 , 98y =108 99} 100} , 101X ={ 102OUTLINE =newImage ( "textures/buttons/x-outline.png" ), 103PRESSED =newImage ( "textures/buttons/x-pressed.png" ), 104COLOR =color ( 255 , 255 , 255 , 255 ), 105POSITION ={ 106x =138 + 256 , 107y =48 108} 109} , 110Y ={ 111OUTLINE =newImage ( "textures/buttons/y-outline.png" ), 112PRESSED =newImage ( "textures/buttons/y-pressed.png" ), 113COLOR =color ( 255 , 255 , 255 , 255 ), 114POSITION ={ 115x =60 + 256 , 116y =0 117} 118} , 119Z ={ 120OUTLINE =newImage ( "textures/buttons/z-outline.png" ), 121PRESSED =newImage ( "textures/buttons/z-pressed.png" ), 122COLOR =color ( 165 , 75 , 165 , 255 ), 123POSITION ={ 124x =128 + 256 , 125y =- 16 126} 127} , 128START ={ 129OUTLINE =newImage ( "textures/buttons/start-outline.png" ), 130PRESSED =newImage ( "textures/buttons/start-pressed.png" ), 131COLOR =color ( 255 , 255 , 255 , 255 ), 132POSITION ={ 133x =256 , 134y =42 135} 136} 137} 138 139local MAX_PORTS =4 140 141function PANEL : Initialize () 142self : super () 143 144self : SetSize ( 512 , 256 ) 145 146self : DockPadding ( 0 , 0 , 0 , 0 ) 147self : DockMargin ( 0 , 0 , 0 , 0 ) 148 149self : MakeAccessor ( "Port" , "m_iPort" , - 1 ) 150 151self : SetScale ( 0.5 ) 152end 153 154local vertices ={ 155{ 1560 , 0 , 1570 , 0 , 1581 , 1 , 1 , 159} , 160{ 161128 , 0 , 1621 , 0 , 1631 , 1 , 1 164} , 165{ 166128 , 128 , 1671 , 1 , 1681 , 1 , 1 169} , 170{ 1710 , 128 , 1720 , 1 , 1731 , 1 , 1 174} , 175} 176 177local rotated_vertices ={} 178 179local function transformVertices ( vertices , x , y , angle , ox , oy ) 180if # vertices ~= # rotated_vertices then 181rotated_vertices ={} 182end 183 184local c =math . cos ( angle ) 185local s =math . sin ( angle ) 186 187for i =1 , # vertices do 188-- Create or use vertex cache 189rotated_vertices [ i ] =rotated_vertices [ i ] or {} 190 191-- Copy and rotate X and Y vertex points 192rotated_vertices [ i ][ 1 ] =x + ( vertices [ i ][ 1 ] - ox ) * c - ( vertices [ i ][ 2 ] - oy ) * s 193rotated_vertices [ i ][ 2 ] =y + ( vertices [ i ][ 1 ] - ox ) * s + ( vertices [ i ][ 2 ] - oy ) * c 194 195-- Copy other vertex settings 196rotated_vertices [ i ][ 3 ] =vertices [ i ][ 3 ] 197rotated_vertices [ i ][ 4 ] =vertices [ i ][ 4 ] 198rotated_vertices [ i ][ 5 ] =vertices [ i ][ 5 ] 199rotated_vertices [ i ][ 6 ] =vertices [ i ][ 6 ] 200end 201 202return rotated_vertices 203end 204 205function PANEL : Paint ( w , h ) 206local controller =watcher . controller [ self . m_iPort ] 207 208if controller then 209 210-- Draw connection status 211 212if controller . plugged == 0xFF then 213local t =timer . getTime () 214graphics . setColor ( hsl ( 360 , 1 , 0.5 , math . sinlerp ( 0.25 , 1 , t * 4 ))) 215graphics . easyDraw ( DISCONNECTED , w - 8 - 42 , h - 8 - 42 , 0 , 32 , 32 ) 216end 217 218-- Draw Joystick 219 220local x , y =controller . joystick . x , 1 - controller . joystick . y 221 222local angle =math . atan2 ( controller . joystick . x , controller . joystick . y ) 223local mag =math . sqrt ( controller . joystick . x ^ 2 + controller . joystick . y ^ 2 ) 224 225local far =mag * 15 226local near =mag * 20 227 228-- Make the rectangle look like its fading into the horizon 229vertices [ 1 ][ 1 ] =far -- x 230vertices [ 1 ][ 2 ] =near -- y 231vertices [ 2 ][ 1 ] =128 - far -- x 232vertices [ 2 ][ 2 ] =near -- y 233 234local rotated =transformVertices ( vertices , 64 + 22 + ( 40 * x ), 64 + 12 + ( 40 * y ), angle , 64 , 64 ) 235 236local wx , wy =self : GetWorldPos () 237local sx , sy =self : GetScale () 238 239graphics . stencil ( function () 240perspective . on ( wx , wy , sx , sy ) 241perspective . quad ( BUTTON_TEXTURES . JOYSTICK . MASK , rotated [ 1 ], rotated [ 2 ], rotated [ 3 ], rotated [ 4 ]) 242perspective . off () 243end , "replace" , 1 ) 244graphics . setStencilTest ( "equal" , 0 ) -- Mask out the gate behind the joystick 245graphics . easyDraw ( BUTTON_TEXTURES . JOYSTICK . BACKGROUND , 22 , 52 , 0 , 128 , 128 ) 246graphics . setStencilTest () 247 248perspective . on ( wx , wy , sx , sy ) 249perspective . quad ( BUTTON_TEXTURES . JOYSTICK . STICK , rotated [ 1 ], rotated [ 2 ], rotated [ 3 ], rotated [ 4 ]) 250perspective . off () 251 252-- Draw C-Stick 253 254local x , y =controller . cstick . x , 1 - controller . cstick . y 255 256local angle =math . atan2 ( controller . cstick . x , controller . cstick . y ) 257local mag =math . sqrt ( controller . cstick . x ^ 2 + controller . cstick . y ^ 2 ) 258 259local far =mag * 12 260local near =mag * 16 261 262-- Make the rectangle look like its fading into the horizon 263vertices [ 1 ][ 1 ] =far -- x 264vertices [ 1 ][ 2 ] =near -- y 265vertices [ 2 ][ 1 ] =128 - far -- x 266vertices [ 2 ][ 2 ] =near -- y 267 268local rotated =transformVertices ( vertices , 64 + 48 + 128 + ( 32 * x ), 64 + 20 + ( 32 * y ), angle , 64 , 64 ) 269 270graphics . setColor ( 255 , 235 , 0 , 255 ) 271graphics . easyDraw ( BUTTON_TEXTURES . CSTICK . BACKGROUND , 48 + 128 , 52 , 0 , 128 , 128 ) 272 273perspective . on ( wx , wy , sx , sy ) 274perspective . quad ( BUTTON_TEXTURES . CSTICK . STICK , rotated [ 1 ], rotated [ 2 ], rotated [ 3 ], rotated [ 4 ]) 275perspective . off () 276 277graphics . setColor ( 255 , 255 , 255 , 255 ) 278 279-- Draw L 280 281graphics . setLineStyle ( "smooth" ) 282graphics . setLineWidth ( 3 ) 283 284graphics . stencil ( function () 285-- Create a rounded rectangle mask 286graphics . rectangle ( "fill" , 24 + 14 , 16 , 100 , 12 , 6 , 6 ) 287end , "replace" , 1 ) 288graphics . setStencilTest ( "greater" , 0 ) -- Only draw within our rounded rectangle mask 289-- L Analog 290graphics . rectangle ( "fill" , 24 + 14 , 16 , 88 * controller . analog . float . l , 12 ) 291 292-- L Button 293if bit . band ( controller . buttons . pressed , BUTTONS . L ) == BUTTONS . L then 294graphics . rectangle ( "fill" , 24 + 14 + 88 , 16 , 12 , 12 ) 295end 296graphics . setStencilTest () 297 298-- Draw outline 299graphics . rectangle ( "line" , 24 + 14 , 16 , 100 , 12 , 6 , 6 ) 300-- Draw segment for button press 301graphics . line ( 24 + 14 + 88 , 16 , 24 + 14 + 88 , 16 + 12 ) 302 303-- Draw R 304 305graphics . stencil ( function () 306-- Create a rounded rectangle mask 307graphics . rectangle ( "fill" , 48 + 128 + 14 , 16 , 100 , 12 , 6 , 6 ) 308end , "replace" , 1 ) 309graphics . setStencilTest ( "greater" , 0 ) -- Only draw within our rounded rectangle mask 310-- R Analog 311graphics . rectangle ( "fill" , 48 + 128 + 14 + 12 + ( 88 * ( 1 - controller . analog . float . r )), 16 , 88 * controller . analog . float . r , 12 ) 312 313-- R Button 314if bit . band ( controller . buttons . pressed , BUTTONS . R ) == BUTTONS . R then 315graphics . rectangle ( "fill" , 48 + 128 + 14 , 16 , 12 , 12 ) 316end 317graphics . setStencilTest () 318 319-- Draw outline 320graphics . rectangle ( "line" , 48 + 128 + 14 , 16 , 100 , 12 , 6 , 6 ) 321-- Draw segment for button press 322graphics . line ( 48 + 128 + 14 + 12 , 16 , 48 + 128 + 14 + 12 , 16 + 12 ) 323 324-- Draw buttons 325 326graphics . easyDraw ( BUTTON_TEXTURES . DPAD . BACKGROUND , 108 , 144 , 0 , 128 , 128 ) 327 328for button , flag in pairs ( BUTTONS ) do 329local texture =BUTTON_TEXTURES [ button ] 330if texture then 331local pos =texture . POSITION 332graphics . setColor ( texture . COLOR ) 333if texture . PRESSED and bit . band ( controller . buttons . pressed , flag ) == flag then -- Check if the button is pressed 334graphics . easyDraw ( texture . PRESSED , pos . x , pos . y , 0 , 128 , 128 ) 335elseif texture . OUTLINE then 336graphics . easyDraw ( texture . OUTLINE , pos . x , pos . y , 0 , 128 , 128 ) 337end 338end 339end 340end 341end 342 343gui . register ( "ControllerDisplay" , PANEL , "Base" )