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: Mon, 26 Nov 2001 07:10:30 -0800 (PST)

Raimar Falke wrote:

On Mon, Nov 12, 2001 at 10:46:51AM -0800, jdorje@xxxxxxxxxxxxxxxxxxxxx wrote:

OK.  The first patch will remain relatively unchanged, then (except for
the names, as above).  A new patch will follow.


Much belated, but here is the new patch.

Changes: only the names.  They're now very long and completely intelligible.

Hopefully this is now in working state to consider for CVS inclusion. However, it's only been tested on gtk, xaw, and win32 (that was the previous version; assuming I got the names right the new version should also work...).

jason
? diff
? freeciv.kdevses
? old
? rc
? topology
Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.1
diff -u -r1.1 mapview_common.c
--- client/mapview_common.c     2001/10/30 12:11:43     1.1
+++ client/mapview_common.c     2001/11/26 11:52:32
@@ -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 map_pos_to_canvas_pos(int map_x, int map_y,
+                      int *canvas_x, int *canvas_y,
+                      int map_view_topleft_map_x, int map_view_topleft_map_y,
+                      int map_view_pixel_width, int map_view_pixel_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_topleft_map_x) map_x += map.xsize;
+    diff_xy = (map_x + map_y) - (map_view_topleft_map_x + 
map_view_topleft_map_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_topleft_map_x;
+    diff_y = map_view_topleft_map_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 < (map_view_pixel_width + NORMAL_TILE_WIDTH/2)
+      && (*canvas_y > -NORMAL_TILE_HEIGHT)
+      && (*canvas_y < map_view_pixel_height);
+  } else { /* is_isometric */
+    /* map_view_map_width is the width in tiles/map positions */
+    int map_view_map_width = (map_view_pixel_width + NORMAL_TILE_WIDTH - 1) / 
NORMAL_TILE_WIDTH;
+    if (map_view_topleft_map_x+map_view_map_width <= map.xsize)
+      *canvas_x = map_x-map_view_topleft_map_x;
+    else if(map_x >= map_view_topleft_map_x)
+      *canvas_x = map_x-map_view_topleft_map_x;
+    else if(map_x < map_adjust_x(map_view_topleft_map_x+map_view_map_width))
+      *canvas_x = map_x+map.xsize-map_view_topleft_map_x;
+    else *canvas_x = -1;
+
+    *canvas_y = map_y - map_view_topleft_map_y;
+
+    *canvas_x *= NORMAL_TILE_WIDTH;
+    *canvas_y *= NORMAL_TILE_HEIGHT;
+
+    return *canvas_x >= 0
+       && *canvas_x < map_view_pixel_width
+       && *canvas_y >= 0
+       && *canvas_y < map_view_pixel_height;
+  }
+}
+
+/**************************************************************************
+Finds the map coordinates corresponding to pixel coordinates.
+**************************************************************************/
+void canvas_pos_to_map_pos(int canvas_x, int canvas_y,
+                    int *map_x, int *map_y,
+                    int map_view_topleft_map_x, int map_view_topleft_map_y)
+{
+  if (is_isometric) {
+    *map_x = map_view_topleft_map_x;
+    *map_y = map_view_topleft_map_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_topleft_map_x + canvas_x/NORMAL_TILE_WIDTH);
+    *map_y = map_adjust_y(map_view_topleft_map_y + 
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_topleft_map_x, int 
*map_view_topleft_map_y,
+                               int map_view_map_width, int map_view_map_height)
+{
+  if (is_isometric) {
+    map_x -= map_view_map_width/2;
+    map_y += map_view_map_width/2;
+    map_x -= map_view_map_height/2;
+    map_y -= map_view_map_height/2;
+
+    *map_view_topleft_map_x = map_adjust_x(map_x);
+    *map_view_topleft_map_y = map_adjust_y(map_y);
+
+    *map_view_topleft_map_y =
+      (*map_view_topleft_map_y > map.ysize + EXTRA_BOTTOM_ROW - 
map_view_map_height) ?
+      map.ysize + EXTRA_BOTTOM_ROW - map_view_map_height :
+      *map_view_topleft_map_y;
+  } else {
+    int new_map_view_x0, new_map_view_y0;
+
+    new_map_view_x0=map_adjust_x(map_x-map_view_map_width/2);
+    new_map_view_y0=map_adjust_y(map_y-map_view_map_height/2);
+    if (new_map_view_y0>map.ysize+EXTRA_BOTTOM_ROW-map_view_map_height)
+      new_map_view_y0=
+       map_adjust_y(map.ysize+EXTRA_BOTTOM_ROW-map_view_map_height);
+
+    *map_view_topleft_map_x=new_map_view_x0;
+    *map_view_topleft_map_y=new_map_view_y0;
   }
 }
Index: client/mapview_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v
retrieving revision 1.1
diff -u -r1.1 mapview_common.h
--- client/mapview_common.h     2001/10/30 12:11:43     1.1
+++ client/mapview_common.h     2001/11/26 11:52:32
@@ -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 map_pos_to_canvas_pos(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 canvas_pos_to_map_pos(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/freeciv/CVS/freeciv/client/gui-gtk/mapview.c,v
retrieving revision 1.107
diff -u -r1.107 mapview.c
--- client/gui-gtk/mapview.c    2001/10/30 12:11:44     1.107
+++ client/gui-gtk/mapview.c    2001/11/26 11:52:33
@@ -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 map_pos_to_canvas_pos(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);
-  }
+  canvas_pos_to_map_pos(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/freeciv/CVS/freeciv/client/gui-mui/mapview.c,v
retrieving revision 1.34
diff -u -r1.34 mapview.c
--- client/gui-mui/mapview.c    2001/10/30 12:11:45     1.34
+++ client/gui-mui/mapview.c    2001/11/26 11:52:33
@@ -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 map_pos_to_canvas_pos(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);
-  }
+  canvas_pos_to_map_pos(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/freeciv/CVS/freeciv/client/gui-win32/mapview.c,v
retrieving revision 1.12
diff -u -r1.12 mapview.c
--- client/gui-win32/mapview.c  2001/11/07 18:39:04     1.12
+++ client/gui-win32/mapview.c  2001/11/26 11:52:34
@@ -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
 };
@@ -671,38 +668,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();
       
@@ -1421,99 +1395,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 map_pos_to_canvas_pos(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);
-  }
+  canvas_pos_to_map_pos(canvas_x, canvas_y, map_x, map_y, map_view_x, 
map_view_y);
 }
 
 
Index: client/gui-xaw/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapview.c,v
retrieving revision 1.87
diff -u -r1.87 mapview.c
--- client/gui-xaw/mapview.c    2001/10/30 12:11:46     1.87
+++ client/gui-xaw/mapview.c    2001/11/26 11:52:35
@@ -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 map_pos_to_canvas_pos(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);
+  canvas_pos_to_map_pos(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]