Cell Selection Renderer

From FIFE development wiki
Jump to: navigation, search

Introduction

The CellSelectionRenderer is a helper renderer. It can be used to highlight locations / cells on your grid. FIFedit uses it to show you which cell is selected. It draw a frame around the cell.

Usage

Like all other renderers, you can enable specific layers for this one. Activated layers are then checked for selected cells, the corresponding cells are colored with the default color (red, rgb values: 255,0,0) which you can change by using setColor().


Options

getName()

string; gets the name of the renderer


setColor(r, g, b)

changes the used color

r,g,b
integer; color values, range 0-255


selectLocation(location)

adds a location to the list of selected locations

location
fife.Location; the used location


deselectLocation(location)

removes a location from the list of selected locations

location
fife.Location; the used location


getLocations()

returns a list of used locations


reset()

removes all selected locations

Example

Here we add a 3 locations, removes one so that two cells are highlighted. Additional we print the coordintes.

 # self.layer is an instance of fife.Layer()
 # self.camera is an instance of fife.Camera()
 
 #get renderer
 renderer = fife.CellSelectionRenderer.getInstance(self.camera)
 
 #active a layer for renderer
 renderer.addActiveLayer(self.layer)
 
 #enable renderer
 renderer.setEnabled(True)
 
 #change color to blue
 renderer.setColor(0, 0, 255)
 
 #create ModelCoordinate and first Location
 coordinate = fife.ModelCoordinate(0, 0)
 loc1 = fife.Location(self.layer)
 loc1.setLayerCoordinates(coordinate)
 
 #add location to selection
 renderer.selectLocation(loc1)
 
 #create second Location
 loc2 = loc1
 coordinates.x = 5
 coordinates.y = 3
 loc2.setLayerCoordinates(coordinates)
 
 #add location to selection
 renderer.selectLocation(loc2)
 
 #create third Location
 loc3 = loc1
 coordinates.x = -3
 coordinates.y = -5
 loc3.setLayerCoordinates(coordinates)
 
 #add location to selection
 renderer.selectLocation(loc3)
 
 #remove first location
 renderer.deselectLocation(loc1)
 
 #get selected locations and print coordinates
 locations = renderer.getLocations()
 for loc in locations:
     print loc.getLayerCoordinates()