BaseView.lua 4.12 KB
BaseView = {}

function BaseView:ctor()
    self.nodes = {};
    self.nodeIndex = 1;
    self.selectedIndex = 1;
end 

function BaseView:onButtonClick(ref, type,onComplete)
    if 0 == type then
--        MyAudio.playSound("BUTTON_CLICK")
    elseif 2 == type then
        onComplete(ref, type)
    end
end

--确认键处理函数
function BaseView:onKeypadOk()
    if not self.nodes[self.nodeIndex] then
        return;
    end

    local node = self.nodes[self.nodeIndex][self.selectedIndex];
    if node and node.fn then
        node:fn();
    end
end

--上键处理函数
function BaseView:onKeypadUp(isBounce)
    if isBounce == nil then
        isBounce = true;
    end
    if self.nodeIndex > 1 then
        local curNode = self.nodes[self.nodeIndex][self.selectedIndex];
        local curPos = curNode:convertToWorldSpace(cc.p(0,0));

        self.nodeIndex = self.nodeIndex - 1;

        local minDinstance = 9999;
        self.selectedIndex = 1;
        for i,v in ipairs(self.nodes[self.nodeIndex]) do
            local _p = v:convertToWorldSpace(cc.p(0,0));
            local _distance = math.abs(_p.x-curPos.x);
            if _distance < minDinstance then
                minDinstance = _distance;
                self.selectedIndex = i;
            end
        end
        self:updateSelectedState(isBounce);
    end
end

--下键处理函数
function BaseView:onKeypadDown(isBounce)
    if isBounce == nil then
        isBounce = true;
    end
    if self.nodes and self.nodeIndex < #self.nodes then
        local curNode = self.nodes[self.nodeIndex][self.selectedIndex];
        local curPos = curNode:convertToWorldSpace(cc.p(0,0));

        self.nodeIndex = self.nodeIndex + 1;

        local minDinstance = 9999;
        self.selectedIndex = 1;
        for i,v in ipairs(self.nodes[self.nodeIndex]) do
            local _p = v:convertToWorldSpace(cc.p(0,0));
            local _distance = math.abs(_p.x-curPos.x);
            if _distance < minDinstance then
                minDinstance = _distance;
                self.selectedIndex = i;
            end
        end
        self:updateSelectedState(isBounce);
    end
end

--左键处理函数
function BaseView:onKeypadLeft(isBounce)
    if isBounce == nil then
        isBounce = true;
    end
    if self.selectedIndex > 1 then
        self.selectedIndex = self.selectedIndex - 1;
        
        self:updateSelectedState(isBounce);
    end
end

--右键处理函数
function BaseView:onKeypadRight(isBounce)
    if isBounce == nil then
        isBounce = true;
    end
    if #self.nodes > 0 and self.selectedIndex < #self.nodes[self.nodeIndex] then
        self.selectedIndex = self.selectedIndex + 1;

        self:updateSelectedState(isBounce);
    end
end

--菜单键处理函数
function BaseView:onKeypadMenu()
    
end

function BaseView:updateSelectedState(isBounce)
    if IS_TV == 0 then
        return;
    end
    if isBounce == nil then
        isBounce = true;
    end
    if #self.nodes == 0 then
        logE("该界面没有设置按键节点");
        return ;
    end
    for i,v in ipairs(self.nodes) do
        for index,node in ipairs(v) do
            local selected = node:getChildByName("selected");
            if selected then
                selected:setVisible(false);
            end
            NodeManager.setHighlight(node,false);
        end
    end
    local node = self.nodes[self.nodeIndex][self.selectedIndex];
    if node then
        local selected = node:getChildByName("selected");
        if selected then
            selected:setVisible(true);
        end
        NodeManager.setHighlight(node,true,isBounce);
    end
end

function BaseView:close()
    local scene = cc.Director:getInstance():getRunningScene();
    scene.keypadManager:removeKeypadReceiver(self);

    self:setVisible(false)
    local delay = cc.DelayTime:create(0.1)
    local del = function()
        self:removeFromParent(true)
        logD("count1:", collectgarbage("count"))
        collectgarbage("collect")
        collectgarbage("collect")
        collectgarbage("collect")
        logD("count2:", collectgarbage("count"))
    end
    self:runAction(cc.Sequence:create(delay, cc.CallFunc:create(del)))
end

return BaseView