-- 显示鼠标当前位置(客户端窗口位置、坐标系的位置、GLView的位置) -- 处理按下鼠标中键移动场景的功能 require("CSharpConstants") local Camera2DControllerTool = class("Camera2DControllerTool") function Camera2DControllerTool:moveTo(endPos) local offset = cc.pSub(endPos , self.startPos); local scale = app.setting.ScreenScale; offset.x = -offset.x / scale; offset.y = -offset.y / scale; local newWorld = cc.pAdd(self.originPos , offset); app.mainCamera:getNode():setTranslation(newWorld); end function Camera2DControllerTool:MouseMove(button , x , y) local glViewPos = app.visibleRect:convertScreenToGLView(x, y); if self.startPos then -- 计算结束点 self:moveTo(glViewPos); end local point = app.visibleRect:convertScreenToCoordinate(x, y); local strMapCoord = ""; if app.editor and app.editor.Map then local tilePos = app.editor.Map:getTilePosition(point.x , point.y); strMapCoord = string.format(" TilePos:%d,%d" , tilePos.x , tilePos.y); end return string.format("Mouse:%d,%d Coordinate:%d,%d GLView:%d,%d" .. strMapCoord , x , y , point.x , point.y , glViewPos.x , glViewPos.y); end function Camera2DControllerTool:MoveDown(button, x, y) -- 鼠标中键按下可以移动场景 if button ~= MouseButtons.Middle then return end -- 鼠标的按下位置 self.startPos = app.visibleRect:convertScreenToGLView(x, y); -- 场景的位置 self.originPos = app.mainCamera:getNode():getTranslation(); end function Camera2DControllerTool:MoveUp(button, x, y) if not self.startPos then return end -- 计算结束点 self:moveTo(app.visibleRect:convertScreenToGLView(x, y)); self.startPos = nil; self.originPos = nil; end function Camera2DControllerTool:KeyDown(Control , Alt , Shift , KeyCode) end function Camera2DControllerTool:update() end -- 摆放物件时使用这个函数计算物件应该放在什么位置 function Camera2DControllerTool:convertScreenToWorldPos(mouseX , mouseY) return app.visibleRect:convertScreenToWorldPos2D(mouseX , mouseY); end -- 刷新设置时调用 function Camera2DControllerTool:refreshSetting() app.setting.ScreenScale = cc.clampf(app.setting.ScreenScale , app.setting.ScreenScaleMin , app.setting.ScreenScaleMax); -- 设置相机缩放 app.mainCamera:getNode():setScale(1 / app.setting.ScreenScale); app.background:removeAllChildren(); -- 设置OpenGL背景颜色 gl.clearColor(0,0,0,0); -- 画手机外壳 if app.setting.ShowIPhone5 then local bgIphone5 = cc.Sprite:create("Editor/iphone5.png"); app.background:addChild(bgIphone5) end -- 设置背景颜色 local bgColor = cc.LayerColor:create(app.setting.BackgroundColor); bgColor:setContentSize(cc.size(app.setting.ResourceSize.width,app.setting.ResourceSize.height)) bgColor:setAnchorPoint(app.setting.OriginAnchor) bgColor:ignoreAnchorPointForPosition(false); bgColor:setGlobalZOrder(-100000000); --app.background:addChild(bgColor) -- 创建背景图 if app.setting.BackgroundImage ~= "" then local bg = cc.Sprite:create(app.setting.BackgroundImage); local size = bg:getContentSize(); bg:setAnchorPoint(app.setting.OriginAnchor) app.background:addChild(bg) end if app.setting.ShowGrid then -- 创建网格 local vertices = {}; local viewSize = cc.size(app.setting.ResourceSize.width,app.setting.ResourceSize.height); local gridWidth = app.setting.GridSize.width local gridHeight = app.setting.GridSize.height local gridCountY = math.floor(viewSize.height / gridHeight); local vertexFormat = {}; table.insert(vertexFormat , {size = 3 , usage = cc.VertexFormatUsage.POSITION}); table.insert(vertexFormat , {size = 4 , usage = cc.VertexFormatUsage.COLOR}); local vertices = ManualVertices:new(vertexFormat); vertices:setColor(app.setting.GridColor); -- 画横向网格 for i = -gridCountY , gridCountY do vertices:drawLine(cc.vec3(-viewSize.width , i * gridHeight , 0) , cc.vec3(viewSize.width , i * gridHeight , 0)); end -- 画竖向网格 local gridCountX = math.floor(viewSize.width / gridWidth); for i = -gridCountX , gridCountX do vertices:drawLine(cc.vec3(i * gridWidth , -viewSize.height , 0) , cc.vec3(i * gridWidth , viewSize.height , 0)) end vertices:setColor(app.setting.AxisColor); -- 画轴 vertices:drawLine(cc.vec3(-viewSize.width , 0 , 0) , cc.vec3(viewSize.width , 0 , 0)) vertices:drawLine(cc.vec3(0 , -viewSize.height , 0) , cc.vec3(0 , viewSize.height , 0)) local mesh = cc.Mesh:createMesh(vertexFormat , vertices:getVertexCount()); mesh:setPrimitiveType(cc.PrimitiveType.LINES); mesh:setVertexData(vertices:getVertices() , 0 , vertices:getVertexCount()); mesh:setBoundingBox(vertices:getBoundingBox()); local model = cc.Model:create(mesh); local material = cc.Material:create("core/preload/shaders/ccDefault.material#ShaderPositionColor"); material:setParameterAutoBinding("CC_MVPMatrix" , "WORLD_VIEW_PROJECTION_MATRIX"); model:setMaterialPointer(material); local node = cc.Node3D:create(); node:addComponent(model); app.background:addChild(node); end end return Camera2DControllerTool;