fork/m-overlay

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

BkacjiosBugfixes + Dark Mode skina8034e9

v2.1.5
23.3 KiB1015 linesraw
1local PANEL = class.create("BasePanel")
2
3require("extensions.table")
4
5local log = require("log")
6
7PANEL:ACCESSOR("Interacted", "m_bInteracted", false)
8PANEL:ACCESSOR("Hovered", "m_bHovered", false)
9PANEL:ACCESSOR("Visible", "m_bVisible", true)
10PANEL:ACCESSOR("Focusable", "m_bFocusable", true)
11PANEL:ACCESSOR("Validated", "m_bValidated", false)
12PANEL:ACCESSOR("AppliedSkin", "m_bAppliedSkin", false)
13PANEL:ACCESSOR("Scissor", "m_bScissor", true)
14
15function PANEL:BasePanel()
16	--self.m_tChildren = setmetatable({}, {__mode = "v"})
17	self.m_tChildren = {}
18	self.m_iWorldPosX = 0
19	self.m_iWorldPosY = 0
20	self.m_fScaleX = 1
21	self.m_fScaleY = 1
22	self.m_iPosX = 0
23	self.m_iPosY = 0
24	self.m_iWidth = 42
25	self.m_iHeight = 24
26	self.m_pParent = nil
27	self.m_iZPos = 0
28	self.m_bOrphaned = false
29	self.m_bDeleted = false
30	self.m_iDock = DOCK_NONE
31	self.m_iCenter = CENTER_NONE
32	self.m_tDockMargins = { left = 2, top = 2, right = 2, bottom = 2 }
33	self.m_tDockPadding = { left = 2, top = 2, right = 2, bottom = 2 }
34end
35
36function PANEL:InheritMethods(panel)
37	local pclass = panel:getClass()
38	for name, value in pairs(pclass) do
39		if type(value) == "function" and not self[name] then
40			self[name] = function(this, ...)
41				return value(panel, ...)
42			end
43		end
44	end
45end
46
47function PANEL:GetClassName()
48	return self:getClass():getName()
49end
50
51function PANEL:__tostring()
52	return ("Panel[%q]"):format(self:GetClassName())
53end
54
55function PANEL:GetConfig()
56	local w, h = self:GetPixelSize()
57	local sx, sy = self:GetScale()
58
59	local config = {
60		classname = self:GetClassName(),
61		pos = { x = self:GetX(), y = self:GetY(), z = self:GetZPos() },
62		size = { width = w, height = h },
63		scale = { x = sx, y = sy },
64		visible = self:IsVisible(),
65		accessors = self.__accessors,
66	}
67
68	if self.m_iDock ~= 0 then
69		config.dock = {
70			mode = self.m_iDock,
71			margins = self.m_tDockMargins,
72			padding = self.m_tDockPadding,
73		}
74	end
75
76	return config
77end
78
79function PANEL:BringToFront()
80	local parent = self.m_pParent
81	if not parent then return end -- There's nothing for it to go in front of!
82	
83	local highest = nil
84
85	for _,child in ipairs(parent.m_tChildren) do
86		if not highest or child:GetZPos() > highest:GetZPos() then
87			highest = child
88		end
89	end
90
91	-- No panels or the highest is already ourself
92	if not highest or highest == self then return end
93
94	-- Get the position of the highest panel
95	local replace = highest:GetZPos()
96
97	-- Set the highest panel to our current position
98	highest:SetZPos(self:GetZPos())
99
100	-- Set our position to where the highest panel used to be
101	self:SetZPos(replace)
102end
103
104function PANEL:Dock(i)
105	self.m_iDock = i
106	self:InvalidateLayout()
107end
108
109function PANEL:GetDock()
110	return self.m_iDock
111end
112
113function PANEL:DockMargin(left, top, right, bottom)
114	self.m_tDockMargins.left = left
115	self.m_tDockMargins.top = top
116	self.m_tDockMargins.right = right
117	self.m_tDockMargins.bottom = bottom
118end
119
120function PANEL:GetDockMargin()
121	return self.m_tDockMargins
122end
123
124function PANEL:DockPadding(left, top, right, bottom)
125	self.m_tDockPadding.left = left
126	self.m_tDockPadding.top = top
127	self.m_tDockPadding.right = right
128	self.m_tDockPadding.bottom = bottom
129end
130
131function PANEL:GetDockPadding()
132	return self.m_tDockPadding
133end
134
135function PANEL:GetWidthPlusMargin()
136	if not self:IsVisible() then return 0 end
137	local margins = self:GetDockMargin()
138	return margins.left + margins.right + self:GetWidth()
139end
140
141function PANEL:GetHeightPlusMargin()
142	if not self:IsVisible() then return 0 end
143	local margins = self:GetDockMargin()
144	return margins.top + margins.bottom + self:GetHeight()
145end
146
147function PANEL:GetSizePlusMargin()
148	local w, h = self:GetWidthPlusMargin(), self:GetHeightPlusMargin()
149
150	--[[for _,child in ipairs(self.m_tChildren) do
151		if child:IsVisible() then
152			margins = child:GetDockMargin()
153			padding = child:GetDockPadding()
154			w = w + margins.left + margins.right + padding.left + padding.right
155			h = h + margins.top + margins.bottom + padding.top + padding.bottom
156		end
157	end]]
158
159	return w, h
160end
161
162--[[function PANEL:GetSpaceAround(panel)
163	local margins = panel:GetDockMargin()
164	local padding = panel:GetDockPadding()
165
166	local w = margins.left + margins.right + padding.left + padding.right
167	local h = margins.top + margins.bottom + padding.top + padding.bottom
168
169	for _,child in ipairs(self.m_tChildren) do
170		if child ~= panel and child:IsVisible() then
171			local margins = child:GetDockMargin()
172			local padding = child:GetDockPadding()
173			w = w + margins.left + margins.right + padding.left + padding.right + child:GetWidth()
174			h = h + margins.top + margins.bottom + padding.top + padding.bottom + child:GetHeight()
175		end
176	end
177
178	local width, height = self:GetSize()
179
180	return w, h
181end]]
182
183function PANEL:SetZPos(i)
184	self.m_iZPos = i
185	self:GetParent():ReorderChildren()
186end
187
188function PANEL:ReorderChildren()
189	table.sort(self.m_tChildren, function(a, b) return a.m_iZPos < b.m_iZPos end)
190end
191
192function PANEL:GetZPos()
193	return self.m_iZPos
194end
195
196function PANEL:CallAll(func, ...)
197	for _,child in ipairs(self.m_tChildren) do
198		child[func](child, ...)
199		child:CallAll(func, ...)
200	end
201end
202
203function PANEL:CallAllVisible(func, ...)
204	if not self:IsVisible() then return end
205	self[func](self, ...)
206	for _,child in ipairs(self.m_tChildren) do
207		child:CallAllVisible(func, ...)
208	end
209end
210
211function PANEL:CallSelfAndParents(func, ...)
212	if not self:IsVisible() then return end
213	if self[func](self, ...) then return end
214	local parent = self.m_pParent
215	if not parent then return end
216	parent:CallSelfAndParents(func, ...)
217end
218
219function PANEL:Remove()
220	local parent = self:GetParent()
221	if parent then
222		parent:OnChildRemoved(self)
223	end
224	self.m_bDeleted = true
225	self:OnRemoved()
226	self:Clear()
227end
228
229function PANEL:Clear()
230	for zpos, child in ipairs(self.m_tChildren) do
231		child:Remove()
232	end
233end
234
235function PANEL:InvalidateLayout()
236	self.m_bValidated = false
237	self:InvalidateChildren()
238	self:InvalidateParents()
239end
240
241function PANEL:InvalidateChildren()
242	for k, child in ipairs(self.m_tChildren) do
243		child.m_bValidated = false
244		child:InvalidateChildren()
245	end
246end
247
248function PANEL:InvalidateParents()
249	if not self.m_pParent then return end
250	self.m_pParent.m_bValidated = false
251	self.m_pParent:InvalidateParents()
252end
253
254function PANEL:QueryTooltip()
255	local tooltip = self:OnQueryTooltip()
256	if tooltip then return tooltip end
257	if self.m_pParent then
258		return self.m_pParent:QueryTooltip()
259	end
260end
261
262function PANEL:MarkAsOrphan()
263	self.m_bOrphaned = true
264end
265
266function PANEL:CleanupOrphans()
267	for key, child in reversedipairs(self.m_tChildren) do
268		child:CleanupOrphans()
269		if child.m_bOrphaned then
270			log.warn("[GUI] %s is now an orphan and will continue to remain in memory", child)
271			table.remove(self.m_tChildren, key)
272		end
273	end
274end
275
276function PANEL:CleanupDeleted()
277	local didDelete = false
278	for key, child in reversedipairs(self.m_tChildren) do
279		child:CleanupDeleted()
280		if child.m_bDeleted then
281			didDelete = true
282
283			table.remove(self.m_tChildren, key)
284			child.m_pParent = nil
285			child = nil
286		end
287	end
288	if didDelete then
289		self:InvalidateLayout()
290	end
291end
292
293function PANEL:SetParent(parent)
294	self.m_pParent = parent
295	if parent then
296		table.insert(parent.m_tChildren, self)
297		self:SetZPos(#parent.m_tChildren)
298		parent:OnChildAdded(self)
299	else
300		-- No parent? Mark as an orphan for abandonment
301		self:MarkAsOrphan()
302	end
303end
304
305function PANEL:HasParent()
306	return self:GetParent() ~= nil
307end
308
309function PANEL:GetParent()
310	return self.m_pParent
311end
312
313function PANEL:GetParents()
314	local parents = {}
315	
316	local parent = self:GetParent()
317	
318	while parent do
319		table.insert(parents, parent)
320		parent = parent:GetParent()
321	end
322	return parents
323end
324
325function PANEL:GetChildren()
326	return self.m_tChildren
327end
328
329function PANEL:GetFamily(family)
330	local family = family or {}
331	
332	local children = self.m_tChildren
333	family[self] = children
334	
335	for _,child in ipairs(children) do
336		child:GetFamily(family[self])
337	end
338	
339	return family
340end
341
342function PANEL:SizeToScreen()
343	self:SetSize(graphics.getPixelDimensions())
344end
345
346function PANEL:SizeToParent()
347	local parent = self:GetParent()
348
349	if parent then
350		self:SetSize(parent:GetSize())
351	end
352end
353
354function PANEL:SizeToChildren(doWidth, doHeight)
355	local all = doWidth == nil and doHeight == nil
356	local padding = self:GetDockPadding()
357
358	local lw, lh = 0, 0
359
360	for _,child in ipairs(self.m_tChildren) do
361		if child:IsVisible() then
362			local x, y = child:GetPos()
363			local margin = child:GetDockMargin()
364			local cw, ch = child:GetPixelSize()
365
366			-- Position + size + margins = maximum bounds
367			cw = margin.left + x + cw
368			ch = margin.top + y + ch
369
370			-- Update the largest widths and heights
371			if cw > lw then lw = cw end
372			if ch > lh then lh = ch end
373		end
374	end
375	if all or doWidth then self.m_iWidth = lw + padding.right end
376	if all or doHeight then self.m_iHeight = lh + padding.bottom end
377end
378
379function PANEL:Add(class)
380	return gui.create(class, self)
381end
382
383function PANEL:SetScale(x, y)
384	y = y or x
385	self.m_fScaleX, self.m_fScaleY = x, y
386end
387
388function PANEL:GetScale()
389	return self.m_fScaleX, self.m_fScaleY
390end
391
392function PANEL:SetPos(x, y)
393	self.m_iPosX, self.m_iPosY = math.floor(x + 0.5), math.floor(y + 0.5)
394end
395
396function PANEL:GetPos()
397	return self.m_iPosX, self.m_iPosY
398end
399
400function PANEL:SetWorldPos(x, y)
401	self.m_iWorldPosX, self.m_iWorldPosY = x, y
402end
403
404function PANEL:GetWorldPos()
405	return self.m_iWorldPosX, self.m_iWorldPosY
406end
407
408function PANEL:LocalToWorld(x, y)
409	local sx, sy = self:GetPos()
410	x = x + sx
411	y = y + sy
412	
413	local parent = self:GetParent()
414	
415	while parent do
416		local px, py = parent:GetPos()
417		x = x + px
418		y = y + py
419		parent = parent:GetParent()
420	end
421	
422	return x, y
423end
424
425function PANEL:WorldToLocal(x, y)
426	local sx, sy = self:LocalToWorld(0, 0)
427	return x - sx, y - sy
428end
429
430function PANEL:SetX(x)
431	self.m_iPosX = math.floor(x + 0.5)
432end
433
434function PANEL:GetX()
435	return self.m_iPosX
436end
437
438function PANEL:SetY(y)
439	self.m_iPosY = math.floor(y + 0.5)
440end
441
442function PANEL:GetY()
443	return self.m_iPosY
444end
445
446function PANEL:SetSize(w, h)
447	self:SetWidth(w)
448	self:SetHeight(h)
449	self:OnResize(w, h)
450end
451
452function PANEL:GetSize()
453	return self:GetWidth(), self:GetHeight()
454end
455
456function PANEL:SetVisible(b)
457	if self.m_bVisible ~= b then
458		self.m_bVisible = b
459		self:InvalidateLayout()
460	end
461end
462
463function PANEL:GetPixelSize()
464	return math.max(0, self.m_iWidth), math.max(0, self.m_iHeight)
465end
466
467function PANEL:SetWidth(w)
468	w = math.max(0, math.floor(w + 0.5))
469	if self.m_iWidth ~= w then
470		self.m_iWidth = w
471		self:InvalidateLayout()
472	end
473end
474
475function PANEL:GetActualWidth()
476	return math.max(0, self.m_iWidth)
477end
478
479function PANEL:GetWidth()
480	return self:GetActualWidth() * self.m_fScaleX
481end
482
483function PANEL:SetHeight(h)
484	h = math.max(0, math.floor(h + 0.5))
485	if self.m_iHeight ~= h then
486		self.m_iHeight = h
487		self:InvalidateLayout()
488	end
489end
490
491function PANEL:GetActualHeight()
492	return math.max(0, self.m_iHeight)
493end
494
495function PANEL:GetHeight()
496	return self:GetActualHeight() * self.m_fScaleY
497end
498
499function PANEL:IsWorldPointInside(x, y)
500	local px, py = self:LocalToWorld(0, 0)
501	return x > px and x < px + self:GetWidth() and y > py and y < py + self:GetHeight()
502end
503
504function PANEL:Render()
505	if not self:IsVisible() then return end
506
507	local x, y = self:LocalToWorld(0, 0)
508	local w, h = self:GetSize()
509	
510	local parent = self:GetParent()
511
512	-- Start the scissor position and size with our own values
513	local sx, sy = x, y
514	local sw, sh = w, h
515
516	while self.m_bScissor and parent do
517		-- If we have a parent, fit the scissor to fit inside their bounds
518		local px, py = parent:LocalToWorld(0, 0)
519		local pw, ph = parent:GetSize()
520
521		if sx < px then
522			sw = math.max(0, sw + sx - px)
523			sx = px
524		end
525		if sx + sw > px + pw then
526			sw = math.max(0, sw - ((sx + sw) - (px + pw)))
527			sx = math.min(sx, px + pw)
528		end
529		if sy < py then
530			sh = math.max(0, sh + sy - py)
531			sy = py
532		end
533		if sy + sh > py + ph then
534			sh = math.max(0, sh - ((sy + sh) - (py + ph)))
535			sy = math.min(sy, py + ph)
536		end
537
538		parent = parent:GetParent()
539	end
540
541	self:SetWorldPos(sx, sy)
542
543	-- Only bother to render if the panel is visible within the scissor
544	if math.max(x, sx) < math.min(x + w, sx + sw) and math.max(y, sy) < math.min(y + h, sy + sh) then
545		local rsx, rsy = self:GetScale()
546
547		graphics.push("all") -- Push the current graphics state
548			graphics.setScissor(sx, sy, sw, sh)
549			graphics.translate(x, y) -- Translate so Paint has localized position values for drawing objects
550			graphics.scale(rsx, rsy)
551
552			local uw, uh = self:GetPixelSize()
553			graphics.setColor(255, 255, 255, 255)
554			self:PrePaint(uw, uh)
555			graphics.setColor(255, 255, 255, 255)
556			self:Paint(uw, uh)
557			graphics.setColor(255, 255, 255, 255)
558			self:PostPaint(uw, uh)
559
560			graphics.origin()
561
562			-- recently added panels are drawn last, thus, ontop of older panels
563			for _, child in ipairs(self.m_tChildren) do
564				child:Render()
565			end
566
567			graphics.setScissor(sx, sy, sw, sh)
568			graphics.translate(x, y) -- Translate so Paint has localized position values for drawing objects
569			graphics.scale(rsx, rsy)
570			graphics.setColor(255, 255, 255, 255)
571			self:PaintOverlay(uw, uh)
572
573			graphics.origin()
574		graphics.pop() -- Reset the graphics state to what it was
575
576		if self.m_bDebug then
577			-- Debug the scissor rect
578			graphics.setColor(255, 0, 0, 25)
579			graphics.rectangle("fill", sx, sy, sw, sh)
580		end
581		
582		graphics.setScissor()
583	end
584end
585
586function PANEL:DisableScissor()
587	self.m_bScissor = false
588end
589
590function PANEL:ApplySkin()
591	if not self.m_bAppliedSkin then
592		self.m_bAppliedSkin = true
593		self:Skin()
594	end
595end
596
597function PANEL:ValidateLayout()
598	-- Layout ourself first..
599	self:Layout()
600	for k, child in ipairs(self.m_tChildren) do
601		-- Recursive call
602		-- layout all children and thier parents AGAIN if needed.
603		-- This allows any adjustments we made during a layout to probagate to up & down the family tree.
604		child:ValidateLayout()
605		-- Layout current child parents
606		child:LayoutParents()
607	end
608end
609
610function PANEL:Layout()
611	-- Check if we have an invalidated layout..
612	if not self.m_bValidated then
613		self.m_bValidated = true
614		self:CenterLayout()
615		self:DockLayout()
616		self:PerformLayout()
617	end
618end
619
620function PANEL:LayoutParents()
621	if not self.m_pParent then return end
622	self.m_pParent:Layout()
623	self.m_pParent:LayoutParents()
624end
625
626function PANEL:CenterLayout()
627	local parent = self:GetParent()
628
629	local w, h = self:GetSize()
630	local pw, ph = graphics.getPixelDimensions() -- Default to window size if no parent
631	if parent then
632		pw, ph = parent:GetSize()
633	end
634
635	local vertical = bit.band(self.m_iCenter, CENTER_VERTICAL) == CENTER_VERTICAL
636	local horizontal = bit.band(self.m_iCenter, CENTER_HORIZONTAL) == CENTER_HORIZONTAL
637
638	if vertical == true then
639		self:SetY((ph / 2) - (h / 2))
640	end
641	if horizontal == true then
642		self:SetX((pw / 2) - (w / 2))
643	end
644end
645
646function PANEL:UnCenter()
647	local want = CENTER_NONE
648	if self.m_iCenter ~= want then
649		self.m_iCenter = want
650		self:InvalidateLayout()
651	end
652end
653
654function PANEL:Center()
655	self:CenterVertical()
656	self:CenterHorizontal()
657end
658
659function PANEL:CenterVertical()
660	local want = bit.bor(self.m_iCenter, CENTER_VERTICAL)
661	if self.m_iCenter ~= want then
662		self.m_iCenter = want
663		self:InvalidateLayout()
664	end
665end
666
667function PANEL:CenterHorizontal()
668	local want = bit.bor(self.m_iCenter, CENTER_HORIZONTAL)
669	if self.m_iCenter ~= want then
670		self.m_iCenter = want
671		self:InvalidateLayout()
672	end
673end
674
675function PANEL:DockLayout()
676	local x, y = 0, 0
677	local w, h = self:GetPixelSize()
678	
679	local padding = self.m_tDockPadding
680	
681	for _,child in ipairs(self.m_tChildren) do
682		local margin = child.m_tDockMargins
683	
684		local dx = x + padding.left
685		local dy = y + padding.top
686		local dw = w - (padding.left + padding.right)
687		local dh = h - (padding.top + padding.bottom)
688	
689		local dock = child.m_iDock
690		if dock ~= DOCK_NONE then
691			local cw, ch = child:GetPixelSize()
692			if(dock == DOCK_TOP) then
693				child:SetPos(dx + margin.left, dy + margin.top)
694				child.m_iWidth = dw - margin.left - margin.right
695				child.m_iHeight = ch
696				if child:IsVisible() then
697					local height = margin.top + margin.bottom + ch
698					y = y + height
699					h = h - height
700				end
701			elseif(dock == DOCK_LEFT) then
702				child:SetPos(dx + margin.left, dy + margin.top)
703				child.m_iWidth = cw
704				child.m_iHeight = dh - margin.top - margin.bottom
705				if child:IsVisible() then
706					local width = margin.left + margin.right + cw
707					x = x + width
708					w = w - width
709				end
710			elseif(dock == DOCK_RIGHT) then
711				child:SetPos((dx + dw) - cw - margin.right, dy + margin.top)
712				child.m_iWidth = cw
713				child.m_iHeight = dh - margin.top - margin.bottom
714				if child:IsVisible() then
715					local width = margin.left + margin.right + cw
716					w = w - width
717				end
718			elseif(dock == DOCK_BOTTOM) then
719				child:SetPos(dx + margin.left, (dy + dh) - ch - margin.bottom)
720				child.m_iWidth = dw - margin.left - margin.right
721				child.m_iHeight = ch
722				if child:IsVisible() then
723					h = h - (ch + margin.bottom + margin.top)
724				end
725			end
726		end
727	end
728	
729	for _,child in ipairs(self.m_tChildren) do
730		local dock = child.m_iDock
731		
732		if dock ~= DOCK_NONE then
733			local margin = child.m_tDockMargins
734		
735			local dx = x + padding.left
736			local dy = y + padding.top
737			local dw = w - (padding.left + padding.right)
738			local dh = h - (padding.top + padding.bottom)
739			
740			if(dock == DOCK_FILL) then
741				child:SetPos(dx + margin.left, dy + margin.top)
742				child.m_iWidth = dw - margin.left - margin.right
743				child.m_iHeight = dh - margin.top - margin.bottom
744			end
745		end
746	end
747end
748
749function PANEL:GiveFocus()
750	gui.setFocusedPanel(self)
751end
752
753function PANEL:MakePopup()
754	self:BringToFront()
755	self:GiveFocus()
756end
757
758function PANEL:HasFocus(checkchildren)
759	if checkchildren then
760		for _,child in ipairs(self.m_tChildren) do
761			if child:HasFocus(checkchildren) then
762				return true
763			end
764		end
765	end
766	return gui.getFocusedPanel() == self
767end
768
769function PANEL:IsHovered()
770	return gui.getHoveredPanel() == self
771end
772
773function PANEL:GetHoveredChild(x, y, ignore)
774	local panel = nil
775	for _,child in reversedipairs(self.m_tChildren) do
776		if child:IsVisible() and child:IsFocusable() and child:IsWorldPointInside(x, y) and ((ignore and child ~= ignore) or not ignore) then
777			panel = child:GetHoveredPanel(x, y)
778			break
779		end
780	end
781	return panel
782end
783
784function PANEL:GetHoveredPanel(x, y, ignore)
785	local panel = self
786	for _,child in reversedipairs(self.m_tChildren) do
787		if child:IsVisible() and child:IsFocusable() and child:IsWorldPointInside(x, y) and ((ignore and child ~= ignore) or not ignore) then
788			panel = child:GetHoveredPanel(x, y)
789			break
790		end
791	end
792	return panel
793end
794
795-- Tries to get find a position that can fit "other" somehwere around ourself without clipping outside our world
796function PANEL:SetPosAround(other, world)
797	world = world or gui.getWorldPanel()
798
799	local pw, ph = self:GetPixelSize()
800
801	local ox, oy = other:GetWorldPos()
802	local ow, oh = other:GetPixelSize()
803
804	local sx, sy = world:GetWorldPos()
805	local sw, sh = world:GetPixelSize()
806
807	local canFitAbove = (oy - sy) >= ph
808	local canFitBelow = ((sy + sh) - (oy + oh)) >= ph
809	local canFitLeft = (ox - sx) >= pw
810	local canFitRight = ((sx + sw) - (ox + ow)) >= pw
811
812	local finalX, finalY = 0, 0
813	local midX, midY = 0,0
814	
815	if canFitAbove or canFitBelow then -- Prefer above or below
816		midX = ox + ow/2
817
818		-- set our x position centered against our other
819		finalX = midX - pw/2
820
821		-- fit our x position within our world
822		finalX = math.min(sx + sw - pw, math.max(sx, finalX))
823		if canFitAbove then
824			-- set y above
825			finalY = oy - ph
826			midY = oy
827		else
828			-- set y below
829			finalY = oy + oh
830			midY = finalY
831		end
832	elseif canFitLeft or canFitRight then -- Try left or right if above and below fails
833		midY = oy + oh/2
834
835		-- set our y position centered against our other
836		finalY = midY - ph/2
837
838		-- fit our y position within our world
839		finalY = math.min(sy + sh - ph, math.max(sy, finalY))
840		if canFitLeft then
841			-- set y left
842			finalX = ox - pw
843			midX = ox
844		else
845			-- set y right
846			finalX = ox + ow
847			midX = finalX
848		end
849	end
850
851	-- return our position as a local position within the given world
852	finalX, finalY = world:WorldToLocal(finalX, finalY)
853	self:SetPos(finalX, finalY)
854	return self:WorldToLocal(midX, midY)
855end
856
857-- PANEL OVERRIDE DEFAULTS
858
859function PANEL:PerformLayout()
860	-- Called when we are Validating the layout.
861	-- Good for manually positioning child panels.
862end
863
864function PANEL:PrePaint(w, h)
865end
866
867function PANEL:Paint(w, h)
868	-- Called every frame, when we are drawing the panel to the screen.
869	-- Used to draw custom things within the panel.
870end
871
872function PANEL:PostPaint(w, h)
873end
874
875function PANEL:PaintOverlay(w, h)
876end
877
878function PANEL:OnResize(w, h)
879	-- Called when the size of the panel has changed.
880	-- Good for manually positioning child panels, like in PerformLayout.
881end
882
883function PANEL:OnFocusChanged(b)
884	-- Called when the panels focus has either been gained or lost
885end
886
887function PANEL:OnHoveredChanged(b)
888	-- Called when the panels hovered state has either been gained or lost
889end
890
891function PANEL:OnKeyPressed(key, isrepeat)
892	-- Called when the panel is focused, and a keyboard key has been pressed
893end
894
895function PANEL:OnKeyReleased(key)
896	-- Called when the panel is focused, and a keyboard key has been released
897end
898
899function PANEL:OnTextInput(text)
900	-- Called when the panel is focused.
901	-- Good for a text input panel.
902end
903
904function PANEL:OnJoyPressed(joy, but)
905	-- Override
906end
907
908function PANEL:OnJoyReleased(joy, but)
909	-- Override
910end
911
912function PANEL:OnMouseMoved(x, y, dx, dy, istouch)
913	--[[
914	Called when when the mouse has been moved.
915
916	number x
917		The mouse position on the x-axis.
918	number y
919		The mouse position on the y-axis.
920	number dx
921		The amount moved along the x-axis since the last time love.mousemoved was called.
922	number dy
923		The amount moved along the y-axis since the last time love.mousemoved was called.
924
925	Returning true will stop the event from going up the family tree
926	Child->Parent->Parent->Parent->etc, etc..
927	]]
928	
929	return false
930end
931
932function PANEL:OnMousePressed(x, y, button, istouch, presses)
933	--[[
934	Called when a mouse press event has been made.
935
936	number x
937		The mouse position on the x-axis.
938	number y
939		The mouse position on the y-axis.
940	number dx
941		The amount moved along the x-axis since the last time love.mousemoved was called.
942	number dy
943		The amount moved along the y-axis since the last time love.mousemoved was called.
944
945	Returning true will stop the event from going up the family tree
946	Child->Parent->Parent->Parent->etc, etc..
947	]]
948	
949	return false
950end
951
952function PANEL:OnMouseReleased(x, y, button, istouch, presses)
953	--[[
954	Called when a mouse release event has been made.
955
956	number x
957		The mouse position on the x-axis.
958	number y
959		The mouse position on the y-axis.
960	number dx
961		The amount moved along the x-axis since the last time love.mousemoved was called.
962	number dy
963		The amount moved along the y-axis since the last time love.mousemoved was called.
964
965	Returning true will stop the event from going up the family tree
966	Child->Parent->Parent->Parent->etc, etc..
967	]]
968	
969	return false
970end
971
972function PANEL:OnMouseWheeled(x, y)
973	--[[
974	Called when the scrollwheel has been turned.
975
976	number x
977		Amount of horizontal mouse wheel movement. Positive values indicate movement to the right.
978	number y
979		Amount of vertical mouse wheel movement. Positive values indicate upward movement.
980
981	See an example in..
982	gui/panels/scrollpanel.lua 
983	&
984	gui/panels/scrollbar.lua 
985
986	Returning true will stop the event from going up the family tree
987	Child->Parent->Parent->Parent->etc, etc..
988    ]]
989
990	return false
991end
992
993function PANEL:OnChildAdded(panel)
994	-- Called when a panel has been added
995end
996
997function PANEL:OnRemoved()
998	
999end
1000
1001function PANEL:OnChildRemoved(panel)
1002	-- Called when a panel has been removed
1003end
1004
1005function PANEL:Think(dt)
1006	-- Called every frame
1007end
1008
1009function PANEL:OnQueryTooltip()
1010	-- Called when the user has hovered over the panel for more than a second
1011end
1012
1013function PANEL:Skin()
1014	
1015end