-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconverter_gui.py
executable file
·316 lines (260 loc) · 10.6 KB
/
converter_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
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
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
# Copyright (c) 2016, Aeva M. Palecek
# 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 os
import re
import sys
import time
import urllib
import traceback
from threading import Thread
from multiprocessing import Pool, cpu_count
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib, Gtk, Gdk, GObject
from nw2png import convert_to_png
from nw2tiled import convert_to_tmx
from util import find_level_parser
def run_jobs(window, converter, jobs):
scope = {
"processed" : 0
}
def job_callback(result):
scope["processed"] += 1
GLib.idle_add(window.advance_progress, scope["processed"])
def error_handler(converter, *params):
try:
converter(*params)
except Exception as error:
print "JOB FAILED:"
print params
traceback.print_exc()
start = time.time()
#pool = Pool(max(cpu_count(), 2))
for job in jobs:
job_callback(error_handler(converter, *job))
#pool.apply_async(converter, job, callback=job_callback)
#pool.close()
#pool.join()
GLib.idle_add(window.conclude_progress, time.time()-start)
class ConverterWindow(object):
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file("gui_layout.glade")
self.builder.connect_signals(self)
self.levels = set()
self.levels_store = self.builder.get_object("level_path_store")
self.levels_view = self.builder.get_object("level_tree_view")
self.setup_treeview()
self.window = self.builder.get_object("main_window")
self.window.show_all()
self.about_popup = self.builder.get_object("about_popup")
self.progress_popup = self.builder.get_object("progress_popup")
self.progress_label = self.builder.get_object("progress_label")
self.progress_bar = self.builder.get_object("progressbar")
self.setup_filters()
self.setup_drag_n_drop()
self.setup_working_dirs()
def setup_treeview(self):
column = Gtk.TreeViewColumn('')
icon_cell = Gtk.CellRendererPixbuf()
icon_cell.set_alignment(0.0, 0.75)
text_cell = Gtk.CellRendererText()
text_cell.set_property("ellipsize", 3)
text_cell.set_padding(6, 0)
column.pack_start(icon_cell, False)
column.pack_end(text_cell, True)
column.add_attribute(icon_cell, "icon_name", 0)
column.add_attribute(text_cell, "text", 1)
self.levels_view.append_column(column)
def setup_drag_n_drop(self):
targets = [("text/uri-list", 0, 0)]
self.levels_view.enable_model_drag_dest(targets, Gdk.DragAction.DEFAULT)
def setup_filters(self):
img_filter = Gtk.FileFilter()
img_filter.set_name("Image files")
img_filter.add_mime_type("image/*")
img_filter.add_pattern("*.png")
img_filter.add_pattern("*.gif")
pics_chooser = self.builder.get_object("pics1_chooser")
pics_chooser.add_filter(img_filter)
def setup_working_dirs(self):
self.working_dir = path = os.getcwd()
self.builder.get_object("pics1_chooser").set_current_folder(path)
self.builder.get_object("search_path_chooser").set_current_folder(path)
self.builder.get_object("output_path_chooser").set_current_folder(path)
def find_common_path(self, pathset):
def cut(path):
tail, head = os.path.split(path)
if tail == path:
return []
else:
return cut(tail) + [head]
def clean(path):
return tuple(cut(os.path.abspath(path)))
paths = map(clean, pathset)
return "/" + os.path.join(*os.path.commonprefix(paths))
def refresh_levels_view(self):
dir_icon = "folder"
doc_icon = "emblem-documents"
all_levels = list(self.levels)
all_levels.sort()
all_paths = set([path for path, level in all_levels])
self.levels_store.clear()
if not all_paths:
return
if len(all_paths) == 1:
# don't bother with nesting
for path, level in all_levels:
self.levels_store.append(None, [doc_icon, level])
else:
common_path = self.find_common_path(all_paths)
path_iters = {}
for path, level in all_levels:
nice_path = os.path.relpath(path, common_path)
if not path_iters.has_key(path):
path_iters[path] = self.levels_store.append(
None, [dir_icon, nice_path])
self.levels_store.append(path_iters[path], [doc_icon, level])
def add_level(self, path):
def is_level(path):
try:
find_level_parser(path)
return True
except:
print "Not a valid level file: %s" % path
return False
if os.path.isdir(path):
for base_dir, dirs, files in os.walk(path):
for file_ in files:
path = os.path.join(base_dir, file_)
if is_level(path):
self.add_level(path)
else:
if os.path.isfile(path) and is_level(path):
path, head = os.path.split(path)
level = (os.path.abspath(path), head)
self.levels.add(level)
def start_progress(self):
self.window.set_sensitive(False)
self.progress_popup.show_all()
self.progress_bar.set_fraction(0.0)
self.progress_label.set_text(
'Converting levels... (0/{0})'.format(
len(self.levels)))
def advance_progress(self, completed):
if completed > len(self.levels):
return
self.progress_bar.set_fraction(completed / float(len(self.levels)))
self.progress_label.set_text(
"Converting levels... ({0}/{1})".format(
completed+1,
len(self.levels)))
def conclude_progress(self, elapsed_time):
self.progress_popup.hide()
message = "All done!\nConverted {0} files in {1} seconds.".format(
len(self.levels), round(elapsed_time, 2))
pop = Gtk.MessageDialog(
self.window,
0, Gtk.MessageType.INFO,
Gtk.ButtonsType.CLOSE, message)
pop.run()
pop.destroy()
self.window.set_sensitive(True)
self.clear_levels()
### the methods below are signal handlers
def image_search_changed(self, *args, **kargs):
chooser = self.builder.get_object("pics1_chooser")
if not chooser.get_filename():
search = self.builder.get_object("search_path_chooser").get_filename()
chooser.set_current_folder(search)
def show_about(self, *args, **kargs):
self.about_popup.run()
self.about_popup.hide()
def show_add_files(self, *args, **kargs):
chooser = Gtk.FileChooserDialog(
"Select Level Files for Conversion", self.window,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
level_filter = Gtk.FileFilter()
level_filter.set_name("Graal Level Files")
level_filter.add_pattern("*.nw")
level_filter.add_pattern("*.graal")
level_filter.add_pattern("*.zelda")
chooser.add_filter(level_filter)
level_filter = Gtk.FileFilter()
level_filter.set_name("All Files")
level_filter.add_pattern("*")
chooser.add_filter(level_filter)
chooser.set_select_multiple(True)
chooser.set_current_folder(self.working_dir)
response = chooser.run()
if response == Gtk.ResponseType.OK:
paths = chooser.get_filenames()
for path in paths:
self.add_level(path)
self.refresh_levels_view()
path = paths[-1]
if os.path.isdir(path):
self.working_dir = path
else:
self.working_dir = os.path.split(path)[0]
chooser.destroy()
def clear_levels(self, *args, **kargs):
self.levels.clear()
self.levels_store.clear()
def file_drop_event(self, widget, drag_context, x, y, data, info, time):
uris = data.get_uris()
prefix = r'^file:///([A-Z]:/){0,1}'
for uri in uris:
if re.match(prefix, uri):
path = re.sub(prefix, '/', uri)
self.add_level(urllib.unquote(path))
self.refresh_levels_view()
def run_converter(self, *args, **kargs):
tmx_button = self.builder.get_object("tmx_radio")
png_button = self.builder.get_object("png_radio")
converter = None
suffix = None
if tmx_button.get_active():
converter = convert_to_tmx
suffix = "tmx"
if png_button.get_active():
converter = convert_to_png
suffix = "png"
self.levels
tileset = self.builder.get_object("pics1_chooser").get_filename()
search = self.builder.get_object("search_path_chooser").get_filename()
out = self.builder.get_object("output_path_chooser").get_filename()
if not tileset or not search or not out:
return
self.start_progress()
processed = 0
jobs = []
for path, level in self.levels:
level_path = os.path.join(path, level)
out_path = os.path.join(out, level) + '.' + suffix
jobs.append((level_path, tileset, search, out_path))
Thread(target=run_jobs, args=(self, converter, jobs)).start()
def shutdown(self, *args, **kargs):
Gtk.main_quit()
def run_main(args=sys.argv):
Gtk.init()
converter = ConverterWindow()
for path in args[1:]:
if os.path.isfile(path):
converter.add_level(path)
converter.refresh_levels_view()
Gtk.main()
if __name__ == "__main__":
run_main()