Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2003:
[Freeciv-Dev] (PR#3798) Change canvas_to_map_pos() from void to bool
Home

[Freeciv-Dev] (PR#3798) Change canvas_to_map_pos() from void to bool

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#3798) Change canvas_to_map_pos() from void to bool
From: "a-l@xxxxxxx" <a-l@xxxxxxx>
Date: Wed, 26 Mar 2003 08:06:35 -0800
Reply-to: rt@xxxxxxxxxxxxxx

Note: This requires #3779 (remove get_map_xy/get_canvas_xy).

canvas_to_map_pos()   is changed from "void" to "bool",
and may now yield an unreal (but normal) tile.

--  Caller may also have to call nearest_real_pos()
    if that's what you really really want.

--  All instances of get_map_xy()  in all clients (nothing in
    gui-stub or gui-beos) are changed to canvas_to_map_pos()
    according to #3779. The returned bool is checked, and the result
    is either discarded or processed with nearest_real_pos().

Effects:

--  Cities will not pop up and units will not be selected when
    you click on unreal tiles. It wasn't obvious which tile
    you'd get anyway.

--  Goto lines do not appear when you move pointer in the black
    (as in unreal, not fogged). An unreal tile _may_ be passed
    to draw_line(), which now checks.

--  But you can still scroll the map by right-clicking on unreal
    tiles, since convenience is more important than precision.

I tested isometric and tried to be meticulous with the modification
in the clients I can't compile.


This patch will enable me to draw a proper topology independent
rectangle (PR#3727).


Arnstein

diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/goto.c unreal/client/goto.c
--- cvs-Mar-24-3779/client/goto.c       Sat Feb 22 10:43:01 2003
+++ unreal/client/goto.c        Wed Mar 26 16:25:36 2003
@@ -819,15 +819,13 @@
 ***********************************************************************/
 void draw_line(int dest_x, int dest_y)
 {
+  /* Replace with check for is_normal_tile later */
+  bool is_real = normalize_map_pos(&dest_x, &dest_y);
   int start_index;
 
   assert(is_active);
 
-  /* Replace with check for is_normal_tile later */
-  assert(is_real_map_pos(dest_x, dest_y));
-  normalize_map_pos(&dest_x, &dest_y);
-
-  if (VECTOR(dest_x, dest_y) == 0) {
+  if (!is_real || VECTOR(dest_x, dest_y) == 0) {
     undraw_line();
     return;
   }
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/gui-gtk/mapctrl.c 
unreal/client/gui-gtk/mapctrl.c
--- cvs-Mar-24-3779/client/gui-gtk/mapctrl.c    Tue Feb 18 15:52:02 2003
+++ unreal/client/gui-gtk/mapctrl.c     Wed Mar 26 01:55:19 2003
@@ -302,9 +302,10 @@
     return TRUE;
   }
 
-  get_map_xy(ev->x, ev->y, &xtile, &ytile);
+  if (canvas_to_map_pos(&xtile, &ytile, ev->x, ev->y)) {
+    wakeup_sentried_units(xtile, ytile);
+  }
 
-  wakeup_sentried_units(xtile, ytile);
   return TRUE;
 }
 
@@ -314,6 +315,7 @@
 gint butt_down_mapcanvas(GtkWidget *w, GdkEventButton *ev)
 {
   int xtile, ytile;
+  bool is_real;
 
   if (!can_client_change_view()) {
     return TRUE;
@@ -325,12 +327,16 @@
     return TRUE;
   }
 
-  get_map_xy(ev->x, ev->y, &xtile, &ytile);
+  is_real = canvas_to_map_pos(&xtile, &ytile, ev->x, ev->y);
+  if (!is_real) {
+    nearest_real_pos(&xtile, &ytile);
+  }
 
-  if (ev->button == 1) {
+  if (is_real && ev->button == 1) {
     do_map_click(xtile, ytile);
     gtk_widget_grab_focus(turn_done_button);
-  } else if ((ev->button == 2) || (ev->state & GDK_CONTROL_MASK)) {
+  } else if (is_real && ((ev->button == 2)
+                || (ev->state & GDK_CONTROL_MASK))) {
     popit(ev, xtile, ytile);
   } else if (ev->button == 3) {
     center_tile_mapcanvas(xtile, ytile);
@@ -373,7 +379,9 @@
     return;
   }
 
-  get_map_xy(ev->x, ev->y, &map_x, &map_y);
+  if (!canvas_to_map_pos(&map_x, &map_y, ev->x, ev->y)) {
+    return;
+  }
 
   pcity = find_city_near_tile(map_x, map_y);
   if (!pcity) {
@@ -453,7 +461,9 @@
   }
   
   gdk_window_get_pointer(map_canvas->window, &x, &y, NULL);
-  get_map_xy(x, y, &x, &y);
+  if (!canvas_to_map_pos(&x, &y, x, y)) {
+    nearest_real_pos(&x, &y);
+  }
 
   pcity = find_city_near_tile(x, y);
   if (!pcity) {
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/gui-gtk-2.0/mapctrl.c 
unreal/client/gui-gtk-2.0/mapctrl.c
--- cvs-Mar-24-3779/client/gui-gtk-2.0/mapctrl.c        Mon Jan 27 05:56:13 2003
+++ unreal/client/gui-gtk-2.0/mapctrl.c Wed Mar 26 12:40:28 2003
@@ -296,7 +296,9 @@
     return TRUE;
   }
 
-  get_map_xy(ev->x, ev->y, &xtile, &ytile);
+  if (canvas_to_map_pos(&xtile, &ytile, ev->x, ev->y)) {
+    wakeup_sentried_units(xtile, ytile);
+  }
 
   wakeup_sentried_units(xtile, ytile);
   return TRUE;
@@ -308,6 +310,7 @@
 gboolean butt_down_mapcanvas(GtkWidget *w, GdkEventButton *ev, gpointer data)
 {
   int xtile, ytile;
+  bool is_real;
 
   if (!can_client_change_view()) {
     return TRUE;
@@ -319,12 +322,16 @@
     return TRUE;
   }
 
-  get_map_xy(ev->x, ev->y, &xtile, &ytile);
+  is_real = canvas_to_map_pos(&xtile, &ytile, ev->x, ev->y);
+  if (!is_real) {
+    nearest_real_pos(&xtile, &ytile);
+  }
 
-  if (ev->button == 1) {
+  if (is_real && ev->button == 1) {
     do_map_click(xtile, ytile);
     gtk_widget_grab_focus(map_canvas);
-  } else if ((ev->button == 2) || (ev->state & GDK_CONTROL_MASK)) {
+  } else if (is_real && ((ev->button == 2)
+                || (ev->state & GDK_CONTROL_MASK))) {
     popit(ev, xtile, ytile);
   } else if (ev->button == 3) {
     center_tile_mapcanvas(xtile, ytile);
@@ -367,7 +374,9 @@
     return;
   }
 
-  get_map_xy(ev->x, ev->y, &map_x, &map_y);
+  if (!canvas_to_map_pos(&map_x, &map_y, ev->x, ev->y)) {
+    return;
+  }
 
   pcity = find_city_near_tile(map_x, map_y);
   if (!pcity) {
@@ -447,7 +456,9 @@
   }
   
   gdk_window_get_pointer(map_canvas->window, &x, &y, NULL);
-  get_map_xy(x, y, &x, &y);
+  if (!canvas_to_map_pos(&x, &y, x, y)) {
+    nearest_real_pos(&x, &y);
+  }
 
   pcity = find_city_near_tile(x, y);
   if (!pcity) {
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/gui-mui/mapclass.c 
unreal/client/gui-mui/mapclass.c
--- cvs-Mar-24-3779/client/gui-mui/mapclass.c   Sun Feb  9 05:58:33 2003
+++ unreal/client/gui-mui/mapclass.c    Wed Mar 26 02:06:32 2003
@@ -1538,7 +1538,9 @@
        if (_isinobject(msg->imsg->MouseX, msg->imsg->MouseY))
        {
           int x, y;
-          get_map_xy(msg->imsg->MouseX - _mleft(o), msg->imsg->MouseY - 
_mtop(o), &x, &y);
+          if (!canvas_to_map_pos(&x, &y, msg->imsg->MouseX - _mleft(o), 
msg->imsg->MouseY - _mtop(o))) {
+            nearest_real_pos(&x, &y);
+          }
 
          if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
          {
@@ -1610,7 +1612,9 @@
       struct tile *ptile;
       int x,y;
 
-      get_map_xy(msg->mx - _mleft(o), msg->my - _mtop(o), &x, &y);
+      if (!canvas_to_map_pos(&x, &y, msg->mx - _mleft(o), msg->my - _mtop(o))) 
 {
+        nearest_real_pos(&x, &y);
+      }
 
       ptile = map_get_tile(x, y);
 
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/gui-sdl/finddlg.c 
unreal/client/gui-sdl/finddlg.c
--- cvs-Mar-24-3779/client/gui-sdl/finddlg.c    Sun Mar 23 20:05:23 2003
+++ unreal/client/gui-sdl/finddlg.c     Wed Mar 26 12:48:56 2003
@@ -157,7 +157,10 @@
   
   h = WINDOW_TILE_HIGH + 3 + FRAME_WH;
   
-  get_map_xy(Main.map->w/2 , Main.map->h/2 , &orginal_x , &orginal_y);
+  if (!canvas_to_map_pos(&orginal_x , &orginal_y ,
+        Main.map->w/2 , Main.map->h/2)) {
+    nearest_real_pos(&orginal_x , &orginal_y);
+  }
   
   pFind_City_Dlg = MALLOC(sizeof(struct ADVANCED_DLG));
   
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/gui-sdl/mapctrl.c 
unreal/client/gui-sdl/mapctrl.c
--- cvs-Mar-24-3779/client/gui-sdl/mapctrl.c    Sun Mar 23 20:05:23 2003
+++ unreal/client/gui-sdl/mapctrl.c     Wed Mar 26 16:38:20 2003
@@ -773,6 +773,7 @@
 void button_down_on_map(SDL_MouseButtonEvent * pButtonEvent)
 {
   int col, row;
+  bool is_real;
 
   if (get_client_state() != CLIENT_GAME_RUNNING_STATE) {
     return;
@@ -785,15 +786,23 @@
   }
 #endif
   
-  get_map_xy((int) pButtonEvent->x, (int) pButtonEvent->y, &col, &row);
+  is_real =
+    canvas_to_map_pos(&col, &row, (int) pButtonEvent->x, (int) 
pButtonEvent->y);
   draw_goto_patrol_lines = FALSE;
   
   if (pButtonEvent->button == SDL_BUTTON_LEFT) {
-    do_map_click(col, row);
+    if (is_real) {
+      do_map_click(col, row);
+    }
   } else {
     if (pButtonEvent->button == SDL_BUTTON_MIDDLE) {
-      popup_advanced_terrain_dialog(col , row);
+      if (is_real) {
+        popup_advanced_terrain_dialog(col , row);
+      }
     } else {
+      if (!is_real) {
+        nearest_real_pos(&col, &row);
+      }
       center_tile_mapcanvas(col, row);
       flush_dirty();
     }
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/gui-sdl/mapview.c 
unreal/client/gui-sdl/mapview.c
--- cvs-Mar-24-3779/client/gui-sdl/mapview.c    Sun Mar 23 20:05:23 2003
+++ unreal/client/gui-sdl/mapview.c     Wed Mar 26 12:59:34 2003
@@ -1221,7 +1221,8 @@
     map_h = mapview_canvas.tile_height;
     
 #if 0
-    get_map_xy(0, 0, &Wx, &Wy);        /* take from Main Map */
+    canvas_to_map_pos(&Wx, &Wy, 0, 0); /* take from Main Map */
+    nearest_real_pos(&Wx, &Wy);
 #endif
     
     Wx = map_view_x0 * Mini_map_cell_w + FRAME_WH;
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/gui-win32/mapctrl.c 
unreal/client/gui-win32/mapctrl.c
--- cvs-Mar-24-3779/client/gui-win32/mapctrl.c  Fri Jan 24 05:56:06 2003
+++ unreal/client/gui-win32/mapctrl.c   Wed Mar 26 16:36:53 2003
@@ -62,7 +62,8 @@
 
   if ((hover_state == HOVER_GOTO || hover_state == HOVER_PATROL)
       && draw_goto_line) {
-    get_map_xy(window_x, window_y, &x, &y);
+    /* May pass an unreal tile */
+    canvas_to_map_pos(&x, &y, window_x, window_y);
     
     get_line_dest(&old_x, &old_y);
     if (old_x != x || old_y != y) {
@@ -278,7 +279,9 @@
       break;
     }
     SetFocus(root_window);
-    get_map_xy(LOWORD(lParam),HIWORD(lParam),&xtile,&ytile);
+    if (!canvas_to_map_pos(&xtile,&ytile,LOWORD(lParam),HIWORD(lParam))) {
+      break;
+    }
     if (wParam&MK_SHIFT) {
       adjust_workers(xtile,ytile);
       wakeup_sentried_units(xtile,ytile);
@@ -289,18 +292,24 @@
     }
     break;
   case WM_MBUTTONDOWN:
-    if (can_client_change_view()) {
-      get_map_xy(LOWORD(lParam), HIWORD(lParam), &xtile, &ytile);
+    if (can_client_change_view()
+        && canvas_to_map_pos(&xtile, &ytile, LOWORD(lParam), HIWORD(lParam))) {
       popit(LOWORD(lParam), HIWORD(lParam), xtile, ytile);
     }
     break;
   case WM_RBUTTONDOWN:
     if (can_client_change_view()) {
-      get_map_xy(LOWORD(lParam),HIWORD(lParam),&xtile,&ytile);
+      bool is_real =
+        canvas_to_map_pos(&xtile,&ytile, LOWORD(lParam),HIWORD(lParam));
       if (wParam&MK_CONTROL) {
-       popit(LOWORD(lParam),HIWORD(lParam),xtile,ytile);       
+        if (is_real) {
+          popit(LOWORD(lParam),HIWORD(lParam),xtile,ytile);
+        }
       } else {
-       center_tile_mapcanvas(xtile,ytile);
+        if (!is_real) {
+          nearest_real_pos(&xtile, &ytile);
+        }
+        center_tile_mapcanvas(xtile,ytile);
       }
     }
     break;
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/gui-xaw/mapctrl.c 
unreal/client/gui-xaw/mapctrl.c
--- cvs-Mar-24-3779/client/gui-xaw/mapctrl.c    Fri Jan 10 05:56:36 2003
+++ unreal/client/gui-xaw/mapctrl.c     Wed Mar 26 01:58:51 2003
@@ -264,7 +264,9 @@
     return;
   }
 
-  get_map_xy(ev->x, ev->y, &x, &y);
+  if (!canvas_to_map_pos(&x, &y, ev->x, ev->y)) {
+    nearest_real_pos(&x, &y);
+  }
 
   if (ev->button==Button1)
     do_map_click(x, y);
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/mapctrl_common.c 
unreal/client/mapctrl_common.c
--- cvs-Mar-24-3779/client/mapctrl_common.c     Wed Mar 26 13:32:10 2003
+++ unreal/client/mapctrl_common.c      Wed Mar 26 16:11:45 2003
@@ -58,7 +58,9 @@
   canvas_y = mapview_canvas.height / 2;
   canvas_x += DIR_DX[gui_dir] * mapview_canvas.width / 2;
   canvas_y += DIR_DY[gui_dir] * mapview_canvas.height / 2;
-  canvas_to_map_pos(&map_x, &map_y, canvas_x, canvas_y);
+  if (!canvas_to_map_pos(&map_x, &map_y, canvas_x, canvas_y)) {
+    nearest_real_pos(&map_x, &map_y);
+  }
   center_tile_mapcanvas(map_x, map_y);
 }
 
@@ -109,6 +111,7 @@
       && draw_goto_line) {
     int x, y, old_x, old_y;
 
+    /* May pass an unreal tile */
     canvas_to_map_pos(&x, &y, canvas_x, canvas_y);
 
     get_line_dest(&old_x, &old_y);
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/mapview_common.c 
unreal/client/mapview_common.c
--- cvs-Mar-24-3779/client/mapview_common.c     Wed Mar 26 13:32:10 2003
+++ unreal/client/mapview_common.c      Wed Mar 26 14:10:04 2003
@@ -229,8 +229,10 @@
 
 /**************************************************************************
   Finds the map coordinates corresponding to pixel coordinates.
+  The tile will be normal, but not neccessarily real. Returns FALSE
+  if the tile is unreal. Caller may use nearest_real_pos() if required.
 **************************************************************************/
-void canvas_to_map_pos(int *map_x, int *map_y, int canvas_x, int canvas_y)
+bool canvas_to_map_pos(int *map_x, int *map_y, int canvas_x, int canvas_y)
 {
   if (is_isometric) {
     /* The basic operation here is a simple pi/4 rotation; however, we
@@ -270,11 +272,7 @@
   *map_x += mapview_canvas.map_x0;
   *map_y += mapview_canvas.map_y0;
 
-  /*
-   * If we are outside the map find the nearest tile, with distance as
-   * seen on the map.
-   */
-  nearest_real_pos(map_x, map_y);
+  return normalize_map_pos(map_x, map_y);
 }
 
 /**************************************************************************
@@ -283,8 +281,10 @@
 void get_center_tile_mapcanvas(int *map_x, int *map_y)
 {
   /* This sets the pointers map_x and map_y */
-  canvas_to_map_pos(map_x, map_y,
-                   mapview_canvas.width / 2, mapview_canvas.height / 2);
+  if (!canvas_to_map_pos(map_x, map_y,
+          mapview_canvas.width / 2, mapview_canvas.height / 2)) {
+    nearest_real_pos(map_x, map_y);
+  }
 }
 
 /**************************************************************************
diff -ruN -Xdiff_ignore cvs-Mar-24-3779/client/mapview_common.h 
unreal/client/mapview_common.h
--- cvs-Mar-24-3779/client/mapview_common.h     Wed Mar 26 13:32:10 2003
+++ unreal/client/mapview_common.h      Tue Mar 25 20:18:01 2003
@@ -138,7 +138,7 @@
 enum color_std get_grid_color(int x1, int y1, int x2, int y2);
 
 bool map_to_canvas_pos(int *canvas_x, int *canvas_y, int map_x, int map_y);
-void canvas_to_map_pos(int *map_x, int *map_y, int canvas_x, int canvas_y);
+bool canvas_to_map_pos(int *map_x, int *map_y, int canvas_x, int canvas_y);
 
 void get_center_tile_mapcanvas(int *map_x, int *map_y);
 void center_tile_mapcanvas(int map_x, int map_y);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#3798) Change canvas_to_map_pos() from void to bool, a-l@xxxxxxx <=