ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
sh_toc.c
Go to the documentation of this file.
1
18#include "ra8_gfx.h"
19#include "ra8_gfx_font.h"
20#include "sh_app.h"
21
23typedef enum : int32_t {
27
29static int32_t sh_toc_visible_rows(void)
30{
31 return ((int32_t)k_sh_fb_h - (int32_t)k_sh_bar_h) / (int32_t)k_sh_toc_row_h;
32}
33
35static void sh_toc_label(uint32_t idx, char* buf, size_t cap)
36{
37 char full[k_sh_linebuf];
38 sh_book_chapter_label(idx, full, sizeof full);
39 sh_fit(buf, cap, full, sh_cells((int32_t)k_sh_fb_w - (2 * (int32_t)k_sh_pad) - k_sh_toc_num_w));
40}
41
42void sh_toc_render(void)
43{
44 (void)ra8_gfx_clear((uint32_t)k_sh_col_card);
45 sh_titlebar("< Cover", g_sh.entry[g_sh.selected].title);
46 const int32_t rows = sh_toc_visible_rows();
47 for (int32_t r = 0; r < rows; ++r) {
48 const uint32_t ci = (uint32_t)(g_sh.toc_scroll + r);
49 if (ci >= g_sh.chapter_count) {
50 break;
51 }
52 const int32_t ry = (int32_t)k_sh_bar_h + (r * (int32_t)k_sh_toc_row_h);
53 const bool cur = (ci == g_sh.chapter);
54 const uint32_t bg = cur ? (uint32_t)k_sh_col_rowhi : (uint32_t)k_sh_col_card;
55 (void)ra8_gfx_rect(0, ry, (int32_t)k_sh_fb_w, (int32_t)k_sh_toc_row_h, bg, true);
56 (void)ra8_gfx_rect(0,
57 ry + (int32_t)k_sh_toc_row_h - 1,
58 (int32_t)k_sh_fb_w,
59 1,
60 (uint32_t)k_sh_col_edge,
61 true);
62 char num[k_sh_dec_base];
63 size_t np = sh_fmt_uint(num, 0U, ci + 1U);
64 num[np] = '\0';
65 const int32_t ty = ry + (((int32_t)k_sh_toc_row_h - (int32_t)k_sh_glyph_h) / 2);
66 (void)
68 char label[k_sh_linebuf];
69 sh_toc_label(ci, label, sizeof label);
71 ty,
72 label,
74 (uint32_t)k_sh_col_ink,
75 bg);
76 }
77}
78
79int32_t sh_toc_hit(int32_t x, int32_t y)
80{
81 (void)x;
82 if (y < (int32_t)k_sh_bar_h) {
83 return -1;
84 }
85 const int32_t row = (y - (int32_t)k_sh_bar_h) / (int32_t)k_sh_toc_row_h;
86 const uint32_t ci = (uint32_t)(g_sh.toc_scroll + row);
87 return (ci < g_sh.chapter_count) ? (int32_t)ci : -1;
88}
89
90void sh_toc_scroll(int32_t dir)
91{
92 const int32_t rows = sh_toc_visible_rows();
93 int32_t top = g_sh.toc_scroll + (dir * rows);
94 const int32_t max = (int32_t)g_sh.chapter_count - rows;
95 if (top > max) {
96 top = max;
97 }
98 if (top < 0) {
99 top = 0;
100 }
101 g_sh.toc_scroll = top;
102}
Software 2D graphics primitives layered on top of a caller-ownedframebuffer (DRW / D/AVE 2D / GLCDC r...
ra8_err_t ra8_gfx_clear(uint32_t color)
Fill the bound framebuffer's clip region with a single colour.
ra8_err_t ra8_gfx_text_out(int32_t x, int32_t y, const char *str, const ra8_gfx_font_t *font, uint32_t fg_color, uint32_t bg_color)
Render a NUL-terminated ASCII string.
ra8_err_t ra8_gfx_rect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color, bool filled)
Draw an axis-aligned rectangle.
Bitmap font descriptor + bundled font handles for ra8_gfx.
const ra8_gfx_font_t ra8_gfx_font_8x16
Bundled 8x16 IBM PC VGA bitmap font, ASCII 0x20..0x7E.
Shared contract for the ereader_shelf multi-screen e-reader.
int32_t sh_cells(int32_t pixels)
Cells of the 8px bitmap font that fit in pixels.
Definition sh_util.c:28
void sh_book_chapter_label(uint32_t chapter, char *out, size_t cap)
Resolve a chapter's TOC label (or "Chapter N") into out.
Definition sh_book.c:536
void sh_titlebar(const char *title, const char *right)
Fill the header bar and draw title left + optional right text.
Definition sh_util.c:76
@ k_sh_glyph_h
Bitmap font cell height.
Definition sh_app.h:49
@ k_sh_fb_h
Panel height in pixels.
Definition sh_app.h:47
@ k_sh_pad
Outer margin / content inset.
Definition sh_app.h:52
@ k_sh_bar_h
Header / title-bar height.
Definition sh_app.h:51
@ k_sh_linebuf
Per-line draw buffer bytes.
Definition sh_app.h:59
@ k_sh_toc_row_h
TOC list row height.
Definition sh_app.h:58
@ k_sh_fb_w
Panel width in pixels.
Definition sh_app.h:46
@ k_sh_dec_base
Decimal formatting base.
Definition sh_app.h:71
size_t sh_fmt_uint(char *dst, size_t pos, uint32_t v)
Append unsigned v as decimal into dst at pos; returns new pos.
Definition sh_util.c:52
sh_state_t g_sh
The single whole-app state instance (defined in main.c).
Definition main.c:64
void sh_fit(char *dst, size_t cap, const char *src, int32_t max_chars)
Copy src into dst (cap bytes), truncating past max_chars with "..".
Definition sh_util.c:33
@ k_sh_col_edge
Card / image border.
Definition sh_app.h:104
@ k_sh_col_ink
Body ink.
Definition sh_app.h:102
@ k_sh_col_rowhi
TOC selected-row fill.
Definition sh_app.h:107
@ k_sh_col_card
Card / page fill (paper).
Definition sh_app.h:100
@ k_sh_col_sub
Secondary ink (author / hints).
Definition sh_app.h:103
void sh_toc_scroll(int32_t dir)
Scroll the TOC list by dir pages (-1 up / +1 down), clamped.
Definition sh_toc.c:90
static int32_t sh_toc_visible_rows(void)
Rows of the chapter list visible at once.
Definition sh_toc.c:29
void sh_toc_render(void)
Render the open book's chapter list (TOC).
Definition sh_toc.c:42
sh_toc_const_t
TOC layout constants.
Definition sh_toc.c:23
@ k_sh_toc_text_pad
Row text inset.
Definition sh_toc.c:24
@ k_sh_toc_num_w
Chapter-number column width.
Definition sh_toc.c:25
int32_t sh_toc_hit(int32_t x, int32_t y)
Hit-test a TOC tap; returns the chapter index or -1.
Definition sh_toc.c:79
static void sh_toc_label(uint32_t idx, char *buf, size_t cap)
Resolve chapter idx's display label into buf (cap bytes), truncated.
Definition sh_toc.c:35