batgrl.gadgets.shadow_caster#
A shadow caster gadget.
Module Attributes
The restrictiveness of the shadow caster. |
Classes
|
A light source. |
|
A restrictive precise angle shadowcaster. |
|
The camera determines the visible portion of the map. |
|
A continuous interval. |
- class batgrl.gadgets.shadow_caster.LightSource(coords: tuple[float, float] = (0.0, 0.0), color: Color = (255, 255, 255))#
Bases:
object
A light source.
- Parameters:
- coordsfloat[tuple, tuple], default: (0.0, 0.0)
Coordinates of light source on map.
- colorColor, default: WHITE
Color of light source.
- Attributes:
- coordsfloat[tuple, tuple]
Coordinates of light source on map.
- colorColor
Color of light source.
- coords: tuple[float, float]#
- batgrl.gadgets.shadow_caster.Restrictiveness#
The restrictiveness of the shadow caster.
Restrictiveness
is one of “permissive”, “moderate”, “restrictive”.For “permissive”, any interval is visible as long as any of it’s start, center, or end points are visible. For “moderate”, the center and either end must be visible. For “restrictive”, all points in the interval must be visible.
alias of
Literal
[‘permissive’, ‘moderate’, ‘reestrictive’]
- class batgrl.gadgets.shadow_caster.ShadowCaster(*, map: ~numpy.ndarray[~typing.Any, ~numpy.dtype[~numpy.uint32]], camera: ~batgrl.gadgets.shadow_caster.ShadowCasterCamera, tile_colors: list[~batgrl.colors.color_types.AColor] | None = None, light_sources: list[~batgrl.gadgets.shadow_caster.LightSource] | None = None, ambient_light: ~batgrl.colors.color_types.Color = (0, 0, 0), restrictiveness: ~typing.Literal['permissive', 'moderate', 'reestrictive'] = 'moderate', light_decay: ~collections.abc.Callable[[float], float] = <function ShadowCaster.<lambda>>, radius: int = 20, smoothing: float = 0.3333333333333333, not_visible_blocks: bool = True, is_transparent: bool = True, default_color: ~batgrl.colors.color_types.AColor = (0, 0, 0, 0), alpha: float = 1.0, interpolation: ~typing.Literal['nearest', 'linear', 'cubic', 'area', 'lanczos'] = 'linear', size: ~batgrl.geometry.basic.Size = (10, 10), pos: ~batgrl.geometry.basic.Point = (0, 0), size_hint: ~batgrl.gadgets.gadget.SizeHint | None = None, pos_hint: ~batgrl.gadgets.gadget.PosHint | None = None, is_visible: bool = True, is_enabled: bool = True)#
Bases:
Graphics
A restrictive precise angle shadowcaster.
cast_shadows()
must be called to generate or updatetexture
.- Parameters:
- mapNDArray[np.uint32]
A 2-d map. Non-zero values are walls.
- cameraShadowCasterCamera
A camera that determines the visible portion of the map.
- tile_colorslist[AColor] | None, default: None
A value n in the map will be colored
tile_colors[n]
. IfNone
, tile_colors will be set to[AGRAY, AWHITE]
.- light_sourceslist[LightSource] | None, default: None
A list of all light sources on the map.
- ambient_lightColor, default: BLACK
Color of ambient light. Default is no light.
- restrictivenessRestrictiveness, default: “moderate”
Restrictiveness of casting algorithm.
- light_decayCallable[[float], float], default: lambda d: 1 if d == 0 else 1 / d
The strength of light as a function of distance from origin.
- radiusint, default: 20
Max visible radius from a light source.
- smoothingfloat, default: 1.0 / 3.0
Smoothness of shadow edges. This value will be clamped between 0 and 1.
- not_visible_blocksbool, default: True
Whether all not-visible cells will be treated as opaque.
- default_colorAColor, default: AColor(0, 0, 0, 0)
Default texture color.
- alphafloat, default: 1.0
Transparency of gadget.
- interpolationInterpolation, default: “linear”
Interpolation used when gadget is resized.
- sizeSize, default: Size(10, 10)
Size of gadget.
- posPoint, default: Point(0, 0)
Position of upper-left corner in parent.
- size_hintSizeHint | None, default: None
Size as a proportion of parent’s height and width.
- pos_hintPosHint | None, default: None
Position as a proportion of parent’s height and width.
- is_transparentbool, default: True
Whether gadget is transparent.
- is_visiblebool, default: True
Whether gadget is visible. Gadget will still receive input events if not visible.
- is_enabledbool, default: True
Whether gadget is enabled. A disabled gadget is not painted and doesn’t receive input events.
- Attributes:
- mapNDArray[np.uint32]
A 2-d map. Non-zero values are walls.
- cameraShadowCasterCamera
A camera that determines the visible portion of the map.
- tile_colorslist[AColor]
A value n in the map will be colored
tile_colors[n]
.- light_sourceslist[LightSource]
A list of all light sources on the map.
- ambient_lightColor
Color of ambient light.
- restrictivenessRestrictiveness
Restrictiveness of casting algorithm.
- light_decayCallable[[float], float]
The strength of light as a function of distance from origin.
- radiusint
Max visible radius from a light source.
- smoothingfloat
Smoothness of shadow edges.
- not_visible_blocksbool
Whether all not-visible cells will be treated as opaque.
- textureNDArray[np.uint8]
uint8 RGBA color array.
- default_colorAColor
Default texture color.
alpha
floatTransparency of gadget.
interpolation
InterpolationInterpolation used when gadget is resized.
size
SizeSize of gadget.
height
intHeight of gadget.
rows
intHeight of gadget.
width
intWidth of gadget.
columns
intWidth of gadget.
pos
PointPosition relative to parent.
top
inty-coordinate of top of gadget.
y
inty-coordinate of top of gadget.
left
intx-coordinate of left side of gadget.
x
intx-coordinate of left side of gadget.
bottom
inty-coordinate of bottom of gadget.
right
intx-coordinate of right side of gadget.
center
PointPosition of center of gadget.
absolute_pos
PointAbsolute position on screen.
size_hint
SizeHintGadget’s size as a proportion of its parent’s size.
pos_hint
PosHintGadget’s position as a proportion of its parent’s size.
- parent: Gadget | None
Parent gadget.
- childrenlist[Gadget]
Children gadgets.
is_transparent
boolWhether gadget is transparent.
is_visible
boolWhether gadget is visible.
is_enabled
boolWhether gadget is enabled.
root
Gadget | NoneReturn the root gadget if connected to gadget tree.
app
AppThe running app.
Methods
cast_shadows()
Update texture by shadow casting all light sources.
to_map_coords(point)
Convert a point in the gadget’s local coordinates to a point in the map’s coordinates.
to_png(path)
Write
texture
to provided path as a png image.clear()
Fill texture with default color.
apply_hints()
Apply size and pos hints.
to_local(point)
Convert point in absolute coordinates to local coordinates.
collides_point(point)
Return true if point collides with visible portion of gadget.
collides_gadget(other)
Return true if other is within gadget’s bounding box.
pull_to_front()
Move to end of gadget stack so gadget is drawn last.
walk()
Yield all descendents of this gadget (preorder traversal).
walk_reverse()
Yield all descendents of this gadget (reverse postorder traversal).
ancestors()
Yield all ancestors of this gadget.
add_gadget(gadget)
Add a child gadget.
add_gadgets(*gadgets)
Add multiple child gadgets.
remove_gadget(gadget)
Remove a child gadget.
prolicide()
Recursively remove all children.
destroy()
Remove this gadget and recursively remove all its children.
bind(prop, callback)
Bind callback to a gadget property.
unbind(uid)
Unbind a callback from a gadget property.
tween(…)
Sequentially update gadget properties over time.
on_size()
Update gadget after a resize.
on_transparency()
Update gadget after transparency is enabled/disabled.
on_add()
Update gadget after being added to the gadget tree.
on_remove()
Update gadget after being removed from the gadget tree.
on_key(key_event)
Handle a key press event.
on_mouse(mouse_event)
Handle a mouse event.
on_paste(paste_event)
Handle a paste event.
on_terminal_focus(focus_event)
Handle a focus event.
- add_gadget(gadget: Self) None #
Add a child gadget.
- Parameters:
- gadgetGadget
A gadget to add as a child.
- add_gadgets(*gadgets: Self) None #
Add multiple child gadgets.
- Parameters:
- *gadgetsGadget
Gadgets to add as children. Can also accept a single iterable of gadgets.
- property alpha: float#
Transparency of gadget.
- ancestors() Iterator[Self] #
Yield all ancestors of this gadget.
- Yields:
- Gadget
An ancestor of this gadget.
- property app#
The running app.
- apply_hints() None #
Apply size and pos hints.
This is called automatically when the gadget is added to the gadget tree and when the gadget’s parent’s size changes.
- bind(prop: str, callback: Callable[[], None]) int #
Bind callback to a gadget property. When the property is updated, callback is called with no arguments.
- Parameters:
- propstr
The name of the gadget property.
- callbackCallable[[], None]
Callback to bind to property.
- Returns:
- int
A unique id used to unbind the callback.
- property bottom: int#
y-coordinate of bottom of gadget.
- cast_shadows()#
Update texture by shadow casting all light sources.
- children: _GadgetList#
The gadget’s children.
- clear()#
Fill texture with default color.
- collides_gadget(other: Self) bool #
Return true if other is within gadget’s bounding box.
- Parameters:
- otherGadget
Another gadget.
- Returns:
- bool
Whether other collides with gadget.
- collides_point(point: Point) bool #
Return true if point collides with visible portion of gadget.
- Parameters:
- pointPoint
A point.
- Returns:
- bool
Whether point collides with gadget.
- property columns: int#
Width of gadget.
- destroy() None #
Remove this gadget and recursively remove all its children.
- dispatch_key(key_event: KeyEvent) bool | None #
Dispatch a key press event until handled.
A key press event is handled if a handler returns
True
.- Parameters:
- key_eventKeyEvent
The key event to dispatch.
- Returns:
- bool | None
Whether the dispatch was handled.
- dispatch_mouse(mouse_event: MouseEvent) bool | None #
Dispatch a mouse event until handled.
A mouse event is handled if a handler returns
True
.- Parameters:
- mouse_eventMouseEvent
The mouse event to dispatch.
- Returns:
- bool | None
Whether the dispatch was handled.
- dispatch_paste(paste_event: PasteEvent) bool | None #
Dispatch a paste event until handled.
A paste event is handled if a handler returns
True
.- Parameters:
- paste_eventPasteEvent
The paste event to dispatch.
- Returns:
- bool | None
Whether the dispatch was handled.
- dispatch_terminal_focus(focus_event: FocusEvent) bool | None #
Dispatch a focus event until handled.
A focus event is handled if a handler returns
True
.- Parameters:
- focus_eventFocusEvent
The focus event to dispatch.
- Returns:
- bool | None
Whether the dispatch was handled.
- property height: int#
Height of gadget.
- property interpolation: Literal['nearest', 'linear', 'cubic', 'area', 'lanczos']#
Interpolation used when gadget is resized.
- property is_enabled: bool#
Whether gadget is enabled. A disabled gadget is not painted and doesn’t receive input events.
- property is_transparent: bool#
Whether gadget is transparent.
- property is_visible: bool#
Whether gadget is visible. Gadget will still receive input events if not visible.
- property left: int#
x-coordinate of left side of gadget.
- on_add() None #
Update gadget after being added to the gadget tree.
- on_key(key_event: KeyEvent) bool | None #
Handle a key press event.
Handled key presses should return
True
.- Parameters:
- key_eventKeyEvent
The key event to handle.
- Returns:
- bool | None
Whether the key event was handled.
- on_mouse(mouse_event: MouseEvent) bool | None #
Handle a mouse event.
Handled mouse events should return
True
.- Parameters:
- mouse_eventMouseEvent
The mouse event to handle.
- Returns:
- bool | None
Whether the mouse event was handled.
- on_paste(paste_event: PasteEvent) bool | None #
Handle a paste event.
Handled paste events should return
True
.- Parameters:
- paste_eventPasteEvent
The paste event to handle.
- Returns:
- bool | None
Whether the paste event was handled.
- on_remove() None #
Update gadget after being removed from the gadget tree.
- on_size()#
Resize texture array.
- on_terminal_focus(focus_event: FocusEvent) bool | None #
Handle a focus event.
Handled focus events should return
True
.- Parameters:
- focus_eventFocusEvent
The focus event to handle.
- Returns:
- bool | None
Whether the focus event was handled.
- on_transparency() None #
Update gadget after transparency is enabled/disabled.
- prolicide() None #
Recursively remove all children.
- pull_to_front() None #
Move gadget to end of gadget stack so that it is drawn last.
- remove_gadget(gadget: Self) None #
Remove a child gadget.
- Parameters:
- gadgetGadget
The gadget to remove from children.
- property right: int#
x-coordinate of right side of gadget.
- property root: Self | None#
Return the root gadget if connected to gadget tree.
- property rows: int#
Height of gadget.
- to_local(point: Point) Point #
Convert point in absolute coordinates to local coordinates.
- Parameters:
- pointPoint
Point in absolute (screen) coordinates.
- Returns:
- Point
The point in local coordinates.
- to_map_coords(point: Point) tuple[float, float] #
Convert a point in the gadget’s local coordinates to a point in the map’s coordinates.
- Parameters:
- pointPoint
Point in local coordinates.
- Returns:
- tuple[float, float]
The coordinates of the point in the map.
- to_png(path: Path)#
Write
texture
to provided path as a png image.
- property top: int#
y-coordinate of top of gadget.
- async tween(*, duration: float = 1.0, easing: Literal['linear', 'in_quad', 'out_quad', 'in_out_quad', 'in_cubic', 'out_cubic', 'in_out_cubic', 'in_quart', 'out_quart', 'in_out_quart', 'in_quint', 'out_quint', 'in_out_quint', 'in_sine', 'out_sine', 'in_out_sine', 'in_exp', 'out_exp', 'in_out_exp', 'in_circ', 'out_circ', 'in_out_circ', 'in_elastic', 'out_elastic', 'in_out_elastic', 'in_back', 'out_back', 'in_out_back', 'in_bounce', 'out_bounce', 'in_out_bounce'] = 'linear', on_start: Callable[[], None] | None = None, on_progress: Callable[[float], None] | None = None, on_complete: Callable[[], None] | None = None, **properties: Real | ndarray[Any, dtype[number]] | Sequence[Real] | PosHint | SizeHint) None #
Coroutine that sequentially updates gadget properties over a duration (in seconds).
- Parameters:
- durationfloat, default: 1.0
The duration of the tween in seconds.
- easingEasing, default: “linear”
The easing used for tweening.
- on_startCallable[[], None] | None, default: None
Called when tween starts.
- on_progressCallable[[float], None] | None, default: None
Called as tween updates with current progress.
- on_completeCallable[[], None] | None, default: None
Called when tween completes.
- **propertiesReal | NDArray[np.number] | Sequence[Real] | PosHint | SizeHint
Gadget properties’ target values. E.g., to smoothly tween a gadget’s position to (5, 10) over 2.5 seconds, specify the pos property as a keyword-argument:
await gadget.tween(pos=(5, 10), duration=2.5, easing="out_bounce")
Warning
Running several tweens on the same properties concurrently will probably result in unexpected behavior.
Notes
Tweened values will be coerced to match the type of the initial value of their corresponding property.
Non-numeric values will be set immediately.
- unbind(uid: int) None #
Unbind a callback from a gadget property.
- Parameters:
- uidint
Unique id returned by the
bind()
method.
- walk() Iterator[Self] #
Yield all descendents of this gadget (preorder traversal).
- Yields:
- Gadget
A descendent of this gadget.
- walk_reverse() Iterator[Self] #
Yield all descendents of this gadget (reverse postorder traversal).
- Yields:
- Gadget
A descendent of this gadget.
- property width: int#
Width of gadget.
- property x: int#
x-coordinate of left side of gadget.
- property y: int#
y-coordinate of top of gadget.
- class batgrl.gadgets.shadow_caster.ShadowCasterCamera(pos: Point, size: Size)#
Bases:
object
The camera determines the visible portion of the map.
If the camera’s size is not equal to the caster’s size, the camera will resize the visible portion of the map to fill the caster’s size. To prevent resizing the visible portion of the map, the camera’s size should be equal to the caster’s size.
- Parameters:
- posPoint
The position of the upper-left corner of the camera on the map.
- sizeSize
Size of the camera.
- Attributes:
- pos
- size
Methods
get_submap(map)
Get the section of the map visible by the camera.
Notes
Submap values for areas of the camera that are out-of-bounds of the map will be zero.
- get_submap(map: ndarray[Any, dtype[uint32]]) ndarray[Any, dtype[uint32]] #
Get the section of a map visible by the camera.
- Returns:
- NDArray[np.uint32]
Section of map visible by the camera.