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
18.7 KiB823 linesraw
1local gui = require("gui")
2
3require("extensions.table")
4
5local PANEL = {}
6
7local graphics = love.graphics
8
9function PANEL:Initialize()
10	self.m_tChildren = {}
11	self.m_tOrphans = {}
12	self.m_iWorldPosX = 0
13	self.m_iWorldPosY = 0
14	self.m_fScaleX = 1
15	self.m_fScaleY = 1
16	self.m_iPosX = 0
17	self.m_iPosY = 0
18	self.m_iWidth = 42
19	self.m_iHeight = 24
20	self.m_bHovered = false
21	self.m_pParent = nil
22	self.m_iZPos = 0
23	self.m_bValidated = false
24	self.m_bVisible = true
25	self.m_bFocusable = true
26	self.m_bOrphaned = false
27	self.m_bDeleted = false
28	self.m_iDock = 0
29	self.m_tDockMargins = { left = 4, top = 4, right = 4, bottom = 4 }
30	self.m_tDockPadding = { left = 4, top = 4, right = 4, bottom = 4 }
31end
32
33function PANEL:GetClassName()
34	return self.__classname
35end
36
37function PANEL:__tostring()
38	return ("Panel[%q]"):format(self.__classname)
39end
40
41function PANEL:GetConfig()
42	local w, h = self:GetActualSize()
43	local sx, sy = self:GetScale()
44
45	local config = {
46		classname = self:GetClassName(),
47		pos = { x = self:GetX(), y = self:GetY(), z = self:GetZPos() },
48		size = { width = w, height = h },
49		scale = { x = sx, y = sy },
50		visible = self:IsVisible(),
51		accessors = self.__accessors,
52	}
53
54	if self.m_iDock ~= 0 then
55		config.dock = {
56			mode = self.m_iDock,
57			margins = self.m_tDockMargins,
58			padding = self.m_tDockPadding,
59		}
60	end
61
62	return config
63end
64
65function PANEL:SetVisible(b)
66	self.m_bVisible = b
67end
68
69function PANEL:IsVisible()
70	return self.m_bVisible
71end
72
73function PANEL:SetFocusable(b)
74	self.m_bFocusable = b
75end
76
77function PANEL:IsFocusable()
78	return self.m_bFocusable
79end
80
81function PANEL:BringToFront()
82	local parent = self.m_pParent
83	if not parent then return end -- There's nothing for it to go in front of!
84	
85	local highest = nil
86
87	for _,child in ipairs(parent.m_tChildren) do
88		if not highest or child:GetZPos() > highest:GetZPos() then
89			highest = child
90		end
91	end
92
93	-- No panels or the highest is already ourself
94	if not highest or highest == self then return end
95
96	-- Get the position of the highest panel
97	local replace = highest:GetZPos()
98
99	-- Set the highest panel to our current position
100	highest:SetZPos(self:GetZPos())
101
102	-- Set our position to where the highest panel used to be
103	self:SetZPos(replace)
104end
105
106function PANEL:Dock(i)
107	self.m_iDock = i
108	self:InvalidateLayout()
109end
110
111function PANEL:GetDock()
112	return self.m_iDock
113end
114
115function PANEL:DockMargin(left, top, right, bottom)
116	self.m_tDockMargins.left = left
117	self.m_tDockMargins.top = top
118	self.m_tDockMargins.right = right
119	self.m_tDockMargins.bottom = bottom
120end
121
122function PANEL:GetDockMargin()
123	return self.m_tDockMargins
124end
125
126function PANEL:DockPadding(left, top, right, bottom)
127	self.m_tDockPadding.left = left
128	self.m_tDockPadding.top = top
129	self.m_tDockPadding.right = right
130	self.m_tDockPadding.bottom = bottom
131end
132
133function PANEL:GetDockPadding()
134	return self.m_tDockPadding
135end
136
137function PANEL:GetWidthPlusMargin()
138	if not self:IsVisible() then return 0 end
139	local margins = self:GetDockMargin()
140	return margins.left + margins.right + self:GetWidth()
141end
142
143function PANEL:GetHeightPlusMargin()
144	if not self:IsVisible() then return 0 end
145	local margins = self:GetDockMargin()
146	return margins.top + margins.bottom + self:GetHeight()
147end
148
149function PANEL:GetSizePlusMargin()
150	local w, h = self:GetWidthPlusMargin(), self:GetHeightPlusMargin()
151
152	--[[for _,child in ipairs(self.m_tChildren) do
153		if child:IsVisible() then
154			margins = child:GetDockMargin()
155			padding = child:GetDockPadding()
156			w = w + margins.left + margins.right + padding.left + padding.right
157			h = h + margins.top + margins.bottom + padding.top + padding.bottom
158		end
159	end]]
160
161	return w, h
162end
163
164--[[function PANEL:GetSpaceAround(panel)
165	local margins = panel:GetDockMargin()
166	local padding = panel:GetDockPadding()
167
168	local w = margins.left + margins.right + padding.left + padding.right
169	local h = margins.top + margins.bottom + padding.top + padding.bottom
170
171	for _,child in ipairs(self.m_tChildren) do
172		if child ~= panel and child:IsVisible() then
173			local margins = child:GetDockMargin()
174			local padding = child:GetDockPadding()
175			w = w + margins.left + margins.right + padding.left + padding.right + child:GetWidth()
176			h = h + margins.top + margins.bottom + padding.top + padding.bottom + child:GetHeight()
177		end
178	end
179
180	local width, height = self:GetSize()
181
182	return w, h
183end]]
184
185function PANEL:SetZPos(i)
186	self.m_iZPos = i
187	self:GetParent():ReorderChildren()
188end
189
190function PANEL:ReorderChildren()
191	table.sort(self.m_tChildren, function(a, b) return a.m_iZPos < b.m_iZPos end)
192end
193
194function PANEL:GetZPos()
195	return self.m_iZPos
196end
197
198function PANEL:CallAll(func, ...)
199	if not self:IsVisible() then return end
200	self[ func ](self, ...)
201	for _,child in ipairs(self.m_tChildren) do
202		child:CallAll(func, ...)
203	end
204end
205
206function PANEL:CallSelfAndParents(func, ...)
207	if not self:IsVisible() then return end
208	if self[ func ](self, ...) then return end
209	local parent = self.m_pParent
210	if not parent then return end
211	parent:CallSelfAndParents(func, ...)
212end
213
214function PANEL:Remove()
215	local parent = self:GetParent()
216	if parent then
217		parent:OnChildRemoved(self)
218	end
219
220	self.m_bDeleted = true
221
222	for zpos, child in ipairs(self.m_tChildren) do
223		child:Remove()
224	end
225end
226
227function PANEL:Clear()
228	for zpos, child in ipairs(self.m_tChildren) do
229		child:Remove()
230	end
231end
232
233function PANEL:InvalidateLayout()
234	self.m_bValidated = false
235	for _,child in ipairs(self.m_tChildren) do
236		child:InvalidateLayout()
237	end
238end
239
240function PANEL:InvalidateParent()
241	if not self.m_pParent then return end
242	self.m_pParent:InvalidateLayout()
243end
244
245function PANEL:MarkAsOrphan()
246	self.m_bOrphaned = true
247end
248
249function PANEL:CleanupOrphans()
250	for key, child in reversedipairs(self.m_tChildren) do
251		child:CleanupOrphans()
252		if child.m_bOrphaned then
253			table.remove(self.m_tChildren, key)
254		end
255	end
256end
257
258function PANEL:CleanupDeleted()
259	for key, child in reversedipairs(self.m_tChildren) do
260		child:CleanupDeleted()
261		if child.m_bDeleted then
262			self.m_tChildren[key] = nil
263			table.remove(self.m_tChildren, key)
264		end
265	end
266end
267
268function PANEL:SetParent(parent)
269	if self.m_pParent then
270		self:MarkAsOrphan()
271	end
272	self.m_pParent = parent
273	if parent then
274		table.insert(parent.m_tChildren, self)
275		parent:OnChildAdded(self)
276		self:SetZPos(#parent.m_tChildren)
277	end
278end
279
280function PANEL:HasParent()
281	return self:GetParent() ~= nil
282end
283
284function PANEL:GetParent()
285	return self.m_pParent
286end
287
288function PANEL:GetParents()
289	local parents = {}
290	
291	local parent = self:GetParent()
292	
293	while parent do
294		table.insert(parents, parent)
295		parent = parent:GetParent()
296	end
297	return parents
298end
299
300function PANEL:GetChildren()
301	return self.m_tChildren
302end
303
304function PANEL:GetFamily(family)
305	local family = family or {}
306	
307	local children = self.m_tChildren
308	family[self] = children
309	
310	for _,child in ipairs(children) do
311		child:GetFamily(family[self])
312	end
313	
314	return family
315end
316
317function PANEL:SizeToScreen()
318	self:SetSize(graphics.getPixelDimensions())
319end
320
321function PANEL:SizeToChildren(doWidth, doHeight)
322	local w,h = 0, 0
323	local padding = self:GetDockPadding()
324
325	w = padding.left	-- + padding.right
326	h = padding.top		-- + padding.bottom
327
328	local lw, lh = 0, 0
329
330	for _,child in ipairs(self.m_tChildren) do
331		if child:IsVisible() then
332			local x, y = child:GetPos()
333			local margin = child:GetDockMargin()
334			local cw, ch = child:GetSize()
335
336			-- Position + size + margins = maximum bounds
337			cw = x + cw + margin.left + margin.right
338			ch = y + ch + margin.top + margin.bottom
339
340			-- Update the largest widths and heights
341			if cw > lw then lw = cw end
342			if ch > lh then lh = ch end
343		end
344	end
345	if doWidth then self:SetWidth(w + lw) end
346	if doHeight then self:SetHeight(h + lh) end
347end
348
349function PANEL:Add(class)
350	return gui.create(class, self)
351end
352
353function PANEL:SetScale(x, y)
354	y = y or x
355	self.m_fScaleX, self.m_fScaleY = x, y
356end
357
358function PANEL:GetScale()
359	return self.m_fScaleX, self.m_fScaleY
360end
361
362function PANEL:SetPos(x, y)
363	self.m_iPosX, self.m_iPosY = x, y
364end
365
366function PANEL:GetPos()
367	return self.m_iPosX, self.m_iPosY
368end
369
370function PANEL:SetWorldPos(x, y)
371	self.m_iWorldPosX, self.m_iWorldPosY = x, y
372end
373
374function PANEL:GetWorldPos()
375	return self.m_iWorldPosX, self.m_iWorldPosY
376end
377
378function PANEL:LocalToWorld(x, y)
379	local sx, sy = self:GetPos()
380	x = x + sx
381	y = y + sy
382	
383	local parent = self:GetParent()
384	
385	while parent do
386		local px, py = parent:GetPos()
387		x = x + px
388		y = y + py
389		parent = parent:GetParent()
390	end
391	
392	return x, y
393end
394PANEL.LocalToScreen = PANEL.LocalToWorld
395
396function PANEL:WorldToLocal(x, y)
397	local sx, sy = self:LocalToScreen(0, 0)
398	return x - sx, y - sy
399end
400PANEL.ScreenToLocal = PANEL.WorldToLocal
401
402function PANEL:SetX(x)
403	self.m_iPosX = x
404end
405
406function PANEL:GetX()
407	return self.m_iPosX
408end
409
410function PANEL:SetY(y)
411	self.m_iPosY = y
412end
413
414function PANEL:GetY()
415	return self.m_iPosY
416end
417
418function PANEL:SetSize(w, h)
419	self.m_iWidth, self.m_iHeight = w, h
420	self:OnResize(w, h)
421end
422
423function PANEL:GetSize()
424	return self.m_iWidth * self.m_fScaleX, self.m_iHeight * self.m_fScaleY
425end
426
427function PANEL:GetActualSize()
428	return self.m_iWidth, self.m_iHeight
429end
430
431function PANEL:SetWidth(w)
432	if self.m_iWidth ~= w then
433		self.m_iWidth = w
434		--self:InvalidateLayout()
435	end
436end
437
438function PANEL:GetWidth()
439	return self.m_iWidth * self.m_fScaleX
440end
441
442function PANEL:SetHeight(h)
443	if self.m_iHeight ~= h then
444		self.m_iHeight = h
445		--self:InvalidateLayout()
446	end
447end
448
449function PANEL:GetHeight()
450	return self.m_iHeight * self.m_fScaleY
451end
452
453function PANEL:IsWorldPointInside(x, y)
454	local px, py = self:LocalToScreen(0, 0)
455	return x > px and x < px + self:GetWidth() and y > py and y < py + self:GetHeight()
456end
457
458function PANEL:Render()
459	if not self:IsVisible() then return end
460
461	local x, y = self:LocalToScreen(0, 0)
462	local w, h = self:GetSize()
463	
464	local parent = self:GetParent()
465	
466	-- Start the scissor position and size with our own values
467	local sx, sy = x, y
468	local sw, sh = w, h
469
470	while parent do
471		-- If we have a parent, fit the scissor to fit inside their bounds
472		local px, py = parent:LocalToScreen(0, 0)
473		local pw, ph = parent:GetSize()
474
475		if sx < px then
476			sw = math.max(0, sw + sx - px)
477			sx = px
478		end
479		if sx + sw > px + pw then
480			sw = math.max(0, sw - ((sx + sw) - (px + pw)))
481			sx = math.min(sx, px + pw)
482		end
483		if sy < py then
484			sh = math.max(0, sh + sy - py)
485			sy = py
486		end
487		if sy + sh > py + ph then
488			sh = math.max(0, sh - ((sy + sh) - (py + ph)))
489			sy = math.min(sy, py + ph)
490		end
491
492		parent = parent:GetParent()
493	end
494
495	graphics.push() -- Push the current graphics state
496		graphics.setScissor(sx, sy, sw, sh) -- Set our scissor so things can't be drawn outside the panel
497			graphics.translate(x, y) -- Translate so Paint has localized position values for drawing objects
498			graphics.scale(self:GetScale())
499				local uw, uh = self:GetActualSize()
500				self:SetWorldPos(x, y)
501				graphics.setColor(255, 255, 255, 255)
502				self:PrePaint(uw, uh)
503				graphics.setColor(255, 255, 255, 255)
504				self:Paint(uw, uh)
505				graphics.setColor(255, 255, 255, 255)
506				self:PostPaint(uw, uh)
507			graphics.origin()
508		graphics.setScissor()
509	graphics.pop() -- Reset the graphics state to what it was
510
511	-- recently added panels are drawn last, thus, ontop of older panels
512	for _, child in ipairs(self.m_tChildren) do
513		child:Render()
514	end
515
516	graphics.push()
517		graphics.setScissor(sx, sy, sw, sh)
518			graphics.translate(x, y)
519			graphics.scale(self:GetScale())
520				graphics.setColor(255, 255, 255, 255)
521				self:PaintOverlay(self:GetActualSize())
522			graphics.origin()
523		graphics.setScissor()
524	graphics.pop() -- Reset the graphics state to what it was
525
526	if self.m_bDebug then
527		-- Debug the scissor rect
528		graphics.setColor(255, 0, 0, 25)
529		graphics.rectangle("fill", sx, sy, sw, sh)
530	end
531end
532
533function PANEL:ValidateLayout()
534	if not self.m_bValidated then
535		self.m_bValidated = true
536		self:DockLayout()
537		self:PerformLayout()
538	end
539
540	for _,child in ipairs(self.m_tChildren) do
541		child:ValidateLayout()
542	end
543end
544
545function PANEL:Center(vertical, horizontal)
546	local parent = self:GetParent()	
547
548	local w, h = self:GetSize()
549	local pw, ph = graphics.getPixelDimensions() -- Default to window size if no parent
550	if parent then
551		pw, ph = parent:GetSize()
552	end
553
554	-- If both vertical and horizontal aren't set, center to both?
555	local all = not vertical and not horizontal
556
557	if vertical or all then
558		self:SetY((ph / 2) - (h / 2))
559	end
560	if horizontal or all then
561		self:SetX((pw / 2) - (w / 2))
562	end
563end
564
565function PANEL:CenterVertical()
566	self:Center(true, false)
567end
568
569function PANEL:CenterHorizontal()
570	self:Center(false, true)
571end
572
573function PANEL:DockLayout()
574	local x, y = 0, 0
575	local w, h = self:GetSize()
576	
577	local padding = self.m_tDockPadding
578	
579	for _,child in ipairs(self.m_tChildren) do
580		local margin = child.m_tDockMargins
581	
582		local dx = x + padding.left
583		local dy = y + padding.top
584		local dw = w - (padding.left + padding.right)
585		local dh = h - (padding.top + padding.bottom)
586	
587		local dock = child.m_iDock
588		if dock ~= DOCK_NONE and child:IsVisible() then
589			if(dock == DOCK_TOP) then
590				child:SetPos(dx + margin.left, dy + margin.top)
591				child:SetSize(dw - margin.left - margin.right, child:GetHeight())
592				local height = margin.top + margin.bottom + child:GetHeight()
593				y = y + height
594				h = h - height
595			elseif(dock == DOCK_LEFT) then
596				child:SetPos(dx + margin.left, dy + margin.top)
597				child:SetSize(child:GetWidth(), dh - margin.top - margin.bottom)
598				local width = margin.left + margin.right + child:GetWidth()
599				x = x + width
600				w = w - width
601			elseif(dock == DOCK_RIGHT) then
602				child:SetPos((dx + dw) - child:GetWidth() - margin.right, dy + margin.top)
603				child:SetSize(child:GetWidth(), dh - margin.top - margin.bottom)
604				local width = margin.left + margin.right + child:GetWidth()
605				w = w - width
606			elseif(dock == DOCK_BOTTOM) then
607				child:SetPos(dx + margin.left, (dy + dh) - child:GetHeight() - margin.bottom)
608				child:SetSize(dw - margin.left - margin.right, child:GetHeight())
609				h = h - (child:GetHeight() + margin.bottom + margin.top)
610			end
611		end
612	end
613	
614	for _,child in ipairs(self.m_tChildren) do
615		local dock = child.m_iDock
616		
617		if dock ~= DOCK_NONE and child:IsVisible() then
618			local margin = child.m_tDockMargins
619		
620			local dx = x + padding.left
621			local dy = y + padding.top
622			local dw = w - (padding.left + padding.right)
623			local dh = h - (padding.top + padding.bottom)
624			
625			if(dock == DOCK_FILL) then
626				child:SetPos(dx + margin.left, dy + margin.top)
627				child:SetSize(dw - margin.left - margin.right, dh - margin.top - margin.bottom)
628			end
629		end
630	end
631end
632
633function PANEL:GiveFocus()
634	gui.setFocusedPanel(self)
635end
636
637function PANEL:MakePopup()
638	self:BringToFront()
639	self:GiveFocus()
640end
641
642function PANEL:HasFocus(checkchildren)
643	if checkchildren then
644		for _,child in ipairs(self.m_tChildren) do
645			if child:HasFocus(checkchildren) then
646				return true
647			end
648		end
649	end
650	return gui.getFocusedPanel() == self
651end
652
653function PANEL:IsHovered()
654	return gui.getHoveredPanel() == self
655end
656
657function PANEL:GetHoveredChild(x, y, ignore)
658	local panel = nil
659	for _,child in reversedipairs(self.m_tChildren) do
660		if child:IsVisible() and child:IsFocusable() and child:IsWorldPointInside(x, y) and ((ignore and child ~= ignore) or not ignore) then
661			panel = child:GetHoveredPanel(x, y)
662			break
663		end
664	end
665	return panel
666end
667
668function PANEL:GetHoveredPanel(x, y, ignore)
669	local panel = self
670	for _,child in reversedipairs(self.m_tChildren) do
671		if child:IsVisible() and child:IsFocusable() and child:IsWorldPointInside(x, y) and ((ignore and child ~= ignore) or not ignore) then
672			panel = child:GetHoveredPanel(x, y)
673			break
674		end
675	end
676	return panel
677end
678
679-- PANEL OVERRIDE DEFAULTS
680
681function PANEL:PerformLayout()
682	-- Called when we are Validating the layout.
683	-- Good for manually positioning child panels.
684end
685
686function PANEL:PrePaint(w, h)
687end
688
689function PANEL:Paint(w, h)
690	-- Called every frame, when we are drawing the panel to the screen.
691	-- Used to draw custom things within the panel.
692end
693
694function PANEL:PostPaint(w, h)
695end
696
697function PANEL:PaintOverlay(w, h)
698end
699
700function PANEL:OnResize(w, h)
701	-- Called when the size of the panel has changed.
702	-- Good for manually positioning child panels, like in PerformLayout.
703end
704
705function PANEL:OnFocusChanged(b)
706	-- Called when the panels focus has either been gained or lost
707end
708
709function PANEL:OnKeyPressed(key, hex)
710	-- Called when the panel is focused, and a keyboard key has been pressed
711end
712
713function PANEL:OnKeyReleased(key)
714	-- Called when the panel is focused, and a keyboard key has been released
715end
716
717function PANEL:OnTextInput(text)
718	-- Called when the panel is focused.
719	-- Good for a text input panel.
720end
721
722function PANEL:OnJoyPressed(joy, but)
723	-- Override
724end
725
726function PANEL:OnJoyReleased(joy, but)
727	-- Override
728end
729
730function PANEL:OnMouseMoved(x, y, dx, dy, istouch)
731	--[[
732	Called when when the mouse has been moved.
733
734	number x
735		The mouse position on the x-axis.
736	number y
737		The mouse position on the y-axis.
738	number dx
739		The amount moved along the x-axis since the last time love.mousemoved was called.
740	number dy
741		The amount moved along the y-axis since the last time love.mousemoved was called.
742
743	Returning true will stop the event from going up the family tree
744	Child->Parent->Parent->Parent->etc, etc..
745	]]
746	
747	return false
748end
749
750function PANEL:OnMousePressed(x, y, button, istouch, presses)
751	--[[
752	Called when a mouse press event has been made.
753
754	number x
755		The mouse position on the x-axis.
756	number y
757		The mouse position on the y-axis.
758	number dx
759		The amount moved along the x-axis since the last time love.mousemoved was called.
760	number dy
761		The amount moved along the y-axis since the last time love.mousemoved was called.
762
763	Returning true will stop the event from going up the family tree
764	Child->Parent->Parent->Parent->etc, etc..
765	]]
766	
767	return false
768end
769
770function PANEL:OnMouseReleased(x, y, button, istouch, presses)
771	--[[
772	Called when a mouse release event has been made.
773
774	number x
775		The mouse position on the x-axis.
776	number y
777		The mouse position on the y-axis.
778	number dx
779		The amount moved along the x-axis since the last time love.mousemoved was called.
780	number dy
781		The amount moved along the y-axis since the last time love.mousemoved was called.
782
783	Returning true will stop the event from going up the family tree
784	Child->Parent->Parent->Parent->etc, etc..
785	]]
786	
787	return false
788end
789
790function PANEL:OnMouseWheeled(x, y)
791	--[[
792	Called when the scrollwheel has been turned.
793
794	number x
795		Amount of horizontal mouse wheel movement. Positive values indicate movement to the right.
796	number y
797		Amount of vertical mouse wheel movement. Positive values indicate upward movement.
798
799	See an example in..
800	gui/panels/core/scrollpanel.lua 
801	&
802	gui/panels/core/scrollbar.lua 
803
804	Returning true will stop the event from going up the family tree
805	Child->Parent->Parent->Parent->etc, etc..
806    ]]
807
808	return false
809end
810
811function PANEL:OnChildAdded(panel)
812	-- Called when a panel has been added
813end
814
815function PANEL:OnChildRemoved(panel)
816	-- Called when a panel has been removed
817end
818
819function PANEL:Think(dt)
820	-- Called every frame
821end
822
823gui.register("Base", PANEL)