From 4a93abe8a34d2e8430a0876306353d0fc6c60e9a Mon Sep 17 00:00:00 2001 From: Hiney Date: Tue, 2 Nov 2021 04:20:50 +1100 Subject: [PATCH] Add Autowidth to dropdown control, dropped text only (#3676) * Add Autowidth to dropdown control * Reduced maximum size to 1000 * Change the width of the box too but keep it to a smaller size limit --- .gitignore | 1 + src/Classes/DropDownControl.lua | 9 +++++++-- src/Classes/TreeTab.lua | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a4fd3586..a47b0918 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .vs/ .vscode/ *.code-workspace +inspect.lua # Development files *.lnk diff --git a/src/Classes/DropDownControl.lua b/src/Classes/DropDownControl.lua index 120c59ef..4f8caea0 100644 --- a/src/Classes/DropDownControl.lua +++ b/src/Classes/DropDownControl.lua @@ -33,6 +33,8 @@ local DropDownClass = newClass("DropDownControl", "Control", "ControlHost", "Too self.list = list or { } self.selIndex = 1 self.selFunc = selFunc + -- droppedWidth will allow for the parent to set the width of the dropped component + self.droppedWidth = self.width end) -- maps the actual dropdown row index (after eventual filtering) to the original (unfiltered) list index @@ -160,6 +162,8 @@ function DropDownClass:IsMouseOver() local cursorX, cursorY = GetCursorPos() local dropExtra = self.dropped and self.dropHeight + 2 or 0 local mOver + width = m_max(width, self.droppedWidth) + if self.dropUp then mOver = cursorX >= x and cursorY >= y - dropExtra and cursorX < x + width and cursorY < y + height else @@ -226,7 +230,7 @@ function DropDownClass:Draw(viewPort) DrawImage(nil, x, y, width, height) if self.dropped then SetDrawLayer(nil, 5) - DrawImage(nil, x, dropY, width, dropExtra) + DrawImage(nil, x, dropY, self.droppedWidth, dropExtra) SetDrawLayer(nil, 0) end if not enabled then @@ -250,7 +254,7 @@ function DropDownClass:Draw(viewPort) if self.dropped then SetDrawLayer(nil, 5) SetDrawColor(0, 0, 0) - DrawImage(nil, x + 1, dropY + 1, width - 2, dropExtra - 2) + DrawImage(nil, x + 1, dropY + 1, self.droppedWidth - 2, dropExtra - 2) SetDrawLayer(nil, 0) end if self.otherDragSource then @@ -291,6 +295,7 @@ function DropDownClass:Draw(viewPort) if self.dropped then SetDrawLayer(nil, 5) self:DrawControls(viewPort) + width = self.droppedWidth -- draw tooltip for hovered item local cursorX, cursorY = GetCursorPos() diff --git a/src/Classes/TreeTab.lua b/src/Classes/TreeTab.lua index 46965f29..94d9940f 100644 --- a/src/Classes/TreeTab.lua +++ b/src/Classes/TreeTab.lua @@ -225,6 +225,21 @@ function TreeTabClass:Draw(viewPort, inputEvents) end t_insert(self.controls.specSelect.list, "Manage trees...") + local dWidth + local lineHeight = self.controls.specSelect.height - 4 + -- do not be smaller than the created width + dWidth = self.controls.specSelect.width + for j=1,#self.controls.specSelect.list do + -- +10 to stop clipping + dWidth = m_max(dWidth, DrawStringWidth(lineHeight, "VAR", self.controls.specSelect.list[j]) + 10) + end + -- no greater than a 1000 + self.controls.specSelect.droppedWidth = m_min(dWidth, 1000) + local boxWidth + -- add 20 to account for the 'down arrow' in the box + boxWidth = DrawStringWidth(lineHeight, "VAR", self.controls.specSelect.list[self.controls.specSelect.selIndex]) + 20 + self.controls.specSelect.width = m_max(m_min(boxWidth, 390), 190) + if not self.controls.treeSearch.hasFocus then self.controls.treeSearch:SetText(self.viewer.searchStr) end