// ============================================================================ // block_core.c - 积木核心操作 // ============================================================================ #include "block_core.h" #include #include // 创建积木 Block* block_create(BlockType type, float x, float y) { Block* block = (Block*)calloc(1, sizeof(Block)); if (!block) return NULL; block->type = type; block->x = x; block->y = y; block->width = 200; block->height = 60; // 初始化槽位 block->input_count = 0; block->output_count = 0; for (int i = 0; i < MAX_BLOCK_INPUTS; i++) { block->inputs[i].owner = block; block->inputs[i].id = i; } for (int i = 0; i < MAX_BLOCK_OUTPUTS; i++) { block->outputs[i].owner = block; block->outputs[i].id = i; } return block; } // 复制积木 (深拷贝,不复制连接关系) Block* block_copy(const Block* src) { if (!src) return NULL; Block* block = (Block*)malloc(sizeof(Block)); if (!block) return NULL; memcpy(block, src, sizeof(Block)); // 重新设置拥有者,断开所有连接 for (int i = 0; i < MAX_BLOCK_INPUTS; i++) { block->inputs[i].owner = block; block->inputs[i].connected = NULL; } for (int i = 0; i < MAX_BLOCK_OUTPUTS; i++) { block->outputs[i].owner = block; block->outputs[i].connected = NULL; } block->next = NULL; block->parent_if = NULL; block->workspace = NULL; block->user_data = NULL; return block; } // 销毁积木 void block_destroy(Block* block) { if (!block) return; free(block); } // 添加输入槽位 BlockSlot* block_add_input(Block* block, SlotKind kind, SlotValueType value_type, const char* label) { if (block->input_count >= MAX_BLOCK_INPUTS) return NULL; BlockSlot* slot = &block->inputs[block->input_count]; slot->kind = kind; slot->value_type = value_type; if (label) strncpy(slot->label, label, MAX_BLOCK_LABEL - 1); slot->value.type = value_type; block->input_count++; return slot; } // 添加输出槽位 BlockSlot* block_add_output(Block* block, SlotKind kind, SlotValueType value_type, const char* label) { if (block->output_count >= MAX_BLOCK_OUTPUTS) return NULL; BlockSlot* slot = &block->outputs[block->output_count]; slot->kind = kind; slot->value_type = value_type; if (label) strncpy(slot->label, label, MAX_BLOCK_LABEL - 1); block->output_count++; return slot; } // 连接两个槽位 bool block_connect(BlockSlot* from, BlockSlot* to) { if (!from || !to) return false; if (from->owner == to->owner) return false; // 不能自连 // 断开旧连接 if (from->connected) from->connected->connected = NULL; if (to->connected) to->connected->connected = NULL; from->connected = to; to->connected = from; return true; } // 断开槽位 void block_disconnect(BlockSlot* slot) { if (!slot || !slot->connected) return; slot->connected->connected = NULL; slot->connected = NULL; } // 检查槽位是否已连接 bool block_slot_connected(BlockSlot* slot) { return slot && slot->connected != NULL; } // 获取槽位的值 SlotValue block_slot_get_value(BlockSlot* slot) { if (!slot) { SlotValue empty = {0}; return empty; } // 如果有连接,从连接的积木获取值 if (slot->connected && slot->connected->owner) { Block* source = slot->connected->owner; // 从源积木的输出槽位获取值 for (int i = 0; i < source->output_count; i++) { if (&source->outputs[i] == slot->connected) { return source->outputs[i].value; } } } return slot->value; } // 设置槽位的值 void block_slot_set_value(BlockSlot* slot, SlotValue value) { if (slot) slot->value = value; } // 积木是否包含点 bool block_contains_point(Block* block, float px, float py) { return px >= block->x && px <= block->x + block->width && py >= block->y && py <= block->y + block->height; } // 获取槽位的世界位置 void block_slot_world_pos(BlockSlot* slot, float* wx, float* wy) { if (!slot || !slot->owner) { *wx = 0; *wy = 0; return; } *wx = slot->owner->x + slot->x; *wy = slot->owner->y + slot->y; }