xnano.context
xnano.context
¶
xnano.context
What a hook is called with, and one frame or update of the clock.
Classes:
-
Plugin–The attachment of a hook: the hook or component that is running right now.
-
ReadLog–What a component read while it was evaluated: the record that decides when it runs again.
-
Context–What a hook is called with.
-
Frame–One frame of a canvas's clock.
-
Update–One step of a simulation, which runs separately from drawing.
Plugin
¶
Plugin(hook: HookHandle[object, object])
The attachment of a hook: the hook or component that is running right now.
context.plugin names that one attachment and nothing else. The hook itself is
context.hook.
Attributes:
-
hook–The hook this attachment belongs to.
-
state(StateNamespace) –State that belongs to this attachment alone, read with
getand written withpush; seeStateNamespace.
Create the attachment of a hook or component. Use context.plugin instead of
calling this.
Parameters:
-
hook(HookHandle[object, object]) –The hook or component driver that is running.
Source code in xnano/context.py
state
property
¶
state: StateNamespace
State that belongs to this hook alone.
No other hook or placement sees it, and it is deleted when the hook is removed. To
share a value between hooks, keep it in context.root.state.
Examples:
import xnano
from xnano_core import Event
with xnano.new(width=8, height=1) as canvas:
@canvas.hook(on='key')
def count(context: xnano.Context) -> None:
state = context.plugin.state
state.push('presses', state.get('presses', default=0) + 1)
context.root.state.push('latest', state.get('presses'))
canvas.push(Event.key_press('a'))
canvas.push(Event.key_press('b'))
canvas.deliver_events()
assert canvas.state.get('latest') == 2
ReadLog
¶
What a component read while it was evaluated: the record that decides when it runs again.
A component that read nothing that changes is evaluated once. One that read the time is evaluated again on every frame (or every beat for a playable), one that read the size when the screen is resized, and one that read an input when that input arrives.
Attributes:
-
uses_time–Whether it read the clock, the event or the held keys.
-
uses_size–Whether it read the size of the canvas or of a buffer.
-
uses_deps–Whether it read the dependencies.
-
input_kinds(set[str]) –The kinds of input it read:
'key','mouse','scroll'or'paste'.
Start an empty record.
Source code in xnano/context.py
Context
¶
Context(canvas: Canvas[DepsT_co, StateT_co], owner: Canvas[object, object] | Placement[object, object], hook: HookHandle[object, object], event: Event | TerminalStateChange | Frame | Update | None, core: CoreContext | None, log: ReadLog | None = None)
Bases: Generic[DepsT_co, StateT_co]
What a hook is called with.
The context carries the event and live views of where the hook is attached. Its members are
read when you use them, so a context costs almost nothing to receive. A generator hook
receives a new context each time it resumes, so context.mouse.position is always the
newest event's.
buffer, parent and root are live objects with concrete values:
context.parent.width is an int, and context.buffer has the verbs of a placement
(move, up, down, left and right). On a placement or canvas themselves,
parent and root are formula scopes whose members are expressions, such as
placement.parent.width.
keyboard, mouse, scroll and clipboard are always there; each member is
None (or 0 for the wheel) unless the event the hook is handling is of that kind.
Each has get() and push(value).
Attributes:
-
canvas(Canvas[DepsT_co, StateT_co]) –The root canvas the hook belongs to.
-
event(Event | TerminalStateChange | Frame | Update | None) –The event, a
Frameforhook(on='frame'), anUpdateforhook(on='tick'), aTerminalStateChangeforhook(on='state'), orNonewhen no event caused the call. -
hook(HookHandle[object, object]) –The registration, so the hook can call
hook.remove(). -
keyboard(KeyboardInput) –The key of the event, with
key,kindandmodifiers. -
mouse(MouseInput) –The mouse button or movement of the event, with
position,button,kindandis_down. -
scroll(ScrollInput) –The wheel steps of the event,
xandy. -
clipboard(ClipboardInput) –The pasted text of the event.
-
buffer(Placement[object, object] | Canvas[object, object]) –The placement (or the canvas) the hook is attached to.
-
parent(Placement[object, object] | Canvas[object, object]) –What contains the placement; the canvas itself for a hook on the canvas.
-
root(Canvas[DepsT_co, StateT_co]) –The root canvas.
-
plugin(Plugin) –The attachment of the hook that is running.
-
deps(DepsT_co) –The dependencies given to
new(deps=...). -
fx(EffectController) –Applies effects to the hook's buffer while the hook is active.
-
time(Time) –The canvas's
Time. -
audio(AudioDevice) –The canvas's
AudioDevice. -
cursor(Cursor) –The canvas's cursor.
-
size(Size) –The canvas's size in cells.
-
input(Input) –The keys and mouse buttons held down right now.
Examples:
import xnano
from xnano_core import Event
with xnano.new(width=20, height=3) as canvas:
label = canvas.write('hi', x=2)
heard = []
@label.hook(on='mouse', when="mouse.kind == 'press'")
def pressed(context: xnano.Context) -> None:
heard.append((context.mouse.position.x, context.buffer.x))
canvas.push(Event.mouse_event(column=3, row=0, kind='press', button='left'))
canvas.deliver_events()
assert heard == [(3, 2)]
Create a context. The runtime does this for you; hooks receive one.
Parameters:
-
canvas(Canvas[DepsT_co, StateT_co]) –The root canvas.
-
owner(Canvas[object, object] | Placement[object, object]) –The canvas or placement the hook is attached to.
-
hook(HookHandle[object, object]) –The hook that is called.
-
event(Event | TerminalStateChange | Frame | Update | None) –The event, or
None. -
core(CoreContext | None) –The runtime's context for the event, or
None. -
log(ReadLog | None, default:None) –Where to record what is read, for a component;
Nonefor a hook.
- API Reference xnano
Methods:
-
local–Convert a cell of the screen into a cell of the hook's buffer.
Source code in xnano/context.py
canvas
instance-attribute
¶
canvas: Canvas[DepsT_co, StateT_co] = canvas
The root canvas the hook belongs to.
hook
instance-attribute
¶
hook: HookHandle[object, object] = hook
The registration, so the hook can call hook.remove().
event
property
¶
keyboard
property
¶
keyboard: KeyboardInput
The key of the event; key is None unless the event is a key.
mouse
property
¶
mouse: MouseInput
The mouse button or movement of the event; position is None for any other.
scroll
property
¶
scroll: ScrollInput
The mouse wheel steps of the event; x and y are 0 unless it is a wheel.
clipboard
property
¶
clipboard: ClipboardInput
The pasted text of the event; text is None unless the event is a paste.
buffer
property
¶
The placement the hook is attached to, or the canvas for a hook on the canvas.
parent
property
¶
What contains the hook's placement: another placement, or the canvas.
local
¶
Convert a cell of the screen into a cell of the hook's buffer.
Parameters:
-
point(Point) –A cell in screen coordinates, such as
context.mouse.position.
Examples:
import xnano
from xnano_core import Point
with xnano.new(width=20, height=4) as canvas:
placement = canvas.write('hi', x=10, y=1)
@placement.hook(on='mouse')
def pressed(context: xnano.Context) -> None:
if context.mouse.position is not None:
context.local(context.mouse.position)
assert placement.local(Point(11, 1)) == Point(1, 0)
Source code in xnano/context.py
Frame
dataclass
¶
Frame(canvas: Canvas[object, object], index: int, elapsed_seconds: float, delta_seconds: float, area: Rectangle)
One frame of a canvas's clock.
Attributes:
-
canvas(Canvas[object, object]) –The canvas the frame belongs to.
-
index(int) –The number of steps the canvas's clock has counted, which is
Time.framewhen the frame was made. -
elapsed_seconds(float) –Seconds on the canvas's clock.
-
delta_seconds(float) –Seconds since the previous frame (
0.0for the first). -
area(Rectangle) –The rectangle the frame is drawn into, in cells.
index
instance-attribute
¶
index: int
The number of steps the canvas's clock has counted, which is
Time.frame when the frame was made.
Update
dataclass
¶
Update(canvas: Canvas[object, object], elapsed_seconds: float, delta_seconds: float, is_fixed_step: bool)
One step of a simulation, which runs separately from drawing.
Attributes:
-
canvas(Canvas[object, object]) –The canvas the update belongs to.
-
elapsed_seconds(float) –Seconds on the update's clock.
-
delta_seconds(float) –Seconds this update advances.
-
is_fixed_step(bool) –Whether the update runs at a fixed rate.