Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2003:
[Freeciv-Dev] (PR#3971) serious cleanup to map overview
Home

[Freeciv-Dev] (PR#3971) serious cleanup to map overview

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#3971) serious cleanup to map overview
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 10 Apr 2003 01:05:17 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Attached is a preliminary patch for a significant cleanup to the
overview code.

Under the current system, each time an operation is done on the map
overview the alignment of the overview is recalculated.  This takes a
lot of extra code, and is error-prone.  It also means the alignment must
always be done the same, simple way - something like only re-aligning
the overview when necessary (to get the fewest recenterings of the
overview) would be impossible.

I ran into significant problems when trying to generalize this for
topologies that wrap in different directions.  Ross's code (an extension
of the current system) is just too complicated for my taste.  So I
completely rethought the algorithm...into something much simpler and
more comprehensible.

Under the proposed system, we track the origin of the overview "window"
(currently just a map_overview_x0 value, although under gen-topologies
this becomes a native position and a y0 value is needed as well).  This
value is recalculated only when the mapview is recentered (via
center_tile_mapcanvas), and is much simpler for the gui code to use.

This is a preliminary patch, because only gui-gtk-2.0 support has been
written.  Pending any redesign of the basic system, I will write support
for the other GUIs.

jason

Index: client/mapview_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.c,v
retrieving revision 1.42
diff -u -r1.42 mapview_common.c
--- client/mapview_common.c     2003/04/07 06:29:56     1.42
+++ client/mapview_common.c     2003/04/10 08:02:57
@@ -33,6 +33,8 @@
 
 struct canvas mapview_canvas;
 
+int map_overview_x0;
+
 /**************************************************************************
  Refreshes a single tile on the map canvas.
 **************************************************************************/
@@ -308,10 +310,22 @@
 }
 
 /**************************************************************************
+  Centers the overview around (map_x, map_y).
+**************************************************************************/
+static void center_tile_overviewcanvas(int map_x, int map_y)
+{
+  map_overview_x0 = (map_x + map.xsize / 2) % map.xsize;
+
+  refresh_overview_viewrect();
+}
+
+/**************************************************************************
   Centers the mapview around (map_x, map_y).
 **************************************************************************/
 void center_tile_mapcanvas(int map_x, int map_y)
 {
+  int map_center_x = map_x, map_center_y = map_y;
+
   /* Find top-left corner. */
   if (is_isometric) {
     map_x -= mapview_canvas.tile_width / 2;
@@ -336,7 +350,7 @@
   mapview_canvas.map_y0 = map_y;
   update_map_canvas_visible();
   update_map_canvas_scrollbars();
-  refresh_overview_viewrect();
+  center_tile_overviewcanvas(map_center_x, map_center_y);
   if (hover_state == HOVER_GOTO || hover_state == HOVER_PATROL) {
     create_line_at_mouse_pos();
   }
Index: client/mapview_common.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/mapview_common.h,v
retrieving revision 1.28
diff -u -r1.28 mapview_common.h
--- client/mapview_common.h     2003/04/03 04:13:48     1.28
+++ client/mapview_common.h     2003/04/10 08:02:57
@@ -31,6 +31,8 @@
 
 extern struct canvas mapview_canvas;
 
+extern int map_overview_x0;
+
 /*
 The bottom row of the map was sometimes hidden.
 
Index: client/gui-gtk-2.0/mapctrl.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/mapctrl.c,v
retrieving revision 1.22
diff -u -r1.22 mapctrl.c
--- client/gui-gtk-2.0/mapctrl.c        2003/04/09 20:47:44     1.22
+++ client/gui-gtk-2.0/mapctrl.c        2003/04/10 08:02:57
@@ -357,16 +357,7 @@
   if (ev->type != GDK_BUTTON_PRESS)
     return TRUE; /* Double-clicks? Triple-clicks? No thanks! */
 
-  if (is_isometric) {
-    xtile = ev->x / OVERVIEW_TILE_WIDTH
-           - (map.xsize / 2 - (map_view_x0
-                               + (map_canvas_store_twidth +
-                                  map_canvas_store_theight) / 2));
-  } else {
-    xtile = ev->x / OVERVIEW_TILE_WIDTH - (map.xsize / 2 -
-                                          (map_view_x0
-                                           + map_canvas_store_twidth / 2));
-  }
+  xtile = ev->x / OVERVIEW_TILE_WIDTH + map_overview_x0;
   ytile = ev->y / OVERVIEW_TILE_HEIGHT;
 
   if (can_client_change_view() && (ev->button == 3)) {
Index: client/gui-gtk-2.0/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/mapview.c,v
retrieving revision 1.53
diff -u -r1.53 mapview.c
--- client/gui-gtk-2.0/mapview.c        2003/04/09 20:47:44     1.53
+++ client/gui-gtk-2.0/mapview.c        2003/04/10 08:02:59
@@ -457,26 +457,17 @@
 **************************************************************************/
 void overview_update_tile(int x, int y)
 {
-  int screen_width, pos;
+  int gui_x = (x - map_overview_x0 + map.xsize) % map.xsize;
+  int gui_y = y;
 
-  if (is_isometric) {
-    screen_width = map_canvas_store_twidth + map_canvas_store_theight;
-  } else {
-    screen_width = map_canvas_store_twidth;
-  }
-  pos = x + map.xsize/2 - (map_view_x0 + screen_width/2);
-  
-  pos %= map.xsize;
-  if (pos < 0)
-    pos += map.xsize;
-  
   set_overview_tile_foreground_color(x, y);
   gdk_draw_rectangle(overview_canvas_store, fill_bg_gc, TRUE,
                     OVERVIEW_TILE_WIDTH * x, OVERVIEW_TILE_HEIGHT * y,
                     OVERVIEW_TILE_WIDTH, OVERVIEW_TILE_HEIGHT);
   
   gdk_draw_rectangle(overview_canvas->window, fill_bg_gc, TRUE,
-                    OVERVIEW_TILE_WIDTH * pos, OVERVIEW_TILE_HEIGHT * y,
+                    OVERVIEW_TILE_WIDTH * gui_x,
+                    OVERVIEW_TILE_HEIGHT * gui_y,
                     OVERVIEW_TILE_WIDTH, OVERVIEW_TILE_HEIGHT);
 }
 
@@ -485,43 +476,30 @@
 **************************************************************************/
 void refresh_overview_viewrect(void)
 {
-  int screen_width, delta;
-  if (is_isometric) {
-    screen_width = map_canvas_store_twidth + map_canvas_store_theight;
-  } else {
-    screen_width = map_canvas_store_twidth;
+  int x0 = OVERVIEW_TILE_WIDTH * map_overview_x0;
+  int x1 = OVERVIEW_TILE_WIDTH * (map.xsize - map_overview_x0);
+  int dy = OVERVIEW_TILE_HEIGHT * map.ysize;
+  int gui_x, gui_y;
+
+  /* Copy the part of the overview to the right of map_overview_x0. */
+  gdk_draw_pixmap(overview_canvas->window, civ_gc, overview_canvas_store,
+                 x0, 0, 0, 0, x1, dy);
+  if (map_overview_x0 > 0) {
+    /* Copy the part of the overview to the left of map_overview_x0. */
+    gdk_draw_pixmap(overview_canvas->window, civ_gc, overview_canvas_store,
+                   0, 0, x1, 0, x0, dy);
   }
-  delta = map.xsize/2 - (map_view_x0 + screen_width/2);
 
-  if (delta>=0) {
-    gdk_draw_pixmap( overview_canvas->window, civ_gc, overview_canvas_store,
-               0, 0, OVERVIEW_TILE_WIDTH * delta, 0,
-               overview_canvas_store_width - OVERVIEW_TILE_WIDTH * delta,
-               overview_canvas_store_height );
-    gdk_draw_pixmap( overview_canvas->window, civ_gc, overview_canvas_store,
-               overview_canvas_store_width - OVERVIEW_TILE_WIDTH * delta, 0,
-               0, 0,
-               OVERVIEW_TILE_WIDTH * delta, overview_canvas_store_height);
-  } else {
-    gdk_draw_pixmap( overview_canvas->window, civ_gc, overview_canvas_store,
-               -OVERVIEW_TILE_WIDTH * delta, 0,
-               0, 0,
-               overview_canvas_store_width + OVERVIEW_TILE_WIDTH *delta,
-               overview_canvas_store_height );
-
-    gdk_draw_pixmap( overview_canvas->window, civ_gc, overview_canvas_store,
-               0, 0,
-               overview_canvas_store_width + OVERVIEW_TILE_WIDTH * delta, 0,
-               -OVERVIEW_TILE_WIDTH * delta, overview_canvas_store_height);
-  }
+  /* Find the origin of the mapview, in overview (gui) coordinates. */
+  gui_x = (mapview_canvas.map_x0 - map_overview_x0 + map.xsize) % map.xsize;
+  gui_y = mapview_canvas.map_y0;
 
   gdk_gc_set_foreground( civ_gc, colors_standard[COLOR_STD_WHITE] );
   
   if (is_isometric) {
-    /* The x's and y's are in overview coordinates. */
-    int Wx = overview_canvas_store_width / 2
-           - OVERVIEW_TILE_WIDTH * screen_width / 2;
-    int Wy = OVERVIEW_TILE_HEIGHT * map_view_y0;
+    /* The x's and y's are in overview canvas coordinates. */
+    int Wx = OVERVIEW_TILE_WIDTH * gui_x;
+    int Wy = OVERVIEW_TILE_HEIGHT * gui_y;
     int Nx = Wx + OVERVIEW_TILE_WIDTH * map_canvas_store_twidth;
     int Ny = Wy - OVERVIEW_TILE_HEIGHT * map_canvas_store_twidth;
     int Sx = Wx + OVERVIEW_TILE_WIDTH * map_canvas_store_theight;
@@ -549,9 +527,8 @@
                  Sx, Sy, Wx, Wy);
   } else {
     gdk_draw_rectangle(overview_canvas->window, civ_gc, FALSE,
-                      (overview_canvas_store_width
-                       - OVERVIEW_TILE_WIDTH * map_canvas_store_twidth) / 2,
-                      OVERVIEW_TILE_HEIGHT * map_view_y0,
+                      OVERVIEW_TILE_WIDTH * gui_x,
+                      OVERVIEW_TILE_HEIGHT * gui_y,
                       OVERVIEW_TILE_WIDTH * map_canvas_store_twidth,
                       OVERVIEW_TILE_HEIGHT * map_canvas_store_theight - 1);
   }

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#3971) serious cleanup to map overview, Jason Short <=