Skip to content

Commit f51dc4f

Browse files
authored
Update tag groups (#1521)
* Add default AFK tag * Restyle tag groups * Display group count with button * Allow tag buttons to add/remove player tag
1 parent c555c77 commit f51dc4f

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

features/gui/tag_group.lua

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ local function notify_players(message)
3838
end
3939
end
4040

41+
local function compare_tag(player, tag_name)
42+
local old_tag = player.tag
43+
local new_tag = '[' .. tag_name .. ']'
44+
return (old_tag == new_tag) or (old_tag == tag_name)
45+
end
46+
4147
local function change_player_tag(player, tag_name, silent)
4248
local old_tag = player.tag
4349
if tag_name == '' and old_tag == '' then
@@ -98,7 +104,7 @@ local function get_size(players, show_offline)
98104
local size = 0
99105

100106
if not players then
101-
return ''
107+
return size
102108
end
103109

104110
if show_offline then
@@ -112,11 +118,7 @@ local function get_size(players, show_offline)
112118
end
113119
end
114120

115-
if size == 0 then
116-
return ''
117-
else
118-
return ' (' .. size .. ')'
119-
end
121+
return size
120122
end
121123

122124
local main_button_name = Gui.uid_name()
@@ -163,8 +165,8 @@ end
163165

164166
local function draw_main_frame_content(parent)
165167
local player = parent.gui.player
166-
local grid = parent.add {type = 'table', column_count = 1}
167-
grid.style.vertical_spacing = 0
168+
local grid = parent.add { type = 'table', column_count = 1 }
169+
grid.style.vertical_spacing = 2
168170

169171
local can_edit = Rank.equal_or_greater_than(player.name, Ranks.regular)
170172

@@ -175,20 +177,19 @@ local function draw_main_frame_content(parent)
175177
local size = get_size(players)
176178
local path = tag_data.path
177179

178-
local row = grid.add {type = 'table', column_count = 4}
179-
row.style.horizontal_spacing = 0
180+
local row = grid.add { type = 'table', column_count = 4 }
180181

181182
if can_edit then
182183
local edit_button =
183184
row.add {
184185
type = 'sprite-button',
185186
name = edit_tag_button_name,
186187
sprite = 'utility/rename_icon',
187-
tooltip = {'tag_group.edit_group'}
188+
tooltip = {'tag_group.edit_group'},
189+
style = 'crafting_queue_slot',
188190
}
189-
edit_button.style.top_padding = 0
190-
edit_button.style.bottom_padding = 0
191-
edit_button.style.maximal_height = 32
191+
edit_button.visible = can_edit
192+
Gui.set_style(edit_button, { padding = 4, size = 32 })
192193
Gui.set_data(edit_button, tag_name)
193194
end
194195

@@ -197,18 +198,19 @@ local function draw_main_frame_content(parent)
197198
type = 'sprite-button',
198199
name = tag_button_name,
199200
sprite = path,
200-
tooltip = tag_name
201+
tooltip = tag_name,
202+
style = 'slot_button_in_shallow_frame',
203+
number = (size > 0) and size or nil,
201204
}
202-
203-
tag_button.style.maximal_height = 32
205+
Gui.set_style(tag_button, { size = 32 })
204206
Gui.set_data(tag_button, tag_name)
205207

206-
local tag_label = row.add {type = 'label', name = tag_label_name, caption = tag_name .. size}
207-
tag_label.style.left_padding = 4
208-
tag_label.style.minimal_width = 120
209-
Gui.set_data(tag_label, {tag_name = tag_name, path = path})
208+
local tag_label = row.add { type = 'label', name = tag_label_name, caption = tag_name, style = 'semibold_caption_label' }
209+
Gui.set_style(tag_label, { left_padding = 4, minimal_width = 120 })
210+
Gui.set_data(tag_label, { tag_name = tag_name, path = path })
210211

211212
local list = row.add {type = 'flow', direction = 'horizontal'}
213+
Gui.set_style(list, { minimal_width = 100 })
212214

213215
if players then
214216
for k, _ in pairs(players) do
@@ -217,13 +219,10 @@ local function draw_main_frame_content(parent)
217219
local color = {r = 0.4 + 0.6 * p.color.r, g = 0.4 + 0.6 * p.color.g, b = 0.4 + 0.6 * p.color.b}
218220

219221
local label = list.add {type = 'label', caption = game.get_player(k).name}
220-
label.style.top_padding = 8
221-
label.style.font_color = color
222+
Gui.set_style(label, { top_padding = 8, font_color = color })
222223
end
223224
end
224225
end
225-
226-
list.style.minimal_width = 100
227226
end
228227
end
229228

@@ -234,20 +233,17 @@ local function draw_main_frame(player)
234233
caption = {'tag_group.choose_your_tag'},
235234
direction = 'vertical'
236235
})
236+
Gui.set_style(main_frame, { maximal_height = 320, maximal_width = 500, natural_width = 320 })
237237

238-
main_frame.style.maximal_height = 500
239-
main_frame.style.maximal_width = 500
240-
main_frame.style.minimal_width = 320
238+
local inner_frame = main_frame.add { type = 'frame', style = 'inside_shallow_frame', name = 'inner' }
241239

