Skip to content

Commit a01f06f

Browse files
committed
Scratch-like category UI
This adds all uncollapsed block categories back into one large scroll container, but now with buttons that can navigate to each category. This allows the user to see the entire selection of blocks, without the tedium of scrolling. I think this will be nicer for adding in a little menu to create variables within the scrollbox as well.
1 parent 896b230 commit a01f06f

File tree

7 files changed

+150
-79
lines changed

7 files changed

+150
-79
lines changed

addons/block_code/block_code_plugin.gd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const DISABLED_CLASSES := [
4141
"Picker",
4242
"TitleBar",
4343
"MainPanel",
44-
"BlockCodePlugin"
44+
"BlockCodePlugin",
45+
"BlockCategoryButton"
4546
]
4647

4748

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@tool
2+
class_name BlockCategoryButton
3+
extends MarginContainer
4+
5+
signal selected
6+
7+
var category: BlockCategory
8+
9+
@onready var _panel := %Panel
10+
@onready var _label := %Label
11+
12+
13+
func _ready():
14+
if not category:
15+
category = BlockCategory.new("Example", Color.RED)
16+
17+
var new_stylebox: StyleBoxFlat = _panel.get_theme_stylebox("panel").duplicate()
18+
new_stylebox.bg_color = category.color
19+
20+
_panel.add_theme_stylebox_override("panel", new_stylebox)
21+
22+
_label.text = category.name
23+
24+
25+
func _on_button_pressed():
26+
selected.emit(category)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[gd_scene load_steps=7 format=3 uid="uid://bdtetj0gs45hv"]
2+
3+
[ext_resource type="Script" path="res://addons/block_code/ui/picker/categories/block_category_button.gd" id="1_pxxnl"]
4+
5+
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_eogpc"]
6+
bg_color = Color(1, 0, 0, 1)
7+
corner_radius_top_left = 100
8+
corner_radius_top_right = 100
9+
corner_radius_bottom_right = 100
10+
corner_radius_bottom_left = 100
11+
12+
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_ousiv"]
13+
14+
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_fyk0j"]
15+
bg_color = Color(1, 1, 1, 0.196078)
16+
17+
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_ha83k"]
18+
bg_color = Color(1, 1, 1, 0.392157)
19+
20+
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_dgecf"]
21+
22+
[node name="BlockCategoryButton" type="MarginContainer"]
23+
custom_minimum_size = Vector2(150, 0)
24+
offset_right = 148.0
25+
offset_bottom = 32.0
26+
script = ExtResource("1_pxxnl")
27+
28+
[node name="HBoxContainer" type="HBoxContainer" parent="."]
29+
layout_mode = 2
30+
31+
[node name="MarginContainer" type="MarginContainer" parent="HBoxContainer"]
32+
custom_minimum_size = Vector2(40, 40)
33+
layout_mode = 2
34+
theme_override_constants/margin_left = 8
35+
theme_override_constants/margin_top = 8
36+
theme_override_constants/margin_right = 8
37+
theme_override_constants/margin_bottom = 8
38+
39+
[node name="Panel" type="Panel" parent="HBoxContainer/MarginContainer"]
40+
unique_name_in_owner = true
41+
layout_mode = 2
42+
theme_override_styles/panel = SubResource("StyleBoxFlat_eogpc")
43+
44+
[node name="Label" type="Label" parent="HBoxContainer"]
45+
unique_name_in_owner = true
46+
layout_mode = 2
47+
text = "Example"
48+
49+
[node name="Button" type="Button" parent="."]
50+
layout_mode = 2
51+
mouse_default_cursor_shape = 2
52+
theme_override_styles/normal = SubResource("StyleBoxEmpty_ousiv")
53+
theme_override_styles/hover = SubResource("StyleBoxFlat_fyk0j")
54+
theme_override_styles/pressed = SubResource("StyleBoxFlat_ha83k")
55+
theme_override_styles/focus = SubResource("StyleBoxEmpty_dgecf")
56+
57+
[connection signal="pressed" from="Button" to="." method="_on_button_pressed"]

addons/block_code/ui/picker/categories/block_category_display.gd

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,18 @@
22
class_name BlockCategoryDisplay
33
extends MarginContainer
44

5-
signal category_expanded(value: bool)
6-
75
var category: BlockCategory
86

