fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
7d53a4b
settings-overhaul
1local gui ={ 2m_tRegisteredSkins ={} , 3m_pWorldPanel =nil , 4m_pHoveredPanel =nil , 5m_pFocusedPanel =nil , 6m_pTooltip =nil , 7m_strSkin ="default" , 8} 9 10DOCK_NONE =0 11DOCK_TOP =1 12DOCK_LEFT =2 13DOCK_BOTTOM =4 14DOCK_RIGHT =8 15DOCK_FILL =16 16 17CENTER_NONE =0 18CENTER_VERTICAL =1 19CENTER_HORIZONTAL =2 20 21local object =require ( "class.object" ) 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 . getTooltip () 36return gui . m_pTooltip 37end 38 39function gui . setTooltip ( title , body ) 40gui . m_pTooltip : SetTitle ( title ) 41gui . m_pTooltip : SetBody ( body ) 42-- Position our tooltip somewhere around the hovered object 43 44gui . m_pTooltip : SetArrowPos ( gui . m_pTooltip : SetPosAround ( gui . m_pHoveredPanel )) 45end 46 47function gui . showTooltip () 48gui . m_pTooltip : SetVisible ( true ) 49end 50 51function gui . hideTooltip () 52gui . m_pTooltip : SetVisible ( false ) 53gui . m_fTooltipTimer =nil 54end 55 56function gui . isToolTipVisible () 57return gui . m_pTooltip : IsVisible () 58end 59 60function gui . updateHoveredPanel () 61local hovered =gui . m_pWorldPanel : GetHoveredPanel ( love . mouse . getPosition ()) 62if gui . m_pHoveredPanel ~= hovered then 63local time =love . timer . getTime () 64 65gui . m_fTooltipTimer =nil 66if gui . isToolTipVisible () then 67gui . hideTooltip () 68gui . m_fTooltipTimerCarryOver =time + 0.35 69end 70 71gui . m_pHoveredPanel : OnHoveredChanged ( false ) 72gui . m_pHoveredPanel =hovered 73gui . m_pHoveredPanel : OnHoveredChanged ( true ) 74 75if gui . m_pHoveredPanel : OnQuertyTooltip () then 76if gui . m_fTooltipTimerCarryOver and gui . m_fTooltipTimerCarryOver >= time then 77gui . showTooltip () 78else 79gui . m_fTooltipTimer =time + 0.75 80end 81end 82end 83end 84 85function gui . getHoveredPanel () 86return gui . m_pHoveredPanel 87end 88 89function gui . getMousePosition () 90return gui . m_pWorldPanel : WorldToLocal ( love . mouse . getPosition ()) 91end 92 93function gui . resize ( w , h ) 94gui . m_pWorldPanel : SizeToScreen () 95gui . m_pWorldPanel : InvalidateLayout () 96end 97 98function gui . newSkin ( name ) 99local tbl ={} 100gui . m_tRegisteredSkins [ name ] =tbl 101return tbl 102end 103 104function gui . create ( name , parent ) 105local obj =object . new ( name ) 106obj : SetParent ( parent or gui . m_pWorldPanel ) 107return obj 108end 109 110function gui . setSkin ( str ) 111gui . m_strSkin =str 112end 113 114function gui . getSkin () 115return gui . m_strSkin 116end 117 118function gui . skinHook ( hook , class , panel , ... ) 119local skin =gui . m_tRegisteredSkins [ gui . m_strSkin ] 120if not skin then return end 121if not skin [ hook .. class ] then return error (( "no function '%s' in skin '%s'" ): format ( hook .. class , gui . m_strSkin )) end 122skin [ hook .. class ]( skin , panel , ... ) 123end 124 125function gui . getWorldPanel () 126return gui . m_pWorldPanel 127end 128 129function gui . joyPressed ( joy , but ) 130gui . m_pHoveredPanel : OnJoyPressed ( joy , but ) 131end 132 133function gui . joyReleased ( joy , but ) 134gui . m_pHoveredPanel : OnJoyReleased ( joy , but ) 135end 136 137function gui . keyPressed ( key , scancode , isrepeat ) 138--if key == "escape" and not isrepeat then 139--end 140gui . m_pFocusedPanel : OnKeyPressed ( key , isrepeat ) 141gui . m_pHoveredPanel : OnHoveredKeyPressed ( key , isrepeat ) 142end 143 144function gui . keyReleased ( key ) 145gui . m_pFocusedPanel : OnKeyReleased ( key ) 146gui . m_pHoveredPanel : OnHoveredKeyReleased ( key ) 147end 148 149function gui . textInput ( text ) 150gui . m_pFocusedPanel : OnTextInput ( text ) 151gui . m_pHoveredPanel : OnHoveredTextInput ( text ) 152end 153 154do 155local INFO =love . graphics . newImage ( "textures/gui/information.png" ) 156 157function gui . render () 158if gui . m_pWorldPanel then 159gui . m_pWorldPanel : Render () 160end 161 162local time =love . timer . getTime () 163if gui . m_fTooltipTimer and gui . m_fTooltipTimer > time then 164local mx , my =gui . getMousePosition () 165local start =- 90 166local percent =1 - (( gui . m_fTooltipTimer - time ) / 0.75 ) 167local progress =math . min ( math . max ( 18 , percent * 360 ), 345 ) 168 169love . graphics . setLineStyle ( "smooth" ) 170love . graphics . setLineWidth ( 2 ) 171 172local r =9 173 174love . graphics . setColor ( color_black ) 175love . graphics . circle ( "line" , mx + 9 + r , my , r ) 176love . graphics . setColor ( color_blue ) 177love . graphics . arc ( "line" , mx + 9 + r , my , r , math . rad ( start ), math . rad ( start + progress )) 178love . graphics . setColor ( color_white ) 179love . graphics . easyDraw ( INFO , mx + 9 , my - r , 0 , 18 , 18 ) 180end 181end 182end 183 184function gui . mouseMoved ( x , y , dx , dy , istouch ) 185gui . updateHoveredPanel () 186local panel =gui . getFocusedPanel () 187local lx , ly =panel : WorldToLocal ( x , y ) 188if not panel : OnMouseMoved ( lx , ly , dx , dy , istouch ) then 189local parent =panel : GetParent () 190while parent do 191lx , ly =parent : WorldToLocal ( x , y ) 192if parent : OnMouseMoved ( lx , ly , dx , dy , istouch ) then break end 193parent =parent : GetParent () 194end 195end 196end 197 198function gui . mousePressed ( x , y , button , istouch , presses ) 199local panel =gui . m_pHoveredPanel 200gui . setFocusedPanel ( panel ) 201 202local lx , ly =panel : WorldToLocal ( x , y ) 203if not panel : OnMousePressed ( lx , ly , button , istouch , presses ) then 204local parent =panel : GetParent () 205while parent do 206lx , ly =parent : WorldToLocal ( x , y ) 207if parent : OnMousePressed ( lx , ly , button , istouch , presses ) then break end 208parent =parent : GetParent () 209end 210end 211end 212 213function gui . mouseReleased ( x , y , button , istouch , presses ) 214local panel =gui . getFocusedPanel () 215local lx , ly =panel : WorldToLocal ( x , y ) 216if not panel : OnMouseReleased ( lx , ly , button , istouch , presses ) then 217local parent =panel : GetParent () 218while parent do 219lx , ly =parent : WorldToLocal ( x , y ) 220if parent : OnMouseReleased ( lx , ly , button , istouch , presses ) then break end 221parent =parent : GetParent () 222end 223end 224end 225 226function gui . mouseWheeled ( x , y ) 227gui . updateHoveredPanel () 228local panel =gui . m_pHoveredPanel 229if not panel : OnMouseWheeled ( x , y ) then 230local parent =panel : GetParent () 231while parent do 232if parent : OnMouseWheeled ( x , y ) then break end 233parent =parent : GetParent () 234end 235end 236end 237 238function gui . update ( dt ) 239gui . m_pWorldPanel : ValidateLayout () 240gui . m_pWorldPanel : CallAll ( "Think" , dt ) 241 242local time =love . timer . getTime () 243 244if gui . m_fTooltipTimer and gui . m_fTooltipTimer <= time and not gui . isToolTipVisible () then 245gui . showTooltip () 246end 247 248gui . m_pWorldPanel : CleanupOrphans () 249gui . m_pWorldPanel : CleanupDeleted () 250end 251 252function gui . shutdown () 253end 254 255function gui . init () 256gui . loadSkins ( "modules/gui/skins" ) 257gui . loadClasses ( "modules/gui/panels" ) 258 259gui . m_pWorldPanel =gui . create ( "Panel" ) 260gui . m_pWorldPanel : DockMargin ( 0 , 0 , 0 , 0 ) 261gui . m_pWorldPanel : DockPadding ( 0 , 0 , 0 , 0 ) 262gui . m_pWorldPanel : SetBGColor ( color_blank ) 263gui . m_pWorldPanel : SetBorderColor ( color_blank ) 264gui . m_pWorldPanel : SizeToScreen () 265gui . m_pWorldPanel : InvalidateLayout () 266gui . m_pWorldPanel : ValidateLayout () 267 268gui . m_pTooltip =gui . create ( "ToolTip" ) 269gui . m_pTooltip : SetZPos ( math . huge ) -- Always on top 270gui . m_pTooltip : SetVisible ( false ) 271 272gui . m_pHoveredPanel =gui . m_pWorldPanel 273gui . m_pFocusedPanel =gui . m_pWorldPanel 274end 275 276do 277local lfs =love . filesystem 278 279local function loadFilesWithGUIEnv ( folder ) 280for i , path in ipairs ( lfs . getDirectoryItems ( folder )) do 281local file =string . format ( "%s/%s" , folder , path ) 282 283if lfs . getInfo ( file , "file" ) then 284local chunk , err =lfs . load ( file ) 285if err then 286return error ( err ) 287end 288 289-- Create a copy of our Lua environment, and merge it with some helper functions 290local env =table . merge ( table . copy ( _G ), require ( "gui.env" )) 291 292-- Update the _G variable 293env . _G =env 294 295-- Set the environment to our copy of the global environment 296setfenv ( chunk , env ) 297chunk () -- Execute the file 298end 299end 300end 301 302function gui . loadSkins ( folder ) 303loadFilesWithGUIEnv ( folder ) 304end 305 306function gui . loadClasses ( folder ) 307loadFilesWithGUIEnv ( folder ) 308end 309end 310 311return gui