1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/modules/fjei/gui.lua

627 lines
23 KiB
Lua
Raw Normal View History

2019-11-11 18:31:30 +02:00
local Functions = require "modules.fjei.functions"
2019-11-11 17:53:00 +02:00
local math_ceil = math.ceil
2019-11-12 22:19:42 +02:00
local string_find = string.find
2019-11-13 00:42:27 +02:00
local table_remove = table.remove
local table_insert = table.insert
2019-11-16 09:56:46 +02:00
local main_window_width = 278
2019-11-11 23:58:20 +02:00
local recipe_window_width = 480
2020-03-03 09:16:19 +02:00
local recipe_window_amount_width = 38
local recipe_window_item_name_width = 128
local recipe_window_position = "center"
2019-11-13 01:40:03 +02:00
local column_count = 6
2020-03-03 09:16:19 +02:00
local line_count = 5
2019-11-11 15:55:09 +02:00
local items_per_page = column_count * line_count
local Public = {}
2019-11-11 17:53:00 +02:00
local function get_total_page_count(player)
if not global.fjei.player_data[player.index].filtered_list then Functions.set_filtered_list(player) end
2019-11-11 18:31:30 +02:00
local count = math_ceil(global.fjei.player_data[player.index].size_of_filtered_list / items_per_page)
if count < 1 then count = 1 end
return count
2019-11-11 17:53:00 +02:00
end
local function set_page_count_caption(player)
if not player.gui.left.fjei_main_window then return end
if not global.fjei.player_data[player.index].filtered_list then Functions.set_filtered_list(player) end
2019-11-11 17:53:00 +02:00
local active_page = global.fjei.player_data[player.index].active_page
local element = player.gui.left.fjei_main_window.fjei_main_window_control_table.fjei_main_window_page_counter
2019-11-13 01:40:03 +02:00
element.caption = active_page .. "/" .. get_total_page_count(player)
2019-11-11 17:53:00 +02:00
end
2020-03-03 09:16:19 +02:00
local function get_formatted_amount(amount)
if amount < 1000 then
amount = amount .. " x "
return amount
end
if amount >= 1000000 then
amount = math.floor(amount * 0.00001) * 0.1 .. "m "
return amount
end
if amount >= 1000 then
amount = math.floor(amount * 0.01) * 0.1 .. "k "
return amount
end
end
2019-11-11 23:58:20 +02:00
local function get_localised_name(name)
2019-11-12 01:05:06 +02:00
local item = game.item_prototypes[name]
2019-11-11 23:58:20 +02:00
if item then return item.localised_name end
local fluid = game.fluid_prototypes[name]
if fluid then return fluid.localised_name end
2019-11-12 01:05:06 +02:00
local recipe = game.recipe_prototypes[name]
if recipe then return recipe.localised_name end
2019-11-11 23:58:20 +02:00
return name
end
local function get_sprite_type(name)
if game.item_prototypes[name] then return "item" end
if game.fluid_prototypes[name] then return "fluid" end
2019-11-12 01:05:06 +02:00
if game.entity_prototypes[name] then return "entity" end
if game.recipe_prototypes[name] then return "recipe" end
2019-11-11 23:58:20 +02:00
return false
end
2019-12-14 11:07:18 +02:00
local function get_active_recipe_name(player)
2020-03-03 09:16:19 +02:00
local fjei_recipe_window = player.gui[recipe_window_position]["fjei_recipe_window"]
2019-12-14 11:07:18 +02:00
if not fjei_recipe_window then return end
if not fjei_recipe_window.recipe_container then return end
local container = fjei_recipe_window.recipe_container.children[1]
if not container then return end
return container.children[1].name
end
2020-03-03 09:16:19 +02:00
local function add_choose_elem_button(element, name, is_recipe)
local elem_type
if is_recipe then
elem_type = "recipe"
else
elem_type = get_sprite_type(name)
end
if not elem_type then return end
local elem_type = get_sprite_type(name)
local choose_elem_button = element.add({type = "choose-elem-button", name = name, elem_type = elem_type})
choose_elem_button.locked = true
choose_elem_button.elem_value = name
choose_elem_button.style.minimal_width = 36
choose_elem_button.style.minimal_height = 36
choose_elem_button.style.maximal_width = 36
choose_elem_button.style.maximal_height = 36
choose_elem_button.style.margin = -1
choose_elem_button.style.right_margin = 2
choose_elem_button.style.padding = 2
end
2019-11-13 17:43:09 +02:00
local function add_sprite_icon(element, name, is_recipe, use_localised_name)
2019-11-12 01:05:06 +02:00
local sprite_type = false
if is_recipe then
sprite_type = "recipe"
else
sprite_type = get_sprite_type(name)
end
2019-11-11 23:58:20 +02:00
if not sprite_type then return end
2019-11-13 17:43:09 +02:00
local sprite
if use_localised_name then
sprite = element.add({type = "sprite", name = name, sprite = sprite_type .. "/" .. name, tooltip = get_localised_name(name)})
else
sprite = element.add({type = "sprite", name = name, sprite = sprite_type .. "/" .. name, tooltip = name})
end
2019-11-11 23:58:20 +02:00
sprite.style.minimal_width = 32
sprite.style.minimal_height = 32
sprite.style.maximal_width = 32
sprite.style.maximal_height = 32
sprite.style.margin = 4
2020-03-03 09:16:19 +02:00
sprite.style.padding = 0
2019-11-11 20:17:54 +02:00
end
2019-11-11 17:53:00 +02:00
local function display_item_list(player)
if not player.gui.left.fjei_main_window then return end
if not global.fjei.player_data[player.index].filtered_list then Functions.set_filtered_list(player) end
2019-11-11 17:53:00 +02:00
local active_page = global.fjei.player_data[player.index].active_page
local starting_index = 1 + (active_page - 1) * items_per_page
2019-11-12 18:31:50 +02:00
local sorted_item_list = global.fjei.sorted_item_list
2019-11-11 17:53:00 +02:00
local filtered_list = global.fjei.player_data[player.index].filtered_list
local item_list_table = player.gui.left.fjei_main_window.fjei_main_window_item_list_table
item_list_table.clear()
2019-11-11 17:53:00 +02:00
for i = starting_index, starting_index + items_per_page - 1, 1 do
if not filtered_list[i] then return end
2019-11-12 22:19:42 +02:00
add_sprite_icon(item_list_table, sorted_item_list[filtered_list[i]], false)
2019-11-11 17:53:00 +02:00
end
end
2019-11-11 15:55:09 +02:00
2019-11-11 22:10:40 +02:00
local function display_history(player)
if not player.gui.left.fjei_main_window then return end
local player_data = global.fjei.player_data[player.index]
local history = player_data.history
if not history then return end
local history_table = player.gui.left.fjei_main_window.fjei_main_window_history_table
history_table.clear()
2019-11-16 09:56:46 +02:00
for i = player_data.size_of_history - column_count, player_data.size_of_history, 1 do
2019-11-11 22:10:40 +02:00
local name = history[i]
if name then
2019-11-12 22:19:42 +02:00
add_sprite_icon(history_table, name, false)
2019-11-11 22:10:40 +02:00
end
end
end
2019-11-11 17:53:00 +02:00
function Public.refresh_main_window(player)
set_page_count_caption(player)
display_item_list(player)
2019-11-11 22:10:40 +02:00
display_history(player)
2019-11-11 17:53:00 +02:00
end
local function draw_main_window(player)
2019-11-11 15:55:09 +02:00
if player.gui.left["fjei_main_window"] then player.gui.left["fjei_main_window"].destroy() end
2019-11-11 17:53:00 +02:00
local frame = player.gui.left.add({type = "frame", name = "fjei_main_window", direction = "vertical"})
2019-11-16 09:56:46 +02:00
frame.style.minimal_width = main_window_width
frame.style.maximal_width = main_window_width
2019-11-12 19:02:54 +02:00
frame.style.padding = 4
frame.style.margin = 2
2019-11-11 15:55:09 +02:00
2019-11-11 17:53:00 +02:00
local t = frame.add({type = "table", name = "fjei_main_window_control_table", column_count = 4})
local element = t.add({type = "label", name = "fjei_main_window_page_counter", caption = " "})
2019-11-12 19:02:54 +02:00
element.style.font = "heading-2"
element.style.font_color = {222, 222, 222}
element.style.right_margin = 4
2019-11-11 17:53:00 +02:00
local element = t.add({type = "sprite-button", name = "fjei_main_window_previous_page", caption = ""})
element.style.font = "heading-1"
2019-11-12 19:02:54 +02:00
element.style.maximal_height = 32
element.style.maximal_width = 32
2019-11-11 17:53:00 +02:00
local element = t.add({type = "sprite-button", name = "fjei_main_window_next_page", caption = ""})
element.style.font = "heading-1"
2019-11-12 19:02:54 +02:00
element.style.maximal_height = 32
element.style.maximal_width = 32
2019-11-11 18:31:30 +02:00
local text = global.fjei.player_data[player.index].active_filter
if not text then text = "" end
local textfield = t.add({ type = "textfield", name = "fjei_main_window_search_textfield", text = text})
2019-11-12 19:02:54 +02:00
textfield.style.left_margin = 4
2019-11-13 01:40:03 +02:00
textfield.style.minimal_width = 140
textfield.style.maximal_width = 140
2019-11-11 17:53:00 +02:00
frame.add({type = "line"})
local t = frame.add({type = "table", name = "fjei_main_window_item_list_table", column_count = column_count})
2019-11-11 22:10:40 +02:00
frame.add({type = "line"})
local t = frame.add({type = "table", name = "fjei_main_window_history_table", column_count = column_count})
2019-11-16 09:56:46 +02:00
2019-11-11 17:53:00 +02:00
Public.refresh_main_window(player)
end
2019-12-13 13:20:52 +02:00
local function refresh_recipe_bar(player, selected_recipe)
2019-12-14 11:07:18 +02:00
local old_recipe_name = get_active_recipe_name(player)
if not old_recipe_name then return end
2019-12-13 13:20:52 +02:00
2020-03-03 09:16:19 +02:00
if not player.gui[recipe_window_position]["fjei_recipe_window"] then return end
if not player.gui[recipe_window_position]["fjei_recipe_window"].header_container then return end
local fjei_recipe_window_select_table = player.gui[recipe_window_position]["fjei_recipe_window"].header_container.header_table.scroll_pane.fjei_recipe_window_select_table
2019-12-13 13:20:52 +02:00
2019-12-14 11:07:18 +02:00
local container = fjei_recipe_window_select_table[old_recipe_name]
2019-12-13 13:20:52 +02:00
container.clear()
2019-12-14 11:07:18 +02:00
add_sprite_icon(container, old_recipe_name, true)
2019-12-13 13:20:52 +02:00
2019-12-14 11:07:18 +02:00
local container = fjei_recipe_window_select_table[selected_recipe]
container.clear()
local element = container.add({ type = "frame", name = "fjei_recipe_window_selected_recipe"})
element.style.margin = 0
element.style.padding = -4
add_sprite_icon(element, selected_recipe, true)
2019-12-13 13:20:52 +02:00
end
local function draw_recipe_window_header(player, container, item_name, recipes, mode, recipe_name)
2019-11-12 01:05:06 +02:00
2019-11-13 17:43:09 +02:00
local t = container.add({type = "table", name = "header_table", column_count = 4})
2019-11-11 23:58:20 +02:00
2019-11-12 22:19:42 +02:00
add_sprite_icon(t, item_name, false)
2019-11-13 17:43:09 +02:00
local element = t.add({type = "label", caption = ""})
if mode == 1 then element.caption = "Product\nof recipe: " else element.caption = "Ingredient\nin recipe: " end
element.style.single_line = false
2019-11-12 22:19:42 +02:00
element.style.font = "heading-2"
element.style.font_color = {222, 222, 222}
2019-11-13 17:43:09 +02:00
element.style.minimal_width = 78
element.style.maximal_width = 78
2019-11-12 22:19:42 +02:00
local scroll_pane = t.add({ type = "scroll-pane", name = "scroll_pane", horizontal_scroll_policy = "always", vertical_scroll_policy = "never"})
2019-11-13 17:43:09 +02:00
scroll_pane.style.minimal_width = recipe_window_width - 190
scroll_pane.style.maximal_width = recipe_window_width - 190
scroll_pane.style.minimal_height = 60
scroll_pane.style.maximal_height = 60
2019-12-14 11:07:18 +02:00
local tt = scroll_pane.add({type = "table", name = "fjei_recipe_window_select_table", column_count = 8192})
2019-12-13 13:20:52 +02:00
for key, name in pairs(recipes) do
2020-03-03 09:16:19 +02:00
if not tt[name] then
local ttt = tt.add({type = "table", name = name, column_count = 1})
if recipe_name == name then
local element = ttt.add({type = "frame", name = "fjei_recipe_window_selected_recipe"})
element.style.margin = 0
element.style.padding = -4
add_sprite_icon(element, name, true)
else
add_sprite_icon(ttt, name, true)
end
2019-12-13 13:20:52 +02:00
end
end
2019-11-12 22:19:42 +02:00
local element = t.add {type = "sprite-button", caption = "X", name = "fjei_close_recipe_window"}
2019-11-12 01:05:06 +02:00
element.style.font = "heading-1"
2019-11-13 17:43:09 +02:00
element.style.margin = 8
2019-11-13 01:40:03 +02:00
element.style.padding = 4
2019-11-13 17:43:09 +02:00
element.style.minimal_width = 34
element.style.maximal_width = 34
element.style.minimal_height = 34
element.style.maximal_height = 34
2019-11-12 22:19:42 +02:00
local element = container.add({type = "line"})
element.style.right_margin = 6
2019-11-13 17:43:09 +02:00
end
local function draw_recipe(player, container, recipe_name)
local recipe = game.recipe_prototypes[recipe_name]
local t = container.add({type = "table", column_count = 2})
2020-03-03 09:16:19 +02:00
add_choose_elem_button(t, recipe.name, true)
2019-11-12 01:17:29 +02:00
2019-11-12 01:05:06 +02:00
local tt = t.add({type = "table", column_count = 1})
2019-11-12 01:17:29 +02:00
local ttt = tt.add({type = "table", column_count = 2})
local element_1 = ttt.add({type = "label", caption = recipe.localised_name})
element_1.style.font = "heading-1"
2019-12-14 12:45:16 +02:00
element_1.style.single_line = false
local element_2 = ttt.add({type = "label", caption = " "})
element_2.style.font = "heading-1"
if not player.force.recipes[recipe_name].enabled then
element_2.style.font_color = {200, 0, 0}
element_2.caption = "*"
local str = "Further research is required to unlock this recipe."
element_2.tooltip = str
element_1.tooltip = str
end
2019-12-14 12:45:16 +02:00
local element = tt.add({type = "label", caption = "" .. math.round(recipe.energy, 2) .. " seconds crafting time"})
element.style.font = "default"
element.style.font_color = {215, 215, 215}
element.style.top_margin = -4
2019-11-13 17:43:09 +02:00
container.add({type = "line"})
2019-11-12 22:19:42 +02:00
2019-11-13 17:43:09 +02:00
local element = container.add({type = "label", caption = "Products:"})
2019-11-11 23:58:20 +02:00
element.style.font = "heading-2"
2019-11-13 17:43:09 +02:00
local t = container.add({type = "table", column_count = 2})
2019-11-11 23:58:20 +02:00
2019-11-13 17:43:09 +02:00
for _, product in pairs(recipe.products) do
2019-11-11 23:58:20 +02:00
local tt = t.add({type = "table", column_count = 3})
2020-03-03 09:16:19 +02:00
local amount = product.amount
if not amount then amount = 1 end
amount = amount * product.probability
local element = tt.add({type = "label", caption = get_formatted_amount(amount)})
element.style.minimal_width = recipe_window_amount_width
element.style.maximal_width = recipe_window_amount_width
element.style.single_line = false
2019-11-12 01:05:06 +02:00
element.style.horizontal_align = "right"
2020-03-03 09:16:19 +02:00
add_choose_elem_button(tt, product.name)
if product.temperature then
local ttt = tt.add({type = "table", column_count = 1})
local element = ttt.add({type = "label", caption = get_localised_name(product.name)})
2020-03-03 09:16:19 +02:00
element.style.minimal_width = recipe_window_item_name_width
element.style.maximal_width = recipe_window_item_name_width
element.style.single_line = false
element.style.font = "default"
local element = ttt.add({type = "label", caption = product.temperature .. " °C"})
else
local element = tt.add({type = "label", caption = get_localised_name(product.name)})
2020-03-03 09:16:19 +02:00
element.style.minimal_width = recipe_window_item_name_width
element.style.maximal_width = recipe_window_item_name_width
element.style.single_line = false
element.style.font = "default"
end
2019-11-11 23:58:20 +02:00
end
2019-11-13 17:43:09 +02:00
local element = container.add({type = "label", caption = "Ingredients:"})
2019-11-11 23:58:20 +02:00
element.style.font = "heading-2"
2019-11-13 17:43:09 +02:00
local t = container.add({type = "table", column_count = 2})
2019-11-11 23:58:20 +02:00
2019-11-13 17:43:09 +02:00
for key, ingredient in pairs(recipe.ingredients) do
2019-11-11 23:58:20 +02:00
local tt = t.add({type = "table", column_count = 3})
2020-03-03 09:16:19 +02:00
local element = tt.add({type = "label", caption = get_formatted_amount(ingredient.amount)})
element.style.minimal_width = recipe_window_amount_width
element.style.maximal_width = recipe_window_amount_width
element.style.single_line = false
2019-11-12 01:05:06 +02:00
element.style.horizontal_align = "right"
2020-03-03 09:16:19 +02:00
add_choose_elem_button(tt, ingredient.name)
if ingredient.temperature then
local ttt = tt.add({type = "table", column_count = 1})
local element = ttt.add({type = "label", caption = get_localised_name(ingredient.name)})
2020-03-03 09:16:19 +02:00
element.style.minimal_width = recipe_window_item_name_width
element.style.maximal_width = recipe_window_item_name_width
element.style.single_line = false
element.style.font = "default"
local element = ttt.add({type = "label", caption = ingredient.temperature .. " °C"})
else
local element = tt.add({type = "label", caption = get_localised_name(ingredient.name)})
2020-03-03 09:16:19 +02:00
element.style.minimal_width = recipe_window_item_name_width
element.style.maximal_width = recipe_window_item_name_width
element.style.single_line = false
element.style.font = "default"
end
2019-11-12 01:05:06 +02:00
end
2019-11-13 17:43:09 +02:00
local machines = Functions.get_crafting_machines_for_recipe(player.force.name, recipe)
2020-03-03 09:16:19 +02:00
local element = container.add({type = "line"})
element.style.top_margin = 2
element.style.bottom_margin = 2
if #machines == 0 then
local t = container.add({type = "table", column_count = 10})
local element = t.add({type = "label", caption = "Made by:"})
element.style.font = "heading-2"
local element = t.add({type = "label", caption = "Crafting method unknown."})
element.style.font = "heading-2"
element.style.font_color = {150, 150, 150}
return
end
2019-11-13 17:43:09 +02:00
local t = container.add({type = "table", column_count = 10})
local element = t.add({type = "label", caption = "Made by:"})
2020-03-03 09:16:19 +02:00
element.style.right_margin = 2
2019-11-13 17:43:09 +02:00
element.style.font = "heading-2"
2019-11-12 01:05:06 +02:00
for key, machine in pairs(machines) do
local prototype = game.entity_prototypes[machine]
2019-12-14 11:07:18 +02:00
if prototype then
2020-03-03 09:16:19 +02:00
add_choose_elem_button(t, machine)
2019-11-13 17:43:09 +02:00
end
end
end
local function create_recipe_window(item_name, player, button, selected_recipe)
local mode
if button == defines.mouse_button_type.left then mode = 1 else mode = 2 end
2020-03-03 09:16:19 +02:00
if selected_recipe and player.gui[recipe_window_position]["fjei_recipe_window"] then
2019-12-13 13:20:52 +02:00
refresh_recipe_bar(player, selected_recipe)
2020-03-03 09:16:19 +02:00
local container = player.gui[recipe_window_position]["fjei_recipe_window"].recipe_container
2019-11-13 17:43:09 +02:00
container.clear()
draw_recipe(player, container, selected_recipe)
return
end
if not global.fjei.item_list[item_name] then return end
--shift researched recipes forward in the list
local recipes = {}
2019-12-14 16:31:10 +02:00
local size_of_recipes = 0
local researched_recipes = {}
2019-12-14 16:31:10 +02:00
local size_of_researched_recipes = 0
local unknown_recipes = {}
2019-12-14 16:31:10 +02:00
local size_of_unknown_recipes = 0
local force_recipes = player.force.recipes
for k, recipe_name in pairs(global.fjei.item_list[item_name][mode]) do
2019-12-14 16:31:10 +02:00
if k > 512 then break end
if force_recipes[recipe_name] then
if force_recipes[recipe_name].enabled then
size_of_researched_recipes = size_of_researched_recipes + 1
researched_recipes[size_of_researched_recipes] = recipe_name
else
2019-12-14 16:31:10 +02:00
size_of_unknown_recipes = size_of_unknown_recipes + 1
unknown_recipes[size_of_unknown_recipes] = recipe_name
end
end
end
2019-12-14 16:31:10 +02:00
for k, recipe_name in pairs(researched_recipes) do
size_of_recipes = size_of_recipes + 1
recipes[size_of_recipes] = recipe_name
end
for k, recipe_name in pairs(unknown_recipes) do
size_of_recipes = size_of_recipes + 1
recipes[size_of_recipes] = recipe_name
end
2019-12-14 16:31:10 +02:00
if size_of_recipes == 0 then return end
2019-11-13 17:43:09 +02:00
if not selected_recipe then
for key, recipe_name in pairs(recipes) do
if #Functions.get_crafting_machines_for_recipe(player.force.name, game.recipe_prototypes[recipe_name]) > 0 then
selected_recipe = recipe_name
break
end
if key > 16 then break end
2019-11-12 01:05:06 +02:00
end
2019-11-13 17:43:09 +02:00
end
local recipe_name = recipes[1]
if selected_recipe then
for k, v in pairs(recipes) do if v == selected_recipe then recipe_name = recipes[k] end end
2019-11-11 23:58:20 +02:00
end
2019-11-13 00:42:27 +02:00
2020-03-03 09:16:19 +02:00
if player.gui[recipe_window_position]["fjei_recipe_window"] then player.gui[recipe_window_position]["fjei_recipe_window"].destroy() end
local frame = player.gui[recipe_window_position].add({type = "frame", name = "fjei_recipe_window", direction = "vertical"})
2019-11-13 17:43:09 +02:00
frame.style.minimal_width = recipe_window_width
frame.style.maximal_width = recipe_window_width
frame.style.padding = 4
2019-12-14 12:45:16 +02:00
frame.style.left_padding = 6
frame.style.right_padding = 6
2019-11-13 17:43:09 +02:00
local container = frame.add({type = "table", name = "header_container", column_count = 1})
2019-12-13 13:20:52 +02:00
draw_recipe_window_header(player, container, item_name, recipes, mode, recipe_name)
2019-11-13 17:43:09 +02:00
local container = frame.add({type = "table", name = "recipe_container", column_count = 1})
draw_recipe(player, container, recipe_name)
2019-11-13 00:42:27 +02:00
return true
2019-11-11 23:58:20 +02:00
end
2019-11-16 09:56:46 +02:00
local function add_to_history(item_name, player)
if not game.item_prototypes[item_name] and not game.fluid_prototypes[item_name] then return end
local player_data = global.fjei.player_data[player.index]
if not player_data.history then
player_data.history = {item_name}
player_data.size_of_history = 1
return
end
--avoid double elements
for _, v in pairs(player_data.history) do
if v == item_name then return end
end
player_data.size_of_history = player_data.size_of_history + 1
player_data.history[player_data.size_of_history] = item_name
if player_data.size_of_history > column_count then player_data.history[player_data.size_of_history - column_count] = nil end
end
local function show_cursor_stack_item(element, player, button)
local cursor_stack = player.cursor_stack
if not cursor_stack then return end
if not cursor_stack.valid_for_read then return end
if not global.fjei then Functions.build_tables() end
if not global.fjei.player_data[player.index] then global.fjei.player_data[player.index] = {} end
if not global.fjei.item_list[cursor_stack.name] then return end
add_to_history(cursor_stack.name, player)
draw_main_window(player)
create_recipe_window(cursor_stack.name, player, button)
return true
end
2019-11-11 18:31:30 +02:00
local function toggle_main_window(element, player, button)
2019-11-16 09:56:46 +02:00
if show_cursor_stack_item(element, player, button) then return true end
2019-11-11 17:53:00 +02:00
if player.gui.left.fjei_main_window then
2019-11-12 01:17:29 +02:00
player.gui.left.fjei_main_window.destroy()
2020-03-03 09:16:19 +02:00
if player.gui[recipe_window_position].fjei_recipe_window then player.gui[recipe_window_position].fjei_recipe_window.destroy() end
2019-11-11 17:53:00 +02:00
else
draw_main_window(player)
2019-11-11 15:55:09 +02:00
end
2019-11-11 17:53:00 +02:00
return true
end
2019-11-11 18:31:30 +02:00
local function main_window_next_page(element, player, button)
2019-11-12 18:31:50 +02:00
local player_data = global.fjei.player_data[player.index]
2019-11-12 03:39:18 +02:00
if button == defines.mouse_button_type.right then
for _ = 1, 5, 1 do
2019-11-12 18:31:50 +02:00
if player_data.active_page == get_total_page_count(player) then
player_data.active_page = 1
2019-11-12 03:39:18 +02:00
else
2019-11-12 18:31:50 +02:00
player_data.active_page = player_data.active_page + 1
2019-11-12 03:39:18 +02:00
end
end
2019-11-11 18:31:30 +02:00
else
2019-11-12 18:31:50 +02:00
if player_data.active_page == get_total_page_count(player) then
player_data.active_page = 1
2019-11-12 03:39:18 +02:00
else
2019-11-12 18:31:50 +02:00
player_data.active_page = player_data.active_page + 1
2019-11-12 03:39:18 +02:00
end
end
2019-11-11 18:31:30 +02:00
Public.refresh_main_window(player)
2019-11-16 09:56:46 +02:00
return true
2019-11-11 18:31:30 +02:00
end
local function main_window_previous_page(element, player, button)
2019-11-12 18:31:50 +02:00
local player_data = global.fjei.player_data[player.index]
2019-11-12 03:39:18 +02:00
if button == defines.mouse_button_type.right then
for _ = 1, 5, 1 do
2019-11-12 18:31:50 +02:00
if player_data.active_page == 1 then
player_data.active_page = get_total_page_count(player)
2019-11-12 03:39:18 +02:00
else
2019-11-12 18:31:50 +02:00
player_data.active_page = player_data.active_page - 1
2019-11-12 03:39:18 +02:00
end
end
2019-11-11 18:31:30 +02:00
else
2019-11-12 18:31:50 +02:00
if player_data.active_page == 1 then
player_data.active_page = get_total_page_count(player)
2019-11-12 03:39:18 +02:00
else
2019-11-12 18:31:50 +02:00
player_data.active_page = player_data.active_page - 1
2019-11-12 03:39:18 +02:00
end
end
2019-11-11 17:53:00 +02:00
Public.refresh_main_window(player)
2019-11-16 09:56:46 +02:00
return true
2019-11-11 17:53:00 +02:00
end
2019-11-11 18:31:30 +02:00
local function clear_search_textfield(element, player, button)
2019-11-16 09:56:46 +02:00
if show_cursor_stack_item(element, player, button) then return true end
if button ~= defines.mouse_button_type.right then return end
2019-11-11 18:31:30 +02:00
global.fjei.player_data[player.index].active_filter = false
element.text = ""
Functions.set_filtered_list(player)
2019-11-11 17:53:00 +02:00
Public.refresh_main_window(player)
2019-11-16 09:56:46 +02:00
return true
2019-11-11 17:53:00 +02:00
end
2019-11-12 01:05:06 +02:00
local function close_recipe_window(element, player, button)
2020-03-03 09:16:19 +02:00
local recipe_window = player.gui[recipe_window_position]["fjei_recipe_window"]
2019-11-14 02:23:47 +02:00
if recipe_window then recipe_window.destroy() end
2019-11-16 09:56:46 +02:00
return true
2019-11-12 01:05:06 +02:00
end
2019-11-11 17:53:00 +02:00
local gui_actions = {
["fjei_toggle_button"] = toggle_main_window,
["fjei_main_window_next_page"] = main_window_next_page,
["fjei_main_window_previous_page"] = main_window_previous_page,
2019-11-12 01:05:06 +02:00
["fjei_main_window_search_textfield"] = clear_search_textfield,
["fjei_close_recipe_window"] = close_recipe_window,
2019-11-11 17:53:00 +02:00
}
2019-11-11 18:31:30 +02:00
function Public.gui_click_actions(element, player, button)
2019-11-11 17:53:00 +02:00
if not gui_actions[element.name] then return end
2019-11-13 17:43:09 +02:00
if not global.fjei then Functions.build_tables() end
if not global.fjei.player_data[player.index] then global.fjei.player_data[player.index] = {} end
2019-11-11 18:31:30 +02:00
gui_actions[element.name](element, player, button)
2019-11-11 22:10:40 +02:00
return true
2019-11-11 15:55:09 +02:00
end
function Public.draw_top_toggle_button(player)
if player.gui.top["fjei_toggle_button"] then return end
2019-11-12 19:02:54 +02:00
local button = player.gui.top.add({type = "sprite-button", name = "fjei_toggle_button", caption = "FJEI"})
button.style.font = "heading-1"
button.style.font_color = {222, 222, 222}
2019-11-11 15:55:09 +02:00
button.style.minimal_height = 38
2019-11-12 19:02:54 +02:00
button.style.minimal_width = 50
2019-11-11 15:55:09 +02:00
button.style.padding = -2
end
2019-11-13 00:42:27 +02:00
function Public.open_recipe(element, player, button)
2019-11-13 17:43:09 +02:00
if not global.fjei then Functions.build_tables() end
if not global.fjei.player_data[player.index] then global.fjei.player_data[player.index] = {} end
2019-11-13 00:42:27 +02:00
local item_name = element.name
local selected_recipe = false
2019-11-13 17:43:09 +02:00
2019-12-13 13:20:52 +02:00
if element.parent.name == "fjei_recipe_window_selected_recipe" then return end
2019-12-14 11:07:18 +02:00
if element.parent then
if element.parent.parent then
if element.parent.parent.name == "fjei_recipe_window_select_table" then selected_recipe = item_name end
end
end
2019-11-13 17:43:09 +02:00
if element.parent.name == "fjei_main_window_item_list_table" or element.parent.name == "fjei_main_window_history_table" then
2020-03-03 09:16:19 +02:00
local recipe_window = player.gui[recipe_window_position]["fjei_recipe_window"]
2019-11-13 17:43:09 +02:00
if recipe_window then
local active_item = recipe_window.header_container.header_table.children[1].name
if active_item == element.name and global.fjei.player_data[player.index].last_button == button then
recipe_window.destroy()
return
end
end
2019-11-13 00:42:27 +02:00
end
2019-11-13 17:43:09 +02:00
2019-11-12 18:31:50 +02:00
add_to_history(item_name, player)
2019-11-13 00:42:27 +02:00
if not create_recipe_window(item_name, player, button, selected_recipe) then return end
2019-11-13 17:43:09 +02:00
display_history(player)
global.fjei.player_data[player.index].last_button = button
2019-11-11 22:10:40 +02:00
end
2019-11-11 15:55:09 +02:00
return Public