Files
Tahgibu/tests/plugins/plug_good.c
T
2026-10-24 11:52:16 -03:00

58 lines
2.7 KiB
C

#include "plug_common.h"
TGB_PLUGIN_DECLARE_API_VERSION;
static const tgb_host_api *H;
static const tgb_color RED = {255, 0, 0, 255};
static tgb_status cmd_cross(tgb_plugin_handle *self, void *user) {
(void)user;
tgb_status s = H->edit_begin(self, 0, 0);
if (s < 0) return s;
int cx = H->doc_width(self) / 2, cy = H->doc_height(self) / 2;
for (int i = -3; i <= 3; i++) { H->put_pixel(self, cx + i, cy, RED); H->put_pixel(self, cx, cy + i, RED); }
return H->edit_end(self);
}
static tgb_status cmd_two_steps(tgb_plugin_handle *self, void *user) {
(void)user;
tgb_status s = H->add_frame(self, H->doc_frame_count(self)); /* paso 1: estructura */
if (s < 0) return s;
s = H->edit_begin(self, 0, 0); /* paso 2: píxeles */
if (s < 0) return s;
H->put_pixel(self, 1, 1, RED);
return H->edit_end(self);
}
static tgb_status cmd_fail_midway(tgb_plugin_handle *self, void *user) {
(void)user;
H->add_frame(self, 0);
if (H->edit_begin(self, 0, 0) == TGB_OK) { H->put_pixel(self, 2, 2, RED); H->edit_end(self); }
return TGB_ERR_PLUGIN; /* falla a mitad: el host debe revertirlo todo */
}
static tgb_status cmd_forget_end(tgb_plugin_handle *self, void *user) {
(void)user;
tgb_status s = H->edit_begin(self, 0, 0);
if (s < 0) return s;
H->put_pixel(self, 3, 3, RED);
return TGB_OK; /* olvida edit_end */
}
static tgb_status cmd_read(tgb_plugin_handle *self, void *user) {
(void)user;
return H->get_pixel(self, 0, 0, 0, 0).a == 0 ? TGB_OK : TGB_ERR_PLUGIN;
}
static tgb_status cmd_outside(tgb_plugin_handle *self, void *user) { (void)user; (void)self; return TGB_OK; }
TGB_PLUGIN_EXPORT tgb_status tgb_plugin_init(const tgb_host_api *host, tgb_plugin_handle *self, tgb_plugin_info *info) {
H = host;
mark("init");
if (host->api_version != TGB_PLUGIN_API_VERSION || host->struct_size < sizeof(tgb_host_api)) return TGB_ERR_INCOMPATIBLE;
host->register_command(self, "dibujar-cruz", cmd_cross, NULL);
host->register_command(self, "dos-pasos", cmd_two_steps, NULL);
host->register_command(self, "falla-a-medias", cmd_fail_midway, NULL);
host->register_command(self, "olvida-cerrar", cmd_forget_end, NULL);
host->register_command(self, "lee-pixel", cmd_read, NULL);
host->register_command(self, "no-hace-nada", cmd_outside, NULL);
info->name = "Cruz de prueba";
info->version = "1.0.0";
info->required_caps = TGB_CAP_READ_DOCUMENT | TGB_CAP_EDIT_DOCUMENT;
return TGB_OK;
}
TGB_PLUGIN_EXPORT void tgb_plugin_shutdown(tgb_plugin_handle *self) { (void)self; mark("shutdown"); }