简单的模态对话框ModalDialog -- 源码分享

简单封了一个模态对话框和大家分享。


使用注意事项:
1. 注意设置contentSize,这个是不屏蔽点击的区域,以屏幕中心点为中心,辐射contentsize大小的区域;

2. m_pModelType 为弹框类型,以便自己在扩展;

3. 为了避免多次弹窗,最好在调用弹窗的类中增加弹窗类属性,以避免多次弹窗;


避免多次弹窗使用实例如下:

1
2
3
4
5
6
7
8
9
10
11
12
void CMainMenuScene::__addModalDialog( EModalType modalType /* = LEVEL_LOADING*/ , CCPoint pos /* = CCPointZero*/ )
{
     m_pModalDialog = CModalDialog::create(modalType);
     m_pModalDialog->setPosition(pos);
     this ->addChild(m_pModalDialog, ZORDER_MODAL_DIALOG);
}
  
void CMainMenuScene::__removeMoadalDialog()
{
     m_pModalDialog->removeFromParentAndCleanup( true );
     m_pModalDialog = NULL;
}


ModalDialog.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
//  ModalDialog.h
//
//  WeArePests
//
//  Created by firedragonpzy o 2014-06-09
//
//
  
#ifndef __WEAREPESTS__MODALDIALOG__
#define __WEAREPESTS__MODALDIALOG__
  
#include "cocos2d.h"
#include "UtilTools\RConfig.h"
#include "UtilTools\VisibleRect.h"
  
USING_NS_CC;
  
enum EModalType
{
     LEVEL_LOADING   //  关卡loading
};
  
//********************************************************************************************************
//Copyright © 2014  firedragonpzy
//
//作  者:firedragonpzy
//日 期:2014/06/09 15:21:14
//描 述:模态窗口,如果有背景图片,设置层的大小为背景图大小,否则为CCSizeZero
//版 本:1.00
//修改历史记录
//版 本     修改时间      修改人     变更内容
//1.00       2014/06/09 15:21:14   firedragonpzy        新增
//********************************************************************************************************
class CModalDialog : public CCLayer
{
public :
     virtual bool init();
     void onEnter();
     void onExit();
     virtual void registerWithTouchDispatcher( void );
     static CModalDialog* create(EModalType modalType = LEVEL_LOADING);
     bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
protected :
  
     CModalDialog( EModalType modalType );
     ~CModalDialog();
  
private :
     //  弹窗类型
     EModalType m_pModelType;
  
     void __initParams();
     void __initResources();
  
     //  是不是在弹框大小的区域内
     bool __isContaintLocation(CCPoint pos);
     //  得到弹窗的区域
     CCRect __getAtalsRect();
};
 
#endif


ModalDialog.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "View\ModalDialog.h"
  
enum ZORDER
{
     ZORDER_BG = -1,
};
  
CModalDialog::CModalDialog( EModalType modalType )
{
     m_pModelType = modalType;
}
  
CModalDialog::~CModalDialog()
{
  
}
  
CModalDialog* CModalDialog::create( EModalType modalType /*= LEVEL_LOADING */ )
{
     CModalDialog *dialog = new CModalDialog(modalType);
  
     if (dialog && dialog->init())
     {
         dialog->autorelease();
         return dialog;
     }
  
     dialog->release();
     dialog = NULL;
     return dialog;
  
}
  
bool CModalDialog::init()
{
     bool bRet = false ;
  
     do
     {
         __initParams();
         CC_BREAK_IF(!CCLayer::init());
         bRet = true ;
     } while (0);
  
     return bRet;
}
  
void CModalDialog::registerWithTouchDispatcher()
{
     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate( this , -128, true );
}
  
void CModalDialog::__initParams()
{
     setContentSize(CCSizeZero);
}
  
void CModalDialog::onEnter()
{
     CCLayer::onEnter();
     setTouchEnabled( true );
     __initResources();
}
  
void CModalDialog::onExit()
{
     CCLayer::onExit();
     CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate( this );
}
  
void CModalDialog::__initResources()
{
     CCSprite *bg = NULL;
     switch (m_pModelType)
     {
     case LEVEL_LOADING:
         bg = CCSprite::create(GAME_LOADING_BG);
         bg->setPosition(ccp(VisibleRect::getVisibleRect().size.width/2, VisibleRect::getVisibleRect().size.height/2));
         this ->addChild(bg,ZORDER_BG);
         setContentSize(bg->getContentSize());
         break ;
     default :
         break ;
     }
}
  
bool CModalDialog::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
     if (!__isContaintLocation(pTouch->getLocation()))
     {
         return true ;
     }
     return false ;
}
  
bool CModalDialog::__isContaintLocation(CCPoint pos)
{
     return __getAtalsRect().containsPoint(pos);
}
  
CCRect CModalDialog::__getAtalsRect()
{
     CCSize cSize = getContentSize();
     return CCRectMake(VisibleRect::getVisibleRect().size.width/2 - cSize.width/2 ,VisibleRect::getVisibleRect().size.height/2 - cSize.height/2, cSize.width,cSize.height);
}

来源网址:http://www.firedragonpzy.com.cn/index.php/archives/4374

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值