TEST_FIXTURE(SDL_fixture, FlatWidgets)
{
	UNITTEST_TIME_CONSTRAINT(50);

	CHECK(SDL_init_ok);
	if(SDL_init_ok)
	{
		Event e;
		e.event_type = EventType::KeyLeft;
		Widget* left = new Widget();
		Widget* right = new Widget();
		Widget* left_bottom = new Widget();
		Widget* right_bottom = new Widget();
		left->LinkRight(right);
		left->LinkDown(left_bottom);
		right->LinkLeft(left);
		right->LinkDown(right_bottom);
		left_bottom->LinkUp(left);
		left_bottom->LinkRight(right_bottom);
		right_bottom->LinkUp(right);
		right_bottom->LinkLeft(left_bottom);


		boost::signals::scoped_connection c = left->OnGainFocus.connect(widgetevent_callback);
		left->SetFocus();
		CHECK_EQUAL(1, widgetevent_callback_count);

		Widget::GetWidgetWithFocus()->HandleEvent(e);
		CHECK_EQUAL(true, left->HasFocus());
		CHECK_EQUAL(false, right->HasFocus());

		e.event_type = EventType::KeyRight;
		Widget::GetWidgetWithFocus()->HandleEvent(e);
		CHECK_EQUAL(false, left->HasFocus());
		CHECK_EQUAL(true, right->HasFocus());

		Widget::GetWidgetWithFocus()->HandleEvent(e);
		CHECK_EQUAL(false, left->HasFocus());
		CHECK_EQUAL(true, right->HasFocus());
		
		e.event_type = EventType::KeyDown;
		Widget::GetWidgetWithFocus()->HandleEvent(e);
		CHECK_EQUAL(true, right_bottom->HasFocus());

		e.event_type = EventType::KeyLeft;
		Widget::GetWidgetWithFocus()->HandleEvent(e);
		CHECK_EQUAL(true, left_bottom->HasFocus());

		e.event_type = EventType::KeyUp;
		Widget::GetWidgetWithFocus()->HandleEvent(e);
		CHECK_EQUAL(true, left->HasFocus());
		Widget::ClearRoot();
	}
}
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));

}
示例#3
0
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;
}
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();
	}
}