From de8315bf726581ba8d5f1965817535ad016f10ae Mon Sep 17 00:00:00 2001 From: grilledham Date: Sat, 9 Jun 2018 11:43:37 +0100 Subject: [PATCH 1/6] added global.lua --- utils/global.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 utils/global.lua diff --git a/utils/global.lua b/utils/global.lua new file mode 100644 index 00000000..dca9497b --- /dev/null +++ b/utils/global.lua @@ -0,0 +1,24 @@ +local Event = require 'utils.event' +local Token = require 'utils.global_token' + +Global = {} + +local data = {} + +function Global.register(tbl, callback) + local token = Token.register_global(tbl) + table.insert(data, {tbl = tbl, callback = callback, token = token}) +end + +Event.on_load( + function() + for _, d in ipairs(data) do + local tbl = Token.get_global(d.token) + d.callback(tbl) + end + + data = nil + end +) + +return Global From 62e8e2f58e78f620437bed51218f258bf13ca48e Mon Sep 17 00:00:00 2001 From: grilledham Date: Thu, 14 Jun 2018 13:32:00 +0100 Subject: [PATCH 2/6] tag_group refactor --- config.lua | 2 +- control.lua | 5 +- resources/band_roles.lua | 201 -------------------------------------- resources/tag_groups.lua | 53 ++++++++++ band.lua => tag_group.lua | 196 ++++++++++++++++++++++++------------- 5 files changed, 182 insertions(+), 275 deletions(-) delete mode 100644 resources/band_roles.lua create mode 100644 resources/tag_groups.lua rename band.lua => tag_group.lua (74%) diff --git a/config.lua b/config.lua index 52935178..a4429ba9 100644 --- a/config.lua +++ b/config.lua @@ -1,4 +1,4 @@ -_DEBUG = false +_DEBUG = true global.scenario = {} global.spys = {"valansch", "air20"} diff --git a/control.lua b/control.lua index 0e1dadc5..2d857231 100644 --- a/control.lua +++ b/control.lua @@ -19,7 +19,7 @@ require 'bot' require 'info' require 'player_list' require 'poll' -require 'band' +require 'tag_group' require 'tasklist' require 'blueprint_helper' require 'paint' @@ -137,7 +137,6 @@ end Event.add(defines.events.on_player_created, player_joined) Event.add(defines.events.on_console_chat, hodor) - Event.add( defines.events.on_player_joined_game, function(event) @@ -154,6 +153,6 @@ Event.add( local gui = player.gui gui.top.style = 'slot_table_spacing_horizontal_flow' - gui.left.style = 'slot_table_spacing_vertical_flow' + gui.left.style = 'slot_table_spacing_vertical_flow' end ) diff --git a/resources/band_roles.lua b/resources/band_roles.lua deleted file mode 100644 index 198858e8..00000000 --- a/resources/band_roles.lua +++ /dev/null @@ -1,201 +0,0 @@ --- A part of band.lua --- Feel free to edit. - -return { - ['Trooper'] = { - paths = {'item/tank'}, - tooltips = { - 'Incoming!!!', - "If the facts don't fit the theory, change the facts.", - 'I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail.', - "There's a fine line between genius and insanity. I have erased this line." - }, - verbs = { - 'strengthened' - } - }, - ['Mining'] = { - paths = { - 'item/iron-axe', - 'item/steel-axe', - 'item/iron-ore', - 'item/copper-ore' - }, - tooltips = { - 'Mine or not to mine', - 'The nation behaves well if it treats the natural resources as assets which\n it must turn over to the next generation increased, and not impaired, in value.' - }, - verbs = { - 'enriched' - -- "smelted" - } - }, - ['Smelting'] = { - paths = { - 'item/stone-furnace', - 'item/steel-furnace' - }, - tooltips = { - 'Mirrors are ice which do not melt: what melts are those who admire themselves in them.', - "It's as certain that as long as fossil fuels are the cheapest energy, we will just keep burning them." - }, - verbs = { - 'fused' - } - }, - ['Production'] = { - paths = { - 'item/assembling-machine-1', - 'item/assembling-machine-2', - 'item/assembling-machine-3' - }, - tooltips = { - "When every physical and mental resources is focused, one's power to solve a problem multiplies tremendously.", - 'The production of too many useful things results in too many useless people. ', - 'Everything must be made as simple as possible. But not simpler.' - }, - verbs = { - 'enhanced' - } - }, - ['Science'] = { - paths = { - 'item/science-pack-1', - 'item/science-pack-2', - 'item/science-pack-3', - 'item/military-science-pack', - 'item/production-science-pack', - 'item/high-tech-science-pack', - 'item/space-science-pack' - }, - tooltips = { - 'Science without religion is lame, religion without science is blind', - 'If we knew what it was we were doing, it would not be called research, would it?', - 'Somewhere, something incredible is waiting to be known.', - "I'm sure the universe is full of intelligent life. It's just been too intelligent to come here." - }, - verbs = { - 'advanced' - } - }, - ['Wizard'] = { - paths = { - 'item/arithmetic-combinator', - 'item/green-wire', - 'item/red-wire', - 'item/decider-combinator' - }, - tooltips = { - "Without mathematics, there's nothing you can do. Everything around you is mathematics. Everything around you is numbers.", - 'Pure mathematics is, in its way, the poetry of logical ideas.', - 'God used beautiful mathematics in creating the world.', - 'The numbers may be said to rule the whole world of quantity, and the four rules of arithmetic may be regarded as the complete equipment of the mathematician.', - 'But if the technological Singularity can happen, it will.' - --"One day it will have a mind of it´s own!." - }, - verbs = { - 'combinated', - 'equaled' - } - }, - ['Trains'] = { - paths = { - 'item/locomotive', - 'entity/curved-rail', - 'item/cargo-wagon', - 'item/fluid-wagon', - 'item/rail-signal', - 'item/rail' - }, - tooltips = { - 'Ch, ch, choooo!', - 'The only way of catching a train I have ever discovered is to miss the train before. ', - "If a trainstation is where the train stops, what's a workstation...?" - }, - verbs = { - 'expanded', - 'derailed' - } - }, - ['Oil'] = { - paths = { - 'item/pumpjack', - 'item/storage-tank', - 'item/oil-refinery', - 'item/chemical-plant', - 'fluid/crude-oil' - }, - tooltips = { - "We're running out of oil!", - 'Black gold', - "Naturally occurring, yellow-to-black liquid found in geological formations beneath the Earth's surface, which is commonly refined into various types of fuels.", - 'Components of petroleum are separated using a technique called fractional distillation.', - 'The petroleum industry generally classifies crude oil by the geographic location it is produced in (e.g. West Texas Intermediate, Brent, or Oman), its API gravity (an oil industry measure of density), and its sulfur content.' - }, - verbs = { - 'lubricated', - 'sulfured' - } - }, - ['Powah!'] = { - paths = {'item/steam-engine', 'item/nuclear-reactor', 'item/solar-panel'}, - tooltips = { - 'I ve Got The power!', - 'Power Overwhelming!!!111', - 'Its Over 9000!!!', - 'If you want to find the secrets of the universe, think in terms of energy, frequency and vibration.' - }, - verbs = { - 'amplified', - 'teslaed', - 'electrified' - } - }, - ['Spaceman'] = { - paths = {'item/rocket-silo', 'item/rocket-fuel'}, - tooltips = { - "That's one small step for a man, one giant leap for mankind.", - "The sky is the limit only for those who aren't afraid to fly!", - 'Apocalyptic explosions, dead reactors, terrorists, mass murder, death-slugs, and now a blindness plague. This is a terrible planet. We should not have come here.', - 'A still more glorious dawn awaits. Not a sunrise, but a galaxy rise. A morning filled with 400 billion suns. The rising of the milky way', - 'The Universe is under no obligation to make sense to you.' - }, - verbs = { - 'warped' - } - }, - ['Cat'] = { - paths = {'item/raw-fish'}, - tooltips = { - '=^.^=', - 'Meow', - "In a cat's eye, all things belong to cats.", - "Cats don't like change without their consent.", - 'Heard melodies are sweet, but those unheard, are sweeter' - }, - verbs = { - 'mewed', - 'purred', - 'miaowed' - } - }, - ['Dog'] = { - paths = { - 'entity/small-biter', - 'entity/medium-biter', - 'entity/big-biter', - 'entity/behemoth-biter' - }, - tooltips = { - 'Not a cat', - 'Friend', - "It's not the size of the dog in the fight, it's the size of the fight in the dog.", - 'When what you want is a relationship, and not a person, get a dog', - 'A dog has one aim in life... to bestow his heart.' - }, - verbs = { - 'woofed', - 'howled' - } - } -} diff --git a/resources/tag_groups.lua b/resources/tag_groups.lua new file mode 100644 index 00000000..527a9bf0 --- /dev/null +++ b/resources/tag_groups.lua @@ -0,0 +1,53 @@ +-- A part of band.lua +-- Feel free to edit. + +return { + ['Trooper'] = { + path = 'item/tank', + verb = 'strengthened' + }, + ['Mining'] = { + path = 'item/electric-mining-drill', + verb = 'enriched' + }, + ['Smelting'] = { + path = 'item/stone-furnace', + verb = 'fused' + }, + ['Production'] = { + path = 'item/assembling-machine-2', + verb = 'enhanced' + }, + ['Science'] = { + path = 'item/science-pack-3', + verb = 'advanced' + }, + ['Wizard'] = { + path = 'item/green-wire', + verb = 'combinated' + }, + ['Trains'] = { + path = 'item/locomotive', + verb = 'derailed' + }, + ['Oil'] = { + path = 'fluid/crude-oil', + verb = 'lubricated' + }, + ['Powah!'] = { + path = 'item/steam-engine', + verb = 'electrified' + }, + ['Spaceman'] = { + path = 'item/rocket-silo', + verb = 'warped' + }, + ['Cat'] = { + path = 'item/raw-fish', + verb = 'mewed' + }, + ['Dog'] = { + path = 'entity/medium-biter', + verb = 'woofed' + } +} diff --git a/band.lua b/tag_group.lua similarity index 74% rename from band.lua rename to tag_group.lua index 79742f22..23f77e10 100644 --- a/band.lua +++ b/tag_group.lua @@ -1,29 +1,29 @@ local Event = require 'utils.event' local Gui = require 'utils.gui' -local Token = require 'utils.global_token' +local Global = require 'utils.global' local UserGroups = require 'user_groups' -local band_roles = require 'resources.band_roles' -local band_roles_token = Token.register_global(band_roles) +local deafult_verb = 'expanded' +local tag_groups = require 'resources.tag_groups' local player_tags = {} -local player_tags_token = Token.register_global(player_tags) -Event.on_load( - function() - band_roles = Token.get_global(band_roles_token) - player_tags = Token.get_global(player_tags_token) +Global.register( + {tag_groups = tag_groups, player_tags = player_tags}, + function(data) + tag_groups = data.tag_groups + player_tags = data.player_tags end ) -local function change_player_tag(player, band_name) +local function change_player_tag(player, tag_name) local old_tag = player.tag - if band_name == '' and old_tag == '' then + if tag_name == '' and old_tag == '' then return false end - local tag_name = '[' .. band_name .. ']' - if old_tag == tag_name then + local tag = '[' .. tag_name .. ']' + if old_tag == tag then return false end @@ -34,35 +34,29 @@ local function change_player_tag(player, band_name) end end - if band_name == '' then + if tag_name == '' then player.tag = '' return true end - local band = band_roles[band_name] + local band = tag_groups[tag_name] if not band then return false end - local players = player_tags[tag_name] + local players = player_tags[tag] if not players then players = {} - player_tags[tag_name] = players + player_tags[tag] = players end players[player.index] = true - player.tag = tag_name + player.tag = tag - local verbs = band.verbs - local verb - if verbs then - verb = verbs[math.random(#verbs)] - else - verb = 'expanded' - end + local verb = band.verb or deafult_verb - game.print(tag_name .. ' squad has `' .. verb .. '` with ' .. player.name) + game.print(tag .. ' squad has `' .. verb .. '` with ' .. player.name) return true end @@ -94,19 +88,19 @@ end local main_button_name = Gui.uid_name() local main_frame_name = Gui.uid_name() +local main_frame_content_name = Gui.uid_name() local band_button_name = Gui.uid_name() local band_label_name = Gui.uid_name() local clear_button_name = Gui.uid_name() local create_tag_button_name = Gui.uid_name() -local delete_tag_button_name = Gui.uid_name() +local edit_tag_button_name = Gui.uid_name() local create_tag_frame_name = Gui.uid_name() local create_tag_choose_icon_name = Gui.uid_name() local create_tag_icon_type_name = Gui.uid_name() -local close_create_tag_name = Gui.uid_name() local confirm_create_tag_name = Gui.uid_name() - -local main_frame_content_name = Gui.uid_name() +local delete_tag_name = Gui.uid_name() +local close_create_tag_name = Gui.uid_name() local function player_joined(event) local player = game.players[event.player_index] @@ -124,13 +118,13 @@ end local function draw_main_frame_content(parent) local player = parent.gui.player - for band_name, band_data in pairs(band_roles) do - local tag_name = '[' .. band_name .. ']' - local players = player_tags[tag_name] + for tag_name, band_data in pairs(tag_groups) do + local tag = '[' .. tag_name .. ']' + local players = player_tags[tag] local size = get_size(players) - local path = band_data.paths[math.random(#band_data.paths)] - local tooltip = band_data.tooltips[math.random(#band_data.tooltips)] + local path = band_data.path + local tooltip = tag_name local row = parent.add {type = 'flow', direction = 'horizontal'} row.style.top_padding = 0 @@ -138,12 +132,12 @@ local function draw_main_frame_content(parent) if player.admin then local delete_button = - row.add {type = 'sprite-button', name = delete_tag_button_name, sprite = 'utility/remove'} - delete_button.tooltip = 'Delete tag group' + row.add {type = 'sprite-button', name = edit_tag_button_name, sprite = 'utility/rename_icon_normal'} + delete_button.tooltip = 'Edit tag group' delete_button.style.top_padding = 0 delete_button.style.bottom_padding = 0 delete_button.style.maximal_height = 32 - Gui.set_data(delete_button, band_name) + Gui.set_data(delete_button, tag_name) end local button = row.add {type = 'sprite-button', name = band_button_name, sprite = path} @@ -151,12 +145,12 @@ local function draw_main_frame_content(parent) button.style.top_padding = 0 button.style.bottom_padding = 0 button.style.maximal_height = 32 - Gui.set_data(button, band_name) + Gui.set_data(button, tag_name) - local role_label = row.add {type = 'label', name = band_label_name, caption = band_name .. size} + local role_label = row.add {type = 'label', name = band_label_name, caption = tag_name .. size} role_label.style.top_padding = 4 role_label.style.minimal_width = 120 - Gui.set_data(role_label, {band_name = band_name, path = path}) + Gui.set_data(role_label, {tag_name = tag_name, path = path}) local list = row.add {type = 'flow', direction = 'horizontal'} @@ -253,20 +247,49 @@ local choices = { 'recipe' } -local function draw_create_tag_frame(event) - local center = event.player.gui.center +local function draw_create_tag_frame(event, tag_data) + local name + local verb + local path + local spirte_type + local frame_caption + local confirm_caption + if tag_data then + name = tag_data.name + verb = tag_data.verb + path = tag_data.path - if center[create_tag_frame_name] then - return + if path and path ~= '' then + spirte_type, path = path:match('([^/]+)/([^/]+)') + else + spirte_type = choices[1] + path = nil + end + + frame_caption = 'Edit Tag' + confirm_caption = 'Edit' + else + name = '' + verb = 'expanded' + spirte_type = choices[1] + frame_caption = 'Create A New Tag' + confirm_caption = 'Create' end - local frame = - center.add {type = 'frame', name = create_tag_frame_name, caption = 'Create A New Tag', direction = 'vertical'} + local center = event.player.gui.center + + local frame = center[create_tag_frame_name] + if frame then + Gui.remove_data_recursivly(frame) + frame.destroy() + end + + frame = center.add {type = 'frame', name = create_tag_frame_name, caption = frame_caption, direction = 'vertical'} local main_table = frame.add {type = 'table', column_count = 2} main_table.add {type = 'label', caption = 'Name'} - local name_field = main_table.add {type = 'textfield'} + local name_field = main_table.add {type = 'textfield', text = name} Gui.set_data(name_field, frame) main_table.add {type = 'label', caption = 'Icon'} @@ -274,16 +297,16 @@ local function draw_create_tag_frame(event) local selection_flow = icons_flow.add {type = 'flow'} local focus - for i, value in ipairs(choices) do + for _, value in ipairs(choices) do local radio = selection_flow.add({type = 'flow'}).add { type = 'radiobutton', name = create_tag_icon_type_name, caption = value, - state = i == 1 + state = value == spirte_type } - if i == 1 then + if value == spirte_type then focus = radio end @@ -294,20 +317,29 @@ local function draw_create_tag_frame(event) icons_flow.add { type = 'choose-elem-button', name = create_tag_choose_icon_name, - elem_type = choices[1] + elem_type = spirte_type } + + if path then + choose.elem_value = path + end + Gui.set_data(choose, frame) main_table.add {type = 'label', caption = 'Verb'} - local verb_field = main_table.add {type = 'textfield', text = 'expanded'} + local verb_field = main_table.add {type = 'textfield', text = verb} Gui.set_data(verb_field, frame) - local flow = frame.add {type = 'flow', direction = 'horizontal'} + local bottom_flow = frame.add {type = 'flow', direction = 'horizontal'} - local close_button = flow.add {type = 'button', name = close_create_tag_name, caption = 'Close'} - Gui.set_data(close_button, frame) + local left_flow = bottom_flow.add{type = 'flow', direction = 'horizontal'} + left_flow.style.align = 'left' - local confirm_button = flow.add {type = 'button', name = confirm_create_tag_name, caption = 'Confirm'} + local right_flow = bottom_flow.add{type = 'flow', direction = 'horizontal'} + right_flow.style.horizontally_stretchable = true + right_flow.style.align = 'right' + + local confirm_button = left_flow.add {type = 'button', name = confirm_create_tag_name, caption = confirm_caption} Gui.set_data(confirm_button, frame) Gui.set_data( @@ -317,10 +349,18 @@ local function draw_create_tag_frame(event) choose = choose, icons_flow = icons_flow, name = name_field, - verb = verb_field + verb = verb_field, + tag_data = tag_data } ) + if tag_data then + left_flow.add {type = 'button', name = delete_tag_name, caption = 'Delete'} + end + + local close_button = right_flow.add {type = 'button', name = close_create_tag_name, caption = 'Close'} + Gui.set_data(close_button, frame) + event.player.opened = frame end @@ -353,12 +393,12 @@ Gui.on_click( end ) -Gui.on_click( - delete_tag_button_name, +--[[ Gui.on_click( + edit_tag_button_name, function(event) local tag_name = Gui.get_data(event.element) - if not band_roles[tag_name] then + if not tag_groups[tag_name] then event.player.print("Sorry, Tag name '" .. tag_name .. "' not found.") return end @@ -375,12 +415,28 @@ Gui.on_click( end end - band_roles[tag_name] = nil + tag_groups[tag_name] = nil redraw_main_frame() game.print(event.player.name .. ' has deleted the ' .. tag_name .. ' tag group') end +) ]] +Gui.on_click( + edit_tag_button_name, + function(event) + local tag_name = Gui.get_data(event.element) + + local tag_data = tag_groups[tag_name] + if not tag_data then + event.player.print("Sorry, Tag name '" .. tag_name .. "' not found.") + return + end + + tag_data.name = tag_name + + draw_create_tag_frame(event, tag_data) + end ) Gui.on_click( @@ -435,7 +491,7 @@ Gui.on_click( return end - if band_roles[name] then + if tag_groups[name] then player.print('Sorry, tag ' .. data.name .. ' is already in use.') return end @@ -445,30 +501,30 @@ Gui.on_click( local path if not sprite or sprite == '' then - path = 'utility/pump_cannot_connect_icon' + path = nil elseif type == 'signal' then path = 'virtual-signal/' .. data.choose.elem_value.name else path = type .. '/' .. data.choose.elem_value end - if not frame.gui.is_valid_sprite_path(path) then + if path and not frame.gui.is_valid_sprite_path(path) then player.print('Sorry, ' .. path .. ' is not a valid sprite') return end local verb = data.verb.text if verb == '' then - verb = 'expanded' + verb = deafult_verb end local band_role = { - paths = {path}, - verbs = {verb}, - tooltips = {name} + path = path, + verb = verb, + tooltip = name } - band_roles[name] = band_role + tag_groups[name] = band_role redraw_main_frame() @@ -530,7 +586,7 @@ local function tag_command(cmd) end local tag_name = string.sub(cmd.parameter, params[1]:len() + 2) - local tag = band_roles[tag_name] + local tag = tag_groups[tag_name] if tag == nil then player_print("Tag '" .. tag_name .. "' does not exist. Create the tag first by clicking Tag -> Create Tag.") From dbed6044c2e68067f0ad3851ee072cdb83adb97d Mon Sep 17 00:00:00 2001 From: grilledham Date: Thu, 14 Jun 2018 23:31:05 +0100 Subject: [PATCH 3/6] updates --- tag_group.lua | 289 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 194 insertions(+), 95 deletions(-) diff --git a/tag_group.lua b/tag_group.lua index 23f77e10..fa680dc1 100644 --- a/tag_group.lua +++ b/tag_group.lua @@ -7,16 +7,26 @@ local deafult_verb = 'expanded' local tag_groups = require 'resources.tag_groups' local player_tags = {} +local no_notify_players = {} Global.register( - {tag_groups = tag_groups, player_tags = player_tags}, + {tag_groups = tag_groups, player_tags = player_tags, no_notify_players = no_notify_players}, function(data) tag_groups = data.tag_groups player_tags = data.player_tags + no_notify_players = no_notify_players end ) -local function change_player_tag(player, tag_name) +local function notify_players(message) + for _, p in ipairs(game.connected_players) do + if p.valid and not no_notify_players[p.index] then + p.print(message) + end + end +end + +local function change_player_tag(player, tag_name, silent) local old_tag = player.tag if tag_name == '' and old_tag == '' then return false @@ -36,11 +46,14 @@ local function change_player_tag(player, tag_name) if tag_name == '' then player.tag = '' + if not silent then + notify_players(player.name .. ' has left the ' .. old_tag .. ' squad') + end return true end - local band = tag_groups[tag_name] - if not band then + local tag_data = tag_groups[tag_name] + if not tag_data then return false end @@ -54,10 +67,11 @@ local function change_player_tag(player, tag_name) player.tag = tag - local verb = band.verb or deafult_verb - - game.print(tag .. ' squad has `' .. verb .. '` with ' .. player.name) + local verb = tag_data.verb or deafult_verb + if not silent then + notify_players(tag .. ' squad has `' .. verb .. '` with ' .. player.name) + end return true end @@ -89,11 +103,12 @@ end local main_button_name = Gui.uid_name() local main_frame_name = Gui.uid_name() local main_frame_content_name = Gui.uid_name() -local band_button_name = Gui.uid_name() -local band_label_name = Gui.uid_name() +local tag_button_name = Gui.uid_name() +local tag_label_name = Gui.uid_name() local clear_button_name = Gui.uid_name() local create_tag_button_name = Gui.uid_name() local edit_tag_button_name = Gui.uid_name() +local notify_checkbox_name = Gui.uid_name() local create_tag_frame_name = Gui.uid_name() local create_tag_choose_icon_name = Gui.uid_name() @@ -117,40 +132,48 @@ end local function draw_main_frame_content(parent) local player = parent.gui.player + local grid = parent.add {type = 'table', column_count = 1} + grid.style.vertical_spacing = 0 - for tag_name, band_data in pairs(tag_groups) do + for tag_name, tag_data in pairs(tag_groups) do local tag = '[' .. tag_name .. ']' local players = player_tags[tag] local size = get_size(players) - local path = band_data.path - local tooltip = tag_name + local path = tag_data.path - local row = parent.add {type = 'flow', direction = 'horizontal'} - row.style.top_padding = 0 - row.style.bottom_padding = 0 + local row = grid.add {type = 'table', column_count = 4} + row.style.horizontal_spacing = 0 - if player.admin then - local delete_button = - row.add {type = 'sprite-button', name = edit_tag_button_name, sprite = 'utility/rename_icon_normal'} - delete_button.tooltip = 'Edit tag group' - delete_button.style.top_padding = 0 - delete_button.style.bottom_padding = 0 - delete_button.style.maximal_height = 32 - Gui.set_data(delete_button, tag_name) + if player.admin or UserGroups.is_regular(player.name) then + local edit_button = + row.add { + type = 'sprite-button', + name = edit_tag_button_name, + sprite = 'utility/rename_icon_normal', + tooltip = 'Edit tag group' + } + edit_button.style.top_padding = 0 + edit_button.style.bottom_padding = 0 + edit_button.style.maximal_height = 32 + Gui.set_data(edit_button, tag_name) end - local button = row.add {type = 'sprite-button', name = band_button_name, sprite = path} - button.tooltip = tooltip - button.style.top_padding = 0 - button.style.bottom_padding = 0 - button.style.maximal_height = 32 - Gui.set_data(button, tag_name) + local tag_button = + row.add { + type = 'sprite-button', + name = tag_button_name, + sprite = path, + tooltip = tag_name + } - local role_label = row.add {type = 'label', name = band_label_name, caption = tag_name .. size} - role_label.style.top_padding = 4 - role_label.style.minimal_width = 120 - Gui.set_data(role_label, {tag_name = tag_name, path = path}) + tag_button.style.maximal_height = 32 + Gui.set_data(tag_button, tag_name) + + local tag_label = row.add {type = 'label', name = tag_label_name, caption = tag_name .. size} + tag_label.style.left_padding = 4 + tag_label.style.minimal_width = 120 + Gui.set_data(tag_label, {tag_name = tag_name, path = path}) local list = row.add {type = 'flow', direction = 'horizontal'} @@ -191,17 +214,34 @@ local function draw_main_frame(player) draw_main_frame_content(scroll_pane) - local flow = main_frame.add {type = 'flow'} - flow.add {type = 'button', name = main_button_name, caption = 'Close'} - flow.add {type = 'button', name = clear_button_name, caption = 'Clear Tag'} + main_frame.add { + type = 'checkbox', + name = notify_checkbox_name, + caption = 'Notify me when tag groups change.', + state = not no_notify_players[player.index], + tooltip = '' + } + + local bottom_flow = main_frame.add {type = 'flow', direction = 'horizontal'} + + local left_flow = bottom_flow.add {type = 'flow', direction = 'horizontal'} + left_flow.style.align = 'left' + left_flow.style.horizontally_stretchable = true + + left_flow.add {type = 'button', name = main_button_name, caption = 'Close'} + + local right_flow = bottom_flow.add {type = 'flow', direction = 'horizontal'} + right_flow.style.align = 'right' + + right_flow.add {type = 'button', name = clear_button_name, caption = 'Clear Tag'} if player.admin or UserGroups.is_regular(player.name) then - flow.add {type = 'button', name = create_tag_button_name, caption = 'Create Tag'} + right_flow.add {type = 'button', name = create_tag_button_name, caption = 'Create Tag'} end end local function redraw_main_frame() - for _, p in ipairs(game.connected_players) do + for _, p in pairs(game.players) do local main_frame = p.gui.left[main_frame_name] if main_frame and main_frame.valid then local content = main_frame[main_frame_content_name] @@ -209,7 +249,9 @@ local function redraw_main_frame() Gui.remove_data_recursivly(content) content.clear() - draw_main_frame_content(content) + if p.connected then + draw_main_frame_content(content) + end end end end @@ -217,7 +259,7 @@ end local function redraw_main_button(player, path) local main_button = player.gui.top[main_button_name] - if path == '' then + if path == '' or path == nil then main_button.sprite = 'utility/pump_cannot_connect_icon' main_button.caption = 'tag' else @@ -261,6 +303,10 @@ local function draw_create_tag_frame(event, tag_data) if path and path ~= '' then spirte_type, path = path:match('([^/]+)/([^/]+)') + if spirte_type == 'virtual-signal' then + spirte_type = 'signal' + path = {type = 'virtual', name = path} + end else spirte_type = choices[1] path = nil @@ -276,7 +322,8 @@ local function draw_create_tag_frame(event, tag_data) confirm_caption = 'Create' end - local center = event.player.gui.center + local player = event.player + local center = player.gui.center local frame = center[create_tag_frame_name] if frame then @@ -332,47 +379,46 @@ local function draw_create_tag_frame(event, tag_data) local bottom_flow = frame.add {type = 'flow', direction = 'horizontal'} - local left_flow = bottom_flow.add{type = 'flow', direction = 'horizontal'} + local left_flow = bottom_flow.add {type = 'flow', direction = 'horizontal'} left_flow.style.align = 'left' + left_flow.style.horizontally_stretchable = true - local right_flow = bottom_flow.add{type = 'flow', direction = 'horizontal'} - right_flow.style.horizontally_stretchable = true - right_flow.style.align = 'right' - - local confirm_button = left_flow.add {type = 'button', name = confirm_create_tag_name, caption = confirm_caption} - Gui.set_data(confirm_button, frame) - - Gui.set_data( - frame, - { - focus = focus, - choose = choose, - icons_flow = icons_flow, - name = name_field, - verb = verb_field, - tag_data = tag_data - } - ) - - if tag_data then - left_flow.add {type = 'button', name = delete_tag_name, caption = 'Delete'} - end - - local close_button = right_flow.add {type = 'button', name = close_create_tag_name, caption = 'Close'} + local close_button = left_flow.add {type = 'button', name = close_create_tag_name, caption = 'Close'} Gui.set_data(close_button, frame) - event.player.opened = frame + local right_flow = bottom_flow.add {type = 'flow', direction = 'horizontal'} + right_flow.style.align = 'right' + + if tag_data then + local delete_button = right_flow.add {type = 'button', name = delete_tag_name, caption = 'Delete'} + Gui.set_data(delete_button, frame) + end + + local confirm_button = right_flow.add {type = 'button', name = confirm_create_tag_name, caption = confirm_caption} + Gui.set_data(confirm_button, frame) + + local data = { + focus = focus, + choose = choose, + icons_flow = icons_flow, + name = name_field, + verb = verb_field, + tag_data = tag_data + } + Gui.set_data(frame, data) + + player.opened = frame end Gui.on_click(main_button_name, toggle) Gui.on_click( - band_button_name, + tag_button_name, function(event) - local tag = Gui.get_data(event.element) + local tag_name = Gui.get_data(event.element) local path = event.element.sprite - if change_player_tag(event.player, tag) then + if change_player_tag(event.player, tag_name) then redraw_main_frame() redraw_main_button(event.player, path) end @@ -380,23 +426,29 @@ Gui.on_click( ) Gui.on_click( - band_label_name, + tag_label_name, function(event) local data = Gui.get_data(event.element) - local tag = data.band_name + local tag_name = data.tag_name local path = data.path - if change_player_tag(event.player, tag) then + if change_player_tag(event.player, tag_name) then redraw_main_frame() redraw_main_button(event.player, path) end end ) ---[[ Gui.on_click( - edit_tag_button_name, +Gui.on_click( + delete_tag_name, function(event) - local tag_name = Gui.get_data(event.element) + local frame = Gui.get_data(event.element) + local data = Gui.get_data(frame) + local tag_data = data.tag_data + local tag_name = tag_data.name + + Gui.remove_data_recursivly(frame) + frame.destroy() if not tag_groups[tag_name] then event.player.print("Sorry, Tag name '" .. tag_name .. "' not found.") @@ -406,7 +458,7 @@ Gui.on_click( local tag = '[' .. tag_name .. ']' for _, player in pairs(game.players) do - if player.tag == tag then + if player.valid and player.tag == tag then change_player_tag(player, '') if player.connected then @@ -419,9 +471,10 @@ Gui.on_click( redraw_main_frame() - game.print(event.player.name .. ' has deleted the ' .. tag_name .. ' tag group') + notify_players(event.player.name .. ' has deleted the ' .. tag_name .. ' tag group') end -) ]] +) + Gui.on_click( edit_tag_button_name, function(event) @@ -439,6 +492,23 @@ Gui.on_click( end ) +Gui.on_click( + notify_checkbox_name, + function(event) + local player_index = event.player_index + local checkbox = event.element + + local new_state + if checkbox.state then + new_state = nil + else + new_state = true + end + + no_notify_players[player_index] = new_state + end +) + Gui.on_click( clear_button_name, function(event) @@ -483,16 +553,17 @@ Gui.on_click( local player = event.player local frame = Gui.get_data(event.element) local data = Gui.get_data(frame) + local old_tag_data = data.tag_data - local name = data.name.text + local tag_name = data.name.text - if name == '' then + if tag_name == '' then player.print('Sorry, the tag needs a name') return end - if tag_groups[name] then - player.print('Sorry, tag ' .. data.name .. ' is already in use.') + if not old_tag_data and tag_groups[tag_name] then + player.print('Sorry, tag ' .. tag_name .. ' is already in use.') return end @@ -518,20 +589,48 @@ Gui.on_click( verb = deafult_verb end - local band_role = { - path = path, - verb = verb, - tooltip = name - } - - tag_groups[name] = band_role - - redraw_main_frame() - Gui.remove_data_recursivly(frame) frame.destroy() - game.print(player.name .. ' has made a new tag group called ' .. name) + local tag_data = { + path = path, + verb = verb + } + tag_groups[tag_name] = tag_data + + local message + if old_tag_data then + local old_name = old_tag_data.name + if old_name == tag_name and old_tag_data.path == path and old_tag_data.verb == verb then + return + end + + if old_name ~= tag_name then + message = player.name .. ' has edited the ' .. tag_name .. ' (formerly ' .. old_name .. ') tag group' + + local old_tag = '[' .. old_name .. ']' + + for _, p in pairs(game.players) do + if p.valid and p.tag == old_tag then + change_player_tag(p, tag_name, true) + + if p.connected then + redraw_main_button(player, '') + end + end + end + + tag_groups[old_name] = nil + else + message = player.name .. ' has edited the ' .. tag_name .. ' tag group' + end + else + message = player.name .. ' has made a new tag group called ' .. tag_name + end + + redraw_main_frame() + + notify_players(message) end ) @@ -600,4 +699,4 @@ local function tag_command(cmd) end end -commands.add_command('tag', ' Sets a players tag. (Admins only)', tag_command) +commands.add_command('tag', " Sets a player's tag. (Admins only)", tag_command) From cc3fefdeb9234a0e25a032a9dce39335b7759c5d Mon Sep 17 00:00:00 2001 From: grilledham Date: Thu, 14 Jun 2018 23:39:00 +0100 Subject: [PATCH 4/6] fixed tooltip --- tag_group.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag_group.lua b/tag_group.lua index fa680dc1..281c0340 100644 --- a/tag_group.lua +++ b/tag_group.lua @@ -219,7 +219,7 @@ local function draw_main_frame(player) name = notify_checkbox_name, caption = 'Notify me when tag groups change.', state = not no_notify_players[player.index], - tooltip = '' + tooltip = 'Receive a message when a player enters or leaves a tag group or when a tag group is created, edited or deleted' } local bottom_flow = main_frame.add {type = 'flow', direction = 'horizontal'} From 48215169c1570119c7bf1fd4f174d6dd0c4d5fc2 Mon Sep 17 00:00:00 2001 From: grilledham Date: Thu, 14 Jun 2018 23:39:25 +0100 Subject: [PATCH 5/6] fixed tooltip --- tag_group.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag_group.lua b/tag_group.lua index 281c0340..2f6673cd 100644 --- a/tag_group.lua +++ b/tag_group.lua @@ -219,7 +219,7 @@ local function draw_main_frame(player) name = notify_checkbox_name, caption = 'Notify me when tag groups change.', state = not no_notify_players[player.index], - tooltip = 'Receive a message when a player enters or leaves a tag group or when a tag group is created, edited or deleted' + tooltip = 'Receive a message when a player enters or leaves a tag group or when a tag group is created, edited or deleted.' } local bottom_flow = main_frame.add {type = 'flow', direction = 'horizontal'} From 60e9020724ec1f66a066423a7323523ed92ad323 Mon Sep 17 00:00:00 2001 From: grilledham Date: Fri, 15 Jun 2018 11:35:21 +0100 Subject: [PATCH 6/6] fixed bug --- tag_group.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag_group.lua b/tag_group.lua index 2f6673cd..9ef2339e 100644 --- a/tag_group.lua +++ b/tag_group.lua @@ -14,7 +14,7 @@ Global.register( function(data) tag_groups = data.tag_groups player_tags = data.player_tags - no_notify_players = no_notify_players + no_notify_players = data.no_notify_players end )