#include "WidgetMgr.h"#include "XWidget.h"#include "Config.h"#include "XButton.h"#include "XImage.h"#include "XStatic.h"#include "XNoTouch.h"#include "XSroll.h"CWidgetMgr* CWidgetMgr::g_pUiMgr = NULL;CWidgetMgr::CWidgetMgr(){ m_deqWnd.clear();}CWidgetMgr::~CWidgetMgr(){ m_deqWnd.clear();}CWidgetMgr* CWidgetMgr::getInstance(){ if (!g_pUiMgr) { g_pUiMgr = new CWidgetMgr(); ASSERT(g_pUiMgr); } return g_pUiMgr;}void CWidgetMgr::destroy(){ SAFE_DELETE(g_pUiMgr);}bool CWidgetMgr::addWnd( CXWindow* pWnd, PRIORITY tpOrder /*= TP_NORMAL*/ ){ ASSERT(pWnd); for (deque::iterator it = m_deqWnd.begin(); it != m_deqWnd.end(); it++) { if (pWnd == (*it)) { CCLog("error: CWidgetMgr::addWnd"); ASSERT(false); } } m_deqWnd.push_back(pWnd); //设置窗口的优先级 //数字越低优先级越高 //pWnd->setTouchPriority((TOUCH_PRIORITY)tpOrder); return true;}void CWidgetMgr::eraseWnd( int nId ){ for (unsigned int i = 0; i < m_deqWnd.size(); i++ ) { if (nId == m_deqWnd[i]->getId()) { m_deqWnd.erase(m_deqWnd.begin() + i); } }}CXWindow* CWidgetMgr::getWnd( int nId ){ for (unsigned int i = 0; i < m_deqWnd.size(); i++ ) { if (nId == m_deqWnd[i]->getId()) { return m_deqWnd[i]; } } CCLog("error: CWidgetMgr::getWnd"); return NULL;}CXWindow* CWidgetMgr::loadWidget( CXWidget* pWnd, int nId ){ vector & vecWidgetInfo = *(CConfig::getInstance()->getWidgetInfo()); for (unsigned int i = 0; i < vecWidgetInfo.size(); i++) { if (vecWidgetInfo[i].nId == nId) { pWnd->init(vecWidgetInfo[i].nId, vecWidgetInfo[i].tPos, vecWidgetInfo[i].rRect, vecWidgetInfo[i].strPic.c_str()); pWnd->setAnchorPoint(vecWidgetInfo[i].tAnchor); vector & vecCtrlInfo = vecWidgetInfo[i].m_vecCtrlInfo; for (unsigned int j = 0; j < vecCtrlInfo.size(); j++) { switch( vecCtrlInfo[j].wType) { case CtrlInfo::_IDC_BTN : { CXButton* pBtn = CXButton::create(vecCtrlInfo[j].nId, vecCtrlInfo[j].tPos,vecCtrlInfo[j].rRect, vecCtrlInfo[j].strPic.c_str(), NULL, vecCtrlInfo[j].strAudioFile.c_str()); if (pBtn) { pBtn->setAnchorPoint(vecCtrlInfo[j].tAnchor); pBtn->setBtnType((CXButton::BTN_TYPE)vecCtrlInfo[j].btnType); pBtn->setBtnState((CXButton::BTN_STATE)vecCtrlInfo[j].btnState); pWnd->addCtrl(pBtn,0); } } break; case CtrlInfo::_IDC_IMG: { CXImage* pImg = CXImage::create(vecCtrlInfo[j].nId, vecCtrlInfo[j].tPos, vecCtrlInfo[j].rRect, vecCtrlInfo[j].strPic.c_str()); if (pImg) { pWnd->setAnchorPoint(vecCtrlInfo[j].tAnchor); pWnd->addCtrl(pImg,0); } } break; case CtrlInfo::_IDC_STA: { CXStatic* pSta = CXStatic::create(vecCtrlInfo[j].nId, vecCtrlInfo[j].tPos,vecCtrlInfo[j].rRect, vecCtrlInfo[j].strFont.c_str(),vecCtrlInfo[j].nFontSize, vecCtrlInfo[j].wAlignment, (CXStatic::LabelType)vecCtrlInfo[j].wLabelType); if (pSta) { pSta->setAnchorPoint(vecCtrlInfo[j].tAnchor); pSta->setText(vecCtrlInfo[j].strText.c_str()); pSta->setColor(vecCtrlInfo[j].color3B); pWnd->addCtrl(pSta,0); } } break; case CtrlInfo::_IDC_SROLL: { CXSroll* pSroll = CXSroll::create(vecCtrlInfo[j].nId, vecCtrlInfo[j].tPos, vecCtrlInfo[j].rRect, NULL); if (pSroll) { pSroll->setSrollType((CXSroll::XSrollType)vecCtrlInfo[j].wRollType); pSroll->setBounds(vecCtrlInfo[j].fBounds); vector & widgetInfo = *CConfig::getInstance()->getSrollInfoByKey(vecCtrlInfo[j].strItem.c_str()); for (vector ::iterator it = widgetInfo.begin(); it != widgetInfo.end(); it++) { CXWidget* pWidget = CXWidget::create(it->nId, it->tPos, it->rRect, it->strPic.c_str(), NULL); for (vector ::iterator itCtrl = it->m_vecCtrlInfo.begin(); itCtrl != it->m_vecCtrlInfo.end(); itCtrl++) { switch( itCtrl->wType) { case CtrlInfo::_IDC_BTN : { CXButton* pBtn = CXButton::create(itCtrl->nId, itCtrl->tPos,itCtrl->rRect, itCtrl->strPic.c_str(), NULL, itCtrl->strAudioFile.c_str()); if (pBtn) { pBtn->setAnchorPoint(itCtrl->tAnchor); pBtn->setBtnType((CXButton::BTN_TYPE)itCtrl->btnType); pBtn->setBtnState((CXButton::BTN_STATE)itCtrl->btnState); pWidget->addCtrl(pBtn,0); pBtn->setTouchEnabled(false); } } break; case CtrlInfo::_IDC_IMG: { CXImage* pImg = CXImage::create(itCtrl->nId, itCtrl->tPos, itCtrl->rRect, itCtrl->strPic.c_str()); if (pImg) { pWnd->setAnchorPoint(itCtrl->tAnchor); pWidget->addCtrl(pImg,0); } } break; case CtrlInfo::_IDC_STA: { CXStatic* pSta = CXStatic::create(itCtrl->nId, itCtrl->tPos,itCtrl->rRect, itCtrl->strFont.c_str(),itCtrl->nFontSize,itCtrl->wAlignment, (CXStatic::LabelType)itCtrl->wLabelType); if (pSta) { pSta->setAnchorPoint(itCtrl->tAnchor); pSta->setText(itCtrl->strText.c_str()); pSta->setColor(itCtrl->color3B); pWidget->addCtrl(pSta,0); } } break; default: break; } } pSroll->addItem(pWidget, 0); } pWnd->addCtrl(pSroll,0); } } break; case CtrlInfo::_IDC_NOT: { CXNoTouch* pNot = CXNoTouch::create(vecCtrlInfo[j].nId, vecCtrlInfo[j].tPos, vecCtrlInfo[j].rRect); if (pNot) { pNot->setNoTouchType((CXNoTouch::NoTouchType)vecCtrlInfo[j].notType); pNot->setAnchorPoint(vecCtrlInfo[j].tAnchor); pNot->setTouchPriority((TOUCH_PRIORITY)vecCtrlInfo[j].nPriority); pWnd->addCtrl(pNot,0); } } break; default: break; } } return pWnd; } } return NULL;}void CWidgetMgr::clearAllWnd(){ for(deque ::iterator it = m_deqWnd.begin(); it != m_deqWnd.end(); it++ ) { (*it)->removeFromParentAndCleanup(true); } m_deqWnd.clear();}