Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2001:
[Freeciv-Dev] Re: get_canvas_xy unification (PR#1054)
Home

[Freeciv-Dev] Re: get_canvas_xy unification (PR#1054)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Cc: bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: get_canvas_xy unification (PR#1054)
From: jdorje@xxxxxxxxxxxxxxxxxxxxx
Date: Wed, 7 Nov 2001 12:07:45 -0800 (PST)

Andreas Kemnade wrote:
> 
> jdorje@xxxxxxxxxxxxxxxxxxxxx writes:
>  > The attached patch makes a new common function, base_get_canvas_xy,
>  > which is called by get_canvas_xy for each GUI platform.
>  >
>  > There are several advantages to this: it reduces overall code,
>  > consolidates places where changes will have to be made in the GUI, and
>  > provides isometric-view capabilities to this function for all GUI's.
>  >
>  > get_canvas_xy is kept around because the variables needed by
>  > base_get_canvas_xy are not common.  But they should be.  I would propose
>  > a naming system that is a cross between the win32 and gtk current
>  > systems: map_view_x0/map_view_y0 for the top-left window corner,
>  > map_win_width/map_win_height for the canvas width and height.  Currently
>  > map_canvas_store_twidth/map_canvas_store_theight are used for the canvas
>  > width in tiles; these are misnamed but I'm not sure what would be better
>  > (map_win_twidth/map_win_theight, perhaps?).  This patch is a big step
>  > toward an eventual total unification.
>  >
>  > Platforms tested: gtk
>  > Untested: xaw (I'll test shortly), win32, mui
> 
> Works on win32.

How about the attached patch instead?  It also unifies get_map_xy and
center_tile_mapcanvas.

The only questionable part is that I've changed EXTRA_BOTTOM_ROW on
win32.  It seems likely to me that the change is correct, though.

The patch is significanly larger, naturally.

jason
Index: client/mapview_common.c
===================================================================
RCS file: /home/jshort/cvs/myfreeciv/client/mapview_common.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 mapview_common.c
--- client/mapview_common.c     2001/11/01 16:42:11     1.1.1.1
+++ client/mapview_common.c     2001/11/05 17:30:11
@@ -21,6 +21,7 @@
 #include "mapview_g.h"
 
 #include "mapview_common.h"
+#include "tilespec.h"
 
 /**************************************************************************
  Refreshes a single tile on the map canvas.
@@ -77,5 +78,144 @@
     return COLOR_STD_RED;
   } else {
     return COLOR_STD_WHITE;
+  }
+}
+
+/**************************************************************************
+Finds the pixel coordinates of a tile.  Beside setting the results in
+canvas_x,canvas_y it returns whether the tile is inside the visible map.
+**************************************************************************/
+int base_get_canvas_xy(int map_x, int map_y,
+                      int *canvas_x, int *canvas_y,
+                      int map_view_x0, int map_view_y0,
+                      int map_win_width, int map_win_height)
+{
+  if (is_isometric) {
+    /* canvas_x,canvas_y is the top corner of the actual tile, not the pixmap.
+       This function also handles non-adjusted tile coords (like -1, -2) as if
+       they were adjusted.  You might want to first take a look at the simpler
+       city_get_canvas() for basic understanding. */
+    int diff_xy;
+    int diff_x, diff_y;
+
+    map_x %= map.xsize;
+    if (map_x < map_view_x0) map_x += map.xsize;
+    diff_xy = (map_x + map_y) - (map_view_x0 + map_view_y0);
+    /* one diff_xy value defines a line parallel with the top of the isometric
+       view. */
+    *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) * 
(NORMAL_TILE_HEIGHT/2);
+
+    /* changing both x and y with the same amount doesn't change the isometric
+       x value. (draw a grid to see it!) */
+    map_x -= diff_xy/2;
+    map_y -= diff_xy/2;
+    diff_x = map_x - map_view_x0;
+    diff_y = map_view_y0 - map_y;
+
+    *canvas_x = (diff_x - 1) * NORMAL_TILE_WIDTH
+      + (diff_x == diff_y ? NORMAL_TILE_WIDTH : NORMAL_TILE_WIDTH/2)
+      /* tiles starting above the visible area */
+      + (diff_y > diff_x ? NORMAL_TILE_WIDTH : 0);
+
+    /* We now have the corner of the sprite. For drawing we move it. */
+    *canvas_x -= NORMAL_TILE_WIDTH/2;
+
+    return (*canvas_x > -NORMAL_TILE_WIDTH)
+      && *canvas_x < (map_win_width + NORMAL_TILE_WIDTH/2)
+      && (*canvas_y > -NORMAL_TILE_HEIGHT)
+      && (*canvas_y < map_win_height);
+  } else { /* is_isometric */
+    /* map_view_width is the width in tiles */
+    int map_view_width = (map_win_width + NORMAL_TILE_WIDTH - 1) / 
NORMAL_TILE_WIDTH;
+    if (map_view_x0+map_view_width <= map.xsize)
+      *canvas_x = map_x-map_view_x0;
+    else if(map_x >= map_view_x0)
+      *canvas_x = map_x-map_view_x0;
+    else if(map_x < map_adjust_x(map_view_x0+map_view_width))
+      *canvas_x = map_x+map.xsize-map_view_x0;
+    else *canvas_x = -1;
+
+    *canvas_y = map_y - map_view_y0;
+
+    *canvas_x *= NORMAL_TILE_WIDTH;
+    *canvas_y *= NORMAL_TILE_HEIGHT;
+
+    return *canvas_x >= 0
+       && *canvas_x < map_win_width
+       && *canvas_y >= 0
+       && *canvas_y < map_win_height;
+  }
+}
+
+/**************************************************************************
+Finds the map coordinates corresponding to pixel coordinates.
+**************************************************************************/
+void base_get_map_xy(int canvas_x, int canvas_y,
+                    int *map_x, int *map_y,
+                    int map_view_x0, int map_view_y0)
+{
+  if (is_isometric) {
+    *map_x = map_view_x0;
+    *map_y = map_view_y0;
+
+    /* first find an equivalent position on the left side of the screen. */
+    *map_x += canvas_x/NORMAL_TILE_WIDTH;
+    *map_y -= canvas_x/NORMAL_TILE_WIDTH;
+    canvas_x %= NORMAL_TILE_WIDTH;
+
+    /* Then move op to the top corner. */
+    *map_x += canvas_y/NORMAL_TILE_HEIGHT;
+    *map_y += canvas_y/NORMAL_TILE_HEIGHT;
+    canvas_y %= NORMAL_TILE_HEIGHT;
+
+    /* We are inside a rectangle, with 2 half tiles starting in the corner,
+       and two tiles further out. Draw a grid to see how this works :). */
+    assert(NORMAL_TILE_WIDTH == 2*NORMAL_TILE_HEIGHT);
+    canvas_y *= 2; /* now we have a square. */
+    if (canvas_x > canvas_y) (*map_y) -= 1;
+    if (canvas_x + canvas_y > NORMAL_TILE_WIDTH) (*map_x) += 1;
+
+    /* If we are outside the map find the nearest tile, with distance as
+       seen on the map. */
+    nearest_real_pos(map_x, map_y);
+  } else { /* is_isometric */
+    *map_x = map_adjust_x(map_view_x0 + canvas_x/NORMAL_TILE_WIDTH);
+    *map_y = map_adjust_y(map_view_y0 + canvas_y/NORMAL_TILE_HEIGHT);
+  }
+}
+
+/**************************************************************************
+Centers the mapview around (map_x, map_y).  (map_view_x0, map_view_y0) is
+the upper-left coordinates of the mapview; these should be (but aren't)
+stored globally - each GUI has separate storate structs for them.
+**************************************************************************/
+void base_center_tile_mapcanvas(int map_x, int map_y,
+                               int *map_view_x0, int *map_view_y0,
+                               int map_view_width, int map_view_height)
+{
+  if (is_isometric) {
+    map_x -= map_view_width/2;
+    map_y += map_view_width/2;
+    map_x -= map_view_height/2;
+    map_y -= map_view_height/2;
+
+    *map_view_x0 = map_adjust_x(map_x);
+    *map_view_y0 = map_adjust_y(map_y);
+
+    *map_view_y0 =
+      (*map_view_y0 > map.ysize + EXTRA_BOTTOM_ROW - map_view_height) ?
+      map.ysize + EXTRA_BOTTOM_ROW - map_view_height :
+      *map_view_y0;
+  } else {
+    int new_map_view_x0, new_map_view_y0;
+
+    new_map_view_x0=map_adjust_x(map_x-map_view_width/2);
+    new_map_view_y0=map_adjust_y(map_y-map_view_height/2);
+    if (new_map_view_y0>map.ysize+EXTRA_BOTTOM_ROW-map_view_height)
+      new_map_view_y0=
+       map_adjust_y(map.ysize+EXTRA_BOTTOM_ROW-map_view_height);
+
+    *map_view_x0=new_map_view_x0;
+    *map_view_y0=new_map_view_y0;
   }
 }
Index: client/mapview_common.h
===================================================================
RCS file: /home/jshort/cvs/myfreeciv/client/mapview_common.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 mapview_common.h
--- client/mapview_common.h     2001/11/01 16:42:11     1.1.1.1
+++ client/mapview_common.h     2001/11/05 17:33:10
@@ -16,7 +16,40 @@
 
 #include "colors_g.h"
 
+
+/*
+The bottom row of the map was sometimes hidden.
+
+As of now the top left corner is always aligned with the tiles. This is what
+causes the problem in the first place. The ideal solution would be to align the
+window with the bottom left tiles if you tried to center the window on a tile
+closer than (screen_tiles_height/2 -1) to the south pole.
+
+But, for now, I just grepped for occurences where the ysize (or the values
+derived from it) were used, and those places that had relevance to drawing the
+map, and I added 1 (using the EXTRA_BOTTOM_ROW constant).
+
+-Thue
+*/
+
+/* If we have isometric view we need to be able to scroll a little extra down.
+   The places that needs to be adjusted are the same as above. */
+#define EXTRA_BOTTOM_ROW (is_isometric ? 6 : 1)
+
+
 void refresh_tile_mapcanvas(int x, int y, int write_to_screen);
 enum color_std get_grid_color(int x1, int y1, int x2, int y2);
+
+int base_get_canvas_xy(int map_x, int map_y,
+                      int *canvas_x, int *canvas_y,
+                      int map_view_x0, int map_view_y0,
+                      int map_win_width, int map_win_height);
+void base_get_map_xy(int canvas_x, int canvas_y,
+                    int *map_x, int *map_y,
+                    int map_view_x0, int map_view_y0);
+               
+void base_center_tile_mapcanvas(int map_x, int map_y,
+                               int *map_view_x0, int *map_view_y0,
+                               int map_win_twidth, int map_win_theight);
 
 #endif /* FC__MAPVIEW_COMMON_H */
Index: client/gui-gtk/mapview.c
===================================================================
RCS file: /home/jshort/cvs/myfreeciv/client/gui-gtk/mapview.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 mapview.c
--- client/gui-gtk/mapview.c    2001/11/01 16:42:13     1.1.1.1
+++ client/gui-gtk/mapview.c    2001/11/05 17:31:32
@@ -48,24 +48,6 @@
 
 #include "mapview.h"
 
-/*
-The bottom row of the map was sometimes hidden.
-
-As of now the top left corner is always aligned with the tiles. This is what
-causes the problem in the first place. The ideal solution would be to align the
-window with the bottom left tiles if you tried to center the window on a tile
-closer than (screen_tiles_height/2 -1) to the south pole.
-
-But, for now, I just grepped for occurences where the ysize (or the values
-derived from it) were used, and those places that had relevance to drawing the
-map, and I added 1 (using the EXTRA_BOTTOM_ROW constant).
-
--Thue
-*/
-/* If we have isometric view we need to be able to scroll a little extra down.
-   The places that needs to be adjusted are the same as above. */
-#define EXTRA_BOTTOM_ROW (is_isometric ? 6 : 1)
-
 /* contains the x0, y0 coordinates of the upper left corner block */
 int map_view_x0, map_view_y0;
 
@@ -249,103 +231,26 @@
 }
 
 /**************************************************************************
-Finds the pixel coordinates of a tile.
-Beside setting the results in canvas_x,canvas_y it returns whether the tile
-is inside the visible map.
+Finds the pixel coordinates of a tile.  Beside setting the results in
+canvas_x,canvas_y it returns whether the tile is inside the visible map.
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 static int get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
 {
-  if (is_isometric) {
-    /* canvas_x,canvas_y is the top corner of the actual tile, not the pixmap.
-       This function also handels non-adjusted tile coords (like -1, -2) as if
-       they were adjusted.
-       You might want to first take a look at the simpler city_get_xy() for 
basic
-       understanding. */
-    int diff_xy;
-    int diff_x, diff_y;
-    int width, height;
-    gdk_window_get_size(map_canvas->window, &width, &height);
-
-    map_x %= map.xsize;
-    if (map_x < map_view_x0) map_x += map.xsize;
-    diff_xy = (map_x + map_y) - (map_view_x0 + map_view_y0);
-    /* one diff_xy value defines a line parallel with the top of the isometric
-       view. */
-    *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) * 
(NORMAL_TILE_HEIGHT/2);
-
-    /* changing both x and y with the same amount doesn't change the isometric
-       x value. (draw a grid to see it!) */
-    map_x -= diff_xy/2;
-    map_y -= diff_xy/2;
-    diff_x = map_x - map_view_x0;
-    diff_y = map_view_y0 - map_y;
-
-    *canvas_x = (diff_x - 1) * NORMAL_TILE_WIDTH
-      + (diff_x == diff_y ? NORMAL_TILE_WIDTH : NORMAL_TILE_WIDTH/2)
-      /* tiles starting above the visible area */
-      + (diff_y > diff_x ? NORMAL_TILE_WIDTH : 0);
-
-    /* We now have the corner of the sprite. For drawing we move it. */
-    *canvas_x -= NORMAL_TILE_WIDTH/2;
-
-    return (*canvas_x > -NORMAL_TILE_WIDTH)
-      && *canvas_x < (width + NORMAL_TILE_WIDTH/2)
-      && (*canvas_y > -NORMAL_TILE_HEIGHT)
-      && (*canvas_y < height);
-  } else { /* is_isometric */
-    if (map_view_x0+map_canvas_store_twidth <= map.xsize)
-      *canvas_x = map_x-map_view_x0;
-    else if(map_x >= map_view_x0)
-      *canvas_x = map_x-map_view_x0;
-    else if(map_x < map_adjust_x(map_view_x0+map_canvas_store_twidth))
-      *canvas_x = map_x+map.xsize-map_view_x0;
-    else *canvas_x = -1;
-
-    *canvas_y = map_y - map_view_y0;
-
-    *canvas_x *= NORMAL_TILE_WIDTH;
-    *canvas_y *= NORMAL_TILE_HEIGHT;
-
-    return *canvas_x >= 0
-       && *canvas_x < map_canvas_store_twidth * NORMAL_TILE_WIDTH
-       && *canvas_y >= 0
-       && *canvas_y < map_canvas_store_theight * NORMAL_TILE_HEIGHT;
-  }
+  int width, height;
+  gdk_window_get_size(map_canvas->window, &width, &height);
+  return base_get_canvas_xy(map_x, map_y, canvas_x, canvas_y, map_view_x0, 
map_view_y0, width, height);
 }
 
 /**************************************************************************
 Finds the map coordinates corresponding to pixel coordinates.
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 void get_map_xy(int canvas_x, int canvas_y, int *map_x, int *map_y)
 {
-  if (is_isometric) {
-    *map_x = map_view_x0;
-    *map_y = map_view_y0;
-
-    /* first find an equivalent position on the left side of the screen. */
-    *map_x += canvas_x/NORMAL_TILE_WIDTH;
-    *map_y -= canvas_x/NORMAL_TILE_WIDTH;
-    canvas_x %= NORMAL_TILE_WIDTH;
-
-    /* Then move op to the top corner. */
-    *map_x += canvas_y/NORMAL_TILE_HEIGHT;
-    *map_y += canvas_y/NORMAL_TILE_HEIGHT;
-    canvas_y %= NORMAL_TILE_HEIGHT;
-
-    /* We are inside a rectangle, with 2 half tiles starting in the corner,
-       and two tiles further out. Draw a grid to see how this works :). */
-    assert(NORMAL_TILE_WIDTH == 2*NORMAL_TILE_HEIGHT);
-    canvas_y *= 2; /* now we have a square. */
-    if (canvas_x > canvas_y) (*map_y) -= 1;
-    if (canvas_x + canvas_y > NORMAL_TILE_WIDTH) (*map_x) += 1;
-
-    /* If we are outside the map find the nearest tile, with distance as
-       seen on the map. */
-    nearest_real_pos(map_x, map_y);
-  } else { /* is_isometric */
-    *map_x = map_adjust_x(map_view_x0 + canvas_x/NORMAL_TILE_WIDTH);
-    *map_y = map_adjust_y(map_view_y0 + canvas_y/NORMAL_TILE_HEIGHT);
-  }
+  base_get_map_xy(canvas_x, canvas_y, map_x, map_y, map_view_x0, map_view_y0);
 }
 
 
