comparison tools/editor/scripts/gui/layerdialog.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children 9d94f4676d17
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 from fife import fife
25 from fife.extensions import pychan
26
27 class LayerDialog(object):
28 """ The B{LayerDialog} provides a gui dialog for creating and editing layers.
29
30 FIXME:
31 - gridtypes can only be square for now
32 - pathing strategy
33 """
34 def __init__(self, engine, map, callback=None, onCancel=None, layer=None):
35 self.engine = engine
36 self.model = engine.getModel()
37 self.map = map
38 self.layer = layer
39 self.callback = callback
40 self.onCancel = onCancel
41 self._widget = pychan.loadXML('gui/layerdialog.xml')
42
43 # TODO: Add access method for adopted grid types?
44 self._widget.findChild(name="gridBox").items = ['square', 'hexagonal']
45
46 # TODO: Ditto for pather?
47 self._widget.findChild(name="pathingBox").items = ['cell_edges_only', 'cell_edges_and_diagonals', 'freeform']
48
49 if layer:
50 cg = layer.getCellGrid()
51 cgtype = 0
52 if cg.getType() == 'hexagonal':
53 cgtype = 1
54
55 self._widget.distributeData({
56 "layerBox" : unicode(layer.getId()),
57 "xScaleBox" : unicode(cg.getXScale()),
58 "yScaleBox" : unicode(cg.getYScale()),
59 "rotBox" : unicode(cg.getRotation()),
60 "xOffsetBox" : unicode(cg.getXShift()),
61 "yOffsetBox" : unicode(cg.getYShift()),
62 "transBox" : unicode(layer.getLayerTransparency())
63 })
64
65 self._widget.findChild(name="pathingBox").selected = int(layer.getPathingStrategy())
66 self._widget.findChild(name="gridBox").selected = int(cgtype)
67 else:
68 self._widget.findChild(name="pathingBox").selected = 0
69 self._widget.findChild(name="gridBox").selected = 0
70
71 self._widget.mapEvents({
72 'okButton' : self._finished,
73 'cancelButton' : self._cancelled
74 })
75
76 self._widget.show()
77
78 def _cancelled(self):
79 """ """
80 if self.onCancel:
81 self.onCancel()
82 self._widget.hide()
83
84 def _finished(self):
85 """ """
86 # Collect and validate data
87 layerId = self._widget.collectData('layerBox')
88 if layerId == '':
89 print 'Please enter a layer id.'
90 return
91
92 try:
93 x_offset = float(self._widget.collectData('xOffsetBox'))
94 y_offset = float(self._widget.collectData('yOffsetBox'))
95 except ValueError:
96 print 'Please enter integer or decimal values for offset.'
97 return
98
99 try:
100 x_scale = float(self._widget.collectData('xScaleBox'))
101 y_scale = float(self._widget.collectData('yScaleBox'))
102 except ValueError:
103 print 'Please enter integer or decimal values for scale.'
104 return
105
106 try:
107 rotation = float(self._widget.collectData('rotBox'))
108 except ValueError:
109 print 'Please enter integer or decimal value for rotation.'
110 return
111
112 try:
113 transparency = int(self._widget.collectData('transBox'))
114 except ValueError:
115 print 'Please enter an integer value in the range of 0-255 for transparency.'
116 return
117
118
119 #Clamp the transparency value between 0 and 255
120 if transparency < 0:
121 transparency = 0
122 if transparency > 255:
123 transparency = 255
124
125 grid_type = int(self._widget.collectData('gridBox'))
126 pathing = int(self._widget.collectData('pathingBox'))
127
128 if grid_type == 0:
129 grid_type = "square"
130 else:
131 grid_type = "hexagonal"
132
133 # Set up layer
134 layer = self.layer
135 cellgrid = None
136
137 cellgrid = self.model.getCellGrid(grid_type)
138 if not cellgrid:
139 print "Invalid grid type"
140 return
141
142 cellgrid.setRotation(rotation)
143 cellgrid.setXScale(x_scale)
144 cellgrid.setYScale(y_scale)
145 cellgrid.setXShift(x_offset)
146 cellgrid.setYShift(y_offset)
147
148 if not self.layer:
149 try:
150 layer = self.map.createLayer(str(layerId), cellgrid)
151
152 except fife.NameClash:
153 print 'The layer ' + str(layerId) + ' already exists!'
154 return
155 else:
156 layer.setCellGrid(cellgrid)
157 try:
158 layer.setId(str(layerId))
159 except fife.NameClash:
160 print 'The layer ' + str(layerId) + ' already exists!'
161 return
162
163 layer.setPathingStrategy(pathing)
164 layer.setLayerTransparency(transparency)
165
166 self.engine.getView().resetRenderers()
167
168 # Hide dialog and call back
169 self._widget.hide()
170
171 if self.callback:
172 pychan.tools.applyOnlySuitable(self.callback, layer=layer)