-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix layers, fix animation defaults, make docs for both, incremenet de…
…signer
- Loading branch information
Showing
11 changed files
with
273 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import weakref | ||
from collections.abc import MutableMapping | ||
|
||
|
||
class MultiWeakKeyDict(MutableMapping): | ||
""" | ||
https://stackoverflow.com/a/74270121/1718155 | ||
A dictionary that uses weak references to keys, and can have multiple keys | ||
""" | ||
def __init__(self, initial_data=None): | ||
self.data = {} | ||
self.helpers = {} | ||
|
||
if initial_data is not None: | ||
for key, value in initial_data.items(): | ||
self[key] = value | ||
|
||
def _remove(self, wref): | ||
for data_key in self.helpers.pop(wref, ()): | ||
try: | ||
del self.data[data_key] | ||
except KeyError: | ||
pass | ||
|
||
def _build_key(self, keys): | ||
if isinstance(keys, tuple): | ||
return tuple(weakref.ref(item, self._remove) if not isinstance(item, str) else item | ||
for item in keys) | ||
else: | ||
return (weakref.ref(keys, self._remove) if not isinstance(keys, str) else keys | ||
,) | ||
|
||
def __setitem__(self, keys, value): | ||
weakrefs = self._build_key(keys) | ||
for item in weakrefs: | ||
self.helpers.setdefault(item, set()).add(weakrefs) | ||
self.data[weakrefs] = value | ||
|
||
def __getitem__(self, keys): | ||
return self.data[self._build_key(keys)] | ||
|
||
def __delitem__(self, keys): | ||
del self.data[self._build_key(keys)] | ||
|
||
def __iter__(self): | ||
for key in self.data: | ||
yield tuple(item if isinstance(item, str) else item() | ||
for item in key) | ||
|
||
def __len__(self): | ||
return len(self.data) | ||
|
||
def __repr__(self): | ||
return f"{self.__class__.__name__}({', '.join('{!r}:{!r}'.format(k, v) for k, v in self.items())})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from designer import * | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class World: | ||
spinner: DesignerObject | ||
|
||
def create_world() -> World: | ||
""" Create the world """ | ||
spinner = emoji("dog") | ||
linear_animation(spinner, 'angle', 0, 360, 3, loop=True) | ||
sequence_animation(spinner, 'name', ['dog', 'cat', 'frog', 'dragon', 'sheep'], 5, loop=True) | ||
return World(spinner) | ||
|
||
|
||
when('starting', create_world) | ||
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from designer import * | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class World: | ||
box: DesignerObject | ||
label: DesignerObject | ||
box2: DesignerObject | ||
label2: DesignerObject | ||
green_circle: DesignerObject | ||
orange_circle: DesignerObject | ||
|
||
set_window_layers(['background', 'foreground']) | ||
|
||
|
||
@starting | ||
def create_world() -> World: | ||
box = rectangle('pink', 20, 50) | ||
label = text('black', "Background Box", 20) | ||
box.size = label.size | ||
box2 = rectangle('cornflowerblue', 50, 20) | ||
label2 = text('black', "Foreground Box", 20) | ||
box2.size = label2.size | ||
box.layer = 'background' | ||
label.layer = 'background:above' | ||
box2.layer = 'foreground' | ||
label2.layer = 'foreground:above' | ||
# Make circle in background layer appear behind everything else | ||
green_circle = circle('green', 200, 200) | ||
green_circle.layer = 'background:bottom' | ||
orange_circle = circle('orange', 5) | ||
orange_circle.layer = 'foreground:top' | ||
|
||
linear_animation(box2, 'x', 0, get_width(), 3, loop=True) | ||
linear_animation(label2, 'x', 0, get_width(), 3, loop=True) | ||
|
||
linear_animation(box, 'y', 0, get_height(), 3, loop=True) | ||
linear_animation(label, 'y', 0, get_height(), 3, loop=True) | ||
|
||
return World(box, label, box2, label2, green_circle, orange_circle) | ||
|
||
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from designer import * | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class World: | ||
dog: DesignerObject | ||
cat: DesignerObject | ||
frog: DesignerObject | ||
dragon: DesignerObject | ||
sheep: DesignerObject | ||
|
||
def create_world() -> World: | ||
""" Create the world """ | ||
dog = emoji("dog") | ||
cat = emoji("cat") | ||
frog = emoji("frog") | ||
dragon = emoji("dragon") | ||
sheep = emoji("sheep") | ||
|
||
# Cat -> Frog -> Dog | ||
dog.layer = 'top' | ||
cat.layer = 'bottom' | ||
frog.pos = cat.pos = dog.pos = [get_width() / 3, get_height() / 3] | ||
cat.x -= 10 | ||
dog.x += 10 | ||
|
||
# Sheep and Dragon layered | ||
set_window_layers(['first', 'second']) | ||
sheep.layer = 'first' | ||
dragon.layer = 'second' | ||
|
||
return World(dog, cat, frog, dragon, sheep) | ||
|
||
def swap_layers(world: World): | ||
""" Swap the layers of the dog and cat """ | ||
world.dog.layer, world.cat.layer = world.cat.layer, world.dog.layer | ||
|
||
world.sheep.layer, world.dragon.layer = world.dragon.layer, world.sheep.layer | ||
|
||
when('starting', create_world) | ||
when('clicking', swap_layers) | ||
start() |