lua -- 系統提示框


--
-- Author: chentong
-- Date: 2014-3-24
-- 系統提示:
-- 先做個簡單的,在中上位置出現提示,逐漸消失,如果有新提示,則直接刪除舊提示。
-- 規則:
-- 1、根據消息編號和語言,從表中找到提示內容,如果有參數則格式化;
-- 
-- 需要擴展:主要是字體,大小,顏色,類型等的一些格式化,不知道CCLabelTTF支不支持,后續研究。
-- 
local Prompt = require(__APP_PACKAGE_NAME__ .. ".network.cliprompt");

local UISystemTips = class("UISystemTips", Behavior);

function UISystemTips:ctor( )
    UISystemTips.super.ctor(self);
    self.Name = "UISystemTips"
    self.layer = nil;
    self.labTips = nil;
    self.imgBg = nil;
    self.labTipsBg = nil; -- 對話框上的提示。
    self.func = nil;
    self.btnConfirm = nil;
    self.btnCancel = nil;
end

function UISystemTips:onAwake( )
    print("UISystemTips:onAwake", self.id);
    
    -- 創建層:
    self.layer = CCLayer:create();

    self.labTips = CCLabelTTF:create("", FONT_NAME, 30, CCSizeMake(600,0), kCCTextAlignmentCenter);
    self.labTips:setAnchorPoint(0.5, 1);
    self.labTips:setPosition(math.round(CONFIG_SCREEN_WIDTH/2), CONFIG_SCREEN_HEIGHT-100);
    self.labTips:setColor(ccc3(230, 120, 230));
    self.layer:addChild(self.labTips);
        
    SceneM.addChildToGlobalNode(self.layer);

    self.layer:setTouchEnabled(false);
    self.layer:registerScriptTouchHandler(function (tag)
        return self.layer:isVisible();
    end, false, -200, true);

    -- 公共框:
    self.imgBg = CCSprite:create(res.common.textures["comm_dlg"]);
    self.imgBg:setPosition(ccp(CONFIG_SCREEN_WIDTH/2, CONFIG_SCREEN_HEIGHT/2));
    self.imgBg:setVisible(false);
    local sizeBg = self.imgBg:getContentSize();
    -- self.imgBg:setScale(0.7);
    self.layer:addChild(self.imgBg);
    -- 提示文字:
    self.labTipsBg = CCLabelTTF:create("", FONT_NAME, 30, CCSizeMake(460,0), kCCTextAlignmentCenter);
    self.labTipsBg:setAnchorPoint(0.5, 1);
    self.labTipsBg:setPosition(math.round(sizeBg.width/2), sizeBg.height-80);
    self.labTipsBg:setColor(ccc3(230, 120, 230));
    self.imgBg:addChild(self.labTipsBg);

    -- 確定按鈕:
    print("\n\n\n====================" .. self:GetTipsString("already_recuit"))
    self.btnConfirm = CCMenuItemImage:create(res.common.textures["comm_btn"],res.common.textures["comm_btn"]);
    self.btnConfirm:registerScriptTapHandler(function (tag)
        if self.func and type(self.func) == "function" then
            self.func(true);
        end
        self:ChangeDlgState(false);
        return true;
    end);
    self.btnConfirm:setPosition(ccp(sizeBg.width/3, 20));
    local labConfirm = CCLabelTTF:create(self:GetTipsString("confirm"), FONT_NAME, 30);
    labConfirm:setPosition(ccp(65, 35));
    self.btnConfirm:addChild(labConfirm);
    -- 取消按鈕:
    self.btnCancel = CCMenuItemImage:create(res.common.textures["comm_btn"],res.common.textures["comm_btn"]);
    self.btnCancel:registerScriptTapHandler(function (tag)
        if self.func and type(self.func) == "function" then
            self.func(false);
        end
        self:ChangeDlgState(false);
        return true;
    end);
    self.btnCancel:setPosition(ccp(sizeBg.width*2/3, 20));
    local labCancel = CCLabelTTF:create(self:GetTipsString("cancel"), FONT_NAME, 30);
    labCancel:setPosition(ccp(65, 35));
    self.btnCancel:addChild(labCancel);

    local menu = CCMenu:create();
    menu:setTouchPriority(-230);
    menu:setPosition(0, 0)
    menu:addChild(self.btnConfirm, 1);
    menu:addChild(self.btnCancel, 1);
    self.imgBg:addChild(menu);
