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