Skip to content

Commit 620bb30

Browse files
authored
feat(ui): much cleaner UI (#17)
1 parent b35a87b commit 620bb30

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

manim_slides/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "4.0.0rc1"
1+
__version__ = "4.0.0"

manim_slides/defaults.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import platform
22

3+
import cv2
4+
5+
FONT_ARGS = (cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 1, cv2.LINE_AA)
6+
PIXELS_PER_CHARACTER = 20
37
FOLDER_PATH: str = "./slides"
48
CONFIG_PATH: str = ".manim-slides.json"
59

manim_slides/present.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818
from .commons import config_path_option
1919
from .config import Config, PresentationConfig, SlideConfig, SlideType
20-
from .defaults import CONFIG_PATH, FOLDER_PATH
20+
from .defaults import CONFIG_PATH, FOLDER_PATH, FONT_ARGS
2121

2222
WINDOW_NAME = "Manim Slides"
23+
WINDOW_INFO_NAME = f"{WINDOW_NAME}: Info"
2324

2425

2526
@unique
@@ -251,6 +252,7 @@ def __init__(
251252
start_paused=False,
252253
fullscreen=False,
253254
skip_all=False,
255+
resolution=(1280, 720),
254256
):
255257
self.presentations = presentations
256258
self.start_paused = start_paused
@@ -267,6 +269,11 @@ def __init__(
267269
self.lag = 0
268270
self.last_time = now()
269271

272+
cv2.namedWindow(
273+
WINDOW_INFO_NAME,
274+
cv2.WINDOW_GUI_NORMAL | cv2.WINDOW_FREERATIO | cv2.WINDOW_AUTOSIZE,
275+
)
276+
270277
if self.is_windows:
271278
user32 = ctypes.windll.user32
272279
self.screen_width, self.screen_height = user32.GetSystemMetrics(
@@ -278,6 +285,12 @@ def __init__(
278285
cv2.setWindowProperty(
279286
WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN
280287
)
288+
else:
289+
cv2.namedWindow(
290+
WINDOW_NAME,
291+
cv2.WINDOW_GUI_NORMAL | cv2.WINDOW_FREERATIO | cv2.WINDOW_NORMAL,
292+
)
293+
cv2.resizeWindow(WINDOW_NAME, *resolution)
281294

282295
def resize_frame_to_screen(self, frame: np.ndarray):
283296
"""
@@ -318,6 +331,8 @@ def run(self):
318331
self.current_presentation_index += 1
319332
self.state = State.PLAYING
320333
self.handle_key()
334+
if self.exit:
335+
continue
321336
self.show_video()
322337
self.show_info()
323338

@@ -336,7 +351,7 @@ def show_video(self):
336351
def show_info(self):
337352
"""Shows updated information about presentations."""
338353
info = np.zeros((130, 420), np.uint8)
339-
font_args = (cv2.FONT_HERSHEY_SIMPLEX, 0.7, 255)
354+
font_args = (FONT_ARGS[0], 0.7, *FONT_ARGS[2:])
340355
grid_x = [30, 230]
341356
grid_y = [30, 70, 110]
342357

@@ -368,7 +383,7 @@ def show_info(self):
368383
*font_args,
369384
)
370385

371-
cv2.imshow(f"{WINDOW_NAME}: Info", info)
386+
cv2.imshow(WINDOW_INFO_NAME, info)
372387

373388
def handle_key(self):
374389
"""Handles key strokes."""
@@ -461,8 +476,17 @@ def _list_scenes(folder) -> List[str]:
461476
is_flag=True,
462477
help="Skip all slides, useful the test if slides are working.",
463478
)
479+
@click.option(
480+
"--resolution",
481+
type=(int, int),
482+
default=(1280, 720),
483+
help="Window resolution used if fullscreen is not set. You may manually resize the window afterward.",
484+
show_default=True,
485+
)
464486
@click.help_option("-h", "--help")
465-
def present(scenes, config_path, folder, start_paused, fullscreen, skip_all):
487+
def present(
488+
scenes, config_path, folder, start_paused, fullscreen, skip_all, resolution
489+
):
466490
"""Present the different scenes."""
467491

468492
if len(scenes) == 0:
@@ -527,5 +551,6 @@ def value_proc(value: str):
527551
start_paused=start_paused,
528552
fullscreen=fullscreen,
529553
skip_all=skip_all,
554+
resolution=resolution,
530555
)
531556
display.run()

manim_slides/wizard.py

+27-7
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,32 @@
77

88
from .commons import config_options
99
from .config import Config
10-
from .defaults import CONFIG_PATH
10+
from .defaults import CONFIG_PATH, FONT_ARGS, PIXELS_PER_CHARACTER
11+
12+
WINDOW_NAME = "Manim Slides Configuration Wizard"
13+
WINDOW_SIZE = (120, 620)
14+
15+
16+
def center_text_horizontally(text, window_size, font_args) -> int:
17+
"""Returns centered position for text to be displayed in current window."""
18+
_, width = window_size
19+
font, scale, _, thickness, _ = font_args
20+
(size_in_pixels, _), _ = cv2.getTextSize(text, font, scale, thickness)
21+
return (width - size_in_pixels) // 2
1122

1223

1324
def prompt(question: str) -> int:
14-
font_args = (cv2.FONT_HERSHEY_SIMPLEX, 0.7, 255)
15-
display = np.zeros((130, 420), np.uint8)
25+
"""Diplays some question in current window and waits for key press."""
26+
display = np.zeros(WINDOW_SIZE, np.uint8)
1627

17-
cv2.putText(display, "* Manim Slides Wizard *", (70, 33), *font_args)
18-
cv2.putText(display, question, (30, 85), *font_args)
28+
text = "* Manim Slides Wizard *"
29+
text_org = center_text_horizontally(text, WINDOW_SIZE, FONT_ARGS), 33
30+
question_org = center_text_horizontally(question, WINDOW_SIZE, FONT_ARGS), 85
1931

20-
cv2.imshow("Manim Slides Configuration Wizard", display)
32+
cv2.putText(display, "* Manim Slides Wizard *", text_org, *FONT_ARGS)
33+
cv2.putText(display, question, question_org, *FONT_ARGS)
34+
35+
cv2.imshow(WINDOW_NAME, display)
2136
return cv2.waitKeyEx(-1)
2237

2338

@@ -61,6 +76,11 @@ def _init(config_path, force, merge, skip_interactive=False):
6176

6277
if not skip_interactive:
6378

79+
cv2.namedWindow(
80+
WINDOW_NAME,
81+
cv2.WINDOW_GUI_NORMAL | cv2.WINDOW_FREERATIO | cv2.WINDOW_AUTOSIZE,
82+
)
83+
6484
prompt("Press any key to continue")
6585

6686
for _, key in config:
@@ -70,6 +90,6 @@ def _init(config_path, force, merge, skip_interactive=False):
7090
config = Config.parse_file(config_path).merge_with(config)
7191

7292
with open(config_path, "w") as config_file:
73-
config_file.write(config.json(indent=4))
93+
config_file.write(config.json(indent=2))
7494

7595
click.echo(f"Configuration file successfully save to `{config_path}`")

0 commit comments

Comments
 (0)