Пример #1
0
	virtual bool OnEvent(const SEvent &event)
	{
		if (event.EventType == EET_GUI_EVENT)
		{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* env = Device->getGUIEnvironment();

			switch(event.GUIEvent.EventType)
			{
			case EGET_SCROLL_BAR_CHANGED:
				if (id == MYGUI_CURRENTIMAGE)
				{
					IGUIImage* img = (IGUIImage*)env->getRootGUIElement()->getElementFromId(MYGUI_IMAGE,true);
					s32 i = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
					img->setImage(FontTool->currentTextures[i]);

					return true;
				}
				break;
			case EGET_COMBO_BOX_CHANGED:
				if (id == MYGUI_CHARSET)
				{
					IGUIComboBox* cbo = (IGUIComboBox*)event.GUIEvent.Caller;
					IGUIComboBox* cbo_1 = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FONTNAME,true);
					core::stringw str=FontTool->FontNames[cbo_1->getSelected()];
					u32 j=0;
					FontTool->selectCharSet(cbo->getSelected());
					// rebuild font list
					cbo = cbo_1;
					cbo->clear();
					for (u32 i=0; i < FontTool->FontNames.size(); ++i){
						cbo->addItem(FontTool->FontNames[i].c_str());
						if(FontTool->FontNames[i]==str) j=i;
					}
					cbo->setSelected(j);
					return true;
				}
				else if(id==MYGUI_FONTNAME){
					IGUIComboBox* cbo = (IGUIComboBox*)event.GUIEvent.Caller;
					std::cout << FontTool->FontNames[cbo->getSelected()].c_str() << std::endl;
				}
				break;
			case EGET_CHECKBOX_CHANGED:
				if (id == MYGUI_VECTOR)
				{
					IGUICheckBox* chk = (IGUICheckBox*)event.GUIEvent.Caller;

					IGUIComboBox *cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FORMAT,true);
					cbo->clear();

					if (chk->isChecked() && VecTool)
					{
						// vector formats
						for (s32 i=0; fileformats[i] != 0; ++i)
							cbo->addItem( core::stringw(vectorfileformats[i]).c_str());

					}
					else
					{

						// bitmap formats
						if (!FontTool->UseAlphaChannel)
						{
							// add non-alpha formats
							for (s32 i=0; fileformats[i] != 0; ++i)
								cbo->addItem( core::stringw(fileformats[i]).c_str());
						}
						// add formats which support alpha
						for (s32 i=0; alphafileformats[i] != 0; ++i)
							cbo->addItem( core::stringw(alphafileformats[i]).c_str());
					}

				}
				break;

			case EGET_BUTTON_CLICKED:

				if (id == MYGUI_CREATE)
				{
					// create the font with the params
					IGUIComboBox* cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_CHARSET, true);
					int charset = cbo->getSelected();

					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FONTNAME,true);
					int fontname = cbo->getSelected();

					/*
					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_SIZE,true);
					int fontsize = cbo->getSelected();
					*/

					int fontsize=wcstol(((IGUIEditBox*)env->getRootGUIElement()->getElementFromId(MYGUI_SIZE,true))->getText(),NULL,10);

					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_TEXWIDTH,true);
					int texwidth = cbo->getSelected();

					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_TEXHEIGHT,true);
					int texheight = cbo->getSelected();

					IGUICheckBox* chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_BOLD,true);
					bool bold = chk->isChecked();
					chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_ITALIC,true);
					bool italic = chk->isChecked();

					chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_ALPHA,true);
					bool alpha = chk->isChecked();

					chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_ANTIALIAS,true);
					bool aa = chk->isChecked();

					chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(201,true);
					bool usedOnly = chk->isChecked();

					chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(202,true);
					bool excludeLatin = chk->isChecked();

					// vector fonts disabled
					//chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_VECTOR,true);
					bool vec = false;//chk->isChecked();

					FontTool->makeBitmapFont(fontname, charset, /*FontTool->FontSizes[fontsize]*/ fontsize, texturesizes[texwidth], texturesizes[texheight], bold, italic, aa, alpha, usedOnly, excludeLatin);

					IGUIScrollBar* scrl = (IGUIScrollBar*)env->getRootGUIElement()->getElementFromId(MYGUI_CURRENTIMAGE,true);
					scrl->setMax(FontTool->currentTextures.size() == 0 ? 0 : FontTool->currentTextures.size()-1);

					if (FontTool->currentTextures.size() > 0)
					{
						IGUIImage* img = (IGUIImage*)env->getRootGUIElement()->getElementFromId(MYGUI_IMAGE,true);
						img->setImage(FontTool->currentTextures[0]);
						scrl->setPos(0);
					}

					// make sure users pick a file format that supports alpha channel
					cbo = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FORMAT,true);
					cbo->clear();

					if (vec)
					{
						// add vector formats
						for (s32 i=0; fileformats[i] != 0; ++i)
							cbo->addItem( core::stringw(vectorfileformats[i]).c_str());
					}
					else
					{
						if (!alpha)
						{
							// add non-alpha formats
							for (s32 i=0; fileformats[i] != 0; ++i)
								cbo->addItem( core::stringw(fileformats[i]).c_str());
						}
						// add formats which support alpha
						for (s32 i=0; alphafileformats[i] != 0; ++i)
							cbo->addItem( core::stringw(alphafileformats[i]).c_str());
					}
					if (VecTool)
					{
						delete VecTool;
						VecTool = 0;
					}
					if (vec)
					{
						VecTool = new CVectorFontTool(FontTool);
					}

					/* Message box letting the user know the process is complete */
					env->addMessageBox(L"Create", completeText);

					return true;
				}

				if (id == MYGUI_SAVE)
				{
					IGUIEditBox *edt  = (IGUIEditBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FILENAME,true);
					core::stringc name = edt->getText();

					IGUIComboBox *fmt  = (IGUIComboBox*)env->getRootGUIElement()->getElementFromId(MYGUI_FORMAT,true);
					core::stringc format = fmt->getItem(fmt->getSelected());

					// vector fonts disabled
					// IGUICheckBox *chk = (IGUICheckBox*)env->getRootGUIElement()->getElementFromId(MYGUI_VECTOR,true);
					// bool vec = chk->isChecked();
					bool vec = false;

					if (vec && VecTool)
						VecTool->saveVectorFont(name.c_str(), format.c_str());
					else
						FontTool->saveBitmapFont(name.c_str(), format.c_str());

					return true;
				}

				if (id == MYGUI_HELPBUTTON)
				{
					env->addMessageBox(L"Irrlicht Unicode Font Tool", helptext);
					return true;
				}

				break;

			default:
				break;
			}
		}

		return false;
	}
