working on autocomplete improvements
This commit is contained in:
@@ -91,19 +91,33 @@
|
||||
</Layer>
|
||||
</Layers>
|
||||
<Scripts>
|
||||
<OnLoad>
|
||||
this.completor = Aux.completion.completor(this)
|
||||
</OnLoad>
|
||||
<OnTextChanged>
|
||||
Aux.completion.make_suggestions(this)
|
||||
this.completor.suggest()
|
||||
</OnTextChanged>
|
||||
<OnTabPressed>
|
||||
Aux.completion.highlight_next(this)
|
||||
if IsShiftKeyDown() then
|
||||
this.completor.previous()
|
||||
else
|
||||
this.completor.next()
|
||||
end
|
||||
</OnTabPressed>
|
||||
<OnEnterPressed>
|
||||
if Aux.completion.highlighted() then
|
||||
Aux.completion.close(this)
|
||||
if this.completor.open() then
|
||||
this.completor.close()
|
||||
end
|
||||
this:ClearFocus()
|
||||
Aux.buy.SearchButton_onclick()
|
||||
</OnEnterPressed>
|
||||
<OnEscapePressed>
|
||||
if this.completor.open() then
|
||||
this.completor.close()
|
||||
else
|
||||
this:ClearFocus()
|
||||
end
|
||||
</OnEscapePressed>
|
||||
</Scripts>
|
||||
</EditBox>
|
||||
|
||||
|
||||
+75
-60
@@ -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
|
||||
Reference in New Issue
Block a user