Skip to content

Commit

Permalink
rollback for selector
Browse files Browse the repository at this point in the history
the old algorithme is better
  • Loading branch information
Helfima committed Feb 28, 2019
1 parent a6ed217 commit 33cefa3
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 162 deletions.
22 changes: 13 additions & 9 deletions controller/Controller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,20 @@ function Controller.sendEvent(event, classname, action, item, item2, item3)
form.state = form.STATE_OPEN
form:onEvent(event, action, item, item2, item3)
end
if form.state == form.STATE_OPEN then
Logging:debug(Controller.classname, "*** Open", form:classname(), form.state)
form:beforeOpen(event, action, item, item2, item3)
form.state = form.STATE_UPDATE
form:open(event, action, item, item2, item3)
end
if form.state == form.STATE_UPDATE then
Logging:debug(Controller.classname, "*** update", form:classname(), form.state)
if action == "OPEN" then
if form.state == form.STATE_OPEN then
Logging:debug(Controller.classname, "*** Open", form:classname(), form.state)
form:beforeOpen(event, action, item, item2, item3)
form.state = form.STATE_UPDATE
form:open(event, action, item, item2, item3)
end
if form.state == form.STATE_UPDATE then
Logging:debug(Controller.classname, "*** update", form:classname(), form.state)
form.state = form.STATE_EVENT
form:update(event, action, item, item2, item3)
end
else
form.state = form.STATE_EVENT
form:update(event, action, item, item2, item3)
end
end
Logging:debug(Controller.classname, "form state end", form:classname(), form.state)
Expand Down
118 changes: 39 additions & 79 deletions selector/AbstractSelector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

AbstractSelector = setclass("HMAbstractSelector", Dialog)

local list_group = {}
local list_subgroup = {}
local list_prototype = {}
local filter_prototype = nil
local filter_prototype_product = true
local cell_prototype_names = nil

-------------------------------------------------------------------------------
-- Return filter - filtre sur les prototypes
--
Expand Down Expand Up @@ -198,24 +201,25 @@ function AbstractSelector.methods:onOpen(event, action, item, item2, item3)
Logging:debug(self:classname(), "onOpen():", action, item, item2, item3)
local player_gui = Player.getGlobalGui()
local close = true
if(action == "OPEN") then
filter_prototype_product = true

local globalPlayer = Player.getGlobal()
if item3 ~= nil then
filter_prototype = item3:lower():gsub("[-]"," ")
else
filter_prototype = nil
end
if event ~= nil and event.button ~= nil and event.button == defines.mouse_button_type.right then
filter_prototype_product = false
end
if player_gui.guiElementLast == nil or player_gui.guiElementLast ~= item..item2 then
filter_prototype_product = true

local globalPlayer = Player.getGlobal()
if item3 ~= nil then
filter_prototype = item3:lower():gsub("[-]"," ")
else
filter_prototype = nil
end
if event ~= nil and event.button ~= nil and event.button == defines.mouse_button_type.right then
filter_prototype_product = false
end
if item ~= nil and item2 ~= nil then
Logging:debug(self:classname(), "guiElementLast", player_gui.guiElementLast, close)
if player_gui.guiElementLast ~= item..item2 then
close = false
end
player_gui.guiElementLast = item..item2
Logging:debug(self:classname(), "guiElementLast", player_gui.guiElementLast, close)
end
Logging:debug(self:classname(), "guiElementLast", player_gui.guiElementLast, item..item2, close)
-- close si nouvel appel
return close
end
Expand Down Expand Up @@ -283,12 +287,12 @@ function AbstractSelector.methods:onEvent(event, action, item, item2, item3)
if action == "change-boolean-settings" then
if globalSettings[item] == nil then globalSettings[item] = defaultSettings[item] end
globalSettings[item] = not(globalSettings[item])
self:applyFilter(item, item2, item3)
self:onUpdate(item, item2, item3)
end

if action == "recipe-filter-switch" then
filter_prototype_product = not(filter_prototype_product)
self:applyFilter(item, item2, item3)
self:onUpdate(item, item2, item3)
end

