BaseViewNew.lua 9.39 KB
BaseViewNew = {}

function BaseViewNew:ctor()
    self.nodes = {};
    self.selected_node = nil;
    self.tolerate_value =  10;
end 

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

--确认键处理函数
function BaseViewNew:onKeypadOk()
    if self.selected_node and self.selected_node.fn then
        self.selected_node:fn();
    end
end

--上键处理函数
function BaseViewNew:onKeypadUp(isBounce,is_show_highlight,isLoop)
    if self.selected_node == nil or IS_TV == 0 then
        return;
    end

    if isBounce == nil then
        isBounce = true;
    end

    local curPos = self.selected_node:convertToWorldSpace(cc.p(0,0));
    local minDinstance = 9999;
    local near_node = nil;
    for i,v in ipairs(self.nodes) do
        for index,node in ipairs(v) do
            local _p = node:convertToWorldSpace(cc.p(0,0));
            if _p.y > curPos.y + self.tolerate_value then
                _distance = Point.distance(curPos,_p);
                if _distance < minDinstance then
                    minDinstance = _distance;
                    near_node = node;
                end
            end
        end
    end

    isLoop = isLoop or false;
    if isLoop and not near_node then
        for i,v in ipairs(self.nodes) do
            if TableUtil.IsInTable(v,self.selected_node) and #v > 1 then
                local one_line_list = {};
                for ii,vv in ipairs(v) do
                    local _p = vv:convertToWorldSpace(cc.p(0,0));
                    local x_distance = math.abs(curPos.x - _p.x);
                    print(x_distance)
                    if x_distance <= self.range_value then
                        one_line_list[#one_line_list+1] = vv;
                    end
                end
                local max_y = 9999;
                for ii,vv in ipairs(one_line_list) do
                    local _p = vv:convertToWorldSpace(cc.p(0,0));
                    if _p.y < max_y then
                        max_y = _p.y;
                        near_node = vv;
                    end
                end
                break;
            end
        end
    end

    if near_node then
        self:updateSelectedState(near_node,isBounce,is_show_highlight);
    end
end

--下键处理函数
function BaseViewNew:onKeypadDown(isBounce,is_show_highlight,isLoop)
    if self.selected_node == nil or IS_TV == 0 then
        return;
    end

    if isBounce == nil then
        isBounce = true;
    end

    local curPos = self.selected_node:convertToWorldSpace(cc.p(0,0));
    local minDinstance = 9999;
    local near_node = nil;
    for i,v in ipairs(self.nodes) do
        for index,node in ipairs(v) do
            local _p = node:convertToWorldSpace(cc.p(0,0));
            if _p.y < curPos.y - self.tolerate_value then
                _distance = Point.distance(curPos,_p);
                if _distance < minDinstance then
                    minDinstance = _distance;
                    near_node = node;
                end
            end
        end
    end

    isLoop = isLoop or false;
    if isLoop and not near_node then
        for i,v in ipairs(self.nodes) do
            if TableUtil.IsInTable(v,self.selected_node) and #v > 1 then
                local one_line_list = {};
                for ii,vv in ipairs(v) do
                    local _p = vv:convertToWorldSpace(cc.p(0,0));
                    local x_distance = math.abs(curPos.x - _p.x);
                    print(x_distance)
                    if x_distance <= self.range_value then
                        one_line_list[#one_line_list+1] = vv;
                    end
                end
                local max_y = -9999;
                for ii,vv in ipairs(one_line_list) do
                    local _p = vv:convertToWorldSpace(cc.p(0,0));
                    if _p.y > max_y then
                        max_y = _p.y;
                        near_node = vv;
                    end
                end
                break;
            end
        end
    end

    if near_node then
        self:updateSelectedState(near_node,isBounce,is_show_highlight);
    end
end

--左键处理函数
function BaseViewNew:onKeypadLeft(isBounce,is_show_highlight,isLoop)
    if self.selected_node == nil or IS_TV == 0 then
        return;
    end

    if isBounce == nil then
        isBounce = true;
    end
    
    local curPos = self.selected_node:convertToWorldSpace(cc.p(0,0));
    local minDinstance = 9999;
    local near_node = nil;
    for i,v in ipairs(self.nodes) do
        for index,node in ipairs(v) do
            local _p = node:convertToWorldSpace(cc.p(0,0));
            if _p.x < curPos.x - self.tolerate_value then
                _distance = Point.distance(curPos,_p);
                if _distance < minDinstance then
                    minDinstance = _distance;
                    near_node = node;
                end
            end
        end
    end

    isLoop = isLoop or false;
    if isLoop and not near_node then
        for i,v in ipairs(self.nodes) do
            if TableUtil.IsInTable(v,self.selected_node) and #v > 1 then
                local max_x = -9999;
                for ii,vv in ipairs(v) do
                    local _p = vv:convertToWorldSpace(cc.p(0,0));
                    print(_p.x)
                    if _p.x > max_x then
                        max_x = _p.x;
                        near_node = vv;
                    end
                end
                break;
            end
        end
    end

    if near_node then
        self:updateSelectedState(near_node,isBounce,is_show_highlight);
    end
end

--右键处理函数
function BaseViewNew:onKeypadRight(isBounce,is_show_highlight,isLoop)
    if self.selected_node == nil or IS_TV == 0 then
        return;
    end

    if isBounce == nil then
        isBounce = true;
    end

    local curPos = self.selected_node:convertToWorldSpace(cc.p(0,0));
    local minDinstance = 9999;
    local near_node = nil;
    for i,v in ipairs(self.nodes) do
        for index,node in ipairs(v) do
            local _p = node:convertToWorldSpace(cc.p(0,0));
            if _p.x > curPos.x + self.tolerate_value then
                _distance = Point.distance(curPos,_p);
                if _distance < minDinstance then
                    minDinstance = _distance;
                    near_node = node;
                end
            end
        end
    end

    isLoop = isLoop or false;
    if isLoop and not near_node then
        for i,v in ipairs(self.nodes) do
            if TableUtil.IsInTable(v,self.selected_node) and #v > 1 then
                local min_x = 9999;
                for ii,vv in ipairs(v) do
                    local _p = vv:convertToWorldSpace(cc.p(0,0));
                    print(_p.x)
                    if _p.x < min_x then
                        min_x = _p.x;
                        near_node = vv;
                    end
                end
                break;
            end
        end
    end

    if near_node then
        self:updateSelectedState(near_node,isBounce,is_show_highlight);
    end
end

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

function BaseViewNew:updateSelectedState(near_node,isBounce,is_show_highlight)
    if IS_TV == 0 then
        return;
    end
    if isBounce == nil then
        isBounce = true;
    end
    if is_show_highlight == nil then
        is_show_highlight = 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

    if near_node then
        if near_node.is_tab then
            for i,v in ipairs(self.nodes) do
                if TableUtil.IsInTable(v,near_node) then
                    if self.selected_node and TableUtil.IsInTable(v,self.selected_node) then
                        break;
                    else
                        for ii,vv in ipairs(v) do
                            if vv.is_selected then
                                near_node = vv;
                                break;
                            end
                        end
                        break;
                    end
                end
            end
        end

        self.selected_node = near_node;
        local selected = near_node:getChildByName("selected");
        if selected then
            selected:setVisible(true);
        end
        if is_show_highlight then
            NodeManager.setHighlight(near_node,true,isBounce);
        end

        if self.selected_node.is_tab and self.selected_node.fn then
            self.selected_node:fn();
        end
    end
end

function BaseViewNew: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 BaseViewNew