加入房间的输入层

来源:互联网 发布:传感器网络的安全问题 编辑:程序博客网 时间:2024/04/30 06:00
--创建输入层
local InputLayer = class("InputLayer", function()
    return cc.Layer:create();
end)


InputLayer.__index = InputLayer;


INPUT_LAYER_JOIN_ROOM = 1;
INPUT_LAYER_SEE_REPLAY = 2;


InputLayer._layerType = 0;
InputLayer._currInputNumIndex = 1; --当前输入的数字索引号
InputLayer._inputNumTable = {}; --当前输入的数字
InputLayer._isEnter = false; 
InputLayer._inputLayer = nil;
InputLayer._rootNode = nil;


--创建场景
function InputLayer:createInputLayer(inputLayerId)
    local layer = InputLayer.new();  
    layer:setName("SCMJ_InputLayer");
    --限定类型 
    if inputLayerId<1 or inputLayerId>2 then
        return nil;
    end
    --添加场景
    layer._layerType = inputLayerId;


    layer._inputLayer = layer:InitLayer();


    --场景节点事件处理  
    local function onNodeEvent(event)  
        if event == "enter" then  
            layer:onEnter()  
--        elseif event == "enterTransitionFinish" then  
--            scene:onEnterTransitionFinish()  
--        elseif event == "exit" then  
--            scene:onExit()  
--        elseif event == "exitTransitionStart" then  
--            scene:onExitTransitionStart()  
--        elseif event == "cleanup" then  
--            scene:cleanup()  
       end  
    end  
    --注册
    layer:registerScriptHandler(onNodeEvent);


    layer:addChild(layer._inputLayer);


    return layer;
end


--进入场景
function InputLayer:onEnter()
    local baseNode = self._rootNode:getChildByName("Node_Base");
    if baseNode then
        ViewPopAnimation(baseNode);
    end


end


------------------------------------------------
--按钮的触摸
function InputLayer:BtnTouchCallBack(sender,eventType)
    if eventType == ccui.TouchEventType.began then
        --播放按钮音效
        ButtonSE();
    end
end


--数字按钮的触摸
function InputLayer:NumBtnTouchCallBack(sender,eventType)
    if eventType == ccui.TouchEventType.began then
        --播放按钮音效
        ButtonSE();
    end
end


--关闭按钮的实现
function InputLayer:CloseBtnCallback(sender)
    --移除界面
    local baseNode = self._rootNode:getChildByName("Node_Base");
    if baseNode then
        ViewCloseAnimation(baseNode, function()
            self:removeFromParent();
        end);
    end
end


--数字按钮的实现
function InputLayer:NumBtnCallBack(sender)
    --获取按钮的标签
    local btnTag = sender:getTag();
    
    local baseNode = self._rootNode:getChildByName("Node_Base");
    if baseNode == nil then
        return nil;
    end


    --获取当前输入的索引
    local currNumIndex = self._currInputNumIndex;
    --不同按钮的不同操作
    if btnTag>0 and btnTag<10 then
        --数字1~9的按钮
        if currNumIndex <= 6 then
            --添加数字到table
            self._inputNumTable[currNumIndex] = btnTag;
            --显示输入的数字
            local textNum = baseNode:getChildByName( string.format("Text_num_%d",currNumIndex) );
            if textNum ~= nil then
                textNum:setString(string.format("%d",btnTag));
            end
            --输入数字的索引增加
            self._currInputNumIndex = currNumIndex + 1;
        end


        --数字索引大于等于6 直接调用确认进入
        if currNumIndex >= 6 then
            self:confirmJoin();
        end


    elseif btnTag == 11 then
        --数字0的按钮
        if currNumIndex <= 6 then
            --显示输入的数字
            local textNum = baseNode:getChildByName( string.format("Text_num_%d",currNumIndex) );
            if textNum ~= nil then
                textNum:setString(string.format("%d",0));
            end
            --添加数字到table
            self._inputNumTable[currNumIndex] = 0;
            --输入数字的索引增加
            self._currInputNumIndex = currNumIndex + 1;
        end


        --数字索引大于等于6 直接调用确认进入
        if currNumIndex >= 6 then
            self:confirmJoin();
        end


    elseif btnTag == 10 then
        --重输的按钮
        self._isEnter = false;
        self:confirmJoin();
        
    elseif btnTag ==12 then
        --删除的按钮
        self._isEnter = false;
        if currNumIndex>1 then
            --删除一个显示的数字
            local textNum = baseNode:getChildByName( string.format("Text_num_%d",currNumIndex-1) );
            if textNum ~= nil then
                textNum:setString("");
            end


            --删除table表中的一个数字
            self._inputNumTable[currNumIndex-1] = nil;
            self._currInputNumIndex = currNumIndex-1;
        end
    --确定按钮
    elseif btnTag == 13 then    
        self:ClearInputNum();
    end
