Task List Updated

This commit is contained in:
Cooldude2606
2019-10-24 01:44:41 +01:00
parent 886fb2c226
commit f7cff8b236
98 changed files with 1849 additions and 488 deletions

View File

@@ -12,337 +12,455 @@ local format_time,table_keys = ext_require('expcore.common','format_time','table
local Tasks = require 'modules.control.tasks' --- @dep modules.control.tasks
--- If a player is allowed to use the edit buttons
local function player_allowed_edit(player,task)
local function check_player_permissions(player,task)
if task then
-- When a task is given check if the player can edit it
local allow_edit_task = config.allow_edit_task
-- Check if the player being the last to edit will override existing permisisons
if config.user_can_edit_own_tasks and task.last_edit_name == player.name then
return true
end
else
if config.any_user_can_add_new_task then
-- Check player has permisison based on value in the config
if allow_edit_task == 'all' then
return true
elseif allow_edit_task == 'admin' then
return player.admin
elseif allow_edit_task == 'expcore.roles' then
return Roles.player_allowed(player,config.edit_tasks_role_permission)
end
end
if config.only_admins_can_edit and not player.admin then
-- Return false as all other condidtions have not been met
return false
else
-- When a task is not given check if the player can add a new task
local allow_add_task = config.allow_add_task
-- Check player has permisison based on value in the config
if allow_add_task == 'all' then
return true
elseif allow_add_task == 'admin' then
return player.admin
elseif allow_add_task == 'expcore.roles' then
return Roles.player_allowed(player,config.expcore_roles_add_permission)
end
-- Return false as all other condidtions have not been met
return false
end
if config.edit_tasks_role_permission and not Roles.player_allowed(player,config.edit_tasks_role_permission) then
return false
end
return true
end
--- Button in the header to add a new task
--- Button displayed in the ehader bar, used to add a new task
-- @element add_new_task
local add_new_task =
Gui.new_button()
:set_sprites('utility/add')
:set_tooltip{'task-list.add-tooltip'}
:set_style('tool_button',function(style)
Gui.set_padding_style(style,-2,-2,-2,-2)
style.height = 20
style.width = 20
end)
:on_click(function(player,element)
Gui.element{
type = 'sprite-button',
sprite = 'utility/add',
tooltip = {'task-list.add-tooltip'},
style = 'tool_button'
}
:style{
padding = -2,
height = 20,
width = 20
}
:on_click(function(player,_,_)
Tasks.add_task(player.force.name,nil,player.name)
end)
--- Used to save changes to a task
--- Button displayed next to tasks which the user is currently editing, used to save changes
-- @element confirm_edit
local confirm_edit =
Gui.new_button()
:set_sprites('utility/downloaded')
:set_tooltip{'task-list.confirm-tooltip'}
:set_style('tool_button',function(style)
Gui.set_padding_style(style,-2,-2,-2,-2)
style.height = 20
style.width = 20
end)
:on_click(function(player,element)
Gui.element{
type = 'sprite-button',
sprite = 'utility/downloaded',
tooltip = {'task-list.confirm-tooltip'},
style = 'tool_button'
}
:style{
padding = -2,
height = 20,
width = 20
}
:on_click(function(player,element,_)
local task_id = element.parent.name
local new_message = element.parent.task.text
local new_message = element.parent.task_entry.text
Tasks.set_editing(task_id,player.name)
Tasks.update_task(task_id,new_message,player.name)
end)
--- Used to cancel any changes you made to a task
--- Button displayed next to tasks which the user is currently editing, used to discard changes
-- @element cancel_edit
local cancel_edit =
Gui.new_button()
:set_sprites('utility/close_black')
:set_tooltip{'task-list.cancel-tooltip'}
:set_style('tool_button',function(style)
Gui.set_padding_style(style,-2,-2,-2,-2)
style.height = 20
style.width = 20
end)
:on_click(function(player,element)
Gui.element{
type = 'sprite-button',
sprite = 'utility/close_black',
tooltip = {'task-list.cancel-tooltip'},
style = 'tool_button'
}
:style{
padding = -2,
height = 20,
width = 20
}
:on_click(function(player,element,_)
local task_id = element.parent.name
Tasks.set_editing(task_id,player.name)
end)
--- Removes the task from the list
--- Button displayed next to tasks which the user is can edit, used to delete a task from the list
-- @element discard_task
local discard_task =
Gui.new_button()
:set_sprites('utility/trash')
:set_tooltip{'task-list.discard-tooltip'}
:set_style('tool_button',function(style)
Gui.set_padding_style(style,-2,-2,-2,-2)
style.height = 20
style.width = 20
end)
:on_click(function(player,element)
local task_id = element.parent.name
Gui.element{
type = 'sprite-button',
sprite = 'utility/trash',
tooltip = {'task-list.discard-tooltip'},
style = 'tool_button'
}
:style{
padding = -2,
height = 20,
width = 20
}
:on_click(function(_,element,_)
local task_id = element.parent.name:sub(6)
Tasks.remove_task(task_id)
end)
--- Opens edit mode for the task
--- Button displayed next to tasks which the user is can edit, used to start editing a task
-- @element edit_task
local edit_task =
Gui.new_button()
:set_sprites('utility/rename_icon_normal')
:set_tooltip{'task-list.edit-tooltip-none'}
:set_style('tool_button',function(style)
Gui.set_padding_style(style,-2,-2,-2,-2)
style.height = 20
style.width = 20
end)
:on_click(function(player,element)
local task_id = element.parent.name
Gui.element{
type = 'sprite-button',
sprite = 'utility/rename_icon_normal',
tooltip = {'task-list.edit-tooltip-none'},
style = 'tool_button'
}
:style{
padding = -2,
height = 20,
width = 20
}
:on_click(function(player,element,_)
local task_id = element.parent.name:sub(6)
Tasks.set_editing(task_id,player.name,true)
end)
--[[ Generates each task, handles both view and edit mode
element
> count-"task_id"
>> label
> "task_id"
>> task
>> cancel_edit (edit mode)
>> confirm_edit (edit mode)
> edit-"task_id"
>> edit_task
>> discard_task
]]
local function generate_task(player,element,task_id)
--- Set of three elements which make up each row of the task table
-- @element add_task_base
local add_task_base =
Gui.element(function(_,parent,task_id)
-- Add the task number label
parent.add{
name = 'count-'..task_id,
type = 'label',
caption = '0)'
}
-- Add a flow which will contain the task message and edit buttons
local task_flow =
parent.add{
name = task_id,
type = 'flow',
}
-- Set the padding on the task flow
local task_flow_style = task_flow.style
task_flow_style.padding = 0
-- Add the two edit buttons outside the task flow
local edit_flow = Gui.alignment(parent,nil,nil,'edit-'..task_id)
edit_task(edit_flow)
discard_task(edit_flow)
-- Return the task flow as the main element
return task_flow
end)
-- Removes the three elements that are added as part of the task base
local function remove_task_base(parent,task_id)
Gui.destroy_if_valid(parent['count-'..task_id])
Gui.destroy_if_valid(parent['edit-'..task_id])
Gui.destroy_if_valid(parent[task_id])
end
--- Default state for a task, contains only a label with the task message
-- @element task_label
local task_label =
Gui.element(function(_,parent,task)
local message = task.message
local last_edit_name = task.last_edit_name
local last_edit_time = task.last_edit_time
-- Draw the element
local element =
parent.add{
name = 'task_entry',
type = 'label',
caption = message,
tooltip = {'task-list.last-edit', last_edit_name, format_time(last_edit_time)}
}
-- Change the style
local style = element.style
style.single_line = false
style.maximal_width = 150
-- Return the element
return element
end)
--- Editing state for a task, contrins a text field and the two edit buttons
-- @element task_editing
local task_editing =
Gui.element(function(_,parent,task)
local message = task.message
-- Draw the element
local element =
parent.add{
name = 'task_entry',
type = 'textfield',
text = message
}
-- Change the style
local style = element.style
style.maximal_width = 150
style.height = 20
-- Add the edit buttons
cancel_edit(parent)
confirm_edit(parent)
-- Return the element
return element
end)
--- Updates a task for a player
local function update_task(player,task_table,task_id)
local task = Tasks.get_task(task_id)
local task_ids = Tasks.get_force_task_ids(player.force.name)
local task_number = table.index_of(task_ids, task_id)
-- Task no longer exists so should be removed from the list
if not task then
-- task is nil so remove it from the list
element.parent.no_tasks.visible = #task_ids == 1
Gui.destroy_if_valid(element['count-'..task_id])
Gui.destroy_if_valid(element['edit-'..task_id])
Gui.destroy_if_valid(element[task_id])
else
local message = task.message
local editing = task.curently_editing[player.name]
local last_edit_name = task.last_edit_name
local last_edit_time = task.last_edit_time
element.parent.no_tasks.visible = false
-- if it is not already present then add it now
local task_area = element[task_id]
if not task_area then
-- label to show the task number
element.add{
name='count-'..task_id,
type='label',
caption=task_number..')'
}
-- area which stores the task and buttons
task_area =
element.add{
name=task_id,
type='flow',
}
Gui.set_padding(task_area)
-- if the player can edit then it adds the edit and delete button
local flow = Gui.create_alignment(element,'edit-'..task_id)
local sub_flow = flow.add{type='flow',name=task_id}
edit_task(sub_flow)
discard_task(sub_flow)
end
-- update the number indexes and the current editing players
element['count-'..task_id].caption = task_number..')'
local edit_area = element['edit-'..task_id][task_id]
local players = table_keys(task.editing)
local allowed = player_allowed_edit(player,task)
edit_area.visible = allowed
if #players > 0 then
edit_area[edit_task.name].tooltip = {'task-list.edit-tooltip',table.concat(players,', ')}
else
edit_area[edit_task.name].tooltip = {'task-list.edit-tooltip-none'}
end
-- draws/updates the task area
local element_type = task_area.task and task_area.task.type or nil
if not editing and element_type == 'label' then
-- update the label already present
task_area.task.caption = message
task_area.task.tooltip = {'task-list.last-edit',last_edit_name,format_time(last_edit_time)}
elseif not editing then
-- create the label, view mode
if edit_area then
edit_area[edit_task.name].enabled = true
end
task_area.clear()
local label =
task_area.add{
name='task',
type='label',
caption=message,
tooltip={'task-list.last-edit',last_edit_name,format_time(last_edit_time)}
}
label.style.single_line = false
label.style.maximal_width = 150
elseif editing and element_type ~= 'textfield' then
-- create the text field, edit mode, update it omitted as value is being edited
if edit_area then
edit_area[edit_task.name].enabled = false
end
task_area.clear()
local entry =
task_area.add{
name='task',
type='textfield',
text=message
}
entry.style.maximal_width = 150
entry.style.height = 20
cancel_edit(task_area)
confirm_edit(task_area)
end
task_table.parent.no_tasks.visible = #task_ids == 0
remove_task_base(task_table,task_id)
return
end
-- Get the task flow for this task
local task_flow = task_table[task_id] or add_task_base(task_table,task_id)
task_table.parent.no_tasks.visible = false
task_table['count-'..task_id].caption = task_number..')'
-- Update the edit flow
local edit_flow = task_table['edit-'..task_id]
local player_allowed_edit = check_player_permissions(player,task)
local players_editing = table_keys(task.curently_editing)
local edit_task_element = edit_flow[edit_task.name]
local discard_task_element = edit_flow[discard_task.name]
edit_task_element.visible = player_allowed_edit
discard_task_element.visible = player_allowed_edit
if #players_editing > 0 then
edit_task_element.tooltip = {'task-list.edit-tooltip',table.concat(players_editing,', ')}
else
edit_task_element.tooltip = {'task-list.edit-tooltip-none'}
end
-- Check if the player is was editing and/or currently editing
local task_entry = task_flow.task_entry or task_label(task_flow,task)
local player_was_editing = task_entry.type == 'textfield'
local player_is_editing = task.curently_editing[player.name]
-- Update the task flow
if not player_was_editing and not player_is_editing then
-- Update the task message label
local message = task.message
local last_edit_name = task.last_edit_name
local last_edit_time = task.last_edit_time
task_entry.caption = message
task_entry.tooltip = {'task-list.last-edit', last_edit_name, format_time(last_edit_time)}
elseif player_was_editing and not player_is_editing then
-- Player was editing but is no longer, remove text field and add label
edit_task_element.enabled = true
task_flow.clear()
task_label(task_flow,task)
elseif not player_was_editing and player_is_editing then
-- Player was not editing but now is, remove label and add text field
edit_task_element.enabled = false
task_flow.clear()
task_editing(task_flow,task)
end
end
--[[ generates the main gui structure
element
> container
>> header
>>> right aligned add_new_task
>> scroll
>>> no_tasks
>>> table
]]
local function generate_container(player,element)
Gui.set_padding(element,2,2,2,2)
element.style.minimal_width = 200
local function update_all_tasks(player,scroll_table)
local task_ids = Tasks.get_force_task_ids(player.force.name)
if #task_ids > 0 then
for _,task_id in ipairs(task_ids) do
update_task(player,scroll_table,task_id)
end
end
end
-- main container which contains the other elements
local container =
element.add{
name='container',
type='frame',
direction='vertical',
style='window_content_frame_packed'
--- Main task list container for the left flow
-- @element task_list_container
local task_list_container =
Gui.element(function(event_trigger,parent)
-- Draw the external container
local frame =
parent.add{
name = event_trigger,
type = 'frame'
}
Gui.set_padding(container)
container.style.vertically_stretchable = false
-- main header for the gui
local header_area = Gui.create_header(
-- Set the frame style
frame.style.padding = 2
-- Draw the internal container
local container =
frame.add{
name = 'container',
type = 'frame',
direction = 'vertical',
style = 'window_content_frame_packed'
}
-- Set the container style
local style = container.style
style.vertically_stretchable = false
-- Draw the header
local header = Gui.header(
container,
{'task-list.main-caption'},
{'task-list.sub-tooltip'},
true
)
--- Right aligned button to toggle the section
if player_allowed_edit(player) then
add_new_task(header_area)
end
-- Draw the new task button
local player = Gui.get_player_from_element(parent)
local add_new_task_element = add_new_task(header)
add_new_task_element.visible = check_player_permissions(player)
-- table that stores all the data
local flow_table = Gui.create_scroll_table(container,3,185)
flow_table.draw_horizontal_lines = true
flow_table.vertical_centering = false
flow_table.style.top_cell_padding = 3
flow_table.style.bottom_cell_padding = 3
-- Draw the scroll table for the tasks
local scroll_table = Gui.scroll_table(container,185,3)
scroll_table.draw_horizontal_lines = true
scroll_table.vertical_centering = false
-- message to say that you have no tasks
local non_made =
flow_table.parent.add{
name='no_tasks',
type='label',
caption={'task-list.no-tasks'}
-- Change the style of the scroll table
local scroll_table_style = scroll_table.style
scroll_table_style.top_cell_padding = 3
scroll_table_style.bottom_cell_padding = 3
-- Draw the no tasks label
local no_tasks_label =
scroll_table.parent.add{
name = 'no_tasks',
type = 'label',
caption = {'task-list.no-tasks'}
}
non_made.style.width = 200
non_made.style.single_line = false
return flow_table
end
-- Change the style of the no tasks label
local no_tasks_style = no_tasks_label.style
no_tasks_style.padding = {2,4}
no_tasks_style.single_line = false
no_tasks_style.width = 200
--- Registers the task list
-- @element task_list
local task_list =
Gui.new_left_frame('gui/task-list')
:set_sprites('utility/not_enough_repair_packs_icon')
:set_direction('vertical')
:set_tooltip{'task-list.main-tooltip'}
:set_open_by_default()
:on_creation(function(player,element)
local data_table = generate_container(player,element)
-- Add any existing tasks
local task_ids = Tasks.get_force_task_ids(player.force.name)
for _,task_id in pairs(task_ids) do
generate_task(player,data_table,task_id)
if #task_ids > 0 then
no_tasks_style.visible = false
for _,task_id in ipairs(task_ids) do
update_task(player,scroll_table,task_id)
end
end
-- Return the exteral container
return frame
end)
:on_update(function(player,element)
local data_table = element.container.scroll.table
:add_to_left_flow(function(player)
local task_ids = Tasks.get_force_task_ids(player.force.name)
return #task_ids > 0
end)
for _,task_id in pairs(task_ids) do
generate_task(player,data_table,task_id)
end
--- Button on the top flow used to toggle the task list container
-- @element task_list_toggle
Gui.element{
type = 'sprite-button',
sprite = 'utility/not_enough_repair_packs_icon',
tooltip = {'task-list.main-tooltip'},
style = Gui.top_flow_button_style
}
:style{
padding = -2
}
:add_to_top_flow(function(player)
return Roles.player_allowed(player,'gui/task-list')
end)
:on_click(function(player,_,_)
Gui.toggle_left_element(player, task_list_container)
end)
--- When a new task is added it will udpate the task list for everyone on that force
Tasks.on_update(function(task,task_id)
local players
Tasks.on_update(function(task,task_id,removed_task)
-- Get the force to update, task is nil when removed
local force
if task then
local force = game.forces[task.force_name]
players = force.connected_players
force = game.forces[task.force_name]
else
players = game.connected_players
force = game.forces[removed_task.force_name]
end
for _,player in pairs(players) do
local frame = task_list:get_frame(player)
local element = frame.container.scroll.table
generate_task(player,element,task_id)
-- Update the task for all the players on the force
local task_ids = Tasks.get_force_task_ids(force.name)
for _,player in pairs(force.connected_players) do
local left_flow = Gui.get_left_flow(player)
local frame = left_flow[task_list_container.name]
local scroll_table = frame.container.scroll.table
-- Update the task that was changed
update_task(player,scroll_table,task_id)
-- Update the numbering of the other tasks if the task was removed
if not task then
for task_number, next_task_id in pairs(task_ids) do
scroll_table['count-'..next_task_id].caption = task_number..')'
end
end
end
end)
--- Update the tasks when the player joins
Event.add(defines.events.on_player_joined_game,task_list 'redraw')
Event.add(defines.events.on_player_joined_game,function(event)
local player = game.players[event.player_index]
local left_flow = Gui.get_left_flow(player)
local frame = left_flow[task_list_container.name]
local scroll_table = frame.container.scroll.table
update_all_tasks(player,scroll_table)
end)
--- Makes sure the right buttons are present when roles change
Event.add(Roles.events.on_role_assigned,task_list 'redraw')
Event.add(Roles.events.on_role_unassigned,task_list 'redraw')
local function role_update_event(event)
local player = game.players[event.player_index]
local left_flow = Gui.get_left_flow(player)
local container = left_flow[task_list_container.name].container
return task_list
-- Update the tasks, incase the user can now edit them
local scroll_table = container.scroll.table
update_all_tasks(player,scroll_table)
-- Update the new task button incase the user can now add them
local add_new_task_element = container.header.alignment[add_new_task.name]
add_new_task_element.visible = check_player_permissions(player)
end
Event.add(Roles.events.on_role_assigned,role_update_event)
Event.add(Roles.events.on_role_unassigned,role_update_event)