bool MainMenuEventReceiver::OnEvent(const SEvent& event)
{
	if (event.EventType == EET_GUI_EVENT)
	{
		s32 id = event.GUIEvent.Caller->getID();
		IGUIEnvironment* env = this->contextGame->guiEnv;
		MainMenuScene* mainmenu = ((MainMenuScene*)this->contextGame->sceneManager->getLastScene());
		char* ipadress;
		wchar_t* inputwchar;

		char* playername;
		wchar_t* namewchar;

		stringc portInput;
		short port;

		Player* newplayer;
		NetworkPacket packet(START_GAME);
		NetworkPacket namepacket(CLIENT_JOIN);
		NetworkPacket quitpacket(CLIENT_QUIT);
		NetworkPacket hostquitpacket(HOST_DISCONNECT);

		switch(event.GUIEvent.EventType)
		{ 
		case EGET_BUTTON_CLICKED:
			switch(id)
			{
			case 1: // Join
				inputwchar = (wchar_t*)mainmenu->Ipadresinput->getText();
				ipadress = (char*)malloc(wcslen(inputwchar)+ 1);
				wcstombs(ipadress, inputwchar, wcslen(inputwchar));
				ipadress[wcslen(inputwchar)] = 0;

				portInput = mainmenu->hostPortInput->getText();
				port = atof(portInput.c_str());

				namewchar = (wchar_t*)mainmenu->Nameinput->getText();
				playername = (char*)malloc(wcslen(namewchar)+ 1);
				wcstombs(playername, namewchar, wcslen(namewchar));
				playername[wcslen(namewchar)] = 0;

				if((*ipadress == ' ' || *ipadress == NULL) || (*playername == ' ' || *playername == NULL) || (port == 0)){
					if(*ipadress == ' ' || *ipadress == NULL || port == 0){
						mainmenu->messagebox =  env->addMessageBox(L"Messsage",L"Fill in an Ipadress and port",true,1,mainmenu->mainMenuWindow);
						mainmenu->messagebox->setDraggable(false);
					}else{
						mainmenu->messagebox = this->contextGame->guiEnv->addMessageBox(L"Message",L"Fill in an Name",true,1,mainmenu->mainMenuWindow);
						mainmenu->messagebox->setDraggable(false);
					}
				}else{
					Network::GetInstance()->InitializeClient(ipadress, port);
					if(!Network::GetInstance()->IsConnected()){
						mainmenu->messagebox =  env->addMessageBox(L"Messsage",L"Not able to connect to server",true,1,mainmenu->mainMenuWindow);
						mainmenu->messagebox->setDraggable(false);
					}else{
						//TODO: package met naam en checksum: Network->getinstance->GetPacketTypeChecksum
						namepacket << namewchar << Network::GetInstance()->GetPacketTypeChecksum();
						Network::GetInstance()->SendPacket(namepacket, true);
						mainmenu->createServerWindow_Button->setVisible(false);
						mainmenu->joinServerWindow_Button->setVisible(false);
						mainmenu->Clientlist->setVisible(true);
						mainmenu->Ipadresinput->setVisible(false);
						mainmenu->Namelabel->setVisible(false);
						// TODO check merge Both??
						mainmenu->waitinglabel = env->addStaticText(L"Waiting for host to start the game",rect<s32>(position2di(300,165),dimension2di(200,25)),false,true,mainmenu->mainMenuWindow);
						mainmenu->Nameinput->setVisible(false);
						mainmenu->quit_button->setVisible(true);
						mainmenu->waitinglabel->setVisible(true);

					}



				}


				return true;
			case 2: // Create
				namewchar = (wchar_t*)mainmenu->Nameinput->getText();
				playername = (char*)malloc(wcslen(namewchar)+ 1);
				wcstombs(playername, namewchar, wcslen(namewchar));
				playername[wcslen(namewchar)] = 0;

				if(*playername == ' ' || *playername == NULL){
					mainmenu->messagebox = this->contextGame->guiEnv->addMessageBox(L"Message",L"Fill in a name",true,1,mainmenu->mainMenuWindow);
					mainmenu->messagebox->setDraggable(false);
					return false;
				}else{
					Network::GetInstance()->InitializeServer();
					mainmenu->createServerWindow_Button->setVisible(false);
					mainmenu->joinServerWindow_Button->setVisible(false);
					mainmenu->Ipadresinput->setVisible(false);
					mainmenu->Namelabel->setVisible(false);
					mainmenu->Nameinput->setVisible(false);
					mainmenu->start_button->setVisible(true);
					mainmenu->startStatic_button->setVisible(true);
					mainmenu->quit_button->setVisible(true);
					mainmenu->Clientlist->setVisible(true);
					newplayer = new Player();
					newplayer->Name = namewchar;
					newplayer->Team = 1;
					mainmenu->playerlist.push_back(newplayer);
					return true;
				}
			case 3: // Start
				mainmenu->StartGame();
				Network::GetInstance()->SendServerPacket(packet, true);
				return true;
			case 4: // Start Test Map
				mainmenu->StartTestGame();
				Network::GetInstance()->SendServerPacket(packet, true);
				return true;
			case 5: // Quit
				if(!Network::GetInstance()->IsServer())
				{
					Network::GetInstance()->SendPacket(quitpacket, true);
				}
				else
				{
					hostquitpacket << L"The host got disconnected";
					Network::GetInstance()->SendServerPacket(hostquitpacket, true);

				}
				mainmenu->playerlist.clear();
				Network::GetInstance()->DeInitialize();
				mainmenu->BackToMainMenu();
				return true;
			default:
				return false;
			}

			break;
		}
	}

	return false;
}