Allow leaving game per keyboard shortcuts

Bug #1774596 reported by GunChleoc
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
widelands
Won't Fix
Medium
Unassigned

Bug Description

It should be possible to leave a game via keyboard shortcuts

ESC: Bring up the main menu
Enter: Bring up the "are you sure" dialog with Enter/Esc keyboard handling
CRTL+Enter: Leave the game immediately

Related branches

Revision history for this message
Antonino Siena (thenifker13-deactivatedaccount) wrote :

I would be interested in this one too - and can write some, if not even all code for this one.

As addition I would say ESC should close currently opened windows first and then bring up the Main Menu when there are no windows remaining.

information type: Public → Public Security
Revision history for this message
Toni Förster (stonerl) wrote :

Yes, it would indeed be nice to close open windows with ESC.

information type: Public Security → Public
Revision history for this message
kaputtnik (franku) wrote :

Hi Antonio :-)

thanks for your offer. Just start coding and in case of any questions don't worry to ask.

If you are new to launchpad and bazaar, please visit this page: https://wl.widelands.org/wiki/bzrprimer/

Revision history for this message
Antonino Siena (thenifker13-deactivatedaccount) wrote :

Im currently looking for a way to write it so that its implemented for all windows except for some which need to be handled differently.

Revision history for this message
GunChleoc (gunchleoc) wrote :

That would be in the ui_basic directory, in the window.cc class.

Override Panel::handle_key(bool down, SDL_Keysym code) to implement the functionality, then look into the class hierarchy to make sure that descendents call it too where applicable. Some classes might need to do something extra before closing the window.

Thanks for taking this on :)

Revision history for this message
Antonino Siena (thenifker13-deactivatedaccount) wrote :

I added the handle_key() to window.cc and want to add it to the existing handle_key() from panel.cc.

What is the object called which is of type Panel in panel.cc so that I can call the handle_key() function from window.cc?

Revision history for this message
Antonino Siena (thenifker13-deactivatedaccount) wrote :

So I want to check if the current Panel is a Window - or is there a different approach that I should use?

Revision history for this message
GunChleoc (gunchleoc) wrote :

You do not need to call handle_key yourself, the main loop takes care of that.

So, what you do is that you add a new function to the UI::Window class and define it in the .h file as

bool handle_key(bool down, SDL_Keysym code) override;

Then, in the .cc file, you have:

bool Window::handle_key(bool down, SDL_Keysym code) {
 // Do your stuff and return true if the key has been handled

 return Panel::handle_key(down, code);
}

You then need to search the code for other classes that implement this function and that inherit from the Window class. Make them call Window::handle_key(down, code); in turn. For example, change

bool GameMainMenuSaveGame::handle_key(bool down, SDL_Keysym code) {
 if (down) {
  switch (code.sym) {
  case SDLK_KP_ENTER:
  case SDLK_RETURN:
   ok();
   return true;
  case SDLK_ESCAPE:
   die();
   return true;
  case SDLK_DELETE:
   load_or_save_.clicked_delete();
   return true;
  default:
   break; // not handled
  }
 }
 return UI::Panel::handle_key(down, code);
}

to

bool GameMainMenuSaveGame::handle_key(bool down, SDL_Keysym code) {
 if (down) {
  switch (code.sym) {
  case SDLK_KP_ENTER:
  case SDLK_RETURN:
   ok();
   return true;
  case SDLK_ESCAPE:
   die();
   return true;
  case SDLK_DELETE:
   load_or_save_.clicked_delete();
   return true;
  default:
   break; // not handled
  }
 }
 return UI::Window::handle_key(down, code);
}

Revision history for this message
Antonino Siena (thenifker13-deactivatedaccount) wrote :

When I add a handle_key() to a .cc and .h in wui/ it doesn't seem to get called - do I need to ensure it somehow?

Another thing what does the boolean return value for the handlers indicate?

Revision history for this message
GunChleoc (gunchleoc) wrote :

It does get called for all classes that inherit from UI::Panel.

We have a specialized log() function that you can use to print to console to check whether the function has been called.

The function needs to return true if the current key has been handled and false if it has not handled the current key. true means that the parent panel will not attempt to handle it.

Revision history for this message
Antonino Siena (thenifker13-deactivatedaccount) wrote :

Got the code ready for review now!

Revision history for this message
Toni Förster (stonerl) wrote :

Please open a merge request.

Revision history for this message
Antonino Siena (thenifker13-deactivatedaccount) wrote :

Created the branch(if I did it properly).

Revision history for this message
GunChleoc (gunchleoc) wrote :

Yes you did. I have attached an image to show you how to make a merge request too. It is better if you do this yourself :)

Revision history for this message
GunChleoc (gunchleoc) wrote :
Changed in widelands:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.