@@ -815,35 +720,13 @@
 }
 
 /**************************************************************************
-...
+Centers the mapview around (x, y).
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 void center_tile_mapcanvas(int x, int y)
 {
-  if (is_isometric) {
-    x -= map_canvas_store_twidth/2;
-    y += map_canvas_store_twidth/2;
-    x -= map_canvas_store_theight/2;
-    y -= map_canvas_store_theight/2;
-
-    map_view_x0 = map_adjust_x(x);
-    map_view_y0 = map_adjust_y(y);
-
-    map_view_y0 =
-      (map_view_y0 > map.ysize + EXTRA_BOTTOM_ROW - map_canvas_store_theight) 
? 
-      map.ysize + EXTRA_BOTTOM_ROW - map_canvas_store_theight :
-      map_view_y0;
-  } else {
-    int new_map_view_x0, new_map_view_y0;
-
-    new_map_view_x0=map_adjust_x(x-map_canvas_store_twidth/2);
-    new_map_view_y0=map_adjust_y(y-map_canvas_store_theight/2);
-    if (new_map_view_y0>map.ysize+EXTRA_BOTTOM_ROW-map_canvas_store_theight)
-      new_map_view_y0=
-       map_adjust_y(map.ysize+EXTRA_BOTTOM_ROW-map_canvas_store_theight);
-
-    map_view_x0=new_map_view_x0;
-    map_view_y0=new_map_view_y0;
-  }
+  base_center_tile_mapcanvas(x, y, &map_view_x0, &map_view_y0, 
map_canvas_store_twidth, map_canvas_store_theight);
 
   update_map_canvas_visible();
   update_map_canvas_scrollbars();
Index: client/gui-mui/mapview.c
===================================================================
RCS file: /home/jshort/cvs/myfreeciv/client/gui-mui/mapview.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 mapview.c
--- client/gui-mui/mapview.c    2001/11/01 16:42:13     1.1.1.1
+++ client/gui-mui/mapview.c    2001/11/05 17:31:32
@@ -54,25 +54,6 @@
 #include "mapclass.h"
 #include "overviewclass.h"
 
-/*
-The bottom row of the map was sometimes hidden.
-
-As of now the top left corner is always aligned with the tiles. This is what
-causes the problem in the first place. The ideal solution would be to align the
-window with the bottom left tiles if you tried to center the window on a tile
-closer than (screen_tiles_height/2 -1) to the south pole.
-
-But, for now, I just grepped for occurences where the ysize (or the values
-derived from it) were used, and those places that had relevance to drawing the
-map, and I added 1 (using the EXTRA_BOTTOM_ROW constant).
-
--Thue
-*/
-
-/* If we have isometric view we need to be able to scroll a little extra down.
-   The places that needs to be adjusted are the same as above. */
-#define EXTRA_BOTTOM_ROW (is_isometric ? 6 : 1)
-
 /**************************************************************************
  Some support functions
 **************************************************************************/
