Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2003:
[Freeciv-Dev] (PR#3997) Add map_to_overview_pos/overview_to_map_pos to m
Home

[Freeciv-Dev] (PR#3997) Add map_to_overview_pos/overview_to_map_pos to m

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#3997) Add map_to_overview_pos/overview_to_map_pos to mapview_common
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 13 Apr 2003 23:07:42 -0700
Reply-to: rt@xxxxxxxxxxxxxx

This patch is an intermediate step in the cleanup of the overview code 
(PR#3971).  It simply introduces two new functions (overview_to_map_pos 
and map_to_overview_pos) in mapview_common, and calls these from the GUI 
code when conversion logic is needed.

- Some logic (including the ugliest stuff) is still embedded in the GUI 
in the buffer-copying code.

- The use of map_adjust_x (replacing manual wrapping) emphasizes that 
this code is not topology-clean.  But at least the number of such places 
has been reduced.

- One buglet is fixed: overview_to_map_pos will now return a normalized 
position.

- This additionally removes one piece of iso-view logic from the GUI code.

- It is tested under some, but not all, clients.

- Note that not all clients use the functions.  I'm not quite sure what 
they do instead.

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/14 04:55:23
@@ -1111,3 +1111,44 @@
     *growth_color = COLOR_STD_WHITE;
   }
 }
+
+/**************************************************************************
+  Return the map coordinates of the origin (top-left) corner of the
+  overview window.  Currently this is calculated on demand.
+**************************************************************************/
+static int get_overview_x0(void)
+{
+  int screen_width;
+
+  if (is_isometric) {
+    screen_width = mapview_canvas.tile_width + mapview_canvas.tile_height;
+  } else {
+    screen_width = mapview_canvas.tile_width;
+  }
+
+  return (mapview_canvas.map_x0 + screen_width / 2) - map.xsize / 2;
+}
+
+/**************************************************************************
+  Finds the overview (canvas) coordinates for a given map position.
+**************************************************************************/
+void map_to_overview_pos(int *overview_x, int *overview_y,
+                        int map_x, int map_y)
+{
+  int overview_x0 = get_overview_x0();
+
+  *overview_x = OVERVIEW_TILE_WIDTH * map_adjust_x(map_x - overview_x0);
+  *overview_y = OVERVIEW_TILE_HEIGHT * map_y;
+}
+
+/**************************************************************************
+  Finds the map coordinates for a given overview (canvas) position.
+**************************************************************************/
+void overview_to_map_pos(int *map_x, int *map_y,
+                        int overview_x, int overview_y)
+{
+  int overview_x0 = get_overview_x0();
+
+  *map_x = map_adjust_x(overview_x / OVERVIEW_TILE_WIDTH + overview_x0);
+  *map_y = overview_y / OVERVIEW_TILE_HEIGHT;
+}
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/14 04:55:23
@@ -174,4 +174,9 @@
 void queue_mapview_update(enum update_type update);
 void unqueue_mapview_updates(void);
 
+void map_to_overview_pos(int *overview_x, int *overview_y,
+                        int map_x, int map_y);
+void overview_to_map_pos(int *map_x, int *map_y,
+                        int overview_x, int overview_y);
+
 #endif /* FC__MAPVIEW_COMMON_H */
