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
8.4 KiB343 linesraw
1local PANEL = {}
2
3local watcher = require("memory.watcher")
4local perspective = require("perspective")
5local state = require("smash.states")
6
7require("extensions.math")
8
9local BUTTONS = {
10	NONE		= 0x0000,
11	DPAD_LEFT	= 0x0001,
12	DPAD_RIGHT	= 0x0002,
13	DPAD_DOWN	= 0x0004,
14	DPAD_UP		= 0x0008,
15	Z			= 0x0010,
16	R			= 0x0020,
17	L			= 0x0040,
18	A			= 0x0100,
19	B			= 0x0200,
20	X			= 0x0400,
21	Y			= 0x0800,
22	START		= 0x1000,
23}
24
25local timer = love.timer
26local graphics = love.graphics
27local newImage = graphics.newImage
28local newFont = graphics.newFont
29local format = string.format
30
31local DISCONNECTED = newImage("textures/buttons/disconnected.png")
32
33local BUTTON_TEXTURES = {
34	DPAD = {
35		BACKGROUND = newImage("textures/buttons/d-pad-gate.png"),
36		POSITION = {
37			x = 100,
38			y = 128,
39		},
40	},
41
42	DPAD_LEFT = {
43		PRESSED = newImage("textures/buttons/d-pad-pressed-left.png"),
44		POSITION = {
45			x = 108,
46			y = 144,
47		},
48	},
49
50	DPAD_RIGHT = {
51		PRESSED = newImage("textures/buttons/d-pad-pressed-right.png"),
52		POSITION = {
53			x = 108,
54			y = 144,
55		},
56	},
57
58	DPAD_UP = {
59		PRESSED = newImage("textures/buttons/d-pad-pressed-up.png"),
60		POSITION = {
61			x = 108,
62			y = 144,
63		},
64	},
65
66	DPAD_DOWN = {
67		PRESSED = newImage("textures/buttons/d-pad-pressed-down.png"),
68		POSITION = {
69			x = 108,
70			y = 144,
71		},
72	},
73
74	JOYSTICK = {
75		BACKGROUND = newImage("textures/buttons/joystick-gate.png"),
76		MASK = newImage("textures/buttons/joystick-mask.png"),
77		STICK = newImage("textures/buttons/joystick-outline.png"),
78	},
79	CSTICK = {
80		BACKGROUND = newImage("textures/buttons/c-stick-gate.png"),
81		STICK = newImage("textures/buttons/c-stick.png"),
82	},
83	A = {
84		OUTLINE = newImage("textures/buttons/a-outline.png"),
85		PRESSED = newImage("textures/buttons/a-pressed.png"),
86		COLOR = color(0, 225, 150, 255),
87		POSITION = {
88			x = 12 + 64 + 256,
89			y = 64
90		}
91	},
92	B = {
93		OUTLINE = newImage("textures/buttons/b-outline.png"),
94		PRESSED = newImage("textures/buttons/b-pressed.png"),
95		COLOR = color(230, 0, 0, 255),
96		POSITION = {
97			x = 16 + 256,
98			y = 108
99		}
100	},
101	X = {
102		OUTLINE = newImage("textures/buttons/x-outline.png"),
103		PRESSED = newImage("textures/buttons/x-pressed.png"),
104		COLOR = color(255, 255, 255, 255),
105		POSITION = {
106			x = 138 + 256,
107			y = 48
108		}
109	},
110	Y = {
111		OUTLINE = newImage("textures/buttons/y-outline.png"),
112		PRESSED = newImage("textures/buttons/y-pressed.png"),
113		COLOR = color(255, 255, 255, 255),
114		POSITION = {
115			x = 60 + 256,
116			y = 0
117		}
118	},
119	Z = {
120		OUTLINE = newImage("textures/buttons/z-outline.png"),
121		PRESSED = newImage("textures/buttons/z-pressed.png"),
122		COLOR = color(165, 75, 165, 255),
123		POSITION = {
124			x = 128 + 256,
125			y = -16
126		}
127	},
128	START = {
129		OUTLINE = newImage("textures/buttons/start-outline.png"),
130		PRESSED = newImage("textures/buttons/start-pressed.png"),
131		COLOR = color(255, 255, 255, 255),
132		POSITION = {
133			x = 256,
134			y = 42
135		}
136	}
137}
138
139local MAX_PORTS = 4
140
141function PANEL:Initialize()
142	self:super()
143
144	self:SetSize(512, 256)
145	
146	self:DockPadding(0,0,0,0)
147	self:DockMargin(0,0,0,0)
148
149	self:MakeAccessor("Port", "m_iPort", -1)
150
151	self:SetScale(0.5)
152end
153
154local vertices = {
155	{
156		0, 0,
157		0, 0,
158		1, 1, 1,
159	},
160	{
161		128, 0,
162		1, 0,
163		1, 1, 1
164	},
165	{
166		128, 128,
167		1, 1,
168		1, 1, 1
169	},
170	{
171		0, 128,
172		0, 1,
173		1, 1, 1
174	},
175}
176
177local rotated_vertices = {}
178
179local function transformVertices(vertices, x, y, angle, ox, oy)
180	if #vertices ~= #rotated_vertices then
181		rotated_vertices = {}
182	end
183
184	local c = math.cos(angle)
185	local s = math.sin(angle)
186
187	for i=1, #vertices do
188		-- Create or use vertex cache
189		rotated_vertices[i] = rotated_vertices[i] or {}
190
191		-- Copy and rotate X and Y vertex points
192		rotated_vertices[i][1] = x + (vertices[i][1] - ox) * c - (vertices[i][2] - oy) * s
193		rotated_vertices[i][2] = y + (vertices[i][1] - ox) * s + (vertices[i][2] - oy) * c
194
195		-- Copy other vertex settings
196		rotated_vertices[i][3] = vertices[i][3]
197		rotated_vertices[i][4] = vertices[i][4]
198		rotated_vertices[i][5] = vertices[i][5]
199		rotated_vertices[i][6] = vertices[i][6]
200	end
201
202	return rotated_vertices
203end
204
205function PANEL:Paint(w, h)
206	local controller = watcher.controller[self.m_iPort]
207
208	if controller then
209
210		-- Draw connection status
211
212		if controller.plugged == 0xFF then
213			local t = timer.getTime()
214			graphics.setColor(hsl(360, 1, 0.5, math.sinlerp(0.25, 1, t*4)))
215			graphics.easyDraw(DISCONNECTED, w - 8 - 42, h - 8 - 42, 0, 32, 32)
216		end
217
218		-- Draw Joystick
219
220		local x, y = controller.joystick.x, 1 - controller.joystick.y
221
222		local angle = math.atan2(controller.joystick.x, controller.joystick.y)
223		local mag = math.sqrt(controller.joystick.x ^ 2 + controller.joystick.y ^ 2)
224
225		local far = mag * 15
226		local near = mag * 20
227
228		-- Make the rectangle look like its fading into the horizon
229		vertices[1][1] = far		-- x
230		vertices[1][2] = near		-- y
231		vertices[2][1] = 128 - far	-- x
232		vertices[2][2] = near		-- y
233
234		local rotated = transformVertices(vertices, 64 + 22 + (40 * x), 64 + 12 + (40 * y), angle, 64, 64)
235
236		local wx, wy = self:GetWorldPos()
237		local sx, sy = self:GetScale()
238
239		graphics.stencil(function()
240			perspective.on(wx, wy, sx, sy)
241			perspective.quad(BUTTON_TEXTURES.JOYSTICK.MASK, rotated[1], rotated[2], rotated[3], rotated[4])
242			perspective.off()
243		end, "replace", 1)
244		graphics.setStencilTest("equal", 0) -- Mask out the gate behind the joystick
245			graphics.easyDraw(BUTTON_TEXTURES.JOYSTICK.BACKGROUND, 22, 52, 0, 128, 128)
246		graphics.setStencilTest()
247
248		perspective.on(wx, wy, sx, sy)
249		perspective.quad(BUTTON_TEXTURES.JOYSTICK.STICK, rotated[1], rotated[2], rotated[3], rotated[4])
250		perspective.off()
251
252		-- Draw C-Stick
253
254		local x, y = controller.cstick.x, 1 - controller.cstick.y
255
256		local angle = math.atan2(controller.cstick.x, controller.cstick.y)
257		local mag = math.sqrt(controller.cstick.x ^ 2 + controller.cstick.y ^ 2)
258
259		local far = mag * 12
260		local near = mag * 16
261
262		-- Make the rectangle look like its fading into the horizon
263		vertices[1][1] = far		-- x
264		vertices[1][2] = near		-- y
265		vertices[2][1] = 128 - far	-- x
266		vertices[2][2] = near		-- y
267
268		local rotated = transformVertices(vertices, 64 + 48 + 128 + (32 * x), 64 + 20 + (32 * y), angle, 64, 64)
269
270		graphics.setColor(255, 235, 0, 255)
271		graphics.easyDraw(BUTTON_TEXTURES.CSTICK.BACKGROUND, 48 + 128, 52, 0, 128, 128)
272
273		perspective.on(wx, wy, sx, sy)
274		perspective.quad(BUTTON_TEXTURES.CSTICK.STICK, rotated[1], rotated[2], rotated[3], rotated[4])
275		perspective.off()
276
277		graphics.setColor(255, 255, 255, 255)
278
279		-- Draw L
280
281		graphics.setLineStyle("smooth")
282		graphics.setLineWidth(3)
283
284		graphics.stencil(function()
285			-- Create a rounded rectangle mask
286			graphics.rectangle("fill", 24 + 14, 16, 100, 12, 6, 6)
287		end, "replace", 1)
288		graphics.setStencilTest("greater", 0) -- Only draw within our rounded rectangle mask
289			-- L Analog
290			graphics.rectangle("fill", 24 + 14, 16, 88 * controller.analog.float.l, 12)
291
292	 		-- L Button
293			if bit.band(controller.buttons.pressed, BUTTONS.L) == BUTTONS.L then
294				graphics.rectangle("fill", 24 + 14 + 88, 16, 12, 12)
295			end
296		graphics.setStencilTest()
297
298		-- Draw outline
299		graphics.rectangle("line", 24 + 14, 16, 100, 12, 6, 6)
300		-- Draw segment for button press
301		graphics.line(24 + 14 + 88, 16, 24 + 14 + 88, 16 + 12)
302
303		-- Draw R
304
305		graphics.stencil(function()
306			-- Create a rounded rectangle mask
307			graphics.rectangle("fill", 48 + 128 + 14, 16, 100, 12, 6, 6)
308		end, "replace", 1)
309		graphics.setStencilTest("greater", 0) -- Only draw within our rounded rectangle mask
310			-- R Analog
311			graphics.rectangle("fill", 48 + 128 + 14 + 12 + (88 * (1 - controller.analog.float.r)), 16, 88 * controller.analog.float.r, 12)
312
313			-- R Button
314			if bit.band(controller.buttons.pressed, BUTTONS.R) == BUTTONS.R then
315				graphics.rectangle("fill", 48 + 128 + 14, 16, 12, 12)
316			end
317		graphics.setStencilTest()
318
319		-- Draw outline
320		graphics.rectangle("line", 48 + 128 + 14, 16, 100, 12, 6, 6)
321		-- Draw segment for button press
322		graphics.line(48 + 128 + 14 + 12, 16, 48 + 128 + 14 + 12, 16 + 12)
323
324		-- Draw buttons
325
326		graphics.easyDraw(BUTTON_TEXTURES.DPAD.BACKGROUND, 108, 144, 0, 128, 128)
327
328		for button, flag in pairs(BUTTONS) do
329			local texture = BUTTON_TEXTURES[button]
330			if texture then
331				local pos = texture.POSITION
332				graphics.setColor(texture.COLOR)
333				if texture.PRESSED and bit.band(controller.buttons.pressed, flag) == flag then -- Check if the button is pressed
334					graphics.easyDraw(texture.PRESSED, pos.x, pos.y, 0, 128, 128)
335				elseif texture.OUTLINE then
336					graphics.easyDraw(texture.OUTLINE, pos.x, pos.y, 0, 128, 128)
337				end
338			end
339		end
340	end
341end
342
343gui.register("ControllerDisplay", PANEL, "Base")