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;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值