bool CWidgetWindow::onTouchBegan(Touch *pTouch, Event *pEvent) { if(m_bModalable) return true; unsigned int nCount = _children.size(); if( m_bTouchEnabled && _visible && nCount > 0 ) { Vec2 touchPointInView = convertToNodeSpace(pTouch->getLocation()); for(int i = nCount-1; i >= 0; --i) { Node* pNode = _children.at(i); CWidget* pWidget = dynamic_cast<CWidget*>(pNode); if( pWidget && pNode->isVisible() && pWidget->isEnabled() && pWidget->isTouchEnabled() ) { if( pNode->getBoundingBox().containsPoint(touchPointInView) ) { if( pWidget->executeTouchBeganHandler(pTouch) != eWidgetTouchNone ) { m_pSelectedWidget = pWidget; m_bIsTouched = true; m_fTouchedDuration = 0.000001f; return true; } } } } m_pSelectedWidget = NULL; } return m_bModalable; }
// Client int main() { vector<CButton *> vecPtrButtons; CWidget *pWidgetFactory = new CWidgetFactory; int nChoice; while (true) { cout << "(1)Mac button\n(2)Windows button\n(0)None\n"; cout << "Enter your choice\n"; cin >> nChoice; // Client now does not have knowledge of the actual classes responsible for the // objects it need. if (nChoice == 1) { vecPtrButtons.push_back(pWidgetFactory->createButton("Mac")); } else if (nChoice == 2) { vecPtrButtons.push_back(pWidgetFactory->createButton("Windows")); } else if (nChoice == 0) { break; } } for (int nIdx = 0; nIdx < vecPtrButtons.size(); nIdx++) { vecPtrButtons[nIdx]->paint(); } for (int nIdx = 0; nIdx < vecPtrButtons.size(); nIdx++) { delete vecPtrButtons[nIdx]; } return(0); }
bool CTUI::Run(int delay) { if (m_bQuit) return false; timeout(delay); // Handle keys chtype key = getch(); if (key != static_cast<chtype>(ERR)) // Input available? { if (!m_pWinManager->HandleKey(key) && IsEscape(key)) return false; } Move(m_CursorPos.first, m_CursorPos.second); while (!m_QueuedDrawWidgets.empty()) { CWidget *w = m_QueuedDrawWidgets.front(); m_QueuedDrawWidgets.pop_front(); // Draw after widget has been removed from queue, this way the widget or childs from the // widget can queue themselves while being drawn. w->Draw(); if (m_QueuedDrawWidgets.empty()) DoUpdate(); // Commit real drawing after last widget } return true; }
CWidgetTouchModel CLayout::onTouchBegan(Touch* pTouch) { m_pSelectedWidget = NULL; m_eSelectedWidgetTouchModel = eWidgetTouchNone; Point tNodePoint = convertToNodeSpace(pTouch->getLocation()); int nCount = _children.size(); if(nCount > 0 ) { for(int i = nCount-1; i >= 0; --i) { Node* pNode = _children.at(i); CWidget* pWidget = dynamic_cast<CWidget*>(pNode); if( pWidget && pNode->isVisible() && pWidget->isEnabled() && pWidget->isTouchEnabled() ) { if( pNode->getBoundingBox().containsPoint(tNodePoint) ) { m_eSelectedWidgetTouchModel = pWidget->executeTouchBeganHandler(pTouch); if( m_eSelectedWidgetTouchModel == eWidgetTouchNone ) { m_pSelectedWidget = NULL; m_eSelectedWidgetTouchModel = eWidgetTouchNone; } else { m_pSelectedWidget = pWidget; return m_eSelectedWidgetTouchModel; } } } } } return eWidgetTouchNone; }
void CControls::render(SDL_Surface *dest, int x, int y) { SDL_Rect src, dst; dst.x = x; dst.y = y; dst.w = 256; dst.h = 128; src.x = 0; src.y = 0; src.w = 256; src.h = 128; //SDL_FillRect(surface, &src, 0); int sz = widgets->size(); for (int i = 0; i < sz; i++) { CWidget *w = widgets->at(i); if (w->update()) { w->draw(surface); modified = true; } } if (modified) SDL_BlitSurface(surface, &src, dest, &dst); modified = false; }
bool CWidgetLayout::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { if( m_bTouchEnabled && m_bVisible && m_pChildren && m_pChildren->count() > 0 ) { CCPoint touchPointInView = convertToNodeSpace(pTouch->getLocation()); if( m_pChildren && m_pChildren->count() > 0 ) { CCObject* pObject = NULL; CCARRAY_FOREACH_REVERSE( m_pChildren, pObject ) { CCNode* pNode = dynamic_cast<CCNode*>(pObject); CWidget* pWidget = dynamic_cast<CWidget*>(pObject); if( pWidget && pNode->isVisible() && pWidget->isEnabled() && pWidget->isTouchEnabled() ) { if( pNode->boundingBox().containsPoint(touchPointInView) ) { if( pWidget->executeTouchBeganHandler(pTouch) != eWidgetTouchNone ) { m_pSelectedWidget = pWidget; m_bIsTouched = true; m_fTouchedDuration = 0.0f; return true; } } } }
Node* CLayout::find(Vector<Node*> pChild, const char* id) { int nCount = pChild.size(); if( nCount> 0 ) { for(int i = 0; i < nCount; ++i) { Node* pObject = pChild.at(i); CWidget* pWidget = dynamic_cast<CWidget*>(pObject); if( pWidget ) { if( strcmp(pWidget->getId(), id) == 0 ) { return pObject; } else { Node* pRet = find(dynamic_cast<Node*>(pObject)->getChildren(), id); if( pRet ) { return pRet; } } } } } return NULL; }
void CWidget::PushEvent(int type) { CWidget *parent = m_pParent; while (parent && !parent->HandleEvent(this, type)) parent = parent->m_pParent; }
CCObject* CWidgetLayout::find(CCArray* pChidren, const char* id) { if( pChidren && pChidren->count() > 0 ) { unsigned int nCount = pChidren->count(); for(unsigned int i = 0; i < nCount; ++i) { CCObject* pObject = pChidren->objectAtIndex(i); CWidget* pWidget = dynamic_cast<CWidget*>(pObject); if( pWidget ) { if( strcmp(pWidget->getId(), id) == 0 ) { return pObject; } else { CCObject* pRet = find(dynamic_cast<CCNode*>(pObject)->getChildren(), id); if( pRet ) { return pRet; } } } } } return NULL; }
bool CWindowManager::CoreHandleEvent(CWidget *emitter, int event) { if (CGroup::CoreHandleEvent(emitter, event)) return true; if (event == EVENT_REQQUEUEDDRAW) { CWidget *focwidget = GetFocusedWidget(); if (focwidget) { if ((focwidget != emitter) && !IsChild(emitter, focwidget)) focwidget->RequestQueuedDraw(); } return true; } else if (event == EVENT_REQUPDATE) { if (IsDirectChild(emitter, this)) { m_WidgetQueue.push_back(emitter); UpdateLayout(); PushEvent(EVENT_REQUPDATE); return true; } } return false; }
Node* CWidgetWindow::find(Vector<Node*> &pChidren, const char* id) { if(pChidren.size() > 0 ) { unsigned int nCount = pChidren.size(); for(unsigned int i = 0; i < nCount; ++i) { Node* pObject = pChidren.at(i); CWidget* pWidget = dynamic_cast<CWidget*>(pObject); if( pWidget ) { if( strcmp(pWidget->getId(), id) == 0 ) { return pObject; } else { Node* pRet = find(dynamic_cast<Node*>(pObject)->getChildren(), id); if( pRet ) { return pRet; } } } } } return NULL; }
int main(int argc, char *argv[]) { QApplication a(argc, argv); CWidget w; w.show(); return a.exec(); }
void CWidget::interruptTouchCascade(CCTouch* pTouch, float fDuration) { for(CCNode* pNode = (CCNode*)m_pThisObject; pNode != NULL; pNode = pNode->getParent()) { CWidget* pWidgetParent = dynamic_cast<CWidget*>(pNode); if( pWidgetParent != NULL ) { pWidgetParent->interruptTouch(pTouch, fDuration); } } }
void CControls::mousedown(int x, int y) { int sz = widgets->size(); for (int i = 0; i < sz; i++) { CWidget *w = widgets->at(i); if (w->visible && (x >= w->x) && (y >= w->y) && (x < w->x + w->w) && (y < w->y + w->h)) { w->mousedown(x - w->x, y - w->y); return; } } }
bool IsParent(CWidget *parent, CWidget *child) { CWidget *w = child->GetParentWidget(); while (w) { if (w == parent) return true; w = w->GetParentWidget(); } return false; }
void CWidgetWindow::onTouchesBegan(const std::vector<Touch*>&touches, Event *unused_event) { for(unsigned int i=0;i<touches.size(); i++) { Touch* pTouch = touches.at(i); if( m_bTouchEnabled && m_bMultiTouchEnabled && _visible && _children.size() > 0 ) { Vec2 touchPointInView = convertToNodeSpace(pTouch->getLocation()); for(int j=_children.size()-1;j>=0;--j){ Node* pNode = _children.at(j); CWidget* pWidget = dynamic_cast<CWidget*>(pNode); if( pWidget && pNode->isVisible() && pWidget->isEnabled() && pWidget->isTouchEnabled() ) { bool bSameWidgetBreak = false; if( pNode->getBoundingBox().containsPoint(touchPointInView) ) { //make sure it can not happened on the same widget map<int, __ccMULTITOUCHTARGET>::iterator mitr = m_mMultiTouchKeeper.begin(); for(; mitr != m_mMultiTouchKeeper.end(); ++mitr) { if( mitr->second.pWidget == pWidget ) { bSameWidgetBreak = true; break; } } if( bSameWidgetBreak ) { break; } if( pWidget->executeTouchBeganHandler(pTouch) != eWidgetTouchNone ) { __ccMULTITOUCHTARGET tKeeper; tKeeper.fTouchedDuration = 0.000001f; tKeeper.pWidget = pWidget; tKeeper.pLongClickedWidgetObject = NULL; m_mMultiTouchKeeper[pTouch->getID()] = tKeeper; return; } } } } } } }
void CWindowManager::CoreDrawLayout() { while (!m_WidgetQueue.empty()) { CWidget *w = m_WidgetQueue.front(); m_WidgetQueue.pop_front(); if (w->Enabled()) { const int width = w->RequestWidth(), height = w->RequestHeight(); const int x = (Width() - width) / 2; const int y = (Height() - height) / 2; SetChildSize(w, x, y, width, height); } } }
//***************************************************************************** void CEntranceSelectDialogWidget::OnClick( //Handles click event. Overrides CDialogWidget::OnClick(). // //Params: const UINT dwTagNo) //(in) Widget that received event. { if (!dwTagNo) return; CWidget *pWidget = GetWidget(dwTagNo); if (pWidget->GetType() == WT_Button) //only buttons will return from dialog { //Return a special enumerated value describing what semantic button type //was pressed. switch (dwTagNo) { case TAG_OK: this->dwDeactivateValue = OK; break; case TAG_DELETE: this->dwDeactivateValue = Delete; break; case TAG_DONE: default: this->dwDeactivateValue = Other; break; } Deactivate(); } }
/////////////////// // Mouse down event int CListview::DoMouseDown(int x, int y, int dx, int dy, MouseButton button, const ModifiersState& modstate) { // Scrollbar if(cScrollbar->InBox(x, y)) { cScrollbar->DoMouseDown(x - cScrollbar->getX(), y - cScrollbar->getY(), dx, dy, button, modstate); CContainerWidget::DoMouseDown(x, y, dx, dy, button, modstate); return WID_PROCESSED; } // // Column headers // iGrabbed = -1; // No column grabbed yet if (bShowColumnHeaders) { // Not grabbed if( y >= cBorder.getTopW() && y < tItemArea.y) { int cur_x = cBorder.getLeftW(); std::vector<CListviewColumn *>::iterator col = tColumns.begin(); int i = 0; for (col++; col != tColumns.end(); col++, i++) { // Reset (*col)->setMouseDown(false); (*col)->setMouseOver(false); // If in the area between two columns, grab if (x >= cur_x-2 && x <= cur_x + 2) { iGrabbed = i; break; } // Click else if (x >= cur_x + 2 && x <= cur_x + (*col)->getWidth() - 2 && iGrabbed <= 0) { SetGameCursor(CURSOR_ARROW); (*col)->setMouseDown(true); (*col)->setMouseOver(true); } cur_x += (*col)->getWidth(); } CContainerWidget::DoMouseDown(x, y, dx, dy, button, modstate); return WID_PROCESSED; } } setSelectedSub(NULL, -1); bool clicked_widget = false; // Go through items and subitems, processing the widgets tMouseOverSubWidget = NULL; int i = 0; int cur_y = tItemArea.y; int count = 0; for(std::list<CListviewItem *>::iterator item = tItems.begin(); // Go through the items item != tItems.end(); item++, i++) { // Ignore invisible items if (!(*item)->isVisible()) continue; // Check if the item is in the display area if (count++ < cScrollbar->getValue()) continue; // Select the item after a click if (y >= cur_y && y < cur_y + (*item)->getHeight()) setSelected(item, i); std::list<CListviewSubitem *>::iterator subitem = (*item)->getSubitems().begin(); std::vector<CListviewColumn *>::iterator col = tColumns.begin(); int j = 0; int cur_x = tItemArea.x; for (; col != tColumns.end() && subitem != (*item)->getSubitems().end(); subitem++, col++, j++) { // Go through the subitems // Don't process hidden subitems if (!(*subitem)->isVisible()) continue; // Process the widget if ((*subitem)->getType() == sub_Widget) { CWidgetSubitem *wsub = (CWidgetSubitem *)(*subitem); CWidget *w = wsub->getWidget(); SDL_Rect r = wsub->getWidgetRect((*item)->getHeight()); r.x += cur_x; r.y += cur_y; if(PointInRect(x, y, r)) { clicked_widget = true; tMouseOverSubWidget = w; // Focus the widget if (w != tFocusedSubWidget) { tFocusedSubWidget->setFocused(false); tFocusedSubWidget = w; w->setFocused(true); } } } // Clicked in the subitem? if (PointInRect(x, y, MakeRect(cur_x, cur_y, (*col)->getWidth(), (*item)->getHeight()))) { setSelectedSub(*subitem, j); } cur_x += (*col)->getWidth(); } cur_y += (*item)->getHeight(); } // Remove the focus from any focused widget if no widget was clicked if (!clicked_widget) if (tFocusedSubWidget) { tFocusedSubWidget->setFocused(false); tFocusedSubWidget = NULL; Repaint(); } CWidget::DoMouseDown(x, y, dx, dy, button, modstate); return WID_PROCESSED; }
/////////////////// // Mouse over event int CListview::DoMouseMove(int x, int y, int dx, int dy, bool down, MouseButton button, const ModifiersState& modstate) { if(cScrollbar->getVisible() && (cScrollbar->getGrabbed() || cScrollbar->InBox(x, y))) { cScrollbar->DoMouseMove(x - cScrollbar->getX(), y - cScrollbar->getY(), dx, dy, down, button, modstate); CContainerWidget::DoMouseMove(x, y, dx, dy, down, button, modstate); return WID_PROCESSED; } // Check that the mouse is inside the listview if (!RelInBox(x, y)) { if (tMouseOverItem) { tMouseOverItem->setActive(false); tMouseOverItem->setDown(false); tMouseOverItem = NULL; } CContainerWidget::DoMouseMove(x, y, dx, dy, down, button, modstate); return WID_PROCESSED; } // Reset the cursor SetGameCursor(CURSOR_ARROW); // Go through the columns and check, if the mouse is in the space between two columns if (bShowColumnHeaders && tColumns.size() > 1) { std::vector<CListviewColumn *>::iterator it = tColumns.begin() + 1; std::vector<CListviewColumn *>::iterator prev = tColumns.begin(); if(y > cBorder.getTopW() && y < tItemArea.y) { int cur_x = cBorder.getLeftW() + tColumns[0]->getWidth(); // Go through the columns for (; it != tColumns.end(); it++, prev++) { if (x >= cur_x && x <= cur_x + 4) { // Between the columns SetGameCursor(CURSOR_RESIZE); break; } else if (x >= cur_x && x < cur_x + (*it)->getWidth() - 2) { // In the column if (!(*it)->isMouseDown()) { (*it)->setMouseDown(down); Repaint(); } break; } cur_x += (*it)->getWidth() - 2; } } else { // Reset the click state of all headers for (; it != tColumns.end(); it++) { if ((*it)->isMouseDown()) { (*it)->setMouseDown(false); Repaint(); } } } // Is any of the columns grabbed? Move it if (iGrabbed > 0 && down) { // Resize the two columns int w1, w2; w1 = w2 = 0; if (iGrabbed > 1) w1 = tColumns[iGrabbed - 1]->getWidth() + dx; w2 = tColumns[iGrabbed]->getWidth() - dx; // Resize only if they both will have at least minimal width if (w1 > MIN_COL_WIDTH && w2 > MIN_COL_WIDTH) { tColumns[iGrabbed - 1]->setWidth(w1); tColumns[iGrabbed]->setWidth(w2); Repaint(); } SetGameCursor(CURSOR_RESIZE); CContainerWidget::DoMouseMove(x, y, dx, dy, down, button, modstate); return WID_PROCESSED; } } // Go through items and subitems, processing the widgets tMouseOverSubWidget = NULL; if (tMouseOverItem) { tMouseOverItem->setActive(false); tMouseOverItem->setDown(false); tMouseOverItem = NULL; Repaint(); } int cur_y = tItemArea.y; int count = 0; for(std::list<CListviewItem *>::iterator item = tItems.begin(); // Go through the items item != tItems.end(); item++) { // Don't process invisible items if (!(*item)->isVisible()) continue; if (count++ < cScrollbar->getValue()) continue; // Reset the style (*item)->setActive(false); (*item)->setDown(false); // Check if the item is under the mouse if (y < cur_y || y >= cur_y + (*item)->getHeight()) { cur_y += (*item)->getHeight(); if (cur_y >= tItemArea.y + tItemArea.h) break; continue; } // Change the style (*item)->setActive(true); (*item)->setDown(down); tMouseOverItem = (*item); std::list<CListviewSubitem *>::iterator subitem = (*item)->getSubitems().begin(); std::vector<CListviewColumn *>::iterator col = tColumns.begin(); int cur_x = tItemArea.x; for (; col != tColumns.end() && subitem != (*item)->getSubitems().end(); subitem++, col++) { // Go through the subitems // Don't process invisible subitems if (!(*subitem)->isVisible()) continue; // Process the widget if ((*subitem)->getType() == sub_Widget) { CWidgetSubitem *wsub = (CWidgetSubitem *)(*subitem); CWidget *w = wsub->getWidget(); SDL_Rect r = wsub->getWidgetRect((*item)->getHeight()); r.x += cur_x; r.y += cur_y; if(PointInRect(x, y, r)) { tMouseOverSubWidget = w; // If in the previous frame the widget wasn't under the mouse and now it is, fire the "enter" event if (!w->InBox(x - dy, y - dy)) w->DoMouseEnter(x, y, dx, dy, modstate); // Mouse move event if (w->DoMouseMove(x, y, dx, dy, down, button, modstate) == WID_PROCESSED) { CContainerWidget::DoMouseMove(x, y, dx, dy, down, button, modstate); return WID_PROCESSED; } } else { // If in the previous frame the widget was under the mouse and now it is not, fire the "leave" event if (PointInRect(x - dy, y - dy, r)) w->DoMouseLeave(x, y, dx, dy, modstate); } } cur_x += (*col)->getWidth(); } Repaint(); cur_y += (*item)->getHeight(); if (cur_y >= tItemArea.y + tItemArea.h) break; } CContainerWidget::DoMouseMove(x, y, dx, dy, down, button, modstate); return WID_PROCESSED; }