Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2002:
[Freeciv-Dev] (PR#2608) replace key_move_xxx with key_unit_move
Home

[Freeciv-Dev] (PR#2608) replace key_move_xxx with key_unit_move

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#2608) replace key_move_xxx with key_unit_move
From: "Jason Short via RT" <rt@xxxxxxxxxxxxxx>
Date: Wed, 18 Dec 2002 23:38:39 -0800
Reply-to: rt@xxxxxxxxxxxxxx

This patch replaces key_move_xxx functions with a single function
key_unit_move.  This new function takes a dir8 parameter giving the
_GUI_ direction of the unit's movement.  This GUI direction is converted
into a _map_ direction by the backend.

This provides three advantages:

- It gets rid of lots of unnecessary code.

- It provides native support for isometric, non-isometric, and (I think)
any future view modes we may come up with.  (Ross, if you think this is
an insufficient design please propose a better one.)

- It provides the beginnings of a distinction between GUI and map
directions.  This can be helpful in many places in the current code
(like this one!), and provides a lot of flexibility for future changes. 
See
http://lists.complete.org/freeciv-dev@xxxxxxxxxxx/2002/01/msg00547.html.gz

One counter-argument could be that the key_move_xxx functions take no
parameters and are easily used directly as callback functions by the
GUI.  But currently no GUI does this - even XAW which has specific
callback functions for each key command needs a different prototype for
them, and uses wrappers.  So this is a non-issue.

The argument that this method is inefficient is also pointless.  dir_ccw
is efficient (and could be made more so in the future), and in any case
this code is not called intensively.

jason


Index: client/control.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/control.c,v
retrieving revision 1.88
diff -u -r1.88 control.c
--- client/control.c    2002/12/11 10:39:41     1.88
+++ client/control.c    2002/12/19 07:25:26
@@ -1385,75 +1385,32 @@
 }
 
 /**************************************************************************
-...
-**************************************************************************/
-void key_move_north(void)
-{
-  if(get_unit_in_focus())
-    request_move_unit_direction(punit_focus, DIR8_NORTH);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-void key_move_north_east(void)
-{
-  if(get_unit_in_focus())
-    request_move_unit_direction(punit_focus, DIR8_NORTHEAST);
-}
+  Convert the given GUI direction into a map direction.
 
-/**************************************************************************
-...
-**************************************************************************/
-void key_move_east(void)
-{
-  if(get_unit_in_focus())
-    request_move_unit_direction(punit_focus, DIR8_EAST);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-void key_move_south_east(void)
-{
-  if(get_unit_in_focus())
-     request_move_unit_direction(punit_focus, DIR8_SOUTHEAST);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-void key_move_south(void)
-{
-  if(get_unit_in_focus())
-     request_move_unit_direction(punit_focus, DIR8_SOUTH);
-}
-
-/**************************************************************************
-...
-**************************************************************************/
-void key_move_south_west(void)
-{
-  if(get_unit_in_focus())
-     request_move_unit_direction(punit_focus, DIR8_SOUTHWEST);
-}
-
-/**************************************************************************
-...
+  GUI directions correspond to the current viewing interface, so that
+  DIR8_NORTH is up on the mapview.  map directions correspond to the
+  underlying map tiles, so that DIR8_NORTH means moving with a vector of
+  (0,-1).
 **************************************************************************/
-void key_move_west(void)
+static enum direction8 gui_to_map_dir(enum direction8 gui_dir)
 {
-  if(get_unit_in_focus())
-    request_move_unit_direction(punit_focus, DIR8_WEST);
+  if (is_isometric) {
+    return dir_ccw(gui_dir);
+  } else {
+    return gui_dir;
+  }
 }
 
 /**************************************************************************
-...
+  Move the focus unit in the given direction.  Here directions are
+  defined according to the GUI, so that north is "up" in the interface.
 **************************************************************************/
-void key_move_north_west(void)
+void key_unit_move(enum direction8 gui_dir)
 {
-  if(get_unit_in_focus())
-     request_move_unit_direction(punit_focus, DIR8_NORTHWEST);
+  if (get_unit_in_focus()) {
+    enum direction8 map_dir = gui_to_map_dir(gui_dir);
+    request_move_unit_direction(get_unit_in_focus(), map_dir);
+  }
 }
 
 /**************************************************************************
Index: client/control.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/control.h,v
retrieving revision 1.30
diff -u -r1.30 control.h
--- client/control.h    2002/11/22 18:52:12     1.30
+++ client/control.h    2002/12/19 07:25:26
@@ -116,14 +116,7 @@
 void key_fog_of_war_toggle(void);
 void key_end_turn(void);
 void key_map_grid_toggle(void);
-void key_move_north(void);
-void key_move_north_east(void);
-void key_move_east(void);
-void key_move_south_east(void);
-void key_move_south(void);
-void key_move_south_west(void);
-void key_move_west(void);
-void key_move_north_west(void);
+void key_unit_move(enum direction8 gui_dir);
 void key_unit_airbase(void);
 void key_unit_auto_attack(void);
 void key_unit_auto_explore(void);
Index: client/gui-gtk/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/gui_main.c,v
retrieving revision 1.123
diff -u -r1.123 gui_main.c
--- client/gui-gtk/gui_main.c   2002/12/16 05:35:30     1.123
+++ client/gui-gtk/gui_main.c   2002/12/19 07:25:26
@@ -248,111 +248,63 @@
     return keypress;
   }
 
-  if (is_isometric && !client_is_observer()) {
+  if (!client_is_observer()) {
     switch (ev->keyval) {
       case GDK_Up:
       case GDK_KP_Up:
       case GDK_8:
-      case GDK_KP_8:           key_move_north_west();  break;
+      case GDK_KP_8:
+       key_unit_move(DIR8_NORTH);
+       break;
 
       case GDK_Page_Up:
       case GDK_KP_Page_Up:
       case GDK_9:
-      case GDK_KP_9:           key_move_north();       break;
+      case GDK_KP_9:
+       key_unit_move(DIR8_NORTHEAST);
+       break;
 
       case GDK_Right:
       case GDK_KP_Right:
       case GDK_6:
-      case GDK_KP_6:           key_move_north_east();  break;
+      case GDK_KP_6:
+       key_unit_move(DIR8_EAST);
+       break;
 
       case GDK_Page_Down:
       case GDK_KP_Page_Down:
       case GDK_3:
-      case GDK_KP_3:           key_move_east();        break;
+      case GDK_KP_3:
+       key_unit_move(DIR8_SOUTHEAST);
+       break;
 
       case GDK_Down:
       case GDK_KP_Down:
       case GDK_2:
-      case GDK_KP_2:           key_move_south_east();  break;
+      case GDK_KP_2:
+       key_unit_move(DIR8_SOUTH);
+       break;
 
       case GDK_End:
       case GDK_KP_End:
       case GDK_1:
-      case GDK_KP_1:           key_move_south();       break;
+      case GDK_KP_1:
+       key_unit_move(DIR8_SOUTHWEST);
+       break;
 
       case GDK_Left:
       case GDK_KP_Left:
       case GDK_4:
-      case GDK_KP_4:           key_move_south_west();  break;
+      case GDK_KP_4:
+       key_unit_move(DIR8_WEST);
+       break;
 
       case GDK_Home:
-      case GDK_KP_Home:
-      case GDK_7:
-      case GDK_KP_7:           key_move_west();        break;
- 
-      case GDK_KP_Begin:
-      case GDK_5:
-      case GDK_KP_5:
-        focus_to_next_unit();
-        break;
-  
-      case GDK_Return:
-      case GDK_KP_Enter:
-        key_end_turn();
-        break;
-  
-      case GDK_Escape:
-        key_cancel_action();
-        break;
-  
-      case GDK_t:
-        key_city_workers(w, ev);
-        break;
-
-      default:
-        return FALSE;
-    }
-  } else if (!client_is_observer()) {
-    switch (ev->keyval) {
-      case GDK_Up:
-      case GDK_KP_Up:
-      case GDK_8:
-      case GDK_KP_8:           key_move_north();       break;
-
-      case GDK_Page_Up:
-      case GDK_KP_Page_Up:
-      case GDK_9:
-      case GDK_KP_9:           key_move_north_east();  break;
-
-      case GDK_Right:
-      case GDK_KP_Right:
-      case GDK_6:
-      case GDK_KP_6:           key_move_east();        break;
-
-      case GDK_Page_Down:
-      case GDK_KP_Page_Down:
-      case GDK_3:
-      case GDK_KP_3:           key_move_south_east();  break;
-
-      case GDK_Down:
-      case GDK_KP_Down:
-      case GDK_2:
-      case GDK_KP_2:           key_move_south();       break;
-
-      case GDK_End:
-      case GDK_KP_End:
-      case GDK_1:
-      case GDK_KP_1:           key_move_south_west();  break;
-
-      case GDK_Left:
-      case GDK_KP_Left:
-      case GDK_4:
-      case GDK_KP_4:           key_move_west();        break;
-
-      case GDK_Home:
       case GDK_KP_Home:                
       case GDK_7:
-      case GDK_KP_7:           key_move_north_west();  break;
+      case GDK_KP_7:
+       key_unit_move(DIR8_NORTHWEST);
+       break;
 
       case GDK_KP_Begin:
       case GDK_Return:
Index: client/gui-gtk-2.0/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/gui_main.c,v
retrieving revision 1.35
diff -u -r1.35 gui_main.c
--- client/gui-gtk-2.0/gui_main.c       2002/12/16 05:35:31     1.35
+++ client/gui-gtk-2.0/gui_main.c       2002/12/19 07:25:26
@@ -340,111 +340,63 @@
     return keypress;
   }
 
-  if (is_isometric && !client_is_observer()) {
+  if (!client_is_observer()) {
     switch (ev->keyval) {
       case GDK_Up:
       case GDK_KP_Up:
       case GDK_8:
-      case GDK_KP_8:           key_move_north_west();  break;
+      case GDK_KP_8:
+       key_unit_move(DIR8_NORTH);
+       break;
 
       case GDK_Page_Up:
       case GDK_KP_Page_Up:
       case GDK_9:
-      case GDK_KP_9:           key_move_north();       break;
+      case GDK_KP_9:
+       key_unit_move(DIR8_NORTHEAST);
+       break;
 
       case GDK_Right:
       case GDK_KP_Right:
       case GDK_6:
-      case GDK_KP_6:           key_move_north_east();  break;
+      case GDK_KP_6:
+       key_unit_move(DIR8_EAST);
+       break;
 
       case GDK_Page_Down:
       case GDK_KP_Page_Down:
       case GDK_3:
-      case GDK_KP_3:           key_move_east();        break;
+      case GDK_KP_3:
+       key_unit_move(DIR8_SOUTHEAST);
+       break;
 
       case GDK_Down:
       case GDK_KP_Down:
       case GDK_2:
-      case GDK_KP_2:           key_move_south_east();  break;
+      case GDK_KP_2:
+       key_unit_move(DIR8_SOUTH);
+       break;
 
       case GDK_End:
       case GDK_KP_End:
       case GDK_1:
-      case GDK_KP_1:           key_move_south();       break;
+      case GDK_KP_1:
+       key_unit_move(DIR8_SOUTHWEST);
+       break;
 
       case GDK_Left:
       case GDK_KP_Left:
       case GDK_4:
-      case GDK_KP_4:           key_move_south_west();  break;
+      case GDK_KP_4:
+       key_unit_move(DIR8_WEST);
+       break;
 
       case GDK_Home:
-      case GDK_KP_Home:
-      case GDK_7:
-      case GDK_KP_7:           key_move_west();        break;
- 
-      case GDK_KP_Begin:
-      case GDK_5:
-      case GDK_KP_5:
-        focus_to_next_unit();
-        break;
-  
-      case GDK_Return:
-      case GDK_KP_Enter:
-        key_end_turn();
-        break;
-  
-      case GDK_Escape:
-        key_cancel_action();
-        break;
-  
-      case GDK_t:
-        key_city_workers(w, ev);
-        break;
-
-      default:
-        return FALSE;
-    }
-  } else if (!client_is_observer()) {
-    switch (ev->keyval) {
-      case GDK_Up:
-      case GDK_KP_Up:
-      case GDK_8:
-      case GDK_KP_8:           key_move_north();       break;
-
-      case GDK_Page_Up:
-      case GDK_KP_Page_Up:
-      case GDK_9:
-      case GDK_KP_9:           key_move_north_east();  break;
-
-      case GDK_Right:
-      case GDK_KP_Right:
-      case GDK_6:
-      case GDK_KP_6:           key_move_east();        break;
-
-      case GDK_Page_Down:
-      case GDK_KP_Page_Down:
-      case GDK_3:
-      case GDK_KP_3:           key_move_south_east();  break;
-
-      case GDK_Down:
-      case GDK_KP_Down:
-      case GDK_2:
-      case GDK_KP_2:           key_move_south();       break;
-
-      case GDK_End:
-      case GDK_KP_End:
-      case GDK_1:
-      case GDK_KP_1:           key_move_south_west();  break;
-
-      case GDK_Left:
-      case GDK_KP_Left:
-      case GDK_4:
-      case GDK_KP_4:           key_move_west();        break;
-
-      case GDK_Home:
       case GDK_KP_Home:                
       case GDK_7:
-      case GDK_KP_7:           key_move_north_west();  break;
+      case GDK_KP_7:
+       key_unit_move(DIR8_NORTHWEST);
+       break;
 
       case GDK_KP_Begin:
       case GDK_Return:
Index: client/gui-mui/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/gui_main.c,v
retrieving revision 1.68
diff -u -r1.68 gui_main.c
--- client/gui-mui/gui_main.c   2002/12/13 19:15:12     1.68
+++ client/gui-mui/gui_main.c   2002/12/19 07:25:26
@@ -343,36 +343,31 @@
 {
   if (*value)
   {
-    if (is_isometric)
-    {
-      switch (*value)
-      {
-       case UNIT_NORTH: key_move_north_west(); break;
-       case UNIT_SOUTH: key_move_south_east(); break;
-       case UNIT_EAST: key_move_north_east(); break;
-       case UNIT_WEST: key_move_south_west(); break;
-       case UNIT_NORTH_EAST: key_move_north(); break;
-       case UNIT_NORTH_WEST: key_move_west(); break;
-       case UNIT_SOUTH_EAST: key_move_east(); break;
-       case UNIT_SOUTH_WEST: key_move_south();break;
-      }
-    } else
-    {
-      switch (*value)
-      {
-       case UNIT_NORTH: key_move_north(); break;
-       case UNIT_SOUTH: key_move_south(); break;
-       case UNIT_EAST: key_move_east(); break;
-       case UNIT_WEST: key_move_west(); break;
-       case UNIT_NORTH_EAST: key_move_north_east(); break;
-       case UNIT_NORTH_WEST: key_move_north_west(); break;
-       case UNIT_SOUTH_EAST: key_move_south_east(); break;
-       case UNIT_SOUTH_WEST: key_move_south_west();break;
-      }
-    }
-
-    switch (*value)
-    {
+    switch (*value) {
+    case UNIT_NORTH:
+      key_unit_move(DIR8_NORTH);
+      break;
+    case UNIT_SOUTH:
+      key_unit_move(DIR8_SOUTH);
+      break;
+    case UNIT_EAST:
+      key_unit_move(DIR8_EAST);
+      break;
+    case UNIT_WEST:
+      key_unit_move(DIR8_WEST);
+      break;
+    case UNIT_NORTH_EAST:
+      key_unit_move(DIR8_NORTHEAST);
+      break;
+    case UNIT_NORTH_WEST:
+      key_unit_move(DIR8_NORTHWEST);
+      break;
+    case UNIT_SOUTH_EAST:
+      key_unit_move(DIR8_SOUTHEAST);
+      break;
+    case UNIT_SOUTH_WEST:
+      key_unit_move(DIR8_SOUTHWEST);
+      break;
     case UNIT_POPUP_CITY:
       {
        struct unit *punit;
Index: client/gui-sdl/gui_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/gui_main.c,v
retrieving revision 1.3
diff -u -r1.3 gui_main.c
--- client/gui-sdl/gui_main.c   2002/12/12 03:05:00     1.3
+++ client/gui-sdl/gui_main.c   2002/12/19 07:25:27
@@ -238,42 +238,42 @@
            break;
          case SDLK_UP:
          case SDLK_KP8:
-           key_move_north_west();
+           key_unit_move(DIR8_NORTH);
            break;
 
          case SDLK_PAGEUP:
          case SDLK_KP9:
-           key_move_north();
+           key_unit_move(DIR8_NORTHEAST);
            break;
 
          case SDLK_RIGHT:
          case SDLK_KP6:
-           key_move_north_east();
+           key_unit_move(DIR8_EAST);
            break;
 
          case SDLK_PAGEDOWN:
          case SDLK_KP3:
-           key_move_east();
+           key_unit_move(DIR8_SOUTHEAST);
            break;
 
          case SDLK_DOWN:
          case SDLK_KP2:
-           key_move_south_east();
+           key_unit_move(DIR8_SOUTH);
            break;
 
          case SDLK_END:
          case SDLK_KP1:
-           key_move_south();
+           key_unit_move(DIR8_SOUTHWEST);
            break;
 
          case SDLK_LEFT:
          case SDLK_KP4:
-           key_move_south_west();
+           key_unit_move(DIR8_WEST);
            break;
 
          case SDLK_HOME:
          case SDLK_KP7:
-           key_move_west();
+           key_unit_move(DIR8_NORTHWEST);
            break;
 
          case SDLK_KP5:
Index: client/gui-win32/menu.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/menu.c,v
retrieving revision 1.12
diff -u -r1.12 menu.c
--- client/gui-win32/menu.c     2002/11/14 09:14:59     1.12
+++ client/gui-win32/menu.c     2002/12/19 07:25:27
@@ -469,32 +469,34 @@
 **************************************************************************/
 void handle_numpad(int code)
 {
-  if (is_isometric) {
-    switch (code)
-      {
-      case IDM_NUMPAD_BASE+1: key_move_south(); break;
-      case IDM_NUMPAD_BASE+2: key_move_south_east(); break;
-      case IDM_NUMPAD_BASE+3: key_move_east(); break;
-      case IDM_NUMPAD_BASE+4: key_move_south_west(); break;
-      case IDM_NUMPAD_BASE+5: focus_to_next_unit(); break;
-      case IDM_NUMPAD_BASE+6: key_move_north_east(); break;
-      case IDM_NUMPAD_BASE+7: key_move_west(); break;
-      case IDM_NUMPAD_BASE+8: key_move_north_west(); break;
-      case IDM_NUMPAD_BASE+9: key_move_north(); break;
-      }
-  } else {
-    switch (code) 
-      { 
-      case IDM_NUMPAD_BASE+1: key_move_south_west(); break;
-      case IDM_NUMPAD_BASE+2: key_move_south(); break;
-      case IDM_NUMPAD_BASE+3: key_move_south_east(); break;
-      case IDM_NUMPAD_BASE+4: key_move_west(); break;
-      case IDM_NUMPAD_BASE+5: focus_to_next_unit(); break;
-      case IDM_NUMPAD_BASE+6: key_move_east(); break;
-      case IDM_NUMPAD_BASE+7: key_move_north_west(); break;
-      case IDM_NUMPAD_BASE+8: key_move_north(); break;
-      case IDM_NUMPAD_BASE+9: key_move_north_east(); break;
-      }
+  switch (code) { 
+  case IDM_NUMPAD_BASE + 1:
+    key_unit_move(DIR8_SOUTHWEST);
+    break;
+  case IDM_NUMPAD_BASE + 2:
+    key_unit_move(DIR8_SOUTH);
+    break;
+  case IDM_NUMPAD_BASE + 3:
+    key_unit_move(DIR8_SOUTHEAST);
+    break;
+  case IDM_NUMPAD_BASE + 4:
+    key_unit_move(DIR8_WEST);
+    break;
+  case IDM_NUMPAD_BASE + 5:
+    focus_to_next_unit();
+    break;
+  case IDM_NUMPAD_BASE + 6:
+    key_unit_move(DIR8_EAST);
+    break;
+  case IDM_NUMPAD_BASE + 7:
+    key_unit_move(DIR8_NORTHWEST);
+    break;
+  case IDM_NUMPAD_BASE + 8:
+    key_unit_move(DIR8_NORTH);
+    break;
+  case IDM_NUMPAD_BASE + 9:
+    key_unit_move(DIR8_NORTHEAST);
+    break;
   }
 }
 
Index: client/gui-xaw/actions.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/actions.c,v
retrieving revision 1.12
diff -u -r1.12 actions.c
--- client/gui-xaw/actions.c    2002/11/14 09:14:59     1.12
+++ client/gui-xaw/actions.c    2002/12/19 07:25:27
@@ -153,42 +153,42 @@
 
 static void xaw_key_move_north(Widget w, XEvent *event, String *argv, Cardinal 
*argc)
 {
-  key_move_north();
+  key_unit_move(DIR8_NORTH);
 }
 
 static void xaw_key_move_north_east(Widget w, XEvent *event, String *argv, 
Cardinal *argc)
 {
-  key_move_north_east();
+  key_unit_move(DIR8_NORTHEAST);
 }
 
 static void xaw_key_move_east(Widget w, XEvent *event, String *argv, Cardinal 
*argc)
 {
-  key_move_east();
+  key_unit_move(DIR8_EAST);
 }
 
 static void xaw_key_move_south_east(Widget w, XEvent *event, String *argv, 
Cardinal *argc)
 {
-  key_move_south_east();
+  key_unit_move(DIR8_SOUTHEAST);
 }
 
 static void xaw_key_move_south(Widget w, XEvent *event, String *argv, Cardinal 
*argc)
 {
-  key_move_south();
+  key_unit_move(DIR8_SOUTH);
 }
 
 static void xaw_key_move_south_west(Widget w, XEvent *event, String *argv, 
Cardinal *argc)
 {
-  key_move_south_west();
+  key_unit_move(DIR8_SOUTHWEST);
 }
 
 static void xaw_key_move_west(Widget w, XEvent *event, String *argv, Cardinal 
*argc)
 {
-  key_move_west();
+  key_unit_move(DIR8_WEST);
 }
 
 static void xaw_key_move_north_west(Widget w, XEvent *event, String *argv, 
Cardinal *argc)
 {
-  key_move_north_west();
+  key_unit_move(DIR8_NORTHWEST);
 }
 
 static void xaw_key_open_city_report(Widget w, XEvent *event, String *argv, 
Cardinal *argc)

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#2608) replace key_move_xxx with key_unit_move, Jason Short via RT <=