Widget* MenuWidget::AddMenuItem(std::string _item) { Widget* pMenuItem = new Widget(); pMenuItem->SetSize(Vector2i(128, 48)); pMenuItem->SetPosition(Vector2i(static_cast<int>(children_.size()) * 8, static_cast<int>(children_.size()) * 40)); pMenuItem->SetText(_item, TextAlignment::Centre); SetSize(Vector2i(128 + static_cast<int>(children_.size()) * 8, 48 + static_cast<int>(children_.size()) * 40)); //Link up if(children_.size() == 1) { children_.at(0)->LinkUp(pMenuItem); children_.at(0)->LinkDown(pMenuItem); } if(children_.size() > 0) { pMenuItem->LinkUp(children_.at(children_.size() - 1)); pMenuItem->LinkDown(children_.at(0)); } //Remove old link if(children_.size() > 1) { children_.at(children_.size() - 1)->LinkDown(pMenuItem); children_.at(0)->LinkUp(pMenuItem); } AddChild(pMenuItem); return pMenuItem; }
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, HierarchalWidgets) { /* Layout |------------------------------| |--------| |A | |E | | |-----------| |-------| | | | | |B | |C | | | | | | | |-------| | | | | | | | | | | | | | | | | | | |-------| | | | | | | |D | | | | | |-----------| |-------| | | | | | | | |------------------------------| |--------| |------------------------------------------| |F | |------------------------------------------| */ UNITTEST_TIME_CONSTRAINT(50); CHECK(SDL_init_ok); if(SDL_init_ok) { Widget* A = new Widget(); Widget* B = new Widget(); Widget* C = new Widget(); Widget* D = new Widget(); Widget* E = new Widget(); Widget* F = new Widget(); A->AddChild(B); A->AddChild(C); A->AddChild(D); A->LinkInnerRight(B); A->LinkInnerLeft(C); A->LinkInnerDown(B); A->LinkInnerUp(B); A->LinkRight(E); E->LinkLeft(A); B->LinkRight(C); C->LinkLeft(B); D->LinkLeft(B); A->LinkDown(F); E->LinkDown(F); F->LinkUp(A); A->SetFocus(); CHECK_EQUAL(true, A->HasFocus()); Widget::GetWidgetWithFocus()->HandleEvent(Event(EventType::KeyDown)); CHECK_EQUAL(true, B->HasFocus()); Widget::GetWidgetWithFocus()->HandleEvent(Event(EventType::KeyDown)); CHECK_EQUAL(true, F->HasFocus()); Widget::GetWidgetWithFocus()->HandleEvent(Event(EventType::KeyUp)); CHECK_EQUAL(true, A->HasFocus()); Widget::GetWidgetWithFocus()->HandleEvent(Event(EventType::KeyUp)); CHECK_EQUAL(true, B->HasFocus()); Widget::GetWidgetWithFocus()->HandleEvent(Event(EventType::KeyUp)); CHECK_EQUAL(true, A->HasFocus()); Widget::GetWidgetWithFocus()->HandleEvent(Event(EventType::KeyRight)); CHECK_EQUAL(true, B->HasFocus()); Widget::GetWidgetWithFocus()->HandleEvent(Event(EventType::KeyRight)); CHECK_EQUAL(true, C->HasFocus()); Widget::GetWidgetWithFocus()->HandleEvent(Event(EventType::KeyRight)); CHECK_EQUAL(true, E->HasFocus()); Widget::ClearRoot(); } }