void ModeMenu::Setup() { const int btn_width = 160; const int btn_height = 40; const int btn_border = 10; Widget* newGame = new Widget(NinePatch("NavyButton.png", 16, 16, 16, 16), btn_width, btn_height); newGame->SetPosition(Vector2i(Widget::GetScreenCentre().x - newGame->GetSize().x / 2, btn_border)); newGame->SetText("New game", TextAlignment::Centre); Widget* scrollRight = new Widget(NinePatch("NavyButton.png", 16, 16, 16, 16), btn_height, btn_height); scrollRight->SetPosition(newGame->GetPosition() + Vector2i(newGame->GetSize().x + btn_border, 0)); scrollRight->SetText(">", TextAlignment::Centre); Widget* scrollLeft = new Widget(NinePatch("NavyButton.png", 16, 16, 16, 16), btn_height, btn_height); scrollLeft->SetPosition(newGame->GetPosition() - Vector2i(scrollLeft->GetSize().x + btn_border, 0)); scrollLeft->SetText("<", TextAlignment::Centre); Widget* explainGame = new Widget(NinePatch("NavyButton.png", 16, 16, 16, 16), btn_width * 2 + btn_border, btn_height * 2); explainGame->SetPosition(Vector2i(btn_border, Widget::GetScreenSize().y - btn_height * 2 - btn_border)); explainGame->SetTextSize(TextSize::Small); explainGame->SetTextWrap(true); explainGame->SetText("You cannot move the paddle, but you can move the wall. Keep the bricks alive by dodging the balls. You score 10 points for each brick left on every bounce", TextAlignment::Centre); explainGame->SetRejectsFocus(true); explainGame->SetHidesHighlight(true); Widget* exitGame = new Widget(NinePatch("NavyButton.png", 16, 16, 16, 16), btn_width, btn_height); exitGame->SetPosition(Vector2i(btn_border * 3 + btn_width * 2, Widget::GetScreenSize().y + - btn_height - btn_border)); exitGame->SetText("Exit", TextAlignment::Centre); Widget* betaTag = new Widget("Beta.png"); betaTag->SetPosition(Widget::GetScreenSize() - betaTag->GetSize()); mFeedbackWidget = new FeedbackWidget(); scrollLeft->LinkDown(exitGame); scrollLeft->LinkRight(newGame); newGame->LinkLeft(scrollLeft); newGame->LinkDown(exitGame); newGame->LinkRight(scrollRight); scrollRight->LinkLeft(newGame); scrollRight->LinkDown(exitGame); exitGame->LinkUp(newGame); //Attach callback exitGame->OnClick.connect(boost::bind(&ModeMenu::clickExit, this, _1)); newGame->OnClick.connect(boost::bind(&ModeMenu::clickNewGame, this, _1)); betaTag->OnClick.connect(boost::bind(&ModeMenu::clickBetaTag, this, _1)); scrollRight->OnClick.connect(boost::bind(&ModeMenu::clickNextLevel, this, _1)); scrollLeft->OnClick.connect(boost::bind(&ModeMenu::clickPrevLevel, this, _1)); }
TEST_FIXTURE(SDL_fixture, WidgetDefaults) { CHECK(SDL_init_ok); if(SDL_init_ok) { Widget* w = new Widget(); CHECK_EQUAL(Vector2i(0,0), w->GetPosition()); CHECK_EQUAL(Vector2i(128,48), w->GetSize()); Widget::ClearRoot(); } }
TEST_FIXTURE(SDL_fixture, WidgetSetters) { CHECK(SDL_init_ok); if(SDL_init_ok) { Widget* w = new Widget(); w->SetSize(Vector2i(50,60)); w->SetPosition(Vector2i(5,5)); CHECK_EQUAL(Vector2i(50, 60), w->GetSize()); CHECK_EQUAL(Vector2i(5, 5), w->GetPosition()); Widget::ClearRoot(); } }
/** * @brief * Center widget */ void Widget::Center(ECenter nCenter) { // Get parent widget Widget *pParent = GetParent(); // Center on parent if (nCenter == CenterParent && pParent) { // Center relative coordinates Vector2i vSize = GetSize(); Vector2i vParentSize = pParent->GetSize(); SetPos((vParentSize - vSize) / 2); } // Center on screen else if (nCenter == CenterScreen || !pParent) { // Center relative to the screen // [TODO] Better center to *current* screen? Something like CenterOnScreen(this->GetScreen()) CenterOnScreen(*GetGui()->GetDefaultScreen()); } }
/** * @brief * Center widget relative to another widget */ void Widget::CenterOnWidget(Widget &cWidget) { // Both widgets must belong to the same GUI if (GetGui() == cWidget.GetGui()) { // Get sizes Vector2i vSize = GetSize(); Vector2i vOtherSize = cWidget.GetSize(); // Get relative position Vector2i vRelative = GetRelativePos(cWidget); // Get desired relative position Vector2i vDesired; vDesired.x = (vOtherSize.width - vSize.width) / 2; vDesired.y = (vOtherSize.height - vSize.height) / 2; // Set new position Vector2i vPos = GetPos() + vDesired - vRelative; SetPos(vPos); } }
void GridDecorator::OnChange(Widget::Property property) { if (property == Widget::SIZE) { mIcons[Align::TOP_CENTER].SetWidth(GetWidth() - (mIcons[Align::TOP_LEFT].GetWidth() + mIcons[Align::TOP_RIGHT].GetWidth())); mIcons[Align::CENTER].SetWidth(GetWidth() - (mIcons[Align::LEFT].GetWidth() + mIcons[Align::RIGHT].GetWidth())); mIcons[Align::BOTTOM_CENTER].SetWidth(GetWidth() - (mIcons[Align::BOTTOM_LEFT].GetWidth() + mIcons[Align::BOTTOM_RIGHT].GetWidth())); mIcons[Align::LEFT].SetHeight(GetHeight() - (mIcons[Align::TOP_LEFT].GetHeight() + mIcons[Align::BOTTOM_LEFT].GetHeight())); mIcons[Align::CENTER].SetHeight(GetHeight() - (mIcons[Align::TOP_CENTER].GetHeight() + mIcons[Align::BOTTOM_CENTER].GetHeight())); mIcons[Align::RIGHT].SetHeight(GetHeight() - (mIcons[Align::TOP_RIGHT].GetHeight() + mIcons[Align::BOTTOM_RIGHT].GetHeight())); } else if (property == Widget::PARENT) { Widget* parent = GetParent(); if (parent) { SetSize(parent->GetSize()); ChangeZIndex(Widget::ALL_BELOW); } } Icon::OnChange(property); }
/** * @brief * Open submenu */ void AbstractMenu::OpenSubmenu(MenuItem *pItem, int nItem) { // Check if new item is valid if (pItem && pItem->GetType() == TypeMenu && nItem >= 0 && nItem < m_nItems) { // Set new item m_pSelectedItem = pItem; // Create popup menu and get size m_pPopupMenu = new PopupMenu(GetGui()->GetRootWidget()); m_pPopupMenu->SetMenu(static_cast<Menu*>(m_pSelectedItem)); m_pPopupMenu->SetFocusStyle(NoFocus); m_pPopupMenu->m_pParentMenu = this; m_pPopupMenu->m_pController = m_pController; m_pPopupMenu->m_bAllowButtonUp = true; Vector2i vSize = m_pPopupMenu->GetPopupSize(); // Determine popup position Vector2i vPos; for (int i=0; i<2; i++) { // Set start position vPos = GetAbsPos(); // Left if (m_nOrientation == Vertical && m_nOpenSideHorizontal == SideLeft) { vPos.x += m_pItems[nItem].vPos1.x - vSize.x; if (m_nOpenSideVertical == SideTop) vPos.y += m_pItems[nItem].vPos2.y - vSize.y; else vPos.y += m_pItems[nItem].vPos1.y; } // Right else if (m_nOrientation == Vertical && m_nOpenSideHorizontal == SideRight) { vPos.x += m_pItems[nItem].vPos2.x; if (m_nOpenSideVertical == SideTop) vPos.y += m_pItems[nItem].vPos2.y - vSize.y; else vPos.y += m_pItems[nItem].vPos1.y; } // Top else if (m_nOrientation == Horizontal && m_nOpenSideVertical == SideTop) { vPos.x += m_pItems[nItem].vPos1.x; vPos.y += m_pItems[nItem].vPos1.y - vSize.y; } // Bottom else if (m_nOrientation == Horizontal && m_nOpenSideVertical == SideBottom) { vPos.x += m_pItems[nItem].vPos1.x; vPos.y += m_pItems[nItem].vPos2.y; } // Check if position is valid bool bChanged = false; if (i == 0) { // Get screen size Widget *pParent = m_pPopupMenu->GetParent(); Vector2i vScreenSize = pParent ? pParent->GetSize() : m_pPopupMenu->GetScreen()->GetSize(); // Switch between left and right if ((m_nOpenSideHorizontal == SideLeft && vPos.x < 0) || (m_nOpenSideHorizontal == SideRight && vPos.x + vSize.x >= vScreenSize.x)) { m_nOpenSideHorizontal = (m_nOpenSideHorizontal == SideLeft ? SideRight : SideLeft); bChanged = true; } // Switch between top and bottom if ((m_nOpenSideVertical == SideTop && vPos.y < 0) || (m_nOpenSideVertical == SideBottom && vPos.y + vSize.y >= vScreenSize.y)) { m_nOpenSideVertical = (m_nOpenSideVertical == SideTop ? SideBottom : SideTop); bChanged = true; } } // Has the position changed? if (!bChanged) break; } // Set popup position and inherit open-side m_pPopupMenu->SetVisible(true); if (!m_pPopupMenu) return; // If something is implemented poorly, the focus may got away causing m_pPopupMenu to be a null pointer m_pPopupMenu->ShowPopupMenu(vPos); if (!m_pPopupMenu) return; // If something is implemented poorly, the focus may got away causing m_pPopupMenu to be a null pointer m_pPopupMenu->SetOpenSide(Horizontal, m_nOpenSideHorizontal); m_pPopupMenu->SetOpenSide(Vertical, m_nOpenSideVertical); // Emit signal SignalSubmenuOpened(m_pSelectedItem); // Inform controller m_pController->SetControlledMenu(m_pPopupMenu); } }