Mercurial > fife-parpg
annotate engine/extensions/loaders.py @ 362:6da97ffede18
Patch by amo-ej1 (Elie de Brauwer):
* Fixes the map editor's save map as functionality
* Close #382
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 18 Oct 2009 20:13:21 +0000 |
parents | dfd48d49c044 |
children | 91e352f81f63 |
rev | line source |
---|---|
331
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
1 # -*- coding: utf-8 -*- |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
2 |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
3 # #################################################################### |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
4 # Copyright (C) 2005-2009 by the FIFE team |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
5 # http://www.fifengine.de |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
6 # This file is part of FIFE. |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
7 # |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
8 # FIFE is free software; you can redistribute it and/or |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
9 # modify it under the terms of the GNU Lesser General Public |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
10 # License as published by the Free Software Foundation; either |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
11 # version 2.1 of the License, or (at your option) any later version. |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
12 # |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
13 # This library is distributed in the hope that it will be useful, |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
16 # Lesser General Public License for more details. |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
17 # |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
18 # You should have received a copy of the GNU Lesser General Public |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
19 # License along with this library; if not, write to the |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
20 # Free Software Foundation, Inc., |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
22 # #################################################################### |
48c99636453e
Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
146
diff
changeset
|
23 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
24 # Loader interface for FIFE's native xml format. |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
25 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
26 import fife |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
27 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
28 from serializers.xmlmap import XMLMapLoader |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
29 from serializers import WrongFileType, NameClash |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
30 |
141
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
31 from serializers.xmlobject import XMLObjectLoader |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
32 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
33 fileExtensions = ('xml',) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
34 |
141
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
35 def loadMapFile(path, engine, callback=None): |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
36 """ load map file and get (an optional) callback if major stuff is done: |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
37 - map creation |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
38 - parsed imports |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
39 - parsed layers |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
40 - parsed cameras |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
41 the callback will send both a string and a float (which shows |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
42 the overall process), callback(string, float) |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
43 |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
44 @return map : map object |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
45 """ |
5e85737281bc
- some modification to loaders and serializers/xmlmap, now you can set a callback which informs you about finished parts of the maploading process
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
10
diff
changeset
|
46 map_loader = XMLMapLoader(engine, callback) |
145
e7a431577c95
Cleaned the basic model up. Code is cleaner now and a bit faster.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
141
diff
changeset
|
47 map = map_loader.loadResource(fife.ResourceLocation(path)) |
146
54b3984e1afc
The getIndex function was a hack that shadowed another bug
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
145
diff
changeset
|
48 print "--- Loading map took: ", map_loader.time_to_load, " seconds." |
145
e7a431577c95
Cleaned the basic model up. Code is cleaner now and a bit faster.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
141
diff
changeset
|
49 return map |
e7a431577c95
Cleaned the basic model up. Code is cleaner now and a bit faster.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
141
diff
changeset
|
50 |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
51 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
52 def loadImportFile(path, engine): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
53 object_loader = XMLObjectLoader(engine.getImagePool(), engine.getAnimationPool(), engine.getModel(), engine.getVFS()) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
54 res = None |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
55 try: |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
56 res = object_loader.loadResource(fife.ResourceLocation(path)) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
57 print 'imported object file ' + path |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
58 except WrongFileType: |
10
ab09325f901e
Camera was attached to instance on a different layer, resulting in strange scrolling. Fixed it, and added a check to prevent this happening again (attach-to-other-layer fails and logs a warning).
jwt@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
59 pass |
ab09325f901e
Camera was attached to instance on a different layer, resulting in strange scrolling. Fixed it, and added a check to prevent this happening again (attach-to-other-layer fails and logs a warning).
jwt@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
60 # print 'ignored non-object file ' + path |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
61 except NameClash: |
10
ab09325f901e
Camera was attached to instance on a different layer, resulting in strange scrolling. Fixed it, and added a check to prevent this happening again (attach-to-other-layer fails and logs a warning).
jwt@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
62 pass |
ab09325f901e
Camera was attached to instance on a different layer, resulting in strange scrolling. Fixed it, and added a check to prevent this happening again (attach-to-other-layer fails and logs a warning).
jwt@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
0
diff
changeset
|
63 # print 'ignored already loaded file ' + path |
0
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
64 return res |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
65 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
66 def loadImportDir(path, engine): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
67 for file in filter(lambda f: f.split('.')[-1] == 'xml', engine.getVFS().listFiles(path)): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
68 loadImportFile('/'.join([path, file]), engine) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
69 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
70 def loadImportDirRec(path, engine): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
71 loadImportDir(path, engine) |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
72 |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
73 for dir in filter(lambda d: not d.startswith('.'), engine.getVFS().listDirectories(path)): |
4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
74 loadImportDirRec('/'.join([path, dir]), engine) |