-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.py
238 lines (209 loc) · 8.77 KB
/
t.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/local/bin/python3
#coding: utf-8
# Licence: GNU AGPLv3
"""FEN animation"""
from __future__ import annotations
import chess
import chess.svg
import logging
import logging.handlers
import manimpango
from pathlib import Path
from itertools import islice
from typing import Optional, List, Union, Tuple
from manim import *
#############
# Constants #
#############
LOG_PATH = f"{__file__}.log"
SCALE = 0.4
########
# Logs #
########
log = logging.getLogger(__file__)
log.setLevel(logging.DEBUG)
format_string = "%(asctime)s | %(levelname)-8s | %(message)s"
# 125000000 bytes = 12.5Mb
handler = logging.handlers.RotatingFileHandler(LOG_PATH, maxBytes=12500000, backupCount=3, encoding="utf8")
handler.setFormatter(logging.Formatter(format_string))
handler.setLevel(logging.DEBUG)
log.addHandler(handler)
handler_2 = logging.StreamHandler(sys.stdout)
handler_2.setFormatter(logging.Formatter(format_string))
handler_2.setLevel(logging.INFO)
if __debug__:
handler_2.setLevel(logging.DEBUG)
log.addHandler(handler_2)
###########
# Classes #
###########
def text_config(font="Andale Mono",width=6,line_spacing=0.6):
return {'font': font, 'width': width, 'line_spacing': line_spacing}
def with_delimiter(board) -> str:
return board.__str__().replace("\n", "/\n")
def one_line(board) -> str:
return board.__str__().replace("\n", "/").replace(" ", "")
def colored_epd(board):
return Text(board.epd().split(" ")[0], font="Andale Mono", t2c={str(i): BLUE for i in range(1,9)} | {'/': ORANGE}) #font_size=27)
def brace_of(x, caption: str, color, direction=UP, buff=0.25):
brace = Brace(x, direction=direction, color=color)
if direction.all() == UP.all():
brace.stretch(-1,0)
brace_caption = Text(caption, font="Andale Mono", color=color)
brace_caption.scale(SCALE + 0.1)
brace_caption.next_to(brace, direction, buff=buff)
return brace, brace_caption
# TODO, refactor must be easier way
def one_line_bluedots(board) -> (List[Text], VGroup):
"""try to put dots in their own Text Node for finer grained animation"""
one_line = board.__str__().replace("\n", "/").replace(" ", "")
starting_dot_part = None # Optional[int]
starting_piece_part = None # Optional[int]
parts = []
g = VGroup()
def on_piece(g: VGroup, x: int):
nonlocal starting_piece_part
nonlocal starting_dot_part
if starting_piece_part is None: # We're at the beginning of a piece sequence
starting_piece_part = x
if starting_dot_part is not None:
dot_part = Text(one_line[starting_dot_part:x], font="Andale Mono", color=BLUE)
g += dot_part
parts.append(dot_part)
starting_dot_part = None
starting_piece_part = x
def on_dot(g: VGroup, x: int):
nonlocal starting_piece_part
nonlocal starting_dot_part
if starting_dot_part is None: # We're at the beginning of a dot sequence
starting_dot_part = x
if starting_piece_part is not None:
piece_part = Text(one_line[starting_piece_part:x], font="Andale Mono",t2c={'/': ORANGE})
parts.append(piece_part)
g += piece_part
starting_piece_part = None
starting_dot_part = x
for (i, char) in enumerate(one_line):
if char != ".":
on_piece(g, i)
else:
on_dot(g, i)
# to close the last part
if one_line[-1] == ".":
# call on_piece when it's a dot to finish the last piece part
on_piece(g, len(one_line))
else:
on_dot(g, len(one_line))
g.arrange_in_grid(cols=len(parts),row_alignments='d')
return (parts, g)
def replace_dots(parts: List[Text]) -> VGroup:
g = VGroup()
for part in parts:
if "." in part.text:
x = Text(str(len(part.text)), font="Andale Mono", color=BLUE)
x.scale(SCALE)
g += x
else:
g += part.copy()
g.arrange_in_grid(cols=len(parts),row_alignments='d',buff=0.05)
return g
def anscii_board(board) -> (List[Text], VGroup):
"""given a board, return a VGroup with each board line consisting of a sub VGroup with an empty ending, to allow smoother animation when adding slashes at the end"""
board_lines = [Text(i, font="Andale Mono") for i in board.__str__().split("\n")]
g = VGroup(*[VGroup(line, Text("")).arrange(RIGHT) for line in board_lines]).arrange(DOWN)
return (board_lines, g)
def anscii_board_delimited(board_lines: List[Text]) -> VGroup:
g = VGroup(*[VGroup(line, Text("/", color=ORANGE)).arrange(RIGHT) for line in board_lines[:-1]])
g += VGroup(board_lines[-1], Text("")).arrange(RIGHT)
g.arrange(DOWN)
return g
def one_line_board(board_lines: List[Text]) -> VGroup:
g = VGroup(*[VGroup(line, Text("/", color=ORANGE)).arrange(RIGHT) for line in board_lines[:-1]])
g += VGroup(board_lines[-1], Text("")).arrange(RIGHT)
g.arrange(RIGHT)
for line in board_lines:
line.font_size = 16
return g
class Fen(Scene):
def construct(self):
fen = "rnb1k2r/1pq1bppp/p2ppn2/6B1/3NPP2/2N2Q2/PPP3PP/2KR1B1R b kq - 4 9"
board = chess.Board(fen=fen)
empty = chess.Board(fen=None)
with open("empty.svg", "w") as f:
f.write(chess.svg.board(empty))
with open("board.svg", "w") as f:
f.write(chess.svg.board(board))
board_svg = SVGMobject("board.svg", width=7)
self.add(board_svg)
self.wait()
board_unicode = Text(board.unicode(empty_square=".", invert_color=True), **text_config()) # Courier New, Optima, Other mono
self.play(FadeOut(board_svg), FadeIn(board_unicode))
self.wait()
board_anscii = Text(board.__str__(), font="Andale Mono", width=810)
board_anscii_one_part = Text(board.__str__(), **text_config())
self.play(ReplacementTransform(board_unicode, board_anscii_one_part, run_time=2))
self.wait()
board_anscii_delimited = Text(with_delimiter(board), t2c={'/': ORANGE}, **text_config(width=6.52))
self.wait()
self.play(TransformMatchingShapes(board_anscii_one_part, board_anscii_delimited))
self.wait()
board_anscii_oneline = Text(one_line(board), font="Andale Mono", t2c={'/': ORANGE})
board_anscii_oneline.scale(SCALE)
self.play(
AnimationGroup(
board_anscii_delimited.animate.scale(SCALE),
ReplacementTransform(board_anscii_delimited, board_anscii_oneline)
)
)
self.wait()
parts, board_anscii_oneline_blue_dot = one_line_bluedots(board)
board_anscii_oneline_blue_dot.scale(SCALE)
self.play(TransformMatchingShapes(board_anscii_oneline, board_anscii_oneline_blue_dot))
board_colored_epd_final = colored_epd(board)
board_colored_epd_final.scale(SCALE)
board_colored_epd = replace_dots(parts)
self.play(ReplacementTransform(board_anscii_oneline_blue_dot, board_colored_epd, run_time=2))
self.play(TransformMatchingShapes(board_colored_epd, board_colored_epd_final))
self.wait()
self.play(board_colored_epd_final.animate.set_color(WHITE).shift(2 * LEFT))
turn = Text(fen.split(" ")[1], font="Andale Mono")
turn.next_to(board_colored_epd_final, RIGHT)
turn.scale(SCALE)
castling = Text(fen.split(" ")[2], font="Andale Mono")
castling.next_to(turn, RIGHT)
castling.scale(SCALE)
ep = Text(fen.split(" ")[3])
ep.next_to(castling, RIGHT)
ep.scale(SCALE)
counters = Text(" ".join(fen.split(" ")[4:]), font="Andale Mono")
counters.next_to(ep, RIGHT,buff=0)
counters.scale(SCALE)
self.play(
AnimationGroup(
FadeIn(turn, shift=DOWN),
FadeIn(castling, shift=DOWN),
FadeIn(ep, shift=DOWN),
FadeIn(counters, shift=DOWN),
lag_ratio=0.4
)
)
self.play(
AnimationGroup(
self.add_brace(board_colored_epd_final, "Board", direction=UP,color=GREEN),
self.add_brace(turn, "Turn", direction=DOWN,color=ORANGE),
self.add_brace(castling, "Castling", direction=UP,color=BLUE),
self.add_brace(ep, "En-passant", direction=DOWN,color=PINK,buff=0),
self.add_brace(counters, "Counters", direction=UP,color=LIGHT_GREY,buff=0.7),
lag_ratio=0.9,
)
)
self.wait()
def add_brace(self, of, caption, direction, color, buff=0.25):
brace, caption = brace_of(of, caption, direction=direction,color=color,buff=buff)
return AnimationGroup(of.animate.set_color(color),Write(caption,run_time=1),Write(brace, run_time=1), lag_ratio=0.4)
########
# Main #
########
# if __name__ == "__main__":
# print('#'*80)
# main()