Drawing#
Two widgets put your own pixels on the screen: ui_image for a sprite and ui_canvas for anything you draw with
graphx.
Images#
gfx_UninitedSprite(badge, 16, 16);
ui_image(badge, 2);
ui_image(sprite, scale) places a graphx sprite at an integer scale (0 counts as 1). It is as wide and high as the
scaled sprite, so in a UI_AUTO column it takes exactly its width.
The command stores a pointer to the sprite, not its pixels, and the sprite is drawn again whenever its tiles are
redrawn. So the sprite must stay valid after the frame: a const sprite from convimg or a static buffer. Changing
the pixels does not redraw it by itself; draw a sprite that changes in a canvas with a new version instead.
Palettes#
CEUI owns the top UI_SLOTS entries of the palette (16 by default, entries 240 to 255). Convert sprites with
convimg using at most 256 minus UI_SLOTS palette entries (max-entries: 240) so their palette stays below
CEUI’s.
To draw a sprite in theme colors, so it follows dark mode and accent changes, fill it with ui_color(slot):
for (int y = 0; y < 16; y++)
for (int x = 0; x < 16; x++)
badge->data[y * 16 + x] = ui_color((x / 4 + y / 4) % 2 ? UI_ACCENT : SLOT_GOLD);
Canvases#
static void draw_scene(ui_rect_t area, void *context)
{
const scene_t *s = context;
gfx_SetColor(ui_color(UI_LINE));
gfx_HorizLine(area.x, area.y + area.h / 2, area.w);
gfx_SetColor(ui_color(UI_ACCENT));
gfx_FillCircle(area.x + s->x, area.y + s->y, 4);
}
ui_canvas(96, draw_scene, &scene, scene.version);
ui_canvas(h, draw, context, version) reserves a rectangle as wide as its column and h pixels high. Your draw
function is called later, while CEUI redraws the screen, with the rectangle in screen coordinates and your context
pointer. It may use any graphx call. The graphx clip region is set to the visible part of the canvas, and CEUI draws
into the back buffer, so draw at area.x and area.y, not at 0.
When a canvas is redrawn#
CEUI cannot see what your function draws, so it redraws a canvas only when:
versionchanged since the frame before,the canvas moved or changed size,
something on top of it or next to it changed and the tiles under the canvas were redrawn anyway.
When your data changes, change version. A counter that you increment is enough:
static void animate(void)
{
if (!scene.running) return;
ui_wake(40);
step(&scene);
scene.version++;
}
A canvas is always redrawn as a whole, so its lines are always clipped the same way, and draw may be called more
than once per frame, once for each rectangle CEUI redraws. It must draw the same thing each time and must not change
your state.
Colors#
Use ui_color(slot) for theme colors, as above. It turns a palette slot like UI_ACCENT into the palette index
graphx wants. Indices below 256 - UI_SLOTS are yours.
Lines and the graphx clipping bug#
graphx’s clipped gfx_Line drops parts of steep lines even inside the clip region. When the whole canvas is visible,
for example when it is not in a scroll area, draw lines with gfx_Line_NoClip, as the showcase does:
gfx_Line_NoClip((uint24_t)last_x, (uint8_t)last_y, (uint24_t)x, (uint8_t)y);
Keep every point inside area when you do, since nothing clips it.
The Canvas screen: an animated canvas, and a sprite drawn with palette slots next to wrapped text.#
Canvases and dimming#
Canvases and sprites are not dimmed under a modal or menu; they keep their colors.