9-
@onready var _button := %Button
10-
@onready var _blocks_container := %BlocksContainer
7+
@onready var _label := %Label
118
@onready var _blocks := %Blocks
12-
@onready var _background := %Background
13-
14-
@onready var _icon_collapsed := EditorInterface.get_editor_theme().get_icon("GuiTreeArrowRight", "EditorIcons")
15-
@onready var _icon_expanded := EditorInterface.get_editor_theme().get_icon("GuiTreeArrowDown", "EditorIcons")
16-
17-
var expanded: bool:
18-
set = _set_expanded
19-
20-
21-
func _set_expanded(value: bool):
22-
expanded = value
23-
24-
_blocks_container.visible = expanded
25-
if expanded:
26-
_button.icon = _icon_expanded
27-
_background.color = category.color.darkened(0.5)
28-
_background.color.a = 0.3
29-
else:
30-
_button.icon = _icon_collapsed
31-
_background.color = category.color.darkened(0.2)
32-
_background.color.a = 0.3
33-
34-
category_expanded.emit(expanded)
359

3610

3711
func _ready():
38-
if not category:
39-
category = BlockCategory.new()
40-
41-
_button.text = category.name
12+
_label.text = category.name
4213

4314
for _block in category.block_list:
4415
var block: Block = _block as Block
4516

4617
block.color = category.color
4718

4819
_blocks.add_child(block)
49-
50-
expanded = false
51-
52-
53-
func _on_button_toggled(toggled_on):
54-
expanded = toggled_on

addons/block_code/ui/picker/categories/block_category_display.tscn

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,25 @@ offset_right = -918.0
1010
offset_bottom = -246.0
1111
grow_horizontal = 2
1212
grow_vertical = 2
13+
theme_override_constants/margin_left = 4
14+
theme_override_constants/margin_bottom = 40
1315
script = ExtResource("1_wkdht")
1416

15-
[node name="Background" type="ColorRect" parent="."]
16-
unique_name_in_owner = true
17+
[node name="VBoxContainer" type="VBoxContainer" parent="."]
1718
layout_mode = 2
1819

19-
[node name="MarginContainer" type="MarginContainer" parent="."]
20+
[node name="MarginContainer" type="MarginContainer" parent="VBoxContainer"]
2021
layout_mode = 2
21-
theme_override_constants/margin_left = 10
22-
theme_override_constants/margin_top = 4
23-
theme_override_constants/margin_right = 10
2422
theme_override_constants/margin_bottom = 4
2523

26-
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
27-
custom_minimum_size = Vector2(400, 0)
28-
layout_mode = 2
29-
30-
[node name="Spacer" type="Control" parent="MarginContainer/VBoxContainer"]
31-
custom_minimum_size = Vector2(0, 4)
32-
layout_mode = 2
33-
34-
[node name="Button" type="Button" parent="MarginContainer/VBoxContainer"]
24+
[node name="Label" type="Label" parent="VBoxContainer/MarginContainer"]
3525
unique_name_in_owner = true
3626
layout_mode = 2
37-
theme_override_font_sizes/font_size = 16
38-
toggle_mode = true
39-
text = "Category"
40-
flat = true
41-
alignment = 0
42-
icon_alignment = 2
27+
theme_override_font_sizes/font_size = 18
28+
text = "Example Category
29+
"
4330

44-
[node name="BlocksContainer" type="VBoxContainer" parent="MarginContainer/VBoxContainer"]
45-
unique_name_in_owner = true
46-
visible = false
47-
layout_mode = 2
48-
49-
[node name="Spacer2" type="Control" parent="MarginContainer/VBoxContainer/BlocksContainer"]
50-
custom_minimum_size = Vector2(0, 4)
51-
layout_mode = 2
52-
53-
[node name="Blocks" type="VBoxContainer" parent="MarginContainer/VBoxContainer/BlocksContainer"]
31+
[node name="Blocks" type="VBoxContainer" parent="VBoxContainer"]
5432
unique_name_in_owner = true
5533
layout_mode = 2
5634
theme_override_constants/separation = 14
57-
58-
[node name="Spacer3" type="Control" parent="MarginContainer/VBoxContainer/BlocksContainer"]
59-
custom_minimum_size = Vector2(0, 50)
60-
layout_mode = 2
61-
62-
[connection signal="toggled" from="MarginContainer/VBoxContainer/Button" to="." method="_on_button_toggled"]

addons/block_code/ui/picker/picker.gd

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ extends MarginContainer
55
signal block_picked(block: Block)
66

