Skip to content

Commit

Permalink
Gadget minimum height/width is now 0 from 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Feb 26, 2024
1 parent 56dc464 commit 40d9f09
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 71 deletions.
19 changes: 12 additions & 7 deletions src/batgrl/gadgets/box_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,18 @@ def alpha(self, alpha: float):
self._image.alpha = alpha

@property
def path(self):
def path(self) -> Path | None:
"""Path to image."""
return self._path

@path.setter
def path(self, value):
self._path = value
def path(self, path: Path | None):
self._path: Path | None = path

if path is None:
self._otexture = np.zeros((1, 1, 3), dtype=np.uint8)
else:
self._otexture = cv2.imread(str(path.absolute()), cv2.IMREAD_COLOR)
self._load_texture()

def on_size(self):
Expand All @@ -219,11 +224,11 @@ def on_size(self):

def _load_texture(self):
h, w = self.size
canvas = self._image.canvas

img = cv2.imread(str(self.path.absolute()), cv2.IMREAD_COLOR)
img_bgr = cv2.resize(img, (2 * w, 2 * h))
if h == 0 or w == 0:
return

canvas = self._image.canvas
img_bgr = cv2.resize(self._otexture, (2 * w, 2 * h))
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
img_hls = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HLS)

Expand Down
19 changes: 12 additions & 7 deletions src/batgrl/gadgets/braille_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,18 @@ def alpha(self, alpha: float):
self._image.alpha = alpha

@property
def path(self):
def path(self) -> Path | None:
"""Path to image."""
return self._path

@path.setter
def path(self, value):
self._path = value
def path(self, path: Path | None):
self._path: Path | None = path

if path is None:
self._otexture = np.zeros((1, 1, 3), dtype=np.uint8)
else:
self._otexture = cv2.imread(str(path.absolute()), cv2.IMREAD_COLOR)
self._load_texture()

def on_size(self):
Expand All @@ -219,11 +224,11 @@ def on_size(self):

def _load_texture(self):
h, w = self.size
canvas = self._image.canvas

img = cv2.imread(str(self.path.absolute()), cv2.IMREAD_COLOR)
img_bgr = cv2.resize(img, (2 * w, 4 * h))
if h == 0 or w == 0:
return

canvas = self._image.canvas
img_bgr = cv2.resize(self._otexture, (2 * w, 4 * h))
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
img_hls = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HLS)

Expand Down
17 changes: 8 additions & 9 deletions src/batgrl/gadgets/braille_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ class BrailleVideo(Gadget):
gray_threshold : int, default: 127
Pixel values over this threshold in the source video will be rendered.
enable_shading : bool, default: False
If true, foreground will be set to `default_fg_color` multiplied by the
normalized grays from the source.
Whether foreground colors are shaded.
invert_colors : bool, default: False
Invert the colors in the source before rendering.
alpha : float, default: 1.0
Expand Down Expand Up @@ -90,8 +89,7 @@ class BrailleVideo(Gadget):
gray_threshold : int
Pixel values over this threshold in the source video will be rendered.
enable_shading : bool
If true, foreground will be set to `default_fg_color` multiplied by the
normalized grays from the source.
Whether foreground colors are shaded.
invert_colors : bool
If true, colors in the source are inverted before video is rendered.
alpha : float
Expand Down Expand Up @@ -315,13 +313,13 @@ def _release_resource(self):
atexit.unregister(self._resource.release)
self._resource = None
self._current_frame = None
self._video.canvas["char"] = " "
self._video.clear()

def _paint_frame(self):
if self._current_frame is None:
h, w = self.size
if self._current_frame is None or h == 0 or w == 0:
return

h, w = self.size
canvas = self._video.canvas
upscaled = cv2.resize(self._current_frame, (2 * w, 4 * h)) > self.gray_threshold
sectioned = np.swapaxes(upscaled.reshape(h, 4, w, 2), 1, 2)
Expand All @@ -332,7 +330,7 @@ def _paint_frame(self):
shades = lerp(self.bg_color, self.fg_color, normals[..., None])
canvas["fg_color"] = shades.astype(np.uint8)
else:
canvas["fg_color"] = self.default_fg_color
canvas["fg_color"] = self.fg_color

