-- package.path = ";preload/?.lua" -- package.path = package.path .. "?;?.lua;preload/?.lua" -- package.path = package.path .. ";preload/tools/?.lua" -- package.path = package.path .. ";Editor/?.lua" -- package.path = package.path .. ";luaScript/?.lua" -- package.path = package.path .. ";dataconfig/?.lua" -- package.path = package.path .. ";romFiles/?.lua" -- package.path = package.path .. ";luaScript/cc/?.lua" -- package.path = package.path .. ";luaScript/Tools/?.lua" -- package.path = package.path .. ";luaScript/Tools/Nodes/?.lua" -- package.path = package.path .. ";luaScript/Tools/Widgets/?.lua" -- package.path = package.path .. ";luaScript/Tools/Tracks/?.lua" -- package.path = package.path .. ";luaScript/Tools/Effect/?.lua" -- package.path = package.path .. ";luaScript/Tools/Other/?.lua" -- package.path = package.path .. ";luaScript/Tools/Effect/ParticleSystem/?.lua" -- package.path = package.path .. ";luaScript/Tools/Effect/ParticleSystem/Affector/?.lua" -- package.path = package.path .. ";luaScript/Tools/Effect/ParticleSystem/Emitter/?.lua" -- package.path = package.path .. ";luaScript/Protocol/?.lua" -- package.path = package.path .. ";luaScript/Config/?.lua" -- package.path = package.path .. ";luaScript/Extension/?.lua" -- package.path = package.path .. ";luaScript/Map/?.lua" -- package.path = package.path .. ";luaScript/Views/?.lua" -- package.path = package.path .. ";luaScript/Config/Languages/?.lua" -- package.path = package.path .. ";luaScript/Config/ConfigDesc/?.lua" -- package.path = package.path .. ";luaScript/User/?.lua" -- package.path = package.path .. ";luaScript/Learning/?.lua" -- package.path = package.path .. ";luaScript/Learning/Script/?.lua" -- cc.FileUtils:getInstance():addSearchPath("core") -- cc.FileUtils:getInstance():addSearchPath("core/preload") -- cc.FileUtils:getInstance():addSearchPath("core/preload/tools") -- cc.FileUtils:getInstance():addSearchPath("core/Editor") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript") -- cc.FileUtils:getInstance():addSearchPath("core/dataconfig") -- cc.FileUtils:getInstance():addSearchPath("core/romFiles") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/cc") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools/Other") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools/Nodes") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools/Widgets") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools/Tracks") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools/Effect") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools/Effect/ParticleSystem") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools/Effect/ParticleSystem/Affector") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Tools/Effect/ParticleSystem/Emitter") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Protocol") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Config") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Extension") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Map") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Views") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Config/Languages") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Config/ConfigDesc") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/User") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Plugins") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/PluginAdvertise") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Learning") -- cc.FileUtils:getInstance():addSearchPath("core/luaScript/Learning/Script") require("EditorGlobalFunction") require("CCBoxNode") require("NodeEditor") require("ObstacleTool") require("CombineEffectEditor") require("CCNode") require("CCEffectNode") require("CCEffectFileEffectNode") require("CCParticleEffectNode") require("CCCombineEffect") require("MapEditor") require("EditorNodeZorder") require("AnimEntityInstance") require("UndoRedoManager") require("MoveTool") require("Move2DTool") require("Move3DTool") require("preload") local EditorApp = class("EditorApp", cc.mvc.AppBase) function EditorApp:ctor() EditorApp.super.ctor(self); -- 用来保存C#提供的回调函数 -- NodeEditor提供setPropertyObject self.cs = {}; -- 当前的编辑器 self.editor = nil; -- 鼠标移动工具 self.cameraController = require("Camera2DControllerTool"):new(); -- 撤销管理器 self.undoRedoManager = require("UndoRedoManager"):new(); -- 主场景 self.mainScene = nil -- 主画布 self.mainLayer = nil -- 主相机 self.mainCamera = nil -- 移动工具 self.activeMoveTool = require("Move2DTool"):new(); -- 底图 self.background = nil -- 自定义侦听渲染的listener self.eventAfterDrawListener = nil -- 对齐工具 self.alignTool = require("AlignTool"):new(); self.setting = { GridColor = cc.c4f(1,1,0,1), AxisColor = cc.c4f(1,1,1,1), GridSize = cc.size(100,100), ResourceSize = cc.size(1280,720), BackgroundColor = cc.c4b(0,0,0 , 255), BackgroundImage = "", ShowFPS = false, ShowGrid = true, ShowIPhone5 = false, ScreenScale = 1, ScreenScaleMax = 16, ScreenScaleMin = 0.1, -- 技能源模型文件 SourceModelFile = "res/default/DefaultModel/DefaultModel.gpb"; -- 技能目标模型文件 TargetModelFile = "res/default/DefaultModel/DefaultModel.gpb"; -- 显示碰撞体的调试信息 ShowCollision = false; OriginAnchor = cc.p(0.5,0.5), DisableAudio = false; -- 默认是否播放UI动画 PlayUIAnimation = false; } self.config = {Setting = self.setting}; -- 可见区域 self.visibleRect = require("VisibleRect"):new(); self.cameraNode3D = nil; end function EditorApp:createObject(name) return require(name):new(); end -- 获取操作的相机 function EditorApp:getMainCameraShakeNode() return self.mainCamera:getNode(); end -- 激活一个编辑器 function EditorApp:activateEditor(editorName) if self.editor then self.editor:unactivate(); end self.editor = require(editorName):new(); self.editor:activate(); -- 清空undo redo列表 self.undoRedoManager:clear(); end function EditorApp:activate2DCamera() self.mainCamera = cc.Director:getInstance():getActiveCamera(); self.cameraController = require("Camera2DControllerTool"):new(); self.mainLayer:setActiveCamera(nil); self.cameraController:update(); self.cameraController:refreshSetting(); -- 切换移动工具 self.activeMoveTool:clear(); self.activeMoveTool = require("Move2DTool"):new(); cc.AudioListener:getInstance():setCamera(self.mainCamera); end function EditorApp:activate3DCamera() if not self.cameraNode3D then -- 创建透视投影相机 local camera = cc.Camera:createPerspective(45 , 1 , 1 , 1000); cc.Director:getInstance():getActiveCamera():getNode():setScale(1.0); local cameraNode = cc.Node3D:create("CameraNode3D"); cameraNode:addComponent(camera); local cameraParentNode = cc.Node3D:create(); cameraParentNode:addChild(cameraNode) self.mainLayer:addChild(cameraParentNode); self.cameraNode3D = cameraNode; end self.mainLayer:setActiveCamera(self.cameraNode3D:getCamera()); self.mainCamera = self.cameraNode3D:getCamera(); self.mainCamera:setAspectRatio(cc.Director:getInstance():getWinSize().width / cc.Director:getInstance():getWinSize().height); self.cameraController = require("Camera3DControllerTool"):new(); self.cameraController:update(); self.cameraController:refreshSetting(); -- 切换移动工具 self.activeMoveTool:clear(); self.activeMoveTool = require("Move3DTool"):new(); cc.AudioListener:getInstance():setCamera(self.mainCamera); end -- 获取相机 function EditorApp:getActivateCamera() return self.mainCamera end -- 激活一个自定义相机 function EditorApp:activateCustomCamera(camera) self.mainLayer:setActiveCamera(camera); self.mainCamera = camera; self.cameraController = require("Camera3DControllerTool"):new(); self.cameraController:update(); self.cameraController:refreshSetting(); -- 切换移动工具 self.activeMoveTool:clear(); self.activeMoveTool = require("Move3DTool"):new(); cc.AudioListener:getInstance():setCamera(self.mainCamera); end function EditorApp:isActiveCamera(camera) return self.mainCamera == camera; end function EditorApp:is3DCamera() if self.mainLayer:getActiveCamera() == nil or self.mainLayer:getActiveCamera() then return false; end return true; end -- 反激活一个编辑器 function EditorApp:unactivateEditor(editorName) if self.editor and iskindof(self.editor , editorName) then self.editor:unactivate(); self.editor = nil; end -- 清空undo redo列表 self.undoRedoManager:clear(); end function EditorApp:run() -- 创建默认场景 self.mainScene = cc.Scene:create(); local layer = cc.Scene:create(); layer:setContentSize(self.setting.ResourceSize); layer:setAnchorPoint(cc.p(0,0)); self.mainLayer = layer; self.mainScene:addChild(layer); -- 默认不使用这个2D相机 self.mainLayer:setActiveCamera(nil); local background = cc.Layer:create(); self.background = background; self.mainLayer:addChild(background, EditorNodeZorder.CoordinateNodeZorder); -- 默认加载通用图 cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/default/default-textures.plist"); cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/ui/zy_tongyong/t_tongyong_1.plist"); cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("res/ui/zy_tongyong/t_tongyong_2.plist"); -- 默认使用2D相机 self:activate2DCamera(); cc.StreamObject:addIgnoreDir("core"); cc.StreamObject:addIgnoreDir("huanle"); cc.StreamObject:addIgnoreDir("chuannan"); cc.StreamObject:addIgnoreDir("youxianmj"); cc.StreamObject:addIgnoreDir("chongqing"); cc.StreamObject:addIgnoreDir("shuzhou"); cc.Director:getInstance():runWithScene(self.mainScene) self:refreshSetting(); end function EditorApp:getOriginOffset() return cc.p(cc.Director:getInstance():getWinSize().width * self.setting.OriginAnchor.x , cc.Director:getInstance():getWinSize().height * self.setting.OriginAnchor.y); end -- 改变编辑器大小 function EditorApp:resize(w , h) self.mainCamera:setAspectRatio(w/h); cc.Application:getInstance():applicationScreenSizeChanged(w, h); cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(w , h , cc.ResolutionPolicy.EXACT_FIT); self.mainScene:setScale(self.setting.ScreenScale); -- 通知可见区域更新 self.visibleRect:updateVisibleRect(); --self.mainScene:setContentSize(cc.Director:getInstance():getWinSize()); self.background:setContentSize(cc.Director:getInstance():getWinSize()); --self.mainLayer:setContentSize(cc.Director:getInstance():getWinSize()); self:refreshSetting(); end -- 创建网格 function EditorApp:refreshSetting() -- 设置全局缩放 local pDirector = cc.Director:getInstance(); local pEGLView = cc.Director:getInstance():getOpenGLView(); cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(pEGLView:getFrameSize().width , pEGLView:getFrameSize().height , cc.ResolutionPolicy.EXACT_FIT); --self.mainScene:setScale(self.setting.ScreenScale); -- 居中 --self.mainLayer:setPosition(self:getOriginOffset()); -- 更新相机控制器 self.cameraController:refreshSetting(); local pDirector = cc.Director:getInstance(); pDirector:setDisplayStats(self.setting.ShowFPS); -- 是否需要显示碰撞体 if self.setting.ShowCollision == true and self.eventAfterDrawListener == nil then self.eventAfterDrawListener = cc.EventListenerCustom:create("director_after_draw" , handler(self , self.onUpdate)); cc.Director:getInstance():getEventDispatcher():addEventListenerWithFixedPriority(self.eventAfterDrawListener, 1); elseif self.setting.ShowCollision == false and self.eventAfterDrawListener ~= nil then cc.Director:getInstance():getEventDispatcher():removeEventListener(self.eventAfterDrawListener) self.eventAfterDrawListener = nil end local winSize = self.setting.ResourceSize; local resourceSize = {width = 1280 ; height = 720}; -- 实际比率 local radio = winSize.width / winSize.height; -- 标准比率 local resourceRadio = resourceSize.width / resourceSize.height; print("实际分辨率" , winSize.width , winSize.height); local finalResourceSize; -- 比16:9还扁,就用标准高度,让宽度变宽 if radio > resourceRadio then finalResourceSize = {width = resourceSize.height * radio ; height = resourceSize.height}; -- 比16:9高,就固定宽度,让高度变高 else finalResourceSize = {width = resourceSize.width ; height = resourceSize.width / radio}; end print("资源分辨率" , finalResourceSize.width , finalResourceSize.height); -- 改变屏幕大小 self.mainLayer:setContentSize(finalResourceSize); -- 更新UI尺寸 if self.editor and self.editor.node and self.editor.node.updateSizeAndPosition then self.editor.node:updateSizeAndPosition(); end end function EditorApp:onUpdate() cc.PhysicsController:getInstance():drawDebug(self.mainCamera:getViewProjectionMatrix()) end function EditorApp:reload() if self.effect then local xmlNode = self.effect:createXmlNode(); self.effect:removeFromParent(); cc.TextureCache:sharedTextureCache():removeUnusedTextures(); self.effect = createEffectFromXmlNode(xmlNode); self:initEffect(self.effect) return self.effect; elseif self.combineEffect then cc.TextureCache:sharedTextureCache():removeUnusedTextures(); return self.combineEffect; end end -- 生成一个目录 local function visitPath(sourcePath , func , ...) print("开始生成目录" .. sourcePath) -- 递归遍历sourcePath的所有目录 for file in lfs.dir(sourcePath) do if file ~= "." and file ~= ".." then local f = sourcePath..'/'..file local attr = lfs.attributes (f , "mode") if attr == "directory" then if visitPath(f , func , ...) then return true; end else if func(f , ...) then return true; end end end end print("生成目录完毕" .. sourcePath) end -- 重新保存所有UI function EditorApp:resaveAllUI() local function saveui(fileName) -- 返回pathName , baseName , ext local pathName , baseName , ext = string.splitFilename(fileName); print(pathName , baseName , ext); if ext ~= "ui" then return false; end local function resaveUI() print("\n\n"); print("正在加载UI:" , fileName); local node = createNodeFromFile(fileName); print("正在重新计算PList"); node:recalcAllPList(); print("正在保存UI:" , fileName); node:saveToFile(fileName); end xpcall(resaveUI , function(msg)print("处理界面“" .. fileName .. "”时出错了:" , msg , debug.traceback());alert("处理界面“" .. fileName .. "”时出错了");end); end cc.BaseObject:setUpgradingMode(true); visitPath("res" , saveui); cc.BaseObject:setUpgradingMode(false); end -- 重新保存所有Model function EditorApp:resaveAllModel() local function saveui(fileName) -- 返回pathName , baseName , ext local pathName , baseName , ext = string.splitFilename(fileName); print(pathName , baseName , ext); if ext ~= "gpb" then return false; end local function resaveUI() print("\n\n"); print("正在加载Model:" , fileName); local node = cc.ModelNode:create(fileName); local function onLoad(index) local skinMesh = node:_getFirstMeshSkin(); if skinMesh then print("蒙皮骨头数量:" , skinMesh:getJointCount()); if skinMesh:getJointCount() >= 39 then print("蒙皮骨头数量超过了39个:" , skinMesh:getJointCount()); end end node:autorelease(); end node:runOnLoad(onLoad); node:retain(); print("正在保存Model:" , fileName); --node:saveToFile(fileName); end xpcall(resaveUI , function()print("处理Effect“" .. fileName .. "”时出错了");alert("处理Effect“" .. fileName .. "”时出错了");end); end cc.BaseObject:setUpgradingMode(true); visitPath("res" , saveui); cc.BaseObject:setUpgradingMode(false); end -- 重新保存所有Effect function EditorApp:resaveAllEffect() local function saveui(fileName) -- 返回pathName , baseName , ext local pathName , baseName , ext = string.splitFilename(fileName); print(pathName , baseName , ext); if ext ~= "effect" then return false; end local function resaveUI() print("\n\n"); print("正在加载Effect:" , fileName); local node = createNodeFromFile(fileName); print("正在保存Effect:" , fileName); node:saveToFile(fileName); end xpcall(resaveUI , function()print("处理Effect“" .. fileName .. "”时出错了");alert("处理Effect“" .. fileName .. "”时出错了");end); end cc.BaseObject:setUpgradingMode(true); visitPath("res" , saveui); cc.BaseObject:setUpgradingMode(false); end -- 重新保存所有CE function EditorApp:resaveAllCE() local function saveCE(fileName) -- 返回pathName , baseName , ext local pathName , baseName , ext = string.splitFilename(fileName); print(pathName , baseName , ext); if ext ~= "ce" then return false; end local function resaveCE() print("\n\n"); print("正在加载CE:" , fileName); local node = createCombineEffectFromXmlFile(fileName); print("正在保存CE:" , fileName); node:saveToFile(fileName); end xpcall(resaveCE , function()print("处理CE“" .. fileName .. "”时出错了");alert("处理CE“" .. fileName .. "”时出错了");end); end cc.BaseObject:setUpgradingMode(true); visitPath("res" , saveCE); cc.BaseObject:setUpgradingMode(false); end -- 重新保存所有Scene function EditorApp:resaveAllScene() -- 载入场景文件 local function loadScene(nodeFile) print("加载场景" , nodeFile); TimeSpan:enterSpan("loadScene" .. nodeFile); TimeSpan:enterSpan("createNodeFromFile"); print("加载模型"); LoadingScene = true; local node = createNodeFromFile(nodeFile); LoadingScene = false; node:setCollectAnimationClip(false); TimeSpan:leaveSpan(); if not EditorMode then -- 普通模式,屏蔽掉HD内容 local hd = node:findNode("HD"); if hd then local function onQualityChanged(key , value) -- 高质量 hd:setVisible(value == 1); end hd:bind(app.systemSetting.info , "graphicQuality" , onQualityChanged); end end -- 创建一个题图载入资源 TimeSpan:enterSpan("requireMap"); print("加载障碍块"); local map = require("Map"):new(); local arr = string.split(nodeFile, "."); map:loadLua(arr[1] .. ".lmap"); TimeSpan:leaveSpan(); TimeSpan:leaveSpan(); return node , map; end local function saveui(fileName) -- 返回pathName , baseName , ext local pathName , baseName , ext = string.splitFilename(fileName); print(pathName , baseName , ext); if ext ~= "scene" then return false; end local function resaveUI() print("\n\n"); print("正在加载Scene:" , fileName); local scene , map = loadScene(fileName); print("正在保存Scene:" , fileName); scene:saveToFile(fileName); local arr = string.split(fileName, "."); map:saveLua(arr[1] .. ".lmap"); map:saveXml(arr[1] .. ".map"); end xpcall(resaveUI , function(msg)print("处理Scene“" .. fileName .. "”时出错了:" , msg);alert("处理Scene“" .. fileName .. "”时出错了:" .. tostring(msg));end); end cc.BaseObject:setUpgradingMode(true); visitPath("res" , saveui); cc.BaseObject:setUpgradingMode(false); end -- 解析所有的配置,导出所有使用过的资源 function EditorApp:exportAllRes(destPath) local fileVisitor = cc.ReferanceResourceVisitor:new() local function exportRes(resFile) print("资源名字", resFile) if string.endsWith(resFile, ".gpb") then local model = cc.ModelNode:create(resFile); model:visitReferanceResource(fileVisitor) elseif string.endsWith(resFile, ".scene") then local object = cc.StreamObject:loadFromFile(resFile) if object then object:visitReferanceResource(fileVisitor) -- 保存地图相关的资源 fileVisitor:visitFile(resFile) local stringArr = string.split(resFile, "."); fileVisitor:visitFile(stringArr[1] .. ".lmap") fileVisitor:visitFile(stringArr[1] .. ".lmap.blocks") fileVisitor:visitFile(stringArr[1] .. ".map") end elseif string.endsWith(resFile, ".ui") or string.endsWith(resFile, ".ce") or string.endsWith(resFile, ".effect") then -- 导出资源本身 local object = cc.StreamObject:loadFromFile(resFile) if object then fileVisitor:visitFile(resFile) object:visitReferanceResource(fileVisitor) end elseif string.endsWith(resFile, ".ogg") or string.endsWith(resFile, ".png") or string.endsWith(resFile, ".jpg") then fileVisitor:visitFile(resFile) elseif string.endsWith(resFile, ".plist") then local stringArr = string.split(resFile, "."); fileVisitor:visitFile(resFile) fileVisitor:visitFile(stringArr[1] .. ".png") end end local function visitOneData(cf) if table.nums(cf) <= 0 then return end for i, v in pairs(cf) do if type(v) == "string" and v ~= "" then local strs = string.split(v, ";") for ii, vv in pairs(strs) do exportRes(vv) end end end end local function parseXmlFile(fileName) print("解析xml文件", fileName) -- 读取整个文件数据 local file = io.open(fileName , "rb"); local fileData = file:read("*a"); file:close(); -- 解析数据 local function onElementStart(name, atts) visitOneData(atts) end -- 这里不能使用xml.eval来解析,android下面,如果下载的文件不是xml格式的,则会宕机 local result , err = tiny.eval(fileData, onElementStart) if not result then print("xml文件解析失败", fileName) return; end end local function parseLuaFile(fileName) print("处理lua文件", fileName) local file = io.open(fileName , "rb"); local fileData = file:read("*a"); file:close(); local uiFile = nil -- 导出UI for w in string.gmatch(fileData, '"([%w_/]+)%.ui"') do uiFile = w .. ".ui" exportRes(uiFile) end -- 导出技能 for w in string.gmatch(fileData, '"([%w_/]+)%.ce"') do uiFile = w .. ".ce" exportRes(uiFile) end -- 导出光效 for w in string.gmatch(fileData, '"([%w_/]+)%.effect"') do uiFile = w .. ".effect" exportRes(uiFile) end -- 导出模型 for w in string.gmatch(fileData, '"([%w_/]+)%.gpb"') do uiFile = w .. ".gpb" exportRes(uiFile) end -- 导出plist for w in string.gmatch(fileData, '"([%w_/]+)%.plist"') do uiFile = w .. ".plist" exportRes(uiFile) end -- 导出png for w in string.gmatch(fileData, '"([%w_/]+)%.png"') do uiFile = w .. ".png" exportRes(uiFile) end -- 导出jpg for w in string.gmatch(fileData, '"([%w_/]+)%.jpg"') do uiFile = w .. ".jpg" exportRes(uiFile) end -- 导出声音 for w in string.gmatch(fileData, '"([%w_/]+)%.ogg"') do uiFile = w .. ".ogg" exportRes(uiFile) end -- 字体 for w in string.gmatch(fileData, '"([%w_/]+)%.fnt"') do uiFile = w .. ".fnt" exportRes(uiFile) end end -- 遍历dataconfig目录的所有文件 visitPath("dataconfig", parseXmlFile); visitPath("luaScript", parseLuaFile); visitPath("preload", parseLuaFile); --exportRes("res/animation/XJL_1/XJL_1.effect") fileVisitor:saveAllRes(destPath) print("导出资源路径", destPath) alert("资源导出完毕") end return EditorApp;