if action == "recipe-filter" then
Expand All @@ -299,8 +303,9 @@ function AbstractSelector.methods:onEvent(event, action, item, item2, item3)
filter_prototype = event.element.parent["filter-text"].text
end
end
self:applyFilter(item, item2, item3)
self:onUpdate(item, item2, item3)
end

end

-------------------------------------------------------------------------------
Expand All @@ -319,19 +324,6 @@ function AbstractSelector.methods:updateGroups(item, item2, item3)
return {},{},{}
end

-------------------------------------------------------------------------------
-- Prepare groups
--
-- @function [parent=#AbstractSelector] prepareGroups
--
function AbstractSelector.methods:prepareGroups()
Logging:trace(self:classname(), "prepareGroups()")
self.list_group = {}
self.list_subgroup = {}
self.list_prototype = {}
self.cell_prototype_names = {}
end

-------------------------------------------------------------------------------
-- On update
--
Expand All @@ -342,19 +334,13 @@ end
-- @param #string item3 third item name
--
function AbstractSelector.methods:onUpdate(item, item2, item3)
Logging:debug(self:classname(), "onUpdate():",item, item2, item3)
Logging:trace(self:classname(), "onUpdate():",item, item2, item3)
-- recuperation recipes
if(not(self.group_ready)) then
self.group_ready = true
-- Run this methode in the next tick
self.state = self.STATE_UPDATE
self:prepareGroups()
return
end
list_group, list_subgroup, list_prototype = self:updateGroups(item, item2, item3)

self:updateFilter(item, item2, item3)
self:updateGroupSelector(item, item2, item3)
self:updateItemList(item, item2, item3)
self:applyFilter(item, item2, item3)
end

-------------------------------------------------------------------------------
Expand Down Expand Up @@ -406,40 +392,16 @@ function AbstractSelector.methods:updateFilter(item, item2, item3)
if self.product_option then
panel["filter"][self:classname().."=recipe-filter-switch=ID=filter-product"].state = filter_prototype_product
panel["filter"][self:classname().."=recipe-filter-switch=ID=filter-ingredient"].state = not(filter_prototype_product)
end
end

end

-------------------------------------------------------------------------------
-- Apply filter
--
-- @function [parent=#AbstractSelector] applyFilter
--
-- @param #string item first item name
-- @param #string item2 second item name
-- @param #string item3 third item name
--
function AbstractSelector.methods:applyFilter(item, item2, item3)
Logging:debug(self:classname(), "applyFilter():", item, item2, item3)
local item_list_panel = self:getItemListPanel()
if(item_list_panel ~= nil and item_list_panel["recipe_list"] ~= nil) then
local recipe_selector_list = item_list_panel["recipe_list"]
for _,group in pairs(recipe_selector_list.children) do
for _,cell in pairs(group.children) do
Logging:debug(self:classname(), "cell.name", cell.name)
if cell_prototype_names ~= nil then
local prototype = cell_prototype_names[cell.name]
Logging:debug(self:classname(), "prototype", prototype)
if(prototype) then
local visible = self:checkFilter(prototype)
Logging:debug(self:classname(), "visible", visible)
ElementGui.setVisible(cell, visible)
end
if filter_prototype ~= nil then
if Player.getSettings("filter_on_text_changed", true) then
panel["filter"]["cell-filter"][self:classname().."=recipe-filter=ID=filter-value"].text = filter_prototype
else
panel["filter"]["cell-filter"]["filter-text"].text = filter_prototype
end
end
end
end

end

-------------------------------------------------------------------------------
Expand Down Expand Up @@ -476,8 +438,7 @@ end
function AbstractSelector.methods:updateItemList(item, item2, item3)
Logging:trace(self:classname(), "updateItemList():", item, item2, item3)
local item_list_panel = self:getItemListPanel()
cell_prototype_names = {}


if item_list_panel["recipe_list"] ~= nil and item_list_panel["recipe_list"].valid then
item_list_panel["recipe_list"].destroy()
end
Expand All @@ -486,13 +447,12 @@ function AbstractSelector.methods:updateItemList(item, item2, item3)
local list = self:getItemList()

local recipe_selector_list = ElementGui.addGuiTable(item_list_panel, "recipe_list", 1, helmod_table_style.list)
for subgroup, list in spairs(list,function(t,a,b) return self.list_subgroup[b]["order"] > self.list_subgroup[a]["order"] end) do
for subgroup, list in spairs(list,function(t,a,b) return list_subgroup[b]["order"] > list_subgroup[a]["order"] end) do
-- boucle subgroup
local guiRecipeSubgroup = ElementGui.addGuiTable(recipe_selector_list, "recipe-table-"..subgroup, 10, "helmod_table_recipe_selector")
for key, prototype in spairs(list,function(t,a,b) return t[b]["order"] > t[a]["order"] end) do
local tooltip = self:buildPrototypeTooltip(prototype)
local cell_name,prototype_name,prototype_type = self:buildPrototypeIcon(guiRecipeSubgroup, prototype, tooltip)
cell_prototype_names[cell_name] = {name = prototype_name, type = prototype_type}
self:buildPrototypeIcon(guiRecipeSubgroup, prototype, tooltip)
end
end

Expand All @@ -509,8 +469,8 @@ function AbstractSelector.methods:getItemList()
Logging:trace(self:classname(), "getItemList()")
local global_player = Player.getGlobal()
local list_selected = {}
if self.list_prototype[global_player.recipeGroupSelected] ~= nil then
list_selected = self.list_prototype[global_player.recipeGroupSelected]
if list_prototype[global_player.recipeGroupSelected] ~= nil then
list_selected = list_prototype[global_player.recipeGroupSelected]
end
return list_selected
end
Expand Down Expand Up @@ -555,11 +515,11 @@ function AbstractSelector.methods:updateGroupSelector(item, item2, item3)
panel["recipe-groups"].destroy()
end

Logging:trace(self:classname(), "list_group:",self.list_group)
Logging:trace(self:classname(), "list_group:",list_group)

-- ajouter de la table des groupes de recipe
local gui_group_panel = ElementGui.addGuiTable(panel, "recipe-groups", 6, "helmod_table_recipe_selector")
for _, group in spairs(self.list_group,function(t,a,b) return t[b]["order"] > t[a]["order"] end) do
for _, group in spairs(list_group,function(t,a,b) return t[b]["order"] > t[a]["order"] end) do
-- set le groupe
if global_player.recipeGroupSelected == nil then global_player.recipeGroupSelected = group.name end
local color = nil
Expand Down
86 changes: 54 additions & 32 deletions selector/ContainerSelector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ function ContainerSelector.methods:checkFilter(prototype)
Logging:trace(self:classname(), "checkFilter()")
local filter_prototype = self:getFilter()
local filter_prototype_product = self:getProductFilter()
local filter_show_disable = Player.getGlobalSettings("filter_show_disable")
local filter_show_hidden = Player.getGlobalSettings("filter_show_hidden")

local find = false
if filter_prototype ~= nil and filter_prototype ~= "" then
if filter_prototype_product ~= true then
Expand All @@ -48,45 +45,71 @@ function ContainerSelector.methods:checkFilter(prototype)
else
find = true
end
return find
return find
end

-------------------------------------------------------------------------------
-- Prepare groups
-- Append groups
--
-- @function [parent=#RecipeSelector] prepareGroups
-- @function [parent=#ContainerSelector] appendGroups
--
function RecipeSelector.methods:prepareGroups()
Logging:debug(self:classname(), "prepareGroups()")
self.list_group = {}
self.list_subgroup = {}
self.list_prototype = {}
for key, entity in pairs(Player.getEntityPrototypes({"storage-tank", "container", "logistic-container", "cargo-wagon", "fluid-wagon", "item-with-entity-data", "car", "logistic-robot", "transport-belt"})) do
self:appendGroups(entity.name, "entity")
-- @param #string name
-- @param #string type
-- @param #table list_group
-- @param #table list_subgroup
-- @param #table list_prototype
--
function ContainerSelector.methods:appendGroups(name, type, list_group, list_subgroup, list_prototype)
Logging:debug(self:classname(), "appendGroups()", name, type)
EntityPrototype.load(name, type)
local find = self:checkFilter(EntityPrototype.native())
local filter_show_disable = Player.getGlobalSettings("filter_show_disable")
local filter_show_hidden = Player.getGlobalSettings("filter_show_hidden")

if find == true and (EntityPrototype.getValid() == true or filter_show_disable == true) then
local group_name = EntityPrototype.native().group.name
local subgroup_name = EntityPrototype.native().subgroup.name

if firstGroup == nil then firstGroup = group_name end
list_group[group_name] = EntityPrototype.native().group
list_subgroup[subgroup_name] = EntityPrototype.native().subgroup
if list_prototype[group_name] == nil then list_prototype[group_name] = {} end
if list_prototype[group_name][subgroup_name] == nil then list_prototype[group_name][subgroup_name] = {} end
table.insert(list_prototype[group_name][subgroup_name], {name=name, type=type, order=EntityPrototype.native().order})
end
end

-------------------------------------------------------------------------------
-- Append groups
-- Update groups
--
-- @function [parent=#RecipeSelector] appendGroups2
-- @function [parent=#ContainerSelector] updateGroups
--
-- @param #string name
-- @param #string type
-- @param #string item first item name
-- @param #string item2 second item name
-- @param #string item3 third item name
--
function RecipeSelector.methods:appendGroups(name, type)
Logging:trace(self:classname(), "appendGroups()", name, type)
EntityPrototype.load(name, type)
local lua_recipe = EntityPrototype.native()
local group_name = lua_recipe.group.name
local subgroup_name = lua_recipe.subgroup.name

if firstGroup == nil then firstGroup = group_name end
self.list_group[group_name] = lua_recipe.group
self.list_subgroup[subgroup_name] = lua_recipe.subgroup
if self.list_prototype[group_name] == nil then self.list_prototype[group_name] = {} end
if self.list_prototype[group_name][subgroup_name] == nil then self.list_prototype[group_name][subgroup_name] = {} end
table.insert(self.list_prototype[group_name][subgroup_name], {name=name, type=type, order=lua_recipe.order})
-- @return list_group, list_subgroup, list_prototype
--
function ContainerSelector.methods:updateGroups(item, item2, item3)
Logging:debug(self:classname(), "updateGroups():", item, item2, item3)
local global_player = Player.getGlobal()
local global_gui = Player.getGlobalGui()
-- recuperation recipes
local list_group = {}
local list_subgroup = {}
local list_prototype = {}

firstGroup = nil

for key, entity in pairs(Player.getEntityPrototypes({"storage-tank", "container", "logistic-container", "cargo-wagon", "fluid-wagon", "item-with-entity-data", "car", "logistic-robot", "transport-belt"})) do
self:appendGroups(entity.name, "entity", list_group, list_subgroup, list_prototype)
end

if list_prototype[global_player.recipeGroupSelected] == nil then
global_player.recipeGroupSelected = firstGroup
end
Logging:debug(self:classname(), "list_group", list_group, "list_subgroup", list_subgroup, "list_prototype", list_prototype)
return list_group, list_subgroup, list_prototype
end

-------------------------------------------------------------------------------
Expand All @@ -110,8 +133,7 @@ end
--
function ContainerSelector.methods:buildPrototypeIcon(guiElement, prototype, tooltip)
Logging:trace(self:classname(), "buildPrototypeIcon(player, guiElement, prototype, tooltip:",player, guiElement, prototype, tooltip)
local element = ElementGui.addGuiButtonSelectSprite(guiElement, self:classname().."=element-select=ID=container=", Player.getEntityIconType(prototype), prototype.name, prototype.name, tooltip)
return element.name,prototype.name,"entity"
ElementGui.addGuiButtonSelectSprite(guiElement, self:classname().."=element-select=ID=container=", Player.getEntityIconType(prototype), prototype.name, prototype.name, tooltip)
end


Expand Down
Loading

0 comments on commit 33cefa3

Please sign in to comment.