-
Notifications
You must be signed in to change notification settings - Fork 0
/
marios_picross_puzzle_editor_gui.py
187 lines (162 loc) · 6.74 KB
/
marios_picross_puzzle_editor_gui.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
# Classic Game Resource Reader (CGRR): Parse resources from classic games.
# Copyright (C) 2014-2015 Tracy Poff
#
# This file is part of CGRR.
#
# CGRR 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.
#
# CGRR 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 CGRR. If not, see <http://www.gnu.org/licenses/>.
"""GUI for editing Mario's Picross levels."""
import logging
from tkinter import (Tk, Frame, Canvas, Menu, Label, Button,
DISABLED, NORMAL, W, E, N, S)
from tkinter.filedialog import askopenfilename, asksaveasfile
import mariospicross
logging.basicConfig(level=logging.DEBUG)
class MainWindow(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background="white")
self.plugin = mariospicross
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Mario's Picross Puzzle Editor")
self.puzzles = None
# Build the menu
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
self.fileMenu = fileMenu
fileMenu.add_command(label="Open Mario's Picross ROM...",
command=self.onOpen)
fileMenu.add_command(label="Save ROM as...",
command=self.onSave,
state=DISABLED)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenu)
# Navigation
Label(self.parent).grid(row=0, column=0)
Label(self.parent).grid(row=0, column=4)
self.parent.grid_columnconfigure(0, weight=1)
self.parent.grid_columnconfigure(4, weight=1)
prevButton = Button(self.parent,
text="<--",
command=self.onPrev,
state=DISABLED
)
self.prevButton = prevButton
prevButton.grid(row=0, column=1)
puzzle_number = 1
self.puzzle_number = puzzle_number
puzzleNumber = Label(self.parent, text="Puzzle #{}".format(puzzle_number))
self.puzzleNumber = puzzleNumber
puzzleNumber.grid(row=0, column=2)
nextButton = Button(self.parent,
text="-->",
command=self.onNext,
state=DISABLED
)
self.nextButton = nextButton
nextButton.grid(row=0, column=3)
# Canvas
canvas = Canvas(self.parent)
self.canvas = canvas
for i in range(15):
for j in range(15):
fillcolor = "gray80"
self.canvas.create_rectangle(10+20*j, 10+20*i,
10+20*(j+1), 10+20*(i+1),
fill=fillcolor,
tags="{},{}".format(i, j)
)
self.canvas.bind("<ButtonPress-1>", self.onClick)
canvas.grid(row=1, columnspan=5, sticky=W+E+N+S)
self.parent.grid_rowconfigure(1, weight=1)
def onOpen(self):
filepath = askopenfilename()
with open(filepath, "rb") as f:
self.rom = f.read()
self.puzzles = self.plugin.read_puzzles_from_rom(self.rom)
self.fileMenu.entryconfig(1, state=DISABLED)
self.fileMenu.entryconfig(2, state=NORMAL)
self.nextButton['state'] = NORMAL
self.prevButton['state'] = NORMAL
self.draw_puzzle()
def onSave(self):
with asksaveasfile(mode='wb') as outfile:
outfile.write(self.rom)
self.plugin.insert_puzzles(outfile, self.puzzles[1:])
def onExit(self):
self.quit()
def draw_puzzle(self):
for i in range(15):
for j in range(15):
fillcolor = (
"gray40" if self.puzzles[self.puzzle_number]['puzzle'][i][j]
else "gray80")
self.canvas.itemconfig("{},{}".format(i,j), fill=fillcolor)
def onPrev(self):
if self.puzzle_number == 1:
self.puzzle_number = 256
else:
self.puzzle_number -= 1
self.puzzleNumber['text'] = "Puzzle #{}".format(self.puzzle_number)
self.draw_puzzle()
def onNext(self):
if self.puzzle_number == 256:
self.puzzle_number = 1
else:
self.puzzle_number += 1
self.puzzleNumber['text'] = "Puzzle #{}".format(self.puzzle_number)
self.draw_puzzle()
def onClick(self, event):
if not self.puzzles:
return
num = event.widget.find_closest(event.x, event.y)[0]
i = num // 15
j = (num - 1) % 15
value = self.puzzles[self.puzzle_number]['puzzle'][i][j]
self.puzzles[self.puzzle_number]['puzzle'][i][j] = not value
fillcolor = (
"gray40" if self.puzzles[self.puzzle_number]['puzzle'][i][j]
else "gray80"
)
self.canvas.itemconfig(num, fill=fillcolor)
# set the puzzle size
if (any([self.puzzles[self.puzzle_number]['puzzle'][i][j]
for i in range(15)
for j in range(10, 15)]) or
any([self.puzzles[self.puzzle_number]['puzzle'][i][j]
for i in range(10,15)
for j in range(15)])):
self.puzzles[self.puzzle_number]['width'] = 15
self.puzzles[self.puzzle_number]['height'] = 15
elif (any([self.puzzles[self.puzzle_number]['puzzle'][i][j]
for i in range(10)
for j in range(5, 10)]) or
any([self.puzzles[self.puzzle_number]['puzzle'][i][j]
for i in range(5,10)
for j in range(10)])):
self.puzzles[self.puzzle_number]['width'] = 10
self.puzzles[self.puzzle_number]['height'] = 10
else:
self.puzzles[self.puzzle_number]['width'] = 5
self.puzzles[self.puzzle_number]['height'] = 5
def main():
root = Tk()
root.geometry("320x350+300+300")
root.resizable(0,0)
app = MainWindow(root)
root.mainloop()
if __name__ == '__main__':
main()