fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
29daa42
GUI
1local PANEL ={} 2 3function PANEL : Initialize () 4-- Initialize values within Panel:Initialize() 5self : super () 6 7-- Display should have no padding or margin 8-- since it will match the size of the window 9self : DockPadding ( 0 , 0 , 0 , 0 ) 10self : DockMargin ( 0 , 0 , 0 , 0 ) 11 12-- Transparent for use with OBS game capture 13self : SetBGColor ( color_blank ) 14self : SetBorderColor ( color_blank ) 15 16-- Member values 17self . m_iSnapThreshold =6 18self . m_pHovered =nil 19self . m_pGrabbed =nil 20self . m_pSelected =nil 21self . m_bDidDrag =false 22self . m_tGrabbedOffset ={ x =0 , y =0 } 23end 24 25function PANEL : PrePaint ( w , h ) 26end 27 28function PANEL : Paint ( w , h ) 29self : super ( "Paint" , w , h ) 30end 31 32function PANEL : PaintOverlay ( w , h ) 33-- Only draw selection in editor mode and if we have a grabbed panel 34if self : IsInEditorMode () then 35 36-- Only draw hovered selection if we are not currently grabbing something 37if not self . m_bDidDrag and self . m_pHovered and self . m_pHovered ~= self then 38local x , y =self . m_pHovered : GetPos () 39local w , h =self . m_pHovered : GetSize () 40 41-- Draw a blue selection for objects we are hovering 42graphics . setLineStyle ( "rough" ) 43graphics . setLineWidth ( 3 ) 44graphics . setColor ( color_blue ) 45graphics . rectangle ( "line" , x , y , w , h ) 46end 47 48if self . m_pSelected then 49local x , y =self . m_pSelected : GetPos () 50local w , h =self . m_pSelected : GetSize () 51 52-- Draw a red selection for objects we have selected 53graphics . setLineStyle ( "rough" ) 54graphics . setLineWidth ( 3 ) 55graphics . setColor ( color_red ) 56graphics . rectangle ( "line" , x , y , w , h ) 57end 58end 59end 60 61function PANEL : IsInEditorMode () 62-- Check the parent for this setting 63return self : GetParent (): IsInEditorMode () 64end 65 66function PANEL : OnMousePressed ( x , y , button , istouch , presses ) 67-- Stop if the button isn't a left click or we're not in editor mode 68if button ~= 1 or not self : IsInEditorMode () then return end 69 70-- Convert x and y coordinates to global screen values 71x , y =self : LocalToWorld ( x , y ) 72 73-- Select an object if we don't have one already, or we clicked outside of the bounds of previous selection 74if not self . m_pSelected or not self . m_pSelected : IsWorldPointInside ( x , y ) then 75self . m_pSelected =self : GetHoveredChild ( x , y ) 76end 77 78-- If we didn't select anything, don't capture the mouse pressed event 79if not self . m_pSelected then return true end 80 81-- Set that we are grabbing a panel 82self . m_pGrabbed =self . m_pSelected 83 84-- We started a new click, turn off the drag flag 85self . m_bDidDrag =false 86 87-- Translate mouse position back to local position within the SceneDisplay 88x , y =self . m_pGrabbed : WorldToLocal ( x , y ) 89 90self . m_tGrabbedOffset ={ x =x , y =y } 91return true -- Stop the mouse event from going up the parent list 92end 93 94function PANEL : OnMouseReleased ( x , y , but ) 95-- Stop if the button isn't a left click or we're not in editor mode 96if but ~= 1 or not self : IsInEditorMode () then return end 97 98-- If we didn't drag anything... 99if not self . m_bDidDrag then 100x , y =self : LocalToWorld ( x , y ) 101 102-- Get the panel we are hoving over, and ignore what we already have selected 103-- May have to change this to Z-Position, as there could be more than 1 object in the background 104self . m_pSelected =self : GetHoveredChild ( x , y , self . m_pSelected ) or self . m_pSelected 105end 106 107self . m_bDidDrag =false 108 109-- We are no longer grabbing the panel 110self . m_pGrabbed =nil 111end 112 113function PANEL : Think ( dt ) 114-- Translate mouse position to screenspace 115local lx , ly =self : LocalToWorld ( love . mouse . getPosition ()) 116 117-- Ignore selection to select and mark panels behind it as hovered over 118self . m_pHovered =self : GetHoveredChild ( lx , ly , self . m_pSelected ) 119end 120 121function PANEL : OnMouseMoved ( mx , my , dx , dy , istouch ) 122-- Translate mouse position to screenspace 123local lx , ly =self : LocalToWorld ( mx , my ) 124 125-- Stop if nothing is grabbed or the grabbed object is docked 126if not self . m_pGrabbed or self . m_pGrabbed : GetDock () ~= 0 then return end 127 128-- Flag that we moved the mouse while grabbing an object 129self . m_bDidDrag =true 130 131-- Get the size of the grabbed object 132local w , h =self . m_pGrabbed : GetSize () 133 134-- Get the size of the scene display 135local pw , ph =self : GetSize () 136 137local t =self . m_iSnapThreshold 138 139-- Get the offset of the mouse 140local gx , gy =self . m_tGrabbedOffset . x , self . m_tGrabbedOffset . y 141 142-- Translate mouse position to panels position 143local x , y =mx - gx , my - gy 144 145-- Position plus size 146local xw , yh =x + w , y + h 147 148-- Edge snapping on display edges 149 150-- Edge snap on x axis 151if x <= t and x >= - t then 152-- Snap to left of the screen 153x =0 154elseif xw <= pw + t and xw >= pw - t then 155-- Snap to right of the screen 156x =pw - w 157end 158 159-- Edge snap on y axis 160if y <= t and y >= - t then 161-- Snap to top of the screen 162y =0 163elseif yh <= ph + t and yh >= ph - t then 164-- Snap to bottom of the screen 165y =ph - h 166end 167 168-- Edge snapping on other objects 169 170for k , child in ipairs ( self : GetChildren ()) do 171-- Ignore what we are grabbing 172if child ~= self . m_pGrabbed then 173-- Get child size 174cw , ch =child : GetSize () 175 176-- Get child position 177local cx , cy =child : GetPos () 178 179-- Position plus size 180local cxw , cyh =cx + cw , cy + ch 181 182-- Edge snapping on x axis 183if x <= cx + t and x >= cx - t then 184-- Snap left edge of panel to left edge of other panel 185x =cx 186elseif x <= cxw + t and x >= cxw - t then 187-- Snap left edge of panel to right edge of other panel 188x =cxw 189elseif xw <= cx + t and xw >= cx - t then 190-- Snap right edge of panel to left edge of other panel 191x =cx - w 192elseif xw <= cxw + t and xw >= cxw - t then 193-- Snap right edge of panel to right edge of other panel 194x =cxw - w 195end 196 197-- Edge snapping on y axis 198if y <= cy + t and y >= cy - t then 199-- Snap top edge of panel to top edge of other panel 200y =cy 201elseif y <= cyh + t and y >= cyh - t then 202-- Snap top edge of panel to bottom edge of other panel 203y =cyh 204elseif yh <= cy + t and yh >= cy - t then 205-- Snap bottom edge of panel to top edge of other panel 206y =cy - h 207elseif yh <= cyh + t and yh >= cyh - t then 208-- Snap bottom edge of panel to bottom edge of other panel 209y =cyh - h 210end 211end 212end 213 214-- Finally set our snapped position 215self . m_pGrabbed : SetPos ( x , y ) 216end 217 218gui . register ( "SceneDisplay" , PANEL , "Panel" )