fork/m-overlay

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

BkacjiosMore class changes27686d7

settings-overhaul
2.0 KiB93 linesraw
1local memory = require("memory")
2
3local PANEL = class.create("GCBind", "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", 0x0042)
29PANEL:ACCESSOR("Binding", "m_bBinding", false)
30
31function PANEL:GCBind()
32	self:super() -- Initialize our baseclass
33	
34	memory.hook("controller.*.buttons.pressed", self, self.OnButtonPressed)
35end
36
37function PANEL:Think()
38	self:SetEnabled(memory.isInGame())
39end
40
41function PANEL:UpdateButtonCombo(buttons)
42	if buttons == 0x0 then buttons = self.BUTTONS.D_DOWN end
43	self.m_bButtonCombo = buttons
44	self:UpdateButtonLabel()
45end
46
47function PANEL:UpdateButtonLabel()
48	local pressed = {}
49
50	for name, mask in pairs(self.BUTTONS) do
51		if bit.band(self.m_bButtonCombo, mask) == mask then
52			table.insert(pressed, name)
53		end
54	end
55
56	-- Show the current button combination
57	self:SetText(table.concat(pressed, "+"))
58end
59
60function PANEL:OnButtonPressed(port, buttons)
61	-- Stop if not in binding mode
62	if self.m_bBinding == false then return end
63
64	-- Once they let go of all buttons, end the binding process
65	if buttons == 0x0 then
66		self.m_bBinding = false
67		return
68	end
69
70	-- Only allow addition of buttons (Once the button is pressed it is locked in for this session)
71	if buttons > self.m_bButtonCombo or buttons >= PANEL.BUTTONS.UP then
72		self.m_bButtonCombo = buttons
73		self:UpdateButtonLabel()
74	end
75end
76
77function PANEL:Paint(w, h)
78	self:super("Paint", w, h)
79end
80
81function PANEL:PaintOverlay(w, h)
82	gui.skinHook("PaintOverlay", "Panel", self, w, h)
83	if self.m_bBinding then
84		graphics.setLineStyle("rough")
85		graphics.setLineWidth(3)
86	end
87end
88
89function PANEL:OnClick()
90	self:SetText("...")
91	self.m_bBinding = true
92	self.m_bButtonCombo = 0x0
93end