From 3ab6326a5f352f6b1ceadac8553bf037cfa0bd25 Mon Sep 17 00:00:00 2001 From: bbassie Date: Sun, 24 Jan 2021 02:02:24 +0100 Subject: [PATCH 01/26] Tasklist refactor to match the new warp system * Removed the ability to pass `task_number` to `Task.add_task` * Unified button styles of the Gui * Made task-list code simular to the new warp-list --- modules/control/tasks.lua | 17 +- modules/gui/task-list.lua | 322 +++++++++++++++++++++----------------- 2 files changed, 181 insertions(+), 158 deletions(-) diff --git a/modules/control/tasks.lua b/modules/control/tasks.lua index ec0f8c38..0b319709 100644 --- a/modules/control/tasks.lua +++ b/modules/control/tasks.lua @@ -31,7 +31,6 @@ end) --[[-- Add a new task for a force, the task can be placed into a certain position for that force @tparam string force_name the name of the force to add the task for -@tparam[opt] number task_number the order place to add the task to, appends to end if omited @tparam[opt] string player_name the player who added this task, will cause them to be listed under editing @tparam[opt] string task_message the message that is used for this task, if not given default is used @treturn string the uid of the task which was created @@ -40,25 +39,21 @@ end) local task_id = Tasks.add_task(game.player.force.name, nil, game.player.name) ]] -function Tasks.add_task(force_name, task_number, player_name, task_message) +function Tasks.add_task(force_name, player_name, task_message) -- Get a new task id local task_id = tostring(force_tasks._uid) task_message = task_message or 'New Task' force_tasks._uid = force_tasks._uid + 1 -- Get the existing tasks for this force - local tasks = force_tasks[force_name] - if not tasks then - force_tasks[force_name] = {} - tasks = force_tasks[force_name] + local task_ids = force_tasks[force_name] + if not task_ids then + task_ids = {} + force_tasks[force_name] = task_ids end -- Insert the task id into the forces tasks - if task_number then - table.insert(tasks, task_number, task_id) - else - table.insert(tasks, task_id) - end + table.insert(task_ids, task_id) -- Create the editing table local editing = {} diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index a83b3aa5..335f2516 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -13,8 +13,7 @@ local format_time = _C.format_time --- @dep expcore.common -- Styles used for sprite buttons local Styles = { - sprite20 = Gui.sprite_style(20), - sprite22 = Gui.sprite_style(20, nil, { right_margin = -3 }) + sprite22 = { height = 22, width = 22, padding = -2 }, } --- If a player is allowed to use the edit buttons @@ -66,130 +65,29 @@ Gui.element{ tooltip = {'task-list.add-tooltip'}, style = 'tool_button' } -:style(Styles.sprite20) +:style(Styles.sprite22) :on_click(function(player, _,_) - Tasks.add_task(player.force.name, nil, player.name) -end) - ---- Button displayed next to tasks which the user is can edit, used to start editing a task --- @element edit_task -local edit_task = -Gui.element{ - type = 'sprite-button', - sprite = 'utility/rename_icon_normal', - tooltip = {'task-list.edit-tooltip-none'}, - style = 'tool_button' -} -:style(Styles.sprite20) -:on_click(function(player, element, _) - local task_id = element.parent.name:sub(6) - Tasks.set_editing(task_id, player.name, true) -end) - ---- 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.element{ - type = 'sprite-button', - sprite = 'utility/trash', - tooltip = {'task-list.discard-tooltip'}, - style = 'tool_button' -} -:style(Styles.sprite20) -:on_click(function(_, element, _) - local task_id = element.parent.name:sub(6) - Tasks.remove_task(task_id) -end) - ---- 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 - local task_number = parent.add{ - name = 'count-'..task_id, - type = 'label', - caption = '0)' - } - task_number.style.left_margin = 1 - - -- Add a flow which will contain the task message and edit buttons - local task_flow = parent.add{ name = task_id, type = 'flow', } - task_flow.style.padding = 0 - - -- Add the two edit buttons outside the task flow - local edit_flow = Gui.alignment(parent, '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 - ---- Button displayed next to tasks which the user is currently editing, used to save changes --- @element confirm_edit -local task_editing -local confirm_edit = -Gui.element{ - type = 'sprite-button', - sprite = 'utility/confirm_slot', - tooltip = {'task-list.confirm-tooltip'}, - style = 'shortcut_bar_button_green' -} -:style(Styles.sprite22) -:on_click(function(player, element, _) - local task_id = element.parent.name - local new_message = element.parent[task_editing.name].text - Tasks.set_editing(task_id, player.name) - Tasks.update_task(task_id, new_message, player.name) -end) - ---- Button displayed next to tasks which the user is currently editing, used to discard changes --- @element cancel_edit -local cancel_edit = -Gui.element{ - type = 'sprite-button', - sprite = 'utility/close_black', - tooltip = {'task-list.cancel-tooltip'}, - style = 'shortcut_bar_button_red' -} -:style(Styles.sprite22) -:on_click(function(player, element, _) - local task_id = element.parent.name - Tasks.set_editing(task_id, player.name) + Tasks.add_task(player.force.name, player.name) end) --- Editing state for a task, contrins a text field and the two edit buttons -- @element task_editing -task_editing = +local task_textfield = Gui.element(function(event_trigger, parent, task) - local message = task.message - -- Draw the element local element = parent.add{ name = event_trigger, type = 'textfield', - text = message, + text = task.message, clear_and_focus_on_right_click = true } - -- Add the edit buttons - cancel_edit(parent) - confirm_edit(parent) - -- Return the element return element end) :style{ - maximal_width = 110, + maximal_width = 131, height = 20 } :on_confirmed(function(player, element, _) @@ -202,23 +100,134 @@ 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 +Gui.element(function(event_trigger, parent, task) local last_edit_name = task.last_edit_name local last_edit_time = task.last_edit_time -- Draw the element return parent.add{ - name = task_editing.name, + name = event_trigger, type = 'label', - caption = message, + caption = task.message, tooltip = {'task-list.last-edit', last_edit_name, format_time(last_edit_time)} } end) :style{ single_line = false, - maximal_width = 150 + maximal_width = 150, + horizontally_stretchable = true } +--- Button displayed next to tasks which the user is can edit, used to start editing a task +-- @element edit_task_button +local edit_task_button = +Gui.element{ + type = 'sprite-button', + sprite = 'utility/rename_icon_normal', + tooltip = {'task-list.edit-tooltip-none'}, + style = 'shortcut_bar_button' +} +:style(Styles.sprite22) +:on_click(function(player, element, _) + local task_id = element.parent.caption + Tasks.set_editing(task_id, player.name, true) +end) + +--- Button displayed next to tasks which the user is can edit, used to remove a task from the list +-- @element remove_task_button +local remove_task_button = +Gui.element{ + type = 'sprite-button', + sprite = 'utility/trash', + tooltip = {'task-list.discard-tooltip'}, + style = 'shortcut_bar_button_red' +} +:style(Styles.sprite22) +:on_click(function(_, element, _) + local task_id = element.parent.caption + Tasks.remove_task(task_id) +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['name-'..task_id]) + Gui.destroy_if_valid(parent['button-'..task_id]) +end + +--- Button displayed next to tasks which the user is currently editing, used to save changes +-- @element confirm_edit_button +local confirm_edit_button = +Gui.element{ + type = 'sprite-button', + sprite = 'utility/confirm_slot', + tooltip = {'task-list.confirm-tooltip'}, + style = 'shortcut_bar_button_green' +} +:style(Styles.sprite22) +:on_click(function(player, element, _) + local task_id = element.parent.caption + local new_message = element.parent.parent['name-'..task_id][task_textfield.name].text + Tasks.set_editing(task_id, player.name) + Tasks.update_task(task_id, new_message, player.name) +end) + +--- Button displayed next to tasks which the user is currently editing, used to cancel changes +-- @element cancel_edit_button +local cancel_edit_button = +Gui.element{ + type = 'sprite-button', + sprite = 'utility/close_black', + tooltip = {'task-list.cancel-tooltip'}, + style = 'shortcut_bar_button_red' +} +:style(Styles.sprite22) +:on_click(function(player, element, _) + local task_id = element.parent.caption + Tasks.set_editing(task_id, player.name) +end) + +--- Set of three elements which make up each row of the task table +-- @element add_task_elements +local add_task_elements = +Gui.element(function(_, parent, task) + -- Add the task number label + local task_number = parent.add{ + name = 'count-'..task.task_id, + type = 'label', + caption = '#)' + } + task_number.style.left_margin = 1 + + -- Add name flow this will contain the task label and textbox + local task_flow = parent.add{ + name = 'name-'..task.task_id, + type = 'flow', + caption = task.task_id, + } + task_flow.style.padding = 0 + + -- Add the label and textfield of the task + task_label(task_flow, task) + task_textfield(task_flow, task) + + -- Add button flow this will contain buttons to manage this specific task + local button_flow = parent.add{ + name = 'button-'..task.task_id, + type = 'flow', + caption = task.task_id, + } + button_flow.style.padding = 0 + + -- Add both edit state buttons + cancel_edit_button(button_flow) + confirm_edit_button(button_flow) + edit_task_button(button_flow) + remove_task_button(button_flow) + + -- Return the task flow as the main element + return { task_flow, button_flow } +end) + --- Updates a task for a player local function update_task(player, task_table, task_id) local task = Tasks.get_task(task_id) @@ -233,19 +242,31 @@ local function update_task(player, task_table, task_id) end -- Get the task flow for this task - local task_flow = task_table[task_id] or add_task_base(task_table, task_id) + if not task_table['name-'..task_id] then + add_task_elements(task_table, task) + end + local count_flow = task_table['count-'..task_id] + local name_flow = task_table['name-'..task_id] + local button_flow = task_table['button-'..task_id] task_table.parent.no_tasks.visible = false - task_table['count-'..task_id].caption = task_number..')' + count_flow.caption = task_number..')' - -- Update the edit flow - local edit_flow = task_table['edit-'..task_id] + -- Create local references to the elements for this task + local label_element = name_flow[task_label.name] + local textfield_element = name_flow[task_textfield.name] + + local cancel_edit_element = button_flow[cancel_edit_button.name] + local confirm_edit_element = button_flow[confirm_edit_button.name] + + local edit_task_element = button_flow[edit_task_button.name] + local remove_task_element = button_flow[remove_task_button.name] + + -- Hide the edit button if the player is not allowed to edit the task local player_allowed_edit = check_player_permissions(player, task) local players_editing = table.get_keys(task.currently_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 + remove_task_element.visible = player_allowed_edit + if #players_editing > 0 then edit_task_element.hovered_sprite = 'utility/warning_icon' edit_task_element.tooltip = {'task-list.edit-tooltip', table.concat(players_editing, ', ')} @@ -254,33 +275,39 @@ local function update_task(player, task_table, task_id) 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_editing.name] or task_label(task_flow, task) - local player_was_editing = task_entry.type == 'textfield' + -- Check if the player is editing the task local player_is_editing = task.currently_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 + if player_is_editing then + -- Set the name elements visibility + label_element.visible = false + textfield_element.visible = true + textfield_element.focus() + task_table.parent.scroll_to_element(textfield_element, 'top-third') + + -- Set the edit buttons + cancel_edit_element.visible = true + confirm_edit_element.visible = true + -- Set the task buttons + edit_task_element.visible = false + remove_task_element.visible = false + else + -- Set the name elements visibility + label_element.visible = true + label_element.caption = 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).focus() - task_table.parent.scroll_to_element(task_flow, 'top-third') + label_element.tooltip = {'task-list.last-edit', last_edit_name, format_time(last_edit_time)} + textfield_element.visible = false + textfield_element.focus() + task_table.parent.scroll_to_element(textfield_element, 'top-third') + -- Set the edit buttons + cancel_edit_element.visible = false + confirm_edit_element.visible = false + -- Set the task buttons + edit_task_element.visible = true and player_allowed_edit + remove_task_element.visible = true and player_allowed_edit end end @@ -299,7 +326,7 @@ end local task_list_container = Gui.element(function(event_trigger, parent) -- Draw the internal container - local container = Gui.container(parent, event_trigger, 200) + local container = Gui.container(parent, event_trigger, 230) -- Draw the header local header = Gui.header( @@ -316,6 +343,7 @@ Gui.element(function(event_trigger, parent) -- Draw the scroll table for the tasks local scroll_table = Gui.scroll_table(container, 190, 3) + scroll_table.parent.vertical_scroll_policy = 'always' scroll_table.draw_horizontal_lines = true scroll_table.vertical_centering = false @@ -334,9 +362,9 @@ Gui.element(function(event_trigger, parent) -- 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 + no_tasks_style.padding = {4, 4} + no_tasks_style.single_line = true + no_tasks_style.horizontally_stretchable = true -- Add any existing tasks local task_ids = Tasks.get_force_task_ids(player.force.name) From 84159a743a14d25a3af5da5bb4f9d7b427a2155d Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sun, 24 Jan 2021 02:39:20 +0000 Subject: [PATCH 02/26] Bumped lua action to v8 --- .github/workflows/dev-deploy.yml | 2 +- .github/workflows/pr-checker.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dev-deploy.yml b/.github/workflows/dev-deploy.yml index 418d1360..f1d49189 100644 --- a/.github/workflows/dev-deploy.yml +++ b/.github/workflows/dev-deploy.yml @@ -18,7 +18,7 @@ jobs: fetch-depth: 0 - name: Install Lua - uses: leafo/gh-actions-lua@v5 + uses: leafo/gh-actions-lua@v8.0.0 - name: Install LuaRocks uses: leafo/gh-actions-luarocks@v2 diff --git a/.github/workflows/pr-checker.yml b/.github/workflows/pr-checker.yml index 77e50408..f2277cd5 100644 --- a/.github/workflows/pr-checker.yml +++ b/.github/workflows/pr-checker.yml @@ -28,7 +28,7 @@ jobs: fetch-depth: 0 - name: Install Lua - uses: leafo/gh-actions-lua@v5 + uses: leafo/gh-actions-lua@v8.0.0 - name: Install LuaRocks uses: leafo/gh-actions-luarocks@v2 From 2bdc1ff8b0ea9065291aaf13553948996990c375 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Sun, 24 Jan 2021 02:56:49 +0000 Subject: [PATCH 03/26] Bumped luarocks action to v4 --- .github/workflows/dev-deploy.yml | 2 +- .github/workflows/pr-checker.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dev-deploy.yml b/.github/workflows/dev-deploy.yml index f1d49189..d1773da3 100644 --- a/.github/workflows/dev-deploy.yml +++ b/.github/workflows/dev-deploy.yml @@ -21,7 +21,7 @@ jobs: uses: leafo/gh-actions-lua@v8.0.0 - name: Install LuaRocks - uses: leafo/gh-actions-luarocks@v2 + uses: leafo/gh-actions-luarocks@v4 - name: Install LDoc run: luarocks install ldoc 1.4.4-1 diff --git a/.github/workflows/pr-checker.yml b/.github/workflows/pr-checker.yml index f2277cd5..aaf4242a 100644 --- a/.github/workflows/pr-checker.yml +++ b/.github/workflows/pr-checker.yml @@ -31,7 +31,7 @@ jobs: uses: leafo/gh-actions-lua@v8.0.0 - name: Install LuaRocks - uses: leafo/gh-actions-luarocks@v2 + uses: leafo/gh-actions-luarocks@v4 - name: Install LDoc run: luarocks install ldoc 1.4.4-1 From 8a652648406fe7b9f1fab9563f9036cc76aabd58 Mon Sep 17 00:00:00 2001 From: bbassie Date: Wed, 27 Jan 2021 01:11:33 +0100 Subject: [PATCH 04/26] Task list styling fixes: * Ported over textbox styles to make it strechable. * Fixed the label to allow multi line and not create weird empty areas below the table. * Fixed cell padding to be better spaced, may need more fine tuning. --- modules/gui/task-list.lua | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 335f2516..d1438cbd 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -87,8 +87,16 @@ Gui.element(function(event_trigger, parent, task) return element end) :style{ - maximal_width = 131, - height = 20 + -- Needed fields to make it squashable and strechable. + minimal_width = 10, + maximal_width = 300, + horizontally_squashable = "on", + horizontally_stretchable = "on", + -- Other styling + height = 22, + padding = -2, + left_margin = 2, + right_margin = 2, } :on_confirmed(function(player, element, _) local task_id = element.parent.name @@ -113,7 +121,7 @@ Gui.element(function(event_trigger, parent, task) end) :style{ single_line = false, - maximal_width = 150, + vertically_stretchable = true, horizontally_stretchable = true } @@ -196,7 +204,7 @@ Gui.element(function(_, parent, task) type = 'label', caption = '#)' } - task_number.style.left_margin = 1 + task_number.style.padding = 0 -- Add name flow this will contain the task label and textbox local task_flow = parent.add{ @@ -326,7 +334,7 @@ end local task_list_container = Gui.element(function(event_trigger, parent) -- Draw the internal container - local container = Gui.container(parent, event_trigger, 230) + local container = Gui.container(parent, event_trigger, 268) -- Draw the header local header = Gui.header( @@ -343,14 +351,15 @@ Gui.element(function(event_trigger, parent) -- Draw the scroll table for the tasks local scroll_table = Gui.scroll_table(container, 190, 3) + -- Set the scroll panel to always show the scrollbar (not doing this will result in a changing gui size) scroll_table.parent.vertical_scroll_policy = 'always' scroll_table.draw_horizontal_lines = true scroll_table.vertical_centering = false -- 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 + scroll_table_style.top_cell_padding = 2 + scroll_table_style.bottom_cell_padding = 4 -- Draw the no tasks label local no_tasks_label = From 1042fe637964ba96dda8351ebc527a43341f227f Mon Sep 17 00:00:00 2001 From: bbassie Date: Wed, 27 Jan 2021 22:13:03 +0100 Subject: [PATCH 05/26] Make number bold --- modules/gui/task-list.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index d1438cbd..7281f9ed 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -205,6 +205,7 @@ Gui.element(function(_, parent, task) caption = '#)' } task_number.style.padding = 0 + task_number.style.font = 'default-semibold' -- Add name flow this will contain the task label and textbox local task_flow = parent.add{ From 3d9c7e3400b6ac775f5641003f65b48f625f6aad Mon Sep 17 00:00:00 2001 From: bbassie Date: Sat, 10 Apr 2021 21:30:29 +0200 Subject: [PATCH 06/26] Task list redesign - Redesigned the gui - Added / changed locales where needed --- locale/en/gui.cfg | 21 +- modules/gui/task-list.lua | 938 +++++++++++++++++++++++--------------- 2 files changed, 586 insertions(+), 373 deletions(-) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index e4fa8611..41338c23 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -69,18 +69,25 @@ net-tooltip=Total net: __1__ no-packs=You have not made any science packs yet [task-list] -main-caption=Task List +main-caption=Task List [img=info] main-tooltip=Task List -sub-tooltip=Tasks that remain to be done -no-tasks=You have no tasks +sub-tooltip=Tasks that remain to be done\n- You can use richtext to include images [img=utility/not_enough_repair_packs_icon] or [color=blue]color[/color] your tasks. +no-tasks=No tasks found! +no-tasks-tooltip=Click on the plus button to the top right of this window to add a new task! last-edit=Last edited by __1__ at __2__ add-tooltip=Add new task -confirm-tooltip=Save changes -cancel-tooltip=Discard changes +confirm=Confirm +confirm-tooltip=Save task +discard=Discard +discard-tooltip=Discard task/changes +close=Close +close-tooltip=Close task details +edit=Edit task edit-tooltip=Currently being edited by: __1__ edit-tooltip-none=Currently being edited by: Nobody -discard-tooltip=Remove task - +create-footer-header=Create task +edit-footer-header=Edit task +view-footer-header=Task details [autofill] main-tooltip=Autofill settings toggle-section-caption=__1__ __2__ diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 7281f9ed..21606601 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -3,17 +3,37 @@ @gui Task-List @alias task_list ]] - -local Gui = require 'expcore.gui' --- @dep expcore.gui -local Event = require 'utils.event' --- @dep utils.event -local Roles = require 'expcore.roles' --- @dep expcore.roles -local config = require 'config.gui.tasks' --- @dep config.gui.tasks -local Tasks = require 'modules.control.tasks' --- @dep modules.control.tasks +local Gui = require "expcore.gui" --- @dep expcore.gui +local Event = require "utils.event" --- @dep utils.event +local Roles = require "expcore.roles" --- @dep expcore.roles +local Datastore = require "expcore.datastore" --- @dep expcore.datastore +local config = require "config.gui.tasks" --- @dep config.gui.tasks +local Tasks = require "modules.control.tasks" --- @dep modules.control.tasks local format_time = _C.format_time --- @dep expcore.common +--- Stores all data for the task gui by player +local TaskGuiData = Datastore.connect("TaskGuiData") +TaskGuiData:set_serializer(Datastore.name_serializer) +local PlayerIsEditing = TaskGuiData:combine("PlayerIsEditing") +PlayerIsEditing:set_default(false) +local PlayerIsCreating = TaskGuiData:combine("PlayerIsCreating") +PlayerIsCreating:set_default(false) +local PlayerSelected = TaskGuiData:combine("PlayerSelected") +PlayerSelected:set_default(nil) + -- Styles used for sprite buttons local Styles = { - sprite22 = { height = 22, width = 22, padding = -2 }, + sprite22 = { + height = 22, + width = 22, + padding = -2 + }, + footer_button = { + height = 22, + maximal_width = 268, + horizontally_stretchable = true, + padding = -2 + } } --- If a player is allowed to use the edit buttons @@ -28,11 +48,11 @@ local function check_player_permissions(player, task) end -- Check player has permisison based on value in the config - if allow_edit_task == 'all' then + if allow_edit_task == "all" then return true - elseif allow_edit_task == 'admin' then + elseif allow_edit_task == "admin" then return player.admin - elseif allow_edit_task == 'expcore.roles' then + elseif allow_edit_task == "expcore.roles" then return Roles.player_allowed(player, config.expcore_roles_allow_edit_task) end @@ -43,11 +63,11 @@ local function check_player_permissions(player, 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 + if allow_add_task == "all" then return true - elseif allow_add_task == 'admin' then + elseif allow_add_task == "admin" then return player.admin - elseif allow_add_task == 'expcore.roles' then + elseif allow_add_task == "expcore.roles" then return Roles.player_allowed(player, config.expcore_roles_allow_add_task) end @@ -56,399 +76,585 @@ local function check_player_permissions(player, task) end end ---- Button displayed in the ehader bar, used to add a new task +--- Elements + +--- Button displayed in the header bar, used to add a new task -- @element add_new_task local add_new_task = -Gui.element{ - type = 'sprite-button', - sprite = 'utility/add', - tooltip = {'task-list.add-tooltip'}, - style = 'tool_button' -} -:style(Styles.sprite22) -:on_click(function(player, _,_) - Tasks.add_task(player.force.name, player.name) -end) - ---- Editing state for a task, contrins a text field and the two edit buttons --- @element task_editing -local task_textfield = -Gui.element(function(event_trigger, parent, task) - -- Draw the element - local element = - parent.add{ - name = event_trigger, - type = 'textfield', - text = task.message, - clear_and_focus_on_right_click = true - } - - -- Return the element - return element -end) -:style{ - -- Needed fields to make it squashable and strechable. - minimal_width = 10, - maximal_width = 300, - horizontally_squashable = "on", - horizontally_stretchable = "on", - -- Other styling - height = 22, - padding = -2, - left_margin = 2, - right_margin = 2, -} -:on_confirmed(function(player, element, _) - local task_id = element.parent.name - local new_message = element.text - Tasks.set_editing(task_id, player.name) - Tasks.update_task(task_id, new_message, player.name) -end) - ---- Default state for a task, contains only a label with the task message --- @element task_label -local task_label = -Gui.element(function(event_trigger, parent, task) - local last_edit_name = task.last_edit_name - local last_edit_time = task.last_edit_time - -- Draw the element - return parent.add{ - name = event_trigger, - type = 'label', - caption = task.message, - tooltip = {'task-list.last-edit', last_edit_name, format_time(last_edit_time)} - } -end) -:style{ - single_line = false, - vertically_stretchable = true, - horizontally_stretchable = true -} - ---- Button displayed next to tasks which the user is can edit, used to start editing a task --- @element edit_task_button -local edit_task_button = -Gui.element{ - type = 'sprite-button', - sprite = 'utility/rename_icon_normal', - tooltip = {'task-list.edit-tooltip-none'}, - style = 'shortcut_bar_button' -} -:style(Styles.sprite22) -:on_click(function(player, element, _) - local task_id = element.parent.caption - Tasks.set_editing(task_id, player.name, true) -end) - ---- Button displayed next to tasks which the user is can edit, used to remove a task from the list --- @element remove_task_button -local remove_task_button = -Gui.element{ - type = 'sprite-button', - sprite = 'utility/trash', - tooltip = {'task-list.discard-tooltip'}, - style = 'shortcut_bar_button_red' -} -:style(Styles.sprite22) -:on_click(function(_, element, _) - local task_id = element.parent.caption - Tasks.remove_task(task_id) -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['name-'..task_id]) - Gui.destroy_if_valid(parent['button-'..task_id]) -end - ---- Button displayed next to tasks which the user is currently editing, used to save changes --- @element confirm_edit_button -local confirm_edit_button = -Gui.element{ - type = 'sprite-button', - sprite = 'utility/confirm_slot', - tooltip = {'task-list.confirm-tooltip'}, - style = 'shortcut_bar_button_green' -} -:style(Styles.sprite22) -:on_click(function(player, element, _) - local task_id = element.parent.caption - local new_message = element.parent.parent['name-'..task_id][task_textfield.name].text - Tasks.set_editing(task_id, player.name) - Tasks.update_task(task_id, new_message, player.name) -end) - ---- Button displayed next to tasks which the user is currently editing, used to cancel changes --- @element cancel_edit_button -local cancel_edit_button = -Gui.element{ - type = 'sprite-button', - sprite = 'utility/close_black', - tooltip = {'task-list.cancel-tooltip'}, - style = 'shortcut_bar_button_red' -} -:style(Styles.sprite22) -:on_click(function(player, element, _) - local task_id = element.parent.caption - Tasks.set_editing(task_id, player.name) -end) - ---- Set of three elements which make up each row of the task table --- @element add_task_elements -local add_task_elements = -Gui.element(function(_, parent, task) - -- Add the task number label - local task_number = parent.add{ - name = 'count-'..task.task_id, - type = 'label', - caption = '#)' - } - task_number.style.padding = 0 - task_number.style.font = 'default-semibold' - - -- Add name flow this will contain the task label and textbox - local task_flow = parent.add{ - name = 'name-'..task.task_id, - type = 'flow', - caption = task.task_id, - } - task_flow.style.padding = 0 - - -- Add the label and textfield of the task - task_label(task_flow, task) - task_textfield(task_flow, task) - - -- Add button flow this will contain buttons to manage this specific task - local button_flow = parent.add{ - name = 'button-'..task.task_id, - type = 'flow', - caption = task.task_id, - } - button_flow.style.padding = 0 - - -- Add both edit state buttons - cancel_edit_button(button_flow) - confirm_edit_button(button_flow) - edit_task_button(button_flow) - remove_task_button(button_flow) - - -- Return the task flow as the main element - return { task_flow, button_flow } -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.get_index(task_ids, task_id) - - -- Task no longer exists so should be removed from the list - if not task then - task_table.parent.no_tasks.visible = #task_ids == 0 - remove_task_base(task_table, task_id) - return + Gui.element { + type = "sprite-button", + sprite = "utility/add", + tooltip = {"task-list.add-tooltip"}, + style = "tool_button" +}:style(Styles.sprite22):on_click( + function(player, _, _) + -- Disable editing + PlayerIsEditing:set(player, false) + -- Clear selected + PlayerSelected:set(player, nil) + -- Open create task footer + PlayerIsCreating:set(player, true) end +) - -- Get the task flow for this task - if not task_table['name-'..task_id] then - add_task_elements(task_table, task) +--- Header displayed when no tasks are in the task list +-- @element no_tasks_found +local no_tasks_found = + Gui.element( + function(_, parent) + local header = + parent.add { + name = "no_tasks_found_element", + type = "frame", + style = "negative_subheader_frame" + } + header.style.horizontally_stretchable = true + -- Flow used for centering the content in the subheader + local center = + header.add { + type = "flow", + style = "centering_horizontal_flow" + } + center.style.horizontally_stretchable = true + center.add { + name = "header_label", + type = "label", + style = "bold_label", + caption = {"", "[img=utility/warning_white] ", {"task-list.no-tasks"}}, + tooltip = "Add task" + } + return header end - local count_flow = task_table['count-'..task_id] - local name_flow = task_table['name-'..task_id] - local button_flow = task_table['button-'..task_id] - task_table.parent.no_tasks.visible = false - count_flow.caption = task_number..')' +) - -- Create local references to the elements for this task - local label_element = name_flow[task_label.name] - local textfield_element = name_flow[task_textfield.name] - - local cancel_edit_element = button_flow[cancel_edit_button.name] - local confirm_edit_element = button_flow[confirm_edit_button.name] - - local edit_task_element = button_flow[edit_task_button.name] - local remove_task_element = button_flow[remove_task_button.name] - - -- Hide the edit button if the player is not allowed to edit the task - local player_allowed_edit = check_player_permissions(player, task) - local players_editing = table.get_keys(task.currently_editing) - edit_task_element.visible = player_allowed_edit - remove_task_element.visible = player_allowed_edit - - if #players_editing > 0 then - edit_task_element.hovered_sprite = 'utility/warning_icon' - edit_task_element.tooltip = {'task-list.edit-tooltip', table.concat(players_editing, ', ')} - else - edit_task_element.hovered_sprite = edit_task_element.sprite - edit_task_element.tooltip = {'task-list.edit-tooltip-none'} +local subfooter_frame = + Gui.element( + function(_, parent, name) + return parent.add { + type = "frame", + name = name, + direction = "vertical", + style = "subfooter_frame" + } end - - -- Check if the player is editing the task - local player_is_editing = task.currently_editing[player.name] - - if player_is_editing then - -- Set the name elements visibility - label_element.visible = false - textfield_element.visible = true - textfield_element.focus() - task_table.parent.scroll_to_element(textfield_element, 'top-third') - - -- Set the edit buttons - cancel_edit_element.visible = true - confirm_edit_element.visible = true - -- Set the task buttons - edit_task_element.visible = false - remove_task_element.visible = false - else - -- Set the name elements visibility - label_element.visible = true - label_element.caption = task.message - local last_edit_name = task.last_edit_name - local last_edit_time = task.last_edit_time - label_element.tooltip = {'task-list.last-edit', last_edit_name, format_time(last_edit_time)} - textfield_element.visible = false - textfield_element.focus() - task_table.parent.scroll_to_element(textfield_element, 'top-third') - - -- Set the edit buttons - cancel_edit_element.visible = false - confirm_edit_element.visible = false - -- Set the task buttons - edit_task_element.visible = true and player_allowed_edit - remove_task_element.visible = true and player_allowed_edit +):style( + { + padding = 5, + use_header_filler = false, + horizontally_stretchable = true + } +) +local subfooter_label = + Gui.element( + function(_, parent, caption) + return parent.add { + name = "footer_label", + type = "label", + style = "heading_1_label", + caption = caption + } end -end +) +local subfooter_actions = + Gui.element( + function(_, parent) + return parent.add { + type = "flow", + name = "actions" + } + end +) --- Update all the tasks for a player -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) +local task_list_item +task_list_item = + Gui.element( + function(event_trigger, parent, task) + local flow = + parent.add { + type = "flow", + name = "task-" .. task.task_id, + caption = task.task_id + } + flow.style.horizontally_stretchable = true + local button = + flow.add { + name = event_trigger, + type = "button", + style = "list_box_item", + caption = task.message + } + button.style.horizontally_stretchable = true + button.style.horizontally_squashable = true + return flow + end +):on_click( + function(player, element, _) + local task_id = element.parent.caption + PlayerSelected:set(player, task_id) + end +) + +--- Scrollable list of all tasks +-- @element no_tasks_found +local task_list = + Gui.element( + function(_, parent) + local scroll_pane = + parent.add { + name = "scroll", + type = "scroll-pane", + direction = "vertical", + horizontal_scroll_policy = "never", + vertical_scroll_policy = "auto", + style = "scroll_pane_under_subheader" + } + scroll_pane.style.horizontally_stretchable = true + scroll_pane.style.padding = 0 + scroll_pane.style.maximal_height = 300 + + local flow = + scroll_pane.add { + name = "task_list", + type = "flow", + direction = "vertical" + } + flow.style.vertical_spacing = 0 + flow.style.horizontally_stretchable = true + + return flow + end +) + +local view_task_edit_button = + Gui.element { + type = "button", + caption = {"", "[img=utility/rename_icon_normal] ", {"task-list.edit"}}, + tooltip = {"task-list.edit-tooltip"}, + style = "shortcut_bar_button" +}:style(Styles.footer_button):on_click( + function(player, _, _) + local selected = PlayerSelected:get(player) + PlayerIsEditing:set(player, true) + + Tasks.set_editing(selected, player.name, true) + end +) +local view_task_close_button = + Gui.element { + type = "button", + caption = {"", "[img=utility/close_black] ", {"task-list.close"}}, + tooltip = {"task-list.close-tooltip"}, + style = "shortcut_bar_button" +}:style(Styles.footer_button):on_click( + function(player, _, _) + PlayerSelected:set(player, nil) + end +) + +local task_view_footer = + Gui.element( + function(_, parent) + local footer = subfooter_frame(parent, "view") + subfooter_label(footer, {"task-list.view-footer-header"}) + + local label = + footer.add { + type = "label", + name = "message", + caption = "New task" + } + label.style.padding = 4 + label.style.single_line = false + + local action_flow = subfooter_actions(footer) + + view_task_edit_button(action_flow) + view_task_close_button(action_flow) + return footer + end +) +local edit_task_confirm_button +local create_task_confirm_button +local task_message_textfield +task_message_textfield = + Gui.element { + type = "textfield", + text = "" +}:style( + { + maximal_width = 268, + horizontally_stretchable = true + } +):on_text_changed( + function(player, element, _) + local isEditing = PlayerIsEditing:get(player) + local isCreating = PlayerIsCreating:get(player) + + local valid = string.len(element.text) > 5 + + if isCreating then + element.parent.actions[create_task_confirm_button.name].enabled = valid + elseif isEditing then + element.parent.actions[edit_task_confirm_button.name].enabled = valid end end -end +):on_confirmed( + function(player, element, _) + local isEditing = PlayerIsEditing:get(player) + local isCreating = PlayerIsCreating:get(player) + + local message = element.text + local valid = string.len(element.text) > 5 + if not valid then + return + end + + if isCreating then + PlayerIsCreating:set(player, false) + + local task_id = Tasks.add_task(player.force.name, player.name, message) + PlayerSelected:set(player, task_id) + elseif isEditing then + local selected = PlayerSelected:get(player) + PlayerIsEditing:set(player, false) + Tasks.update_task(selected, message, player.name) + Tasks.set_editing(selected, player.name, nil) + end + end +) +edit_task_confirm_button = + Gui.element { + type = "button", + caption = {"", "[img=utility/check_mark] ", {"task-list.confirm"}}, + tooltip = {"task-list.confirm-tooltip"}, + style = "shortcut_bar_button_green" +}:style(Styles.footer_button):on_click( + function(player, element, _) + local selected = PlayerSelected:get(player) + PlayerIsEditing:set(player, false) + local new_message = element.parent.parent[task_message_textfield.name].text + Tasks.update_task(selected, new_message, player.name) + Tasks.set_editing(selected, player.name, nil) + end +) +local edit_task_discard_button = + Gui.element { + type = "button", + caption = {"", "[img=utility/close_black] ", {"task-list.discard"}}, + tooltip = {"task-list.discard-tooltip"}, + style = "shortcut_bar_button_red" +}:style(Styles.footer_button):on_click( + function(player, _, _) + local selected = PlayerSelected:get(player) + Tasks.set_editing(selected, player.name, nil) + PlayerIsEditing:set(player, false) + -- Redraw selected + PlayerSelected:set(player, selected) + end +) +local task_edit_footer = + Gui.element( + function(_, parent) + local footer = subfooter_frame(parent, "edit") + subfooter_label(footer, {"task-list.edit-footer-header"}) + + task_message_textfield(footer) + + local action_flow = subfooter_actions(footer) + + edit_task_discard_button(action_flow) + edit_task_confirm_button(action_flow) + + return footer + end +) + +create_task_confirm_button = + Gui.element { + type = "button", + caption = {"", "[img=utility/check_mark] ", {"task-list.confirm"}}, + tooltip = {"task-list.confirm-tooltip"}, + style = "shortcut_bar_button_green", + enabled = false +}:style(Styles.footer_button):on_click( + function(player, element, _) + local message = element.parent.parent[task_message_textfield.name].text + PlayerIsCreating:set(player, false) + + local task_id = Tasks.add_task(player.force.name, player.name, message) + PlayerSelected:set(player, task_id) + end +) +local create_task_discard_button = + Gui.element { + type = "button", + caption = {"", "[img=utility/close_black] ", {"task-list.discard"}}, + tooltip = {"task-list.discard-tooltip"}, + style = "shortcut_bar_button_red" +}:style(Styles.footer_button):on_click( + function(player, _, _) + PlayerIsCreating:set(player, false) + end +) +local task_create_footer = + Gui.element( + function(_, parent) + local footer = subfooter_frame(parent, "create") + subfooter_label(footer, {"task-list.create-footer-header"}) + + task_message_textfield(footer) + + local action_flow = subfooter_actions(footer) + + create_task_discard_button(action_flow) + create_task_confirm_button(action_flow) + + return footer + end +) --- Main task list container for the left flow -- @element task_list_container local task_list_container = -Gui.element(function(event_trigger, parent) - -- Draw the internal container - local container = Gui.container(parent, event_trigger, 268) + Gui.element( + function(event_trigger, parent) + -- Draw the internal container + local container = Gui.container(parent, event_trigger, 268) + container.style.maximal_width = 268 + container.style.minimal_width = 268 - -- Draw the header - local header = Gui.header( - container, - {'task-list.main-caption'}, - {'task-list.sub-tooltip'}, - true - ) + -- Draw the header + local header = Gui.header(container, {"task-list.main-caption"}, {"task-list.sub-tooltip"}, true) - -- 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) + -- 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) - -- Draw the scroll table for the tasks - local scroll_table = Gui.scroll_table(container, 190, 3) - -- Set the scroll panel to always show the scrollbar (not doing this will result in a changing gui size) - scroll_table.parent.vertical_scroll_policy = 'always' - scroll_table.draw_horizontal_lines = true - scroll_table.vertical_centering = false + -- Draw no task found element + no_tasks_found(container) - -- Change the style of the scroll table - local scroll_table_style = scroll_table.style - scroll_table_style.top_cell_padding = 2 - scroll_table_style.bottom_cell_padding = 4 + -- Draw task list element + task_list(container) - -- Draw the no tasks label - local no_tasks_label = - scroll_table.parent.add{ - name = 'no_tasks', - type = 'label', - caption = {'task-list.no-tasks'} - } - - -- Change the style of the no tasks label - local no_tasks_style = no_tasks_label.style - no_tasks_style.padding = {4, 4} - no_tasks_style.single_line = true - no_tasks_style.horizontally_stretchable = true - - -- Add any existing tasks - local task_ids = Tasks.get_force_task_ids(player.force.name) - if #task_ids > 0 then - no_tasks_label.visible = false - for _, task_id in ipairs(task_ids) do - update_task(player, scroll_table, task_id) - end + local task_view_footer_element = task_view_footer(container) + local task_edit_footer_element = task_edit_footer(container) + local task_create_footer_element = task_create_footer(container) + task_view_footer_element.visible = false + task_edit_footer_element.visible = false + task_create_footer_element.visible = false + -- Return the exteral container + return container.parent end - - -- Return the exteral container - return container.parent -end) -:add_to_left_flow(function(player) - local task_ids = Tasks.get_force_task_ids(player.force.name) - return #task_ids > 0 -end) +):add_to_left_flow( + function(player) + local task_ids = Tasks.get_force_task_ids(player.force.name) + return #task_ids > 0 + end +) --- Button on the top flow used to toggle the task list container -- @element toggle_left_element -Gui.left_toolbar_button('utility/not_enough_repair_packs_icon', {'task-list.main-tooltip'}, task_list_container, function(player) - return Roles.player_allowed(player, 'gui/task-list') -end) +Gui.left_toolbar_button( + "utility/not_enough_repair_packs_icon", + {"task-list.main-tooltip"}, + task_list_container, + function(player) + return Roles.player_allowed(player, "gui/task-list") + end +) + +local update_task = function(player, task_list_element, task_id) + local task = Tasks.get_task(task_id) + local task_ids = Tasks.get_force_task_ids(player.force.name) + + -- Task no longer exists so should be removed from the list + if not task then + task_list_element["task-" .. task_id] = nil + return + end + task_list_element.parent.parent.no_tasks_found_element.visible = #task_ids == 0 + + local element + -- If task does not exist yet add it to the list + if not task_list_element["task-" .. task_id] then + element = task_list_item(task_list_element, task) + else + -- If the task exists update the caption + element = task_list_element["task-" .. task_id] + element[task_list_item.name].caption = task.message + end + -- Set tooltip + local last_edit_name = task.last_edit_name + local last_edit_time = task.last_edit_time + element[task_list_item.name].tooltip = {"task-list.last-edit", last_edit_name, format_time(last_edit_time)} +end + +-- Update the footer task edit view +local update_task_edit_footer = function(player, task_id) + local task = Tasks.get_task(task_id) + local frame = Gui.get_left_element(player, task_list_container) + local edit_flow = frame.container.edit + + local message_element = edit_flow[task_message_textfield.name] + + message_element.focus() + message_element.text = task.message +end + +-- Update the footer task view +local update_task_view_footer = function(player, task_id) + local task = Tasks.get_task(task_id) + local frame = Gui.get_left_element(player, task_list_container) + local view_flow = frame.container.view + local has_permission = check_player_permissions(player, task) + + local message_element = view_flow.message + local edit_button_element = view_flow.actions[view_task_edit_button.name] + + edit_button_element.visible = has_permission + message_element.caption = task.message + + local players_editing = table.get_keys(task.currently_editing) + if #players_editing > 0 then + -- edit_task_element.hovered_sprite = 'utility/warning_icon' + edit_button_element.tooltip = {"task-list.edit-tooltip", table.concat(players_editing, ", ")} + else + -- edit_task_element.hovered_sprite = edit_task_element.sprite + edit_button_element.tooltip = {"task-list.edit-tooltip-none"} + end +end --- When a new task is added it will update the task list for everyone on that force -Tasks.on_update(function(task_id, task, old_task) - -- Get the force to update, task is nil when removed - local force - if task then - force = game.forces[task.force_name] - else - force = game.forces[old_task.force_name] - end +Tasks.on_update( + function(task_id, task, old_task) + -- Get the force to update, task is nil when removed + local force + if task then + force = game.forces[task.force_name] + else + force = game.forces[old_task.force_name] + end - -- 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 frame = Gui.get_left_element(player, task_list_container) - 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..')' + -- Update the task for all the players on the force + for _, player in pairs(force.connected_players) do + -- Update the task view if the player has the changed task selected + local selected = PlayerSelected:get(player) + if selected == task_id then + update_task_view_footer(player, selected) end + + local frame = Gui.get_left_element(player, task_list_container) + local task_list_element = frame.container.scroll.task_list + + -- Update the task that was changed + update_task(player, task_list_element, task_id) end end +) -end) +PlayerIsCreating:on_update( + function(player_name, new_state, _) + local player = game.players[player_name] ---- Update the tasks when the player joins -Event.add(defines.events.on_player_joined_game, function(event) - local player = game.players[event.player_index] - local frame = Gui.get_left_element(player, task_list_container) - local scroll_table = frame.container.scroll.table - update_all_tasks(player, scroll_table) -end) + local frame = Gui.get_left_element(player, task_list_container) + local create = frame.container.create + + -- Clear the textfield + frame.container.create[task_message_textfield.name].text = "" + frame.container.create.actions[create_task_confirm_button.name].enabled = false + + if new_state then + create.visible = true + else + create.visible = false + end + end +) + +-- When a player selects a different warp from the list +PlayerSelected:on_update( + function(player_name, new_state, old_state) + local player = game.players[player_name] + + local frame = Gui.get_left_element(player, task_list_container) + local task_list_element = frame.container.scroll.task_list + local view_flow = frame.container.view + local edit_flow = frame.container.edit + local isEditing = PlayerIsEditing:get(player) + + if old_state then + task_list_element["task-" .. old_state][task_list_item.name].enabled = true + end + + if new_state then + task_list_element["task-" .. new_state][task_list_item.name].enabled = false + + update_task_view_footer(player, new_state) + + if isEditing then + update_task_edit_footer(player, new_state) + Tasks.set_editing(old_state, player.name, nil) + Tasks.set_editing(new_state, player.name, true) + view_flow.visible = false + edit_flow.visible = true + else + view_flow.visible = true + edit_flow.visible = false + end + else + -- If new state nil then hide footer elements and set editing to nil for old_state + if old_state then + Tasks.set_editing(old_state, player.name, nil) + end + view_flow.visible = false + edit_flow.visible = false + end + end +) + +-- When the edit view opens or closes +PlayerIsEditing:on_update( + function(player_name, new_state, _) + local player = game.players[player_name] + + local frame = Gui.get_left_element(player, task_list_container) + local view_flow = frame.container.view + local edit_flow = frame.container.edit + + local selected = PlayerSelected:get(player) + if new_state then + update_task_edit_footer(player, selected) + view_flow.visible = false + edit_flow.visible = true + else + view_flow.visible = true + edit_flow.visible = false + end + end +) --- Makes sure the right buttons are present when roles change local function role_update_event(event) local player = game.players[event.player_index] local container = Gui.get_left_element(player, task_list_container).container - - -- Update the tasks, incase the user can now edit them - local scroll_table = container.scroll.table - update_all_tasks(player, scroll_table) + -- Update the view task + local selected = PlayerSelected:get(player) + if selected then + update_task_view_footer(player, selected) + -- Set selected to the same value, this will make sure if player was in a editing state he will leave it if no permissions. + PlayerSelected:set(selected) + end -- Update the new task button incase the user can now add them + local has_permission = check_player_permissions(player) local add_new_task_element = container.header.alignment[add_new_task.name] - add_new_task_element.visible = check_player_permissions(player) + add_new_task_element.visible = has_permission + local isCreating = PlayerIsCreating:get(player) + if isCreating and not has_permission then + PlayerIsCreating:set(false) + end end Event.add(Roles.events.on_role_assigned, role_update_event) -Event.add(Roles.events.on_role_unassigned, role_update_event) \ No newline at end of file +Event.add(Roles.events.on_role_unassigned, role_update_event) From d853d1c9912b80a9cee18e9e9530a859e0b15ece Mon Sep 17 00:00:00 2001 From: bbassie Date: Sat, 10 Apr 2021 22:13:21 +0200 Subject: [PATCH 07/26] Delete button and some fixes - Add delete task button - Fix update task item - Fix no tasks found visibility and tooltip --- locale/en/gui.cfg | 2 ++ modules/gui/task-list.lua | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index 41338c23..557253d2 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -80,6 +80,8 @@ confirm=Confirm confirm-tooltip=Save task discard=Discard discard-tooltip=Discard task/changes +delete=Delete +delete-tooltip=Delete task close=Close close-tooltip=Close task details edit=Edit task diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 21606601..3d7e7ac0 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -121,7 +121,7 @@ local no_tasks_found = type = "label", style = "bold_label", caption = {"", "[img=utility/warning_white] ", {"task-list.no-tasks"}}, - tooltip = "Add task" + tooltip = {"task-list.no-tasks-tooltip"} } return header end @@ -250,6 +250,19 @@ local view_task_close_button = PlayerSelected:set(player, nil) end ) +local view_task_delete_button = + Gui.element { + type = "button", + caption = {"", "[img=utility/trash] ", {"task-list.delete"}}, + tooltip = {"task-list.delete-tooltip"}, + style = "shortcut_bar_button_red" +}:style(Styles.footer_button):on_click( + function(player, _, _) + local selected = PlayerSelected:get(player) + PlayerSelected:set(player, nil) + Tasks.remove_task(selected) + end +) local task_view_footer = Gui.element( @@ -267,7 +280,7 @@ local task_view_footer = label.style.single_line = false local action_flow = subfooter_actions(footer) - + view_task_delete_button(action_flow) view_task_edit_button(action_flow) view_task_close_button(action_flow) return footer @@ -467,13 +480,14 @@ Gui.left_toolbar_button( local update_task = function(player, task_list_element, task_id) local task = Tasks.get_task(task_id) local task_ids = Tasks.get_force_task_ids(player.force.name) + -- Set visibility of the no_tasks_found element + task_list_element.parent.parent.no_tasks_found_element.visible = #task_ids == 0 -- Task no longer exists so should be removed from the list if not task then - task_list_element["task-" .. task_id] = nil + task_list_element["task-" .. task_id].destroy() return end - task_list_element.parent.parent.no_tasks_found_element.visible = #task_ids == 0 local element -- If task does not exist yet add it to the list @@ -511,8 +525,10 @@ local update_task_view_footer = function(player, task_id) local message_element = view_flow.message local edit_button_element = view_flow.actions[view_task_edit_button.name] + local delete_button_element = view_flow.actions[view_task_delete_button.name] edit_button_element.visible = has_permission + delete_button_element.visible = has_permission message_element.caption = task.message local players_editing = table.get_keys(task.currently_editing) From 11296b3b367d6fdfe7b899c97de37b7c1db80470 Mon Sep 17 00:00:00 2001 From: bbassie Date: Sun, 11 Apr 2021 19:54:38 +0200 Subject: [PATCH 08/26] Small improvements - Removed extra redraw that doesn't seem to be needed - Fixed that it would not focus on the create new warp textbox --- modules/gui/task-list.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 3d7e7ac0..f4c632bd 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -361,8 +361,6 @@ local edit_task_discard_button = local selected = PlayerSelected:get(player) Tasks.set_editing(selected, player.name, nil) PlayerIsEditing:set(player, false) - -- Redraw selected - PlayerSelected:set(player, selected) end ) local task_edit_footer = @@ -577,8 +575,11 @@ PlayerIsCreating:on_update( local create = frame.container.create -- Clear the textfield - frame.container.create[task_message_textfield.name].text = "" - frame.container.create.actions[create_task_confirm_button.name].enabled = false + local message_element = frame.container.create[task_message_textfield.name] + local confirm_button_element = frame.container.create.actions[create_task_confirm_button.name] + message_element.focus() + message_element.text = "" + confirm_button_element.enabled = false if new_state then create.visible = true From 745f10b10d032a35b9144e24f9bfc172b440c0b5 Mon Sep 17 00:00:00 2001 From: bbassie Date: Sun, 11 Apr 2021 20:09:35 +0200 Subject: [PATCH 09/26] Added file and logic comments --- modules/gui/task-list.lua | 58 +++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index f4c632bd..25ab7c86 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -127,6 +127,8 @@ local no_tasks_found = end ) +--- Frame element with the right styling +-- @element subfooter_frame local subfooter_frame = Gui.element( function(_, parent, name) @@ -144,6 +146,9 @@ local subfooter_frame = horizontally_stretchable = true } ) + +--- Label element preset +-- @element subfooter_label local subfooter_label = Gui.element( function(_, parent, caption) @@ -155,6 +160,9 @@ local subfooter_label = } end ) + +--- Action flow that contains action buttons +-- @element subfooter_actions local subfooter_actions = Gui.element( function(_, parent) @@ -165,8 +173,9 @@ local subfooter_actions = end ) -local task_list_item -task_list_item = +--- Button element with a flow around it to fix duplicate name inside of the scroll flow +-- @element task_list_item +local task_list_item = Gui.element( function(event_trigger, parent, task) local flow = @@ -225,6 +234,8 @@ local task_list = end ) +--- Button element inside the task view footer to start editing a task +-- @element view_task_edit_button local view_task_edit_button = Gui.element { type = "button", @@ -239,6 +250,9 @@ local view_task_edit_button = Tasks.set_editing(selected, player.name, true) end ) + +--- Button to close the view task footer +-- @element view_task_close_button local view_task_close_button = Gui.element { type = "button", @@ -250,6 +264,9 @@ local view_task_close_button = PlayerSelected:set(player, nil) end ) + +--- Button to delete the task +-- @element view_task_delete_button local view_task_delete_button = Gui.element { type = "button", @@ -264,6 +281,8 @@ local view_task_delete_button = end ) +--- Subfooter inside the tasklist container +-- @element task_view_footer local task_view_footer = Gui.element( function(_, parent) @@ -286,10 +305,14 @@ local task_view_footer = return footer end ) + +-- Button variable initialisation because it is used inside the textfield element events local edit_task_confirm_button local create_task_confirm_button -local task_message_textfield -task_message_textfield = + +--- Textfield element used in both the create and edit footers +-- @element task_message_textfield +local task_message_textfield = Gui.element { type = "textfield", text = "" @@ -335,6 +358,9 @@ task_message_textfield = end end ) + +--- Button to confirm the changes +-- @element edit_task_confirm_button edit_task_confirm_button = Gui.element { type = "button", @@ -350,6 +376,9 @@ edit_task_confirm_button = Tasks.set_editing(selected, player.name, nil) end ) + +--- Button to discard the changes +-- @element edit_task_discard_button local edit_task_discard_button = Gui.element { type = "button", @@ -363,6 +392,9 @@ local edit_task_discard_button = PlayerIsEditing:set(player, false) end ) + +--- Subfooter inside the tasklist container +-- @element task_edit_footer local task_edit_footer = Gui.element( function(_, parent) @@ -380,6 +412,8 @@ local task_edit_footer = end ) +--- Button to confirm the changes +-- @element create_task_confirm_button create_task_confirm_button = Gui.element { type = "button", @@ -396,6 +430,9 @@ create_task_confirm_button = PlayerSelected:set(player, task_id) end ) + +--- Button to discard the changes +-- @element create_task_discard_button local create_task_discard_button = Gui.element { type = "button", @@ -407,6 +444,9 @@ local create_task_discard_button = PlayerIsCreating:set(player, false) end ) + +--- Subfooter inside the tasklist container +-- @element task_create_footer local task_create_footer = Gui.element( function(_, parent) @@ -475,10 +515,11 @@ Gui.left_toolbar_button( end ) +-- Function to update a single task and some of the elements inside the container local update_task = function(player, task_list_element, task_id) local task = Tasks.get_task(task_id) local task_ids = Tasks.get_force_task_ids(player.force.name) - -- Set visibility of the no_tasks_found element + -- Set visibility of the no_tasks_found element depending on the amount of tasks still in the task manager task_list_element.parent.parent.no_tasks_found_element.visible = #task_ids == 0 -- Task no longer exists so should be removed from the list @@ -567,6 +608,7 @@ Tasks.on_update( end ) +-- When a player is creating a new task. PlayerIsCreating:on_update( function(player_name, new_state, _) local player = game.players[player_name] @@ -600,15 +642,19 @@ PlayerSelected:on_update( local edit_flow = frame.container.edit local isEditing = PlayerIsEditing:get(player) + -- If the selection has a old state re-enable the button list element if old_state then task_list_element["task-" .. old_state][task_list_item.name].enabled = true end if new_state then + -- Disable the selected element task_list_element["task-" .. new_state][task_list_item.name].enabled = false + -- Update the view footer update_task_view_footer(player, new_state) + -- Depending on if the player is currently editing change the current edit session to the new task if isEditing then update_task_edit_footer(player, new_state) Tasks.set_editing(old_state, player.name, nil) @@ -663,7 +709,7 @@ local function role_update_event(event) PlayerSelected:set(selected) end - -- Update the new task button incase the user can now add them + -- Update the new task button and create footer incase the user can now add them local has_permission = check_player_permissions(player) local add_new_task_element = container.header.alignment[add_new_task.name] add_new_task_element.visible = has_permission From 7bbefd67b445720090a4067840d3b49165a62cfc Mon Sep 17 00:00:00 2001 From: bbassie Date: Sun, 11 Apr 2021 23:03:16 +0200 Subject: [PATCH 10/26] Change button size - The icons were clipping --- modules/gui/task-list.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 25ab7c86..656d5500 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -29,7 +29,7 @@ local Styles = { padding = -2 }, footer_button = { - height = 22, + height = 29, maximal_width = 268, horizontally_stretchable = true, padding = -2 From 465627535b561dbb01bfabf96ec0fa5c6af9c47b Mon Sep 17 00:00:00 2001 From: bbassie Date: Sun, 11 Apr 2021 23:07:07 +0200 Subject: [PATCH 11/26] Change tooltip of the confirm button - Tooltip didn't show usefull information on why you could not confirm --- locale/en/gui.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index 557253d2..260f1c2e 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -77,7 +77,7 @@ no-tasks-tooltip=Click on the plus button to the top right of this window to add last-edit=Last edited by __1__ at __2__ add-tooltip=Add new task confirm=Confirm -confirm-tooltip=Save task +confirm-tooltip=Save task (minimum of 5 characters long) discard=Discard discard-tooltip=Discard task/changes delete=Delete From eeb9f31b57bfae9fe7b5ff2641a26ba3a0c30055 Mon Sep 17 00:00:00 2001 From: bbassie Date: Wed, 14 Apr 2021 00:24:52 +0200 Subject: [PATCH 12/26] Basic task with title and body implementation - Parsing of the message is a bit low effort and will need to be improved. --- modules/control/tasks.lua | 19 +++++---- modules/gui/task-list.lua | 85 ++++++++++++++++++++++----------------- 2 files changed, 61 insertions(+), 43 deletions(-) diff --git a/modules/control/tasks.lua b/modules/control/tasks.lua index 0b319709..0e143725 100644 --- a/modules/control/tasks.lua +++ b/modules/control/tasks.lua @@ -32,17 +32,19 @@ end) --[[-- Add a new task for a force, the task can be placed into a certain position for that force @tparam string force_name the name of the force to add the task for @tparam[opt] string player_name the player who added this task, will cause them to be listed under editing -@tparam[opt] string task_message the message that is used for this task, if not given default is used +@tparam[opt] string task_title the message title that is used for this task, if not given default is used +@tparam[opt] string task_body the message body that is used for this task, if not given default is used @treturn string the uid of the task which was created @usage-- Adding a new task for your force local task_id = Tasks.add_task(game.player.force.name, nil, game.player.name) ]] -function Tasks.add_task(force_name, player_name, task_message) +function Tasks.add_task(force_name, player_name, message_title, message_body) -- Get a new task id local task_id = tostring(force_tasks._uid) - task_message = task_message or 'New Task' + message_title = message_title or 'New Task' + message_body = message_body or 'Do x or y' force_tasks._uid = force_tasks._uid + 1 -- Get the existing tasks for this force @@ -65,7 +67,8 @@ function Tasks.add_task(force_name, player_name, task_message) TaskData:set(task_id, { task_id = task_id, force_name = force_name, - message = task_message, + title = message_title, + body = message_body, last_edit_name = player_name or '', last_edit_time = game.tick, currently_editing = editing @@ -90,18 +93,20 @@ end --[[-- Update the message and last edited information for a task @tparam string task_id the uid of the task that you want to update -@tparam string new_message the message that you want to have for the task +@tparam string message_title the message title that you want to have for the task +@tparam string message_body the message body that you want to have for the task @tparam[opt='server'] string player_name the name of the player who made the edit @usage-- Updating the message for on a task Task.update_task(task_id, 'We need more iron!', game.player.name) ]] -function Tasks.update_task(task_id, new_message, player_name) +function Tasks.update_task(task_id, message_title, message_body, player_name) TaskData:update(task_id, function(_, task) task.last_edit_name = player_name or '' task.last_edit_time = game.tick - task.message = new_message + task.title = message_title + task.body = message_body end) end diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 656d5500..ecfb7852 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -190,7 +190,7 @@ local task_list_item = name = event_trigger, type = "button", style = "list_box_item", - caption = task.message + caption = task.title } button.style.horizontally_stretchable = true button.style.horizontally_squashable = true @@ -289,14 +289,23 @@ local task_view_footer = local footer = subfooter_frame(parent, "view") subfooter_label(footer, {"task-list.view-footer-header"}) - local label = + local title_label = footer.add { type = "label", - name = "message", + name = "title", caption = "New task" } - label.style.padding = 4 - label.style.single_line = false + title_label.style.padding = 4 + title_label.style.font = "default-bold" + title_label.style.single_line = false + local body_label = + footer.add { + type = "label", + name = "body", + caption = "Do x or y" + } + body_label.style.padding = 4 + body_label.style.single_line = false local action_flow = subfooter_actions(footer) view_task_delete_button(action_flow) @@ -306,6 +315,29 @@ local task_view_footer = end ) +local message_pattern = "(.-)\n(.*)" + +--- Parce a string into a message object with title and body +-- @tparam string str message data +local function parse_message(str) + -- Trimm the spaces of the string + local trimmed = (string.gsub(str, "^%s*(.-)%s*$", "%1")) + local message = { title = "", body = "" } + -- If it doesn't match the patter return the str as a title + local match = string.match(trimmed, message_pattern) + if not match then + message.title = trimmed + return message + end + -- If message has multiple lines + for key, value in string.gmatch(trimmed, message_pattern) do + message.title = key + message.body = value + break + end + return message +end + -- Button variable initialisation because it is used inside the textfield element events local edit_task_confirm_button local create_task_confirm_button @@ -314,11 +346,12 @@ local create_task_confirm_button -- @element task_message_textfield local task_message_textfield = Gui.element { - type = "textfield", + type = "text-box", text = "" }:style( { maximal_width = 268, + minimal_height = 100, horizontally_stretchable = true } ):on_text_changed( @@ -334,29 +367,6 @@ local task_message_textfield = element.parent.actions[edit_task_confirm_button.name].enabled = valid end end -):on_confirmed( - function(player, element, _) - local isEditing = PlayerIsEditing:get(player) - local isCreating = PlayerIsCreating:get(player) - - local message = element.text - local valid = string.len(element.text) > 5 - if not valid then - return - end - - if isCreating then - PlayerIsCreating:set(player, false) - - local task_id = Tasks.add_task(player.force.name, player.name, message) - PlayerSelected:set(player, task_id) - elseif isEditing then - local selected = PlayerSelected:get(player) - PlayerIsEditing:set(player, false) - Tasks.update_task(selected, message, player.name) - Tasks.set_editing(selected, player.name, nil) - end - end ) --- Button to confirm the changes @@ -372,7 +382,8 @@ edit_task_confirm_button = local selected = PlayerSelected:get(player) PlayerIsEditing:set(player, false) local new_message = element.parent.parent[task_message_textfield.name].text - Tasks.update_task(selected, new_message, player.name) + local parsed = parse_message(new_message) + Tasks.update_task(selected, parsed.title, parsed.body, player.name) Tasks.set_editing(selected, player.name, nil) end ) @@ -425,8 +436,8 @@ create_task_confirm_button = function(player, element, _) local message = element.parent.parent[task_message_textfield.name].text PlayerIsCreating:set(player, false) - - local task_id = Tasks.add_task(player.force.name, player.name, message) + local parsed = parse_message(message) + local task_id = Tasks.add_task(player.force.name, player.name, parsed.title, parsed.body) PlayerSelected:set(player, task_id) end ) @@ -535,7 +546,7 @@ local update_task = function(player, task_list_element, task_id) else -- If the task exists update the caption element = task_list_element["task-" .. task_id] - element[task_list_item.name].caption = task.message + element[task_list_item.name].caption = task.title end -- Set tooltip local last_edit_name = task.last_edit_name @@ -552,7 +563,7 @@ local update_task_edit_footer = function(player, task_id) local message_element = edit_flow[task_message_textfield.name] message_element.focus() - message_element.text = task.message + message_element.text = task.title .. "\n" .. task.body end -- Update the footer task view @@ -562,13 +573,15 @@ local update_task_view_footer = function(player, task_id) local view_flow = frame.container.view local has_permission = check_player_permissions(player, task) - local message_element = view_flow.message + local title_element = view_flow.title + local body_element = view_flow.body local edit_button_element = view_flow.actions[view_task_edit_button.name] local delete_button_element = view_flow.actions[view_task_delete_button.name] edit_button_element.visible = has_permission delete_button_element.visible = has_permission - message_element.caption = task.message + title_element.caption = task.title + body_element.caption = task.body local players_editing = table.get_keys(task.currently_editing) if #players_editing > 0 then From b08e93e2455d6b1dcdfaf6883ab34e254d481a57 Mon Sep 17 00:00:00 2001 From: bbassie Date: Fri, 16 Apr 2021 22:45:58 +0200 Subject: [PATCH 13/26] Fix parse_message --- modules/gui/task-list.lua | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index ecfb7852..d8173d36 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -323,17 +323,13 @@ local function parse_message(str) -- Trimm the spaces of the string local trimmed = (string.gsub(str, "^%s*(.-)%s*$", "%1")) local message = { title = "", body = "" } - -- If it doesn't match the patter return the str as a title - local match = string.match(trimmed, message_pattern) - if not match then + local title, body = string.match(trimmed, message_pattern) + if not title then + -- If it doesn't match the patter return the str as a title message.title = trimmed - return message - end - -- If message has multiple lines - for key, value in string.gmatch(trimmed, message_pattern) do - message.title = key - message.body = value - break + else + message.title = title + message.body = body end return message end From 8674704f15179819f0862aec38fed631c8dd0797 Mon Sep 17 00:00:00 2001 From: bbassie Date: Fri, 16 Apr 2021 22:50:16 +0200 Subject: [PATCH 14/26] Fix docs gen param argument name mismatch param and formal argument name mismatch: 'task_title' 'message_title' param and formal argument name mismatch: 'task_body' 'message_body' --- modules/control/tasks.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/control/tasks.lua b/modules/control/tasks.lua index 0e143725..1420a6b5 100644 --- a/modules/control/tasks.lua +++ b/modules/control/tasks.lua @@ -40,11 +40,11 @@ end) local task_id = Tasks.add_task(game.player.force.name, nil, game.player.name) ]] -function Tasks.add_task(force_name, player_name, message_title, message_body) +function Tasks.add_task(force_name, player_name, task_title, task_body) -- Get a new task id local task_id = tostring(force_tasks._uid) - message_title = message_title or 'New Task' - message_body = message_body or 'Do x or y' + task_title = task_title or 'New Task' + task_body = task_body or 'Do x or y' force_tasks._uid = force_tasks._uid + 1 -- Get the existing tasks for this force @@ -67,8 +67,8 @@ function Tasks.add_task(force_name, player_name, message_title, message_body) TaskData:set(task_id, { task_id = task_id, force_name = force_name, - title = message_title, - body = message_body, + title = task_title, + body = task_body, last_edit_name = player_name or '', last_edit_time = game.tick, currently_editing = editing From ea2447717514a1f874799cab550e7db31e22b8be Mon Sep 17 00:00:00 2001 From: bbassie Date: Sun, 25 Apr 2021 22:26:22 +0200 Subject: [PATCH 15/26] Fixed some of the requested changes * Made add_task and update_task have consistent arguments * Removed default task title and body * Task list resized to fit to 10 items --- modules/control/tasks.lua | 20 +++++++++----------- modules/gui/task-list.lua | 12 +++++------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/modules/control/tasks.lua b/modules/control/tasks.lua index 1420a6b5..caacaeea 100644 --- a/modules/control/tasks.lua +++ b/modules/control/tasks.lua @@ -43,8 +43,6 @@ local task_id = Tasks.add_task(game.player.force.name, nil, game.player.name) function Tasks.add_task(force_name, player_name, task_title, task_body) -- Get a new task id local task_id = tostring(force_tasks._uid) - task_title = task_title or 'New Task' - task_body = task_body or 'Do x or y' force_tasks._uid = force_tasks._uid + 1 -- Get the existing tasks for this force @@ -67,8 +65,8 @@ function Tasks.add_task(force_name, player_name, task_title, task_body) TaskData:set(task_id, { task_id = task_id, force_name = force_name, - title = task_title, - body = task_body, + title = task_title or '', + body = task_body or '', last_edit_name = player_name or '', last_edit_time = game.tick, currently_editing = editing @@ -93,20 +91,20 @@ end --[[-- Update the message and last edited information for a task @tparam string task_id the uid of the task that you want to update -@tparam string message_title the message title that you want to have for the task -@tparam string message_body the message body that you want to have for the task -@tparam[opt='server'] string player_name the name of the player who made the edit +@tparam string player_name the name of the player who made the edit +@tparam string task_title the message title that you want to have for the task +@tparam string task_body the message body that you want to have for the task @usage-- Updating the message for on a task Task.update_task(task_id, 'We need more iron!', game.player.name) ]] -function Tasks.update_task(task_id, message_title, message_body, player_name) +function Tasks.update_task(task_id, player_name, task_title, task_body) TaskData:update(task_id, function(_, task) - task.last_edit_name = player_name or '' + task.last_edit_name = player_name task.last_edit_time = game.tick - task.title = message_title - task.body = message_body + task.title = task_title + task.body = task_body end) end diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index d8173d36..8b8a347f 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -219,7 +219,7 @@ local task_list = } scroll_pane.style.horizontally_stretchable = true scroll_pane.style.padding = 0 - scroll_pane.style.maximal_height = 300 + scroll_pane.style.maximal_height = 280 local flow = scroll_pane.add { @@ -292,8 +292,7 @@ local task_view_footer = local title_label = footer.add { type = "label", - name = "title", - caption = "New task" + name = "title" } title_label.style.padding = 4 title_label.style.font = "default-bold" @@ -301,8 +300,7 @@ local task_view_footer = local body_label = footer.add { type = "label", - name = "body", - caption = "Do x or y" + name = "body" } body_label.style.padding = 4 body_label.style.single_line = false @@ -321,7 +319,7 @@ local message_pattern = "(.-)\n(.*)" -- @tparam string str message data local function parse_message(str) -- Trimm the spaces of the string - local trimmed = (string.gsub(str, "^%s*(.-)%s*$", "%1")) + local trimmed = string.gsub(str, "^%s*(.-)%s*$", "%1") local message = { title = "", body = "" } local title, body = string.match(trimmed, message_pattern) if not title then @@ -379,7 +377,7 @@ edit_task_confirm_button = PlayerIsEditing:set(player, false) local new_message = element.parent.parent[task_message_textfield.name].text local parsed = parse_message(new_message) - Tasks.update_task(selected, parsed.title, parsed.body, player.name) + Tasks.update_task(selected, player.name, parsed.title, parsed.body) Tasks.set_editing(selected, player.name, nil) end ) From 9e6d4e74544f732945877feb2a2d838ca8d9a53d Mon Sep 17 00:00:00 2001 From: bbassie Date: Mon, 26 Apr 2021 02:31:23 +0200 Subject: [PATCH 16/26] Move close button to the side of the header --- locale/en/gui.cfg | 1 - modules/gui/task-list.lua | 20 +++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/locale/en/gui.cfg b/locale/en/gui.cfg index 260f1c2e..585dadb1 100644 --- a/locale/en/gui.cfg +++ b/locale/en/gui.cfg @@ -82,7 +82,6 @@ discard=Discard discard-tooltip=Discard task/changes delete=Delete delete-tooltip=Delete task -close=Close close-tooltip=Close task details edit=Edit task edit-tooltip=Currently being edited by: __1__ diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 8b8a347f..0ed3a28d 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -254,12 +254,13 @@ local view_task_edit_button = --- Button to close the view task footer -- @element view_task_close_button local view_task_close_button = - Gui.element { - type = "button", - caption = {"", "[img=utility/close_black] ", {"task-list.close"}}, - tooltip = {"task-list.close-tooltip"}, - style = "shortcut_bar_button" -}:style(Styles.footer_button):on_click( +Gui.element{ + type = "sprite-button", + sprite = "utility/collapse_dark", + hovered_sprite = "utility/collapse", + tooltip = {"task-list.close-tooltip"} +} +:style(Styles.sprite22):on_click( function(player, _, _) PlayerSelected:set(player, nil) end @@ -287,8 +288,10 @@ local task_view_footer = Gui.element( function(_, parent) local footer = subfooter_frame(parent, "view") - subfooter_label(footer, {"task-list.view-footer-header"}) - + local vertical_flow = footer.add{ type = "flow" } + subfooter_label(vertical_flow, {"task-list.view-footer-header"}) + local alignment = Gui.alignment(vertical_flow) + view_task_close_button(alignment) local title_label = footer.add { type = "label", @@ -308,7 +311,6 @@ local task_view_footer = local action_flow = subfooter_actions(footer) view_task_delete_button(action_flow) view_task_edit_button(action_flow) - view_task_close_button(action_flow) return footer end ) From 77e0aff451d30fdecdd98dee10510e533572e060 Mon Sep 17 00:00:00 2001 From: bbassie Date: Mon, 26 Apr 2021 10:57:19 +0200 Subject: [PATCH 17/26] Variable name fix --- modules/gui/task-list.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 0ed3a28d..aa8e84be 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -288,9 +288,9 @@ local task_view_footer = Gui.element( function(_, parent) local footer = subfooter_frame(parent, "view") - local vertical_flow = footer.add{ type = "flow" } - subfooter_label(vertical_flow, {"task-list.view-footer-header"}) - local alignment = Gui.alignment(vertical_flow) + local flow = footer.add{ type = "flow" } + subfooter_label(flow, {"task-list.view-footer-header"}) + local alignment = Gui.alignment(flow) view_task_close_button(alignment) local title_label = footer.add { From 0f5868378a931c20deffccafcf1b4fa34abbd3ce Mon Sep 17 00:00:00 2001 From: Bastiaan Date: Tue, 27 Apr 2021 23:16:18 +0200 Subject: [PATCH 18/26] Fix documentation modules/control/tasks.lua Co-authored-by: mark9064 <30447455+mark9064@users.noreply.github.com> --- modules/control/tasks.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/control/tasks.lua b/modules/control/tasks.lua index caacaeea..00f331b4 100644 --- a/modules/control/tasks.lua +++ b/modules/control/tasks.lua @@ -32,7 +32,7 @@ end) --[[-- Add a new task for a force, the task can be placed into a certain position for that force @tparam string force_name the name of the force to add the task for @tparam[opt] string player_name the player who added this task, will cause them to be listed under editing -@tparam[opt] string task_title the message title that is used for this task, if not given default is used +@tparam[opt] string task_title the message title that is used for the task, if not given the default is used @tparam[opt] string task_body the message body that is used for this task, if not given default is used @treturn string the uid of the task which was created @@ -179,4 +179,4 @@ function Tasks.get_editing(task_id, player_name) end -- Module Return -return Tasks \ No newline at end of file +return Tasks From 254d69f646bbf65a3a0e03820578312a1c7240b2 Mon Sep 17 00:00:00 2001 From: Bastiaan Date: Tue, 27 Apr 2021 23:29:28 +0200 Subject: [PATCH 19/26] Fix documentation modules/gui/task-list.lua Co-authored-by: mark9064 <30447455+mark9064@users.noreply.github.com> --- modules/gui/task-list.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index aa8e84be..f1031754 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -317,7 +317,7 @@ local task_view_footer = local message_pattern = "(.-)\n(.*)" ---- Parce a string into a message object with title and body +--- Parse a string into a message object with title and body -- @tparam string str message data local function parse_message(str) -- Trimm the spaces of the string From ea8e093c318780540beef14a101a6632e98ff401 Mon Sep 17 00:00:00 2001 From: Bastiaan Date: Tue, 27 Apr 2021 23:33:05 +0200 Subject: [PATCH 20/26] Apply suggestions from code review Co-authored-by: mark9064 <30447455+mark9064@users.noreply.github.com> --- modules/gui/task-list.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index f1031754..d8c9b260 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -320,12 +320,12 @@ local message_pattern = "(.-)\n(.*)" --- Parse a string into a message object with title and body -- @tparam string str message data local function parse_message(str) - -- Trimm the spaces of the string + -- Trim the spaces of the string local trimmed = string.gsub(str, "^%s*(.-)%s*$", "%1") local message = { title = "", body = "" } local title, body = string.match(trimmed, message_pattern) if not title then - -- If it doesn't match the patter return the str as a title + -- If it doesn't match the pattern return the str as a title message.title = trimmed else message.title = title @@ -501,7 +501,7 @@ local task_list_container = task_view_footer_element.visible = false task_edit_footer_element.visible = false task_create_footer_element.visible = false - -- Return the exteral container + -- Return the external container return container.parent end ):add_to_left_flow( @@ -675,7 +675,7 @@ PlayerSelected:on_update( edit_flow.visible = false end else - -- If new state nil then hide footer elements and set editing to nil for old_state + -- If new state is nil then hide footer elements and set editing to nil for old_state if old_state then Tasks.set_editing(old_state, player.name, nil) end @@ -718,7 +718,7 @@ local function role_update_event(event) PlayerSelected:set(selected) end - -- Update the new task button and create footer incase the user can now add them + -- Update the new task button and create footer in case the user can now add them local has_permission = check_player_permissions(player) local add_new_task_element = container.header.alignment[add_new_task.name] add_new_task_element.visible = has_permission From a3fe06b1f8b76e3f40872856cefe4b3fee0fcd53 Mon Sep 17 00:00:00 2001 From: bbassie Date: Wed, 28 Apr 2021 00:25:19 +0200 Subject: [PATCH 21/26] Fix documentation and some names of functions, variables and arguments --- modules/control/tasks.lua | 14 ++--- modules/gui/task-list.lua | 126 +++++++++++++++++++------------------- 2 files changed, 70 insertions(+), 70 deletions(-) diff --git a/modules/control/tasks.lua b/modules/control/tasks.lua index caacaeea..a83da65a 100644 --- a/modules/control/tasks.lua +++ b/modules/control/tasks.lua @@ -32,12 +32,12 @@ end) --[[-- Add a new task for a force, the task can be placed into a certain position for that force @tparam string force_name the name of the force to add the task for @tparam[opt] string player_name the player who added this task, will cause them to be listed under editing -@tparam[opt] string task_title the message title that is used for this task, if not given default is used -@tparam[opt] string task_body the message body that is used for this task, if not given default is used +@tparam[opt] string task_title the task title, if not given default is used +@tparam[opt] string task_body the task body, if not given default is used @treturn string the uid of the task which was created @usage-- Adding a new task for your force -local task_id = Tasks.add_task(game.player.force.name, nil, game.player.name) +local task_id = Tasks.add_task(game.player.force.name, game.player.name, nil, nil) ]] function Tasks.add_task(force_name, player_name, task_title, task_body) @@ -90,13 +90,13 @@ function Tasks.remove_task(task_id) end --[[-- Update the message and last edited information for a task -@tparam string task_id the uid of the task that you want to update +@tparam string task_id the uid of the task to update @tparam string player_name the name of the player who made the edit -@tparam string task_title the message title that you want to have for the task -@tparam string task_body the message body that you want to have for the task +@tparam string task_title the title of the task to update to +@tparam string task_body the body of the task to update to @usage-- Updating the message for on a task -Task.update_task(task_id, 'We need more iron!', game.player.name) +Task.update_task(task_id, game.player.name, 'We need more iron!', 'Build more iron outposts.') ]] function Tasks.update_task(task_id, player_name, task_title, task_body) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index aa8e84be..b4819e7e 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -92,7 +92,7 @@ local add_new_task = PlayerIsEditing:set(player, false) -- Clear selected PlayerSelected:set(player, nil) - -- Open create task footer + -- Open task create footer PlayerIsCreating:set(player, true) end ) @@ -204,7 +204,7 @@ local task_list_item = ) --- Scrollable list of all tasks --- @element no_tasks_found +-- @element task_list local task_list = Gui.element( function(_, parent) @@ -235,8 +235,8 @@ local task_list = ) --- Button element inside the task view footer to start editing a task --- @element view_task_edit_button -local view_task_edit_button = +-- @element task_view_edit_button +local task_view_edit_button = Gui.element { type = "button", caption = {"", "[img=utility/rename_icon_normal] ", {"task-list.edit"}}, @@ -251,9 +251,9 @@ local view_task_edit_button = end ) ---- Button to close the view task footer --- @element view_task_close_button -local view_task_close_button = +--- Button to close the task view footer +-- @element task_view_close_button +local task_view_close_button = Gui.element{ type = "sprite-button", sprite = "utility/collapse_dark", @@ -266,9 +266,9 @@ Gui.element{ end ) ---- Button to delete the task --- @element view_task_delete_button -local view_task_delete_button = +--- Button to delete the task inside the task view footer +-- @element task_view_delete_button +local task_view_delete_button = Gui.element { type = "button", caption = {"", "[img=utility/trash] ", {"task-list.delete"}}, @@ -282,7 +282,7 @@ local view_task_delete_button = end ) ---- Subfooter inside the tasklist container +--- Subfooter inside the tasklist container that holds all the elements for viewing a task -- @element task_view_footer local task_view_footer = Gui.element( @@ -291,7 +291,7 @@ local task_view_footer = local flow = footer.add{ type = "flow" } subfooter_label(flow, {"task-list.view-footer-header"}) local alignment = Gui.alignment(flow) - view_task_close_button(alignment) + task_view_close_button(alignment) local title_label = footer.add { type = "label", @@ -309,8 +309,8 @@ local task_view_footer = body_label.style.single_line = false local action_flow = subfooter_actions(footer) - view_task_delete_button(action_flow) - view_task_edit_button(action_flow) + task_view_delete_button(action_flow) + task_view_edit_button(action_flow) return footer end ) @@ -335,10 +335,10 @@ local function parse_message(str) end -- Button variable initialisation because it is used inside the textfield element events -local edit_task_confirm_button -local create_task_confirm_button +local task_edit_confirm_button +local task_create_confirm_button ---- Textfield element used in both the create and edit footers +--- Textfield element used in both the task create and edit footers -- @element task_message_textfield local task_message_textfield = Gui.element { @@ -358,16 +358,16 @@ local task_message_textfield = local valid = string.len(element.text) > 5 if isCreating then - element.parent.actions[create_task_confirm_button.name].enabled = valid + element.parent.actions[task_create_confirm_button.name].enabled = valid elseif isEditing then - element.parent.actions[edit_task_confirm_button.name].enabled = valid + element.parent.actions[task_edit_confirm_button.name].enabled = valid end end ) ---- Button to confirm the changes --- @element edit_task_confirm_button -edit_task_confirm_button = +--- Button to confirm the changes inside the task edit footer +-- @element task_edit_confirm_button +task_edit_confirm_button = Gui.element { type = "button", caption = {"", "[img=utility/check_mark] ", {"task-list.confirm"}}, @@ -384,7 +384,7 @@ edit_task_confirm_button = end ) ---- Button to discard the changes +--- Button to discard the changes inside the task edit footer -- @element edit_task_discard_button local edit_task_discard_button = Gui.element { @@ -400,7 +400,7 @@ local edit_task_discard_button = end ) ---- Subfooter inside the tasklist container +--- Subfooter inside the tasklist container that holds all the elements for editing a task -- @element task_edit_footer local task_edit_footer = Gui.element( @@ -413,15 +413,15 @@ local task_edit_footer = local action_flow = subfooter_actions(footer) edit_task_discard_button(action_flow) - edit_task_confirm_button(action_flow) + task_edit_confirm_button(action_flow) return footer end ) ---- Button to confirm the changes --- @element create_task_confirm_button -create_task_confirm_button = +--- Button to confirm the changes inside the task create footer +-- @element task_create_confirm_button +task_create_confirm_button = Gui.element { type = "button", caption = {"", "[img=utility/check_mark] ", {"task-list.confirm"}}, @@ -438,9 +438,9 @@ create_task_confirm_button = end ) ---- Button to discard the changes --- @element create_task_discard_button -local create_task_discard_button = +--- Button to discard the changes inside the task create footer +-- @element task_create_discard_button +local task_create_discard_button = Gui.element { type = "button", caption = {"", "[img=utility/close_black] ", {"task-list.discard"}}, @@ -452,7 +452,7 @@ local create_task_discard_button = end ) ---- Subfooter inside the tasklist container +--- Subfooter inside the tasklist container that holds all the elements to create a new task -- @element task_create_footer local task_create_footer = Gui.element( @@ -464,8 +464,8 @@ local task_create_footer = local action_flow = subfooter_actions(footer) - create_task_discard_button(action_flow) - create_task_confirm_button(action_flow) + task_create_discard_button(action_flow) + task_create_confirm_button(action_flow) return footer end @@ -571,8 +571,8 @@ local update_task_view_footer = function(player, task_id) local title_element = view_flow.title local body_element = view_flow.body - local edit_button_element = view_flow.actions[view_task_edit_button.name] - local delete_button_element = view_flow.actions[view_task_delete_button.name] + local edit_button_element = view_flow.actions[task_view_edit_button.name] + local delete_button_element = view_flow.actions[task_view_delete_button.name] edit_button_element.visible = has_permission delete_button_element.visible = has_permission @@ -581,28 +581,27 @@ local update_task_view_footer = function(player, task_id) local players_editing = table.get_keys(task.currently_editing) if #players_editing > 0 then - -- edit_task_element.hovered_sprite = 'utility/warning_icon' edit_button_element.tooltip = {"task-list.edit-tooltip", table.concat(players_editing, ", ")} else - -- edit_task_element.hovered_sprite = edit_task_element.sprite edit_button_element.tooltip = {"task-list.edit-tooltip-none"} end end --- When a new task is added it will update the task list for everyone on that force +--- Or when a task is updated it will update the specific task elements Tasks.on_update( - function(task_id, task, old_task) + function(task_id, curr_state, prev_state) -- Get the force to update, task is nil when removed local force - if task then - force = game.forces[task.force_name] + if curr_state then + force = game.forces[curr_state.force_name] else - force = game.forces[old_task.force_name] + force = game.forces[prev_state.force_name] end -- Update the task for all the players on the force for _, player in pairs(force.connected_players) do - -- Update the task view if the player has the changed task selected + -- Update the task view elements if the player currently being looped over has this specific task selected local selected = PlayerSelected:get(player) if selected == task_id then update_task_view_footer(player, selected) @@ -619,7 +618,7 @@ Tasks.on_update( -- When a player is creating a new task. PlayerIsCreating:on_update( - function(player_name, new_state, _) + function(player_name, curr_state, _) local player = game.players[player_name] local frame = Gui.get_left_element(player, task_list_container) @@ -627,12 +626,12 @@ PlayerIsCreating:on_update( -- Clear the textfield local message_element = frame.container.create[task_message_textfield.name] - local confirm_button_element = frame.container.create.actions[create_task_confirm_button.name] + local confirm_button_element = frame.container.create.actions[task_create_confirm_button.name] message_element.focus() message_element.text = "" confirm_button_element.enabled = false - if new_state then + if curr_state then create.visible = true else create.visible = false @@ -642,7 +641,7 @@ PlayerIsCreating:on_update( -- When a player selects a different warp from the list PlayerSelected:on_update( - function(player_name, new_state, old_state) + function(player_name, curr_state, prev_state) local player = game.players[player_name] local frame = Gui.get_left_element(player, task_list_container) @@ -651,23 +650,23 @@ PlayerSelected:on_update( local edit_flow = frame.container.edit local isEditing = PlayerIsEditing:get(player) - -- If the selection has a old state re-enable the button list element - if old_state then - task_list_element["task-" .. old_state][task_list_item.name].enabled = true + -- If the selection has an previous state re-enable the button list element + if prev_state then + task_list_element["task-" .. prev_state][task_list_item.name].enabled = true end - if new_state then + if curr_state then -- Disable the selected element - task_list_element["task-" .. new_state][task_list_item.name].enabled = false + task_list_element["task-" .. curr_state][task_list_item.name].enabled = false -- Update the view footer - update_task_view_footer(player, new_state) + update_task_view_footer(player, curr_state) - -- Depending on if the player is currently editing change the current edit session to the new task + -- Depending on if the player is currently editing change the current task edit footer to the current task if isEditing then - update_task_edit_footer(player, new_state) - Tasks.set_editing(old_state, player.name, nil) - Tasks.set_editing(new_state, player.name, true) + update_task_edit_footer(player, curr_state) + Tasks.set_editing(prev_state, player.name, nil) + Tasks.set_editing(curr_state, player.name, true) view_flow.visible = false edit_flow.visible = true else @@ -675,9 +674,9 @@ PlayerSelected:on_update( edit_flow.visible = false end else - -- If new state nil then hide footer elements and set editing to nil for old_state - if old_state then - Tasks.set_editing(old_state, player.name, nil) + -- If curr_state nil then hide footer elements and set editing to nil for prev_state + if prev_state then + Tasks.set_editing(prev_state, player.name, nil) end view_flow.visible = false edit_flow.visible = false @@ -687,7 +686,7 @@ PlayerSelected:on_update( -- When the edit view opens or closes PlayerIsEditing:on_update( - function(player_name, new_state, _) + function(player_name, curr_state, _) local player = game.players[player_name] local frame = Gui.get_left_element(player, task_list_container) @@ -695,7 +694,7 @@ PlayerIsEditing:on_update( local edit_flow = frame.container.edit local selected = PlayerSelected:get(player) - if new_state then + if curr_state then update_task_edit_footer(player, selected) view_flow.visible = false edit_flow.visible = true @@ -714,7 +713,8 @@ local function role_update_event(event) local selected = PlayerSelected:get(player) if selected then update_task_view_footer(player, selected) - -- Set selected to the same value, this will make sure if player was in a editing state he will leave it if no permissions. + -- Resetting the players selected task to make sure the player does not see an + -- button to edit the task. PlayerSelected:set(selected) end From 909f4ce42696d0676eccae9c05108a81ef5d2302 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 29 Apr 2021 21:39:09 +0100 Subject: [PATCH 22/26] Removed player editing during add_task --- modules/control/tasks.lua | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/modules/control/tasks.lua b/modules/control/tasks.lua index 8f25de8a..04d61b4d 100644 --- a/modules/control/tasks.lua +++ b/modules/control/tasks.lua @@ -55,12 +55,6 @@ function Tasks.add_task(force_name, player_name, task_title, task_body) -- Insert the task id into the forces tasks table.insert(task_ids, task_id) - -- Create the editing table - local editing = {} - if player_name then - editing[player_name] = true - end - -- Add the new task to the store TaskData:set(task_id, { task_id = task_id, @@ -69,7 +63,7 @@ function Tasks.add_task(force_name, player_name, task_title, task_body) body = task_body or '', last_edit_name = player_name or '', last_edit_time = game.tick, - currently_editing = editing + currently_editing = {} }) return task_id From e1e20162cf4ab33e337941abd7657fe31b5d4875 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 29 Apr 2021 22:03:51 +0100 Subject: [PATCH 23/26] Fixed tasks not updating while offline --- modules/gui/task-list.lua | 50 +++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 76afe54e..f646bceb 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -471,6 +471,27 @@ local task_create_footer = end ) +--- Clear and repopulate the task list with all current tasks +local repopulate_task_list = function(task_list_element) + local force = Gui.get_player_from_element(task_list_element).force + local task_ids = Tasks.get_force_task_ids(force.name) + task_list_element.clear() + + -- Set visibility of the no_tasks_found element depending on the amount of tasks still in the task manager + task_list_element.parent.parent.no_tasks_found_element.visible = #task_ids == 0 + + -- Add each task to the flow + for _, task_id in ipairs(task_ids) do + -- Add the task + local task = Tasks.get_task(task_id) + local element = task_list_item(task_list_element, task) + -- Set tooltip + local last_edit_name = task.last_edit_name + local last_edit_time = task.last_edit_time + element[task_list_item.name].tooltip = {"task-list.last-edit", last_edit_name, format_time(last_edit_time)} + end +end + --- Main task list container for the left flow -- @element task_list_container local task_list_container = @@ -493,7 +514,8 @@ local task_list_container = no_tasks_found(container) -- Draw task list element - task_list(container) + local task_list_element = task_list(container) + repopulate_task_list(task_list_element) local task_view_footer_element = task_view_footer(container) local task_edit_footer_element = task_edit_footer(container) @@ -713,9 +735,9 @@ local function role_update_event(event) local selected = PlayerSelected:get(player) if selected then update_task_view_footer(player, selected) - -- Resetting the players selected task to make sure the player does not see an + PlayerSelected:set(player, selected) -- button to edit the task. - PlayerSelected:set(selected) + -- Resetting the players selected task to make sure the player does not see an end -- Update the new task button and create footer in case the user can now add them @@ -724,9 +746,29 @@ local function role_update_event(event) add_new_task_element.visible = has_permission local isCreating = PlayerIsCreating:get(player) if isCreating and not has_permission then - PlayerIsCreating:set(false) + PlayerIsCreating:set(player, false) end end Event.add(Roles.events.on_role_assigned, role_update_event) Event.add(Roles.events.on_role_unassigned, role_update_event) + +--- Redraw all tasks and clear editing/creating after joining or changing force +local function reset_task_list(event) + -- Repopulate the task list + local player = game.players[event.player_index] + local frame = Gui.get_left_element(player, task_list_container) + local task_list_element = frame.container.scroll.task_list + repopulate_task_list(task_list_element) + + -- Check if the selected task is still valid + local selected = PlayerSelected:get(player) + if selected and Tasks.get_task(selected) ~= nil then + PlayerIsCreating:set(player, false) + PlayerIsEditing:set(player, false) + PlayerSelected:set(player, nil) + end +end + +Event.add(defines.events.on_player_joined_game, reset_task_list) +Event.add(defines.events.on_player_changed_force, reset_task_list) \ No newline at end of file From 531d5d807ff0fb2917f645f6d38f18770e1fe9a0 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 29 Apr 2021 22:36:59 +0100 Subject: [PATCH 24/26] Fixed task removal --- modules/gui/task-list.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index f646bceb..13227c10 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -626,7 +626,11 @@ Tasks.on_update( -- Update the task view elements if the player currently being looped over has this specific task selected local selected = PlayerSelected:get(player) if selected == task_id then - update_task_view_footer(player, selected) + if task then + update_task_view_footer(player, selected) + else + PlayerSelected:set(player, nil) + end end local frame = Gui.get_left_element(player, task_list_container) @@ -697,7 +701,7 @@ PlayerSelected:on_update( end else -- If curr_state nil then hide footer elements and set editing to nil for prev_state - if prev_state then + if prev_state and Tasks.get_task(prev_state) then Tasks.set_editing(prev_state, player.name, nil) end view_flow.visible = false From bb0bf13728fe584ef479ec961a7cb3ddfd28bcb5 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 29 Apr 2021 22:44:45 +0100 Subject: [PATCH 25/26] Fixed opening view and create at same time --- modules/gui/task-list.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index 13227c10..a95fc414 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -675,6 +675,7 @@ PlayerSelected:on_update( local view_flow = frame.container.view local edit_flow = frame.container.edit local isEditing = PlayerIsEditing:get(player) + local isCreating = PlayerIsCreating:get(player) -- If the selection has an previous state re-enable the button list element if prev_state then @@ -688,6 +689,11 @@ PlayerSelected:on_update( -- Update the view footer update_task_view_footer(player, curr_state) + -- If a player is creating then remove the creation dialogue + if isCreating then + PlayerIsCreating:set(player, false) + end + -- Depending on if the player is currently editing change the current task edit footer to the current task if isEditing then update_task_edit_footer(player, curr_state) From 27b4f8b8535135e24cc63758fad4cecd5c7a5ef8 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 29 Apr 2021 23:03:14 +0100 Subject: [PATCH 26/26] Fixed linting errors --- modules/gui/task-list.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/gui/task-list.lua b/modules/gui/task-list.lua index a95fc414..4b5c8ed4 100644 --- a/modules/gui/task-list.lua +++ b/modules/gui/task-list.lua @@ -626,7 +626,7 @@ Tasks.on_update( -- Update the task view elements if the player currently being looped over has this specific task selected local selected = PlayerSelected:get(player) if selected == task_id then - if task then + if curr_state then update_task_view_footer(player, selected) else PlayerSelected:set(player, nil)