fork/m-overlay

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

BkacjiosAdded a bind button for toggling muting music. Added some icons to buttons.a3968ab

v2.1.1b
2.4 KiB107 linesraw
1local memory = require("memory")
2
3local PANEL = class.create("GCBinder", "Button")
4
5PANEL.BUTTONS = {
6	Z = 0x0010,
7	R = 0x0020,
8	L = 0x0040,
9	A = 0x0100,
10	B = 0x0200,
11	X = 0x0400,
12	Y = 0x0800,
13	D_LEFT = 0x0001,
14	D_RIGHT = 0x0002,
15	D_DOWN = 0x0004,
16	D_UP = 0x0008,
17	START = 0x1000,
18	UP = 0x10000,
19	DOWN = 0x20000,
20	LEFT = 0x40000,
21	RIGHT = 0x80000,
22	C_UP = 0x100000,
23	C_DOWN = 0x200000,
24	C_LEFT = 0x400000,
25	C_RIGHT = 0x800000,
26}
27
28PANEL:ACCESSOR("ButtonCombo", "m_bButtonCombo", 0x0)
29PANEL:ACCESSOR("BindingCombo", "m_bBindingCombo", 0x0)
30PANEL:ACCESSOR("Binding", "m_bBinding", false)
31
32function PANEL:GCBinder()
33	self:super() -- Initialize our baseclass
34	memory.hook("controller.*.buttons.pressed", self, self.OnButtonPressed)
35	self:UpdateButtonLabel()
36end
37
38function PANEL:Think()
39	self:SetEnabled(memory.isInGame())
40end
41
42function PANEL:SetButtonCombo(buttons)
43	if buttons then
44		self.m_bButtonCombo = buttons
45		self:UpdateButtonLabel()
46	end
47end
48
49function PANEL:UpdateButtonLabel()
50	local pressed = {}
51
52	for name, mask in pairs(self.BUTTONS) do
53		if bit.band(self.m_bBinding and self.m_bBindingCombo or self.m_bButtonCombo, mask) == mask then
54			table.insert(pressed, name)
55		end
56	end
57
58	-- Show the current button combination
59	self:SetText(table.concat(pressed, "+"))
60end
61
62function PANEL:OnButtonPressed(port, buttons)
63	-- Stop if not in binding mode
64	if self.m_bBinding == false then return end
65
66	-- Once they let go of all buttons, end the binding process
67	if buttons == 0x0 then
68		self.m_bBinding = false
69		self.m_bButtonCombo = self.m_bBindingCombo
70		self:UpdateButtonLabel()
71		return
72	end
73
74	-- Only allow addition of buttons (Once the button is pressed it is locked in for this session)
75	if buttons > self.m_bBindingCombo or buttons >= PANEL.BUTTONS.UP then
76		self.m_bBindingCombo = buttons
77		self:UpdateButtonLabel()
78	end
79end
80
81function PANEL:OnKeyPressed(key, isrepeat)
82	if self.m_bBinding and key == "backspace" or key == "escape" then
83		self.m_bBinding = false
84		self:UpdateButtonLabel()
85		return true
86	end
87end
88
89function PANEL:Paint(w, h)
90	self:super("Paint", w, h)
91end
92
93function PANEL:PaintOverlay(w, h)
94	gui.skinHook("PaintOverlay", "Panel", self, w, h)
95	if self.m_bBinding then
96		graphics.setColor(color_blue)
97		graphics.setLineStyle("rough")
98		graphics.setLineWidth(3)
99		graphics.innerRectangle(0, 0, w, h)
100	end
101end
102
103function PANEL:OnClick()
104	self:SetText("...")
105	self.m_bBinding = true
106	self.m_bBindingCombo = 0x0
107end