Index: client/gui-gtk/mapctrl.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/mapctrl.c,v
retrieving revision 1.83
diff -u -r1.83 mapctrl.c
--- client/gui-gtk/mapctrl.c    2003/04/12 06:15:18     1.83
+++ client/gui-gtk/mapctrl.c    2003/04/14 04:55:27
@@ -361,17 +361,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));
-  }
-  ytile = ev->y / OVERVIEW_TILE_HEIGHT;
+  overview_to_map_pos(&xtile, &ytile, ev->x, ev->y);
   
   if (can_client_change_view() && ev->button == 3) {
     center_tile_mapcanvas(xtile, ytile);
Index: client/gui-gtk/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/mapview.c,v
retrieving revision 1.166
diff -u -r1.166 mapview.c
--- client/gui-gtk/mapview.c    2003/04/09 20:47:43     1.166
+++ client/gui-gtk/mapview.c    2003/04/14 04:55:36
@@ -448,26 +448,17 @@
 **************************************************************************/
 void overview_update_tile(int x, int y)
 {
-  int screen_width, pos;
+  int overview_x, overview_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);
+  map_to_overview_pos(&overview_x, &overview_y, x, y);
   
-  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_x, overview_y,
                     OVERVIEW_TILE_WIDTH, OVERVIEW_TILE_HEIGHT);
 }
 
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.23
diff -u -r1.23 mapctrl.c
--- client/gui-gtk-2.0/mapctrl.c        2003/04/12 06:15:18     1.23
+++ client/gui-gtk-2.0/mapctrl.c        2003/04/14 04:55:36
@@ -353,17 +353,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));
-  }
-  ytile = ev->y / OVERVIEW_TILE_HEIGHT;
+  overview_to_map_pos(&xtile, &ytile, ev->x, ev->y);
 
   if (can_client_change_view() && (ev->button == 3)) {
     center_tile_mapcanvas(xtile, ytile);
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/14 04:55:37
@@ -457,26 +457,17 @@
 **************************************************************************/
 void overview_update_tile(int x, int y)
 {
-  int screen_width, pos;
+  int overview_x, overview_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;
-  
+  map_to_overview_pos(&overview_x, &overview_y, x, y);
+
   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_x, overview_y,
                     OVERVIEW_TILE_WIDTH, OVERVIEW_TILE_HEIGHT);
 }
 
Index: client/gui-win32/mapctrl.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/mapctrl.c,v
retrieving revision 1.26
diff -u -r1.26 mapctrl.c
--- client/gui-win32/mapctrl.c  2003/04/12 06:15:18     1.26
+++ client/gui-win32/mapctrl.c  2003/04/14 04:55:41
@@ -314,20 +314,13 @@
 void overview_handle_rbut(int x, int y)
 {
  int xtile, ytile;        
- if (is_isometric) {
-   xtile = x / OVERVIEW_TILE_WIDTH
-          - (map.xsize / 2 - (map_view_x + (map_view_width
-                                            + map_view_height) / 2));
- } else {
-   xtile = x / OVERVIEW_TILE_WIDTH
-          - (map.xsize / 2 - (map_view_x + map_view_width / 2));
- }
-
- ytile = y / OVERVIEW_TILE_HEIGHT;
 
  if (!can_client_change_view()) {
    return;
  }
+
+ overview_to_map_pos(&xtile, &ytile, x, y);
+
  center_tile_mapcanvas(xtile,ytile); 
 
 }
