fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

blackops7799More config file work (exporting/importing/defaults) Properly cleanup orphaned panels and "removed" panels SetZPos will now reorder the parents child panels MakePopup will take the place of the highest Z positioned panel29daa42

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