fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
b58a563
GUI
1local gui ={ 2m_tRegisteredSkins ={} , 3m_pWorldPanel =nil , 4m_pSceneManager =nil , 5m_pFocusedPanel =nil , 6m_strSkin ="default" , 7m_bEditorMode =false , 8m_tOrphans ={} , 9m_tDeleted ={} , 10} 11 12DOCK_NONE =0 13DOCK_TOP =1 14DOCK_LEFT =2 15DOCK_BOTTOM =4 16DOCK_RIGHT =8 17DOCK_FILL =16 18 19local json =require ( "serializer.json" ) 20local class =require ( "class" ) 21--local nfd = require("nfd") 22 23require ( "extensions.table" ) 24 25function gui . setFocusedPanel ( panel ) 26gui . m_pFocusedPanel : OnFocusChanged ( false ) 27gui . m_pFocusedPanel =panel 28gui . m_pFocusedPanel : OnFocusChanged ( true ) 29end 30 31function gui . getFocusedPanel () 32return gui . m_pFocusedPanel 33end 34 35function gui . getHoveredPanel () 36return gui . m_pWorldPanel : GetHoveredPanel ( love . mouse . getPosition ()) 37end 38 39function gui . newSceneLayout () 40 41end 42 43function gui . openSceneLayout () 44local file =nfd . open ( "json" , "layout.json" ) 45 46if not file then return end 47 48local f , err =io . open ( file , "r" ) 49assert ( f , err ) 50local layout =json . decode ( f : read ( "*all" )) 51f : close () 52 53( table . tostring ( layout )) 54gui . loadConfig ( layout ) 55end 56 57function gui . getDefaultConfig () 58return { 59width =512 , 60height =256 , 61layout ={ 62{ 63classname ="ControllerDisplay" , 64pos ={ 65x =0 , 66y =0 , 67z =1 68} , 69size ={ 70height =256 , 71width =512 72} , 73visible =true , 74accessors ={ 75m_iPort =1 76} , 77scale ={ 78x =1 , 79y =1 80} 81} 82} 83} 84end 85 86function gui . loadConfig ( config ) 87local canvas =gui . getScenePanel (): GetDisplay () 88canvas : Clear () 89canvas : SetSize ( config . width , config . height ) 90 91for i , panel in ipairs ( config . layout ) do 92local p =gui . createScenePanel ( panel . classname ) 93p : SetPos ( panel . pos . x , panel . pos . y ) 94p : SetZPos ( panel . pos . z ) 95p : SetSize ( panel . size . width , panel . size . height ) 96p : SetVisible ( panel . visible ) 97p : SetScale ( panel . scale . x , panel . scale . y ) 98for key , value in pairs ( panel . accessors ) do 99p . __accessors [ key ] =value 100end 101end 102end 103 104function gui . saveSceneLayout () 105local file =nfd . save ( "json" , "layout.json" ) 106 107if not file then return end 108 109local canvas =gui . getScenePanel (): GetDisplay () 110 111local config ={ 112width =canvas : GetWidth (), 113height =canvas : GetHeight (), 114layout ={} 115} 116 117for k , child in ipairs ( gui . m_pSceneManager : GetDisplay (): GetChildren ()) do 118config . layout [ k ] =child : GetConfig () 119end 120 121local data =json . encode ( config , true ) 122 123--assert(love.filesystem.write(file, data, #data)) 124 125local f , err =io . open ( file , "w" ) 126assert ( f , err ) 127 128f : write ( data ) 129f : close () 130end 131 132function gui . loadSceneLayout ( file ) 133 134end 135 136function gui . resize ( w , h ) 137gui . getWorldPanel (): SizeToScreen () 138gui . getWorldPanel (): InvalidateLayout () 139 140--gui.m_pSceneManager:SizeToScreen() 141--gui.m_pWorldPanel:InvalidateLayout() 142end 143 144function gui . registerSkin ( name , tbl ) 145gui . m_tRegisteredSkins [ name ] =tbl 146end 147 148function gui . register ( name , tbl , base ) 149class . register ( name , base )( tbl ) 150end 151 152function gui . create ( name , parent ) 153local obj =class . new ( name ) 154obj : SetParent ( parent or gui . m_pWorldPanel ) 155return obj 156end 157 158function gui . createScenePanel ( name ) 159return gui . m_pSceneManager : AddToScene ( name ) 160end 161 162function gui . setSkin ( str ) 163gui . m_strSkin =str 164end 165 166function gui . getSkin () 167return gui . m_strSkin 168end 169 170function gui . skinHook ( hook , class , panel , ... ) 171local skin =gui . m_tRegisteredSkins [ gui . m_strSkin ] 172if not skin then return end 173if not skin [ hook .. class ] then return error (( "no function '%s' in skin '%s'" ): format ( hook .. class , gui . m_strSkin )) end 174skin [ hook .. class ]( skin , panel , ... ) 175end 176 177function gui . getInfoBar () 178return gui . m_pInfoBar 179end 180 181function gui . getWorldPanel () 182return gui . m_pWorldPanel 183end 184 185function gui . getScenePanel () 186return gui . m_pSceneManager 187end 188 189function gui . joyPressed ( joy , but ) 190gui . getHoveredPanel (): OnJoyPressed ( joy , but ) 191end 192 193function gui . joyReleased ( joy , but ) 194gui . getHoveredPanel (): OnJoyReleased ( joy , but ) 195end 196 197function gui . isInEditorMode () 198return gui . m_bEditorMode 199end 200 201function gui . toggleEditorMode () 202-- Toggle editor mode 203gui . m_bEditorMode =not gui . m_bEditorMode 204 205local scene =gui . getScenePanel () 206 207-- Get the size of the display canvas 208local w , h =scene : GetDisplay (): GetSize () 209 210-- Get the original window size and flags 211local ow , oh , flags =love . window . getMode () 212 213scene : SetEditorMode ( gui . m_bEditorMode ) 214 215local uw , uh =scene : GetUnusableSpace () 216 217-- Resize min/max bounds and toggle resizing 218flags . resizable =gui . m_bEditorMode 219 220if gui . m_bEditorMode then 221-- Readjust the window size to fit the taskbars and stuff 222w , h =w + uw , h + uh 223 224-- Minimum size we will allow 225flags . minwidth =512 + uw 226flags . minheight =256 + uh 227else 228flags . minwidth =w 229flags . minheight =h 230end 231 232love . window . setMode ( w , h , flags ) 233 234local world =gui . getWorldPanel () 235world : SizeToScreen () 236world : InvalidateLayout () 237world : ValidateLayout () 238end 239 240function gui . keyPressed ( key , scancode , isrepeat ) 241if key == "escape" and not isrepeat then 242gui . toggleEditorMode () 243end 244gui . m_pFocusedPanel : OnKeyPressed ( key , isrepeat ) 245end 246 247function gui . keyReleased ( key ) 248gui . m_pFocusedPanel : OnKeyReleased ( key ) 249end 250 251function gui . textInput ( text ) 252gui . m_pFocusedPanel : OnTextInput ( text ) 253end 254 255function gui . render () 256if gui . m_pWorldPanel then 257gui . m_pWorldPanel : Render () 258end 259end 260 261function gui . mouseMoved ( x , y , dx , dy , istouch ) 262local panel =gui . getFocusedPanel () 263local lx , ly =panel : WorldToLocal ( x , y ) 264if not panel : OnMouseMoved ( lx , ly , dx , dy , istouch ) then 265local parent =panel : GetParent () 266while parent do 267lx , ly =parent : WorldToLocal ( x , y ) 268if parent : OnMouseMoved ( lx , ly , dx , dy , istouch ) then break end 269parent =parent : GetParent () 270end 271end 272end 273 274function gui . mousePressed ( x , y , button , istouch , presses ) 275local panel =gui . getHoveredPanel () 276gui . setFocusedPanel ( panel ) 277 278local lx , ly =panel : WorldToLocal ( x , y ) 279if not panel : OnMousePressed ( lx , ly , button , istouch , presses ) then 280local parent =panel : GetParent () 281while parent do 282lx , ly =parent : WorldToLocal ( x , y ) 283if parent : OnMousePressed ( lx , ly , button , istouch , presses ) then break end 284parent =parent : GetParent () 285end 286end 287end 288 289function gui . mouseReleased ( x , y , button , istouch , presses ) 290local panel =gui . getFocusedPanel () 291local lx , ly =panel : WorldToLocal ( x , y ) 292if not panel : OnMouseReleased ( lx , ly , button , istouch , presses ) then 293local parent =panel : GetParent () 294while parent do 295lx , ly =parent : WorldToLocal ( x , y ) 296if parent : OnMouseReleased ( lx , ly , button , istouch , presses ) then break end 297parent =parent : GetParent () 298end 299end 300end 301 302function gui . mouseWheeled ( x , y ) 303local panel =gui . getHoveredPanel () 304if not panel : OnMouseWheeled ( x , y ) then 305local parent =panel : GetParent () 306while parent do 307if parent : OnMouseWheeled ( x , y ) then break end 308parent =parent : GetParent () 309end 310end 311end 312 313function gui . tick ( dt ) 314gui . m_pWorldPanel : ValidateLayout () 315gui . m_pWorldPanel : CallAll ( "Think" , dt ) 316gui . m_pWorldPanel : CleanupOrphans () 317gui . m_pWorldPanel : CleanupDeleted () 318end 319 320function gui . shutdown () 321-- TODO: Save layout of SceneDisplay GUI 322end 323 324function gui . init () 325gui . loadSkins ( "modules/gui/skins" ) 326gui . loadClasses ( "modules/gui/panels/core" ) 327gui . loadClasses ( "modules/gui/panels/editor" ) 328gui . loadClasses ( "modules/gui/panels/smash" ) 329class . init () -- Initialize all classes, sets inheritance 330 331gui . m_pWorldPanel =gui . create ( "Panel" ) 332gui . m_pWorldPanel : DockMargin ( 0 , 0 , 0 , 0 ) 333gui . m_pWorldPanel : DockPadding ( 0 , 0 , 0 , 0 ) 334gui . m_pWorldPanel : SetBGColor ( color_blank ) 335gui . m_pWorldPanel : SetBorderColor ( color_blank ) 336gui . m_pWorldPanel : SizeToScreen () 337 338gui . m_pFocusedPanel =gui . m_pWorldPanel 339 340gui . m_pSceneManager =gui . m_pWorldPanel : Add ( "SceneEditor" ) 341gui . m_pSceneManager : Dock ( DOCK_FILL ) 342end 343 344do 345local lfs =love . filesystem 346 347local function loadFilesWithGUIEnv ( folder ) 348for i , path in ipairs ( lfs . getDirectoryItems ( folder )) do 349local file =string . format ( "%s/%s" , folder , path ) 350 351if lfs . getInfo ( file , "file" ) then 352local chunk , err =lfs . load ( file ) 353if err then 354return error ( err ) 355end 356 357-- Create a copy of our Lua environment, and merge it with some helper functions 358local env =table . merge ( table . copy ( _G ), require ( "gui.env" )) 359 360-- Update the _G variable 361env . _G =env 362 363-- Reset these to a fresh state, since the script will probably use one of them. 364env . PANEL ={} 365env . SKIN ={} 366 367-- Set the environment to our copy of the global environment 368setfenv ( chunk , env ) 369chunk () -- Execute the file 370end 371end 372end 373 374function gui . loadSkins ( folder ) 375loadFilesWithGUIEnv ( folder ) 376end 377 378function gui . loadClasses ( folder ) 379require ( "gui.panels.base" ) 380loadFilesWithGUIEnv ( folder ) 381end 382end 383 384return gui