fork/m-overlay

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

BkacjiosBugfixes + Dark Mode skina8034e9

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