/* * TinyBlocks - tb_control.c * Categoria "Controle": repita / repita até que / sempre / se / se-senão. * Cada bloco C diz como roda por uma TinyBlocks_FlowClass (tb_flow.h); quem * executa os scripts so segue as bocas que ela escolhe. */ #include #include "tb_control.h" static void eval_noop(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; self->dirty = false; } /* -- Fluxos --- * step: 0 = entrando, 1 = vai testar/entrar na volta, 2 = a volta acabou * (pausa um quadro, como no Scratch, e testa de novo). */ static int repeat_next(TinyBlocks_Flow* f) { if (f->step == 0) { f->count = tb_flow_arg_integer(f, 0); f->step = 1; } if (f->step == 2) { f->step = 1; return TB_FLOW_YIELD; } if (f->count-- <= 0) return TB_FLOW_DONE; f->step = 2; return 0; } static int until_next(TinyBlocks_Flow* f) { if (f->step == 2) { f->step = 1; return TB_FLOW_YIELD; } if (tb_flow_arg_truthy(f, 0)) return TB_FLOW_DONE; f->step = 2; return 0; } static int forever_next(TinyBlocks_Flow* f) { if (f->step == 2) { f->step = 1; return TB_FLOW_YIELD; } f->step = 2; return 0; } static int if_next(TinyBlocks_Flow* f) { if (f->step++ > 0) return TB_FLOW_DONE; return tb_flow_arg_truthy(f, 0) ? 0 : TB_FLOW_DONE; } static int if_else_next(TinyBlocks_Flow* f) { if (f->step++ > 0) return TB_FLOW_DONE; return tb_flow_arg_truthy(f, 0) ? 0 : 1; } static const TinyBlocks_FlowClass s_repeat = { 1, { NULL }, false, repeat_next, NULL }; static const TinyBlocks_FlowClass s_until = { 1, { NULL }, false, until_next, NULL }; static const TinyBlocks_FlowClass s_forever = { 1, { NULL }, true, forever_next, NULL }; static const TinyBlocks_FlowClass s_if = { 1, { NULL }, false, if_next, NULL }; static const TinyBlocks_FlowClass s_if_else = { 2, { "senão" }, false, if_else_next, NULL }; /* -- Registro --- */ static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name, const TinyBlocks_SlotDesc* in, uint32_t ni, const TinyBlocks_FlowClass* flow, bool cond) { TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, NULL, 0, eval_noop); if (!t) return false; tb_type_block_set_flow(t, flow); if (cond) tb_type_block_set_input_flags(t, 0, TB_SLOT_BOOLEAN); /* campo hexagonal */ return tb_library_add(lib, &t->base); } static bool control_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { static const TinyBlocks_Category cat = { "Controle", {255/255.f, 171/255.f, 25/255.f, 1.f}, {207/255.f, 139/255.f, 23/255.f, 1.f}, }; tb_library_set_category(lib, &cat); /* Metadados dos blocos (tooltip do editor: Alt + hover). */ TB_BLOCK_META("Control.Repeat", "repeat_next", "Repete o corpo do bloco o numero de vezes indicado"); TB_BLOCK_META("Control.RepeatUntil", "until_next", "Repete o corpo enquanto a condicao for falsa"); TB_BLOCK_META("Control.Forever", "forever_next", "Repete o corpo para sempre (ate clicar no botao vermelho)"); TB_BLOCK_META("Control.If", "if_next", "Executa o corpo se a condicao for verdadeira"); TB_BLOCK_META("Control.IfElse", "if_else_next", "Executa o corpo 'entao' ou 'senao' conforme a condicao"); static const TinyBlocks_SlotDesc times_in[] = {{"vezes", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc cond_in[] = {{"condição", TB_VALUE_INTEGER}}; return reg(rt, lib, "Control.Repeat", times_in, 1, &s_repeat, false) && reg(rt, lib, "Control.RepeatUntil", cond_in, 1, &s_until, true) && reg(rt, lib, "Control.Forever", NULL, 0, &s_forever, false) && reg(rt, lib, "Control.If", cond_in, 1, &s_if, true) && reg(rt, lib, "Control.IfElse", cond_in, 1, &s_if_else, true); } static void control_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { (void)rt; (void)lib; } TINYBLOCKS_DEFINE_LIBRARY(control, "tinyblocks-control", "1.0.0", control_init, control_deinit) const TinyBlocks_LibraryDescriptor* tb_control_get_library(void) { return &_tb_lib_desc_control; }