annotate engine/extensions/loaders.py @ 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 - this can be used to prepare other parts of your client (e. g. load ingame gui) or to show a maploading progress bar USAGE (example): - map = loadMapFile(mapfile, engine, my_callback) - def mycallback(msg, percent): print msg; print percent
author chewie@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 03 Oct 2008 12:14:39 +0000
parents ab09325f901e
children e7a431577c95
rev   line source
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
1 # coding: utf-8
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
2 # 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
3
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
4 import fife
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
5
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
6 from serializers.xmlmap import XMLMapLoader
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
7 from serializers import WrongFileType, NameClash
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
8
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
9 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
10
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
11 fileExtensions = ('xml',)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
12
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
13 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
14 """ 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
15 - 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
16 - 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
17 - 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
18 - 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
19 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
20 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
21
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
22 @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
23 """
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
24 map_loader = XMLMapLoader(engine, callback)
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
25 return map_loader.loadResource(fife.ResourceLocation(path))
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
26
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
27 def loadImportFile(path, engine):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28 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
29 res = None
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
30 try:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31 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
32 print 'imported object file ' + path
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
33 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
34 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
35 # 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
36 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
37 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
38 # 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
39 return res
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
40
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
41 def loadImportDir(path, engine):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 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
43 loadImportFile('/'.join([path, file]), engine)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
45 def loadImportDirRec(path, engine):
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46 loadImportDir(path, engine)
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
48 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
49 loadImportDirRec('/'.join([path, dir]), engine)