natiny_view_set_hook
void natiny_view_set_hook(NatinyView view, uint32_t hook,
natiny_view_fn fn, void* userdata);
Sets the function a view calls when it is drawn, when the pointer does something to it, or when it is destroyed.
A view with a NATINY_VIEW_HOOK_DRAW function draws its children when that
function calls
natiny_render_view_children. A view
without a draw function draws its children automatically.
Parameters
| Name | Type | Description |
|---|---|---|
view |
NatinyView |
View handle |
hook |
uint32_t |
Which event; one of the constants below |
fn |
natiny_view_fn |
Called when the event happens; NULL removes the hook |
userdata |
void* |
Passed to fn unchanged; the view does not own it |
Constants
| Constant | Called |
|---|---|
NATINY_VIEW_HOOK_DRAW |
Every render, to draw the view |
NATINY_VIEW_HOOK_MOUSE_ENTER |
When the pointer comes over the view or one of its pickable descendants |
NATINY_VIEW_HOOK_MOUSE_LEAVE |
When it is no longer over either |
NATINY_VIEW_HOOK_MOUSE_MOVE |
When the pointer moves over the view, or anywhere while the view is pressed |
NATINY_VIEW_HOOK_MOUSE_DOWN |
When the button goes down on the view |
NATINY_VIEW_HOOK_MOUSE_UP |
When the button that pressed the view is released, wherever the pointer is |
NATINY_VIEW_HOOK_MOUSE_CLICK |
After NATINY_VIEW_HOOK_MOUSE_UP, when the release is over the view that was pressed |
NATINY_VIEW_HOOK_DESTROY |
When the view is destroyed, before it is gone |
Callback
typedef void (*natiny_view_fn)(NatinyView view, void* userdata);
| Argument | Type | Description |
|---|---|---|
view |
NatinyView |
The view the hook was set on |
userdata |
void* |
The pointer given with this hook |
Example
typedef struct { const char* label; int clicks; } Button;
static void button_draw(NatinyView self, void* userdata) {
Button* button = userdata;
float x, y, w, h;
natiny_view_get_rect(self, &x, &y, &w, &h);
natiny_render_set_color(natiny_view_get_pressed(self) ? PRESSED
: natiny_view_get_hovered(self) ? HOVER : IDLE);
natiny_render_rounded_rectangle(x, y, w, h, 5, 5, 5, 5);
natiny_render_set_color(INK);
natiny_render_text_box(font, x, y, w, h, button->label,
NATINY_ALIGN_CENTER | NATINY_ALIGN_MIDDLE);
}
static void button_click(NatinyView self, void* userdata) {
((Button*)userdata)->clicks++;
}
static Button play = { "Play", 0 };
NatinyView view = natiny_view_create(NATINY_LAYOUT_FREE);
natiny_view_set_min_size(view, 120, 36);
natiny_view_set_hook(view, NATINY_VIEW_HOOK_DRAW, button_draw, &play);
natiny_view_set_hook(view, NATINY_VIEW_HOOK_MOUSE_CLICK, button_click, &play);
Notes
- Every hook except
NATINY_VIEW_HOOK_DESTROYruns insidenatiny_render_view, after all rectangles are laid out: first the pointer hooks, thenNATINY_VIEW_HOOK_DRAW, from the root down. - A scissor a
NATINY_VIEW_HOOK_DRAWfunction begins and does not end is ended when the function returns. userdatais not freed by the engine.NATINY_VIEW_HOOK_DESTROYis where to free it.