-
Notifications
You must be signed in to change notification settings - Fork 5
/
editor.py
201 lines (180 loc) · 5.55 KB
/
editor.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
# Tampio Compiler
# Copyright (C) 2017 Iikka Hauhio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pygame
from tampio import compileCode
WIDTH = 1600
HEIGHT = 800
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
FONT_SIZE = 20
FONTS = {}
default_font = pygame.font.SysFont("liberationserif", FONT_SIZE)
italic_font = pygame.font.SysFont("liberationserif", FONT_SIZE)
italic_font.set_italic(True)
bold_font = pygame.font.SysFont("liberationserif", FONT_SIZE)
bold_font.set_bold(True)
underline_font = pygame.font.SysFont("liberationserif", FONT_SIZE)
underline_font.set_underline(True)
FONTS["default"] = default_font
FONTS["small-caps"] = default_font
FONTS["italic"] = italic_font
FONTS["bold"] = bold_font
FONTS["underline"] = underline_font
STYLES = {
"": "default",
"function": "italic",
"keyword": "bold",
"field": "underline",
"variable": "small-caps",
"type": "small-caps"
}
def make_tokens(code):
tl, _, _ = compileCode(code)
tokens = []
line = []
for token, style in zip(tl.tokens, tl.styles):
for char in list(token.token):
if char == "\n":
tokens.append(line)
line = []
else:
line.append((char, style))
tokens.append(line)
return tokens
def make_texts(tokens):
texts = []
for char, style in tokens:
font = FONTS[STYLES.get(style, "default")]
if STYLES.get(style, "default") == "small-caps":
image = font.render(char.upper(), True, (0, 0, 0))
if char != char.upper():
new_image = pygame.Surface((image.get_width(), image.get_height()), pygame.SRCALPHA)
new_image.blit(pygame.transform.scale(image, (image.get_width(), int(0.9*image.get_height()))), (0, int(0.1*image.get_height())))
image = new_image
else:
image = font.render(char, True, (0, 0, 0))
texts.append((char, style, image))
return texts
code = ""
tokens = make_tokens(code)
image_lines = [make_texts(l) for l in tokens]
cursor_x = 0
cursor_y = 0
scroll = 0
def fix_scroll():
global scroll
if 1.5*FONT_SIZE*(cursor_y-scroll-2) > HEIGHT*0.9:
scroll += 1
if cursor_y-scroll < 0:
scroll -= 1
keys_down = []
key_timer = {}
tick = 0
done = False
while not done:
tick = (tick + 1) % 80
line = tokens[cursor_y]
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
keys_down.append(event.key)
key_timer[event.key] = 0
elif event.type == pygame.KEYUP:
keys_down.remove(event.key)
for key in keys_down:
key_timer[key] += 1
if 1 < key_timer[key] < 20:
continue
if cursor_x > 0 and key == pygame.K_LEFT:
cursor_x -= 1
elif cursor_x == 0 and cursor_y > 0 and key == pygame.K_LEFT:
cursor_y -= 1
line = tokens[cursor_y]
cursor_x = len(line)
elif cursor_x < len(line) and key == pygame.K_RIGHT:
cursor_x += 1
elif cursor_x == len(line) and cursor_y < len(tokens)-1 and key == pygame.K_RIGHT:
cursor_x = 0
cursor_y += 1
line = tokens[cursor_y]
elif cursor_y > 0 and key == pygame.K_UP:
cursor_y -= 1
line = tokens[cursor_y]
if cursor_x > len(line):
cursor_x = len(line)
elif cursor_y < len(tokens)-1 and key == pygame.K_DOWN:
cursor_y += 1
line = tokens[cursor_y]
if cursor_x > len(line):
cursor_x = len(line)
elif cursor_x > 0 and key == pygame.K_BACKSPACE:
del line[cursor_x-1]
cursor_x -= 1
elif cursor_x == 0 and cursor_y > 0 and key == pygame.K_BACKSPACE:
cursor_x = len(tokens[cursor_y-1])
tokens[cursor_y-1] += tokens[cursor_y]
del tokens[cursor_y]
cursor_y -= 1
lins = tokens[cursor_y]
elif key == pygame.K_RETURN:
new_line = line[cursor_x:]
del line[cursor_x:]
tokens.insert(cursor_y+1, new_line)
cursor_y += 1
cursor_x = 0
elif key in list(range(ord("a"), ord("z")+1)) + [ord(c) for c in list("åäö.,- \t1234567890+")]:
char = chr(key)
if pygame.K_LSHIFT in keys_down or pygame.K_RSHIFT in keys_down:
if char in "1234567890+.,-":
char = "!\"#¤%&/()=?:;_"["1234567890+.,-".index(char)]
else:
char = char.upper()
line.insert(cursor_x, (char, ""))
cursor_x += 1
image_lines = [make_texts(l) for l in tokens]
fix_scroll()
if tick%30 == 0:
tokens = make_tokens("\n".join(["".join([c[0] for c in l]) for l in tokens]))
image_lines = [make_texts(l) for l in tokens]
screen.fill((255, 255, 255))
y = 0
for j, image_line in list(enumerate(image_lines))[scroll:]:
i = 0
x = 0
for token, style, image in image_line:
if x+5+FONT_SIZE > WIDTH:
y += int(FONT_SIZE*1.5)
x = 0
if i == cursor_x and j == cursor_y:
pygame.draw.line(screen, (0,0,0), [5+x, 5+y], [5+x,5+y+FONT_SIZE], 1)
if token == " ":
x += FONT_SIZE//2
elif token == "\t":
x += FONT_SIZE*4
x -= x%(FONT_SIZE*4)
else:
screen.blit(image, (5+x, 5+y))
x += image.get_width()
if STYLES.get(style, "default") == "italic":
x -= int(image.get_width()*0.2)
i += 1
if i == cursor_x and j == cursor_y:
pygame.draw.line(screen, (0,0,0), [5+x, 5+y], [5+x,5+y+FONT_SIZE], 1)
y += int(FONT_SIZE*1.5)
pygame.display.flip()
clock.tick(60)