77
@onready var _block_list := %BlockList
8+
@onready var _block_scroll := %BlockScroll
9+
@onready var _category_list := %CategoryList
810

911

1012
func bsd_selected(bsd: BlockScriptData):
@@ -33,6 +35,9 @@ func bsd_selected(bsd: BlockScriptData):
3335

3436

3537
func reset_picker():
38+
for c in _category_list.get_children():
39+
c.queue_free()
40+
3641
for c in _block_list.get_children():
3742
c.queue_free()
3843

@@ -46,6 +51,12 @@ func init_picker(extra_blocks: Array[Block] = [], extra_categories: Array[BlockC
4651
for _category in block_categories:
4752
var category: BlockCategory = _category as BlockCategory
4853

54+
var block_category_button: BlockCategoryButton = preload("res://addons/block_code/ui/picker/categories/block_category_button.tscn").instantiate()
55+
block_category_button.category = category
56+
block_category_button.selected.connect(_category_selected)
57+
58+
_category_list.add_child(block_category_button)
59+
4960
var block_category_display := preload("res://addons/block_code/ui/picker/categories/block_category_display.tscn").instantiate()
5061
block_category_display.category = category
5162

@@ -55,6 +66,15 @@ func init_picker(extra_blocks: Array[Block] = [], extra_categories: Array[BlockC
5566
var block: Block = _block as Block
5667
block.drag_started.connect(_block_picked)
5768

69+
_block_scroll.scroll_vertical = 0
70+
5871

5972
func _block_picked(block: Block):
6073
block_picked.emit(block)
74+
75+
76+
func _category_selected(category: BlockCategory):
77+
for block_category_display in _block_list.get_children():
78+
if block_category_display.category.name == category.name:
79+
_block_scroll.scroll_vertical = block_category_display.position.y
80+
break

addons/block_code/ui/picker/picker.tscn

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,48 @@
44

55
[node name="Picker" type="MarginContainer"]
66
custom_minimum_size = Vector2(200, 0)
7-
offset_right = 200.0
8-
offset_bottom = 104.0
7+
offset_right = 251.0
8+
offset_bottom = 253.0
99
script = ExtResource("1_tkt44")
1010

1111
[node name="Panel" type="Panel" parent="."]
1212
layout_mode = 2
1313

14-
[node name="ScrollContainer" type="ScrollContainer" parent="."]
14+
[node name="HBoxContainer" type="HBoxContainer" parent="."]
1515
layout_mode = 2
16+
theme_override_constants/separation = 12
17+
18+
[node name="CategoryMarginContainer" type="MarginContainer" parent="HBoxContainer"]
19+
layout_mode = 2
20+
theme_override_constants/margin_left = 12
21+
theme_override_constants/margin_top = 12
22+
theme_override_constants/margin_right = 0
23+
theme_override_constants/margin_bottom = 12
24+
25+
[node name="CategoryScroll" type="ScrollContainer" parent="HBoxContainer/CategoryMarginContainer"]
26+
layout_mode = 2
27+
size_flags_horizontal = 3
28+
horizontal_scroll_mode = 0
29+
30+
[node name="CategoryList" type="VBoxContainer" parent="HBoxContainer/CategoryMarginContainer/CategoryScroll"]
31+
unique_name_in_owner = true
32+
layout_mode = 2
33+
theme_override_constants/separation = 4
34+
35+
[node name="BlockMarginContainer" type="MarginContainer" parent="HBoxContainer"]
36+
layout_mode = 2
37+
theme_override_constants/margin_left = 0
38+
theme_override_constants/margin_top = 12
39+
theme_override_constants/margin_right = 12
40+
theme_override_constants/margin_bottom = 12
41+
42+
[node name="BlockScroll" type="ScrollContainer" parent="HBoxContainer/BlockMarginContainer"]
43+
unique_name_in_owner = true
44+
layout_mode = 2
45+
size_flags_horizontal = 3
1646
horizontal_scroll_mode = 0
1747

18-
[node name="BlockList" type="VBoxContainer" parent="ScrollContainer"]
48+
[node name="BlockList" type="VBoxContainer" parent="HBoxContainer/BlockMarginContainer/BlockScroll"]
1949
unique_name_in_owner = true
2050
layout_mode = 2
2151
theme_override_constants/separation = 0

0 commit comments

Comments
 (0)