Index: client/gui-win32/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/mapview.c,v
retrieving revision 1.66
diff -u -r1.66 mapview.c
--- client/gui-win32/mapview.c  2003/04/09 20:47:44     1.66
+++ client/gui-win32/mapview.c  2003/04/14 04:55:42
@@ -465,13 +465,9 @@
 {
   HDC hdc;
   RECT rc;
-  int screen_width=is_isometric?map_view_width+map_view_height:map_view_width;
-  int pos = x + map.xsize/2 - (map_view_x + screen_width/2);
-  
-  pos %= map.xsize;
-  if (pos < 0)
-    pos += map.xsize;
-  
+  int overview_x, overview_y;
+
+  map_to_overview_pos(&overview_x, &overview_y, x, y);
  
   rc.left = OVERVIEW_TILE_WIDTH * x;
   rc.right = rc.left + OVERVIEW_TILE_WIDTH;
@@ -480,8 +476,8 @@
   FillRect(overviewstoredc,&rc,brush_std[overview_tile_color(x, y)]);
 
   hdc=GetDC(root_window);
-  rc.left = OVERVIEW_TILE_WIDTH * pos + overview_win_x;
-  rc.top = OVERVIEW_TILE_HEIGHT * y + overview_win_y;
+  rc.left = overview_x + overview_win_x;
+  rc.top = overview_y + overview_win_y;
   rc.right = rc.left + OVERVIEW_TILE_WIDTH;
   rc.bottom = rc.top + OVERVIEW_TILE_HEIGHT;
   FillRect(hdc,&rc,brush_std[overview_tile_color(x,y)]);
@@ -883,7 +879,7 @@
 refresh_overview_canvas(void)
 {
   HDC hdc=GetDC(root_window);
-  int pos;
+  int overview_x, overview_y;
   RECT rc;
 
   whole_map_iterate(x, y) {
@@ -892,14 +888,12 @@
     rc.top = OVERVIEW_TILE_HEIGHT * y;
     rc.bottom = rc.top + OVERVIEEW_TILE_HEIGHT;
     FillRect(overviewstoredc, &rc, brush_std[overview_tile_color(x, y)]);
+
+    map_to_overview_pos(&overview_x, &overview_y, x, y);
 
-    pos = x + map.xsize / 2 - (map_view_x + map_win_width / 2);
-    pos %= map.xsize;
-    if (pos < 0)
-      pos += map.xsize;
-    rc.left = overview_win_x + OVERVIEW_TILE_WIDTH * pos;
+    rc.left = overview_win_x + overview_x;
     rc.right = rc.left + OVERVIEW_TILE_WIDTH;
-    rc.top = overview_win_y + OVERVIEW_TILE_HEIGHT * y;
+    rc.top = overview_win_y + overview_y;
     rc.bottom = rc.top + OVERVIEW_TILE_HEIGHT;
     FillRect(hdc, &rc, brush_std[overview_tile_color(x, y)]);
   } whole_map_iterate_end;
Index: client/gui-xaw/mapctrl.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapctrl.c,v
retrieving revision 1.71
diff -u -r1.71 mapctrl.c
--- client/gui-xaw/mapctrl.c    2003/04/12 06:15:18     1.71
+++ client/gui-xaw/mapctrl.c    2003/04/14 04:55:42
@@ -314,16 +314,12 @@
 void mapctrl_btn_overviewcanvas(XEvent *event)
 {
   int xtile, ytile;
-  XButtonEvent *ev=&event->xbutton;
 
-  xtile = ev->x / OVERVIEW_TILE_WIDTH - (map.xsize / 2
-                                        - (map_view_x0
-                                           + map_canvas_store_twidth / 2));
-  ytile=ev->y / OVERVIEW_TILE_HEIGHT;
-
   if (!can_client_change_view()) {
     return;
   }
+
+  overview_to_map_pos(&xtile, &ytile, event->xbutton.x, event->xbutton.y);
 
   if(ev->button==Button1)
     do_unit_goto(xtile,ytile);
Index: client/gui-xaw/mapview.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/mapview.c,v
retrieving revision 1.132
diff -u -r1.132 mapview.c
--- client/gui-xaw/mapview.c    2003/04/09 20:47:44     1.132
+++ client/gui-xaw/mapview.c    2003/04/14 04:55:43
@@ -413,19 +413,17 @@
 **************************************************************************/
 void overview_update_tile(int x, int y)
 {
-  int pos = x + map.xsize/2 - (map_view_x0 + map_canvas_store_twidth/2);
+  int overview_x, overview_y;
+
+  map_to_overview_pos(&overview_x, &overview_y, x, y);
   
-  pos %= map.xsize;
-  if (pos < 0)
-    pos += map.xsize;
-  
   set_overview_tile_foreground_color(x, y);
   XFillRectangle(display, overview_canvas_store, fill_bg_gc,
                 OVERVIEW_TILE_WIDTH * x, OVERVIEW_TILE_HEIGHT * y,
                 OVERVIEW_TILE_WIDTH, OVERVIEW_TILE_HEIGHT);
   
   XFillRectangle(display, XtWindow(overview_canvas), fill_bg_gc, 
-                OVERVIEW_TILE_WIDTH * pos, OVERVIEW_TILE_HEIGHT * y,
+                overview_x, overview_y,
                 OVERVIEW_TILE_WIDTH, OVERVIEW_TILE_HEIGHT);
 }
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#3997) Add map_to_overview_pos/overview_to_map_pos to mapview_common, Jason Short <=