242240
local scroll_pane =
243-
main_frame.add {
241+
inner_frame.add {
244242
type = 'scroll-pane',
245243
name = main_frame_content_name,
246244
vertical_scroll_policy = 'always'
247245
}
248-
249-
scroll_pane.style.horizontally_stretchable = true
250-
scroll_pane.style.right_padding = 0
246+
Gui.set_style(scroll_pane, { horizontally_stretchable = true, padding = 4 })
251247

252248
draw_main_frame_content(scroll_pane)
253249

@@ -264,9 +260,7 @@ local function draw_main_frame(player)
264260
local bottom_flow = main_frame.add {type = 'flow', direction = 'horizontal'}
265261

266262
local left_flow = bottom_flow.add {type = 'flow', direction = 'horizontal'}
267-
left_flow.style.horizontal_align = 'left'
268-
left_flow.style.horizontally_stretchable = true
269-
263+
Gui.set_style(left_flow, { horizontal_align = 'left', horizontally_stretchable = true })
270264
Gui.make_close_button(left_flow, main_button_name)
271265

272266
local right_flow = bottom_flow.add {type = 'flow', direction = 'horizontal'}
@@ -285,7 +279,7 @@ local function redraw_main_frame()
285279
for _, p in pairs(game.players) do
286280
local main_frame = Gui.get_left_element(p, main_frame_name)
287281
if main_frame and main_frame.valid then
288-
local content = main_frame[main_frame_content_name]
282+
local content = main_frame.inner[main_frame_content_name]
289283

290284
Gui.remove_data_recursively(content)
291285
content.clear()
@@ -300,11 +294,9 @@ end
300294
local function redraw_main_button(player, path)
301295
local main_button = Gui.get_top_element(player, main_button_name)
302296

303-
if path == '' or path == nil then
304-
main_button.sprite = 'utility/pump_cannot_connect_icon'
305-
main_button.caption = 'tag'
297+
if (path == '') or (path == nil) or (not helpers.is_valid_sprite_path(path)) then
298+
main_button.sprite = 'utility/bookmark'
306299
else
307-
main_button.caption = ''
308300
main_button.sprite = path
309301
end
310302
end
@@ -377,6 +369,7 @@ local function draw_create_tag_frame(event, tag_data)
377369
end
378370

379371
frame = center.add({type = 'frame', name = create_tag_frame_name, caption = frame_caption, direction = 'vertical'})
372+
local inner = frame.add { type = 'frame', style = 'inside_shallow_frame_with_padding', direction = 'vertical', name = 'inner' }
380373

381374
if tag_data then
382375
local text = LocaleBuilder.new({'common.created_by', tag_data.created_by or '<Server>'})
@@ -386,10 +379,10 @@ local function draw_create_tag_frame(event, tag_data)
386379
text = text:add(', '):add({'common.edited_by', concat(edited_by, ', ')})
387380
end
388381

389-
frame.add({type = 'label', caption = text})
382+
inner.add({type = 'label', caption = text})
390383
end
391384

392-
local main_table = frame.add {type = 'table', column_count = 2}
385+
local main_table = inner.add {type = 'table', column_count = 2}
393386

394387
main_table.add {type = 'label', caption = {'common.name'}}
395388
local name_field = main_table.add {type = 'textfield', text = name}
@@ -420,7 +413,8 @@ local function draw_create_tag_frame(event, tag_data)
420413
icons_flow.add {
421414
type = 'choose-elem-button',
422415
name = create_tag_choose_icon_name,
423-
elem_type = spirte_type
416+
elem_type = spirte_type,
417+
style = 'slot_button_in_shallow_frame',
424418
}
425419

426420
if path then
@@ -452,11 +446,13 @@ local function draw_create_tag_frame(event, tag_data)
452446
right_flow.style.horizontal_align = 'right'
453447

454448
if tag_data then
455-
local delete_button = right_flow.add {type = 'button', name = delete_tag_name, caption = {'common.delete'}}
449+
local delete_button = right_flow.add {type = 'button', name = delete_tag_name, caption = {'common.delete'}, style = 'red_back_button'}
450+
Gui.set_style(delete_button, { height = 28, font = 'default-semibold' })
456451
Gui.set_data(delete_button, frame)
457452
end
458453

459-
local confirm_button = right_flow.add {type = 'button', name = confirm_create_tag_name, caption = confirm_caption}
454+
local confirm_button = right_flow.add {type = 'button', name = confirm_create_tag_name, caption = confirm_caption, style = 'confirm_button', tooltip = ''}
455+
Gui.set_style(confirm_button, { height = 28, font = 'default-semibold' })
460456
Gui.set_data(confirm_button, frame)
461457

462458
local data = {
@@ -478,12 +474,20 @@ Gui.on_click(main_button_name, toggle)
478474
Gui.on_click(
479475
tag_button_name,
480476
function(event)
477+
local player = event.player
481478
local tag_name = Gui.get_data(event.element)
482479
local path = event.element.sprite
483480

484-
if change_player_tag(event.player, tag_name) then
481+
if compare_tag(player, tag_name) then
482+
if change_player_tag(player, '') then
483+
redraw_main_frame()
484+
end
485+
return
486+
end
487+
488+
if change_player_tag(player, tag_name) then
485489
redraw_main_frame()
486-
redraw_main_button(event.player, path)
490+
redraw_main_button(player, path)
487491
end
488492
end
489493
)

resources/tag_groups.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,10 @@ return {
6161
path = 'entity/medium-biter',
6262
join_message = '{tag} has woofed with {player}',
6363
leave_message = '{player} has left the {tag} squad'
64+
},
65+
['AFK'] = {
66+
path = 'entity/entity-ghost',
67+
join_message = '{player} has gone {tag} to get some pizza for everyone',
68+
leave_message = 'Welcome back from {tag} {player}, hope you brought some pizza for us'
6469
}
6570
}

0 commit comments

Comments
 (0)