def _time_delta(self) -> float:
return time.monotonic() - self._resource.get(cv2.CAP_PROP_POS_MSEC) / 1000
Expand Down Expand Up @@ -366,6 +364,7 @@ async def _play_video(self):
self.play()
else:
self._current_frame = None
self._video.clear()

def on_size(self):
"""Resize canvas and colors arrays."""
Expand Down Expand Up @@ -412,4 +411,4 @@ def stop(self):
self.pause()
self.seek(0)
self._current_frame = None
self.clear()
self._video.clear()
8 changes: 1 addition & 7 deletions src/batgrl/gadgets/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,7 @@ def _enter_callback(textbox: Textbox):

class _Prompt(Text):
def set_text(self, text: str, **kwargs):
# To fake a 0-width prompt move the prompt offscreen when text is empty.
self._text = text
if len(text) == 0:
self.left = -1
else:
self.left = 0
super().set_text(text, **kwargs)


Expand Down Expand Up @@ -495,8 +490,7 @@ def update_bars():

def _add_text_to_output(self, text: str, with_prompt: bool = False):
if with_prompt:
prompt = self._prompt._text
text = f"{prompt}{text}"
text = f"{self._prompt._text}{text}"
else:
text = text.rstrip("\n")
if not text:
Expand Down
4 changes: 2 additions & 2 deletions src/batgrl/gadgets/gadget.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def __init__(
self.children: list[Gadget] = []

h, w = size
self._size = Size(clamp(h, 1, None), clamp(w, 1, None))
self._size = Size(clamp(h, 0, None), clamp(w, 0, None))
self._pos = Point(*pos)

if size_hint is None:
Expand Down Expand Up @@ -594,7 +594,7 @@ def size(self, size: Size):
return

h, w = size
size = Size(clamp(int(h), 1, None), clamp(int(w), 1, None))
size = Size(clamp(int(h), 0, None), clamp(int(w), 0, None))

if self.root:
self.root._render_lock.acquire()
Expand Down
17 changes: 4 additions & 13 deletions src/batgrl/gadgets/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
clamp,
lerp,
)
from .texture_tools import Interpolation, _composite
from .texture_tools import Interpolation, _composite, resize_texture

