Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Window shape that holds a surface #56

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/tempe/window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# SPDX-FileCopyrightText: 2024-present Unital Software <info@unital.dev>
#
# SPDX-License-Identifier: MIT

from .raster import Raster
from .surface import Surface
from .shapes import Shape


class Window(Shape):
"""A Shape which displays a surface."""

def __init__(self, offset=None, *, surface=None, clip=None):
super().__init__(surface, clip)
if offset is None:
if clip is not None:
offset = clip[:2]
else:
offset = (0, 0)
self.offset = offset
self.subsurface = Surface()

def draw_raster(self, raster):
x, y = self.offset
# new raster translated to subsurface coordinates
if self.clip is None:
raster = raster.clip(self.clip)
raster.x -= x
raster.y -= y
else:
raster = Raster(
raster.buf,
raster.x - x,
raster.y - y,
raster.w,
raster.h,
raster.stride,
raster.offset,
)
self.subsurface.draw(raster)

def update(self, offset=None):
if offset is None:
for rect in self.subsurface._damage:
x, y, w, h = rect
self.surface.damage(
(
(x + self.offset[0]),
(y + self.offset[1]),
w,
h,
)
)
self.subsurface._damage = []
self.subsurface.refresh_needed.clear()
elif self.clip is None:
# invalidate old drawing areas
for layer_shapes in self.subsurface.layers.values:
for shape in layer_shapes:
shape.update()
self.update()
# shift
self.offset = offset
# invalidate new drawing areas
for layer_shapes in self.subsurface.layers.values:
for shape in layer_shapes:
shape.update()
self.update()
else:
self.offset = offset
self.surface.damage(self.clip)
24 changes: 24 additions & 0 deletions src/tempe/window.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-FileCopyrightText: 2024-present Unital Software <info@unital.dev>
#
# SPDX-License-Identifier: MIT

from .raster import Raster
from .surface import Surface, rectangle
from .shapes import Shape


class Window(Shape):
"""A Shape which displays a surface."""

def __init__(
self,
offset: tuple[int, int] | None = None,
*,
surface: Surface | None = None,
clip: rectangle | None = None,
): ...

def draw_raster(self, raster: Raster):
"""Draw the Window's contents in the Raster"""

def update(self, offset: tuple[int, int] | None = None, **kwargs): ...