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 scale arg to Layer.add_label #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
153 changes: 73 additions & 80 deletions wasabi2d/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _draw(self, camera):
else:
self._draw_inner(camera)

identity = np.identity(4, dtype='f4')
identity = np.identity(4, dtype="f4")

def _draw_inner(self, camera):
self.group.shadermgr.set_proj(camera._getproj(self.parallax))
Expand All @@ -87,7 +87,7 @@ def set_effect(self, name: str, **kwargs) -> Any:
the effect.

"""
mod = importlib.import_module(f'wasabi2d.effects.{name}')
mod = importlib.import_module(f"wasabi2d.effects.{name}")
cls = getattr(mod, name.title())
self.effect = cls(self.ctx, **kwargs)
self.effect_has_camera = None
Expand All @@ -102,10 +102,10 @@ def add_sprite(
*,
pos=(0, 0),
angle=0,
anchor_x='center',
anchor_y='center',
anchor_x="center",
anchor_y="center",
color=(1, 1, 1, 1),
scale=1.0
scale=1.0,
):
spr = Sprite(
layer=self,
Expand All @@ -129,11 +129,11 @@ def _get_or_create_vao(self, k, constructor):

def _lines_vao(self):
"""Get a VAO for objects made of line strips."""
return self._get_or_create_vao('lines', line_vao)
return self._get_or_create_vao("lines", line_vao)

def _fill_vao(self):
"""Get a VAO for objects made of colored triangles."""
return self._get_or_create_vao('shapes', shape_vao)
return self._get_or_create_vao("shapes", shape_vao)

def _load_texture(self, name):
"""Load a texture."""
Expand All @@ -145,16 +145,17 @@ def _load_texture(self, name):

def _text_vao(self, font):
"""Get a VAO for objects made of font glyphs."""
return self._get_or_create_vao(('text', font), text_vao)

def add_circle(self,
*,
radius: float,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
fill: bool = True,
stroke_width: float = 1.0,
) -> Circle:
return self._get_or_create_vao(("text", font), text_vao)

def add_circle(
self,
*,
radius: float,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
fill: bool = True,
stroke_width: float = 1.0,
) -> Circle:
c = Circle(
layer=self,
radius=radius,
Expand All @@ -170,15 +171,15 @@ def add_circle(self,
return c

def add_star(
self,
*,
points: int,
inner_radius: float = 1.0,
outer_radius: float = None,
pos: Tuple[float, float] = (0, 0),
fill: bool = True,
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
stroke_width: float = 1.0,
self,
*,
points: int,
inner_radius: float = 1.0,
outer_radius: float = None,
pos: Tuple[float, float] = (0, 0),
fill: bool = True,
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
stroke_width: float = 1.0,
) -> Circle:
assert points >= 3, "Stars must have at least 3 points."

Expand All @@ -202,14 +203,15 @@ def add_star(
c.orig_verts[1::2, :2] *= inner_radius
return c

def add_polygon(self,
vertices,
*,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
fill: bool = True,
stroke_width: float = 1.0,
) -> Polygon:
def add_polygon(
self,
vertices,
*,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
fill: bool = True,
stroke_width: float = 1.0,
) -> Polygon:
c = Polygon(
layer=self,
vertices=vertices,
Expand All @@ -224,13 +226,14 @@ def add_polygon(self,
c._migrate_stroke(self._lines_vao())
return c

def add_line(self,
vertices,
*,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
stroke_width: float = 1.0,
) -> PolyLine:
def add_line(
self,
vertices,
*,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
stroke_width: float = 1.0,
) -> PolyLine:
"""Add a line strip.

To create a single line segment of two points, pass two vertices!
Expand All @@ -248,14 +251,15 @@ def add_line(self,
return c

def add_rect(
self,
width: float,
height: float,
*,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
fill: bool = True,
stroke_width: float = 1) -> Rect:
self,
width: float,
height: float,
*,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
fill: bool = True,
stroke_width: float = 1,
) -> Rect:

c = Rect(
layer=self,
Expand All @@ -272,53 +276,41 @@ def add_rect(
c._migrate_stroke(self._lines_vao())
return c

def add_label(self,
text: str,
*,
font: Optional[str] = None,
align: str = 'left',
fontsize: int = 20,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
) -> Rect:
def add_label(
self,
text: str,
*,
font: Optional[str] = None,
align: str = "left",
fontsize: int = 20,
pos: Tuple[float, float] = (0, 0),
color: Tuple[float, float, float, float] = (1, 1, 1, 1),
scale: float = 1.0,
) -> Rect:

fa = self.group.fontmgr.get(font)
c = Label(
text,
fa,
self,
align=align,
fontsize=fontsize,
pos=pos,
color=color
)
c = Label(text, fa, self, align=align, fontsize=fontsize, pos=pos, color=color)
c.scale = scale
self.objects.add(c)
return c

def add_particle_group(
self,
texture=None,
clock=default_clock,
**kwargs
self, texture=None, clock=default_clock, **kwargs
) -> ParticleGroup:
"""Create a group of particles.

We do not actually emit any particles at this time.
"""
c = ParticleGroup(
layer=self,
clock=default_clock,
**kwargs
)
c = ParticleGroup(layer=self, clock=default_clock, **kwargs)

vao = self.arrays[c] = ParticleVAO(
c,
mode=moderngl.POINTS,
ctx=self.ctx,
prog=self.group.shadermgr.load('primitives/particle')
prog=self.group.shadermgr.load("primitives/particle"),
)
if texture is None:
tex = self.ctx.texture((1, 1), 4, data=b'\xff' * 4)
tex = self.ctx.texture((1, 1), 4, data=b"\xff" * 4)
else:
tex = self._load_texture(texture)
tex.repeat_x = tex.repeat_y = False
Expand All @@ -332,6 +324,7 @@ def add_particle_group(
def add_tile_map(self, *args, **kwargs):
"""Create a tile map in the layer."""
from .primitives.tile_map import TileMap

tiles = TileMap(self, *args, **kwargs)
self.objects.add(tiles)
return tiles
Expand All @@ -343,8 +336,8 @@ def __new__(cls, ctx):

def __init__(self, ctx):
self.ctx = ctx
if 'shadermgr' in ctx.extra:
self.shadermgr = ctx.extra['shadermgr']
if "shadermgr" in ctx.extra:
self.shadermgr = ctx.extra["shadermgr"]
else:
self.shadermgr = ShaderManager(self.ctx)
self.fontmgr = FontManager(self.ctx)
Expand Down