2017-09-24 14:17:10 +02:00
----------------------------------------------------------------------------------------------------------------------------------------
-- Create last lists for your Factory Workers
-- based on MewMew's Poll
----------------------------------------------------------------------------------------------------------------------------------------
local function create_tasklist_gui ( event )
2017-09-25 00:36:54 +02:00
local player = game.players [ event.player_index ]
if player.gui . top.tasklist == nil then
2017-09-26 14:18:26 +02:00
local button = player.gui . top.add { name = " tasklist " , type = " sprite-button " , sprite = " item/discharge-defense-remote " }
2017-09-25 00:36:54 +02:00
button.style . font = " default-bold "
button.style . minimal_height = 38
button.style . minimal_width = 38
button.style . top_padding = 2
button.style . left_padding = 4
button.style . right_padding = 4
button.style . bottom_padding = 2
end
2017-09-24 14:17:10 +02:00
end
local function tasklist_show ( player )
2017-09-25 00:36:54 +02:00
player.gui . left.direction = " horizontal "
local frame = player.gui . left.add { type = " frame " , name = " tasklist-panel " , direction = " vertical " }
frame.add { type = " table " , name = " tasklist_panel_table " , colspan = 1 }
local tasklist_panel_table = frame.tasklist_panel_table
tasklist_panel_table.add { type = " label " , caption = " Task List: " , single_line = false , name = " tasklist_title " }
local tasklist_label = tasklist_panel_table [ " tasklist_title " ]
tasklist_label.style . maximal_width = 208
tasklist_label.style . minimal_width = 208
tasklist_label.style . maximal_height = 165
tasklist_label.style . font = " default "
local counter = 0
for y = 1 , 5 do
if not ( global.tasklist_items [ y ] == " " ) then
counter = counter + 1
local z = tostring ( y )
tasklist_panel_table.add { type = " label " , caption = counter .. " . " .. global.tasklist_items [ y ] , single_line = false , name = " tasklist_item_label_ " .. z }
local tasklist_label = tasklist_panel_table [ " tasklist_item_label_ " .. z ]
tasklist_label.style . maximal_width = 208
tasklist_label.style . minimal_width = 208
tasklist_label.style . maximal_height = 165
tasklist_label.style . font = " default "
end
end
if global.tasklist_author ~= nil then
2017-09-30 14:07:41 +02:00
tasklist_panel_table.add { type = " label " , caption = " Updated by: " .. global.tasklist_author , single_line = false , name = " tasklist_author " }
2017-09-25 00:36:54 +02:00
local tasklist_label = tasklist_panel_table [ " tasklist_author " ]
tasklist_label.style . maximal_width = 208
tasklist_label.style . minimal_width = 208
tasklist_label.style . maximal_height = 165
tasklist_label.style . font = " default "
2017-09-30 14:07:41 +02:00
tasklist_label.style . font_color = { r = 0.55 , g = 0.55 , b = 0.55 }
2017-09-25 00:36:54 +02:00
end
frame.add { type = " table " , name = " tasklist_panel_button_table " , colspan = 3 }
local tasklist_panel_button_table = frame.tasklist_panel_button_table
global.tasklist_panel_creation_time [ player.index ] = game.tick
if is_regular ( player.name ) or is_mod ( player.name ) or player.admin then
tasklist_panel_button_table.add { type = " button " , caption = " New Tasks " , name = " new_tasklist_assembler_button " }
tasklist_panel_button_table.new_tasklist_assembler_button . style.font = " default-bold "
tasklist_panel_button_table.new_tasklist_assembler_button . style.minimal_height = 38
end
tasklist_panel_button_table.add { type = " button " , caption = " Hide " , name = " tasklist_hide_button " }
tasklist_panel_button_table.tasklist_hide_button . style.minimal_width = 70
tasklist_panel_button_table.tasklist_hide_button . style.font = " default-bold "
tasklist_panel_button_table.tasklist_hide_button . style.minimal_height = 38
-- Possible for later preferences tasklist_panel_button_table.add { type = "checkbox", caption = "Show Tasklist", state = global.autoshow_tasklist_for_player[player.name], name = "auto_show_tasklist_checkbox" }
2017-09-24 14:17:10 +02:00
end
local function tasklist ( player )
2017-09-25 00:36:54 +02:00
local frame = player.gui . left [ " tasklist-assembler " ]
frame = frame.table_tasklist_assembler
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
global.tasklist_items = { " " , " " , " " , " " , " " }
global.tasklist_items [ 1 ] = frame.textfield_task_1 . text
global.tasklist_items [ 2 ] = frame.textfield_task_2 . text
global.tasklist_items [ 3 ] = frame.textfield_task_3 . text
global.tasklist_items [ 4 ] = frame.textfield_task_4 . text
global.tasklist_items [ 5 ] = frame.textfield_task_5 . text
if ( global.tasklist_items [ 5 ] .. global.tasklist_items [ 4 ] .. global.tasklist_items [ 3 ] .. global.tasklist_items [ 2 ] .. global.tasklist_items [ 1 ] == " " ) then
return
end
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
global.tasklist_author = player.name
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
local msg = player.name
msg = msg .. " has created an updated tasklist! "
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
local frame = player.gui . left [ " tasklist-assembler " ]
frame.destroy ( )
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
for _ , player in pairs ( game.players ) do
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
local frame = player.gui . left [ " tasklist-panel " ]
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
if ( frame ) then
frame.destroy ( )
end
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
if ( global.autoshow_tasklist_for_player [ player.name ] == true ) then
tasklist_show ( player )
end
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
end
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
game.print ( msg )
2017-09-24 14:17:10 +02:00
end
local function tasklist_assembler ( player )
2017-09-25 00:36:54 +02:00
local frame = player.gui . left.add { type = " frame " , name = " tasklist-assembler " , caption = " " }
local frame_table = frame.add { type = " table " , name = " table_tasklist_assembler " , colspan = 2 }
frame_table.add { type = " label " , caption = " Task #1: " }
frame_table.add { type = " textfield " , name = " textfield_task_1 " , text = global.tasklist_items [ 1 ] , }
frame_table [ " textfield_task_1 " ] . style.minimal_width = 450
frame_table [ " textfield_task_1 " ] . style.maximal_width = 450
frame_table.add { type = " label " , caption = " Task #2: " }
frame_table.add { type = " textfield " , name = " textfield_task_2 " , text = global.tasklist_items [ 2 ] }
frame_table [ " textfield_task_2 " ] . style.minimal_width = 450
frame_table [ " textfield_task_2 " ] . style.maximal_width = 450
frame_table.add { type = " label " , caption = " Task #3: " }
frame_table.add { type = " textfield " , name = " textfield_task_3 " , text = global.tasklist_items [ 3 ] }
frame_table [ " textfield_task_3 " ] . style.minimal_width = 450
frame_table [ " textfield_task_3 " ] . style.maximal_width = 450
frame_table.add { type = " label " , caption = " Task #4: " }
frame_table.add { type = " textfield " , name = " textfield_task_4 " , text = global.tasklist_items [ 4 ] }
frame_table [ " textfield_task_4 " ] . style.minimal_width = 450
frame_table [ " textfield_task_4 " ] . style.maximal_width = 450
frame_table.add { type = " label " , caption = " Task #5: " }
frame_table.add { type = " textfield " , name = " textfield_task_5 " , text = global.tasklist_items [ 5 ] }
frame_table [ " textfield_task_5 " ] . style.minimal_width = 450
frame_table [ " textfield_task_5 " ] . style.maximal_width = 450
frame_table.add { type = " label " , caption = " " }
frame_table.add { type = " button " , name = " create_new_tasklist_button " , caption = " Publish " }
2017-09-24 14:17:10 +02:00
end
function tasklist_sync_for_new_joining_player ( event )
2017-09-25 00:36:54 +02:00
if not global.tasklist_items then global.tasklist_items = { " " , " " , " " , " " , " " } end
if not global.autoshow_tasklist_for_player then global.autoshow_tasklist_for_player = { } end
if not global.tasklist_duration_in_seconds then global.tasklist_duration_in_seconds = 99 end
if not global.tasklist_panel_creation_time then global.tasklist_panel_creation_time = { } end
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
local player = game.players [ event.player_index ]
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
global.autoshow_tasklist_for_player [ player.name ] = true
2017-09-24 14:17:10 +02:00
2017-09-25 00:36:54 +02:00
local frame = player.gui . left [ " tasklist-panel " ]
if ( frame == nil ) then
tasklist_show ( player )
end
2017-09-24 14:17:10 +02:00
end
local function on_gui_click ( event )
2017-09-25 00:36:54 +02:00
if not ( event and event.element and event.element . valid ) then return end
local player = game.players [ event.element . player_index ]
local name = event.element . name
if ( name == " tasklist " ) then
local frame = player.gui . left [ " tasklist-panel " ]
if ( frame ) then
frame.destroy ( )
else
tasklist_show ( player )
end
local frame = player.gui . left [ " tasklist-assembler " ]
if ( frame ) then
frame.destroy ( )
end
end
if ( name == " new_tasklist_assembler_button " ) then
local frame = player.gui . left [ " tasklist-assembler " ]
if ( frame ) then
frame.destroy ( )
else
tasklist_assembler ( player )
end
end
if ( name == " create_new_tasklist_button " ) then
tasklist ( player )
end
if ( name == " tasklist_hide_button " ) then
local frame = player.gui . left [ " tasklist-panel " ]
if ( frame ) then
frame.destroy ( )
end
local frame = player.gui . left [ " tasklist-assembler " ]
if ( frame ) then
frame.destroy ( )
end
end
if ( name == " auto_show_tasklist_checkbox " ) then
global.autoshow_tasklist_for_player [ player.name ] = event.element . state
end
2017-09-24 14:17:10 +02:00
end
Event.register ( defines.events . on_gui_click , on_gui_click )
Event.register ( defines.events . on_player_joined_game , create_tasklist_gui )
Event.register ( defines.events . on_player_joined_game , tasklist_sync_for_new_joining_player )