@@ -373,113 +354,30 @@
 
 
 /**************************************************************************
-Finds the pixel coordinates of a tile.
-Beside setting the results in canvas_x,canvas_y it returns whether the tile
-is inside the visible map.
+Finds the pixel coordinates of a tile.  Beside setting the results in
+canvas_x,canvas_y it returns whether the tile is inside the visible map.
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 int get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
 {
-  int map_canvas_store_twidth = xget(main_map_area, MUIA_Map_HorizVisible);
-  int map_canvas_store_theight = xget(main_map_area, MUIA_Map_VertVisible);
   int map_view_x0 = xget(main_map_area, MUIA_Map_HorizFirst);
   int map_view_y0 = xget(main_map_area, MUIA_Map_VertFirst);
-
-  if (is_isometric) {
-    /* canvas_x,canvas_y is the top corner of the actual tile, not the pixmap.
-       This function also handels non-adjusted tile coords (like -1, -2) as if
-       they were adjusted.
-       You might want to first take a look at the simpler city_get_xy() for 
basic
-       understanding. */
-    int diff_xy;
-    int diff_x, diff_y;
-    int width, height;
-
-    width = _mwidth(main_map_area); /* !! */
-    height = _mheight(main_map_area); /* !! */
-
-    map_x %= map.xsize;
-    if (map_x < map_view_x0) map_x += map.xsize;
-    diff_xy = (map_x + map_y) - (map_view_x0 + map_view_y0);
-    /* one diff_xy value defines a line parallel with the top of the isometric
-       view. */
-    *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) * 
(NORMAL_TILE_HEIGHT/2);
-
-    /* changing both x and y with the same amount doesn't change the isometric
-       x value. (draw a grid to see it!) */
-    map_x -= diff_xy/2;
-    map_y -= diff_xy/2;
-    diff_x = map_x - map_view_x0;
-    diff_y = map_view_y0 - map_y;
-
-    *canvas_x = (diff_x - 1) * NORMAL_TILE_WIDTH
-      + (diff_x == diff_y ? NORMAL_TILE_WIDTH : NORMAL_TILE_WIDTH/2)
-      /* tiles starting above the visible area */
-      + (diff_y > diff_x ? NORMAL_TILE_WIDTH : 0);
-
-    /* We now have the corner of the sprite. For drawing we move it. */
-    *canvas_x -= NORMAL_TILE_WIDTH/2;
-
-    return (*canvas_x > -NORMAL_TILE_WIDTH)
-      && *canvas_x < (width + NORMAL_TILE_WIDTH/2)
-      && (*canvas_y > -NORMAL_TILE_HEIGHT)
-      && (*canvas_y < height);
-  } else { /* is_isometric */
-    if (map_view_x0+map_canvas_store_twidth <= map.xsize)
-      *canvas_x = map_x-map_view_x0;
-    else if(map_x >= map_view_x0)
-      *canvas_x = map_x-map_view_x0;
-    else if(map_x < map_adjust_x(map_view_x0+map_canvas_store_twidth))
-      *canvas_x = map_x+map.xsize-map_view_x0;
-    else *canvas_x = -1;
-
-    *canvas_y = map_y - map_view_y0;
-
-    *canvas_x *= NORMAL_TILE_WIDTH;
-    *canvas_y *= NORMAL_TILE_HEIGHT;
-
-    return *canvas_x >= 0
-       && *canvas_x < map_canvas_store_twidth * NORMAL_TILE_WIDTH
-       && *canvas_y >= 0
-       && *canvas_y < map_canvas_store_theight * NORMAL_TILE_HEIGHT;
-  }
+  int width = _mwidth(main_map_area); /* !! */
+  int height = _mheight(main_map_area); /* !! */
+  return base_get_canvas_xy(map_x, map_y, canvas_x, canvas_y, map_view_x0, 
map_view_y0, width, height);
 }
 
 /**************************************************************************
 Finds the map coordinates corresponding to pixel coordinates.
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 void get_map_xy(int canvas_x, int canvas_y, int *map_x, int *map_y)
 {
   int map_view_x0 = xget(main_map_area, MUIA_Map_HorizFirst);
   int map_view_y0 = xget(main_map_area, MUIA_Map_VertFirst);
-
-  if (is_isometric) {
-    *map_x = map_view_x0;
-    *map_y = map_view_y0;
-
-    /* first find an equivalent position on the left side of the screen. */
-    *map_x += canvas_x/NORMAL_TILE_WIDTH;
-    *map_y -= canvas_x/NORMAL_TILE_WIDTH;
-    canvas_x %= NORMAL_TILE_WIDTH;
-
-    /* Then move op to the top corner. */
-    *map_x += canvas_y/NORMAL_TILE_HEIGHT;
-    *map_y += canvas_y/NORMAL_TILE_HEIGHT;
-    canvas_y %= NORMAL_TILE_HEIGHT;
-
-    /* We are inside a rectangle, with 2 half tiles starting in the corner,
-       and two tiles further out. Draw a grid to see how this works :). */
-    assert(NORMAL_TILE_WIDTH == 2*NORMAL_TILE_HEIGHT);
-    canvas_y *= 2; /* now we have a square. */
-    if (canvas_x > canvas_y) (*map_y) -= 1;
-    if (canvas_x + canvas_y > NORMAL_TILE_WIDTH) (*map_x) += 1;
-
-    /* If we are outside the map find the nearest tile, with distance as
-       seen on the map. */
-    nearest_real_pos(map_x, map_y);
-  } else { /* is_isometric */
-    *map_x = map_adjust_x(map_view_x0 + canvas_x/NORMAL_TILE_WIDTH);
-    *map_y = map_adjust_y(map_view_y0 + canvas_y/NORMAL_TILE_HEIGHT);
-  }
+  base_get_map_xy(canvas_x, canvas_y, map_x, map_y, map_view_x0, map_view_y0);
 }
 
 /**************************************************************************
@@ -588,34 +486,16 @@
 }
 
 /**************************************************************************
-...
+Centers the mapview around (x, y).
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 void center_tile_mapcanvas(int x, int y)
 {
   int map_view_x0;
   int map_view_y0;
-  int map_canvas_store_twidth = get_map_x_visible();
-  int map_canvas_store_theight = get_map_y_visible();
 
-  if (is_isometric) {
-    x -= map_canvas_store_twidth/2;
-    y += map_canvas_store_twidth/2;
-    x -= map_canvas_store_theight/2;
-    y -= map_canvas_store_theight/2;
-
-    map_view_x0 = map_adjust_x(x);
-    map_view_y0 = map_adjust_y(y);
-
-    map_view_y0 =
-      (map_view_y0 > map.ysize + EXTRA_BOTTOM_ROW - map_canvas_store_theight) 
? 
-      map.ysize + EXTRA_BOTTOM_ROW - map_canvas_store_theight :
-      map_view_y0;
-  } else {
-    map_view_x0=map_adjust_x(x-map_canvas_store_twidth/2);
-    map_view_y0=map_adjust_y(y-map_canvas_store_theight/2);
-    if (map_view_y0>map.ysize+EXTRA_BOTTOM_ROW-map_canvas_store_theight)
-      
map_view_y0=map_adjust_y(map.ysize+EXTRA_BOTTOM_ROW-map_canvas_store_theight);
-  }
+  base_center_tile_mapcanvas(x, y, &map_view_x0, &map_view_y0, 
get_map_x_visible(), get_map_y_visible());
 
   set_map_xy_start(map_view_x0, map_view_y0);
 // remove me
Index: client/gui-win32/mapview.c
===================================================================
RCS file: /home/jshort/cvs/myfreeciv/client/gui-win32/mapview.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 mapview.c
--- client/gui-win32/mapview.c  2001/11/01 16:42:13     1.1.1.1
+++ client/gui-win32/mapview.c  2001/11/05 17:31:32
@@ -43,9 +43,6 @@
 #include "gui_main.h"
 #include "mapview.h"
 
-
-#define EXTRA_BOTTOM_ROW 1
-
 enum draw_part {
   D_T_L = 1, D_T_R = 2, D_M_L = 4, D_M_R = 8, D_B_L = 16, D_B_R = 32
 };
@@ -666,38 +663,15 @@
 }
 
 /**************************************************************************
+Centers the mapview around (x, y).
 
+This function is almost identical between all GUI's.
 **************************************************************************/
 void
 center_tile_mapcanvas(int x, int y)
 {
-  if (is_isometric)
-    {
-      x -= map_view_width/2;
-      y += map_view_width/2;
-      x -= map_view_height/2;
-      y -= map_view_height/2;
-      map_view_x = map_adjust_x(x);
-      map_view_y = map_adjust_y(y);
-      map_view_y = (map_view_y > map.ysize + EXTRA_BOTTOM_ROW - 
-                   map_view_height) ? 
-       map.ysize + EXTRA_BOTTOM_ROW - map_view_height :
-       map_view_y;
-      
-    }
-  else
-    {
-      int new_map_view_x0, new_map_view_y0;
-      
-      new_map_view_x0=map_adjust_x(x-map_view_width/2);
-      new_map_view_y0=map_adjust_y(y-map_view_height/2);
-      if(new_map_view_y0>map.ysize+EXTRA_BOTTOM_ROW-map_view_height)
-       new_map_view_y0=
-         map_adjust_y(map.ysize+EXTRA_BOTTOM_ROW-map_view_height);
-      
-      map_view_x=new_map_view_x0;
-      map_view_y=new_map_view_y0;
-    }
+  base_center_tile_mapcanvas(x, y, &map_view_x, &map_view_y, map_view_width, 
map_view_height);
+
   update_map_canvas_visible();
   update_map_canvas_scrollbars();
       
@@ -1236,99 +1210,25 @@
 }
 
 /**************************************************************************
+Finds the pixel coordinates of a tile.  Beside setting the results in
+canvas_x,canvas_y it returns whether the tile is inside the visible map.
 
+This function is almost identical between all GUI's.
 **************************************************************************/
 static int get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
 {
-  if (is_isometric) {
-    /* canvas_x,canvas_y is the top corner of the actual tile, not the pixmap.
-       This function also handels non-adjusted tile coords (like -1, -2) as if
-       they were adjusted.
-       You might want to first take a look at the simpler city_get_xy() for 
basic
-       understanding. */
-    int diff_xy;
-    int diff_x, diff_y;
-    int width, height;
-    width=map_win_width;
-    height=map_win_height;
-    map_x %= map.xsize;
-    if (map_x < map_view_x) map_x += map.xsize;
-    diff_xy = (map_x + map_y) - (map_view_x + map_view_y);
-    /* one diff_xy value defines a line parallel with the top of the isometric
-       view. */
-    *canvas_y = diff_xy/2 * NORMAL_TILE_HEIGHT + (diff_xy%2) * 
(NORMAL_TILE_HEIGHT/2);
-    /* changing both x and y with the same amount doesn't change the isometric
-       x value. (draw a grid to see it!) */
-    map_x -= diff_xy/2;
-    map_y -= diff_xy/2;
-    diff_x = map_x - map_view_x;
-    diff_y = map_view_y - map_y;
-    
-    *canvas_x = (diff_x - 1) * NORMAL_TILE_WIDTH
-      + (diff_x == diff_y ? NORMAL_TILE_WIDTH : NORMAL_TILE_WIDTH/2)
-      /* tiles starting above the visible area */
-      + (diff_y > diff_x ? NORMAL_TILE_WIDTH : 0);
-    
-    /* We now have the corner of the sprite. For drawing we move it. */
-    *canvas_x -= NORMAL_TILE_WIDTH/2;
-    
-    return (*canvas_x > (-NORMAL_TILE_WIDTH))
-      && *canvas_x < (width+NORMAL_TILE_WIDTH/2)
-      && (*canvas_y > (-NORMAL_TILE_HEIGHT))
-      && (*canvas_y < (height)); 
-  } else { /* is_isometric */
-    if (map_view_x+map_view_width<=map.xsize)
-      *canvas_x=map_x-map_view_x;
-    else if (map_x >= map_view_x)
-      *canvas_x=map_x-map_view_x;
-    else if (map_x < map_adjust_x(map_view_x+map_view_width))
-      *canvas_x=map_x+map.xsize-map_view_x;
-    else *canvas_x=-1;
-    *canvas_y=map_y-map_view_y;
-    *canvas_x*=NORMAL_TILE_WIDTH;
-    *canvas_y*=NORMAL_TILE_HEIGHT;
-    
-    return *canvas_x >= 0
-      && *canvas_x <(map_view_width*NORMAL_TILE_WIDTH)
-      && *canvas_y >=0
-      && *canvas_y <(map_view_height*NORMAL_TILE_HEIGHT);
-  }
+  return base_get_canvas_xy(map_x, map_y, canvas_x, canvas_y, map_view_x, 
map_view_y, map_win_width, map_win_height);
 }
 
 
 /**************************************************************************
 Finds the map coordinates corresponding to pixel coordinates.
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 void get_map_xy(int canvas_x, int canvas_y, int *map_x, int *map_y)
 {
-  if (is_isometric) {
-    *map_x = map_view_x;
-    *map_y = map_view_y;
-
-    /* first find an equivalent position on the left side of the screen. */
-    *map_x += canvas_x/NORMAL_TILE_WIDTH;
-    *map_y -= canvas_x/NORMAL_TILE_WIDTH;
-    canvas_x %= NORMAL_TILE_WIDTH;
-
-    /* Then move op to the top corner. */
-    *map_x += canvas_y/NORMAL_TILE_HEIGHT;
-    *map_y += canvas_y/NORMAL_TILE_HEIGHT;
-    canvas_y %= NORMAL_TILE_HEIGHT;
-
-    /* We are inside a rectangle, with 2 half tiles starting in the corner,
-       and two tiles further out. Draw a grid to see how this works :). */
-    assert(NORMAL_TILE_WIDTH == 2*NORMAL_TILE_HEIGHT);
-    canvas_y *= 2; /* now we have a square. */
-    if (canvas_x > canvas_y) (*map_y) -= 1;
-    if (canvas_x + canvas_y > NORMAL_TILE_WIDTH) (*map_x) += 1;
-
-    /* If we are outside the map find the nearest tile, with distance as
-       seen on the map. */
-    nearest_real_pos(map_x, map_y);
-  } else { /* is_isometric */
-    *map_x = map_adjust_x(map_view_x + canvas_x/NORMAL_TILE_WIDTH);
-    *map_y = map_adjust_y(map_view_y + canvas_y/NORMAL_TILE_HEIGHT);
-  }
+  base_get_map_xy(canvas_x, canvas_y, map_x, map_y, map_view_x, map_view_y);
 }
 
 
