77-- * Provide a copy of or a link to the original license (GPL-3.0 or later); see LICENSE.md or <https://www.gnu.org/licenses/>.
88
99
10- local reservedKeys <const> = {
10+ local ReservedKeys <const> = {
1111 kb = Set .new (0x01 , 0x07 , 0x0A , 0x0B , 0x1B , 0x24 , 0x2C , 0x2D , 0x46 , 0x5B , 0x5C , 0x5E ),
1212 gpad = Set .new (23 , 24 , 25 , 71 , 75 )
1313}
1414
15-
15+ local NoUnbindKeys <const> = Set .new (" gui_toggle" )
16+ local DefaultConfig <const> = Serializer :GetDefaultConfig ()
1617local keyName , keyCode
17- local currentKeyName = " "
18- local _reserved = false
19- local button_size = vec2 :new (120 , 32 )
18+ local currentKeyName = " "
19+ local _reserved = false
20+ local key_container_size = vec2 :new (160 , 32 )
21+ local button_size = vec2 :new (120 , 32 )
22+
2023
2124local function GetCurrentKey ()
2225 local _ , code , name = KeyManager :IsAnyKeyPressed ()
@@ -30,16 +33,32 @@ local function GetCurrentKey()
3033 return code , name
3134end
3235
36+ local function IsDefault (current_key , default_key , isController )
37+ if (type (current_key ) ~= type (default_key )) then
38+ return false
39+ end
40+
41+ if (isController ) then
42+ return current_key .name == default_key .name
43+ end
44+
45+ return current_key == default_key
46+ end
47+
3348--- @param gvarKey string
3449--- @param isController ? boolean
35- local function DrawKeybinds (gvarKey , isController )
50+ local function DrawKeybind (gvarKey , isController )
3651 local label = gvarKey :replace (" _" , " " ):titlecase ()
3752 local main_path = isController and " gamepad_keybinds" or " keyboard_keybinds"
3853 local current_path = _F (" %s.%s" , main_path , gvarKey )
3954 local current_key = table .get_nested_key (GVars , current_path )
40- local key_container_width = 160
41- local reset_button_width = 80
55+ local default_key = table .get_nested_key (DefaultConfig , current_path )
4256 local style = ImGui .GetStyle ()
57+ local framePaddingX = style .FramePadding .x
58+ local unbind_label = _T (" SETTINGS_KEYBINDS_UNBIND" )
59+ local reset_label = _T (" GENERIC_RESET" )
60+ local unbind_button_width = ImGui .CalcTextSize (unbind_label ) + (framePaddingX * 2 )
61+ local reset_button_width = ImGui .CalcTextSize (reset_label ) + (framePaddingX * 2 )
4362
4463 ImGui .BulletText (label )
4564 local avail_x , _ = ImGui .GetContentRegionAvail ()
@@ -53,27 +72,51 @@ local function DrawKeybinds(gvarKey, isController)
5372 ImGui .SameLine ()
5473 ImGui .SetCursorPosX (
5574 avail_x
56- - key_container_width
75+ - key_container_size .x
76+ - unbind_button_width
5777 - reset_button_width
58- - style .ItemSpacing .x
78+ - ( style .ItemSpacing .x * 2 )
5979 )
6080
61- ImGui .SetNextItemWidth (key_container_width )
62- currentKeyName , _ = ImGui .InputText (_F (" ##%s" , label ), currentKeyName , 16 , ImGuiInputTextFlags .ReadOnly )
63-
64- if ImGui .IsItemClicked (0 ) then
81+ if (ImGui .Button (_F (" %s##%s" , currentKeyName , label ), key_container_size .x , key_container_size .y )) then
82+ GUI :PlaySound (GUI .Sounds .Click )
6583 ImGui .OpenPopup (label )
6684 Backend .disable_input = true
6785 end
6886
87+ local is_default = IsDefault (current_key , default_key , isController )
88+ local resetPopup = _F (" %s##%s%s" , reset_label , label , currentKeyName )
89+ ImGui .SameLine ()
90+ ImGui .BeginDisabled (is_default or default_key == nil )
91+ if (GUI :Button (reset_label )) then
92+ ImGui .OpenPopup (resetPopup )
93+ end
94+ ImGui .EndDisabled ()
95+ if (is_default ) then
96+ GUI :Tooltip (_T (" SETTINGS_KEYBINDS_NO_RESET" ))
97+ end
98+
99+ local no_unbind = NoUnbindKeys :Contains (gvarKey )
100+ local unbindPopup = _F (" %s##%s%s" , unbind_label , label , currentKeyName )
69101 ImGui .SameLine ()
70- ImGui .BeginDisabled (keyCode == 0 )
71- if ImGui .Button (_F (" %s##%s" , _T (" GENERIC_RESET" ), label ), reset_button_width , 32 ) then
72- GUI :PlaySound (" Delete" )
102+ ImGui .BeginDisabled (keyCode == 0 or no_unbind )
103+ if (GUI :Button (_F (" %s##%s" , unbind_label , label ), { size = vec2 :new (unbind_button_width , 32 ) })) then
104+ ImGui .OpenPopup (unbindPopup )
105+ end
106+ ImGui .EndDisabled ()
107+ if (no_unbind ) then
108+ GUI :Tooltip (_T (" SETTINGS_KEYBINDS_NO_UNBIND" ))
109+ end
110+
111+ if (default_key and ImGui .DialogBox (resetPopup , _T (" SETTINGS_KEYBINDS_RESET_COFNIRM" ), ImGuiDialogBoxStyle .WARN )) then
112+ local newKey = isController and table .copy (default_key ) or default_key
113+ table .set_nested_key (GVars , current_path , newKey )
114+ end
115+
116+ if (ImGui .DialogBox (unbindPopup , _T (" SETTINGS_KEYBINDS_UNBIND_COFNIRM" ), ImGuiDialogBoxStyle .WARN )) then
73117 local newKey = isController and { name = " Unbound" , code = 0 } or " Unbound"
74118 table .set_nested_key (GVars , current_path , newKey )
75119 end
76- ImGui .EndDisabled ()
77120
78121 ImGui .SetNextWindowSize (400 , 220 )
79122 if ImGui .BeginPopupModal (
@@ -89,13 +132,25 @@ local function DrawKeybinds(gvarKey, isController)
89132 if ImGui .SmallButton (" X" ) then
90133 ImGui .CloseCurrentPopup ()
91134 Backend .disable_input = false
92- keyCode , keyName = nil , nil
135+ keyCode , keyName = nil , nil
93136 end
94137
95138 ImGui .Separator ()
96139 ImGui .Dummy (1 , 10 )
97140
98- local reserved_set = isController and reservedKeys .gpad or reservedKeys .kb
141+ local confirm_label = _T (" GENERIC_CONFIRM" )
142+ local clear_label = _T (" GENERIC_CLEAR" )
143+ local confirm_label_width = ImGui .CalcTextSize (confirm_label ) + (framePaddingX * 2 )
144+ local clear_label_width = ImGui .CalcTextSize (clear_label ) + (framePaddingX * 2 )
145+ if (confirm_label_width > button_size .x ) then
146+ button_size .x = confirm_label_width
147+ end
148+
149+ if (clear_label_width > button_size .x ) then
150+ button_size .x = clear_label_width
151+ end
152+
153+ local reserved_set = isController and ReservedKeys .gpad or ReservedKeys .kb
99154 if (not keyName ) then
100155 ImGui .Text (ImGui .TextSpinner (_T (" SETTINGS_HOTKEY_WAIT" ), 10 , ImGuiSpinnerStyle .BOUNCE_DOTS ))
101156
@@ -130,7 +185,7 @@ local function DrawKeybinds(gvarKey, isController)
130185 ImGui .Dummy (0 , math.max (0 , availY - button_size .y - style .WindowPadding .y ))
131186
132187 if (keyCode and keyName and not _reserved ) then
133- if (GUI :Button (_T ( " GENERIC_CONFIRM " ) , { size = button_size })) then
188+ if (GUI :Button (confirm_label , { size = button_size })) then
134189 if (not isController ) then
135190 KeyManager :UpdateKeybind (current_key , { id = keyName })
136191 end
@@ -139,15 +194,15 @@ local function DrawKeybinds(gvarKey, isController)
139194 table .set_nested_key (GVars , current_path , newKey )
140195 ImGui .CloseCurrentPopup ()
141196 Backend .disable_input = false
142- keyCode , keyName = nil , nil
197+ keyCode , keyName = nil , nil
143198 end
144199
145200 ImGui .SameLine ()
146201 ImGui .SetCursorPosX (winSize .x - button_size .x - style .WindowPadding .x )
147202 end
148203
149204 if (keyName ) then
150- if (GUI :Button (_T ( " GENERIC_CLEAR " ) , { size = button_size })) then
205+ if (GUI :Button (clear_label , { size = button_size })) then
151206 keyCode , keyName = nil , nil
152207 end
153208 end
@@ -160,14 +215,14 @@ return function()
160215 if (ImGui .BeginTabBar (" ##ss_keybinds" )) then
161216 if (ImGui .BeginTabItem (_T (" SETTINGS_KEYBINDS_KEYBOARD" ))) then
162217 for key in pairs (GVars .keyboard_keybinds ) do
163- DrawKeybinds (key , false )
218+ DrawKeybind (key , false )
164219 end
165220 ImGui .EndTabItem ()
166221 end
167222
168223 if (ImGui .BeginTabItem (_T (" SETTINGS_KEYBINDS_CONTROLLER" ))) then
169224 for key in pairs (GVars .gamepad_keybinds ) do
170- DrawKeybinds (key , true )
225+ DrawKeybind (key , true )
171226 end
172227 ImGui .EndTabItem ()
173228 end
0 commit comments