fork/m-overlay

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

blackops7799Linux portb58a563

GUI
8.3 KiB384 linesraw
1local gui = {
2	m_tRegisteredSkins = {},
3	m_pWorldPanel = nil,
4	m_pSceneManager = nil,
5	m_pFocusedPanel = nil,
6	m_strSkin = "default",
7	m_bEditorMode = false,
8	m_tOrphans = {},
9	m_tDeleted = {},
10}
11
12DOCK_NONE = 0
13DOCK_TOP = 1
14DOCK_LEFT = 2
15DOCK_BOTTOM = 4
16DOCK_RIGHT = 8
17DOCK_FILL = 16
18
19local json = require("serializer.json")
20local class = require("class")
21--local nfd = require("nfd")
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.getHoveredPanel()
36	return gui.m_pWorldPanel:GetHoveredPanel(love.mouse.getPosition())
37end
38
39function gui.newSceneLayout()
40	
41end
42
43function gui.openSceneLayout()
44	local file = nfd.open("json", "layout.json")
45
46	if not file then return end
47
48	local f, err = io.open(file, "r")
49	assert(f, err)
50	local layout = json.decode(f:read("*all"))
51	f:close()
52
53	print(table.tostring(layout))
54	gui.loadConfig(layout)
55end
56
57function gui.getDefaultConfig()
58	return {
59		width = 512,
60		height = 256,
61		layout = {
62			{
63				classname = "ControllerDisplay",
64				pos = {
65					x = 0,
66					y = 0,
67					z = 1
68				},
69				size = {
70					height = 256,
71					width = 512
72				},
73				visible = true,
74				accessors = {
75					m_iPort = 1
76				},
77				scale = {
78					x = 1,
79					y = 1
80				}
81			}
82		}
83	}
84end
85
86function gui.loadConfig(config)
87	local canvas = gui.getScenePanel():GetDisplay()
88	canvas:Clear()
89	canvas:SetSize(config.width, config.height)
90
91	for i, panel in ipairs(config.layout) do
92		local p = gui.createScenePanel(panel.classname)
93		p:SetPos(panel.pos.x, panel.pos.y)
94		p:SetZPos(panel.pos.z)
95		p:SetSize(panel.size.width, panel.size.height)
96		p:SetVisible(panel.visible)
97		p:SetScale(panel.scale.x, panel.scale.y)
98		for key, value in pairs(panel.accessors) do
99			p.__accessors[key] = value
100		end
101	end
102end
103
104function gui.saveSceneLayout()
105	local file = nfd.save("json", "layout.json")
106
107	if not file then return end
108
109	local canvas = gui.getScenePanel():GetDisplay()
110
111	local config = {
112		width = canvas:GetWidth(),
113		height = canvas:GetHeight(),
114		layout = {}
115	}
116
117	for k, child in ipairs(gui.m_pSceneManager:GetDisplay():GetChildren()) do
118		config.layout[k] = child:GetConfig()
119	end
120
121	local data = json.encode(config, true)
122
123	--assert(love.filesystem.write(file, data, #data))
124
125	local f, err = io.open(file, "w")
126	assert(f, err)
127
128	f:write(data)
129	f:close()
130end
131
132function gui.loadSceneLayout(file)
133
134end
135
136function gui.resize(w, h)
137	gui.getWorldPanel():SizeToScreen()
138	gui.getWorldPanel():InvalidateLayout()
139
140	--gui.m_pSceneManager:SizeToScreen()
141	--gui.m_pWorldPanel:InvalidateLayout()
142end
143
144function gui.registerSkin(name, tbl)
145	gui.m_tRegisteredSkins[name] = tbl
146end
147
148function gui.register(name, tbl, base)
149	class.register(name, base)(tbl)
150end
151
152function gui.create(name, parent)
153	local obj = class.new(name)
154	obj:SetParent(parent or gui.m_pWorldPanel)
155	return obj
156end
157
158function gui.createScenePanel(name)
159	return gui.m_pSceneManager:AddToScene(name)
160end
161
162function gui.setSkin(str)
163	gui.m_strSkin = str
164end
165
166function gui.getSkin()
167	return gui.m_strSkin
168end
169
170function gui.skinHook(hook, class, panel, ...)
171	local skin = gui.m_tRegisteredSkins[gui.m_strSkin]
172	if not skin then return end
173	if not skin[hook .. class] then return error(("no function '%s' in skin '%s'"):format(hook .. class, gui.m_strSkin)) end
174	skin[hook .. class](skin, panel, ...)
175end
176
177function gui.getInfoBar()
178	return gui.m_pInfoBar
179end
180
181function gui.getWorldPanel()
182	return gui.m_pWorldPanel
183end
184
185function gui.getScenePanel()
186	return gui.m_pSceneManager
187end
188
189function gui.joyPressed(joy, but)
190	gui.getHoveredPanel():OnJoyPressed(joy, but)
191end
192
193function gui.joyReleased(joy, but)
194	gui.getHoveredPanel():OnJoyReleased(joy, but)
195end
196
197function gui.isInEditorMode()
198	return gui.m_bEditorMode
199end
200
201function gui.toggleEditorMode()
202	-- Toggle editor mode
203	gui.m_bEditorMode = not gui.m_bEditorMode
204
205	local scene = gui.getScenePanel()
206
207	-- Get the size of the display canvas
208	local w, h = scene:GetDisplay():GetSize()
209
210	-- Get the original window size and flags
211	local ow, oh, flags = love.window.getMode()
212
213	scene:SetEditorMode(gui.m_bEditorMode)
214
215	local uw, uh = scene:GetUnusableSpace()
216
217	-- Resize min/max bounds and toggle resizing
218	flags.resizable = gui.m_bEditorMode
219
220	if gui.m_bEditorMode then
221		-- Readjust the window size to fit the taskbars and stuff
222		w, h = w + uw, h + uh
223		
224		-- Minimum size we will allow
225		flags.minwidth = 512 + uw
226		flags.minheight = 256 + uh
227	else
228		flags.minwidth = w
229		flags.minheight = h
230	end
231
232	love.window.setMode(w, h, flags)
233
234	local world = gui.getWorldPanel()
235	world:SizeToScreen()
236	world:InvalidateLayout()
237	world:ValidateLayout()
238end
239
240function gui.keyPressed(key, scancode, isrepeat)
241	if key == "escape" and not isrepeat then
242		gui.toggleEditorMode()
243	end
244	gui.m_pFocusedPanel:OnKeyPressed(key, isrepeat)
245end
246
247function gui.keyReleased(key)
248	gui.m_pFocusedPanel:OnKeyReleased(key)
249end
250
251function gui.textInput(text)
252	gui.m_pFocusedPanel:OnTextInput(text)
253end
254
255function gui.render()
256	if gui.m_pWorldPanel then
257		gui.m_pWorldPanel:Render()
258	end
259end
260
261function gui.mouseMoved(x, y, dx, dy, istouch)
262	local panel = gui.getFocusedPanel()
263	local lx, ly = panel:WorldToLocal(x,y)
264	if not panel:OnMouseMoved(lx, ly, dx, dy, istouch) then
265		local parent = panel:GetParent()
266		while parent do
267			lx, ly = parent:WorldToLocal(x, y)
268			if parent:OnMouseMoved(lx, ly, dx, dy, istouch) then break end
269			parent = parent:GetParent()
270		end
271	end
272end
273
274function gui.mousePressed(x, y, button, istouch, presses)
275	local panel = gui.getHoveredPanel()
276	gui.setFocusedPanel(panel)
277
278	local lx, ly = panel:WorldToLocal(x, y)
279	if not panel:OnMousePressed(lx, ly, button, istouch, presses) then
280		local parent = panel:GetParent()
281		while parent do
282			lx, ly = parent:WorldToLocal(x, y)
283			if parent:OnMousePressed(lx, ly, button, istouch, presses) then break end
284			parent = parent:GetParent()
285		end
286	end
287end
288
289function gui.mouseReleased(x, y, button, istouch, presses)
290	local panel = gui.getFocusedPanel()
291	local lx, ly = panel:WorldToLocal(x,y)
292	if not panel:OnMouseReleased(lx, ly, button, istouch, presses) then
293		local parent = panel:GetParent()
294		while parent do
295			lx, ly = parent:WorldToLocal(x, y)
296			if parent:OnMouseReleased(lx, ly, button, istouch, presses) then break end
297			parent = parent:GetParent()
298		end
299	end
300end
301
302function gui.mouseWheeled(x, y)
303	local panel = gui.getHoveredPanel()
304	if not panel:OnMouseWheeled(x, y) then
305		local parent = panel:GetParent()
306		while parent do
307			if parent:OnMouseWheeled(x, y) then break end
308			parent = parent:GetParent()
309		end
310	end
311end
312
313function gui.tick(dt)
314	gui.m_pWorldPanel:ValidateLayout()
315	gui.m_pWorldPanel:CallAll("Think", dt)
316	gui.m_pWorldPanel:CleanupOrphans()
317	gui.m_pWorldPanel:CleanupDeleted()
318end
319
320function gui.shutdown()
321	-- TODO: Save layout of SceneDisplay GUI
322end
323
324function gui.init()
325	gui.loadSkins("modules/gui/skins")
326	gui.loadClasses("modules/gui/panels/core")
327	gui.loadClasses("modules/gui/panels/editor")
328	gui.loadClasses("modules/gui/panels/smash")
329	class.init() -- Initialize all classes, sets inheritance
330
331	gui.m_pWorldPanel = gui.create("Panel")
332	gui.m_pWorldPanel:DockMargin(0, 0, 0, 0)
333	gui.m_pWorldPanel:DockPadding(0, 0, 0, 0)
334	gui.m_pWorldPanel:SetBGColor(color_blank)
335	gui.m_pWorldPanel:SetBorderColor(color_blank)
336	gui.m_pWorldPanel:SizeToScreen()
337
338	gui.m_pFocusedPanel = gui.m_pWorldPanel
339
340	gui.m_pSceneManager = gui.m_pWorldPanel:Add("SceneEditor")
341	gui.m_pSceneManager:Dock(DOCK_FILL)
342end
343
344do
345	local lfs = love.filesystem
346
347	local function loadFilesWithGUIEnv(folder)
348		for i, path in ipairs(lfs.getDirectoryItems(folder)) do
349			local file = string.format("%s/%s", folder, path)
350
351			if lfs.getInfo(file, "file") then
352				local chunk, err = lfs.load(file)
353				if err then
354					return error(err)
355				end
356
357				-- Create a copy of our Lua environment, and merge it with some helper functions
358				local env = table.merge(table.copy(_G), require("gui.env"))
359
360				-- Update the _G variable
361				env._G = env
362
363				-- Reset these to a fresh state, since the script will probably use one of them.
364				env.PANEL = {}
365				env.SKIN = {}
366
367				-- Set the environment to our copy of the global environment
368				setfenv(chunk, env)
369				chunk() -- Execute the file
370			end
371		end
372	end
373
374	function gui.loadSkins(folder)
375		loadFilesWithGUIEnv(folder)
376	end
377
378	function gui.loadClasses(folder)
379		require("gui.panels.base")
380		loadFilesWithGUIEnv(folder)
381	end
382end
383
384return gui