__all__ = [
"Anchor",
Expand Down Expand Up @@ -214,11 +214,7 @@ def __init__(
self.alpha = alpha

h, w = self.size
self.texture = np.full(
(2 * h, w, 4),
default_color,
dtype=np.uint8,
)
self.texture = np.full((2 * h, w, 4), default_color, dtype=np.uint8)

@property
def alpha(self) -> float:
Expand All @@ -232,13 +228,8 @@ def alpha(self, alpha: float):

def on_size(self):
"""Resize texture array."""
h, w = self._size

self.texture = cv2.resize(
self.texture,
(w, 2 * h),
interpolation=Interpolation._to_cv_enum[self.interpolation],
)
h, w = self.size
self.texture = resize_texture(self.texture, (2 * h, w), self.interpolation)

@property
def interpolation(self) -> Interpolation:
Expand Down
20 changes: 7 additions & 13 deletions src/batgrl/gadgets/image.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""An image gadget."""
from pathlib import Path

import cv2
import numpy as np
from numpy.typing import NDArray

Expand All @@ -16,7 +15,7 @@
SizeHint,
SizeHintDict,
)
from .texture_tools import read_texture
from .texture_tools import read_texture, resize_texture

__all__ = [
"Image",
Expand Down Expand Up @@ -214,23 +213,18 @@ def path(self) -> Path | None:
return self._path

@path.setter
def path(self, new_path: Path | None):
self._path = new_path
if new_path is not None:
self._otexture = read_texture(new_path)
def path(self, path: Path | None):
self._path = path
if path is None:
self._otexture = np.full((1, 1, 4), self.default_color, dtype=np.uint8)
else:
self._otexture = np.full((2, 1, 4), self.default_color, dtype=np.uint8)

self._otexture = read_texture(path)
self.on_size()

def on_size(self):
"""Resize texture array."""
h, w = self._size
self.texture = cv2.resize(
self._otexture,
(w, 2 * h),
interpolation=Interpolation._to_cv_enum[self.interpolation],
)
self.texture = resize_texture(self._otexture, (2 * h, w), self.interpolation)

@classmethod
def from_texture(
Expand Down
4 changes: 2 additions & 2 deletions src/batgrl/gadgets/line_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,10 @@ def y_label(self, y_label: str | None):
self._build_plot()

def _build_plot(self):
if self.root is None:
h, w = self.size
if self.root is None or h == 0 or w == 0:
return

h, w = self.size
has_x_label = self._x_label_gadget.is_enabled = bool(self.x_label is not None)
has_y_label = self._y_label_gadget.is_enabled = bool(self.y_label is not None)

Expand Down
5 changes: 4 additions & 1 deletion src/batgrl/gadgets/raycaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ def cast_sprites(self):

def _render(self, canvas_view: NDArray[Cell]):
"""Render visible region of gadget."""
h, w = self.size
if h == 0 or w == 0:
return

# Early calculations on rays can be vectorized:
np.dot(self._ray_angles, self.camera._plane, out=self._rotated_angles)
with np.errstate(divide="ignore"):
Expand All @@ -637,7 +641,6 @@ def _render(self, canvas_view: NDArray[Cell]):
np.multiply(self._sides, self._steps, out=self._sides)
np.multiply(self._sides, self._deltas, out=self._sides)

h = self.texture.shape[0] // 2
self.texture[:h, :, :3] = self.ceiling_color
self.texture[h:, :, :3] = self.floor_color
self.texture[..., 3] = 255
Expand Down
2 changes: 0 additions & 2 deletions src/batgrl/gadgets/scroll_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,8 +721,6 @@ def scroll_to_rect(self, pos: Point, size: Size = Size(1, 1)):

y, x = pos
h, w = size
h = clamp(h, 1, None)
w = clamp(w, 1, None)
gy, gx = self.view.pos
ay, ax = gy + y, gx + x
if ay < 0:
Expand Down
2 changes: 2 additions & 0 deletions src/batgrl/gadgets/shadow_caster.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ def __init__(
def cast_shadows(self):
"""Update texture by shadow casting all light sources."""
h, w, _ = self.texture.shape
if h == 0 or w == 0:
return

cy, cx = self.camera.pos
ch, cw = self.camera.size
Expand Down
2 changes: 1 addition & 1 deletion src/batgrl/gadgets/split_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def min_split_height(self) -> int:
@min_split_height.setter
def min_split_height(self, min_split_height: int):
self._min_split_height = clamp(min_split_height, 1, None)
self.split_row = self.split_row # Clamp split and call `on_size`
self.split_row = self.split_row

@property
def split_row(self) -> int:
Expand Down
5 changes: 4 additions & 1 deletion src/batgrl/gadgets/texture_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def resize_texture(
Parameters
----------
texture : NDArray[np.uint8]
The texture to resize.
An RGBA texture to resize.
size : Size
The new size of the texture.
interpolation : Interpolation, default: "linear"
Expand All @@ -72,7 +72,10 @@ def resize_texture(
NDArray[np.uint8]
A new uint8 RGBA array.
"""
old_h, old_w, _ = texture.shape
h, w = size
if old_h == 0 or old_w == 0 or h == 0 or w == 0:
return np.zeros((h, w, 4), np.uint8)
return cv2.resize(
texture,
(w, h),
Expand Down
14 changes: 8 additions & 6 deletions src/batgrl/gadgets/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,14 @@ def _time_delta(self) -> float:

def _display_current_frame(self):
h, w = self.size
if self._current_frame is not None:
self.texture = cv2.resize(
self._current_frame,
(w, 2 * h),
interpolation=Interpolation._to_cv_enum[self.interpolation],
)
if self._current_frame is None or h == 0 or w == 0:
return

self.texture = cv2.resize(
self._current_frame,
(w, 2 * h),
interpolation=Interpolation._to_cv_enum[self.interpolation],
)

async def _play_video(self):
if self._resource is None:
Expand Down

0 comments on commit 40d9f09

Please sign in to comment.