Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

145 рядки
5.0 KiB

  1. -- 显示鼠标当前位置(客户端窗口位置、坐标系的位置、GLView的位置)
  2. -- 处理按下鼠标中键移动场景的功能
  3. require("CSharpConstants")
  4. local Camera2DControllerTool = class("Camera2DControllerTool")
  5. function Camera2DControllerTool:moveTo(endPos)
  6. local offset = cc.pSub(endPos , self.startPos);
  7. local scale = app.setting.ScreenScale;
  8. offset.x = -offset.x / scale;
  9. offset.y = -offset.y / scale;
  10. local newWorld = cc.pAdd(self.originPos , offset);
  11. app.mainCamera:getNode():setTranslation(newWorld);
  12. end
  13. function Camera2DControllerTool:MouseMove(button , x , y)
  14. local glViewPos = app.visibleRect:convertScreenToGLView(x, y);
  15. if self.startPos then
  16. -- 计算结束点
  17. self:moveTo(glViewPos);
  18. end
  19. local point = app.visibleRect:convertScreenToCoordinate(x, y);
  20. local strMapCoord = "";
  21. if app.editor and app.editor.Map then
  22. local tilePos = app.editor.Map:getTilePosition(point.x , point.y);
  23. strMapCoord = string.format(" TilePos:%d,%d" , tilePos.x , tilePos.y);
  24. end
  25. return string.format("Mouse:%d,%d Coordinate:%d,%d GLView:%d,%d" .. strMapCoord , x , y , point.x , point.y , glViewPos.x , glViewPos.y);
  26. end
  27. function Camera2DControllerTool:MoveDown(button, x, y)
  28. -- 鼠标中键按下可以移动场景
  29. if button ~= MouseButtons.Middle then return end
  30. -- 鼠标的按下位置
  31. self.startPos = app.visibleRect:convertScreenToGLView(x, y);
  32. -- 场景的位置
  33. self.originPos = app.mainCamera:getNode():getTranslation();
  34. end
  35. function Camera2DControllerTool:MoveUp(button, x, y)
  36. if not self.startPos then return end
  37. -- 计算结束点
  38. self:moveTo(app.visibleRect:convertScreenToGLView(x, y));
  39. self.startPos = nil;
  40. self.originPos = nil;
  41. end
  42. function Camera2DControllerTool:KeyDown(Control , Alt , Shift , KeyCode)
  43. end
  44. function Camera2DControllerTool:update()
  45. end
  46. -- 摆放物件时使用这个函数计算物件应该放在什么位置
  47. function Camera2DControllerTool:convertScreenToWorldPos(mouseX , mouseY)
  48. return app.visibleRect:convertScreenToWorldPos2D(mouseX , mouseY);
  49. end
  50. -- 刷新设置时调用
  51. function Camera2DControllerTool:refreshSetting()
  52. app.setting.ScreenScale = cc.clampf(app.setting.ScreenScale , app.setting.ScreenScaleMin , app.setting.ScreenScaleMax);
  53. -- 设置相机缩放
  54. app.mainCamera:getNode():setScale(1 / app.setting.ScreenScale);
  55. app.background:removeAllChildren();
  56. -- 设置OpenGL背景颜色
  57. gl.clearColor(0,0,0,0);
  58. -- 画手机外壳
  59. if app.setting.ShowIPhone5 then
  60. local bgIphone5 = cc.Sprite:create("Editor/iphone5.png");
  61. app.background:addChild(bgIphone5)
  62. end
  63. -- 设置背景颜色
  64. local bgColor = cc.LayerColor:create(app.setting.BackgroundColor);
  65. bgColor:setContentSize(cc.size(app.setting.ResourceSize.width,app.setting.ResourceSize.height))
  66. bgColor:setAnchorPoint(app.setting.OriginAnchor)
  67. bgColor:ignoreAnchorPointForPosition(false);
  68. bgColor:setGlobalZOrder(-100000000);
  69. --app.background:addChild(bgColor)
  70. -- 创建背景图
  71. if app.setting.BackgroundImage ~= "" then
  72. local bg = cc.Sprite:create(app.setting.BackgroundImage);
  73. local size = bg:getContentSize();
  74. bg:setAnchorPoint(app.setting.OriginAnchor)
  75. app.background:addChild(bg)
  76. end
  77. if app.setting.ShowGrid then
  78. -- 创建网格
  79. local vertices = {};
  80. local viewSize = cc.size(app.setting.ResourceSize.width,app.setting.ResourceSize.height);
  81. local gridWidth = app.setting.GridSize.width
  82. local gridHeight = app.setting.GridSize.height
  83. local gridCountY = math.floor(viewSize.height / gridHeight);
  84. local vertexFormat = {};
  85. table.insert(vertexFormat , {size = 3 , usage = cc.VertexFormatUsage.POSITION});
  86. table.insert(vertexFormat , {size = 4 , usage = cc.VertexFormatUsage.COLOR});
  87. local vertices = ManualVertices:new(vertexFormat);
  88. vertices:setColor(app.setting.GridColor);
  89. -- 画横向网格
  90. for i = -gridCountY , gridCountY do
  91. vertices:drawLine(cc.vec3(-viewSize.width , i * gridHeight , 0) , cc.vec3(viewSize.width , i * gridHeight , 0));
  92. end
  93. -- 画竖向网格
  94. local gridCountX = math.floor(viewSize.width / gridWidth);
  95. for i = -gridCountX , gridCountX do
  96. vertices:drawLine(cc.vec3(i * gridWidth , -viewSize.height , 0) , cc.vec3(i * gridWidth , viewSize.height , 0))
  97. end
  98. vertices:setColor(app.setting.AxisColor);
  99. -- 画轴
  100. vertices:drawLine(cc.vec3(-viewSize.width , 0 , 0) , cc.vec3(viewSize.width , 0 , 0))
  101. vertices:drawLine(cc.vec3(0 , -viewSize.height , 0) , cc.vec3(0 , viewSize.height , 0))
  102. local mesh = cc.Mesh:createMesh(vertexFormat , vertices:getVertexCount());
  103. mesh:setPrimitiveType(cc.PrimitiveType.LINES);
  104. mesh:setVertexData(vertices:getVertices() , 0 , vertices:getVertexCount());
  105. mesh:setBoundingBox(vertices:getBoundingBox());
  106. local model = cc.Model:create(mesh);
  107. local material = cc.Material:create("core/preload/shaders/ccDefault.material#ShaderPositionColor");
  108. material:setParameterAutoBinding("CC_MVPMatrix" , "WORLD_VIEW_PROJECTION_MATRIX");
  109. model:setMaterialPointer(material);
  110. local node = cc.Node3D:create();
  111. node:addComponent(model);
  112. app.background:addChild(node);
  113. end
  114. end
  115. return Camera2DControllerTool;