From 33fca1a819d83db01ff2f8b7e129979adec86cc9 Mon Sep 17 00:00:00 2001 From: aquietone Date: Sat, 10 Dec 2022 12:26:50 -0800 Subject: [PATCH] limit context menu size based on num items --- CHANGELOG | 4 ++++ condbuilder.lua | 62 +++++++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 28 deletions(-) create mode 100644 CHANGELOG diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..20b8be0 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,4 @@ +0.2 +- Limit context menu size based on number of items +0.1 +- Initial release diff --git a/condbuilder.lua b/condbuilder.lua index 16c55f7..4a3f54b 100644 --- a/condbuilder.lua +++ b/condbuilder.lua @@ -1,4 +1,4 @@ --- Macro Condition Builder v0.1 - aquietone +-- Macro Condition Builder v0.2 - aquietone local mq = require('mq') local isOpen = true @@ -35,35 +35,41 @@ local function drawReferenceLink() end local function drawContextMenu() - if ImGui.BeginPopupContextItem() then - if expression:len() >= 2 and expression:sub(-2) == '${' then - -- The string ends with ${ so we can offer up some TLOs as hints - for _,tlo in ipairs(TLOOptions) do - if ImGui.Selectable(tlo) then - expression = expression .. tlo - end + local menuitems = {} + if expression:len() >= 2 and expression:sub(-2) == '${' then + -- The string ends with ${ so we can offer up some TLOs as hints + for _,tlo in ipairs(TLOOptions) do + table.insert(menuitems, tlo) + end + elseif expression:len() > 1 and expression:sub(-1) == '.' then + -- determine TLO name before the . to lookup members + local tlo = expression:match('.*[{.](.*)%.') + if mq.TLO[tlo] then + -- The string before the trailing . is a valid TLO, so we can offer up + -- the TLOs members as hints + local tlotype = mq.gettype(mq.TLO[tlo]) + for i=0,300 do + local tlomember = mq.TLO.Type(tlotype).Member(i)() + table.insert(menuitems, tlomember) end - elseif expression:len() > 1 and expression:sub(-1) == '.' then - -- determine TLO name before the . to lookup members - local tlo = expression:match('.*[{.](.*)%.') - if mq.TLO[tlo] then - -- The string before the trailing . is a valid TLO, so we can offer up - -- the TLOs members as hints - local tlotype = mq.gettype(mq.TLO[tlo]) - for i=0,300 do - local tlomember = mq.TLO.Type(tlotype).Member(i)() - if tlomember and ImGui.Selectable(tlomember) then - expression = expression .. tlomember - end + end + end + if #menuitems > 20 then + ImGui.SetNextWindowSize(-1, ImGui.GetTextLineHeight()*20) + end + if ImGui.BeginPopupContextItem() then + if #menuitems == 0 then + -- Not a TLO. It could be a member or a parameter like + -- Me.CleanName. or Spawn[id 123]. + -- Or it could just be something incomplete / invalid + ImGui.Text('No Suggestions') + else + for _,item in ipairs(menuitems) do + if ImGui.Selectable(item) then + expression = expression .. item end - else - -- Not a TLO. It could be a member or a parameter like - -- Me.CleanName. or Spawn[id 123]. - -- Or it could just be something incomplete / invalid - ImGui.Text('No Suggestions') end - else - ImGui.Text('No Suggestions') + end ImGui.EndPopup() end @@ -120,4 +126,4 @@ mq.imgui.init('condbuilder', expressionBuilder) while isOpen do mq.delay(1000) -end \ No newline at end of file +end