Skip to content

Commit 3dcaf40

Browse files
author
1onar
committed
Refactor keyboard layout generation to handle dynamic button positioning and sizing
- Simplified the logic for creating and positioning key frames based on layout data.
1 parent 5cc8b44 commit 3dcaf40

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

Frames/Controller.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ local modifier_keys = {
448448
LSHIFT = true, RSHIFT = true,
449449
}
450450

451+
-- This function updates the controller layout by creating, positioning, and resizing button frames based on the current configuration.
451452
function addon:generate_controller_key_frames()
452453
-- Clear existing keys to avoid leftover data from previous layouts
453454
for i = 1, #addon.keys_controller do

Frames/Keyboard.lua

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ function addon:generate_keyboard_key_frames()
366366
local cx, cy = addon.keyboard_frame:GetCenter()
367367
local left, right, top, bottom = cx, cx, cy, cy
368368

369+
-- Variables to track the maximum width and height
370+
local max_width = 0
371+
local max_height = 0
372+
369373
-- Loop through each key in the layout and position it within the frame.
370374
for _, layout_data in pairs(keyui_settings.layout_current_keyboard) do
371375
for i = 1, #layout_data do
@@ -402,7 +406,7 @@ function addon:generate_keyboard_key_frames()
402406

403407
-- Position the key within the frame.
404408
button:SetPoint("TOPLEFT", addon.keyboard_frame, "TOPLEFT", button_data[2], button_data[3])
405-
button.raw_key= button_data[1]
409+
button.raw_key = button_data[1]
406410
button.is_modifier = modifier_keys[button.raw_key] or false
407411
button:Show()
408412

@@ -412,20 +416,29 @@ function addon:generate_keyboard_key_frames()
412416
if r > right then right = r end
413417
if t > top then top = t end
414418
if b < bottom then bottom = b end
419+
420+
-- Update the max width and height based on button position and size
421+
if button_data[2] + button_data[4] > max_width then
422+
max_width = button_data[2] + button_data[4]
423+
end
424+
if button_data[3] - button_data[5] < max_height then
425+
max_height = button_data[3] - button_data[5]
426+
end
415427
end
416428
end
417429

418-
-- Adjust the frame size based on the extreme positions of the keys.
419-
addon.keyboard_frame:SetWidth(right - left + 12)
420-
addon.keyboard_frame:SetHeight(top - bottom + 12)
430+
-- Adjust the frame size based on the maximum width and height.
431+
-- Handle negative Y-values correctly by ensuring proper expansion of the frame
432+
addon.keyboard_frame:SetWidth(max_width + 6)
433+
addon.keyboard_frame:SetHeight(max_height - 6)
421434

422-
addon.keyboard_frame.edit_frame:SetSize( addon.keyboard_frame:GetWidth(), addon.keyboard_frame:GetHeight())
435+
addon.keyboard_frame.edit_frame:SetSize(addon.keyboard_frame:GetWidth(), addon.keyboard_frame:GetHeight())
423436
else
424437
-- Set a fallback size if the layout is empty.
425438
addon.keyboard_frame:SetWidth(940)
426439
addon.keyboard_frame:SetHeight(382)
427440

428-
addon.keyboard_frame.edit_frame:SetSize( addon.keyboard_frame:GetWidth(), addon.keyboard_frame:GetHeight())
441+
addon.keyboard_frame.edit_frame:SetSize(addon.keyboard_frame:GetWidth(), addon.keyboard_frame:GetHeight())
429442
end
430443
end
431444
end

0 commit comments

Comments
 (0)