fork/m-overlay

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

BkacjiosLayout the rest of all settings pads.7d53a4b

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