working on autocomplete improvements
This commit is contained in:
@@ -91,19 +91,33 @@
|
|||||||
</Layer>
|
</Layer>
|
||||||
</Layers>
|
</Layers>
|
||||||
<Scripts>
|
<Scripts>
|
||||||
|
<OnLoad>
|
||||||
|
this.completor = Aux.completion.completor(this)
|
||||||
|
</OnLoad>
|
||||||
<OnTextChanged>
|
<OnTextChanged>
|
||||||
Aux.completion.make_suggestions(this)
|
this.completor.suggest()
|
||||||
</OnTextChanged>
|
</OnTextChanged>
|
||||||
<OnTabPressed>
|
<OnTabPressed>
|
||||||
Aux.completion.highlight_next(this)
|
if IsShiftKeyDown() then
|
||||||
|
this.completor.previous()
|
||||||
|
else
|
||||||
|
this.completor.next()
|
||||||
|
end
|
||||||
</OnTabPressed>
|
</OnTabPressed>
|
||||||
<OnEnterPressed>
|
<OnEnterPressed>
|
||||||
if Aux.completion.highlighted() then
|
if this.completor.open() then
|
||||||
Aux.completion.close(this)
|
this.completor.close()
|
||||||
end
|
end
|
||||||
this:ClearFocus()
|
this:ClearFocus()
|
||||||
Aux.buy.SearchButton_onclick()
|
Aux.buy.SearchButton_onclick()
|
||||||
</OnEnterPressed>
|
</OnEnterPressed>
|
||||||
|
<OnEscapePressed>
|
||||||
|
if this.completor.open() then
|
||||||
|
this.completor.close()
|
||||||
|
else
|
||||||
|
this:ClearFocus()
|
||||||
|
end
|
||||||
|
</OnEscapePressed>
|
||||||
</Scripts>
|
</Scripts>
|
||||||
</EditBox>
|
</EditBox>
|
||||||
|
|
||||||
|
|||||||
+75
-60
@@ -2,18 +2,10 @@ Aux.completion = {}
|
|||||||
|
|
||||||
local NUM_MATCHES = 5
|
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 item_names = {}
|
||||||
|
|
||||||
local current_suggestions = {}
|
|
||||||
local selected_index = 0
|
|
||||||
|
|
||||||
local current_input
|
|
||||||
|
|
||||||
local programmatically_set_input
|
|
||||||
|
|
||||||
|
|
||||||
for item_id=1,30000 do
|
for item_id=1,30000 do
|
||||||
local name = GetItemInfo(item_id)
|
local name = GetItemInfo(item_id)
|
||||||
if name then
|
if name then
|
||||||
@@ -78,57 +70,6 @@ function suggestions(input)
|
|||||||
return Aux.util.map(best, function(match) return match.name end)
|
return Aux.util.map(best, function(match) return match.name end)
|
||||||
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)
|
function populate_dropdown(input_box, suggestions)
|
||||||
for _, suggestion in ipairs(suggestions) do
|
for _, suggestion in ipairs(suggestions) do
|
||||||
UIDropDownMenu_AddButton{
|
UIDropDownMenu_AddButton{
|
||||||
@@ -145,3 +86,77 @@ end
|
|||||||
function toggle_dropdown(input_box)
|
function toggle_dropdown(input_box)
|
||||||
ToggleDropDownMenu(1, nil, AuxCompletionDropDown, input_box, -12, 4)
|
ToggleDropDownMenu(1, nil, AuxCompletionDropDown, input_box, -12, 4)
|
||||||
end
|
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