Handle the Open Dialog to save/load

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
tBane
Posts: 33
Joined: Wed Feb 21, 2024 2:25 pm
Location: Poland

Handle the Open Dialog to save/load

Post by tBane »

Hi. I tried to handle the open dialog to save/load.
I wrote this code. Where did I go wrong?

Code: Select all

switch (event.GUIEvent.EventType)
{
case EGET_MENU_ITEM_SELECTED:		// main menu events
{
	IGUIContextMenu* menu = (IGUIContextMenu*)event.GUIEvent.Caller;
	s32 id = menu->getItemCommandId(menu->getSelectedItem());

	if (id == GUI_CREATEWORLD)
		createWorld();

	if (id == GUI_LOADWORLD)
	{
		openDialog = GUI->addFileOpenDialog(L"Please select a map file to load");
		hexMap->loadFromFile( stringc( openDialog->getFileName()).c_str());
		cout << "load world\n";
	}

	if (id == GUI_SAVEWORLD)
	{
		openDialog = GUI->addFileOpenDialog(L"Please select a map file to save");
		hexMap->saveAsFile( stringc( openDialog->getFileName()).c_str());
		cout << "save world\n";
	}
		
	if (id == GUI_GENERATEWORLD)
		generateWorld();

	if (id == GUI_TUTORIAL)
		tutorial();

	if (id == GUI_INFO)
		info();

}
break;
}
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Handle the Open Dialog to save/load

Post by CuteAlien »

Dialog runs a while so you can't just start and immediately get result. User has to click something first. So you start it - then react later to event to do next thing. Event is usually gui event EGET_FILE_SELECTED.

It's used in a few examples like 22.MaterialViewer or 09.Meshviewer so check those if you need some code.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
tBane
Posts: 33
Joined: Wed Feb 21, 2024 2:25 pm
Location: Poland

Re: Handle the Open Dialog to save/load

Post by tBane »

ok, I used this event. Now I cant manually write filename in dialog box.

Code: Select all

case EGET_FILE_SELECTED:
{
	
	if (openDialogState == openDialogStates::load)
	{
		
		IGUIFileOpenDialog* dialog =
			(IGUIFileOpenDialog*)event.GUIEvent.Caller;
		
		io::path extension;
		core::getFileNameExtension(extension, dialog->getFileName());
		extension.make_lower();

		if (extension == ".hexmap")
		{
			hexMap->destroy();
			hexMap->loadFromFile(stringc(dialog->getFileName()).c_str());
		}
	}

	if (openDialogState == openDialogStates::save)
	{
		IGUIFileOpenDialog* dialog =
			(IGUIFileOpenDialog*)event.GUIEvent.Caller;

		std::string filename = stringc(dialog->getFileName()).c_str();
		io::path extension;
		core::getFileNameExtension(extension, dialog->getFileName());
		extension.make_lower();

		if (extension != ".hexmap")
			filename = filename + ".hexmap";
	
		hexMap->saveAsFile(filename);
	}

	openDialogState = openDialogStates::none;

}
break;
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Handle the Open Dialog to save/load

Post by CuteAlien »

Why not? If keys don't do anything you probably catch the key-events. Like returning true by default in event-handler (which means event handled so it's no longer passed on to Irrlicht).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
tBane
Posts: 33
Joined: Wed Feb 21, 2024 2:25 pm
Location: Poland

Re: Handle the Open Dialog to save/load

Post by tBane »

Correctly:

Code: Select all

case EGET_MENU_ITEM_SELECTED:		// main menu events
{
	IGUIContextMenu* menu = (IGUIContextMenu*)event.GUIEvent.Caller;
	s32 id = menu->getItemCommandId(menu->getSelectedItem());
	switch (id)
	{
		case GUI_CREATEWORLD:
			if (hexMap != nullptr)
				hexMap->destroy();
			hexMap->createEmpty(40, 40);
			std::cout << "created new Empty world" << std::endl;
			break;

		case GUI_LOADWORLD:
			openDialog = GUI->addFileOpenDialog(L"Please select a map file to open");
			openDialogState = openDialogStates::load;
			break;

		case GUI_SAVEWORLD:
			openDialog = GUI->addFileOpenDialog(L"Please select a map file to save");
			openDialogState = openDialogStates::save;
		break;

		case GUI_GENERATEWORLD:
			generateWorld();
			break;

		case GUI_TUTORIAL:
			tutorial();
			break;

		case GUI_INFO:
			info();
			break;

	}	// switch(id)
	
}
Post Reply