Skip to content

Commit

Permalink
Replace StrEnum types with Literal types.
Browse files Browse the repository at this point in the history
Several StrEnum types have been replaced with literal
types, including `Anchor` which breaks everything.
This is motivated by a desire to simplify the public api
of this library.
  • Loading branch information
salt-die committed Sep 16, 2023
1 parent 0737c5a commit bc409fe
Show file tree
Hide file tree
Showing 68 changed files with 359 additions and 410 deletions.
8 changes: 2 additions & 6 deletions examples/advanced/connect4/connect4/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ def __init__(self):
self._board = Board()
h, w = self._board.size

super().__init__(
size=(h + 2, w),
pos_hint=(0.5, 0.5),
anchor="center",
)
super().__init__(size=(h + 2, w), pos_hint=(0.5, 0.5))

self._label = TextWidget(size=(1, 10), pos_hint=(None, 0.5), anchor="center")
self._label = TextWidget(size=(1, 10), pos_hint=(None, 0.5))
self.add_widgets(self._board, self._label)

def on_add(self):
Expand Down
8 changes: 3 additions & 5 deletions examples/advanced/doom_fire.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from nurses_2.colors import Color, ColorPair
from nurses_2.widgets.graphic_widget import GraphicWidget
from nurses_2.widgets.slider import Slider
from nurses_2.widgets.text_widget import Anchor, TextWidget
from nurses_2.widgets.text_widget import TextWidget
from nurses_2.widgets.widget import Widget

FIRE_PALETTE = np.array(
Expand Down Expand Up @@ -133,7 +133,7 @@ async def on_start(self):
strength_label = TextWidget(
size=(1, 22),
pos_hint=(None, 0.5),
anchor=Anchor.TOP_CENTER,
anchor="top",
default_color_pair=SLIDER_DEFAULT,
)
strength_label.add_str(f"Current Strength: {doomfire.fire_strength:2d}", (0, 1))
Expand All @@ -153,9 +153,7 @@ def slider_update(v):
handle_color_pair=SLIDER_HANDLE,
)

slider_container = Widget(
size=(2, 38), pos_hint=(0, 0.5), anchor=Anchor.TOP_CENTER
)
slider_container = Widget(size=(2, 38), pos_hint=(0, 0.5), anchor="top")
slider_container.add_widgets(strength_label, slider)
self.add_widgets(doomfire, slider_container)

Expand Down
5 changes: 1 addition & 4 deletions examples/advanced/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ async def on_start(self):
size=button_canvas.shape,
pos=(10, 0),
pos_hint=(None, 0.5),
anchor="center",
is_transparent=True,
)
button.canvas["char"] = button_canvas
Expand All @@ -140,9 +139,7 @@ async def on_start(self):
banner = TextWidget(size=(h, w), is_transparent=True)
banner.canvas["char"] = banner_canvas

bleed = Bleed(
banner, button, size=(h + 9, w), pos_hint=(0.5, 0.5), anchor="center"
)
bleed = Bleed(banner, button, size=(h + 9, w), pos_hint=(0.5, 0.5))

self.add_widget(bleed)

Expand Down
4 changes: 1 addition & 3 deletions examples/advanced/hack/hack/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ async def on_start(self):

modal.memory = memory

