quick 中 "我的项目" 中的列表从那里来的?

quick 中 "我的项目" 中的列表从那里来的?

1. WelcomeScene.lua 场景

self:createOpenRecents(cc.player.settings.PLAYER_OPEN_RECENTS, node)

-- 我的项目recents = cc.player.settings.PLAYER_OPEN_RECENTS
function WelcomeScene:createOpenRecents(recents, node)
		-- 创建列表
    local localProjectListView = require("app.scenes.ListViewEx").new {}
     -- add items 添加列表项
    for i,v in ipairs(recents) do
        local container = self:createListItem("#Logo.png", v.title, v.title)
        local item = localProjectListView:newItem()
    end
    localProjectListView:reload()
end

2. cc.player.settings.PLAYER_OPEN_RECENTS

上面这个东西在 player.lua 文件中, 每次打开quick模拟器时,都会执行这个文件

C++ 代码
- (void) loadLuaConfig
{    
    //
    // set user home dir
    //
    lua_pushstring(pEngine->getLuaStack()->getLuaState(), path.UTF8String);
    lua_setglobal(pEngine->getLuaStack()->getLuaState(), "__USER_HOME__");

    //
    // load player.lua file
    //
    string playerCoreFilePath = _project.getQuickCocos2dxRootPath() + "quick/welcome/src/player.lua";
    pEngine->executeScriptFile(playerCoreFilePath.c_str());
}
player.lua 文件
function player:init()
    self.defaultSettings = [[
        PLAYER_WINDOW_WIDTH = 960,
        PLAYER_WINDOW_HEIGHT = 640,
        PLAYER_OPEN_LAST_PROJECT = true,
        PLAYER_OPEN_RECENTS ={ //这里是上面的默认值,为空table
        },

    ]]

   self:readSettings()
end

//重点在这里,self.configFilePath = player.userHomeDir .. ".quick_player.lua"
//这个就是配置文件,文件名是~/.quick_player.lua,如果没有这个文件,就使用默认配置,
//如果有这个文件就读取文件的保存的配置
function player:readSettings()
    self.userHomeDir = __USER_HOME__
    self.configFilePath = player.userHomeDir .. ".quick_player.lua"
	-- do not use cc.FileUtils:getInstance():isFileExist
	-- "__USER_HOME__" is ANSI encoding, while cocos engine use UTF8.
    if not self:isFileExist(player.configFilePath) then
        self:restorDefaultSettings()
    end
    self:loadSetting(player.configFilePath)
end
function player:loadSetting(fileName)
    local file, err = io.open(fileName, "rb")
    if err then return err end

    local data = file:read("*all")
    local func = loadstring("local settings = {" .. data .. "} return settings")
    self.settings = func()
   //这里就是我们最上面所使用的table
    self.settings.PLAYER_OPEN_RECENTS = self.settings.PLAYER_OPEN_RECENTS or {}
    file:close()
end

3. cc.player.settings.PLAYER_OPEN_RECENTS 使用

//打开按钮的事件处理, WelcomScene.lua
cc.ui.UIPushButton.new(images, {scale9 = true})
    :setButtonSize(buttonWidth, buttonHeight)
    :setButtonLabel("normal", cc.ui.UILabel.new({
		       text = "打开",
            size = 18,
        }))
    :onButtonClicked(function()
        local projectConfig = ProjectConfig:new()
        local argumentVector = vector_string_:new_local()
        local index = self.localProjectListView_:getCurrentIndex()
        if index > 0 then
        //这里使用了
            local arguments = cc.player.settings.PLAYER_OPEN_RECENTS[index].args
            for _,v in ipairs(arguments) do
                argumentVector:push_back(v)
            end
            projectConfig:parseCommandLine(argumentVector)
          //根据工程配置打开工程  PlayerProtocol:getInstance():openNewPlayerWithProjectConfig(projectConfig)
        end
    end)

4. cc.player.settings.PLAYER_OPEN_RECENTS 保存

我们打开一个示例,或者从我的项目中打开一个功能,那怎么加入到cc.player.settings.PLAYER_OPEN_RECENTS 里面的呢?上面我们分析了是这个列表是通过文件获取的,那相应的保存只要保存到那个文件中就行了

--
-- save player setting to ~/.quick_player.lua
--
local player = {}

function player:saveSetting(fileName)
    fileName = fileName or player.configFilePath
    local file, err = io.open(fileName, "wb")
    if err then return err end

    local ret =  table.serializeToString(self.settings)
    file:write(ret)
    file:close()
end     

但是保存在那里调用的呢?其实是在每次打开工程的时候调用的,也就是我们每次打开一个工程的时候
,就会保存到打开过的工程列表中

function player:init()
      -- record project 记录打开的工程,也就是保存到~/.quick_player.lua文件中
      // 这里就是把打开的工程保存到文件中, 那__PLAYER_OPEN_TITLE__和__PLAYER_OPEN_COMMAND__从那里来的呢?这个是从C++ 传过来的
    if __PLAYER_OPEN_TITLE__ and __PLAYER_OPEN_COMMAND__ then
        local title = string.gsub(__PLAYER_OPEN_TITLE__, '\', '/')
        local args = string.gsub(__PLAYER_OPEN_COMMAND__, '\', '/'):splitBySep(' ')

        self.projectConfig_ = ProjectConfig:new()
        local argumentVector = vector_string_:new_local()
        local arguments = args
        for _,v in ipairs(arguments) do
            argumentVector:push_back(v)
        end
        self.projectConfig_:parseCommandLine(argumentVector)
        //self:openProject里面调用self:saveSetting()保存新打开的工程到
        //工程列表中
        self:openProject(title, args)
    end

    self:buildUI()
end

上面的self:openProject方法

--
-- title: string
-- args : table
--
function player:openProject( title, args )
    local welcomeTitle = self.quickRootPath .. "quick/welcome/"
    if title == welcomeTitle then return end

    local recents = self.settings.PLAYER_OPEN_RECENTS
    if recents then
    		//去掉重复的,通过title相同判断,并把新打开的工程放到第一个位置
        local index = #recents
        while index > 0 do
            local v = recents[index]
            if v.title == title then table.remove(recents, index) end
            index = index - 1
        end
        table.insert(recents, 1, {title=title, args=args})
        self:saveSetting()
    end
end
__PLAYER_OPEN_TITLE__和__PLAYER_OPEN_COMMAND__从那里来的呢?这个是从C++ 传过来的,下面就是C++部分的代码
AppController.mm文件
- (void) loadLuaConfig 函数中:
    //
    // ugly: Add the opening project to the "Open Recents" list
    //
    lua_pushstring(pEngine->getLuaStack()->getLuaState(), _project.getProjectDir().c_str());
    lua_setglobal(pEngine->getLuaStack()->getLuaState(), "__PLAYER_OPEN_TITLE__");
    
    lua_pushstring(pEngine->getLuaStack()->getLuaState(), _project.makeCommandLine().c_str());
    lua_setglobal(pEngine->getLuaStack()->getLuaState(), "__PLAYER_OPEN_COMMAND__");
原文地址:https://www.cnblogs.com/ZhYQ-Note/p/6186096.html