1.無法立即編輯 但是確定可以!須自行補上缺失的h
2.按鈕直接引用 button 元件3. 加入 CC_SYNTHESIZE ,將變數獨立。
有加入背景變暗功能 m_UI_Background
4.使用方式
init:
Layer* setTimeDialog = DislogSetTime::create();
//setTimeDialog->setPosition(winSize*0.5);
setTimeDialog->setVisible(false);
addChild(setTimeDialog,16);
show:
setTimeDialog->setVisible(true);
DislogSetTime* pDialog=(DislogSetTime*)setTimeDialog;
pDialog->setDayName(sDayString);
pDialog->setSetTime(scheduleTime);
pDialog->showDialog();
Hide:
setTimeDialog->setVisible(false);
DislogSetTime* pDialog=(DislogSetTime*)setTimeDialog;
pDialog->HideDialog();
程式碼:
////////////////////////////////////////////////////////////
#ifndef __ROBOTDIALOG_H__
#define __ROBOTDIALOG_H__
#include "Def_ImageList.h"
#include "Robot_NumberPicker.h"
#include "extensions/cocos-ext.h"
#include "ui/CocosGUI.h"
using namespace ui;
using namespace cocos2d;
//頁面種類
enum _DIALOGSETTIMEITEM_
{
DIATIME_LAYER_BK=0,
DIATIME_SPRITE_CANEL,
DIATIME_SPRITE_SAVE,
DIATIME_NOTOUCH, //此線以下不偵測TOUCH
DIATIME_LAB_MESSAGE=DIATIME_NOTOUCH,
DIATIME_PICKER_MIN,
DIATIME_PICKER_HOUR,
DIATIME_SIZE,
};
class DislogSetTime: public Layer
{
//對話框
protected:
//初始化對話框
void InitDialog();
/* 加載對話框
* @parm:msg 消息 顯示在界面上的提示消息
* @parm:fun 回調函數 給確定按鈕調用的
* @parm:isNeedCancel 是否需要取消按鈕
* @parm:isModalDialog 是否是模態對話框
*/
void LoadDialog(std::string msg,std::function<void(Ref*)> fun,bool isNeedCancel = false,bool isModalDialog = true);
//對話框加載完成
// void LoadDialogFinish(Armature *a, MovementEventType b, std::string c);
//隱藏對話框
void HideDialog();
//隱藏對話框完成
// void HideDialogFinish(Armature *a, MovementEventType b, std::string c);
//對話框取消按鈕callback
void PressBtnCancel(Ref *pSender, Widget::TouchEventType type);
//對話框確認按鈕callback
void PressBtnConfirm(Ref *pSender, Widget::TouchEventType type);
//virtual void SetScene();
//標誌變量
public:
//視窗變數
Size visibleSize;
Vec2 origin;
bool m_IsNeedExit;
//UI變量
public:
Sprite* m_spr_ExitTip; //退出提示
Sprite* Dialog; //對話框
Label* m_DialogMsg; //對話框消息
UINumberPicker *m_Hourpicker;
UINumberPicker *m_Minatepicker;
Button* m_DialogConfirm; //對話框確認按鈕
Button* m_DialogCancel; //對話框取消按鈕
//層變量
Layer* m_UI_Dialog; //對話框層 最高層
Layer* m_UI_Background; //背景層 底層
public:
std::function<void(Ref*)> m_Dialog_ConfirmCallback; //對話框確認回調
//---------------函數---------------------------
CREATE_FUNC(DislogSetTime);
DislogSetTime();
~DislogSetTime();
virtual bool init() override;
virtual void onEnter() override;
virtual void onExit() override;
void releaseUI();
//bool onTouchBegan(Touch* touch, Event* event) override;
//顯示對話框
void showDialog();
//特別獨立出來的單一變數
CC_SYNTHESIZE(string, dayName, DayName);
CC_SYNTHESIZE(time_t, setTime, SetTime);
//工具
public:
Point getCenterPoint() { return Point(origin.x+visibleSize.width/2,origin.y+visibleSize.height/2); };
Point getOriginPoint() { return Point(origin.x,origin.y); };
};
#endif // __ROBOTDIALOG_H__
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Def_ImageList.h"
#include "Robot_Dialog.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace std;
using namespace cocos2d::ui;
DislogSetTime::DislogSetTime()
{
}
DislogSetTime::~DislogSetTime()
{
}
bool DislogSetTime::init()
{
if (!Layer::init())
{
return false;
}
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
m_UI_Dialog = Layer::create();
addChild(m_UI_Dialog,40);
m_UI_Dialog->setPosition(getOriginPoint());
//第一個參數是顏色ccc4(r,g,b,a) a取值(0~255),越大越不透明 下面兩個參數為寬高,不傳默認為屏幕大小
m_UI_Background = LayerColor::create(Color4B(0,0,0,153),visibleSize.width,visibleSize.height);
m_UI_Background->setPosition(getOriginPoint());
this->addChild(m_UI_Background,10);
InitDialog();
// SetScene();
return true;
}
//初始化對話框
void DislogSetTime::InitDialog()
{
// auto ImageScale = Director::getInstance()->getContentScaleFactor();
const float winWidth=950;
const float winHeight=900;
float W_unit=1.0f;
float H_unit=1.0f;
Point LabSite_UM_MESSAGE=Vec2(winWidth*0.5*W_unit,(winHeight-25)*H_unit);
Point PickerSiteOnBtn_Minate=Vec2(0.7*winWidth*W_unit,winHeight*0.3*H_unit);
Point PickerSiteOnBtn_Hour=Vec2(0.3*winWidth*W_unit,winHeight*0.3*H_unit);
//設定滾輪大小
Size slotItemSize =Size(210*W_unit,150*H_unit);
//建立對話框
Dialog = Sprite::createWithSpriteFrameName(S_BTN_DIALOG_BK);
// Dialog->setContentSize(Size(winWidth*W_unit,winHeight*H_unit));
Dialog->setPosition(getCenterPoint());
m_UI_Dialog->addChild(Dialog);
Dialog->setVisible(true);
//視窗說明文件
m_DialogMsg = Label::createWithSystemFont(STR_RESERV_SetTime_CHS, S_FONE, getFontSize(G_FSIZE_RESERV_SetTime));
m_DialogMsg->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
m_DialogMsg->setTextColor(COLOR_STR_BLUE);
m_DialogMsg->setPosition(LabSite_UM_MESSAGE);
Dialog->addChild(m_DialogMsg,15);
//加入數字滾輪:分
m_Minatepicker = UINumberPicker::create();
m_Minatepicker->setItemSize(slotItemSize.width,slotItemSize.height);
m_Minatepicker->setDataprovider(3, 0, 59,1);
m_Minatepicker->setPosition(PickerSiteOnBtn_Minate); //set the postions
// picker->setSkinFrameName(S_RESERVATION_SETTIME_BK, S_SWITCH_BACKGROUNP);
Dialog->addChild(m_Minatepicker,12);
m_Minatepicker->setTag(DIATIME_PICKER_MIN);
// m_Minatepicker->valueChangeHandler = std::bind(&DislogSetTime::pickerChangeHandler,this, std::placeholders::_1);
//加入數字滾輪:時
m_Hourpicker = UINumberPicker::create();
m_Hourpicker->setItemSize(slotItemSize.width,slotItemSize.height);
m_Hourpicker->setDataprovider(3, 0, 23,1);
m_Hourpicker->setPosition(PickerSiteOnBtn_Hour); //set the postions
// picker->setSkinFrameName(S_RESERVATION_SETTIME_BK, S_SWITCH_BACKGROUNP);
Dialog->addChild(m_Hourpicker,12);
// m_Hourpicker->valueChangeHandler = std::bind(&DislogSetTime::pickerChangeHandler,this, std::placeholders::_1);
m_Hourpicker->setTag(DIATIME_PICKER_HOUR);
//底下兩個按鈕
//BUTTON元件 版本
m_DialogConfirm = Button::create();
m_DialogConfirm->setTouchEnabled(false);
m_DialogConfirm->loadTextureNormal(S_BTN_DIALOG_RIGHT, Widget::TextureResType::PLIST);
m_DialogConfirm->setTitleText(STR_BTN_Save_CHS);
m_DialogConfirm->setTitleFontSize(getFontSize(G_FSIZE_DIALOG_BtnLabel));
m_DialogConfirm->setTitleColor(COLOR_BG_WHITE);
m_DialogConfirm->setPosition(Vec2(winWidth*W_unit,0*H_unit));
m_DialogConfirm->setAnchorPoint(Vec2::ANCHOR_BOTTOM_RIGHT);
m_DialogConfirm->addTouchEventListener(CC_CALLBACK_2(DislogSetTime::PressBtnConfirm,this));
Dialog->addChild(m_DialogConfirm,12);
m_DialogCancel = Button::create();
m_DialogCancel->loadTextureNormal(S_BTN_DIALOG_LEFT, Widget::TextureResType::PLIST);
m_DialogCancel->setTitleText(STR_BTN_Canel_CHS);
m_DialogCancel->setTitleFontSize(getFontSize(G_FSIZE_DIALOG_BtnLabel));
m_DialogCancel->setTitleColor(COLOR_BG_WHITE);
m_DialogCancel->setPosition(Vec2(0,0));
m_DialogCancel->setAnchorPoint(Vec2::ZERO);
m_DialogCancel->addTouchEventListener(CC_CALLBACK_2(DislogSetTime::PressBtnConfirm,this));
m_DialogCancel->addTouchEventListener(CC_CALLBACK_2(DislogSetTime::PressBtnCancel, this));
Dialog->addChild(m_DialogCancel,12);
}
//void DislogSetTime::SetScene()
//{
// auto bg = ImageView::create();
// bg->setContentSize(Size(950,900));
// bg->setPosition(getCenterPoint());
// auto listen = EventListenerTouchOneByOne::create();
// listen->onTouchBegan = CC_CALLBACK_2(DislogSetTime::onTouchBegan, this);
// _eventDispatcher->addEventListenerWithSceneGraphPriority(listen,Dialog);
// m_UI_Background->addChild(bg);
//}
//bool DislogSetTime::onTouchBegan(Touch* touch, Event* event)
//{
// auto target = static_cast<Layer*>(event->getCurrentTarget());
// if(target==nullptr)
// {
// return true;
// }
//// log("%s",__PRETTY_FUNCTION__);
//// Vec2 touchLocation = touch->getLocation();
//// Vec2 nodePosition = target->convertToNodeSpace( touchLocation );
// if(!isVisible())
// return false;
// return true;
//}
void DislogSetTime::releaseUI()
{
time_t setTime=getSetTime();
char sShowHelpStr[48];
sprintf(sShowHelpStr,STR_RESERV_SetTime_CHS, getDayName().c_str());
m_DialogMsg->setString(sShowHelpStr);
tm *pTime=localtime(&setTime);
m_Hourpicker->setValue(pTime->tm_hour);
m_Minatepicker->setValue(pTime->tm_min);
}
//进入
void DislogSetTime::onEnter()
{
Layer::onEnter();
}
//退出
void DislogSetTime::onExit()
{
Layer::onExit();
}
//顯示對話框
void DislogSetTime::showDialog()
{
m_UI_Dialog->setVisible(true);
m_UI_Background->setVisible(true);
m_DialogConfirm->setVisible(true);
m_DialogConfirm->setTouchEnabled(true);
m_DialogCancel->setVisible(true);
m_DialogCancel->setTouchEnabled(true);
auto listener1 = EventListenerTouchOneByOne::create();//创建一个触摸监听
listener1->setSwallowTouches(true);//设置不想向下传递触摸
listener1->onTouchBegan = [](Touch* touch, Event* event){
CCLOG("touch Swallow");
return true;
};
//增加遮蔽版
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,m_UI_Dialog);
releaseUI();
}
//隐藏對話框
void DislogSetTime::HideDialog()
{
m_UI_Dialog->setVisible(false);
m_UI_Background->setVisible(false);
m_DialogConfirm->setVisible(false);
m_DialogConfirm->setTouchEnabled(false);
m_DialogCancel->setVisible(false);
m_DialogCancel->setTouchEnabled(false);
//移除遮蔽版
_eventDispatcher->removeEventListenersForTarget(m_UI_Dialog);
}
//取消按鈕
void DislogSetTime::PressBtnCancel(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
default:
break;
case Widget::TouchEventType::ENDED:
{
HideDialog();
break;
}
}
}
//確認按鈕
void DislogSetTime::PressBtnConfirm(Ref *pSender, Widget::TouchEventType type)
{
switch (type)
{
default:
break;
case Widget::TouchEventType::ENDED:
{
HideDialog();
break;
}
}
}
//////////////////////////////////////////
沒有留言:
張貼留言