def __init__(self, gm): self.mode = "collision" MapManagerBase.__init__(self, gm) self.camHandler = self.gm.editorCam # while self.mode == "edit", self.objectMode can become : # drag, rotating, scaling self.objectMode = None self.selectedObj = None self.gui = EditorGui(self)
class MapEditor(MapManagerBase): def __init__(self, gm): self.mode = "collision" MapManagerBase.__init__(self, gm) self.camHandler = self.gm.editorCam # while self.mode == "edit", self.objectMode can become : # drag, rotating, scaling self.objectMode = None self.selectedObj = None self.gui = EditorGui(self) #----------------------------- # modes and input def start(self): #print "Starting editor" self.gui.show() self.startAccept() self.task = taskMgr.add(self.update, "MapEditorTask") #if self.map.bgMusic: # self.map.bgMusic.stop() #self.map.collisionShow() self.map.collisionGrid.rebuild() print "rebuilding grid" def stop(self): #print "Stopping editor" self.gui.hide() taskMgr.remove(self.task) self.ignoreAll() #self.map.collisionHide() self.map.collisionGrid.np.flattenStrong() print "flattening grid" self.camHandler.stop() def startAccept(self): for key in [ "mouse1", "mouse3", FORWARD, BACKWARD, STRAFE_LEFT, STRAFE_RIGHT, TURN_LEFT, TURN_RIGHT, UP, DOWN, EDITOR_DOWN, EDITOR_LEFT, EDITOR_RIGHT, EDITOR_UP, "h", "b", "t", "g" ]: self.keyDic[key] = 0 self.accept(key, self.setKey, [key, 1]) keyUp = key + "-up" self.accept(keyUp, self.setKey, [key, 0]) self.setMode(self.mode) self.accept("space", self.toggle) self.accept(SAVE, self.save) self.accept(OPEN, self.load, ["maps/mapCode.txt"]) def setMode(self, mode="collision"): if mode == "collision": msg = "Editor switched to collision mode" self.mode = "collision" self.gui.setInfo(msg) for key in ["mouse1", "mouse2", "mouse3"]: self.keyDic[key] = 0 self.accept(key, self.setKey, [key, 1]) keyUp = key + "-up" self.accept(keyUp, self.setKey, [key, 0]) self.accept(CLEAR_COLLISION, self.map.clearCollision) self.accept(FILL_COLLISION, self.map.fillCollision) elif mode == "object": msg = "Editor switched to object mode" self.mode = "object" self.gui.setInfo(msg) self.accept("mouse1", self.onClickObject) # left click self.accept("mouse2", self.onClickObject2) # scroll click self.accept("mouse3", self.onClickObject3) # right click self.ignore(CLEAR_COLLISION) self.ignore(FILL_COLLISION) self.accept("wheel_up", self.camHandler.moveHeight, [-0.05]) self.accept("wheel_down", self.camHandler.moveHeight, [0.05]) def toggle(self): if self.mode == "collision": self.setMode("object") elif self.mode == "object": self.setMode("collision") def stopObjectAction(self): self.objectMode = None self.selectedObj = None self.gui.objectMenu.hide() self.startAccept() def startDrag(self, obj, extraArgs=[]): self.gui.objectMenu.hide() self.objectMode = "drag" self.gui.setInfo("dragging") self.selectedObj = obj self.accept("mouse1", self.stopDrag) def stopDrag(self, extraArgs=[]): self.gui.setInfo("stopped dragging") self.stopObjectAction() def startMoveZ(self, obj, extraArgs=[]): self.gui.objectMenu.hide() self.objectMode = "moveZ" self.gui.setInfo("moving object Z") self.selectedObj = obj self.accept("mouse1", self.stopMoveZ) def stopMoveZ(self, extraArgs=[]): self.gui.setInfo("stopped moving object Z") self.stopObjectAction() def startRotate(self, obj, extraArgs=[]): self.gui.objectMenu.hide() self.objectMode = "rotate" self.gui.setInfo("rotating object") self.selectedObj = obj self.accept("mouse1", self.stopRotate) def stopRotate(self, extraArgs=[]): self.gui.setInfo("stopped rotating object") self.stopObjectAction() def startScale(self, obj, extraArgs=[]): self.gui.objectMenu.hide() self.objectMode = "scale" self.gui.setInfo("scaling object") self.selectedObj = obj self.accept("mouse1", self.stopScale) def stopScale(self, extraArgs=[]): self.gui.setInfo("stopped scaling object") self.stopObjectAction() def onClickObject(self): # click on MapObject : name = self.getHoverObjectName() if name is not None: print "map editor : left click on %s" % (name) else: print "map editor : left click on nothing" def onClickObject2(self): # click on MapObject : name = self.getHoverObjectName() if name is not None: print "map editor : middle click on %s" % (name) else: print "map editor : middle click on nothing" def onClickObject3(self): # click on MapObject : name = self.getHoverObjectName() if name is not None: print "map editor : right click on %s" % (name) if base.mouseWatcherNode.hasMouse(): mpos = base.mouseWatcherNode.getMouse() self.gui.openObjectMenu(self.map.mapObjects[name], mpos) self.gui.objectMenu.buttons[1].bind(DGG.B1PRESS, self.startDrag, [self.map.mapObjects[name]]) self.gui.objectMenu.buttons[2].bind(DGG.B1PRESS, self.startRotate, [self.map.mapObjects[name]]) self.gui.objectMenu.buttons[3].bind(DGG.B1PRESS, self.startMoveZ, [self.map.mapObjects[name]]) self.gui.objectMenu.buttons[4].bind(DGG.B1PRESS, self.startScale, [self.map.mapObjects[name]]) self.gui.objectMenu.buttons[5].bind(DGG.B1PRESS, self.duplicateMapObject, [self.map.mapObjects[name]]) self.gui.objectMenu.buttons[6].bind(DGG.B1PRESS, self.removeMapObject, [name]) else: print "map editor : right click on nothing" self.gui.objectMenu.hide() #----------------------------- # map file handling def save(self): msg = "Editor : saving map" self.gui.setInfo(msg) self.map.save(self.map.filename) def load(self, filename, extraArgs=[]): #print "Editor : loading map %s" % (filename) msg = "Loading map " + filename + "... Please wait..." self.gui.setInfo(msg) self.gm.load(filename) #----------------------------- # map objects def addMapObject(self, genre, name, pos=(0,0,0), hpr=(0,0,0), scale=(1,1,1)): self.map.addMapObject(genre, name, pos, hpr, scale) def addNewMapObject(self, genre, extraArgs=[]): self.gui.addObjectMenu.retract() name = self.map.getAvailableName(genre) self.addMapObject(genre, name) obj = self.map.mapObjects[name] self.setMode("object") self.startDrag(obj) def duplicateMapObject(self, obj, extraArgs=[]): self.gui.addObjectMenu.retract() genre = obj.genre name = self.map.getAvailableName(genre) self.addMapObject(genre, name) newObj = self.map.mapObjects[name] newObj.setScale(obj.getScale()) newObj.setRot(obj.getRot()) self.setMode("object") self.startDrag(newObj) def removeMapObject(self, name, extraArgs=[]): if name in self.map.mapObjects: self.stopObjectAction() self.map.removeMapObject(name) #self.map.mapObjects[name].destroy() #del self.map.mapObjects[name] def setMapObjectPos(self, name, x, y, z=0): if name in self.mapObjects: self.map.mapObjects[name].setPos(x, y, z) #----------------------------- # map collisions def clearCollision(self, args=[]): if self.mode == "collision": self.map.collisionGrid.clear() def fillCollision(self, args=[]): if self.mode == "collision": self.map.collisionGrid.fill() def addCollision(self, x, y): self.map.collisionGrid.showTile(x, y) def removeCollision(self, x, y): self.map.collisionGrid.hideTile(x, y) def checkObj(self, dt=0.01): if self.keyDic[EDITOR_LEFT]: self.selectedObj.rotate(dt) elif self.keyDic[EDITOR_RIGHT]: self.selectedObj.rotate(-dt) if self.keyDic[EDITOR_UP]: self.selectedObj.moveZ(dt) elif self.keyDic[EDITOR_DOWN]: self.selectedObj.moveZ(-dt) def scaleObj(self, dt=0.01): if self.keyDic[EDITOR_UP]: self.selectedObj.scale(dt) elif self.keyDic[EDITOR_DOWN]: self.selectedObj.scale(-dt) def updateCam(self, dt=0.01): if self.keyDic[FORWARD]: self.camHandler.forward(dt) if self.keyDic[BACKWARD]: self.camHandler.backward(dt) if self.keyDic[STRAFE_LEFT]: self.camHandler.strafeLeft(dt) if self.keyDic[STRAFE_RIGHT]: self.camHandler.strafeRight(dt) if self.keyDic[TURN_LEFT]: self.camHandler.turnLeft(dt) if self.keyDic[TURN_RIGHT]: self.camHandler.turnRight(dt) if self.keyDic[UP]: self.camHandler.lookUp(dt) if self.keyDic[DOWN]: self.camHandler.lookDown(dt) #----------------------------- # editor update task def update(self, task): dt = globalClock.getDt() if base.mouseWatcherNode.hasMouse(): mpos = base.mouseWatcherNode.getMouse() pos = self.clicker.getMouseTilePos(mpos) else: mpos = None pos = None # collision editing if self.mode == "collision" and self.keyDic["mouse1"] and pos is not None: self.addCollision(pos[0], pos[1]) elif self.mode == "collision" and self.keyDic["mouse3"] and pos is not None: self.removeCollision(pos[0], pos[1]) # map objects control if self.mode == "object" and mpos is not None: if self.objectMode == "drag": objPos = self.clicker.getMousePos(mpos) objPos = Vec3(objPos[0], objPos[1], self.selectedObj.getZ()) self.selectedObj.setPos(objPos) self.checkObj(dt) #elif self.objectMode == "rotate": elif self.objectMode == "rotate" or self.objectMode == "moveZ": self.checkObj(dt) elif self.objectMode == "scale": self.scaleObj(dt) name = self.getHoverObjectName() if name is not None: #msg = "mapObject : " + name msg = "Map object : " + name + "\npos = " + str(self.map.mapObjects[name].getPos()) self.gui.setObjInfo(mpos, msg) else: self.gui.clearObjInfo() # camera control self.updateCam(dt) return task.cont