-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeaterOpener.py
More file actions
304 lines (298 loc) · 15.1 KB
/
theaterOpener.py
File metadata and controls
304 lines (298 loc) · 15.1 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import glob
import threading
import subprocess
import Pallet.manipulationBS as mBS
import usefull
import Share
share = Share.Share()
blockprogram = ''
starterBlock = ''
finishCodes = {'inside': [], 'outside': []}
numberofTab = 0
isInCloneChunk = None
tkimgSetup = 'photoImages = []'
cachedTkimg = []
#runningProgram = False
def exePy():
global programRunning
global programProcess
#programProcess = subprocess.Popen(['python', 'temp/program.py'], stdout=PIPE, stderr=PIPE)
programRunning = True
programProcess = subprocess.Popen(['python','temp/program.py'], shell=True)
programProcess.wait()
programRunning = False
def addCodeSnippet(codeSnippet):
global blockprogram
global starterBlock
global numberofTab
if starterBlock == 'keyPress' or starterBlock == 'while':
for i in range(numberofTab):
blockprogram += '\t'
blockprogram += codeSnippet
else:
blockprogram += codeSnippet
blockprogram += '\n'
return True
programVariables = []
thisObjectIndex = None
def superEntry2code(superEntry):
global programVariables
global thisObjectIndex
if superEntry[0] != '*':
return superEntry
if superEntry[1:] in programVariables:
return f"programVariables['{superEntry[1:]}']"
elif superEntry == '*この物体のX座標':
return f"canvas.bbox(actor{thisObjectIndex})[0]"
elif superEntry == '*この物体のY座標':
return f"canvas.bbox({getActorCurrentlyEligible()})[1]"
elif superEntry == '*0から1までの乱数':
return f'random.random()'
elif mBS.baseofBlock(superEntry) == '*[] + []':
return f'{superEntry2code(mBS.getEntryfromBlockscript(superEntry, 0))} + {mBS.getEntryfromBlockscript(superEntry, 1)}'
else:
usefull.printWarning(f'Could not convert "{superEntry}" to code.\n')
return ''
def translateEntryfromBlockscript(blockscript, index):
entry = mBS.getEntryfromBlockscript(blockscript, index)
if mBS.baseofBlock(entry) == '*[] x []':
return f"{translateEntryfromBlockscript(entry, 0)}*{translateEntryfromBlockscript(entry, 1)}"
else:
return superEntry2code(entry) if entry[0] == '*' else entry
def getActorCurrentlyEligible():
global thisObjectIndex
return f'cloneActor{thisObjectIndex}[indexofCloneActor{thisObjectIndex}]' if isInCloneChunk else f'actor{thisObjectIndex}'
def tkPhotoImage(tkimgPath):
global cachedTkimg
global tkimgSetup
if not tkimgPath in cachedTkimg:
tkimgSetup += f"photoImages.append(tk.PhotoImage(file=r'{tkimgPath}'))\n"
cachedTkimg.append(tkimgPath)
return f'photoImages[{cachedTkimg.index(tkimgPath)}]'
def unveiling(pallet, objects, title):
global blockprogram
global starterBlock
global finishCodes
global numberofTab
global programRunning
global programProcess
global programVariables
'''現在作業しているオブジェクトのインデックス'''
global thisObjectIndex
'''現在のチャンクが「分身の動き」か'''
global isInCloneChunk
global tkimgSetup
global cachedTkimg
exePyPath = 'temp/program.py'
#if platform.system() == 'Windows':
# exePyPath = exePyPath.replace('/', '\\')
sourcefile = open(exePyPath, 'w', encoding='utf-8')
blockprogram = (
'keyPressFuncs = []\n'
'def keyPressFunc(event):\n'
'\tfor func in keyPressFuncs:\n'
'\t\tfunc(event)\n'
"root.bind('<KeyRelease>', keyPressFunc)\n"
'programVariables = {}\n')
numberofKeyPressFuncs = 0
programVariables = []
for variable in pallet.variableBlocks:
programVariables.append(variable[1:-1])
for variable in programVariables:
addCodeSnippet(f"programVariables['{variable}'] = None")
thisObjectIndex = 0
numberofWhileLoop = 1
blockprogram += (
"'''------------------------------------\n"
"| Setup complete. Let's get started! |\n"
"-------------------------------------'''\n"
)
tkimgSetup = 'photoImages = []\n'
cachedTkimg = []
for loopi, code in enumerate(pallet.codes):
chunks = []
temp2splitIntoChunk = []
for loopIdx, blockscript in enumerate(code):
if pallet.isStarterBlock(blockscript):
chunks.append(temp2splitIntoChunk)
temp2splitIntoChunk = [blockscript]
else:
temp2splitIntoChunk.append(blockscript)
if loopIdx == len(code) - 1:
chunks.append(temp2splitIntoChunk)
for loopIdx, chunk in enumerate(chunks):
if chunk[0] == '分身の動き':
chunks.insert(0, chunks.pop(loopIdx))
chunkHead = chunk[0]
if loopi != 0:
blockprogram += (
f'actor{loopi} = canvas.create_image(100, 100, image='
f"{tkPhotoImage(glob.glob(f'savedData/{share.title}/costume/{objects.nameofObject[loopi]}/*')[objects.actors['indexofDefaultCostume'][loopi]])})\n"
)
if chunkHead == '分身の動き':
blockprogram += (
f'cloneActor{loopi} = []\n'
f'numberofCloneActors{loopi} = 0\n'
)
thisObjectIndex += 1
variables = {}
ifDepth = 0
for chunk in chunks:
if chunk[0] == '分身の動き':
isInCloneChunk = True
else:
isInCloneChunk = False
for blockscript in chunk:
#print(mBS.baseofBlock(blockscript))
if '[*]' in pallet.formofBlock(blockscript):
superEntry = pallet.getSuperEntryfromBlockscript(blockscript, 0)
if pallet.formofBlock(superEntry) == '[] x []':
blockscript = blockscript.replace('*' + superEntry,
str(
int(pallet.getEntryfromBlockscript_old(superEntry, 0)) *
int(pallet.getEntryfromBlockscript_old(superEntry, 1))
)
)
if mBS.baseofBlock(blockscript) == '{}キーが離されたとき':
numberofTab = 0
addCodeSnippet(f"def keyPress{numberofKeyPressFuncs}(event):")
starterBlock = 'keyPress'
finishCodes['outside'].append(f'keyPressFuncs.append(keyPress{numberofKeyPressFuncs})')
numberofTab = 1
typeofChar = None
if pallet.getDropdownfromBlockscript(blockscript, 0) == '上':
typeofChar = 'Up'
elif pallet.getDropdownfromBlockscript(blockscript, 0) == '下':
typeofChar = 'Down'
elif pallet.getDropdownfromBlockscript(blockscript, 0) == '右':
typeofChar = 'Right'
elif pallet.getDropdownfromBlockscript(blockscript, 0) == '左':
typeofChar = 'Left'
elif pallet.getDropdownfromBlockscript(blockscript, 0) == 'w':
typeofChar = 'w'
elif pallet.getDropdownfromBlockscript(blockscript, 0) == 's':
typeofChar = 's'
if typeofChar == None:
print(f"[ERROR] this program didn't support this type of char: {pallet.getDropdownfromBlockscript(blockscript, 0)}")
addCodeSnippet(f"if event.keysym == '{typeofChar}':")
numberofTab = 2
numberofKeyPressFuncs += 1
elif blockscript == 'ずっと~':
addCodeSnippet(f'def loop{numberofWhileLoop}():')
numberofTab += 1
ifDepth += 1
starterBlock = 'while'
finishCodes['inside'].append(f'root.after(40, loop{numberofWhileLoop})')
finishCodes['outside'].append(f'root.after(40, loop{numberofWhileLoop})')
numberofWhileLoop += 1
elif blockscript == '幕が上がったとき':
numberofTab = 0
addCodeSnippet('# 幕が上がったとき')
elif mBS.baseofBlock(blockscript) == '衣裳を{}にする':
#print(pallet.getDropdownfromBlockscript(blockscript, 0))
addCodeSnippet(f"canvas.itemconfigure({getActorCurrentlyEligible()}, image={tkPhotoImage(r'savedData/ロケット一代/costume/小惑星/asteroid.png')})")
elif mBS.baseofBlock(blockscript) == '[]回~':
addCodeSnippet(f'for i in range({translateEntryfromBlockscript(blockscript, 0)}):')
numberofTab += 1
elif blockscript == '自分自身の分身を作る':
addCodeSnippet(f'clone()')
elif blockscript == '分身の動き':
addCodeSnippet(f'def clone():')
numberofTab += 1
addCodeSnippet(f'global numberofCloneActors{loopi}')
addCodeSnippet(f'indexofCloneActor{loopi} = numberofCloneActors{loopi} - 1')
addCodeSnippet(f'numberofCloneActors{loopi} += 1')
addCodeSnippet(f"cloneActor{loopi}.append(canvas.create_image(100, 100, image={tkPhotoImage(glob.glob(f'savedData/{share.title}/costume/{objects.nameofObject[loopi]}/*')[0])}))")
elif mBS.baseofBlock(blockscript) == 'ウィンドウの幅を[]pxに、高さを[]pxにする':
addCodeSnippet(f"root.geometry('{translateEntryfromBlockscript(blockscript, 0)}x{translateEntryfromBlockscript(blockscript, 1)}+0+0')")
elif mBS.baseofBlock(blockscript) == 'ウィンドウの背景色を[]にする':
addCodeSnippet(f"canvas['background']='{pallet.getEntryfromBlockscript_old(blockscript, 0)}'")
elif mBS.baseofBlock(blockscript) == '上に[]px動く':
movePx = mBS.getEntryfromBlockscript(blockscript, 0)
if movePx[0] == '*':
addCodeSnippet(f'canvas.move(actor{loopi}, 0, -{superEntry2code(movePx)})')
else:
addCodeSnippet(f'canvas.move(actor{loopi}, 0, -{movePx})')
elif mBS.baseofBlock(blockscript) == '下に[]px動く':
addCodeSnippet(f'canvas.move({getActorCurrentlyEligible()}, 0, {translateEntryfromBlockscript(blockscript, 0)})')
elif mBS.baseofBlock(blockscript) == '右に[]px動く':
movePx = mBS.getEntryfromBlockscript(blockscript, 0)
if movePx[0] == '*':
addCodeSnippet(f'canvas.move(actor{loopi}, {superEntry2code(movePx)}, 0)')
else:
addCodeSnippet(f'canvas.move(actor{loopi}, {movePx}, 0)')
elif mBS.baseofBlock(blockscript) == '左に[]px動く':
addCodeSnippet(f'canvas.move(actor{loopi}, -{pallet.getEntryfromBlockscript_old(blockscript, 0)}, 0)')
elif mBS.baseofBlock(blockscript) == 'x座標を[]に、y座標を[]にする':
addCodeSnippet(f'''canvas.move({getActorCurrentlyEligible()}, {translateEntryfromBlockscript(blockscript, 0)
} - canvas.bbox({getActorCurrentlyEligible()})[0], {
translateEntryfromBlockscript(blockscript, 1)} - canvas.bbox({getActorCurrentlyEligible()})[1])''')
elif blockscript == '隠れる':
addCodeSnippet(f"canvas.itemconfigure(actor{loopi}, state='hidden')")
elif mBS.baseofBlock(blockscript) == 'もし<[] = []>なら~':
terms = pallet.getTermsfromBlockscript(blockscript, 0)
if pallet.getEntryfromBlockscript_old(terms, 0)[0] == '*':
addCodeSnippet(f'if {superEntry2code(pallet.getEntryfromBlockscript_old(terms, 0))} == {pallet.getEntryfromBlockscript_old(terms, 1)}:')
else:
if mBS.baseofBlock(terms) == '<[] = []>':
addCodeSnippet(f'if {pallet.getEntryfromBlockscript_old(terms, 0)} == {pallet.getEntryfromBlockscript_old(terms, 1)}:')
ifDepth += 1
numberofTab += 1
elif mBS.baseofBlock(blockscript) == 'もし<[]が[]以上>なら~':
terms = pallet.getTermsfromBlockscript(blockscript, 0)
if pallet.getEntryfromBlockscript_old(terms, 0)[0] == '*':
addCodeSnippet(f'if {superEntry2code(mBS.getEntryfromBlockscript(terms, 0))} >= ' +
f'{superEntry2code(mBS.getEntryfromBlockscript(terms, 1))}:')
else:
if mBS.baseofBlock(terms) == '<[]が[]以上>':
addCodeSnippet(f'if {pallet.getEntryfromBlockscript_old(terms, 0)} >= {pallet.getEntryfromBlockscript_old(terms, 1)}:')
ifDepth += 1
numberofTab += 1
finishCodes['inside'].append('')
finishCodes['outside'].append('')
elif blockscript == '~する':
addCodeSnippet('# ~する')
if finishCodes['inside'] != []:
if finishCodes['inside'][-1] != '':
addCodeSnippet(finishCodes['inside'][-1])
finishCodes['inside'].pop(-1)
if ifDepth > 0:
numberofTab -= 1
ifDepth -= 1
if finishCodes['outside'] != []:
if finishCodes['outside'][-1] != '':
addCodeSnippet(finishCodes['outside'][-1])
finishCodes['outside'].pop(-1)
elif blockscript[:15] == '+variableSetter':
addCodeSnippet(f"""programVariables['{blockscript.split(">")[1]}']=""" + superEntry2code(pallet.getEntryfromBlockscript_old(blockscript, 0)))
else:
blockprogram += f'# {blockscript}\n'
if finishCodes['inside'] != []:
addCodeSnippet(finishCodes['inside'][-1])
finishCodes['inside'] = []
if finishCodes['outside'] != []:
blockprogram += finishCodes['outside'][-1] + '\n'
finishCodes['outside'] = []
numberofTab = 0
sourcefile.write(
'import random\n'
'import tkinter as tk\n'
'root = tk.Tk()\n'
f"root.title('{title.get()}')\n"
'canvas = tk.Canvas(root, highlightthickness=False)\n'
"canvas.pack(fill='both', expand=True)\n"
+ tkimgSetup
+ blockprogram
+ 'root.mainloop()')
sourcefile.close()
# run!
#subprocess.call(exePyPath, shell=True)
#programProcess = subprocess.Popen(['python', 'temp/program.py'], stdout=PIPE, stderr=PIPE)
threading.Thread(target=exePy).start()
#time.sleep(1)
#programRunning = True
#programProcess = subprocess.Popen(['python', 'temp/program.py'], shell=False)
#programRunning = False
#time.sleep(1)
#print('killing...')
return