Index: client/gui-xaw/mapview.c
===================================================================
RCS file: /home/jshort/cvs/myfreeciv/client/gui-xaw/mapview.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 mapview.c
--- client/gui-xaw/mapview.c    2001/11/01 16:42:15     1.1.1.1
+++ client/gui-xaw/mapview.c    2001/11/05 17:31:32
@@ -49,22 +49,6 @@
 
 #include "mapview.h"
 
-/*
-The bottom row of the map was sometimes hidden.
-
-As of now the top left corner is always aligned with the tiles. This is what
-causes the problem in the first place. The ideal solution would be to align the
-window with the bottom left tiles if you tried to center the window on a tile
-closer than (screen_tiles_height/2 -1) to the south pole.
-
-But, for now, I just grepped for occurences where the ysize (or the values
-derived from it) were used, and those places that had relevance to drawing the
-map, and I added 1 (using the EXTRA_BOTTOM_ROW constant).
-
--Thue
-*/
-#define EXTRA_BOTTOM_ROW 1
-
 /* contains the x0, y0 coordinates of the upper left corner block */
 int map_view_x0, map_view_y0;
 
@@ -82,38 +66,27 @@
 int scaled_intro_pixmap_width, scaled_intro_pixmap_height;
 
 /**************************************************************************
-Finds the pixel coordinates of a tile.
-Beside setting the results in canvas_x,canvas_y it returns whether the tile
-is inside the visible map.
+Finds the pixel coordinates of a tile.  Beside setting the results in
+canvas_x,canvas_y it returns whether the tile is inside the visible map.
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 static int get_canvas_xy(int map_x, int map_y, int *canvas_x, int *canvas_y)
 {
-  if (map_view_x0+map_canvas_store_twidth <= map.xsize)
-    *canvas_x = map_x-map_view_x0;
-  else if(map_x >= map_view_x0)
-    *canvas_x = map_x-map_view_x0;
-  else if(map_x < map_adjust_x(map_view_x0+map_canvas_store_twidth))
-    *canvas_x = map_x+map.xsize-map_view_x0;
-  else *canvas_x = -1;
-
-  *canvas_y = map_y - map_view_y0;
-
-  *canvas_x *= NORMAL_TILE_WIDTH;
-  *canvas_y *= NORMAL_TILE_HEIGHT;
-
-  return *canvas_x >= 0
-      && *canvas_x < map_canvas_store_twidth * NORMAL_TILE_WIDTH
-      && *canvas_y >= 0
-      && *canvas_y < map_canvas_store_theight * NORMAL_TILE_HEIGHT;
+  int width, height;
+  XtVaGetValues(map_canvas, XtNwidth, &width, XtNheight, &height, NULL);
+
+  return base_get_canvas_xy(map_x, map_y, canvas_x, canvas_y, map_view_x0, 
map_view_y0, width, height);
 }
 
 /**************************************************************************
 Finds the map coordinates corresponding to pixel coordinates.
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 void get_map_xy(int canvas_x, int canvas_y, int *map_x, int *map_y)
 {
-  *map_x = map_adjust_x(map_view_x0 + canvas_x/NORMAL_TILE_WIDTH);
-  *map_y = map_adjust_y(map_view_y0 + canvas_y/NORMAL_TILE_HEIGHT);
+  base_get_map_xy(canvas_x, canvas_y, map_x, map_y, map_view_x0, map_view_y0);
 }
 
 /**************************************************************************
@@ -489,20 +462,13 @@
 }
 
 /**************************************************************************
-...
+Centers the mapview around (x, y).
+
+This function is almost identical between all GUI's.
 **************************************************************************/
 void center_tile_mapcanvas(int x, int y)
 {
-  int new_map_view_x0, new_map_view_y0;
-
-  new_map_view_x0=map_adjust_x(x-map_canvas_store_twidth/2);
-  new_map_view_y0=map_adjust_y(y-map_canvas_store_theight/2);
-  if(new_map_view_y0>map.ysize+EXTRA_BOTTOM_ROW-map_canvas_store_theight)
-     new_map_view_y0=
-       map_adjust_y(map.ysize+EXTRA_BOTTOM_ROW-map_canvas_store_theight);
-
-  map_view_x0=new_map_view_x0;
-  map_view_y0=new_map_view_y0;
+  base_center_tile_mapcanvas(x, y, &map_view_x0, &map_view_y0, 
map_canvas_store_twidth, map_canvas_store_theight);
 
   update_map_canvas_visible();
   update_map_canvas_scrollbars();

[Prev in Thread] Current Thread [Next in Thread]