1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
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
24 from serializers
import WrongFileType
27 fileExtensions
= ('sqlite',)
30 from plugins
.mapwizard
import MapWizard
32 class myMapWizard(plugins
.plugin
.Plugin
):
33 def __init__(self
, engine
):
35 self
.menu_items
= { 'New Map' : self
.new_map
}
40 self
.map = _empty(self
.engine
)
41 self
.newMap
= self
.map
42 plugins
.mapwizard
.MapWizard
= myMapWizard
49 cellgrid
= fife
.SquareGrid(True)
51 cellgrid
.setRotation(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()
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)
73 def _load(file, engine
):
74 if not db("attach ? AS island", file).success
:
75 raise WrongFileType(file)
78 map.setResourceFile(file)
79 layer
= map.getLayer('ground')
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
)
92 already
.append((int(x
), int(y
)))
94 # raise WrongFileType(file)
101 def _save(file, engine
, map):
102 if not db("attach ? AS island", file).success
:
103 raise WrongFileType(file)
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...
109 db('delete from island.ground')
112 if not db('CREATE TABLE island.island_properties (name TEXT PRIMARY KEY NOT NULL, value TEXT NOT NULL)').success
:
115 db('delete from island.island_properties')
117 layer
= map.getLayer('ground')
119 instances
= layer
.getInstances()
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())
127 rotation
= instance
.getRotation()
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
)))
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]])
148 (common
,l1
,l2
) = commonpath(pathsplit(p1
), pathsplit(p2
))
151 p
= [ '../' * len(l1
) ]
153 return os
.path
.join( *p
)
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 object = engine
.getModel().createObject(str(ground_id
), 'ground')
164 fife
.ObjectVisual
.create(object)
165 visual
= object.get2dGfxVisual()
167 for rotation
, file in [(45, animation_45
), (135, animation_135
), (225, animation_225
), (315, animation_315
)]:
168 img
= engine
.getImagePool().addResourceFromFile(relpath(os
.getcwd(), os
.path
.abspath(os
.path
.dirname(__file__
) + '/../' + file)))
169 visual
.addStaticImage(int(rotation
), img
)
170 img
= engine
.getImagePool().getImage(img
)
174 def loadMapFile(path
, engine
, content
= ''):
179 return _load(path
, engine
)
181 def saveMapFile(path
, engine
, map, importList
=[]):
186 return _save(path
, engine
, map)
188 def loadImportFile(path
, engine
):
194 def loadImportDir(path
, engine
):
200 def loadImportDirRec(path
, engine
):