natiny.view.set_hook
natiny.view.set_hook(view, hook, fn)
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 |
number |
View handle |
hook |
integer |
Which event; one of the constants below |
fn |
function |
Called with the view handle; nil removes the hook |
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
function(view) end
| Argument | Type | Description |
|---|---|---|
view |
number |
The view the hook was set on |
Nothing the function returns is used. Read what it needs with
natiny.view.get_rect,
get_hovered,
get_pressed and
get_pointer.
Example
local button = natiny.view.create(natiny.view.FREE, toolbar)
natiny.view.set_min_size(button, 120, 36)
natiny.view.set_hook(button, natiny.view.HOOK_DRAW, function(self)
local x, y, w, h = natiny.view.get_rect(self)
local color = IDLE
if natiny.view.get_pressed(self) then color = PRESSED
elseif natiny.view.get_hovered(self) then color = HOVER end
natiny.render.set_color(color)
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, "Play",
natiny.render.ALIGN_CENTER | natiny.render.ALIGN_MIDDLE)
end)
natiny.view.set_hook(button, natiny.view.HOOK_MOUSE_CLICK, function(self)
start_game()
end)
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.