예제 #1
0
 def newLevel(self, width, height):
     self.graph = SceneGraph(self.size, width, height, debugMode=True)
     self.graph.forceFocus = True
예제 #2
0
class SceneController(object):
    def __init__(self, window):
        self.tools = {
            "pan" : PanTool(self),
            "plotline" : PlotLineTool(self),
            "placeitem" : PlaceItemTool(self),
            "select" : SelectTool(self, window)
        }
        #clears to a grey.
        glClearColor(0.4,0.4,0.4,0.)
        self.graph = None
        self.edited = False
        self.size = (0,0)
        self.scale = 1.0
        self.keys = Keys(self)
        self.mouseCoord = (0,0)
        self.grid = Grid(self, window)
        self.currentLayer = None
        self.currentTool = None
        self.keys = Keys(self)
        window.push_handlers(self)
        window.push_handlers(self.keys)
        
    def setActiveLayer(self, name):
        # sets the reference to the current layer
        if name == "none":
            self.currentLayer = None
            self.currentTool = None
            return
        try:
            self.currentLayer = self.graph.layers[name]
        except KeyError:
            print "Set Active Layer Key Error"
                    
        
    def setCurrentTool(self, tool):
        for t in self.tools:
            self.tools[t].deactivate()
        # search tool list for name and set
        # tool with given name to current tool.
        try:
            self.currentTool = self.tools[tool].activate()
        except KeyError:
            print "Set Current Tool Key Error"
        
        
    def newLevel(self, width, height):
        self.graph = SceneGraph(self.size, width, height, debugMode=True)
        self.graph.forceFocus = True
    
    def loadLevel(self, fileName):
        print "graph loaded"
        #TODO delete old graph and assets
        #self.graph = SceneGraph.fromMapFile(fileName, self.size)
        #self.graph.forceFocus = True
            
    def resize(self, width, height):
        self.size = (width, height)
        if self.graph:
            self.graph.viewportWidth = width
            self.graph.viewportHeight = height
            
    def update(self, dt):
        if self.graph:
            self.pollPanKeys(dt)
            self.graph.update(dt)
    
    def draw(self):
        if self.graph:
            glPushMatrix()
            glScalef(self.scale, self.scale, 1.0)
            self.graph.draw()
            glPopMatrix()
        self.grid.batch.draw()
            
    
    # input event handlers      
    def pollPanKeys(self, dt):
        moveRate = dt * 400
        if self.keys[key.UP]:
            self.graph.moveFocus(y=-moveRate)
        if self.keys[key.DOWN]:
            self.graph.moveFocus(y=moveRate)
        if self.keys[key.LEFT]:
            self.graph.moveFocus(moveRate)
        if self.keys[key.RIGHT]:
            self.graph.moveFocus(-moveRate)
            
    def key_press(self, symbol, modifiers):
        if symbol == key.HOME:
            if self.graph != None:
                self.graph.setFocus(0,0)
        if self.currentTool is not None:
        	self.currentTool.key_press(symbol, modifiers)
    
    def key_release(self, symbol, modifiers):
        if self.currentTool is not None:
        	self.currentTool.key_release(symbol, modifiers)
               
    def on_mouse_motion(self, x, y, dx, dy):
        self.mouseCoord = (x,y)
        if self.currentTool is not None:
        	self.currentTool.on_mouse_motion(x, y, dx, dy)
        
    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        if self.currentTool == None:
            if self.graph != None:
                self.graph.moveFocus(dx, dy)
        if self.currentTool is not None:
        	self.currentTool.on_mouse_drag(x, y, dx, dy, buttons, modifiers)
        
    def on_mouse_press(self,x, y, button, modifiers):
        if self.currentTool is not None:
        	self.currentTool.on_mouse_press(x, y, button , modifiers)
    
    def on_mouse_scdfroll(self,x, y, scroll_x, scroll_y):
        if self.currentTool is not None:
        	self.currentTool.on_mouse_scdfroll(x, y, scroll_x, scroll_y)
    
    def on_mouse_release(self, x, y, button, modifiers):
        if self.currentTool is not None:
        	self.currentTool.on_mouse_release(x, y, button, modifiers)