Skip to content

Commit a880925

Browse files
committed
fix: some bugs
- Refactor keybinds UI and block unbinding the main GUI toggle key. - Fix UI issues and bugs related to the previous `Translator` refactor. Closes YimMenu-Lua#123 Closes YimMenu-Lua#124
1 parent b8d8a17 commit a880925

26 files changed

Lines changed: 195 additions & 66 deletions

SSV2/includes/frontend/helpers/measure_text_width.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99

1010
---@param labels array<string>
11-
---@param padding float
11+
---@param padding? float
1212
---@return float
1313
return function(labels, padding)
1414
local max = 0
15-
padding = padding or 0
15+
padding = padding or 0
1616

1717
for i = 1, #labels do
1818
local w = ImGui.CalcTextSize(labels[i])

SSV2/includes/frontend/settings/keybinds_ui.lua

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
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()
1617
local 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

2124
local function GetCurrentKey()
2225
local _, code, name = KeyManager:IsAnyKeyPressed()
@@ -30,16 +33,32 @@ local function GetCurrentKey()
3033
return code, name
3134
end
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

SSV2/includes/frontend/yrv3/clubhouse.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ return function()
2828
)
2929
ImGui.Spacing()
3030

31-
local iso = GVars.backend.language_code
32-
local bulletWidth = bulletWidths[iso]
31+
local index = GVars.backend.language_index
32+
local bulletWidth = bulletWidths[index]
3333
if (not bulletWidth) then
3434
bulletWidth = measureBulletWidths({
3535
_T("YRV3_CASH_SAFE"),
3636
_T("YRV3_MC_CLIENT_BIKE_LABEL"),
3737
}, 60.0)
3838

39-
bulletWidths[iso] = bulletWidth
39+
bulletWidths[index] = bulletWidth
4040
end
4141

4242
local cashSafe = clubhouse:GetCashSafe()

SSV2/includes/frontend/yrv3/factory.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ return function(bb, notOwnedLabel)
2929
return
3030
end
3131

32-
local iso = GVars.backend.language_code
33-
local bulletWidth = bulletWidths[iso]
32+
local index = GVars.backend.language_index
33+
local bulletWidth = bulletWidths[index]
3434
if (not bulletWidth) then
3535
bulletWidth = measureBulletWidths({
3636
_T("YRV3_EQUIP_UPGDRADE"),
@@ -40,7 +40,7 @@ return function(bb, notOwnedLabel)
4040
_T("YRV3_VALUE_TOTAL"),
4141
}, 60.0)
4242

43-
bulletWidths[iso] = bulletWidth
43+
bulletWidths[index] = bulletWidth
4444
end
4545

4646
local name = bb:GetName()

SSV2/includes/frontend/yrv3/money_fronts.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ return function()
2121
end
2222

2323
local clearHeatLabel = _F("%s %s", _T("GENERIC_CLEAR"), _T("YRV3_CWASH_HEAT"))
24-
local iso = GVars.backend.language_code
25-
local bulletWidth = bulletWidths[iso]
24+
local index = GVars.backend.language_index
25+
local bulletWidth = bulletWidths[index]
2626
if (not bulletWidth) then
2727
bulletWidth = measureBulletWidths({
2828
_T("YRV3_CWASH_WORK_EARNINGS"),
@@ -31,7 +31,7 @@ return function()
3131
clearHeatLabel
3232
}, 60.0)
3333

34-
bulletWidths[iso] = bulletWidth
34+
bulletWidths[index] = bulletWidth
3535
end
3636

3737
drawBasicBusiness(carWash, true, bulletWidth, clearHeatLabel)

SSV2/includes/frontend/yrv3/nightclub.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ return function()
6060
bools.coloredNameplate, _ = GUI:CustomToggle("Synthwave & Pain", bools.coloredNameplate)
6161
ImGui.Spacing()
6262

63-
local iso = GVars.backend.language_code
64-
local bulletWidth = bulletWidths[iso]
63+
local index = GVars.backend.language_index
64+
local bulletWidth = bulletWidths[index]
6565
if (not bulletWidth) then
6666
bulletWidth = measureBulletWidths({
6767
_T("YRV3_POPULARITY"),
6868
_T("YRV3_CASH_SAFE"),
6969
}, 60.0)
7070

71-
bulletWidths[iso] = bulletWidth
71+
bulletWidths[index] = bulletWidth
7272
end
7373

7474
local popValue = club:GetPopularity()

SSV2/includes/frontend/yrv3/office.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ return function()
6666
showEarningsData = GUI:CustomToggle(_T("YRV3_SHOW_EARNINGS_DATA"), showEarningsData)
6767

6868
if (showEarningsData) then
69-
local iso = GVars.backend.language_code
70-
local bulletWidth = bulletWidths[iso]
69+
local index = GVars.backend.language_index
70+
local bulletWidth = bulletWidths[index]
7171
if (not bulletWidth) then
7272
local labels = {}
7373
for _, data in ipairs(earningData) do
7474
labels[#labels + 1] = _T(data.label)
7575
end
7676
bulletWidth = measureBulletWidths(labels, 60.0)
7777

78-
bulletWidths[iso] = bulletWidth
78+
bulletWidths[index] = bulletWidth
7979
end
8080

8181
ImGui.Separator()

SSV2/includes/frontend/yrv3/salvage_yard.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ return function()
3030

3131
ImGui.Spacing()
3232

33-
local iso = GVars.backend.language_code
34-
local bulletWidth = bulletWidths[iso]
33+
local index = GVars.backend.language_index
34+
local bulletWidth = bulletWidths[index]
3535
if (not bulletWidth) then
3636
bulletWidth = measureBulletWidths({
3737
_T("YRV3_CASH_SAFE"),
3838
_T("SY_INCOME_THRESHOLD"),
3939
}, 60.0)
4040

41-
bulletWidths[iso] = bulletWidth
41+
bulletWidths[index] = bulletWidth
4242
end
4343

4444
local childWidth = 240

SSV2/includes/frontend/yrv3/vehicle_warehouse.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ return function(warehouse)
2424
return
2525
end
2626

27-
local iso = GVars.backend.language_code
28-
local bulletWidth = bulletWidths[iso]
27+
local index = GVars.backend.language_index
28+
local bulletWidth = bulletWidths[index]
2929
if (not bulletWidth) then
3030
bulletWidth = measureBulletWidths({
3131
_T("YRV3_IE_VEHS_AMT"),
@@ -36,7 +36,7 @@ return function(warehouse)
3636
_T("YRV3_IE_STEAL_TOP_CHANCE"),
3737
}, 60.0)
3838

39-
bulletWidths[iso] = bulletWidth
39+
bulletWidths[index] = bulletWidth
4040
end
4141

4242
local name = warehouse:GetName()

SSV2/includes/frontend/yrv3/warehouse.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ return function(warehouse, notOwnedLabel)
2424
return
2525
end
2626

27-
local iso = GVars.backend.language_code
28-
local bulletWidth = bulletWidths[iso]
27+
local index = GVars.backend.language_index
28+
local bulletWidth = bulletWidths[index]
2929
if (not bulletWidth) then
3030
bulletWidth = measureBulletWidths({
3131
_T("YRV3_CARGO_AMT"),
3232
_T("YRV3_VALUE_TOTAL"),
3333
}, 60.0)
3434

35-
bulletWidths[iso] = bulletWidth
35+
bulletWidths[index] = bulletWidth
3636
end
3737

3838
local name = warehouse:GetName()

0 commit comments

Comments
 (0)