end

function UISystemTips:onEnter( )
    
end

function UISystemTips:onExit( )
    -- 
    SceneM.removeChildToGlobalNode(self.layer);
end

function UISystemTips:getName( )
    return self.Name;
end

-- 系統提示:
-- idx:為信息號,可從表(cliprompt)中找到對應信息;如果表中找不到,則直接輸出信息號。
function UISystemTips:SystemTips(idx, ...)
    if idx==nil then
        echoError("UISystemTip:SystemTip Error: 傳入空的錯誤碼!");
        return
    end

    self.labTips:stopAllActions();
    
    -- 根據語言和id,從表中找到提示信息:    
    local strTip = self:GetTipsString(idx) or idx;
    -- 可以做些字符串解析:如改變字體顏色,變化成圖標,加上某個前綴等等。
    
    -- 根據入參,格式化:
    if ... ~=nil then
        strTip = string.format(strTip, ...);
    end

    self.labTips:setString(strTip);
    -- 創建消失動畫。
    local actionFadeOut = CCFadeOut:create(5);
     self.labTips:runAction(actionFadeOut);
end

-- 系統提示框:
-- idx:為信息號,可從表(cliprompt)中找到對應信息;
-- fun(可選):回調的函數,原型為fun(bCancel);
-- type(可選):兩種類型:1、確定按鈕;2、確定和取消按鈕;默認只有確定按鈕。
-- ...(可選):參數列表。
function UISystemTips:SystemDlg( idx, func, nType, ...)
    -- 根據語言和id,從表中找到提示信息:    
    local strTip = self:GetTipsString(idx) or idx;
    -- 根據入參,格式化:
    if ... ~=nil then
        strTip = string.format(strTip, ...);
    end
    self.labTipsBg:setString(strTip);

    -- 默認為類型1,即一個確定按鈕;
    local nDlgType = nType or 1;
    local sizeBg = self.imgBg:getContentSize();
    if nDlgType==1 then
        -- 1:隱藏取消按鈕;確定按鈕居中:
        self.btnCancel:setVisible(false);
        self.btnConfirm:setPosition(ccp(sizeBg.width/2, 20));
    else
        -- 2:顯示取消按鈕;確定按鈕左移:
        self.btnCancel:setVisible(true);
        self.btnConfirm:setPosition(ccp(sizeBg.width/3, 20));
    end

    self.func = func;
    self:ChangeDlgState(true);
end

function UISystemTips:ChangeDlgState(bOpen)
    if bOpen then
        self.imgBg:setVisible(true);
        self.layer:setTouchEnabled(true);
    else
        self.imgBg:setVisible(false);
        self.layer:setTouchEnabled(false);
    end
end

function UISystemTips:GetTipsString( idx )
    -- 如果是number:
    local strIdx;
    if type(idx)=="number" then
        strIdx = Prompt.CliPrompt[idx];
        if strIdx==nil then
            echoError("沒有找到指定錯誤碼:"..idx);
            return "錯誤碼:"..idx;
        end
    elseif type(idx)=="string" then
        strIdx = idx;
    else
        echoError("錯誤的解析碼類型。");
    end

    local tabTips = Prompt.CliPrompt[strIdx];
    if not tabTips then
        echoError("沒有找到指定提示文本:"..strIdx);
        return nil;
    end

    return tabTips[LANGUAGE];
end

return UISystemTips;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM