diff --git a/buy.xml b/buy.xml index 25624d1..98c3e1c 100644 --- a/buy.xml +++ b/buy.xml @@ -91,19 +91,33 @@ + + this.completor = Aux.completion.completor(this) + - Aux.completion.make_suggestions(this) + this.completor.suggest() - Aux.completion.highlight_next(this) + if IsShiftKeyDown() then + this.completor.previous() + else + this.completor.next() + end - if Aux.completion.highlighted() then - Aux.completion.close(this) + if this.completor.open() then + this.completor.close() end this:ClearFocus() Aux.buy.SearchButton_onclick() + + if this.completor.open() then + this.completor.close() + else + this:ClearFocus() + end + diff --git a/completion.lua b/completion.lua index 02cd17b..c18ec72 100644 --- a/completion.lua +++ b/completion.lua @@ -2,18 +2,10 @@ Aux.completion = {} local NUM_MATCHES = 5 -local leven, fuzzy, populate_dropdown, toggle_dropdown, suggestions, highlight +local fuzzy, populate_dropdown, toggle_dropdown, suggestions, update_highlighting local item_names = {} -local current_suggestions = {} -local selected_index = 0 - -local current_input - -local programmatically_set_input - - for item_id=1,30000 do local name = GetItemInfo(item_id) if name then @@ -78,57 +70,6 @@ function suggestions(input) return Aux.util.map(best, function(match) return match.name end) end -function Aux.completion.make_suggestions(input_box) - - local input = input_box:GetText() - if programmatically_set_input == input then - programmatically_set_input = nil - return - end - programmatically_set_input = nil - - current_input = input - selected_index = 0 - update_highlighting() - - if current_input == '' then - current_suggestions = {} - else - current_suggestions = suggestions(current_input) - UIDropDownMenu_Initialize(AuxCompletionDropDown, function() populate_dropdown(input_box, current_suggestions) end) - end - - if DropDownList1:IsVisible() then - toggle_dropdown(input_box) - end - toggle_dropdown(input_box) -end - -function Aux.completion.highlight_next(input_box) - if getn(current_suggestions) > 0 then - selected_index = selected_index > 0 and math.mod(selected_index + 1, getn(current_suggestions) + 1) or 1 - update_highlighting() - if selected_index == 0 then - Aux.completion.set_quietly(input_box, current_input) - else - Aux.completion.set_quietly(input_box, current_suggestions[selected_index]) - end - end -end - -function Aux.completion.highlighted() - return selected_index ~= 0 -end - -function Aux.completion.close() - CloseDropDownMenus(1) -end - -function Aux.completion.set_quietly(edit_box, text) - programmatically_set_input = text - edit_box:SetText(text) -end - function populate_dropdown(input_box, suggestions) for _, suggestion in ipairs(suggestions) do UIDropDownMenu_AddButton{ @@ -144,4 +85,78 @@ end function toggle_dropdown(input_box) ToggleDropDownMenu(1, nil, AuxCompletionDropDown, input_box, -12, 4) +end + +function Aux.completion.completor() + local self = {} + + local current_suggestions = {} + local selected_index = 0 + local current_input + local programmatically_set_input + + function self.close() + CloseDropDownMenus(1) + end + + function self.set_quietly(edit_box, text) + programmatically_set_input = text + edit_box:SetText(text) + end + + function self.highlighted() + return selected_index ~= 0 + end + + function self.suggest(input_box) + + local input = input_box:GetText() + if programmatically_set_input == input then + programmatically_set_input = nil + return + end + programmatically_set_input = nil + + current_input = input + selected_index = 0 + update_highlighting() + + if current_input == '' then + current_suggestions = {} + else + current_suggestions = suggestions(current_input) + UIDropDownMenu_Initialize(AuxCompletionDropDown, function() populate_dropdown(input_box, current_suggestions) end) + end + + if DropDownList1:IsVisible() then + toggle_dropdown(input_box) + end + toggle_dropdown(input_box) + end + + function self.next(input_box) + if getn(current_suggestions) > 0 then + selected_index = selected_index > 0 and math.mod(selected_index + 1, getn(current_suggestions) + 1) or 1 + update_highlighting() + if selected_index == 0 then + Aux.completion.set_quietly(input_box, current_input) + else + Aux.completion.set_quietly(input_box, current_suggestions[selected_index]) + end + end + end + + function self.previous(input_box) + if getn(current_suggestions) > 0 then + selected_index = selected_index > 0 and math.mod(selected_index + 1, getn(current_suggestions) + 1) or 1 + update_highlighting() + if selected_index == 0 then + Aux.completion.set_quietly(input_box, current_input) + else + Aux.completion.set_quietly(input_box, current_suggestions[selected_index]) + end + end + end + + return self end \ No newline at end of file