terminal = Image(
path=TERMINAL, size=(36, 63), pos_hint=(0.5, 0.5), anchor="center"
)
terminal = Image(path=TERMINAL, size=(36, 63), pos_hint=(0.5, 0.5))
container = BOLDCRT(
size=(22, 53),
pos=(5, 5),
Expand Down
1 change: 0 additions & 1 deletion examples/advanced/hack/hack/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __init__(self, **kwargs):
self.message_box = TextWidget(
size=(6, 20),
pos_hint=(0.5, 0.5),
anchor="center",
default_color_pair=DEFAULT_COLOR_PAIR,
)
self.message_box.add_border("heavy")
Expand Down
6 changes: 3 additions & 3 deletions examples/advanced/io_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,14 @@ def on_mouse(self, mouse_event):

class InputApp(App):
async def on_start(self):
keyboard = KeyboardWidget(pos_hint=(0.5, 0), anchor="left_center")
mouse = MouseWidget(pos_hint=(0.5, 1.0), anchor="right_center")
keyboard = KeyboardWidget(pos_hint=(0.5, 0), anchor="left")
mouse = MouseWidget(pos_hint=(0.5, 1.0), anchor="right")

container_size = (
max(keyboard.height, mouse.height),
keyboard.width + mouse.width + 2,
)
container = Widget(size=container_size, pos_hint=(0.5, 0.5), anchor="center")
container = Widget(size=container_size, pos_hint=(0.5, 0.5))
container.add_widgets(keyboard, mouse)
self.add_widget(container)

Expand Down
11 changes: 10 additions & 1 deletion examples/advanced/minesweeper/minesweeper/minefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from nurses_2.io import MouseButton, MouseEventType

from .colors import *
from .colors import FLAG_COLOR, HIDDEN, HIDDEN_REVERSED
from .grid import Grid
from .unicode_chars import FLAG

Expand Down Expand Up @@ -232,3 +232,12 @@ def render(self, canvas_view, colors_view, source: tuple[slice, slice]):
colors_view[visible] = self.colors[source][visible]

self.render_children(source, canvas_view, colors_view)

if self.hidden_cells.sum() == self.nmines:
self._game_over(win=True)
return

def _game_over(self, win: bool):
self.hidden[:] = 0
self._is_gameover = True
self.parent.game_over(win=win)
16 changes: 12 additions & 4 deletions examples/advanced/minesweeper/minesweeper/minesweeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
import numpy as np

from nurses_2.widgets.behaviors.button_behavior import ButtonBehavior
from nurses_2.widgets.text_widget import Anchor, Point, TextWidget
from nurses_2.widgets.text_widget import Point, TextWidget
from nurses_2.widgets.widget import Widget

from .colors import DATA_BAR, FLAG_COLOR
from .count import Count
from .grid import Grid
from .minefield import Minefield
from .unicode_chars import *
from .unicode_chars import (
BAD_FLAG,
BOMB,
COOL,
EXPLODED,
FLAG,
HAPPY,
KNOCKED_OUT,
SURPRISED,
)

SIZE = 16, 30
NMINES = 99
Expand Down Expand Up @@ -47,7 +56,7 @@ def __init__(self, pos=Point(0, 0), **kwargs):

self.timer = TextWidget(
size=(1, 20),
anchor=Anchor.TOP_RIGHT,
anchor="top-right",
pos_hint=(None, 0.95),
default_color_pair=DATA_BAR,
)
Expand All @@ -64,7 +73,6 @@ def __init__(self, pos=Point(0, 0), **kwargs):
self.reset_button = ResetButton(
size=(1, 2),
default_color_pair=DATA_BAR,
anchor=Anchor.CENTER,
pos_hint=(None, 0.5),
)

Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/palettes.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def update_palette():

hue_selector.callback = slope_selector.callback = update_palette

container = Widget(size=(H + 2, W), pos_hint=(0.5, 0.5), anchor="center")
container = Widget(size=(H + 2, W), pos_hint=(0.5, 0.5))
container.add_widgets(hue_selector, slope_selector, palette)
self.add_widget(container)

Expand Down
23 changes: 8 additions & 15 deletions examples/advanced/rigid_body_physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
Requires `pymunk`
"""
import asyncio
from enum import Enum
from math import ceil, degrees
from pathlib import Path
from typing import Literal

import cv2
import numpy as np
Expand All @@ -23,13 +23,6 @@
)
from nurses_2.widgets.image import Image


class RenderMode(str, Enum):
OUTLINE = "outline"
FILL = "fill"
SPRITE = "sprite"


BOX_SIZE = W, H = Vec2d(9, 9)
BOX_MASS = 0.1
BALL_MASS = 100
Expand All @@ -54,14 +47,14 @@ def __init__(
self,
space: pymunk.Space,
dt: float = 0.01,
render_mode: RenderMode = RenderMode.SPRITE,
render_mode: Literal["outline", "fill", "sprite"] = "sprite",
shape_color: AColor = AWHITE,
**kwargs,
):
super().__init__(**kwargs)
self.space = space
self.dt = dt
self.render_mode = RenderMode(render_mode)
self.render_mode = render_mode
self.shape_color = shape_color

def stop_simulation(self):
Expand Down Expand Up @@ -91,7 +84,7 @@ def _draw_space(self):

for shape in self.space.shapes:
if isinstance(shape, pymunk.shapes.Segment):
if self.render_mode is not RenderMode.SPRITE:
if self.render_mode != "sprite":
a = shape.a.rotated(shape.body.angle) + shape.body.position
b = shape.b.rotated(shape.body.angle) + shape.body.position
cv2.line(
Expand All @@ -101,7 +94,7 @@ def _draw_space(self):
self.shape_color,
)
elif isinstance(shape, pymunk.shapes.Poly):
if self.render_mode is RenderMode.SPRITE:
if self.render_mode == "sprite":
angle = degrees(shape.body.angle)
rot = cv2.getRotationMatrix2D(BOX_CENTER, angle, 1)
box = cv2.boxPoints((BOX_CENTER, BOX_SIZE, angle))
Expand All @@ -127,7 +120,7 @@ def _draw_space(self):
for vertex in shape.get_vertices()
]
)
if self.render_mode is RenderMode.FILL:
if self.render_mode == "fill":
cv2.fillPoly(self.texture, [vertices], color=self.shape_color)
else:
cv2.polylines(
Expand All @@ -137,7 +130,7 @@ def _draw_space(self):
color=self.shape_color,
)
elif isinstance(shape, pymunk.shapes.Circle):
if self.render_mode is RenderMode.SPRITE:
if self.render_mode == "sprite":
angle == degrees(shape.body.angle)
rot = cv2.getRotationMatrix2D(BALL_CENTER, angle, 1)
src = cv2.warpAffine(BALL, rot, BALL_SIZE)
Expand All @@ -149,7 +142,7 @@ def _draw_space(self):
pos = shape.body.position
center = to_tex_coords(pos)
circle_edge = pos + Vec2d(shape.radius, 0).rotated(shape.body.angle)
if self.render_mode is RenderMode.FILL:
if self.render_mode == "fill":
cv2.circle(
self.texture,
center,
Expand Down
7 changes: 2 additions & 5 deletions examples/advanced/sandbox/sandbox/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from nurses_2.colors import ABLACK, ColorPair
from nurses_2.io import MouseButton
from nurses_2.widgets.graphic_widget import Anchor, GraphicWidget, Size
from nurses_2.widgets.graphic_widget import GraphicWidget, Size
from nurses_2.widgets.text_widget import TextWidget

from .element_buttons import MENU_BACKGROUND_COLOR, ButtonContainer
Expand All @@ -25,9 +25,7 @@ class Sandbox(GraphicWidget):
"""

def __init__(self, size: Size):
super().__init__(
size=size, anchor=Anchor.CENTER, pos_hint=(0.5, 0.5), default_color=ABLACK
)
super().__init__(size=size, pos_hint=(0.5, 0.5), default_color=ABLACK)

def on_add(self):
super().on_add()
Expand All @@ -40,7 +38,6 @@ def on_add(self):
self.display = TextWidget(
size=(1, 9),
pos=(1, 0),
anchor=Anchor.CENTER,
pos_hint=(None, 0.5),
default_color_pair=ColorPair.from_colors(Air.COLOR, MENU_BACKGROUND_COLOR),
)
Expand Down
3 changes: 1 addition & 2 deletions examples/advanced/shadow_casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ async def on_start(self):

caster = ShadowCaster(
size=(17, 34),
anchor="center",
map=map_,
camera=Camera((0, 0), (34, 34)),
tile_colors=[AWHITE, ACYAN, AMAGENTA],
Expand Down Expand Up @@ -196,7 +195,7 @@ def callback(state):
)
grid_layout.size = grid_layout.minimum_grid_size

container = Widget(pos_hint=(0.5, 0.5), anchor="center", size=(17, 58))
container = Widget(pos_hint=(0.5, 0.5), size=(17, 58))
container.add_widgets(caster, grid_layout)
self.add_widget(container)

Expand Down
9 changes: 2 additions & 7 deletions examples/advanced/snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,9 @@ async def _update(self):

class SnakeApp(App):
async def on_start(self):
kwargs = dict(
size=(HEIGHT // 2, WIDTH),
pos_hint=(0.5, 0.5),
anchor="center",
)

kwargs = dict(size=(HEIGHT // 2, WIDTH), pos_hint=(0.5, 0.5))
background = Animation(path=SPINNER, alpha=0.5, **kwargs)
# background.play()
background.play()
snake = Snake(**kwargs)

self.add_widgets(background, snake)
Expand Down
3 changes: 1 addition & 2 deletions examples/advanced/sph/sph/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from nurses_2.app import App
from nurses_2.colors import BLACK, WHITE_ON_BLACK, Color, ColorPair
from nurses_2.io import MouseButton
from nurses_2.widgets.graphic_widget import Anchor, GraphicWidget
from nurses_2.widgets.graphic_widget import GraphicWidget
from nurses_2.widgets.slider import Slider
from nurses_2.widgets.text_widget import TextWidget

Expand Down Expand Up @@ -95,7 +95,6 @@ async def on_start(self):
container = TextWidget(
size=(height, width),
pos_hint=(0.5, 0.5),
anchor=Anchor.CENTER,
default_color_pair=WHITE_ON_BLACK,
)

Expand Down
3 changes: 1 addition & 2 deletions examples/advanced/tetris/tetris/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from nurses_2.app import App
from nurses_2.widgets.widget_data_structures import Anchor

from .tetris import Tetris


class TetrisApp(App):
async def on_start(self):
tetris = Tetris(pos_hint=(0.5, 0.5), anchor=Anchor.CENTER)
tetris = Tetris(pos_hint=(0.5, 0.5))

self.add_widget(tetris)

Expand Down
4 changes: 1 addition & 3 deletions examples/advanced/tetris/tetris/modal_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

from nurses_2.colors import BLACK, Color, ColorPair, gradient
from nurses_2.widgets.text_widget import Anchor, TextWidget
from nurses_2.widgets.text_widget import TextWidget

LIGHT_PURPLE = Color.from_hex("8d46dd")
DARK_PURPLE = Color.from_hex("190c54")
Expand Down Expand Up @@ -78,14 +78,12 @@
class ModalScreen(TextWidget):
def __init__(
self,
anchor=Anchor.CENTER,
pos_hint=(0.5, 0.5),
is_enabled=False,
**kwargs,
):
super().__init__(
size=(10, 70),
anchor=anchor,
pos_hint=pos_hint,
is_enabled=is_enabled,
**kwargs,
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/tetris/tetris/tetris.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ def __init__(self, matrix_size=(22, 10), arika=True, **kwargs):
for widget in self.walk():
setup_background(widget)

self.held_piece = Piece(pos_hint=(0.5, 0.5), anchor="center", is_enabled=False)
self.held_piece = Piece(pos_hint=(0.5, 0.5), is_enabled=False)
held_space.add_widget(self.held_piece)

self.next_piece = Piece(pos_hint=(0.5, 0.5), anchor="center", is_enabled=False)
self.next_piece = Piece(pos_hint=(0.5, 0.5), is_enabled=False)
next_space.add_widget(self.next_piece)

self.matrix = np.zeros(matrix_size, dtype=np.bool8)
Expand Down
Loading

0 comments on commit bc409fe

Please sign in to comment.