Adiciona libraries toml/fs, metadados .toml e build por library
- tinyblocks-toml: lexer + parser TOML (include/lexeme.h, include/ast.h, src/lexer.c, src/parser.c) e bloco Toml.Get. - tinyblocks-fs: categoria "Arquivos" com FS.Open/Read/Write/Append/Tell/Close. - library.toml em cada library com metadados (categoria, cor, descricao) e receita de build, sem precisar embutir isso no .so. - Makefiles proprios por library com build/ local; Makefile raiz delega. - Palco (tb_stage) movido para o core; pen deixa de depender do editor, o que torna os .so arrastaveis. - Editor linka com -rdynamic para resolver simbolos do core em .so via dlopen. - Testes: test-toml (parser) e test-plugin (dlopen de .so standalone).
This commit is contained in:
1 parent
90d47bf8a0
commit
dc23b92d15
48 files changed
+2405
-744
No files matched your search
@@ -1,35 +1,33 @@
|
||||
# Makefile da library editor (editor visual estilo Scratch)
|
||||
# Constroi build/libtinyblocks-editor.a e build/libtinyblocks-editor.so
|
||||
# Inclui o widget Editor.Stage (tb_stage.c).
|
||||
CC := gcc
|
||||
BUILD := ../../build
|
||||
|
||||
SDL2_CFLAGS := $(shell sdl2-config --cflags)
|
||||
SDL2_LIBS := $(shell sdl2-config --libs)
|
||||
SDL2TTF_CFLAGS:= $(shell pkg-config --cflags SDL2_ttf 2>/dev/null)
|
||||
SDL2TTF_LIBS := $(shell pkg-config --libs SDL2_ttf 2>/dev/null)
|
||||
|
||||
ifeq ($(SDL2TTF_LIBS),)
|
||||
$(error SDL2_ttf não encontrado. Instale: sudo apt install libsdl2-ttf-dev)
|
||||
endif
|
||||
BUILD := build
|
||||
CORE := ../../build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L \
|
||||
-I../../include \
|
||||
-Iinclude \
|
||||
$(SDL2_CFLAGS) $(SDL2TTF_CFLAGS)
|
||||
-D_POSIX_C_SOURCE=200809L \
|
||||
-I../../include -Iinclude -Isrc \
|
||||
-I../tinyblocks-graphics/include \
|
||||
-I../tinyblocks-variables/include
|
||||
|
||||
LDFLAGS := $(SDL2_LIBS) $(SDL2TTF_LIBS) -lm
|
||||
|
||||
OBJ := $(BUILD)/tb_editor.o
|
||||
OBJ := $(BUILD)/tb_editor.o $(BUILD)/tb_stage_widget.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-editor.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-editor.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
|
||||
all: static shared
|
||||
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(OBJ): src/tb_editor.c include/tb_editor.h
|
||||
$(BUILD)/tb_editor.o: src/tb_editor.c include/tb_editor.h src/tb_stage_widget.h \
|
||||
../tinyblocks-graphics/include/tb_graphics.h \
|
||||
../tinyblocks-variables/include/tb_variables.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(BUILD)/tb_stage_widget.o: src/tb_stage_widget.c src/tb_stage_widget.h \
|
||||
../tinyblocks-graphics/include/tb_graphics.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
@@ -37,7 +35,7 @@ $(LIB_A): $(OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJ)
|
||||
$(CC) -shared -o $@ $^ $(LDFLAGS)
|
||||
$(CC) -shared -o $@ $^ -ldl
|
||||
|
||||
clean:
|
||||
$(RM) $(OBJ) $(LIB_A) $(LIB_SO)
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library editor. Lido pela UI/build sem recompilar o .so.
|
||||
[library]
|
||||
name = "tinyblocks-editor"
|
||||
version = "1.0.0"
|
||||
category = "Meus Blocos"
|
||||
description = "Editor visual e blocos compostos"
|
||||
color = [255, 102, 128, 255]
|
||||
dark = [255, 51, 85, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_editor.c", "src/tb_stage_widget.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-editor.so"
|
||||
static = "build/libtinyblocks-editor.a"
|
||||
depends = []
|
||||
@@ -35,8 +35,9 @@
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_renderable_block.h>
|
||||
#include <tb_graphics.h>
|
||||
#include <tinyblocks/tb_stage.h>
|
||||
#include <tb_variables.h>
|
||||
#include "tb_stage.h"
|
||||
#include "tb_stage_widget.h"
|
||||
#include "../include/tb_editor.h"
|
||||
|
||||
|
||||
@@ -2555,7 +2556,7 @@ static void render_stage(Ed* ed, TinyBlocks_RenderableBlock* rb) {
|
||||
rb_text_c(rb, bx + (bw - tw(msg, FS_UI))*0.5f, by + ph*0.5f, msg, COL_TEXT_DIM, FS_UI);
|
||||
} else {
|
||||
TinyBlocks_RenderableBlock* sub = NULL;
|
||||
tb_stage_set_view(ed->stage, bw-2, ph-2);
|
||||
tb_stage_set_view(bw-2, ph-2);
|
||||
clip_set(ed, rb, bx+1, by+1, bw-2, ph-2);
|
||||
if (ed->stage->type->on_draw(ed->stage, ed->rt, &sub) && sub) {
|
||||
tb_draw_merge_offset(rb, sub, bx+1, by+1);
|
||||
|
||||
@@ -1,215 +0,0 @@
|
||||
/*
|
||||
* TinyBlocks - tb_stage.c
|
||||
* O Palco do editor: uma tela lógica de 480x360 (origem no centro, y para
|
||||
* cima, como no Scratch) onde os blocos Stage.* desenham. É um componente de
|
||||
* UI: o editor o embute no painel direito (chamando seu on_draw) ou o entrega
|
||||
* como cena de um Graphics.Window próprio ("destacar palco").
|
||||
*
|
||||
* Os desenhos ficam numa lista retida e são re-emitidos a cada on_draw,
|
||||
* escalados para o tamanho da área (letterbox mantendo 4:3).
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <tb_graphics.h>
|
||||
#include "tb_stage.h"
|
||||
|
||||
#define STAGE_W 480.f
|
||||
#define STAGE_H 360.f
|
||||
#define STAGE_MAX_CMDS 4000
|
||||
#define STAGE_TEXT_FS 14.f
|
||||
|
||||
typedef enum { SC_RECT, SC_CIRCLE, SC_LINE, SC_TEXT } StageCmdKind;
|
||||
|
||||
typedef struct {
|
||||
StageCmdKind kind;
|
||||
float a, b, c, d; /* geometria em coordenadas do palco */
|
||||
float size; /* espessura da caneta */
|
||||
float col[3];
|
||||
char text[96];
|
||||
} StageCmd;
|
||||
|
||||
typedef struct {
|
||||
StageCmd* cmds;
|
||||
int n, cap;
|
||||
float bg[3];
|
||||
float pen[3];
|
||||
float pen_size;
|
||||
float view_w, view_h;
|
||||
bool full_warned;
|
||||
} Stage;
|
||||
|
||||
static TinyBlocks_Block* s_current = NULL;
|
||||
static TinyBlocks* s_rt = NULL; /* para mensagens de erro */
|
||||
|
||||
static Stage* stage_of(TinyBlocks_Block* b) { return b ? *(Stage**)b->userdata : NULL; }
|
||||
|
||||
TinyBlocks_Block* tb_stage_current(void) { return s_current; }
|
||||
|
||||
void tb_stage_set_view(TinyBlocks_Block* stage, float w, float h) {
|
||||
Stage* s = stage_of(stage);
|
||||
if (s && w > 0 && h > 0) { s->view_w = w; s->view_h = h; }
|
||||
}
|
||||
|
||||
/* ===
|
||||
* Editor.Stage
|
||||
* === */
|
||||
static bool stage_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
Stage* s = (Stage*)calloc(1, sizeof(Stage));
|
||||
if (!s) return false;
|
||||
s->bg[0] = s->bg[1] = s->bg[2] = 1.f;
|
||||
s->pen[0] = 76/255.f; s->pen[1] = 151/255.f; s->pen[2] = 1.f;
|
||||
s->pen_size = 2.f;
|
||||
s->view_w = STAGE_W; s->view_h = STAGE_H;
|
||||
*(Stage**)self->userdata = s;
|
||||
s_current = self;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void stage_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
Stage* s = stage_of(self);
|
||||
if (s) { free(s->cmds); free(s); }
|
||||
*(Stage**)self->userdata = NULL;
|
||||
if (s_current == self) s_current = NULL;
|
||||
}
|
||||
|
||||
/* input 0 (opcional): Graphics.Input da janela que mostra o palco - dá o tamanho da vista */
|
||||
static void eval_stage(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Block* in = v.kind == TB_VALUE_BLOCK ? v.as.block : NULL;
|
||||
if (!in || in->output_count < GFX_IN_COUNT) return;
|
||||
TinyBlocks_Value w = in->outputs[GFX_IN_WIN_W], h = in->outputs[GFX_IN_WIN_H];
|
||||
if (w.kind == TB_VALUE_INTEGER && h.kind == TB_VALUE_INTEGER)
|
||||
tb_stage_set_view(self, (float)w.as.integer, (float)h.as.integer);
|
||||
}
|
||||
|
||||
static bool stage_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, TinyBlocks_RenderableBlock** dst) {
|
||||
(void)rt;
|
||||
Stage* s = stage_of(self);
|
||||
if (!s) return false;
|
||||
TinyBlocks_RenderableBlock* rb = tb_renderable_acquire();
|
||||
if (!rb) return false;
|
||||
|
||||
float sc = s->view_w / STAGE_W < s->view_h / STAGE_H ? s->view_w / STAGE_W : s->view_h / STAGE_H;
|
||||
float ox = (s->view_w - STAGE_W*sc) * 0.5f, oy = (s->view_h - STAGE_H*sc) * 0.5f;
|
||||
#define SX(x) (ox + ((x) + STAGE_W*0.5f) * sc)
|
||||
#define SY(y) (oy + (STAGE_H*0.5f - (y)) * sc)
|
||||
|
||||
if (ox > 0.5f || oy > 0.5f) tb_draw_fill_rect(rb, 0, 0, s->view_w, s->view_h, 0.12f, 0.12f, 0.14f, 1.f);
|
||||
tb_draw_fill_rect(rb, ox, oy, STAGE_W*sc, STAGE_H*sc, s->bg[0], s->bg[1], s->bg[2], 1.f);
|
||||
tb_draw_set_clip(rb, ox, oy, STAGE_W*sc, STAGE_H*sc);
|
||||
|
||||
for (int i = 0; i < s->n && rb->count < TB_DRAW_CMD_CAP - 2; i++) {
|
||||
const StageCmd* c = &s->cmds[i];
|
||||
float r = c->col[0], g = c->col[1], b = c->col[2];
|
||||
switch (c->kind) {
|
||||
case SC_RECT:
|
||||
tb_draw_fill_rect(rb, SX(c->a - c->c*0.5f), SY(c->b + c->d*0.5f), c->c*sc, c->d*sc, r, g, b, 1.f);
|
||||
break;
|
||||
case SC_CIRCLE:
|
||||
tb_draw_fill_circle(rb, SX(c->a), SY(c->b), c->c*sc, r, g, b, 1.f);
|
||||
break;
|
||||
case SC_LINE:
|
||||
tb_draw_stroke_line(rb, SX(c->a), SY(c->b), SX(c->c), SY(c->d), c->size*sc, r, g, b, 1.f);
|
||||
break;
|
||||
case SC_TEXT: {
|
||||
float fs = STAGE_TEXT_FS * sc;
|
||||
tb_draw_text(rb, SX(c->a), SY(c->b) + fs*0.36f, c->text, fs, r, g, b, 1.f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
tb_draw_clear_clip(rb);
|
||||
#undef SX
|
||||
#undef SY
|
||||
*dst = rb;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ===
|
||||
* API de desenho - usada pela library tinyblocks-pen (categoria "Caneta")
|
||||
* === */
|
||||
static float chan(float v) { return v < 0 ? 0.f : v > 255 ? 1.f : v / 255.f; }
|
||||
|
||||
static StageCmd* push(TinyBlocks* rt, StageCmdKind kind) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (!s) { tinyblocks_set_error(rt, "nenhum palco (Editor.Stage) criado"); return NULL; }
|
||||
if (s->n >= STAGE_MAX_CMDS) {
|
||||
if (!s->full_warned) tinyblocks_set_error(rt, "palco cheio (%d desenhos) = use \"apague tudo\"", STAGE_MAX_CMDS);
|
||||
s->full_warned = true;
|
||||
return NULL;
|
||||
}
|
||||
if (s->n >= s->cap) {
|
||||
int nc = s->cap ? s->cap * 2 : 64;
|
||||
StageCmd* p = (StageCmd*)realloc(s->cmds, (size_t)nc * sizeof(StageCmd));
|
||||
if (!p) return NULL;
|
||||
s->cmds = p; s->cap = nc;
|
||||
}
|
||||
StageCmd* c = &s->cmds[s->n++];
|
||||
memset(c, 0, sizeof(*c));
|
||||
c->kind = kind;
|
||||
memcpy(c->col, s->pen, sizeof(c->col));
|
||||
c->size = s->pen_size;
|
||||
return c;
|
||||
}
|
||||
|
||||
void tb_stage_clear(void) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (s) { s->n = 0; s->full_warned = false; }
|
||||
}
|
||||
|
||||
void tb_stage_set_background(float r, float g, float b) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (s) { s->bg[0] = chan(r); s->bg[1] = chan(g); s->bg[2] = chan(b); }
|
||||
}
|
||||
|
||||
void tb_stage_set_pen(float r, float g, float b) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (s) { s->pen[0] = chan(r); s->pen[1] = chan(g); s->pen[2] = chan(b); }
|
||||
}
|
||||
|
||||
void tb_stage_set_pen_size(float size) {
|
||||
Stage* s = stage_of(s_current);
|
||||
if (s) s->pen_size = size < 1.f ? 1.f : size;
|
||||
}
|
||||
|
||||
void tb_stage_line(float x1, float y1, float x2, float y2) {
|
||||
StageCmd* c = push(s_rt, SC_LINE);
|
||||
if (c) { c->a = x1; c->b = y1; c->c = x2; c->d = y2; }
|
||||
}
|
||||
|
||||
void tb_stage_rect(float x, float y, float w, float h) {
|
||||
StageCmd* c = push(s_rt, SC_RECT);
|
||||
if (c) { c->a = x; c->b = y; c->c = w; c->d = h; }
|
||||
}
|
||||
|
||||
void tb_stage_circle(float x, float y, float radius) {
|
||||
StageCmd* c = push(s_rt, SC_CIRCLE);
|
||||
if (c) { c->a = x; c->b = y; c->c = radius; }
|
||||
}
|
||||
|
||||
void tb_stage_text(const char* text, float x, float y) {
|
||||
StageCmd* c = push(s_rt, SC_TEXT);
|
||||
if (c) {
|
||||
snprintf(c->text, sizeof(c->text), "%s", text ? text : "");
|
||||
c->a = x; c->b = y;
|
||||
}
|
||||
}
|
||||
|
||||
/* ===
|
||||
* Registro do widget Editor.Stage
|
||||
* === */
|
||||
bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
s_rt = rt;
|
||||
static const TinyBlocks_SlotDesc stage_in[] = {{"entrada", TB_VALUE_BLOCK}};
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "Editor.Stage", stage_in, 1, NULL, 0, eval_stage);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_instance_hooks(t, sizeof(Stage*), stage_instance_init, stage_instance_deinit);
|
||||
t->on_draw = stage_on_draw;
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* TinyBlocks - tb_stage.h
|
||||
* Palco do editor (Editor.Stage) e API de desenho usada pela library Caneta.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_EDITOR_STAGE_H
|
||||
#define TINYBLOCKS_EDITOR_STAGE_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
/* Registra apenas o widget Editor.Stage na library do editor */
|
||||
bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib);
|
||||
|
||||
/* Tamanho da área onde o palco será desenhado (on_draw usa 0..w - 0..h) */
|
||||
void tb_stage_set_view(TinyBlocks_Block* stage, float w, float h);
|
||||
|
||||
/* Palco onde o desenho acontece (o último Editor.Stage criado) */
|
||||
TinyBlocks_Block* tb_stage_current(void);
|
||||
|
||||
/* == API de desenho (usada pela library tinyblocks-pen) === */
|
||||
void tb_stage_clear(void);
|
||||
void tb_stage_set_background(float r, float g, float b); /* canais 0..255 */
|
||||
void tb_stage_set_pen(float r, float g, float b); /* canais 0..255 */
|
||||
void tb_stage_set_pen_size(float size);
|
||||
void tb_stage_line(float x1, float y1, float x2, float y2);
|
||||
void tb_stage_rect(float x, float y, float w, float h);
|
||||
void tb_stage_circle(float x, float y, float radius);
|
||||
void tb_stage_text(const char* text, float x, float y);
|
||||
|
||||
#endif /* TINYBLOCKS_EDITOR_STAGE_H */
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* TinyBlocks - tb_stage_widget.c (editor)
|
||||
* Editor.Stage: widget que renderiza o palco logico do runtime. Toda a
|
||||
* logica de desenho/estado fica no core (tb_stage.h); aqui so adaptamos ao
|
||||
* loop de UI do editor.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_stage.h>
|
||||
#include <tinyblocks/tb_renderable_block.h>
|
||||
#include <tb_graphics.h>
|
||||
#include "tb_stage_widget.h"
|
||||
|
||||
/* input 0 (opcional): Graphics.Input da janela que mostra o palco - da o tamanho da vista */
|
||||
static void eval_stage(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Block* in = v.kind == TB_VALUE_BLOCK ? v.as.block : NULL;
|
||||
if (!in || in->output_count < GFX_IN_COUNT) return;
|
||||
TinyBlocks_Value w = in->outputs[GFX_IN_WIN_W], h = in->outputs[GFX_IN_WIN_H];
|
||||
if (w.kind == TB_VALUE_INTEGER && h.kind == TB_VALUE_INTEGER)
|
||||
tb_stage_set_view((float)w.as.integer, (float)h.as.integer);
|
||||
}
|
||||
|
||||
static bool stage_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, TinyBlocks_RenderableBlock** dst) {
|
||||
(void)self; (void)rt;
|
||||
TinyBlocks_RenderableBlock* rb = tb_renderable_acquire();
|
||||
if (!rb) return false;
|
||||
tb_stage_render(rb);
|
||||
*dst = rb;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool stage_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)self; (void)rt;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void stage_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)self; (void)rt;
|
||||
tb_stage_reset();
|
||||
}
|
||||
|
||||
bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_SlotDesc stage_in[] = {{"entrada", TB_VALUE_BLOCK}};
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "Editor.Stage", stage_in, 1, NULL, 0, eval_stage);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_instance_hooks(t, 1, stage_instance_init, stage_instance_deinit);
|
||||
t->on_draw = stage_on_draw;
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* TinyBlocks - tb_stage_widget.h (editor)
|
||||
* Widget Editor.Stage: exibe o palco logico do runtime (tb_stage.h).
|
||||
*/
|
||||
#ifndef TINYBLOCKS_EDITOR_STAGE_WIDGET_H
|
||||
#define TINYBLOCKS_EDITOR_STAGE_WIDGET_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
/* Registra o TypeBlock Editor.Stage na library do editor */
|
||||
bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib);
|
||||
|
||||
#endif /* TINYBLOCKS_EDITOR_STAGE_WIDGET_H */
|
||||
Reference in new issue
Block a user