end


--确认加入房间
function InputLayer:confirmJoin()
    if self._currInputNumIndex >= 7 and self._isEnter == false then
        --self._isEnter = true;
        --跳转
        print("InputLayer: number is full!");
        if self._layerType == INPUT_LAYER_JOIN_ROOM then
            --发送加入房间的信息
            local RoomDataManager = require("app.Manager.RoomDataManager");  
            --获取房间号          
            local inputNumStr = "";
            for i=1,6 do
                inputNumStr = inputNumStr .. IntToString(self._inputNumTable[i]);
            end
            local roomId = StringToInt(inputNumStr);
           
            --发送房间号
            RoomDataManager:SendJoinRoom(roomId);
            print("send roomId=",roomId);
--                isCoinPlay = false      --非匹配赛
            --添加加载层
            AddLoadingLayer(10, TryToReconnectServer);


        elseif self._layerType == INPUT_LAYER_SEE_REPLAY then
            --查看他人回放的界面


        end
    end
end


function InputLayer:ClearInputNum()
    local baseNode = self._rootNode:getChildByName("Node_Base");
    if baseNode == nil then
        return nil;
    end


    --清除显示的数字
    for i=1,6 do
        local textNum = baseNode:getChildByName( string.format("Text_num_%d",i) );
        if textNum ~= nil then
            textNum:setString("");
        end
    end


    --清除table
    self._inputNumTable = {};
    self._currInputNumIndex = 1;
end
------------------------------------------------------------------
--加载场景
function InputLayer:InitLayer()


    --创建有颜色的底层
    local diLayer = cc.LayerColor:create(cc.c4b(0,0,0,150));


    --获取界面
    local upLayer = cc.CSLoader:createNode("InputLayer.csb");
    self._rootNode = upLayer;
    diLayer:addChild(upLayer);


    --关闭按钮的功能
    local function closeBtnFunc(sender)
        self:CloseBtnCallback(sender);
    end


    local baseNode = upLayer:getChildByName("Node_Base");
    if baseNode == nil then
        return nil;
    end


    --获取关闭按钮
    local closeBtn = baseNode:getChildByName("Button_back");
    if closeBtn ~= nil then
        closeBtn:addClickEventListener(closeBtnFunc);
        closeBtn:addTouchEventListener(handler(self, self.BtnTouchCallBack));
    end


    --获取文字
    local textNotice = baseNode:getChildByName("Text_title");
    if textNotice ~= nil then
        if self._layerType == INPUT_LAYER_JOIN_ROOM then
            --显示请输入房间号
            print("id=",DictTable_InputLayer_JoinRoom,"; g_DictTable Count=",#g_DictTable)      
            textNotice:setString( g_DictTable[DictTable_InputLayer_JoinRoom]["Content"] );       
        else
            --显示请输入他人分享的回访码
            textNotice:setString(g_DictTable[DictTable_InputLayer_SeeReplay]["Content"]);
        end
    end


    --获取输入的号码
    for i=1,6 do
        local TextNum = baseNode:getChildByName( string.format("Text_num_%d",i) );
        if TextNum ~= nil then
            TextNum:setString("");
        end
    end
    
    --按钮的功能
    local function numBtnTouch(sender,eventType)
        self:NumBtnTouchCallBack(sender,eventType);
    end
    local function numBtnFunc(sender)
        self:NumBtnCallBack(sender);
    end
    --获取按钮
    for i=1,13 do
        local numBtn = baseNode:getChildByName( string.format("Button_num_%d",i) );
        if numBtn ~= nil then
            numBtn:addClickEventListener(numBtnFunc);
            numBtn:addTouchEventListener(numBtnTouch);
            numBtn:setTag(i);
        end
    end
   


    return diLayer;
end






return InputLayer;