Skip to content

Commit

Permalink
Merge pull request #226 from endlessm/add-category-block-order
Browse files Browse the repository at this point in the history
Add category block order
  • Loading branch information
manuq authored Sep 24, 2024
2 parents 4cd2a69 + 9559577 commit b036959
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion addons/block_code/ui/picker/categories/block_category_display.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@ extends MarginContainer
signal block_picked(block: Block)

const BlockCategory = preload("res://addons/block_code/ui/picker/categories/block_category.gd")
const BlockDefinition = preload("res://addons/block_code/code_generation/block_definition.gd")
const Util = preload("res://addons/block_code/ui/util.gd")

const CATEGORY_ORDER_OVERRIDE = {
"Lifecycle":
[
"ready",
"process",
],
"Logic | Conditionals":
[
"if",
"else",
"else_if",
]
}

var category: BlockCategory

@onready var _context := BlockEditorContext.get_default()
Expand All @@ -20,11 +35,25 @@ func _ready():
if _context.block_script == null:
return

for block_definition in _context.block_script.get_blocks_in_category(category):
if category == null:
return

var category_order = CATEGORY_ORDER_OVERRIDE.get(category.name)
var block_definitions = _context.block_script.get_blocks_in_category(category)
if category_order:
block_definitions.sort_custom(_sort_blocks_by_list_order.bind(category_order))

for block_definition in block_definitions:
var block: Block = _context.block_script.instantiate_block(block_definition)

block.color = category.color
block.can_delete = false
block.drag_started.connect(func(block: Block): block_picked.emit(block))

_blocks.add_child(block)


static func _sort_blocks_by_list_order(block_definition_a, block_definition_b, name_order: Array) -> bool:
var a_order = name_order.find(block_definition_a.name)
var b_order = name_order.find(block_definition_b.name)
return a_order >= 0 and a_order < b_order or b_order == -1

0 comments on commit b036959

Please sign in to comment.