Package arcmap :: Module mapgrid
[hide private]
[frames] | no frames]

Source Code for Module arcmap.mapgrid

  1  ################################################################################ 
  2  # Authors: Brian Schott (Sir Alaran) 
  3  # Copyright: Brian Schott (Sir Alaran) 
  4  # Date: Sep 22 2009 
  5  # License: 
  6  # 
  7  # This program is free software: you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation, either version 3 of the License, or 
 10  # (at your option) any later version. 
 11  # 
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 15  # GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 19  ################################################################################ 
 20   
 21   
 22  """ 
 23  Module for drawing the map to the screen 
 24  """ 
 25   
 26  __docformat__ = "epytext" 
 27   
 28  import math 
 29  import logging 
 30  import cairo 
 31  import gtk 
 32   
 33  import dialogs 
 34  import mapcontroller 
 35  import tilegrid 
 36  import editortools 
 37   
 38   
39 -class MapGrid(tilegrid.TileGrid, mapcontroller.MapListener):
40 """ Handles drawing the map to the screen """ 41 42 # map = a reference to the map that the grid will be editing 43 # selection = the selection from the
44 - def __init__(self, controller, statusBar):
45 """ 46 @type controller: MapController 47 @param controller: the map controller 48 @type statusBar: gtk.StatusBar 49 @param statusBar: status bar to update 50 """ 51 mapcontroller.MapListener.__init__(self, controller) 52 controller.setThumbnailSource(self) 53 54 # Currently selected shape 55 self.selectedShape = None 56 57 # list of cairo.ImageSurface 58 self.layers = [] 59 60 # Access to the parent window's status bar 61 self.__statusBar = statusBar 62 self.__contextID = self.__statusBar.get_context_id("Map Grid") 63 self.__lastMessage = -1 64 65 pixelWidth = controller.mapWidth() * controller.mapTileSize() 66 pixelHeight = controller.mapHeight() * controller.mapTileSize() 67 tilegrid.TileGrid.__init__(self, pixelWidth, pixelHeight, 68 controller.mapTileSize()) 69 self.layers = self.getController().drawLayers() 70 self.redrawBuffer() 71 72 self.addTool(editortools.TileDrawTool(controller), 73 editortools.TILE_DRAW_ID) 74 self.addTool(editortools.TileDeleteTool(controller), 75 editortools.TILE_DELETE_ID) 76 self.addTool(editortools.PhysicsSelectTool(controller), 77 editortools.PHYSICS_SELECT_ID) 78 self.addTool(editortools.CircleDrawTool(controller), 79 editortools.CIRCLE_DRAW_ID) 80 self.addTool(editortools.PolygonDrawTool(controller), 81 editortools.POLYGON_DRAW_ID) 82 self.addTool(editortools.LightDrawTool(controller), 83 editortools.LIGHT_DRAW_ID)
84
85 - def specialRedraw(self, context):
86 # Draw tile layers 87 for index, layer in enumerate(self.layers): 88 if self.getController().mapLayerVisibility(index): 89 context.set_source_surface(layer) 90 context.paint()
91
92 - def setSize(self, width, height, xOffset, yOffset):
93 tilegrid.TileGrid.setSize(self, width, height, 0, 0) 94 newLayers = [] 95 for oldLayer in self.layers: 96 newLayer = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) 97 context = cairo.Context(newLayer) 98 99 context.set_source_surface(oldLayer, 0, 0) 100 # WARNING: Do not mess with the magical ternary operations! 101 context.rectangle(xOffset if xOffset > 0 else 0, 102 yOffset if yOffset > 0 else 0, 103 oldLayer.get_width(), 104 oldLayer.get_height()) 105 106 context.fill() 107 newLayers.append(newLayer) 108 self.layers = newLayers
109
110 - def addTool(self, tool, toolID):
111 tilegrid.TileGrid.addTool(self, tool, toolID) 112 self.__lastMessage = self.__statusBar.push(self.__contextID, 113 self.getToolInstructions())
114 115
116 - def toolSelect(self, toolID):
117 tilegrid.TileGrid.toolSelect(self, toolID) 118 self.__statusBar.pop(self.__lastMessage) 119 self.__lastMessage = self.__statusBar.push(self.__contextID, 120 self.getToolInstructions()) 121 self.redrawBuffer() 122 self.queue_draw()
123 124 ############################################################################ 125 # MapListener code 126 ############################################################################ 127
128 - def listenSetVisibilty(self, index, visible):
129 self.redrawBuffer() 130 self.queue_draw()
131
132 - def listenResize(self, width, height, xOffset, yOffset):
133 self.layers = self.getController().drawLayers() 134 ts = self.getController().mapTileSize() 135 136 self.setSize(width * ts, height * ts, xOffset * ts, yOffset * ts) 137 self.redrawBuffer() 138 self.queue_draw()
139
140 - def listenAddLayer(self, layerName):
141 c = self.getController() 142 width = c.mapWidth() * c.mapTileSize() 143 height = c.mapHeight() * c.mapTileSize() 144 self.layers.append(cairo.ImageSurface(cairo.FORMAT_ARGB32, width, 145 height)) 146 self.redrawBuffer() 147 self.queue_draw()
148
149 - def listenRemoveLayer(self, index):
150 del self.layers[index] 151 self.redrawBuffer() 152 self.queue_draw()
153
154 - def listenSwapLayers(self, index1, index2):
155 tempLayer = self.layers[index1] 156 self.layers[index1] = self.layers[index2] 157 self.layers[index2] = tempLayer 158 self.redrawBuffer() 159 self.queue_draw()
160
161 - def listenAddTile(self, surface, x, y, z):
162 ts = self.getController().mapTileSize() 163 context = cairo.Context(self.layers[z]) 164 context.set_source_surface(surface, x * ts, y * ts) 165 context.rectangle(x * ts, y * ts, ts, ts) 166 context.set_operator(cairo.OPERATOR_SOURCE) 167 context.fill() 168 self.redrawBuffer() 169 self.queue_draw()
170
171 - def listenRemoveTile(self, x, y, z):
172 ts = self.getController().mapTileSize() 173 context = cairo.Context(self.layers[z]) 174 context.set_source_rgba(0.0, 0.0, 0.0, 0.0) 175 context.rectangle(x * ts, y * ts, ts, ts) 176 context.set_operator(cairo.OPERATOR_SOURCE) 177 context.fill() 178 self.redrawBuffer() 179 self.queue_draw()
180
181 - def listenUndoRedo(self):
182 self.redrawBuffer() 183 self.queue_draw()
184 185 ############################################################################ 186 # End MapListener code 187 ############################################################################ 188