Imported Upstream version 2008.1+svn1656
[opeanno-debian-packaging.git] / editor / loaders.py
blobf8b3ce71f250717c742ad0e1778d35f495059f8c
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
22 from dbreader import DbReader
23 import fife
24 from serializers import WrongFileType
25 import os.path
27 fileExtensions = ('sqlite',)
28 _inited = False
30 from plugins.mapwizard import MapWizard
31 import plugins.plugin
32 class myMapWizard(plugins.plugin.Plugin):
33 def __init__(self, engine):
34 self.engine = engine
35 self.menu_items = { 'New Map' : self.new_map }
36 self.newMap = None
37 self.new_map()
39 def new_map(self):
40 self.map = _empty(self.engine)
41 self.newMap = self.map
42 plugins.mapwizard.MapWizard = myMapWizard
44 def _empty(engine):
45 global _inited
46 if not _inited:
47 _init(engine)
48 _inited = True
49 cellgrid = fife.SquareGrid(True)
50 cellgrid.thisown = 0
51 cellgrid.setRotation(0)
52 cellgrid.setXScale(1)
53 cellgrid.setYScale(1)
54 cellgrid.setXShift(0)
55 cellgrid.setYShift(0)
57 engine.getModel().deleteMaps()
58 map = engine.getModel().createMap("island")
60 layer = map.createLayer('ground', cellgrid)
61 layer.setPathingStrategy(fife.CELL_EDGES_AND_DIAGONALS)
62 view = engine.getView()
64 view.clearCameras()
65 cam = view.addCamera("main", layer, fife.Rect(0, 0, 1024, 768), fife.ExactModelCoordinate(0.0, 0.0, 0.0))
66 cam.setCellImageDimensions(32, 16)
67 cam.setRotation(315.0)
68 cam.setTilt(-60)
69 cam.setZoom(1)
71 return map
73 def _load(file, engine):
74 if not db("attach ? AS island", file).success:
75 raise WrongFileType(file)
77 map = _empty(engine)
78 map.setResourceFile(file)
79 layer = map.getLayer('ground')
81 nr = 0
82 already = []
83 for (x, y, ground_id) in db("select x, y, ground_id from island.ground"):
84 if (int(x), int(y)) not in already:
85 instance = layer.createInstance(engine.getModel().getObject(str(ground_id), 'ground'), fife.ModelCoordinate(int(x), int(y), 0), str(nr))
86 location = fife.Location(layer)
87 location.setLayerCoordinates(fife.ModelCoordinate(int(x + 1), int(y), 0))
88 instance.setFacingLocation(location)
89 fife.InstanceVisual.create(instance)
90 instance.thisown = 0
91 nr+=1
92 already.append((int(x), int(y)))
93 #except:
94 # raise WrongFileType(file)
96 db("detach island")
98 map.importDirs = []
99 return map
101 def _save(file, engine, map):
102 if not db("attach ? AS island", file).success:
103 raise WrongFileType(file)
105 try:
106 if not db('create table island.ground (x INTEGER NOT NULL, y INTEGER NOT NULL, ground_id INTEGER NOT NULL)').success:
107 raise int #just throw anything, int was the first i thought of...
108 except:
109 db('delete from island.ground')
111 try:
112 if not db('CREATE TABLE island.island_properties (name TEXT PRIMARY KEY NOT NULL, value TEXT NOT NULL)').success:
113 raise int
114 except:
115 db('delete from island.island_properties')
117 layer = map.getLayer('ground')
119 instances = layer.getInstances()
121 already = []
122 for instance in instances:
123 coord = instance.getLocation().getLayerCoordinates()
124 x,y = int(coord.x), int(coord.y)
125 if (int(x), int(y)) not in already:
126 ground_id = int(instance.getObject().getId().split(':')[0])
127 rotation = instance.getRotation()
128 if rotation != 0:
129 ground_id = db('select rowid from data.ground where animation_45 = (select animation_%d from data.ground where rowid = ? limit 1) limit 1' % ((rotation + 45) % 360,), ground_id)[0][0]
130 db('insert into island.ground (x,y,ground_id) values (?, ?, ?)',x,y,ground_id)
131 already.append((int(x), int(y)))
133 db("detach island")
135 def pathsplit(p, rest=[]):
136 (h,t) = os.path.split(p)
137 if len(h) < 1: return [t]+rest
138 if len(t) < 1: return [h]+rest
139 return pathsplit(h,[t]+rest)
141 def commonpath(l1, l2, common=[]):
142 if len(l1) < 1: return (common, l1, l2)
143 if len(l2) < 1: return (common, l1, l2)
144 if l1[0] != l2[0]: return (common, l1, l2)
145 return commonpath(l1[1:], l2[1:], common+[l1[0]])
147 def relpath(p1, p2):
148 (common,l1,l2) = commonpath(pathsplit(p1), pathsplit(p2))
149 p = []
150 if len(l1) > 0:
151 p = [ '../' * len(l1) ]
152 p = p + l2
153 return os.path.join( *p )
155 def _init(engine):
156 global db
157 db = DbReader(':memory:')
158 db("attach ? AS data", os.path.abspath(os.path.dirname(__file__) + '/../content/openanno.sqlite'))
160 for (ground_id, animation_45, animation_135, animation_225, animation_315) in db("SELECT rowid, (select file from data.animation where animation_id = animation_45 limit 1), (select file from data.animation where animation_id = animation_135 limit 1), (select file from data.animation where animation_id = animation_225 limit 1), (select file from data.animation where animation_id = animation_315 limit 1) FROM data.ground"):
161 print 'Loading ground #%i ...' % (ground_id)
162 name = os.path.basename(animation_45)
163 object = engine.getModel().createObject(str(ground_id) + ":" +str(name), 'ground')
164 object.thisown = 0
165 fife.ObjectVisual.create(object)
166 visual = object.get2dGfxVisual()
168 for rotation, file in [(45, animation_45), (135, animation_135), (225, animation_225), (315, animation_315)]:
169 img = engine.getImagePool().addResourceFromFile(relpath(os.getcwd(), os.path.abspath(os.path.dirname(__file__) + '/../' + file)))
170 visual.addStaticImage(int(rotation), img)
171 img = engine.getImagePool().getImage(img)
172 img.setXShift(0)
173 img.setYShift(0)
175 def loadMapFile(path, engine, content = ''):
176 global _inited
177 if not _inited:
178 _init(engine)
179 _inited = True
180 return _load(path, engine)
182 def saveMapFile(path, engine, map, importList=[]):
183 global _inited
184 if not _inited:
185 _init(engine)
186 _inited = True
187 return _save(path, engine, map)
189 def loadImportFile(path, engine):
190 global _inited
191 if not _inited:
192 _init(engine)
193 _inited = True
195 def loadImportDir(path, engine):
196 global _inited
197 if not _inited:
198 _init(engine)
199 _inited = True
201 def loadImportDirRec(path, engine):
202 global _inited
203 if not _inited:
204 _init(engine)
205 _inited = True